blob: cc1487145d33e213e987c918057358cfe06b6319 [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
6 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
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>
37#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080038#include <linux/ftrace_event.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050039#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020040#include <linux/mm_types.h>
Li Zefan877c6852013-03-05 11:38:08 +080041#include <linux/cgroup.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>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020045
Frederic Weisbecker76369132011-05-19 19:55:04 +020046#include "internal.h"
47
Ingo Molnarcdd6c482009-09-21 12:02:48 +020048#include <asm/irq_regs.h>
49
Jiri Olsafadfe7b2014-08-01 14:33:02 +020050static struct workqueue_struct *perf_wq;
51
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010052struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020053 struct task_struct *p;
54 int (*func)(void *info);
55 void *info;
56 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010057};
58
59static void remote_function(void *data)
60{
61 struct remote_function_call *tfc = data;
62 struct task_struct *p = tfc->p;
63
64 if (p) {
65 tfc->ret = -EAGAIN;
66 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
67 return;
68 }
69
70 tfc->ret = tfc->func(tfc->info);
71}
72
73/**
74 * task_function_call - call a function on the cpu on which a task runs
75 * @p: the task to evaluate
76 * @func: the function to be called
77 * @info: the function call argument
78 *
79 * Calls the function @func when the task is currently running. This might
80 * be on the current CPU, which just calls the function directly
81 *
82 * returns: @func return value, or
83 * -ESRCH - when the process isn't running
84 * -EAGAIN - when the process moved away
85 */
86static int
87task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
88{
89 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020090 .p = p,
91 .func = func,
92 .info = info,
93 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010094 };
95
96 if (task_curr(p))
97 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
98
99 return data.ret;
100}
101
102/**
103 * cpu_function_call - call a function on the cpu
104 * @func: the function to be called
105 * @info: the function call argument
106 *
107 * Calls the function @func on the remote cpu.
108 *
109 * returns: @func return value or -ENXIO when the cpu is offline
110 */
111static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
112{
113 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200114 .p = NULL,
115 .func = func,
116 .info = info,
117 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100118 };
119
120 smp_call_function_single(cpu, remote_function, &data, 1);
121
122 return data.ret;
123}
124
Jiri Olsaf8697762014-08-01 14:33:01 +0200125#define EVENT_OWNER_KERNEL ((void *) -1)
126
127static bool is_kernel_event(struct perf_event *event)
128{
129 return event->owner == EVENT_OWNER_KERNEL;
130}
131
Stephane Eraniane5d13672011-02-14 11:20:01 +0200132#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
133 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100134 PERF_FLAG_PID_CGROUP |\
135 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200136
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100137/*
138 * branch priv levels that need permission checks
139 */
140#define PERF_SAMPLE_BRANCH_PERM_PLM \
141 (PERF_SAMPLE_BRANCH_KERNEL |\
142 PERF_SAMPLE_BRANCH_HV)
143
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200144enum event_type_t {
145 EVENT_FLEXIBLE = 0x1,
146 EVENT_PINNED = 0x2,
147 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
148};
149
Stephane Eraniane5d13672011-02-14 11:20:01 +0200150/*
151 * perf_sched_events : >0 events exist
152 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
153 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100154struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200155static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Stephane Eraniand010b332012-02-09 23:21:00 +0100156static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200157
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200158static atomic_t nr_mmap_events __read_mostly;
159static atomic_t nr_comm_events __read_mostly;
160static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200161static atomic_t nr_freq_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200162
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200163static LIST_HEAD(pmus);
164static DEFINE_MUTEX(pmus_lock);
165static struct srcu_struct pmus_srcu;
166
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200167/*
168 * perf event paranoia level:
169 * -1 - not paranoid at all
170 * 0 - disallow raw tracepoint access for unpriv
171 * 1 - disallow cpu events for unpriv
172 * 2 - disallow kernel profiling for unpriv
173 */
174int sysctl_perf_event_paranoid __read_mostly = 1;
175
Frederic Weisbecker20443382011-03-31 03:33:29 +0200176/* Minimum for 512 kiB + 1 user control page */
177int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200178
179/*
180 * max perf event sample rate
181 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700182#define DEFAULT_MAX_SAMPLE_RATE 100000
183#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
184#define DEFAULT_CPU_TIME_MAX_PERCENT 25
185
186int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
187
188static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
189static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
190
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200191static int perf_sample_allowed_ns __read_mostly =
192 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700193
194void update_perf_cpu_limits(void)
195{
196 u64 tmp = perf_sample_period_ns;
197
198 tmp *= sysctl_perf_cpu_time_max_percent;
Stephane Eraniane5302922013-07-05 00:30:11 +0200199 do_div(tmp, 100);
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200200 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
Dave Hansen14c63f12013-06-21 08:51:36 -0700201}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100202
Stephane Eranian9e630202013-04-03 14:21:33 +0200203static int perf_rotate_context(struct perf_cpu_context *cpuctx);
204
Peter Zijlstra163ec432011-02-16 11:22:34 +0100205int perf_proc_update_handler(struct ctl_table *table, int write,
206 void __user *buffer, size_t *lenp,
207 loff_t *ppos)
208{
Knut Petersen723478c2013-09-25 14:29:37 +0200209 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100210
211 if (ret || !write)
212 return ret;
213
214 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700215 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
216 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100217
218 return 0;
219}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200220
Dave Hansen14c63f12013-06-21 08:51:36 -0700221int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
222
223int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
224 void __user *buffer, size_t *lenp,
225 loff_t *ppos)
226{
227 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
228
229 if (ret || !write)
230 return ret;
231
232 update_perf_cpu_limits();
233
234 return 0;
235}
236
237/*
238 * perf samples are done in some very critical code paths (NMIs).
239 * If they take too much CPU time, the system can lock up and not
240 * get any real work done. This will drop the sample rate when
241 * we detect that events are taking too long.
242 */
243#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200244static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700245
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100246static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700247{
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100248 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Dave Hansen14c63f12013-06-21 08:51:36 -0700249 u64 avg_local_sample_len;
Stephane Eraniane5302922013-07-05 00:30:11 +0200250 u64 local_samples_len;
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100251
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500252 local_samples_len = __this_cpu_read(running_sample_length);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100253 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
254
255 printk_ratelimited(KERN_WARNING
256 "perf interrupt took too long (%lld > %lld), lowering "
257 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100258 avg_local_sample_len, allowed_ns >> 1,
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100259 sysctl_perf_event_sample_rate);
260}
261
262static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
263
264void perf_sample_event_took(u64 sample_len_ns)
265{
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200266 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100267 u64 avg_local_sample_len;
268 u64 local_samples_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700269
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200270 if (allowed_ns == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700271 return;
272
273 /* decay the counter by 1 average sample */
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500274 local_samples_len = __this_cpu_read(running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700275 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
276 local_samples_len += sample_len_ns;
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500277 __this_cpu_write(running_sample_length, local_samples_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700278
279 /*
280 * note: this will be biased artifically low until we have
281 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
282 * from having to maintain a count.
283 */
284 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
285
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200286 if (avg_local_sample_len <= allowed_ns)
Dave Hansen14c63f12013-06-21 08:51:36 -0700287 return;
288
289 if (max_samples_per_tick <= 1)
290 return;
291
292 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
293 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
294 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
295
Dave Hansen14c63f12013-06-21 08:51:36 -0700296 update_perf_cpu_limits();
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100297
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100298 if (!irq_work_queue(&perf_duration_work)) {
299 early_printk("perf interrupt took too long (%lld > %lld), lowering "
300 "kernel.perf_event_max_sample_rate to %d\n",
301 avg_local_sample_len, allowed_ns >> 1,
302 sysctl_perf_event_sample_rate);
303 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700304}
305
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200306static atomic64_t perf_event_id;
307
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200308static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
309 enum event_type_t event_type);
310
311static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200312 enum event_type_t event_type,
313 struct task_struct *task);
314
315static void update_context_time(struct perf_event_context *ctx);
316static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200317
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200318void __weak perf_event_print_debug(void) { }
319
Matt Fleming84c79912010-10-03 21:41:13 +0100320extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200321{
Matt Fleming84c79912010-10-03 21:41:13 +0100322 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200323}
324
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200325static inline u64 perf_clock(void)
326{
327 return local_clock();
328}
329
Stephane Eraniane5d13672011-02-14 11:20:01 +0200330static inline struct perf_cpu_context *
331__get_cpu_context(struct perf_event_context *ctx)
332{
333 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
334}
335
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200336static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
337 struct perf_event_context *ctx)
338{
339 raw_spin_lock(&cpuctx->ctx.lock);
340 if (ctx)
341 raw_spin_lock(&ctx->lock);
342}
343
344static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
345 struct perf_event_context *ctx)
346{
347 if (ctx)
348 raw_spin_unlock(&ctx->lock);
349 raw_spin_unlock(&cpuctx->ctx.lock);
350}
351
Stephane Eraniane5d13672011-02-14 11:20:01 +0200352#ifdef CONFIG_CGROUP_PERF
353
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200354/*
Li Zefan877c6852013-03-05 11:38:08 +0800355 * perf_cgroup_info keeps track of time_enabled for a cgroup.
356 * This is a per-cpu dynamically allocated data structure.
357 */
358struct perf_cgroup_info {
359 u64 time;
360 u64 timestamp;
361};
362
363struct perf_cgroup {
364 struct cgroup_subsys_state css;
Namhyung Kim86e213e2013-03-18 18:56:34 +0900365 struct perf_cgroup_info __percpu *info;
Li Zefan877c6852013-03-05 11:38:08 +0800366};
367
368/*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200369 * Must ensure cgroup is pinned (css_get) before calling
370 * this function. In other words, we cannot call this function
371 * if there is no cgroup event for the current CPU context.
372 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200373static inline struct perf_cgroup *
374perf_cgroup_from_task(struct task_struct *task)
375{
Tejun Heo073219e2014-02-08 10:36:58 -0500376 return container_of(task_css(task, perf_event_cgrp_id),
Tejun Heo8af01f52013-08-08 20:11:22 -0400377 struct perf_cgroup, css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200378}
379
380static inline bool
381perf_cgroup_match(struct perf_event *event)
382{
383 struct perf_event_context *ctx = event->ctx;
384 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
385
Tejun Heoef824fa2013-04-08 19:00:38 -0700386 /* @event doesn't care about cgroup */
387 if (!event->cgrp)
388 return true;
389
390 /* wants specific cgroup scope but @cpuctx isn't associated with any */
391 if (!cpuctx->cgrp)
392 return false;
393
394 /*
395 * Cgroup scoping is recursive. An event enabled for a cgroup is
396 * also enabled for all its descendant cgroups. If @cpuctx's
397 * cgroup is a descendant of @event's (the test covers identity
398 * case), it's a match.
399 */
400 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
401 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200402}
403
Stephane Eraniane5d13672011-02-14 11:20:01 +0200404static inline void perf_detach_cgroup(struct perf_event *event)
405{
Zefan Li4e2ba652014-09-19 16:53:14 +0800406 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200407 event->cgrp = NULL;
408}
409
410static inline int is_cgroup_event(struct perf_event *event)
411{
412 return event->cgrp != NULL;
413}
414
415static inline u64 perf_cgroup_event_time(struct perf_event *event)
416{
417 struct perf_cgroup_info *t;
418
419 t = per_cpu_ptr(event->cgrp->info, event->cpu);
420 return t->time;
421}
422
423static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
424{
425 struct perf_cgroup_info *info;
426 u64 now;
427
428 now = perf_clock();
429
430 info = this_cpu_ptr(cgrp->info);
431
432 info->time += now - info->timestamp;
433 info->timestamp = now;
434}
435
436static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
437{
438 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
439 if (cgrp_out)
440 __update_cgrp_time(cgrp_out);
441}
442
443static inline void update_cgrp_time_from_event(struct perf_event *event)
444{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200445 struct perf_cgroup *cgrp;
446
Stephane Eraniane5d13672011-02-14 11:20:01 +0200447 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200448 * ensure we access cgroup data only when needed and
449 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200450 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200451 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200452 return;
453
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200454 cgrp = perf_cgroup_from_task(current);
455 /*
456 * Do not update time when cgroup is not active
457 */
458 if (cgrp == event->cgrp)
459 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200460}
461
462static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200463perf_cgroup_set_timestamp(struct task_struct *task,
464 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200465{
466 struct perf_cgroup *cgrp;
467 struct perf_cgroup_info *info;
468
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200469 /*
470 * ctx->lock held by caller
471 * ensure we do not access cgroup data
472 * unless we have the cgroup pinned (css_get)
473 */
474 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200475 return;
476
477 cgrp = perf_cgroup_from_task(task);
478 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200479 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200480}
481
482#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
483#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
484
485/*
486 * reschedule events based on the cgroup constraint of task.
487 *
488 * mode SWOUT : schedule out everything
489 * mode SWIN : schedule in based on cgroup for next
490 */
491void perf_cgroup_switch(struct task_struct *task, int mode)
492{
493 struct perf_cpu_context *cpuctx;
494 struct pmu *pmu;
495 unsigned long flags;
496
497 /*
498 * disable interrupts to avoid geting nr_cgroup
499 * changes via __perf_event_disable(). Also
500 * avoids preemption.
501 */
502 local_irq_save(flags);
503
504 /*
505 * we reschedule only in the presence of cgroup
506 * constrained events.
507 */
508 rcu_read_lock();
509
510 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200511 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200512 if (cpuctx->unique_pmu != pmu)
513 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200514
Stephane Eraniane5d13672011-02-14 11:20:01 +0200515 /*
516 * perf_cgroup_events says at least one
517 * context on this CPU has cgroup events.
518 *
519 * ctx->nr_cgroups reports the number of cgroup
520 * events for a context.
521 */
522 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200523 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
524 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200525
526 if (mode & PERF_CGROUP_SWOUT) {
527 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
528 /*
529 * must not be done before ctxswout due
530 * to event_filter_match() in event_sched_out()
531 */
532 cpuctx->cgrp = NULL;
533 }
534
535 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200536 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200537 /*
538 * set cgrp before ctxsw in to allow
539 * event_filter_match() to not have to pass
540 * task around
Stephane Eraniane5d13672011-02-14 11:20:01 +0200541 */
542 cpuctx->cgrp = perf_cgroup_from_task(task);
543 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
544 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200545 perf_pmu_enable(cpuctx->ctx.pmu);
546 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200547 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200548 }
549
550 rcu_read_unlock();
551
552 local_irq_restore(flags);
553}
554
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200555static inline void perf_cgroup_sched_out(struct task_struct *task,
556 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200557{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200558 struct perf_cgroup *cgrp1;
559 struct perf_cgroup *cgrp2 = NULL;
560
561 /*
562 * we come here when we know perf_cgroup_events > 0
563 */
564 cgrp1 = perf_cgroup_from_task(task);
565
566 /*
567 * next is NULL when called from perf_event_enable_on_exec()
568 * that will systematically cause a cgroup_switch()
569 */
570 if (next)
571 cgrp2 = perf_cgroup_from_task(next);
572
573 /*
574 * only schedule out current cgroup events if we know
575 * that we are switching to a different cgroup. Otherwise,
576 * do no touch the cgroup events.
577 */
578 if (cgrp1 != cgrp2)
579 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200580}
581
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200582static inline void perf_cgroup_sched_in(struct task_struct *prev,
583 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200584{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200585 struct perf_cgroup *cgrp1;
586 struct perf_cgroup *cgrp2 = NULL;
587
588 /*
589 * we come here when we know perf_cgroup_events > 0
590 */
591 cgrp1 = perf_cgroup_from_task(task);
592
593 /* prev can never be NULL */
594 cgrp2 = perf_cgroup_from_task(prev);
595
596 /*
597 * only need to schedule in cgroup events if we are changing
598 * cgroup during ctxsw. Cgroup events were not scheduled
599 * out of ctxsw out if that was not the case.
600 */
601 if (cgrp1 != cgrp2)
602 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200603}
604
605static inline int perf_cgroup_connect(int fd, struct perf_event *event,
606 struct perf_event_attr *attr,
607 struct perf_event *group_leader)
608{
609 struct perf_cgroup *cgrp;
610 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400611 struct fd f = fdget(fd);
612 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200613
Al Viro2903ff02012-08-28 12:52:22 -0400614 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200615 return -EBADF;
616
Al Virob5830432014-10-31 01:22:04 -0400617 css = css_tryget_online_from_dir(f.file->f_path.dentry,
Tejun Heoec903c02014-05-13 12:11:01 -0400618 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800619 if (IS_ERR(css)) {
620 ret = PTR_ERR(css);
621 goto out;
622 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200623
624 cgrp = container_of(css, struct perf_cgroup, css);
625 event->cgrp = cgrp;
626
627 /*
628 * all events in a group must monitor
629 * the same cgroup because a task belongs
630 * to only one perf cgroup at a time
631 */
632 if (group_leader && group_leader->cgrp != cgrp) {
633 perf_detach_cgroup(event);
634 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200635 }
Li Zefan3db272c2011-03-03 14:25:37 +0800636out:
Al Viro2903ff02012-08-28 12:52:22 -0400637 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200638 return ret;
639}
640
641static inline void
642perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
643{
644 struct perf_cgroup_info *t;
645 t = per_cpu_ptr(event->cgrp->info, event->cpu);
646 event->shadow_ctx_time = now - t->timestamp;
647}
648
649static inline void
650perf_cgroup_defer_enabled(struct perf_event *event)
651{
652 /*
653 * when the current task's perf cgroup does not match
654 * the event's, we need to remember to call the
655 * perf_mark_enable() function the first time a task with
656 * a matching perf cgroup is scheduled in.
657 */
658 if (is_cgroup_event(event) && !perf_cgroup_match(event))
659 event->cgrp_defer_enabled = 1;
660}
661
662static inline void
663perf_cgroup_mark_enabled(struct perf_event *event,
664 struct perf_event_context *ctx)
665{
666 struct perf_event *sub;
667 u64 tstamp = perf_event_time(event);
668
669 if (!event->cgrp_defer_enabled)
670 return;
671
672 event->cgrp_defer_enabled = 0;
673
674 event->tstamp_enabled = tstamp - event->total_time_enabled;
675 list_for_each_entry(sub, &event->sibling_list, group_entry) {
676 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
677 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
678 sub->cgrp_defer_enabled = 0;
679 }
680 }
681}
682#else /* !CONFIG_CGROUP_PERF */
683
684static inline bool
685perf_cgroup_match(struct perf_event *event)
686{
687 return true;
688}
689
690static inline void perf_detach_cgroup(struct perf_event *event)
691{}
692
693static inline int is_cgroup_event(struct perf_event *event)
694{
695 return 0;
696}
697
698static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
699{
700 return 0;
701}
702
703static inline void update_cgrp_time_from_event(struct perf_event *event)
704{
705}
706
707static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
708{
709}
710
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200711static inline void perf_cgroup_sched_out(struct task_struct *task,
712 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200713{
714}
715
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200716static inline void perf_cgroup_sched_in(struct task_struct *prev,
717 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200718{
719}
720
721static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
722 struct perf_event_attr *attr,
723 struct perf_event *group_leader)
724{
725 return -EINVAL;
726}
727
728static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200729perf_cgroup_set_timestamp(struct task_struct *task,
730 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200731{
732}
733
734void
735perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
736{
737}
738
739static inline void
740perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
741{
742}
743
744static inline u64 perf_cgroup_event_time(struct perf_event *event)
745{
746 return 0;
747}
748
749static inline void
750perf_cgroup_defer_enabled(struct perf_event *event)
751{
752}
753
754static inline void
755perf_cgroup_mark_enabled(struct perf_event *event,
756 struct perf_event_context *ctx)
757{
758}
759#endif
760
Stephane Eranian9e630202013-04-03 14:21:33 +0200761/*
762 * set default to be dependent on timer tick just
763 * like original code
764 */
765#define PERF_CPU_HRTIMER (1000 / HZ)
766/*
767 * function must be called with interrupts disbled
768 */
769static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
770{
771 struct perf_cpu_context *cpuctx;
772 enum hrtimer_restart ret = HRTIMER_NORESTART;
773 int rotations = 0;
774
775 WARN_ON(!irqs_disabled());
776
777 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
778
779 rotations = perf_rotate_context(cpuctx);
780
781 /*
782 * arm timer if needed
783 */
784 if (rotations) {
785 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
786 ret = HRTIMER_RESTART;
787 }
788
789 return ret;
790}
791
792/* CPU is going down */
793void perf_cpu_hrtimer_cancel(int cpu)
794{
795 struct perf_cpu_context *cpuctx;
796 struct pmu *pmu;
797 unsigned long flags;
798
799 if (WARN_ON(cpu != smp_processor_id()))
800 return;
801
802 local_irq_save(flags);
803
804 rcu_read_lock();
805
806 list_for_each_entry_rcu(pmu, &pmus, entry) {
807 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
808
809 if (pmu->task_ctx_nr == perf_sw_context)
810 continue;
811
812 hrtimer_cancel(&cpuctx->hrtimer);
813 }
814
815 rcu_read_unlock();
816
817 local_irq_restore(flags);
818}
819
820static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
821{
822 struct hrtimer *hr = &cpuctx->hrtimer;
823 struct pmu *pmu = cpuctx->ctx.pmu;
Stephane Eranian62b85632013-04-03 14:21:34 +0200824 int timer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200825
826 /* no multiplexing needed for SW PMU */
827 if (pmu->task_ctx_nr == perf_sw_context)
828 return;
829
Stephane Eranian62b85632013-04-03 14:21:34 +0200830 /*
831 * check default is sane, if not set then force to
832 * default interval (1/tick)
833 */
834 timer = pmu->hrtimer_interval_ms;
835 if (timer < 1)
836 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
837
838 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200839
840 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
841 hr->function = perf_cpu_hrtimer_handler;
842}
843
844static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
845{
846 struct hrtimer *hr = &cpuctx->hrtimer;
847 struct pmu *pmu = cpuctx->ctx.pmu;
848
849 /* not for SW PMU */
850 if (pmu->task_ctx_nr == perf_sw_context)
851 return;
852
853 if (hrtimer_active(hr))
854 return;
855
856 if (!hrtimer_callback_running(hr))
857 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
858 0, HRTIMER_MODE_REL_PINNED, 0);
859}
860
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200861void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200862{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200863 int *count = this_cpu_ptr(pmu->pmu_disable_count);
864 if (!(*count)++)
865 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200866}
867
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200868void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200869{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200870 int *count = this_cpu_ptr(pmu->pmu_disable_count);
871 if (!--(*count))
872 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200873}
874
Mark Rutland2fde4f92015-01-07 15:01:54 +0000875static DEFINE_PER_CPU(struct list_head, active_ctx_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200876
877/*
Mark Rutland2fde4f92015-01-07 15:01:54 +0000878 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
879 * perf_event_task_tick() are fully serialized because they're strictly cpu
880 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
881 * disabled, while perf_event_task_tick is called from IRQ context.
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200882 */
Mark Rutland2fde4f92015-01-07 15:01:54 +0000883static void perf_event_ctx_activate(struct perf_event_context *ctx)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200884{
Mark Rutland2fde4f92015-01-07 15:01:54 +0000885 struct list_head *head = this_cpu_ptr(&active_ctx_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200886
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200887 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200888
Mark Rutland2fde4f92015-01-07 15:01:54 +0000889 WARN_ON(!list_empty(&ctx->active_ctx_list));
890
891 list_add(&ctx->active_ctx_list, head);
892}
893
894static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
895{
896 WARN_ON(!irqs_disabled());
897
898 WARN_ON(list_empty(&ctx->active_ctx_list));
899
900 list_del_init(&ctx->active_ctx_list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200901}
902
903static void get_ctx(struct perf_event_context *ctx)
904{
905 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
906}
907
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200908static void put_ctx(struct perf_event_context *ctx)
909{
910 if (atomic_dec_and_test(&ctx->refcount)) {
911 if (ctx->parent_ctx)
912 put_ctx(ctx->parent_ctx);
913 if (ctx->task)
914 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800915 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200916 }
917}
918
Peter Zijlstra211de6e2014-09-30 19:23:08 +0200919/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100920 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
921 * perf_pmu_migrate_context() we need some magic.
922 *
923 * Those places that change perf_event::ctx will hold both
924 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
925 *
926 * Lock ordering is by mutex address. There is one other site where
927 * perf_event_context::mutex nests and that is put_event(). But remember that
928 * that is a parent<->child context relation, and migration does not affect
929 * children, therefore these two orderings should not interact.
930 *
931 * The change in perf_event::ctx does not affect children (as claimed above)
932 * because the sys_perf_event_open() case will install a new event and break
933 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
934 * concerned with cpuctx and that doesn't have children.
935 *
936 * The places that change perf_event::ctx will issue:
937 *
938 * perf_remove_from_context();
939 * synchronize_rcu();
940 * perf_install_in_context();
941 *
942 * to affect the change. The remove_from_context() + synchronize_rcu() should
943 * quiesce the event, after which we can install it in the new location. This
944 * means that only external vectors (perf_fops, prctl) can perturb the event
945 * while in transit. Therefore all such accessors should also acquire
946 * perf_event_context::mutex to serialize against this.
947 *
948 * However; because event->ctx can change while we're waiting to acquire
949 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
950 * function.
951 *
952 * Lock order:
953 * task_struct::perf_event_mutex
954 * perf_event_context::mutex
955 * perf_event_context::lock
956 * perf_event::child_mutex;
957 * perf_event::mmap_mutex
958 * mmap_sem
959 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +0100960static struct perf_event_context *
961perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100962{
963 struct perf_event_context *ctx;
964
965again:
966 rcu_read_lock();
967 ctx = ACCESS_ONCE(event->ctx);
968 if (!atomic_inc_not_zero(&ctx->refcount)) {
969 rcu_read_unlock();
970 goto again;
971 }
972 rcu_read_unlock();
973
Peter Zijlstraa83fe282015-01-29 14:44:34 +0100974 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100975 if (event->ctx != ctx) {
976 mutex_unlock(&ctx->mutex);
977 put_ctx(ctx);
978 goto again;
979 }
980
981 return ctx;
982}
983
Peter Zijlstraa83fe282015-01-29 14:44:34 +0100984static inline struct perf_event_context *
985perf_event_ctx_lock(struct perf_event *event)
986{
987 return perf_event_ctx_lock_nested(event, 0);
988}
989
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100990static void perf_event_ctx_unlock(struct perf_event *event,
991 struct perf_event_context *ctx)
992{
993 mutex_unlock(&ctx->mutex);
994 put_ctx(ctx);
995}
996
997/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +0200998 * This must be done under the ctx->lock, such as to serialize against
999 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1000 * calling scheduler related locks and ctx->lock nests inside those.
1001 */
1002static __must_check struct perf_event_context *
1003unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001004{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001005 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1006
1007 lockdep_assert_held(&ctx->lock);
1008
1009 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001010 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001011 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001012
1013 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001014}
1015
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001016static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1017{
1018 /*
1019 * only top level events have the pid namespace they were created in
1020 */
1021 if (event->parent)
1022 event = event->parent;
1023
1024 return task_tgid_nr_ns(p, event->ns);
1025}
1026
1027static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1028{
1029 /*
1030 * only top level events have the pid namespace they were created in
1031 */
1032 if (event->parent)
1033 event = event->parent;
1034
1035 return task_pid_nr_ns(p, event->ns);
1036}
1037
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001038/*
1039 * If we inherit events we want to return the parent event id
1040 * to userspace.
1041 */
1042static u64 primary_event_id(struct perf_event *event)
1043{
1044 u64 id = event->id;
1045
1046 if (event->parent)
1047 id = event->parent->id;
1048
1049 return id;
1050}
1051
1052/*
1053 * Get the perf_event_context for a task and lock it.
1054 * This has to cope with with the fact that until it is locked,
1055 * the context could get moved to another task.
1056 */
1057static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001058perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001059{
1060 struct perf_event_context *ctx;
1061
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001062retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001063 /*
1064 * One of the few rules of preemptible RCU is that one cannot do
1065 * rcu_read_unlock() while holding a scheduler (or nested) lock when
1066 * part of the read side critical section was preemptible -- see
1067 * rcu_read_unlock_special().
1068 *
1069 * Since ctx->lock nests under rq->lock we must ensure the entire read
1070 * side critical section is non-preemptible.
1071 */
1072 preempt_disable();
1073 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001074 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001075 if (ctx) {
1076 /*
1077 * If this context is a clone of another, it might
1078 * get swapped for another underneath us by
1079 * perf_event_task_sched_out, though the
1080 * rcu_read_lock() protects us from any context
1081 * getting freed. Lock the context and check if it
1082 * got swapped before we could get the lock, and retry
1083 * if so. If we locked the right context, then it
1084 * can't get swapped on us any more.
1085 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001086 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001087 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001088 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001089 rcu_read_unlock();
1090 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001091 goto retry;
1092 }
1093
1094 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001095 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001096 ctx = NULL;
1097 }
1098 }
1099 rcu_read_unlock();
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001100 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001101 return ctx;
1102}
1103
1104/*
1105 * Get the context for a task and increment its pin_count so it
1106 * can't get swapped to another task. This also increments its
1107 * reference count so that the context can't get freed.
1108 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001109static struct perf_event_context *
1110perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001111{
1112 struct perf_event_context *ctx;
1113 unsigned long flags;
1114
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001115 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001116 if (ctx) {
1117 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001118 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001119 }
1120 return ctx;
1121}
1122
1123static void perf_unpin_context(struct perf_event_context *ctx)
1124{
1125 unsigned long flags;
1126
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001127 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001128 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001129 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001130}
1131
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001132/*
1133 * Update the record of the current time in a context.
1134 */
1135static void update_context_time(struct perf_event_context *ctx)
1136{
1137 u64 now = perf_clock();
1138
1139 ctx->time += now - ctx->timestamp;
1140 ctx->timestamp = now;
1141}
1142
Stephane Eranian41587552011-01-03 18:20:01 +02001143static u64 perf_event_time(struct perf_event *event)
1144{
1145 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001146
1147 if (is_cgroup_event(event))
1148 return perf_cgroup_event_time(event);
1149
Stephane Eranian41587552011-01-03 18:20:01 +02001150 return ctx ? ctx->time : 0;
1151}
1152
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001153/*
1154 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001155 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001156 */
1157static void update_event_times(struct perf_event *event)
1158{
1159 struct perf_event_context *ctx = event->ctx;
1160 u64 run_end;
1161
1162 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1163 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1164 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001165 /*
1166 * in cgroup mode, time_enabled represents
1167 * the time the event was enabled AND active
1168 * tasks were in the monitored cgroup. This is
1169 * independent of the activity of the context as
1170 * there may be a mix of cgroup and non-cgroup events.
1171 *
1172 * That is why we treat cgroup events differently
1173 * here.
1174 */
1175 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001176 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001177 else if (ctx->is_active)
1178 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001179 else
1180 run_end = event->tstamp_stopped;
1181
1182 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001183
1184 if (event->state == PERF_EVENT_STATE_INACTIVE)
1185 run_end = event->tstamp_stopped;
1186 else
Stephane Eranian41587552011-01-03 18:20:01 +02001187 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001188
1189 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001190
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001191}
1192
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001193/*
1194 * Update total_time_enabled and total_time_running for all events in a group.
1195 */
1196static void update_group_times(struct perf_event *leader)
1197{
1198 struct perf_event *event;
1199
1200 update_event_times(leader);
1201 list_for_each_entry(event, &leader->sibling_list, group_entry)
1202 update_event_times(event);
1203}
1204
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001205static struct list_head *
1206ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1207{
1208 if (event->attr.pinned)
1209 return &ctx->pinned_groups;
1210 else
1211 return &ctx->flexible_groups;
1212}
1213
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001214/*
1215 * Add a event from the lists for its context.
1216 * Must be called with ctx->mutex and ctx->lock held.
1217 */
1218static void
1219list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1220{
Peter Zijlstra8a495422010-05-27 15:47:49 +02001221 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1222 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001223
1224 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001225 * If we're a stand alone event or group leader, we go to the context
1226 * list, group events are kept attached to the group so that
1227 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001228 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001229 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001230 struct list_head *list;
1231
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001232 if (is_software_event(event))
1233 event->group_flags |= PERF_GROUP_SOFTWARE;
1234
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001235 list = ctx_group_list(event, ctx);
1236 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001237 }
1238
Peter Zijlstra08309372011-03-03 11:31:20 +01001239 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001240 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001241
Stephane Eraniand010b332012-02-09 23:21:00 +01001242 if (has_branch_stack(event))
1243 ctx->nr_branch_stack++;
1244
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001245 list_add_rcu(&event->event_entry, &ctx->event_list);
1246 ctx->nr_events++;
1247 if (event->attr.inherit_stat)
1248 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001249
1250 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001251}
1252
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001253/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001254 * Initialize event state based on the perf_event_attr::disabled.
1255 */
1256static inline void perf_event__state_init(struct perf_event *event)
1257{
1258 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1259 PERF_EVENT_STATE_INACTIVE;
1260}
1261
1262/*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001263 * Called at perf_event creation and when events are attached/detached from a
1264 * group.
1265 */
1266static void perf_event__read_size(struct perf_event *event)
1267{
1268 int entry = sizeof(u64); /* value */
1269 int size = 0;
1270 int nr = 1;
1271
1272 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1273 size += sizeof(u64);
1274
1275 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1276 size += sizeof(u64);
1277
1278 if (event->attr.read_format & PERF_FORMAT_ID)
1279 entry += sizeof(u64);
1280
1281 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1282 nr += event->group_leader->nr_siblings;
1283 size += sizeof(u64);
1284 }
1285
1286 size += entry * nr;
1287 event->read_size = size;
1288}
1289
1290static void perf_event__header_size(struct perf_event *event)
1291{
1292 struct perf_sample_data *data;
1293 u64 sample_type = event->attr.sample_type;
1294 u16 size = 0;
1295
1296 perf_event__read_size(event);
1297
1298 if (sample_type & PERF_SAMPLE_IP)
1299 size += sizeof(data->ip);
1300
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001301 if (sample_type & PERF_SAMPLE_ADDR)
1302 size += sizeof(data->addr);
1303
1304 if (sample_type & PERF_SAMPLE_PERIOD)
1305 size += sizeof(data->period);
1306
Andi Kleenc3feedf2013-01-24 16:10:28 +01001307 if (sample_type & PERF_SAMPLE_WEIGHT)
1308 size += sizeof(data->weight);
1309
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001310 if (sample_type & PERF_SAMPLE_READ)
1311 size += event->read_size;
1312
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001313 if (sample_type & PERF_SAMPLE_DATA_SRC)
1314 size += sizeof(data->data_src.val);
1315
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001316 if (sample_type & PERF_SAMPLE_TRANSACTION)
1317 size += sizeof(data->txn);
1318
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001319 event->header_size = size;
1320}
1321
1322static void perf_event__id_header_size(struct perf_event *event)
1323{
1324 struct perf_sample_data *data;
1325 u64 sample_type = event->attr.sample_type;
1326 u16 size = 0;
1327
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001328 if (sample_type & PERF_SAMPLE_TID)
1329 size += sizeof(data->tid_entry);
1330
1331 if (sample_type & PERF_SAMPLE_TIME)
1332 size += sizeof(data->time);
1333
Adrian Hunterff3d5272013-08-27 11:23:07 +03001334 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1335 size += sizeof(data->id);
1336
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001337 if (sample_type & PERF_SAMPLE_ID)
1338 size += sizeof(data->id);
1339
1340 if (sample_type & PERF_SAMPLE_STREAM_ID)
1341 size += sizeof(data->stream_id);
1342
1343 if (sample_type & PERF_SAMPLE_CPU)
1344 size += sizeof(data->cpu_entry);
1345
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001346 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001347}
1348
Peter Zijlstra8a495422010-05-27 15:47:49 +02001349static void perf_group_attach(struct perf_event *event)
1350{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001351 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001352
Peter Zijlstra74c33372010-10-15 11:40:29 +02001353 /*
1354 * We can have double attach due to group movement in perf_event_open.
1355 */
1356 if (event->attach_state & PERF_ATTACH_GROUP)
1357 return;
1358
Peter Zijlstra8a495422010-05-27 15:47:49 +02001359 event->attach_state |= PERF_ATTACH_GROUP;
1360
1361 if (group_leader == event)
1362 return;
1363
Peter Zijlstra652884f2015-01-23 11:20:10 +01001364 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1365
Peter Zijlstra8a495422010-05-27 15:47:49 +02001366 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1367 !is_software_event(event))
1368 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1369
1370 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1371 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001372
1373 perf_event__header_size(group_leader);
1374
1375 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1376 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001377}
1378
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001379/*
1380 * Remove a event from the lists for its context.
1381 * Must be called with ctx->mutex and ctx->lock held.
1382 */
1383static void
1384list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1385{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001386 struct perf_cpu_context *cpuctx;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001387
1388 WARN_ON_ONCE(event->ctx != ctx);
1389 lockdep_assert_held(&ctx->lock);
1390
Peter Zijlstra8a495422010-05-27 15:47:49 +02001391 /*
1392 * We can have double detach due to exit/hot-unplug + close.
1393 */
1394 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001395 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001396
1397 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1398
Stephane Eranian68cacd22011-03-23 16:03:06 +01001399 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001400 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001401 cpuctx = __get_cpu_context(ctx);
1402 /*
1403 * if there are no more cgroup events
1404 * then cler cgrp to avoid stale pointer
1405 * in update_cgrp_time_from_cpuctx()
1406 */
1407 if (!ctx->nr_cgroups)
1408 cpuctx->cgrp = NULL;
1409 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001410
Stephane Eraniand010b332012-02-09 23:21:00 +01001411 if (has_branch_stack(event))
1412 ctx->nr_branch_stack--;
1413
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001414 ctx->nr_events--;
1415 if (event->attr.inherit_stat)
1416 ctx->nr_stat--;
1417
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001418 list_del_rcu(&event->event_entry);
1419
Peter Zijlstra8a495422010-05-27 15:47:49 +02001420 if (event->group_leader == event)
1421 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001422
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001423 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001424
1425 /*
1426 * If event was in error state, then keep it
1427 * that way, otherwise bogus counts will be
1428 * returned on read(). The only way to get out
1429 * of error state is by explicit re-enabling
1430 * of the event
1431 */
1432 if (event->state > PERF_EVENT_STATE_OFF)
1433 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001434
1435 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001436}
1437
Peter Zijlstra8a495422010-05-27 15:47:49 +02001438static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001439{
1440 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001441 struct list_head *list = NULL;
1442
1443 /*
1444 * We can have double detach due to exit/hot-unplug + close.
1445 */
1446 if (!(event->attach_state & PERF_ATTACH_GROUP))
1447 return;
1448
1449 event->attach_state &= ~PERF_ATTACH_GROUP;
1450
1451 /*
1452 * If this is a sibling, remove it from its group.
1453 */
1454 if (event->group_leader != event) {
1455 list_del_init(&event->group_entry);
1456 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001457 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001458 }
1459
1460 if (!list_empty(&event->group_entry))
1461 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001462
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001463 /*
1464 * If this was a group event with sibling events then
1465 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001466 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001467 */
1468 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001469 if (list)
1470 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001471 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001472
1473 /* Inherit group flags from the previous leader */
1474 sibling->group_flags = event->group_flags;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001475
1476 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001477 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001478
1479out:
1480 perf_event__header_size(event->group_leader);
1481
1482 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1483 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001484}
1485
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001486/*
1487 * User event without the task.
1488 */
1489static bool is_orphaned_event(struct perf_event *event)
1490{
1491 return event && !is_kernel_event(event) && !event->owner;
1492}
1493
1494/*
1495 * Event has a parent but parent's task finished and it's
1496 * alive only because of children holding refference.
1497 */
1498static bool is_orphaned_child(struct perf_event *event)
1499{
1500 return is_orphaned_event(event->parent);
1501}
1502
1503static void orphans_remove_work(struct work_struct *work);
1504
1505static void schedule_orphans_remove(struct perf_event_context *ctx)
1506{
1507 if (!ctx->task || ctx->orphans_remove_sched || !perf_wq)
1508 return;
1509
1510 if (queue_delayed_work(perf_wq, &ctx->orphans_remove, 1)) {
1511 get_ctx(ctx);
1512 ctx->orphans_remove_sched = true;
1513 }
1514}
1515
1516static int __init perf_workqueue_init(void)
1517{
1518 perf_wq = create_singlethread_workqueue("perf");
1519 WARN(!perf_wq, "failed to create perf workqueue\n");
1520 return perf_wq ? 0 : -1;
1521}
1522
1523core_initcall(perf_workqueue_init);
1524
Stephane Eranianfa66f072010-08-26 16:40:01 +02001525static inline int
1526event_filter_match(struct perf_event *event)
1527{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001528 return (event->cpu == -1 || event->cpu == smp_processor_id())
1529 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001530}
1531
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001532static void
1533event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001534 struct perf_cpu_context *cpuctx,
1535 struct perf_event_context *ctx)
1536{
Stephane Eranian41587552011-01-03 18:20:01 +02001537 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001538 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001539
1540 WARN_ON_ONCE(event->ctx != ctx);
1541 lockdep_assert_held(&ctx->lock);
1542
Stephane Eranianfa66f072010-08-26 16:40:01 +02001543 /*
1544 * An event which could not be activated because of
1545 * filter mismatch still needs to have its timings
1546 * maintained, otherwise bogus information is return
1547 * via read() for time_enabled, time_running:
1548 */
1549 if (event->state == PERF_EVENT_STATE_INACTIVE
1550 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001551 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001552 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001553 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001554 }
1555
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001556 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001557 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001558
Alexander Shishkin44377272013-12-16 14:17:36 +02001559 perf_pmu_disable(event->pmu);
1560
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001561 event->state = PERF_EVENT_STATE_INACTIVE;
1562 if (event->pending_disable) {
1563 event->pending_disable = 0;
1564 event->state = PERF_EVENT_STATE_OFF;
1565 }
Stephane Eranian41587552011-01-03 18:20:01 +02001566 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001567 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001568 event->oncpu = -1;
1569
1570 if (!is_software_event(event))
1571 cpuctx->active_oncpu--;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001572 if (!--ctx->nr_active)
1573 perf_event_ctx_deactivate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001574 if (event->attr.freq && event->attr.sample_freq)
1575 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001576 if (event->attr.exclusive || !cpuctx->active_oncpu)
1577 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001578
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001579 if (is_orphaned_child(event))
1580 schedule_orphans_remove(ctx);
1581
Alexander Shishkin44377272013-12-16 14:17:36 +02001582 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001583}
1584
1585static void
1586group_sched_out(struct perf_event *group_event,
1587 struct perf_cpu_context *cpuctx,
1588 struct perf_event_context *ctx)
1589{
1590 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001591 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001592
1593 event_sched_out(group_event, cpuctx, ctx);
1594
1595 /*
1596 * Schedule out siblings (if any):
1597 */
1598 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1599 event_sched_out(event, cpuctx, ctx);
1600
Stephane Eranianfa66f072010-08-26 16:40:01 +02001601 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001602 cpuctx->exclusive = 0;
1603}
1604
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001605struct remove_event {
1606 struct perf_event *event;
1607 bool detach_group;
1608};
1609
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001610/*
1611 * Cross CPU call to remove a performance event
1612 *
1613 * We disable the event on the hardware level first. After that we
1614 * remove it from the context list.
1615 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001616static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001617{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001618 struct remove_event *re = info;
1619 struct perf_event *event = re->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001620 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001621 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001622
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001623 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001624 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001625 if (re->detach_group)
1626 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001627 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001628 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1629 ctx->is_active = 0;
1630 cpuctx->task_ctx = NULL;
1631 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001632 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001633
1634 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001635}
1636
1637
1638/*
1639 * Remove the event from a task's (or a CPU's) list of events.
1640 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001641 * CPU events are removed with a smp call. For task events we only
1642 * call when the task is on a CPU.
1643 *
1644 * If event->ctx is a cloned context, callers must make sure that
1645 * every task struct that event->ctx->task could possibly point to
1646 * remains valid. This is OK when called from perf_release since
1647 * that only calls us on the top-level context, which can't be a clone.
1648 * When called from perf_event_exit_task, it's OK because the
1649 * context has been detached from its task.
1650 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001651static void perf_remove_from_context(struct perf_event *event, bool detach_group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001652{
1653 struct perf_event_context *ctx = event->ctx;
1654 struct task_struct *task = ctx->task;
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001655 struct remove_event re = {
1656 .event = event,
1657 .detach_group = detach_group,
1658 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001659
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001660 lockdep_assert_held(&ctx->mutex);
1661
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001662 if (!task) {
1663 /*
Mark Rutland226424e2014-11-05 16:11:44 +00001664 * Per cpu events are removed via an smp call. The removal can
1665 * fail if the CPU is currently offline, but in that case we
1666 * already called __perf_remove_from_context from
1667 * perf_event_exit_cpu.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001668 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001669 cpu_function_call(event->cpu, __perf_remove_from_context, &re);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001670 return;
1671 }
1672
1673retry:
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001674 if (!task_function_call(task, __perf_remove_from_context, &re))
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001675 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001676
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001677 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001678 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001679 * If we failed to find a running task, but find the context active now
1680 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001681 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001682 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001683 raw_spin_unlock_irq(&ctx->lock);
Cong Wang3577af72014-09-02 15:27:20 -07001684 /*
1685 * Reload the task pointer, it might have been changed by
1686 * a concurrent perf_event_context_sched_out().
1687 */
1688 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001689 goto retry;
1690 }
1691
1692 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001693 * Since the task isn't running, its safe to remove the event, us
1694 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001695 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001696 if (detach_group)
1697 perf_group_detach(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001698 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001699 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001700}
1701
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001702/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001703 * Cross CPU call to disable a performance event
1704 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301705int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001706{
1707 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001708 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001709 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001710
1711 /*
1712 * If this is a per-task event, need to check whether this
1713 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001714 *
1715 * Can trigger due to concurrent perf_event_context_sched_out()
1716 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001717 */
1718 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001719 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001720
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001721 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001722
1723 /*
1724 * If the event is on, turn it off.
1725 * If it is in error state, leave it in error state.
1726 */
1727 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1728 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001729 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001730 update_group_times(event);
1731 if (event == event->group_leader)
1732 group_sched_out(event, cpuctx, ctx);
1733 else
1734 event_sched_out(event, cpuctx, ctx);
1735 event->state = PERF_EVENT_STATE_OFF;
1736 }
1737
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001738 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001739
1740 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001741}
1742
1743/*
1744 * Disable a event.
1745 *
1746 * If event->ctx is a cloned context, callers must make sure that
1747 * every task struct that event->ctx->task could possibly point to
1748 * remains valid. This condition is satisifed when called through
1749 * perf_event_for_each_child or perf_event_for_each because they
1750 * hold the top-level event's child_mutex, so any descendant that
1751 * goes to exit will block in sync_child_event.
1752 * When called from perf_pending_event it's OK because event->ctx
1753 * is the current context on this CPU and preemption is disabled,
1754 * hence we can't get into perf_event_task_sched_out for this context.
1755 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001756static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001757{
1758 struct perf_event_context *ctx = event->ctx;
1759 struct task_struct *task = ctx->task;
1760
1761 if (!task) {
1762 /*
1763 * Disable the event on the cpu that it's on
1764 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001765 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001766 return;
1767 }
1768
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001769retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001770 if (!task_function_call(task, __perf_event_disable, event))
1771 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001772
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001773 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001774 /*
1775 * If the event is still active, we need to retry the cross-call.
1776 */
1777 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001778 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001779 /*
1780 * Reload the task pointer, it might have been changed by
1781 * a concurrent perf_event_context_sched_out().
1782 */
1783 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001784 goto retry;
1785 }
1786
1787 /*
1788 * Since we have the lock this context can't be scheduled
1789 * in, so we can change the state safely.
1790 */
1791 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1792 update_group_times(event);
1793 event->state = PERF_EVENT_STATE_OFF;
1794 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001795 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001796}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001797
1798/*
1799 * Strictly speaking kernel users cannot create groups and therefore this
1800 * interface does not need the perf_event_ctx_lock() magic.
1801 */
1802void perf_event_disable(struct perf_event *event)
1803{
1804 struct perf_event_context *ctx;
1805
1806 ctx = perf_event_ctx_lock(event);
1807 _perf_event_disable(event);
1808 perf_event_ctx_unlock(event, ctx);
1809}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001810EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001811
Stephane Eraniane5d13672011-02-14 11:20:01 +02001812static void perf_set_shadow_time(struct perf_event *event,
1813 struct perf_event_context *ctx,
1814 u64 tstamp)
1815{
1816 /*
1817 * use the correct time source for the time snapshot
1818 *
1819 * We could get by without this by leveraging the
1820 * fact that to get to this function, the caller
1821 * has most likely already called update_context_time()
1822 * and update_cgrp_time_xx() and thus both timestamp
1823 * are identical (or very close). Given that tstamp is,
1824 * already adjusted for cgroup, we could say that:
1825 * tstamp - ctx->timestamp
1826 * is equivalent to
1827 * tstamp - cgrp->timestamp.
1828 *
1829 * Then, in perf_output_read(), the calculation would
1830 * work with no changes because:
1831 * - event is guaranteed scheduled in
1832 * - no scheduled out in between
1833 * - thus the timestamp would be the same
1834 *
1835 * But this is a bit hairy.
1836 *
1837 * So instead, we have an explicit cgroup call to remain
1838 * within the time time source all along. We believe it
1839 * is cleaner and simpler to understand.
1840 */
1841 if (is_cgroup_event(event))
1842 perf_cgroup_set_shadow_time(event, tstamp);
1843 else
1844 event->shadow_ctx_time = tstamp - ctx->timestamp;
1845}
1846
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001847#define MAX_INTERRUPTS (~0ULL)
1848
1849static void perf_log_throttle(struct perf_event *event, int enable);
1850
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001851static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001852event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001853 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001854 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001855{
Stephane Eranian41587552011-01-03 18:20:01 +02001856 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001857 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001858
Peter Zijlstra63342412014-05-05 11:49:16 +02001859 lockdep_assert_held(&ctx->lock);
1860
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001861 if (event->state <= PERF_EVENT_STATE_OFF)
1862 return 0;
1863
1864 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001865 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001866
1867 /*
1868 * Unthrottle events, since we scheduled we might have missed several
1869 * ticks already, also for a heavily scheduling task there is little
1870 * guarantee it'll get a tick in a timely manner.
1871 */
1872 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1873 perf_log_throttle(event, 1);
1874 event->hw.interrupts = 0;
1875 }
1876
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001877 /*
1878 * The new state must be visible before we turn it on in the hardware:
1879 */
1880 smp_wmb();
1881
Alexander Shishkin44377272013-12-16 14:17:36 +02001882 perf_pmu_disable(event->pmu);
1883
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001884 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001885 event->state = PERF_EVENT_STATE_INACTIVE;
1886 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001887 ret = -EAGAIN;
1888 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001889 }
1890
Stephane Eranian41587552011-01-03 18:20:01 +02001891 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001892
Stephane Eraniane5d13672011-02-14 11:20:01 +02001893 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001894
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001895 if (!is_software_event(event))
1896 cpuctx->active_oncpu++;
Mark Rutland2fde4f92015-01-07 15:01:54 +00001897 if (!ctx->nr_active++)
1898 perf_event_ctx_activate(ctx);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001899 if (event->attr.freq && event->attr.sample_freq)
1900 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001901
1902 if (event->attr.exclusive)
1903 cpuctx->exclusive = 1;
1904
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001905 if (is_orphaned_child(event))
1906 schedule_orphans_remove(ctx);
1907
Alexander Shishkin44377272013-12-16 14:17:36 +02001908out:
1909 perf_pmu_enable(event->pmu);
1910
1911 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001912}
1913
1914static int
1915group_sched_in(struct perf_event *group_event,
1916 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001917 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001918{
Lin Ming6bde9b62010-04-23 13:56:00 +08001919 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001920 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001921 u64 now = ctx->time;
1922 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001923
1924 if (group_event->state == PERF_EVENT_STATE_OFF)
1925 return 0;
1926
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001927 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001928
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001929 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001930 pmu->cancel_txn(pmu);
Stephane Eranian9e630202013-04-03 14:21:33 +02001931 perf_cpu_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001932 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001933 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001934
1935 /*
1936 * Schedule in siblings as one group (if any):
1937 */
1938 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001939 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001940 partial_group = event;
1941 goto group_error;
1942 }
1943 }
1944
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001945 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001946 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001947
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001948group_error:
1949 /*
1950 * Groups can be scheduled in as one unit only, so undo any
1951 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001952 * The events up to the failed event are scheduled out normally,
1953 * tstamp_stopped will be updated.
1954 *
1955 * The failed events and the remaining siblings need to have
1956 * their timings updated as if they had gone thru event_sched_in()
1957 * and event_sched_out(). This is required to get consistent timings
1958 * across the group. This also takes care of the case where the group
1959 * could never be scheduled by ensuring tstamp_stopped is set to mark
1960 * the time the event was actually stopped, such that time delta
1961 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001962 */
1963 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1964 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001965 simulate = true;
1966
1967 if (simulate) {
1968 event->tstamp_running += now - event->tstamp_stopped;
1969 event->tstamp_stopped = now;
1970 } else {
1971 event_sched_out(event, cpuctx, ctx);
1972 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001973 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001974 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001975
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001976 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001977
Stephane Eranian9e630202013-04-03 14:21:33 +02001978 perf_cpu_hrtimer_restart(cpuctx);
1979
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001980 return -EAGAIN;
1981}
1982
1983/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001984 * Work out whether we can put this event group on the CPU now.
1985 */
1986static int group_can_go_on(struct perf_event *event,
1987 struct perf_cpu_context *cpuctx,
1988 int can_add_hw)
1989{
1990 /*
1991 * Groups consisting entirely of software events can always go on.
1992 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001993 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001994 return 1;
1995 /*
1996 * If an exclusive group is already on, no other hardware
1997 * events can go on.
1998 */
1999 if (cpuctx->exclusive)
2000 return 0;
2001 /*
2002 * If this group is exclusive and there are already
2003 * events on the CPU, it can't go on.
2004 */
2005 if (event->attr.exclusive && cpuctx->active_oncpu)
2006 return 0;
2007 /*
2008 * Otherwise, try to add it if all previous groups were able
2009 * to go on.
2010 */
2011 return can_add_hw;
2012}
2013
2014static void add_event_to_ctx(struct perf_event *event,
2015 struct perf_event_context *ctx)
2016{
Stephane Eranian41587552011-01-03 18:20:01 +02002017 u64 tstamp = perf_event_time(event);
2018
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002019 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002020 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002021 event->tstamp_enabled = tstamp;
2022 event->tstamp_running = tstamp;
2023 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002024}
2025
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002026static void task_ctx_sched_out(struct perf_event_context *ctx);
2027static void
2028ctx_sched_in(struct perf_event_context *ctx,
2029 struct perf_cpu_context *cpuctx,
2030 enum event_type_t event_type,
2031 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002032
Peter Zijlstradce58552011-04-09 21:17:46 +02002033static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2034 struct perf_event_context *ctx,
2035 struct task_struct *task)
2036{
2037 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2038 if (ctx)
2039 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2040 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2041 if (ctx)
2042 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2043}
2044
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002045/*
2046 * Cross CPU call to install and enable a performance event
2047 *
2048 * Must be called with ctx->mutex held
2049 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002050static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002051{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002052 struct perf_event *event = info;
2053 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002054 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002055 struct perf_event_context *task_ctx = cpuctx->task_ctx;
2056 struct task_struct *task = current;
2057
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002058 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002059 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002060
2061 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002062 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002063 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002064 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002065 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002066
2067 /*
2068 * If the context we're installing events in is not the
2069 * active task_ctx, flip them.
2070 */
2071 if (ctx->task && task_ctx != ctx) {
2072 if (task_ctx)
2073 raw_spin_unlock(&task_ctx->lock);
2074 raw_spin_lock(&ctx->lock);
2075 task_ctx = ctx;
2076 }
2077
2078 if (task_ctx) {
2079 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002080 task = task_ctx->task;
2081 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002082
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002083 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002084
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002085 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002086 /*
2087 * update cgrp time only if current cgrp
2088 * matches event->cgrp. Must be done before
2089 * calling add_event_to_ctx()
2090 */
2091 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002092
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002093 add_event_to_ctx(event, ctx);
2094
2095 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002096 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002097 */
Peter Zijlstradce58552011-04-09 21:17:46 +02002098 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002099
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002100 perf_pmu_enable(cpuctx->ctx.pmu);
2101 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002102
2103 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002104}
2105
2106/*
2107 * Attach a performance event to a context
2108 *
2109 * First we add the event to the list with the hardware enable bit
2110 * in event->hw_config cleared.
2111 *
2112 * If the event is attached to a task which is on a CPU we use a smp
2113 * call to enable it in the task context. The task might have been
2114 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002115 */
2116static void
2117perf_install_in_context(struct perf_event_context *ctx,
2118 struct perf_event *event,
2119 int cpu)
2120{
2121 struct task_struct *task = ctx->task;
2122
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002123 lockdep_assert_held(&ctx->mutex);
2124
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002125 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002126 if (event->cpu != -1)
2127 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002128
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002129 if (!task) {
2130 /*
2131 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002132 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002133 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002134 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002135 return;
2136 }
2137
2138retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002139 if (!task_function_call(task, __perf_install_in_context, event))
2140 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002141
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002142 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002143 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002144 * If we failed to find a running task, but find the context active now
2145 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002146 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002147 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002148 raw_spin_unlock_irq(&ctx->lock);
Cong Wang3577af72014-09-02 15:27:20 -07002149 /*
2150 * Reload the task pointer, it might have been changed by
2151 * a concurrent perf_event_context_sched_out().
2152 */
2153 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002154 goto retry;
2155 }
2156
2157 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002158 * Since the task isn't running, its safe to add the event, us holding
2159 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002160 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002161 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002162 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002163}
2164
2165/*
2166 * Put a event into inactive state and update time fields.
2167 * Enabling the leader of a group effectively enables all
2168 * the group members that aren't explicitly disabled, so we
2169 * have to update their ->tstamp_enabled also.
2170 * Note: this works for group members as well as group leaders
2171 * since the non-leader members' sibling_lists will be empty.
2172 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002173static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002174{
2175 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002176 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002177
2178 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002179 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002180 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002181 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2182 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002183 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002184}
2185
2186/*
2187 * Cross CPU call to enable a performance event
2188 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002189static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002190{
2191 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002192 struct perf_event_context *ctx = event->ctx;
2193 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002194 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002195 int err;
2196
Jiri Olsa06f41792013-07-09 17:44:11 +02002197 /*
2198 * There's a time window between 'ctx->is_active' check
2199 * in perf_event_enable function and this place having:
2200 * - IRQs on
2201 * - ctx->lock unlocked
2202 *
2203 * where the task could be killed and 'ctx' deactivated
2204 * by perf_event_exit_task.
2205 */
2206 if (!ctx->is_active)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002207 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002208
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002209 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002210 update_context_time(ctx);
2211
2212 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2213 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002214
2215 /*
2216 * set current task's cgroup time reference point
2217 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002218 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002219
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002220 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002221
Stephane Eraniane5d13672011-02-14 11:20:01 +02002222 if (!event_filter_match(event)) {
2223 if (is_cgroup_event(event))
2224 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002225 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002226 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002227
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002228 /*
2229 * If the event is in a group and isn't the group leader,
2230 * then don't put it on unless the group is on.
2231 */
2232 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2233 goto unlock;
2234
2235 if (!group_can_go_on(event, cpuctx, 1)) {
2236 err = -EEXIST;
2237 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002238 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002239 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002240 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01002241 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002242 }
2243
2244 if (err) {
2245 /*
2246 * If this event can't go on and it's part of a
2247 * group, then the whole group has to come off.
2248 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002249 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002250 group_sched_out(leader, cpuctx, ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002251 perf_cpu_hrtimer_restart(cpuctx);
2252 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002253 if (leader->attr.pinned) {
2254 update_group_times(leader);
2255 leader->state = PERF_EVENT_STATE_ERROR;
2256 }
2257 }
2258
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002259unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002260 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002261
2262 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002263}
2264
2265/*
2266 * Enable a event.
2267 *
2268 * If event->ctx is a cloned context, callers must make sure that
2269 * every task struct that event->ctx->task could possibly point to
2270 * remains valid. This condition is satisfied when called through
2271 * perf_event_for_each_child or perf_event_for_each as described
2272 * for perf_event_disable.
2273 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002274static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002275{
2276 struct perf_event_context *ctx = event->ctx;
2277 struct task_struct *task = ctx->task;
2278
2279 if (!task) {
2280 /*
2281 * Enable the event on the cpu that it's on
2282 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002283 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002284 return;
2285 }
2286
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002287 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002288 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2289 goto out;
2290
2291 /*
2292 * If the event is in error state, clear that first.
2293 * That way, if we see the event in error state below, we
2294 * know that it has gone back into error state, as distinct
2295 * from the task having been scheduled away before the
2296 * cross-call arrived.
2297 */
2298 if (event->state == PERF_EVENT_STATE_ERROR)
2299 event->state = PERF_EVENT_STATE_OFF;
2300
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002301retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002302 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002303 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002304 goto out;
2305 }
2306
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002307 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002308
2309 if (!task_function_call(task, __perf_event_enable, event))
2310 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002311
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002312 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002313
2314 /*
2315 * If the context is active and the event is still off,
2316 * we need to retry the cross-call.
2317 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002318 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2319 /*
2320 * task could have been flipped by a concurrent
2321 * perf_event_context_sched_out()
2322 */
2323 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002324 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002325 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002326
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002327out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002328 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002329}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002330
2331/*
2332 * See perf_event_disable();
2333 */
2334void perf_event_enable(struct perf_event *event)
2335{
2336 struct perf_event_context *ctx;
2337
2338 ctx = perf_event_ctx_lock(event);
2339 _perf_event_enable(event);
2340 perf_event_ctx_unlock(event, ctx);
2341}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002342EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002343
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002344static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002345{
2346 /*
2347 * not supported on inherited events
2348 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002349 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002350 return -EINVAL;
2351
2352 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002353 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002354
2355 return 0;
2356}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002357
2358/*
2359 * See perf_event_disable()
2360 */
2361int perf_event_refresh(struct perf_event *event, int refresh)
2362{
2363 struct perf_event_context *ctx;
2364 int ret;
2365
2366 ctx = perf_event_ctx_lock(event);
2367 ret = _perf_event_refresh(event, refresh);
2368 perf_event_ctx_unlock(event, ctx);
2369
2370 return ret;
2371}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002372EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002373
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002374static void ctx_sched_out(struct perf_event_context *ctx,
2375 struct perf_cpu_context *cpuctx,
2376 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002377{
2378 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002379 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002380
Peter Zijlstradb24d332011-04-09 21:17:45 +02002381 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002382 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002383 return;
2384
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002385 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002386 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002387 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002388 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002389
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002390 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002391 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002392 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2393 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002394 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002395
Peter Zijlstradb24d332011-04-09 21:17:45 +02002396 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002397 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002398 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002399 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002400 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002401}
2402
2403/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002404 * Test whether two contexts are equivalent, i.e. whether they have both been
2405 * cloned from the same version of the same context.
2406 *
2407 * Equivalence is measured using a generation number in the context that is
2408 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2409 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002410 */
2411static int context_equiv(struct perf_event_context *ctx1,
2412 struct perf_event_context *ctx2)
2413{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002414 lockdep_assert_held(&ctx1->lock);
2415 lockdep_assert_held(&ctx2->lock);
2416
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002417 /* Pinning disables the swap optimization */
2418 if (ctx1->pin_count || ctx2->pin_count)
2419 return 0;
2420
2421 /* If ctx1 is the parent of ctx2 */
2422 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2423 return 1;
2424
2425 /* If ctx2 is the parent of ctx1 */
2426 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2427 return 1;
2428
2429 /*
2430 * If ctx1 and ctx2 have the same parent; we flatten the parent
2431 * hierarchy, see perf_event_init_context().
2432 */
2433 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2434 ctx1->parent_gen == ctx2->parent_gen)
2435 return 1;
2436
2437 /* Unmatched */
2438 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002439}
2440
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002441static void __perf_event_sync_stat(struct perf_event *event,
2442 struct perf_event *next_event)
2443{
2444 u64 value;
2445
2446 if (!event->attr.inherit_stat)
2447 return;
2448
2449 /*
2450 * Update the event value, we cannot use perf_event_read()
2451 * because we're in the middle of a context switch and have IRQs
2452 * disabled, which upsets smp_call_function_single(), however
2453 * we know the event must be on the current CPU, therefore we
2454 * don't need to use it.
2455 */
2456 switch (event->state) {
2457 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002458 event->pmu->read(event);
2459 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002460
2461 case PERF_EVENT_STATE_INACTIVE:
2462 update_event_times(event);
2463 break;
2464
2465 default:
2466 break;
2467 }
2468
2469 /*
2470 * In order to keep per-task stats reliable we need to flip the event
2471 * values when we flip the contexts.
2472 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002473 value = local64_read(&next_event->count);
2474 value = local64_xchg(&event->count, value);
2475 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002476
2477 swap(event->total_time_enabled, next_event->total_time_enabled);
2478 swap(event->total_time_running, next_event->total_time_running);
2479
2480 /*
2481 * Since we swizzled the values, update the user visible data too.
2482 */
2483 perf_event_update_userpage(event);
2484 perf_event_update_userpage(next_event);
2485}
2486
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002487static void perf_event_sync_stat(struct perf_event_context *ctx,
2488 struct perf_event_context *next_ctx)
2489{
2490 struct perf_event *event, *next_event;
2491
2492 if (!ctx->nr_stat)
2493 return;
2494
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002495 update_context_time(ctx);
2496
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002497 event = list_first_entry(&ctx->event_list,
2498 struct perf_event, event_entry);
2499
2500 next_event = list_first_entry(&next_ctx->event_list,
2501 struct perf_event, event_entry);
2502
2503 while (&event->event_entry != &ctx->event_list &&
2504 &next_event->event_entry != &next_ctx->event_list) {
2505
2506 __perf_event_sync_stat(event, next_event);
2507
2508 event = list_next_entry(event, event_entry);
2509 next_event = list_next_entry(next_event, event_entry);
2510 }
2511}
2512
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002513static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2514 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002515{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002516 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002517 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002518 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002519 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002520 int do_switch = 1;
2521
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002522 if (likely(!ctx))
2523 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002524
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002525 cpuctx = __get_cpu_context(ctx);
2526 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002527 return;
2528
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002529 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002530 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002531 if (!next_ctx)
2532 goto unlock;
2533
2534 parent = rcu_dereference(ctx->parent_ctx);
2535 next_parent = rcu_dereference(next_ctx->parent_ctx);
2536
2537 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002538 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002539 goto unlock;
2540
2541 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002542 /*
2543 * Looks like the two contexts are clones, so we might be
2544 * able to optimize the context switch. We lock both
2545 * contexts and check that they are clones under the
2546 * lock (including re-checking that neither has been
2547 * uncloned in the meantime). It doesn't matter which
2548 * order we take the locks because no other cpu could
2549 * be trying to lock both of these tasks.
2550 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002551 raw_spin_lock(&ctx->lock);
2552 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002553 if (context_equiv(ctx, next_ctx)) {
2554 /*
2555 * XXX do we need a memory barrier of sorts
2556 * wrt to rcu_dereference() of perf_event_ctxp
2557 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002558 task->perf_event_ctxp[ctxn] = next_ctx;
2559 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002560 ctx->task = next;
2561 next_ctx->task = task;
2562 do_switch = 0;
2563
2564 perf_event_sync_stat(ctx, next_ctx);
2565 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002566 raw_spin_unlock(&next_ctx->lock);
2567 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002568 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002569unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002570 rcu_read_unlock();
2571
2572 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002573 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002574 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002575 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002576 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002577 }
2578}
2579
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002580#define for_each_task_context_nr(ctxn) \
2581 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2582
2583/*
2584 * Called from scheduler to remove the events of the current task,
2585 * with interrupts disabled.
2586 *
2587 * We stop each event and update the event value in event->count.
2588 *
2589 * This does not protect us against NMI, but disable()
2590 * sets the disabled bit in the control field of event _before_
2591 * accessing the event control register. If a NMI hits, then it will
2592 * not restart the event.
2593 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002594void __perf_event_task_sched_out(struct task_struct *task,
2595 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002596{
2597 int ctxn;
2598
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002599 for_each_task_context_nr(ctxn)
2600 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002601
2602 /*
2603 * if cgroup events exist on this CPU, then we need
2604 * to check if we have to switch out PMU state.
2605 * cgroup event are system-wide mode only
2606 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002607 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002608 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002609}
2610
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002611static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002612{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002613 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002614
2615 if (!cpuctx->task_ctx)
2616 return;
2617
2618 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2619 return;
2620
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002621 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002622 cpuctx->task_ctx = NULL;
2623}
2624
2625/*
2626 * Called with IRQs disabled
2627 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002628static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2629 enum event_type_t event_type)
2630{
2631 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002632}
2633
2634static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002635ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002636 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002637{
2638 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002639
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002640 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2641 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002642 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002643 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002644 continue;
2645
Stephane Eraniane5d13672011-02-14 11:20:01 +02002646 /* may need to reset tstamp_enabled */
2647 if (is_cgroup_event(event))
2648 perf_cgroup_mark_enabled(event, ctx);
2649
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002650 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002651 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002652
2653 /*
2654 * If this pinned group hasn't been scheduled,
2655 * put it in error state.
2656 */
2657 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2658 update_group_times(event);
2659 event->state = PERF_EVENT_STATE_ERROR;
2660 }
2661 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002662}
2663
2664static void
2665ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002666 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002667{
2668 struct perf_event *event;
2669 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002670
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002671 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2672 /* Ignore events in OFF or ERROR state */
2673 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002674 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002675 /*
2676 * Listen to the 'cpu' scheduling filter constraint
2677 * of events:
2678 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002679 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002680 continue;
2681
Stephane Eraniane5d13672011-02-14 11:20:01 +02002682 /* may need to reset tstamp_enabled */
2683 if (is_cgroup_event(event))
2684 perf_cgroup_mark_enabled(event, ctx);
2685
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002686 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002687 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002688 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002689 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002690 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002691}
2692
2693static void
2694ctx_sched_in(struct perf_event_context *ctx,
2695 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002696 enum event_type_t event_type,
2697 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002698{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002699 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002700 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002701
Peter Zijlstradb24d332011-04-09 21:17:45 +02002702 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002703 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002704 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002705
Stephane Eraniane5d13672011-02-14 11:20:01 +02002706 now = perf_clock();
2707 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002708 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002709 /*
2710 * First go through the list and put on any pinned groups
2711 * in order to give them the best chance of going on.
2712 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002713 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002714 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002715
2716 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002717 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002718 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002719}
2720
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002721static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002722 enum event_type_t event_type,
2723 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002724{
2725 struct perf_event_context *ctx = &cpuctx->ctx;
2726
Stephane Eraniane5d13672011-02-14 11:20:01 +02002727 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002728}
2729
Stephane Eraniane5d13672011-02-14 11:20:01 +02002730static void perf_event_context_sched_in(struct perf_event_context *ctx,
2731 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002732{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002733 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002734
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002735 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002736 if (cpuctx->task_ctx == ctx)
2737 return;
2738
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002739 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002740 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002741 /*
2742 * We want to keep the following priority order:
2743 * cpu pinned (that don't need to move), task pinned,
2744 * cpu flexible, task flexible.
2745 */
2746 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2747
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002748 if (ctx->nr_events)
2749 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002750
Gleb Natapov86b47c22011-11-22 16:08:21 +02002751 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2752
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002753 perf_pmu_enable(ctx->pmu);
2754 perf_ctx_unlock(cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002755}
2756
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002757/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002758 * When sampling the branck stack in system-wide, it may be necessary
2759 * to flush the stack on context switch. This happens when the branch
2760 * stack does not tag its entries with the pid of the current task.
2761 * Otherwise it becomes impossible to associate a branch entry with a
2762 * task. This ambiguity is more likely to appear when the branch stack
2763 * supports priv level filtering and the user sets it to monitor only
2764 * at the user level (which could be a useful measurement in system-wide
2765 * mode). In that case, the risk is high of having a branch stack with
2766 * branch from multiple tasks. Flushing may mean dropping the existing
2767 * entries or stashing them somewhere in the PMU specific code layer.
2768 *
2769 * This function provides the context switch callback to the lower code
2770 * layer. It is invoked ONLY when there is at least one system-wide context
2771 * with at least one active event using taken branch sampling.
2772 */
2773static void perf_branch_stack_sched_in(struct task_struct *prev,
2774 struct task_struct *task)
2775{
2776 struct perf_cpu_context *cpuctx;
2777 struct pmu *pmu;
2778 unsigned long flags;
2779
2780 /* no need to flush branch stack if not changing task */
2781 if (prev == task)
2782 return;
2783
2784 local_irq_save(flags);
2785
2786 rcu_read_lock();
2787
2788 list_for_each_entry_rcu(pmu, &pmus, entry) {
2789 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2790
2791 /*
2792 * check if the context has at least one
2793 * event using PERF_SAMPLE_BRANCH_STACK
2794 */
2795 if (cpuctx->ctx.nr_branch_stack > 0
2796 && pmu->flush_branch_stack) {
2797
Stephane Eraniand010b332012-02-09 23:21:00 +01002798 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2799
2800 perf_pmu_disable(pmu);
2801
2802 pmu->flush_branch_stack();
2803
2804 perf_pmu_enable(pmu);
2805
2806 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2807 }
2808 }
2809
2810 rcu_read_unlock();
2811
2812 local_irq_restore(flags);
2813}
2814
2815/*
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002816 * Called from scheduler to add the events of the current task
2817 * with interrupts disabled.
2818 *
2819 * We restore the event value and then enable it.
2820 *
2821 * This does not protect us against NMI, but enable()
2822 * sets the enabled bit in the control field of event _before_
2823 * accessing the event control register. If a NMI hits, then it will
2824 * keep the event running.
2825 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002826void __perf_event_task_sched_in(struct task_struct *prev,
2827 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002828{
2829 struct perf_event_context *ctx;
2830 int ctxn;
2831
2832 for_each_task_context_nr(ctxn) {
2833 ctx = task->perf_event_ctxp[ctxn];
2834 if (likely(!ctx))
2835 continue;
2836
Stephane Eraniane5d13672011-02-14 11:20:01 +02002837 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002838 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002839 /*
2840 * if cgroup events exist on this CPU, then we need
2841 * to check if we have to switch in PMU state.
2842 * cgroup event are system-wide mode only
2843 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002844 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002845 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002846
2847 /* check for system-wide branch_stack events */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002848 if (atomic_read(this_cpu_ptr(&perf_branch_stack_events)))
Stephane Eraniand010b332012-02-09 23:21:00 +01002849 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002850}
2851
Peter Zijlstraabd50712010-01-26 18:50:16 +01002852static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2853{
2854 u64 frequency = event->attr.sample_freq;
2855 u64 sec = NSEC_PER_SEC;
2856 u64 divisor, dividend;
2857
2858 int count_fls, nsec_fls, frequency_fls, sec_fls;
2859
2860 count_fls = fls64(count);
2861 nsec_fls = fls64(nsec);
2862 frequency_fls = fls64(frequency);
2863 sec_fls = 30;
2864
2865 /*
2866 * We got @count in @nsec, with a target of sample_freq HZ
2867 * the target period becomes:
2868 *
2869 * @count * 10^9
2870 * period = -------------------
2871 * @nsec * sample_freq
2872 *
2873 */
2874
2875 /*
2876 * Reduce accuracy by one bit such that @a and @b converge
2877 * to a similar magnitude.
2878 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002879#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002880do { \
2881 if (a##_fls > b##_fls) { \
2882 a >>= 1; \
2883 a##_fls--; \
2884 } else { \
2885 b >>= 1; \
2886 b##_fls--; \
2887 } \
2888} while (0)
2889
2890 /*
2891 * Reduce accuracy until either term fits in a u64, then proceed with
2892 * the other, so that finally we can do a u64/u64 division.
2893 */
2894 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2895 REDUCE_FLS(nsec, frequency);
2896 REDUCE_FLS(sec, count);
2897 }
2898
2899 if (count_fls + sec_fls > 64) {
2900 divisor = nsec * frequency;
2901
2902 while (count_fls + sec_fls > 64) {
2903 REDUCE_FLS(count, sec);
2904 divisor >>= 1;
2905 }
2906
2907 dividend = count * sec;
2908 } else {
2909 dividend = count * sec;
2910
2911 while (nsec_fls + frequency_fls > 64) {
2912 REDUCE_FLS(nsec, frequency);
2913 dividend >>= 1;
2914 }
2915
2916 divisor = nsec * frequency;
2917 }
2918
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002919 if (!divisor)
2920 return dividend;
2921
Peter Zijlstraabd50712010-01-26 18:50:16 +01002922 return div64_u64(dividend, divisor);
2923}
2924
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002925static DEFINE_PER_CPU(int, perf_throttled_count);
2926static DEFINE_PER_CPU(u64, perf_throttled_seq);
2927
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002928static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002929{
2930 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002931 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002932 s64 delta;
2933
Peter Zijlstraabd50712010-01-26 18:50:16 +01002934 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002935
2936 delta = (s64)(period - hwc->sample_period);
2937 delta = (delta + 7) / 8; /* low pass filter */
2938
2939 sample_period = hwc->sample_period + delta;
2940
2941 if (!sample_period)
2942 sample_period = 1;
2943
2944 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002945
Peter Zijlstrae7850592010-05-21 14:43:08 +02002946 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002947 if (disable)
2948 event->pmu->stop(event, PERF_EF_UPDATE);
2949
Peter Zijlstrae7850592010-05-21 14:43:08 +02002950 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002951
2952 if (disable)
2953 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002954 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002955}
2956
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002957/*
2958 * combine freq adjustment with unthrottling to avoid two passes over the
2959 * events. At the same time, make sure, having freq events does not change
2960 * the rate of unthrottling as that would introduce bias.
2961 */
2962static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2963 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002964{
2965 struct perf_event *event;
2966 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002967 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002968 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002969
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002970 /*
2971 * only need to iterate over all events iff:
2972 * - context have events in frequency mode (needs freq adjust)
2973 * - there are events to unthrottle on this cpu
2974 */
2975 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002976 return;
2977
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002978 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002979 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002980
Paul Mackerras03541f82009-10-14 16:58:03 +11002981 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002982 if (event->state != PERF_EVENT_STATE_ACTIVE)
2983 continue;
2984
Stephane Eranian5632ab12011-01-03 18:20:01 +02002985 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002986 continue;
2987
Alexander Shishkin44377272013-12-16 14:17:36 +02002988 perf_pmu_disable(event->pmu);
2989
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002990 hwc = &event->hw;
2991
Jiri Olsaae23bff2013-08-24 16:45:54 +02002992 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002993 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002994 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002995 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002996 }
2997
2998 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02002999 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003000
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003001 /*
3002 * stop the event and update event->count
3003 */
3004 event->pmu->stop(event, PERF_EF_UPDATE);
3005
Peter Zijlstrae7850592010-05-21 14:43:08 +02003006 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003007 delta = now - hwc->freq_count_stamp;
3008 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003009
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003010 /*
3011 * restart the event
3012 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003013 * we have stopped the event so tell that
3014 * to perf_adjust_period() to avoid stopping it
3015 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003016 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003017 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003018 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003019
3020 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003021 next:
3022 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003023 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003024
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003025 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003026 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003027}
3028
3029/*
3030 * Round-robin a context's events:
3031 */
3032static void rotate_ctx(struct perf_event_context *ctx)
3033{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003034 /*
3035 * Rotate the first entry last of non-pinned groups. Rotation might be
3036 * disabled by the inheritance code.
3037 */
3038 if (!ctx->rotate_disable)
3039 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003040}
3041
Stephane Eranian9e630202013-04-03 14:21:33 +02003042static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003043{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003044 struct perf_event_context *ctx = NULL;
Mark Rutland2fde4f92015-01-07 15:01:54 +00003045 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003046
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003047 if (cpuctx->ctx.nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003048 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3049 rotate = 1;
3050 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003051
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003052 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003053 if (ctx && ctx->nr_events) {
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003054 if (ctx->nr_events != ctx->nr_active)
3055 rotate = 1;
3056 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003057
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003058 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003059 goto done;
3060
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003061 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003062 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003063
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003064 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3065 if (ctx)
3066 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003067
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003068 rotate_ctx(&cpuctx->ctx);
3069 if (ctx)
3070 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003071
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003072 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003073
3074 perf_pmu_enable(cpuctx->ctx.pmu);
3075 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003076done:
Stephane Eranian9e630202013-04-03 14:21:33 +02003077
3078 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003079}
3080
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003081#ifdef CONFIG_NO_HZ_FULL
3082bool perf_event_can_stop_tick(void)
3083{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003084 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003085 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003086 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003087 else
3088 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003089}
3090#endif
3091
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003092void perf_event_task_tick(void)
3093{
Mark Rutland2fde4f92015-01-07 15:01:54 +00003094 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3095 struct perf_event_context *ctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003096 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003097
3098 WARN_ON(!irqs_disabled());
3099
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003100 __this_cpu_inc(perf_throttled_seq);
3101 throttled = __this_cpu_xchg(perf_throttled_count, 0);
3102
Mark Rutland2fde4f92015-01-07 15:01:54 +00003103 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003104 perf_adjust_freq_unthr_context(ctx, throttled);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003105}
3106
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003107static int event_enable_on_exec(struct perf_event *event,
3108 struct perf_event_context *ctx)
3109{
3110 if (!event->attr.enable_on_exec)
3111 return 0;
3112
3113 event->attr.enable_on_exec = 0;
3114 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3115 return 0;
3116
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003117 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003118
3119 return 1;
3120}
3121
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003122/*
3123 * Enable all of a task's events that have been marked enable-on-exec.
3124 * This expects task == current.
3125 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003126static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003127{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003128 struct perf_event_context *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003129 struct perf_event *event;
3130 unsigned long flags;
3131 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003132 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003133
3134 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003135 if (!ctx || !ctx->nr_events)
3136 goto out;
3137
Stephane Eraniane566b762011-04-06 02:54:54 +02003138 /*
3139 * We must ctxsw out cgroup events to avoid conflict
3140 * when invoking perf_task_event_sched_in() later on
3141 * in this function. Otherwise we end up trying to
3142 * ctxswin cgroup events which are already scheduled
3143 * in.
3144 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003145 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003146
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003147 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02003148 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003149
Peter Zijlstrab79387e2011-11-22 11:25:43 +01003150 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003151 ret = event_enable_on_exec(event, ctx);
3152 if (ret)
3153 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003154 }
3155
3156 /*
3157 * Unclone this context if we enabled any event.
3158 */
3159 if (enabled)
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003160 clone_ctx = unclone_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003161
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003162 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003163
Stephane Eraniane566b762011-04-06 02:54:54 +02003164 /*
3165 * Also calls ctxswin for cgroup events, if any:
3166 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003167 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003168out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003169 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003170
3171 if (clone_ctx)
3172 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003173}
3174
Peter Zijlstrae041e322014-05-21 17:32:19 +02003175void perf_event_exec(void)
3176{
3177 struct perf_event_context *ctx;
3178 int ctxn;
3179
3180 rcu_read_lock();
3181 for_each_task_context_nr(ctxn) {
3182 ctx = current->perf_event_ctxp[ctxn];
3183 if (!ctx)
3184 continue;
3185
3186 perf_event_enable_on_exec(ctx);
3187 }
3188 rcu_read_unlock();
3189}
3190
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003191/*
3192 * Cross CPU call to read the hardware event
3193 */
3194static void __perf_event_read(void *info)
3195{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003196 struct perf_event *event = info;
3197 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003198 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003199
3200 /*
3201 * If this is a task context, we need to check whether it is
3202 * the current task context of this cpu. If not it has been
3203 * scheduled out before the smp call arrived. In that case
3204 * event->count would have been updated to a recent sample
3205 * when the event was scheduled out.
3206 */
3207 if (ctx->task && cpuctx->task_ctx != ctx)
3208 return;
3209
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003210 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003211 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003212 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003213 update_cgrp_time_from_event(event);
3214 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003215 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003216 if (event->state == PERF_EVENT_STATE_ACTIVE)
3217 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003218 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003219}
3220
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003221static inline u64 perf_event_count(struct perf_event *event)
3222{
Peter Zijlstrae7850592010-05-21 14:43:08 +02003223 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003224}
3225
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003226static u64 perf_event_read(struct perf_event *event)
3227{
3228 /*
3229 * If event is enabled and currently active on a CPU, update the
3230 * value in the event structure:
3231 */
3232 if (event->state == PERF_EVENT_STATE_ACTIVE) {
3233 smp_call_function_single(event->oncpu,
3234 __perf_event_read, event, 1);
3235 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003236 struct perf_event_context *ctx = event->ctx;
3237 unsigned long flags;
3238
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003239 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003240 /*
3241 * may read while context is not active
3242 * (e.g., thread is blocked), in that case
3243 * we cannot update context time
3244 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003245 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003246 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003247 update_cgrp_time_from_event(event);
3248 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003249 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003250 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003251 }
3252
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003253 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003254}
3255
3256/*
3257 * Initialize the perf_event context in a task_struct:
3258 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003259static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003260{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003261 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003262 mutex_init(&ctx->mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00003263 INIT_LIST_HEAD(&ctx->active_ctx_list);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003264 INIT_LIST_HEAD(&ctx->pinned_groups);
3265 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003266 INIT_LIST_HEAD(&ctx->event_list);
3267 atomic_set(&ctx->refcount, 1);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003268 INIT_DELAYED_WORK(&ctx->orphans_remove, orphans_remove_work);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003269}
3270
Peter Zijlstraeb184472010-09-07 15:55:13 +02003271static struct perf_event_context *
3272alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003273{
3274 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003275
3276 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3277 if (!ctx)
3278 return NULL;
3279
3280 __perf_event_init_context(ctx);
3281 if (task) {
3282 ctx->task = task;
3283 get_task_struct(task);
3284 }
3285 ctx->pmu = pmu;
3286
3287 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003288}
3289
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003290static struct task_struct *
3291find_lively_task_by_vpid(pid_t vpid)
3292{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003293 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003294 int err;
3295
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003296 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003297 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003298 task = current;
3299 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003300 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003301 if (task)
3302 get_task_struct(task);
3303 rcu_read_unlock();
3304
3305 if (!task)
3306 return ERR_PTR(-ESRCH);
3307
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003308 /* Reuse ptrace permission checks for now. */
3309 err = -EACCES;
3310 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3311 goto errout;
3312
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003313 return task;
3314errout:
3315 put_task_struct(task);
3316 return ERR_PTR(err);
3317
3318}
3319
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003320/*
3321 * Returns a matching context with refcount and pincount.
3322 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003323static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07003324find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003325{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003326 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003327 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003328 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003329 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003330
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003331 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003332 /* Must be root to operate on a CPU event: */
3333 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3334 return ERR_PTR(-EACCES);
3335
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003336 /*
3337 * We could be clever and allow to attach a event to an
3338 * offline CPU and activate it when the CPU comes up, but
3339 * that's for later.
3340 */
3341 if (!cpu_online(cpu))
3342 return ERR_PTR(-ENODEV);
3343
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003344 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003345 ctx = &cpuctx->ctx;
3346 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003347 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003348
3349 return ctx;
3350 }
3351
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003352 err = -EINVAL;
3353 ctxn = pmu->task_ctx_nr;
3354 if (ctxn < 0)
3355 goto errout;
3356
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003357retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003358 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003359 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003360 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003361 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003362 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003363
3364 if (clone_ctx)
3365 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003366 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003367 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003368 err = -ENOMEM;
3369 if (!ctx)
3370 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003371
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003372 err = 0;
3373 mutex_lock(&task->perf_event_mutex);
3374 /*
3375 * If it has already passed perf_event_exit_task().
3376 * we must see PF_EXITING, it takes this mutex too.
3377 */
3378 if (task->flags & PF_EXITING)
3379 err = -ESRCH;
3380 else if (task->perf_event_ctxp[ctxn])
3381 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003382 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003383 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003384 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003385 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003386 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003387 mutex_unlock(&task->perf_event_mutex);
3388
3389 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003390 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003391
3392 if (err == -EAGAIN)
3393 goto retry;
3394 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003395 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003396 }
3397
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003398 return ctx;
3399
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003400errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003401 return ERR_PTR(err);
3402}
3403
Li Zefan6fb29152009-10-15 11:21:42 +08003404static void perf_event_free_filter(struct perf_event *event);
3405
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003406static void free_event_rcu(struct rcu_head *head)
3407{
3408 struct perf_event *event;
3409
3410 event = container_of(head, struct perf_event, rcu_head);
3411 if (event->ns)
3412 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003413 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003414 kfree(event);
3415}
3416
Frederic Weisbecker76369132011-05-19 19:55:04 +02003417static void ring_buffer_put(struct ring_buffer *rb);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003418static void ring_buffer_attach(struct perf_event *event,
3419 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003420
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003421static void unaccount_event_cpu(struct perf_event *event, int cpu)
3422{
3423 if (event->parent)
3424 return;
3425
3426 if (has_branch_stack(event)) {
3427 if (!(event->attach_state & PERF_ATTACH_TASK))
3428 atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
3429 }
3430 if (is_cgroup_event(event))
3431 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3432}
3433
3434static void unaccount_event(struct perf_event *event)
3435{
3436 if (event->parent)
3437 return;
3438
3439 if (event->attach_state & PERF_ATTACH_TASK)
3440 static_key_slow_dec_deferred(&perf_sched_events);
3441 if (event->attr.mmap || event->attr.mmap_data)
3442 atomic_dec(&nr_mmap_events);
3443 if (event->attr.comm)
3444 atomic_dec(&nr_comm_events);
3445 if (event->attr.task)
3446 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003447 if (event->attr.freq)
3448 atomic_dec(&nr_freq_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003449 if (is_cgroup_event(event))
3450 static_key_slow_dec_deferred(&perf_sched_events);
3451 if (has_branch_stack(event))
3452 static_key_slow_dec_deferred(&perf_sched_events);
3453
3454 unaccount_event_cpu(event, event->cpu);
3455}
3456
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003457static void __free_event(struct perf_event *event)
3458{
3459 if (!event->parent) {
3460 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3461 put_callchain_buffers();
3462 }
3463
3464 if (event->destroy)
3465 event->destroy(event);
3466
3467 if (event->ctx)
3468 put_ctx(event->ctx);
3469
Yan, Zhengc464c762014-03-18 16:56:41 +08003470 if (event->pmu)
3471 module_put(event->pmu->module);
3472
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003473 call_rcu(&event->rcu_head, free_event_rcu);
3474}
Peter Zijlstra683ede42014-05-05 12:11:24 +02003475
3476static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003477{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003478 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003479
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003480 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003481
Frederic Weisbecker76369132011-05-19 19:55:04 +02003482 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003483 /*
3484 * Can happen when we close an event with re-directed output.
3485 *
3486 * Since we have a 0 refcount, perf_mmap_close() will skip
3487 * over us; possibly making our ring_buffer_put() the last.
3488 */
3489 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003490 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003491 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003492 }
3493
Stephane Eraniane5d13672011-02-14 11:20:01 +02003494 if (is_cgroup_event(event))
3495 perf_detach_cgroup(event);
3496
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003497 __free_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003498}
3499
Peter Zijlstra683ede42014-05-05 12:11:24 +02003500/*
3501 * Used to free events which have a known refcount of 1, such as in error paths
3502 * where the event isn't exposed yet and inherited events.
3503 */
3504static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003505{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003506 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3507 "unexpected event refcount: %ld; ptr=%p\n",
3508 atomic_long_read(&event->refcount), event)) {
3509 /* leak to avoid use-after-free */
3510 return;
3511 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003512
Peter Zijlstra683ede42014-05-05 12:11:24 +02003513 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003514}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003515
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003516/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003517 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003518 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003519static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003520{
Peter Zijlstra88821352010-11-09 19:01:43 +01003521 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003522
Peter Zijlstra88821352010-11-09 19:01:43 +01003523 rcu_read_lock();
3524 owner = ACCESS_ONCE(event->owner);
3525 /*
3526 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3527 * !owner it means the list deletion is complete and we can indeed
3528 * free this event, otherwise we need to serialize on
3529 * owner->perf_event_mutex.
3530 */
3531 smp_read_barrier_depends();
3532 if (owner) {
3533 /*
3534 * Since delayed_put_task_struct() also drops the last
3535 * task reference we can safely take a new reference
3536 * while holding the rcu_read_lock().
3537 */
3538 get_task_struct(owner);
3539 }
3540 rcu_read_unlock();
3541
3542 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003543 /*
3544 * If we're here through perf_event_exit_task() we're already
3545 * holding ctx->mutex which would be an inversion wrt. the
3546 * normal lock order.
3547 *
3548 * However we can safely take this lock because its the child
3549 * ctx->mutex.
3550 */
3551 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
3552
Peter Zijlstra88821352010-11-09 19:01:43 +01003553 /*
3554 * We have to re-check the event->owner field, if it is cleared
3555 * we raced with perf_event_exit_task(), acquiring the mutex
3556 * ensured they're done, and we can proceed with freeing the
3557 * event.
3558 */
3559 if (event->owner)
3560 list_del_init(&event->owner_entry);
3561 mutex_unlock(&owner->perf_event_mutex);
3562 put_task_struct(owner);
3563 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003564}
3565
3566/*
3567 * Called when the last reference to the file is gone.
3568 */
3569static void put_event(struct perf_event *event)
3570{
Peter Zijlstraa83fe282015-01-29 14:44:34 +01003571 struct perf_event_context *ctx;
Jiri Olsaf8697762014-08-01 14:33:01 +02003572
3573 if (!atomic_long_dec_and_test(&event->refcount))
3574 return;
3575
3576 if (!is_kernel_event(event))
3577 perf_remove_from_owner(event);
Peter Zijlstra88821352010-11-09 19:01:43 +01003578
Peter Zijlstra683ede42014-05-05 12:11:24 +02003579 /*
3580 * There are two ways this annotation is useful:
3581 *
3582 * 1) there is a lock recursion from perf_event_exit_task
3583 * see the comment there.
3584 *
3585 * 2) there is a lock-inversion with mmap_sem through
3586 * perf_event_read_group(), which takes faults while
3587 * holding ctx->mutex, however this is called after
3588 * the last filedesc died, so there is no possibility
3589 * to trigger the AB-BA case.
3590 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01003591 ctx = perf_event_ctx_lock_nested(event, SINGLE_DEPTH_NESTING);
3592 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra683ede42014-05-05 12:11:24 +02003593 perf_remove_from_context(event, true);
3594 mutex_unlock(&ctx->mutex);
3595
3596 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003597}
3598
Peter Zijlstra683ede42014-05-05 12:11:24 +02003599int perf_event_release_kernel(struct perf_event *event)
3600{
3601 put_event(event);
3602 return 0;
3603}
3604EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3605
Al Viroa6fa9412012-08-20 14:59:25 +01003606static int perf_release(struct inode *inode, struct file *file)
3607{
3608 put_event(file->private_data);
3609 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003610}
3611
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003612/*
3613 * Remove all orphanes events from the context.
3614 */
3615static void orphans_remove_work(struct work_struct *work)
3616{
3617 struct perf_event_context *ctx;
3618 struct perf_event *event, *tmp;
3619
3620 ctx = container_of(work, struct perf_event_context,
3621 orphans_remove.work);
3622
3623 mutex_lock(&ctx->mutex);
3624 list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) {
3625 struct perf_event *parent_event = event->parent;
3626
3627 if (!is_orphaned_child(event))
3628 continue;
3629
3630 perf_remove_from_context(event, true);
3631
3632 mutex_lock(&parent_event->child_mutex);
3633 list_del_init(&event->child_list);
3634 mutex_unlock(&parent_event->child_mutex);
3635
3636 free_event(event);
3637 put_event(parent_event);
3638 }
3639
3640 raw_spin_lock_irq(&ctx->lock);
3641 ctx->orphans_remove_sched = false;
3642 raw_spin_unlock_irq(&ctx->lock);
3643 mutex_unlock(&ctx->mutex);
3644
3645 put_ctx(ctx);
3646}
3647
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003648u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003649{
3650 struct perf_event *child;
3651 u64 total = 0;
3652
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003653 *enabled = 0;
3654 *running = 0;
3655
Peter Zijlstra6f105812009-11-20 22:19:56 +01003656 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003657 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003658 *enabled += event->total_time_enabled +
3659 atomic64_read(&event->child_total_time_enabled);
3660 *running += event->total_time_running +
3661 atomic64_read(&event->child_total_time_running);
3662
3663 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003664 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003665 *enabled += child->total_time_enabled;
3666 *running += child->total_time_running;
3667 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003668 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003669
3670 return total;
3671}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003672EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003673
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003674static int perf_event_read_group(struct perf_event *event,
3675 u64 read_format, char __user *buf)
3676{
3677 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003678 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003679 int n = 0, size = 0, ret;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003680 u64 count, enabled, running;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003681 u64 values[5];
Peter Zijlstraabf48682009-11-20 22:19:49 +01003682
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003683 lockdep_assert_held(&ctx->mutex);
3684
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003685 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003686
3687 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003688 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3689 values[n++] = enabled;
3690 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3691 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003692 values[n++] = count;
3693 if (read_format & PERF_FORMAT_ID)
3694 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003695
3696 size = n * sizeof(u64);
3697
3698 if (copy_to_user(buf, values, size))
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003699 return -EFAULT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003700
Peter Zijlstra6f105812009-11-20 22:19:56 +01003701 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003702
3703 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003704 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003705
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003706 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003707 if (read_format & PERF_FORMAT_ID)
3708 values[n++] = primary_event_id(sub);
3709
3710 size = n * sizeof(u64);
3711
Stephane Eranian184d3da2009-11-23 21:40:49 -08003712 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003713 return -EFAULT;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003714 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003715
3716 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003717 }
3718
Peter Zijlstraabf48682009-11-20 22:19:49 +01003719 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003720}
3721
3722static int perf_event_read_one(struct perf_event *event,
3723 u64 read_format, char __user *buf)
3724{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003725 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003726 u64 values[4];
3727 int n = 0;
3728
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003729 values[n++] = perf_event_read_value(event, &enabled, &running);
3730 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3731 values[n++] = enabled;
3732 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3733 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003734 if (read_format & PERF_FORMAT_ID)
3735 values[n++] = primary_event_id(event);
3736
3737 if (copy_to_user(buf, values, n * sizeof(u64)))
3738 return -EFAULT;
3739
3740 return n * sizeof(u64);
3741}
3742
Jiri Olsadc633982014-09-12 13:18:26 +02003743static bool is_event_hup(struct perf_event *event)
3744{
3745 bool no_children;
3746
3747 if (event->state != PERF_EVENT_STATE_EXIT)
3748 return false;
3749
3750 mutex_lock(&event->child_mutex);
3751 no_children = list_empty(&event->child_list);
3752 mutex_unlock(&event->child_mutex);
3753 return no_children;
3754}
3755
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003756/*
3757 * Read the performance event - simple non blocking version for now
3758 */
3759static ssize_t
3760perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3761{
3762 u64 read_format = event->attr.read_format;
3763 int ret;
3764
3765 /*
3766 * Return end-of-file for a read on a event that is in
3767 * error state (i.e. because it was pinned but it couldn't be
3768 * scheduled on to the CPU at some point).
3769 */
3770 if (event->state == PERF_EVENT_STATE_ERROR)
3771 return 0;
3772
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003773 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003774 return -ENOSPC;
3775
3776 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003777 if (read_format & PERF_FORMAT_GROUP)
3778 ret = perf_event_read_group(event, read_format, buf);
3779 else
3780 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003781
3782 return ret;
3783}
3784
3785static ssize_t
3786perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3787{
3788 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003789 struct perf_event_context *ctx;
3790 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003791
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003792 ctx = perf_event_ctx_lock(event);
3793 ret = perf_read_hw(event, buf, count);
3794 perf_event_ctx_unlock(event, ctx);
3795
3796 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003797}
3798
3799static unsigned int perf_poll(struct file *file, poll_table *wait)
3800{
3801 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003802 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02003803 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003804
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02003805 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04003806
Jiri Olsadc633982014-09-12 13:18:26 +02003807 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04003808 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003809
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003810 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003811 * Pin the event->rb by taking event->mmap_mutex; otherwise
3812 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003813 */
3814 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003815 rb = event->rb;
3816 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003817 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003818 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003819 return events;
3820}
3821
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003822static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003823{
3824 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003825 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003826 perf_event_update_userpage(event);
3827}
3828
3829/*
3830 * Holding the top-level event's child_mutex means that any
3831 * descendant process that has inherited this event will block
3832 * in sync_child_event if it goes to exit, thus satisfying the
3833 * task existence requirements of perf_event_enable/disable.
3834 */
3835static void perf_event_for_each_child(struct perf_event *event,
3836 void (*func)(struct perf_event *))
3837{
3838 struct perf_event *child;
3839
3840 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003841
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003842 mutex_lock(&event->child_mutex);
3843 func(event);
3844 list_for_each_entry(child, &event->child_list, child_list)
3845 func(child);
3846 mutex_unlock(&event->child_mutex);
3847}
3848
3849static void perf_event_for_each(struct perf_event *event,
3850 void (*func)(struct perf_event *))
3851{
3852 struct perf_event_context *ctx = event->ctx;
3853 struct perf_event *sibling;
3854
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003855 lockdep_assert_held(&ctx->mutex);
3856
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003857 event = event->group_leader;
3858
3859 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003860 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003861 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003862}
3863
3864static int perf_event_period(struct perf_event *event, u64 __user *arg)
3865{
3866 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrabad71922013-11-27 13:54:38 +00003867 int ret = 0, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003868 u64 value;
3869
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003870 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003871 return -EINVAL;
3872
John Blackwoodad0cf342010-09-28 18:03:11 -04003873 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003874 return -EFAULT;
3875
3876 if (!value)
3877 return -EINVAL;
3878
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003879 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003880 if (event->attr.freq) {
3881 if (value > sysctl_perf_event_sample_rate) {
3882 ret = -EINVAL;
3883 goto unlock;
3884 }
3885
3886 event->attr.sample_freq = value;
3887 } else {
3888 event->attr.sample_period = value;
3889 event->hw.sample_period = value;
3890 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00003891
3892 active = (event->state == PERF_EVENT_STATE_ACTIVE);
3893 if (active) {
3894 perf_pmu_disable(ctx->pmu);
3895 event->pmu->stop(event, PERF_EF_UPDATE);
3896 }
3897
3898 local64_set(&event->hw.period_left, 0);
3899
3900 if (active) {
3901 event->pmu->start(event, PERF_EF_RELOAD);
3902 perf_pmu_enable(ctx->pmu);
3903 }
3904
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003905unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003906 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003907
3908 return ret;
3909}
3910
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003911static const struct file_operations perf_fops;
3912
Al Viro2903ff02012-08-28 12:52:22 -04003913static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003914{
Al Viro2903ff02012-08-28 12:52:22 -04003915 struct fd f = fdget(fd);
3916 if (!f.file)
3917 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003918
Al Viro2903ff02012-08-28 12:52:22 -04003919 if (f.file->f_op != &perf_fops) {
3920 fdput(f);
3921 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003922 }
Al Viro2903ff02012-08-28 12:52:22 -04003923 *p = f;
3924 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003925}
3926
3927static int perf_event_set_output(struct perf_event *event,
3928 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003929static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003930
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003931static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003932{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003933 void (*func)(struct perf_event *);
3934 u32 flags = arg;
3935
3936 switch (cmd) {
3937 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003938 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003939 break;
3940 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003941 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003942 break;
3943 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003944 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003945 break;
3946
3947 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003948 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003949
3950 case PERF_EVENT_IOC_PERIOD:
3951 return perf_event_period(event, (u64 __user *)arg);
3952
Jiri Olsacf4957f2012-10-24 13:37:58 +02003953 case PERF_EVENT_IOC_ID:
3954 {
3955 u64 id = primary_event_id(event);
3956
3957 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
3958 return -EFAULT;
3959 return 0;
3960 }
3961
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003962 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003963 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003964 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003965 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003966 struct perf_event *output_event;
3967 struct fd output;
3968 ret = perf_fget_light(arg, &output);
3969 if (ret)
3970 return ret;
3971 output_event = output.file->private_data;
3972 ret = perf_event_set_output(event, output_event);
3973 fdput(output);
3974 } else {
3975 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003976 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003977 return ret;
3978 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003979
Li Zefan6fb29152009-10-15 11:21:42 +08003980 case PERF_EVENT_IOC_SET_FILTER:
3981 return perf_event_set_filter(event, (void __user *)arg);
3982
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003983 default:
3984 return -ENOTTY;
3985 }
3986
3987 if (flags & PERF_IOC_FLAG_GROUP)
3988 perf_event_for_each(event, func);
3989 else
3990 perf_event_for_each_child(event, func);
3991
3992 return 0;
3993}
3994
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003995static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3996{
3997 struct perf_event *event = file->private_data;
3998 struct perf_event_context *ctx;
3999 long ret;
4000
4001 ctx = perf_event_ctx_lock(event);
4002 ret = _perf_ioctl(event, cmd, arg);
4003 perf_event_ctx_unlock(event, ctx);
4004
4005 return ret;
4006}
4007
Pawel Mollb3f20782014-06-13 16:03:32 +01004008#ifdef CONFIG_COMPAT
4009static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4010 unsigned long arg)
4011{
4012 switch (_IOC_NR(cmd)) {
4013 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4014 case _IOC_NR(PERF_EVENT_IOC_ID):
4015 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4016 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4017 cmd &= ~IOCSIZE_MASK;
4018 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4019 }
4020 break;
4021 }
4022 return perf_ioctl(file, cmd, arg);
4023}
4024#else
4025# define perf_compat_ioctl NULL
4026#endif
4027
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004028int perf_event_task_enable(void)
4029{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004030 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004031 struct perf_event *event;
4032
4033 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004034 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4035 ctx = perf_event_ctx_lock(event);
4036 perf_event_for_each_child(event, _perf_event_enable);
4037 perf_event_ctx_unlock(event, ctx);
4038 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004039 mutex_unlock(&current->perf_event_mutex);
4040
4041 return 0;
4042}
4043
4044int perf_event_task_disable(void)
4045{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004046 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004047 struct perf_event *event;
4048
4049 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004050 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4051 ctx = perf_event_ctx_lock(event);
4052 perf_event_for_each_child(event, _perf_event_disable);
4053 perf_event_ctx_unlock(event, ctx);
4054 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004055 mutex_unlock(&current->perf_event_mutex);
4056
4057 return 0;
4058}
4059
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004060static int perf_event_index(struct perf_event *event)
4061{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004062 if (event->hw.state & PERF_HES_STOPPED)
4063 return 0;
4064
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004065 if (event->state != PERF_EVENT_STATE_ACTIVE)
4066 return 0;
4067
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004068 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004069}
4070
Eric B Munsonc4794292011-06-23 16:34:38 -04004071static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004072 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004073 u64 *enabled,
4074 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004075{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004076 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004077
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004078 *now = perf_clock();
4079 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004080 *enabled = ctx_time - event->tstamp_enabled;
4081 *running = ctx_time - event->tstamp_running;
4082}
4083
Peter Zijlstrafa731582013-09-19 10:16:42 +02004084static void perf_event_init_userpage(struct perf_event *event)
4085{
4086 struct perf_event_mmap_page *userpg;
4087 struct ring_buffer *rb;
4088
4089 rcu_read_lock();
4090 rb = rcu_dereference(event->rb);
4091 if (!rb)
4092 goto unlock;
4093
4094 userpg = rb->user_page;
4095
4096 /* Allow new userspace to detect that bit 0 is deprecated */
4097 userpg->cap_bit0_is_deprecated = 1;
4098 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
4099
4100unlock:
4101 rcu_read_unlock();
4102}
4103
Peter Zijlstrac7206202012-03-22 17:26:36 +01004104void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004105{
4106}
4107
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004108/*
4109 * Callers need to ensure there can be no nesting of this function, otherwise
4110 * the seqlock logic goes bad. We can not serialize this because the arch
4111 * code calls this from NMI context.
4112 */
4113void perf_event_update_userpage(struct perf_event *event)
4114{
4115 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004116 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004117 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004118
4119 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004120 rb = rcu_dereference(event->rb);
4121 if (!rb)
4122 goto unlock;
4123
Eric B Munson0d641202011-06-24 12:26:26 -04004124 /*
4125 * compute total_time_enabled, total_time_running
4126 * based on snapshot values taken when the event
4127 * was last scheduled in.
4128 *
4129 * we cannot simply called update_context_time()
4130 * because of locking issue as we can be called in
4131 * NMI context
4132 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004133 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004134
Frederic Weisbecker76369132011-05-19 19:55:04 +02004135 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004136 /*
4137 * Disable preemption so as to not let the corresponding user-space
4138 * spin too long if we get preempted.
4139 */
4140 preempt_disable();
4141 ++userpg->lock;
4142 barrier();
4143 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004144 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004145 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004146 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004147
Eric B Munson0d641202011-06-24 12:26:26 -04004148 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004149 atomic64_read(&event->child_total_time_enabled);
4150
Eric B Munson0d641202011-06-24 12:26:26 -04004151 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004152 atomic64_read(&event->child_total_time_running);
4153
Peter Zijlstrac7206202012-03-22 17:26:36 +01004154 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004155
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004156 barrier();
4157 ++userpg->lock;
4158 preempt_enable();
4159unlock:
4160 rcu_read_unlock();
4161}
4162
Peter Zijlstra906010b2009-09-21 16:08:49 +02004163static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4164{
4165 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004166 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004167 int ret = VM_FAULT_SIGBUS;
4168
4169 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4170 if (vmf->pgoff == 0)
4171 ret = 0;
4172 return ret;
4173 }
4174
4175 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004176 rb = rcu_dereference(event->rb);
4177 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004178 goto unlock;
4179
4180 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4181 goto unlock;
4182
Frederic Weisbecker76369132011-05-19 19:55:04 +02004183 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004184 if (!vmf->page)
4185 goto unlock;
4186
4187 get_page(vmf->page);
4188 vmf->page->mapping = vma->vm_file->f_mapping;
4189 vmf->page->index = vmf->pgoff;
4190
4191 ret = 0;
4192unlock:
4193 rcu_read_unlock();
4194
4195 return ret;
4196}
4197
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004198static void ring_buffer_attach(struct perf_event *event,
4199 struct ring_buffer *rb)
4200{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004201 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004202 unsigned long flags;
4203
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004204 if (event->rb) {
4205 /*
4206 * Should be impossible, we set this when removing
4207 * event->rb_entry and wait/clear when adding event->rb_entry.
4208 */
4209 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004210
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004211 old_rb = event->rb;
4212 event->rcu_batches = get_state_synchronize_rcu();
4213 event->rcu_pending = 1;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004214
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004215 spin_lock_irqsave(&old_rb->event_lock, flags);
4216 list_del_rcu(&event->rb_entry);
4217 spin_unlock_irqrestore(&old_rb->event_lock, flags);
4218 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004219
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004220 if (event->rcu_pending && rb) {
4221 cond_synchronize_rcu(event->rcu_batches);
4222 event->rcu_pending = 0;
4223 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004224
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004225 if (rb) {
4226 spin_lock_irqsave(&rb->event_lock, flags);
4227 list_add_rcu(&event->rb_entry, &rb->event_list);
4228 spin_unlock_irqrestore(&rb->event_lock, flags);
4229 }
4230
4231 rcu_assign_pointer(event->rb, rb);
4232
4233 if (old_rb) {
4234 ring_buffer_put(old_rb);
4235 /*
4236 * Since we detached before setting the new rb, so that we
4237 * could attach the new rb, we could have missed a wakeup.
4238 * Provide it now.
4239 */
4240 wake_up_all(&event->waitq);
4241 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004242}
4243
4244static void ring_buffer_wakeup(struct perf_event *event)
4245{
4246 struct ring_buffer *rb;
4247
4248 rcu_read_lock();
4249 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004250 if (rb) {
4251 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4252 wake_up_all(&event->waitq);
4253 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004254 rcu_read_unlock();
4255}
4256
Frederic Weisbecker76369132011-05-19 19:55:04 +02004257static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004258{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004259 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004260
Frederic Weisbecker76369132011-05-19 19:55:04 +02004261 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
4262 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004263}
4264
Frederic Weisbecker76369132011-05-19 19:55:04 +02004265static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004266{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004267 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004268
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004269 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004270 rb = rcu_dereference(event->rb);
4271 if (rb) {
4272 if (!atomic_inc_not_zero(&rb->refcount))
4273 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004274 }
4275 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004276
Frederic Weisbecker76369132011-05-19 19:55:04 +02004277 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004278}
4279
Frederic Weisbecker76369132011-05-19 19:55:04 +02004280static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004281{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004282 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004283 return;
4284
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004285 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004286
Frederic Weisbecker76369132011-05-19 19:55:04 +02004287 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004288}
4289
4290static void perf_mmap_open(struct vm_area_struct *vma)
4291{
4292 struct perf_event *event = vma->vm_file->private_data;
4293
4294 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004295 atomic_inc(&event->rb->mmap_count);
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004296
4297 if (event->pmu->event_mapped)
4298 event->pmu->event_mapped(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004299}
4300
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004301/*
4302 * A buffer can be mmap()ed multiple times; either directly through the same
4303 * event, or through other events by use of perf_event_set_output().
4304 *
4305 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4306 * the buffer here, where we still have a VM context. This means we need
4307 * to detach all events redirecting to us.
4308 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004309static void perf_mmap_close(struct vm_area_struct *vma)
4310{
4311 struct perf_event *event = vma->vm_file->private_data;
4312
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004313 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004314 struct user_struct *mmap_user = rb->mmap_user;
4315 int mmap_locked = rb->mmap_locked;
4316 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004317
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004318 if (event->pmu->event_unmapped)
4319 event->pmu->event_unmapped(event);
4320
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004321 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004322
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004323 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004324 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004325
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004326 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004327 mutex_unlock(&event->mmap_mutex);
4328
4329 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004330 if (atomic_read(&rb->mmap_count))
4331 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004332
4333 /*
4334 * No other mmap()s, detach from all other events that might redirect
4335 * into the now unreachable buffer. Somewhat complicated by the
4336 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4337 */
4338again:
4339 rcu_read_lock();
4340 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4341 if (!atomic_long_inc_not_zero(&event->refcount)) {
4342 /*
4343 * This event is en-route to free_event() which will
4344 * detach it and remove it from the list.
4345 */
4346 continue;
4347 }
4348 rcu_read_unlock();
4349
4350 mutex_lock(&event->mmap_mutex);
4351 /*
4352 * Check we didn't race with perf_event_set_output() which can
4353 * swizzle the rb from under us while we were waiting to
4354 * acquire mmap_mutex.
4355 *
4356 * If we find a different rb; ignore this event, a next
4357 * iteration will no longer find it on the list. We have to
4358 * still restart the iteration to make sure we're not now
4359 * iterating the wrong list.
4360 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004361 if (event->rb == rb)
4362 ring_buffer_attach(event, NULL);
4363
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004364 mutex_unlock(&event->mmap_mutex);
4365 put_event(event);
4366
4367 /*
4368 * Restart the iteration; either we're on the wrong list or
4369 * destroyed its integrity by doing a deletion.
4370 */
4371 goto again;
4372 }
4373 rcu_read_unlock();
4374
4375 /*
4376 * It could be there's still a few 0-ref events on the list; they'll
4377 * get cleaned up by free_event() -- they'll also still have their
4378 * ref on the rb and will free it whenever they are done with it.
4379 *
4380 * Aside from that, this buffer is 'fully' detached and unmapped,
4381 * undo the VM accounting.
4382 */
4383
4384 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4385 vma->vm_mm->pinned_vm -= mmap_locked;
4386 free_uid(mmap_user);
4387
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004388out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004389 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004390}
4391
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004392static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004393 .open = perf_mmap_open,
4394 .close = perf_mmap_close,
4395 .fault = perf_mmap_fault,
4396 .page_mkwrite = perf_mmap_fault,
4397};
4398
4399static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4400{
4401 struct perf_event *event = file->private_data;
4402 unsigned long user_locked, user_lock_limit;
4403 struct user_struct *user = current_user();
4404 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004405 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004406 unsigned long vma_size;
4407 unsigned long nr_pages;
4408 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004409 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004410
Peter Zijlstrac7920612010-05-18 10:33:24 +02004411 /*
4412 * Don't allow mmap() of inherited per-task counters. This would
4413 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004414 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004415 */
4416 if (event->cpu == -1 && event->attr.inherit)
4417 return -EINVAL;
4418
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004419 if (!(vma->vm_flags & VM_SHARED))
4420 return -EINVAL;
4421
4422 vma_size = vma->vm_end - vma->vm_start;
4423 nr_pages = (vma_size / PAGE_SIZE) - 1;
4424
4425 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004426 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004427 * can do bitmasks instead of modulo.
4428 */
4429 if (nr_pages != 0 && !is_power_of_2(nr_pages))
4430 return -EINVAL;
4431
4432 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4433 return -EINVAL;
4434
4435 if (vma->vm_pgoff != 0)
4436 return -EINVAL;
4437
4438 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004439again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004440 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004441 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004442 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004443 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004444 goto unlock;
4445 }
4446
4447 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4448 /*
4449 * Raced against perf_mmap_close() through
4450 * perf_event_set_output(). Try again, hope for better
4451 * luck.
4452 */
4453 mutex_unlock(&event->mmap_mutex);
4454 goto again;
4455 }
4456
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004457 goto unlock;
4458 }
4459
4460 user_extra = nr_pages + 1;
4461 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4462
4463 /*
4464 * Increase the limit linearly with more CPUs:
4465 */
4466 user_lock_limit *= num_online_cpus();
4467
4468 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4469
4470 extra = 0;
4471 if (user_locked > user_lock_limit)
4472 extra = user_locked - user_lock_limit;
4473
Jiri Slaby78d7d402010-03-05 13:42:54 -08004474 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004475 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004476 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004477
4478 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4479 !capable(CAP_IPC_LOCK)) {
4480 ret = -EPERM;
4481 goto unlock;
4482 }
4483
Frederic Weisbecker76369132011-05-19 19:55:04 +02004484 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004485
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004486 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004487 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004488
Vince Weaver4ec83632011-06-01 15:15:36 -04004489 rb = rb_alloc(nr_pages,
4490 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4491 event->cpu, flags);
4492
Frederic Weisbecker76369132011-05-19 19:55:04 +02004493 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004494 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004495 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004496 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004497
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004498 atomic_set(&rb->mmap_count, 1);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004499 rb->mmap_locked = extra;
4500 rb->mmap_user = get_current_user();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004501
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004502 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004503 vma->vm_mm->pinned_vm += extra;
4504
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004505 ring_buffer_attach(event, rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004506
Peter Zijlstrafa731582013-09-19 10:16:42 +02004507 perf_event_init_userpage(event);
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01004508 perf_event_update_userpage(event);
4509
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004510unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004511 if (!ret)
4512 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004513 mutex_unlock(&event->mmap_mutex);
4514
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004515 /*
4516 * Since pinned accounting is per vm we cannot allow fork() to copy our
4517 * vma.
4518 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004519 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004520 vma->vm_ops = &perf_mmap_vmops;
4521
Andy Lutomirski1e0fb9e2014-10-24 15:58:10 -07004522 if (event->pmu->event_mapped)
4523 event->pmu->event_mapped(event);
4524
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004525 return ret;
4526}
4527
4528static int perf_fasync(int fd, struct file *filp, int on)
4529{
Al Viro496ad9a2013-01-23 17:07:38 -05004530 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004531 struct perf_event *event = filp->private_data;
4532 int retval;
4533
4534 mutex_lock(&inode->i_mutex);
4535 retval = fasync_helper(fd, filp, on, &event->fasync);
4536 mutex_unlock(&inode->i_mutex);
4537
4538 if (retval < 0)
4539 return retval;
4540
4541 return 0;
4542}
4543
4544static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004545 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004546 .release = perf_release,
4547 .read = perf_read,
4548 .poll = perf_poll,
4549 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01004550 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004551 .mmap = perf_mmap,
4552 .fasync = perf_fasync,
4553};
4554
4555/*
4556 * Perf event wakeup
4557 *
4558 * If there's data, ensure we set the poll() state and publish everything
4559 * to user-space before waking everybody up.
4560 */
4561
4562void perf_event_wakeup(struct perf_event *event)
4563{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004564 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004565
4566 if (event->pending_kill) {
4567 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4568 event->pending_kill = 0;
4569 }
4570}
4571
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004572static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004573{
4574 struct perf_event *event = container_of(entry,
4575 struct perf_event, pending);
4576
4577 if (event->pending_disable) {
4578 event->pending_disable = 0;
4579 __perf_event_disable(event);
4580 }
4581
4582 if (event->pending_wakeup) {
4583 event->pending_wakeup = 0;
4584 perf_event_wakeup(event);
4585 }
4586}
4587
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004588/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004589 * We assume there is only KVM supporting the callbacks.
4590 * Later on, we might change it to a list if there is
4591 * another virtualization implementation supporting the callbacks.
4592 */
4593struct perf_guest_info_callbacks *perf_guest_cbs;
4594
4595int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4596{
4597 perf_guest_cbs = cbs;
4598 return 0;
4599}
4600EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4601
4602int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4603{
4604 perf_guest_cbs = NULL;
4605 return 0;
4606}
4607EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4608
Jiri Olsa40189942012-08-07 15:20:37 +02004609static void
4610perf_output_sample_regs(struct perf_output_handle *handle,
4611 struct pt_regs *regs, u64 mask)
4612{
4613 int bit;
4614
4615 for_each_set_bit(bit, (const unsigned long *) &mask,
4616 sizeof(mask) * BITS_PER_BYTE) {
4617 u64 val;
4618
4619 val = perf_reg_value(regs, bit);
4620 perf_output_put(handle, val);
4621 }
4622}
4623
Stephane Eranian60e23642014-09-24 13:48:37 +02004624static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004625 struct pt_regs *regs,
4626 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02004627{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004628 if (user_mode(regs)) {
4629 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02004630 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004631 } else if (current->mm) {
4632 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02004633 } else {
4634 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
4635 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02004636 }
4637}
4638
Stephane Eranian60e23642014-09-24 13:48:37 +02004639static void perf_sample_regs_intr(struct perf_regs *regs_intr,
4640 struct pt_regs *regs)
4641{
4642 regs_intr->regs = regs;
4643 regs_intr->abi = perf_reg_abi(current);
4644}
4645
4646
Jiri Olsac5ebced2012-08-07 15:20:40 +02004647/*
4648 * Get remaining task size from user stack pointer.
4649 *
4650 * It'd be better to take stack vma map and limit this more
4651 * precisly, but there's no way to get it safely under interrupt,
4652 * so using TASK_SIZE as limit.
4653 */
4654static u64 perf_ustack_task_size(struct pt_regs *regs)
4655{
4656 unsigned long addr = perf_user_stack_pointer(regs);
4657
4658 if (!addr || addr >= TASK_SIZE)
4659 return 0;
4660
4661 return TASK_SIZE - addr;
4662}
4663
4664static u16
4665perf_sample_ustack_size(u16 stack_size, u16 header_size,
4666 struct pt_regs *regs)
4667{
4668 u64 task_size;
4669
4670 /* No regs, no stack pointer, no dump. */
4671 if (!regs)
4672 return 0;
4673
4674 /*
4675 * Check if we fit in with the requested stack size into the:
4676 * - TASK_SIZE
4677 * If we don't, we limit the size to the TASK_SIZE.
4678 *
4679 * - remaining sample size
4680 * If we don't, we customize the stack size to
4681 * fit in to the remaining sample size.
4682 */
4683
4684 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4685 stack_size = min(stack_size, (u16) task_size);
4686
4687 /* Current header size plus static size and dynamic size. */
4688 header_size += 2 * sizeof(u64);
4689
4690 /* Do we fit in with the current stack dump size? */
4691 if ((u16) (header_size + stack_size) < header_size) {
4692 /*
4693 * If we overflow the maximum size for the sample,
4694 * we customize the stack dump size to fit in.
4695 */
4696 stack_size = USHRT_MAX - header_size - sizeof(u64);
4697 stack_size = round_up(stack_size, sizeof(u64));
4698 }
4699
4700 return stack_size;
4701}
4702
4703static void
4704perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4705 struct pt_regs *regs)
4706{
4707 /* Case of a kernel thread, nothing to dump */
4708 if (!regs) {
4709 u64 size = 0;
4710 perf_output_put(handle, size);
4711 } else {
4712 unsigned long sp;
4713 unsigned int rem;
4714 u64 dyn_size;
4715
4716 /*
4717 * We dump:
4718 * static size
4719 * - the size requested by user or the best one we can fit
4720 * in to the sample max size
4721 * data
4722 * - user stack dump data
4723 * dynamic size
4724 * - the actual dumped size
4725 */
4726
4727 /* Static size. */
4728 perf_output_put(handle, dump_size);
4729
4730 /* Data. */
4731 sp = perf_user_stack_pointer(regs);
4732 rem = __output_copy_user(handle, (void *) sp, dump_size);
4733 dyn_size = dump_size - rem;
4734
4735 perf_output_skip(handle, rem);
4736
4737 /* Dynamic size. */
4738 perf_output_put(handle, dyn_size);
4739 }
4740}
4741
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004742static void __perf_event_header__init_id(struct perf_event_header *header,
4743 struct perf_sample_data *data,
4744 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004745{
4746 u64 sample_type = event->attr.sample_type;
4747
4748 data->type = sample_type;
4749 header->size += event->id_header_size;
4750
4751 if (sample_type & PERF_SAMPLE_TID) {
4752 /* namespace issues */
4753 data->tid_entry.pid = perf_event_pid(event, current);
4754 data->tid_entry.tid = perf_event_tid(event, current);
4755 }
4756
4757 if (sample_type & PERF_SAMPLE_TIME)
4758 data->time = perf_clock();
4759
Adrian Hunterff3d5272013-08-27 11:23:07 +03004760 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004761 data->id = primary_event_id(event);
4762
4763 if (sample_type & PERF_SAMPLE_STREAM_ID)
4764 data->stream_id = event->id;
4765
4766 if (sample_type & PERF_SAMPLE_CPU) {
4767 data->cpu_entry.cpu = raw_smp_processor_id();
4768 data->cpu_entry.reserved = 0;
4769 }
4770}
4771
Frederic Weisbecker76369132011-05-19 19:55:04 +02004772void perf_event_header__init_id(struct perf_event_header *header,
4773 struct perf_sample_data *data,
4774 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004775{
4776 if (event->attr.sample_id_all)
4777 __perf_event_header__init_id(header, data, event);
4778}
4779
4780static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4781 struct perf_sample_data *data)
4782{
4783 u64 sample_type = data->type;
4784
4785 if (sample_type & PERF_SAMPLE_TID)
4786 perf_output_put(handle, data->tid_entry);
4787
4788 if (sample_type & PERF_SAMPLE_TIME)
4789 perf_output_put(handle, data->time);
4790
4791 if (sample_type & PERF_SAMPLE_ID)
4792 perf_output_put(handle, data->id);
4793
4794 if (sample_type & PERF_SAMPLE_STREAM_ID)
4795 perf_output_put(handle, data->stream_id);
4796
4797 if (sample_type & PERF_SAMPLE_CPU)
4798 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03004799
4800 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4801 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004802}
4803
Frederic Weisbecker76369132011-05-19 19:55:04 +02004804void perf_event__output_id_sample(struct perf_event *event,
4805 struct perf_output_handle *handle,
4806 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004807{
4808 if (event->attr.sample_id_all)
4809 __perf_event__output_id_sample(handle, sample);
4810}
4811
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004812static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004813 struct perf_event *event,
4814 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004815{
4816 u64 read_format = event->attr.read_format;
4817 u64 values[4];
4818 int n = 0;
4819
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004820 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004821 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004822 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004823 atomic64_read(&event->child_total_time_enabled);
4824 }
4825 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004826 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004827 atomic64_read(&event->child_total_time_running);
4828 }
4829 if (read_format & PERF_FORMAT_ID)
4830 values[n++] = primary_event_id(event);
4831
Frederic Weisbecker76369132011-05-19 19:55:04 +02004832 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004833}
4834
4835/*
4836 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4837 */
4838static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004839 struct perf_event *event,
4840 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004841{
4842 struct perf_event *leader = event->group_leader, *sub;
4843 u64 read_format = event->attr.read_format;
4844 u64 values[5];
4845 int n = 0;
4846
4847 values[n++] = 1 + leader->nr_siblings;
4848
4849 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004850 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004851
4852 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004853 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004854
4855 if (leader != event)
4856 leader->pmu->read(leader);
4857
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004858 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004859 if (read_format & PERF_FORMAT_ID)
4860 values[n++] = primary_event_id(leader);
4861
Frederic Weisbecker76369132011-05-19 19:55:04 +02004862 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004863
4864 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4865 n = 0;
4866
Jiri Olsa6f5ab002012-10-15 20:13:45 +02004867 if ((sub != event) &&
4868 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004869 sub->pmu->read(sub);
4870
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004871 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004872 if (read_format & PERF_FORMAT_ID)
4873 values[n++] = primary_event_id(sub);
4874
Frederic Weisbecker76369132011-05-19 19:55:04 +02004875 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004876 }
4877}
4878
Stephane Eranianeed01522010-10-26 16:08:01 +02004879#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4880 PERF_FORMAT_TOTAL_TIME_RUNNING)
4881
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004882static void perf_output_read(struct perf_output_handle *handle,
4883 struct perf_event *event)
4884{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004885 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004886 u64 read_format = event->attr.read_format;
4887
4888 /*
4889 * compute total_time_enabled, total_time_running
4890 * based on snapshot values taken when the event
4891 * was last scheduled in.
4892 *
4893 * we cannot simply called update_context_time()
4894 * because of locking issue as we are called in
4895 * NMI context
4896 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004897 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004898 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004899
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004900 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004901 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004902 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004903 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004904}
4905
4906void perf_output_sample(struct perf_output_handle *handle,
4907 struct perf_event_header *header,
4908 struct perf_sample_data *data,
4909 struct perf_event *event)
4910{
4911 u64 sample_type = data->type;
4912
4913 perf_output_put(handle, *header);
4914
Adrian Hunterff3d5272013-08-27 11:23:07 +03004915 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4916 perf_output_put(handle, data->id);
4917
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004918 if (sample_type & PERF_SAMPLE_IP)
4919 perf_output_put(handle, data->ip);
4920
4921 if (sample_type & PERF_SAMPLE_TID)
4922 perf_output_put(handle, data->tid_entry);
4923
4924 if (sample_type & PERF_SAMPLE_TIME)
4925 perf_output_put(handle, data->time);
4926
4927 if (sample_type & PERF_SAMPLE_ADDR)
4928 perf_output_put(handle, data->addr);
4929
4930 if (sample_type & PERF_SAMPLE_ID)
4931 perf_output_put(handle, data->id);
4932
4933 if (sample_type & PERF_SAMPLE_STREAM_ID)
4934 perf_output_put(handle, data->stream_id);
4935
4936 if (sample_type & PERF_SAMPLE_CPU)
4937 perf_output_put(handle, data->cpu_entry);
4938
4939 if (sample_type & PERF_SAMPLE_PERIOD)
4940 perf_output_put(handle, data->period);
4941
4942 if (sample_type & PERF_SAMPLE_READ)
4943 perf_output_read(handle, event);
4944
4945 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4946 if (data->callchain) {
4947 int size = 1;
4948
4949 if (data->callchain)
4950 size += data->callchain->nr;
4951
4952 size *= sizeof(u64);
4953
Frederic Weisbecker76369132011-05-19 19:55:04 +02004954 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004955 } else {
4956 u64 nr = 0;
4957 perf_output_put(handle, nr);
4958 }
4959 }
4960
4961 if (sample_type & PERF_SAMPLE_RAW) {
4962 if (data->raw) {
4963 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004964 __output_copy(handle, data->raw->data,
4965 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004966 } else {
4967 struct {
4968 u32 size;
4969 u32 data;
4970 } raw = {
4971 .size = sizeof(u32),
4972 .data = 0,
4973 };
4974 perf_output_put(handle, raw);
4975 }
4976 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004977
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004978 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4979 if (data->br_stack) {
4980 size_t size;
4981
4982 size = data->br_stack->nr
4983 * sizeof(struct perf_branch_entry);
4984
4985 perf_output_put(handle, data->br_stack->nr);
4986 perf_output_copy(handle, data->br_stack->entries, size);
4987 } else {
4988 /*
4989 * we always store at least the value of nr
4990 */
4991 u64 nr = 0;
4992 perf_output_put(handle, nr);
4993 }
4994 }
Jiri Olsa40189942012-08-07 15:20:37 +02004995
4996 if (sample_type & PERF_SAMPLE_REGS_USER) {
4997 u64 abi = data->regs_user.abi;
4998
4999 /*
5000 * If there are no regs to dump, notice it through
5001 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5002 */
5003 perf_output_put(handle, abi);
5004
5005 if (abi) {
5006 u64 mask = event->attr.sample_regs_user;
5007 perf_output_sample_regs(handle,
5008 data->regs_user.regs,
5009 mask);
5010 }
5011 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005012
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005013 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005014 perf_output_sample_ustack(handle,
5015 data->stack_user_size,
5016 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005017 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005018
5019 if (sample_type & PERF_SAMPLE_WEIGHT)
5020 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005021
5022 if (sample_type & PERF_SAMPLE_DATA_SRC)
5023 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005024
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005025 if (sample_type & PERF_SAMPLE_TRANSACTION)
5026 perf_output_put(handle, data->txn);
5027
Stephane Eranian60e23642014-09-24 13:48:37 +02005028 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5029 u64 abi = data->regs_intr.abi;
5030 /*
5031 * If there are no regs to dump, notice it through
5032 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5033 */
5034 perf_output_put(handle, abi);
5035
5036 if (abi) {
5037 u64 mask = event->attr.sample_regs_intr;
5038
5039 perf_output_sample_regs(handle,
5040 data->regs_intr.regs,
5041 mask);
5042 }
5043 }
5044
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005045 if (!event->attr.watermark) {
5046 int wakeup_events = event->attr.wakeup_events;
5047
5048 if (wakeup_events) {
5049 struct ring_buffer *rb = handle->rb;
5050 int events = local_inc_return(&rb->events);
5051
5052 if (events >= wakeup_events) {
5053 local_sub(wakeup_events, &rb->events);
5054 local_inc(&rb->wakeup);
5055 }
5056 }
5057 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005058}
5059
5060void perf_prepare_sample(struct perf_event_header *header,
5061 struct perf_sample_data *data,
5062 struct perf_event *event,
5063 struct pt_regs *regs)
5064{
5065 u64 sample_type = event->attr.sample_type;
5066
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005067 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005068 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005069
5070 header->misc = 0;
5071 header->misc |= perf_misc_flags(regs);
5072
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005073 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005074
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005075 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005076 data->ip = perf_instruction_pointer(regs);
5077
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005078 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5079 int size = 1;
5080
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005081 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005082
5083 if (data->callchain)
5084 size += data->callchain->nr;
5085
5086 header->size += size * sizeof(u64);
5087 }
5088
5089 if (sample_type & PERF_SAMPLE_RAW) {
5090 int size = sizeof(u32);
5091
5092 if (data->raw)
5093 size += data->raw->size;
5094 else
5095 size += sizeof(u32);
5096
5097 WARN_ON_ONCE(size & (sizeof(u64)-1));
5098 header->size += size;
5099 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005100
5101 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5102 int size = sizeof(u64); /* nr */
5103 if (data->br_stack) {
5104 size += data->br_stack->nr
5105 * sizeof(struct perf_branch_entry);
5106 }
5107 header->size += size;
5108 }
Jiri Olsa40189942012-08-07 15:20:37 +02005109
Peter Zijlstra25657112014-09-24 13:48:42 +02005110 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005111 perf_sample_regs_user(&data->regs_user, regs,
5112 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005113
Jiri Olsa40189942012-08-07 15:20:37 +02005114 if (sample_type & PERF_SAMPLE_REGS_USER) {
5115 /* regs dump ABI info */
5116 int size = sizeof(u64);
5117
Jiri Olsa40189942012-08-07 15:20:37 +02005118 if (data->regs_user.regs) {
5119 u64 mask = event->attr.sample_regs_user;
5120 size += hweight64(mask) * sizeof(u64);
5121 }
5122
5123 header->size += size;
5124 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005125
5126 if (sample_type & PERF_SAMPLE_STACK_USER) {
5127 /*
5128 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5129 * processed as the last one or have additional check added
5130 * in case new sample type is added, because we could eat
5131 * up the rest of the sample size.
5132 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005133 u16 stack_size = event->attr.sample_stack_user;
5134 u16 size = sizeof(u64);
5135
Jiri Olsac5ebced2012-08-07 15:20:40 +02005136 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005137 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005138
5139 /*
5140 * If there is something to dump, add space for the dump
5141 * itself and for the field that tells the dynamic size,
5142 * which is how many have been actually dumped.
5143 */
5144 if (stack_size)
5145 size += sizeof(u64) + stack_size;
5146
5147 data->stack_user_size = stack_size;
5148 header->size += size;
5149 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005150
5151 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5152 /* regs dump ABI info */
5153 int size = sizeof(u64);
5154
5155 perf_sample_regs_intr(&data->regs_intr, regs);
5156
5157 if (data->regs_intr.regs) {
5158 u64 mask = event->attr.sample_regs_intr;
5159
5160 size += hweight64(mask) * sizeof(u64);
5161 }
5162
5163 header->size += size;
5164 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005165}
5166
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005167static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005168 struct perf_sample_data *data,
5169 struct pt_regs *regs)
5170{
5171 struct perf_output_handle handle;
5172 struct perf_event_header header;
5173
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005174 /* protect the callchain buffers */
5175 rcu_read_lock();
5176
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005177 perf_prepare_sample(&header, data, event, regs);
5178
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005179 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005180 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005181
5182 perf_output_sample(&handle, &header, data, event);
5183
5184 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005185
5186exit:
5187 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005188}
5189
5190/*
5191 * read event_id
5192 */
5193
5194struct perf_read_event {
5195 struct perf_event_header header;
5196
5197 u32 pid;
5198 u32 tid;
5199};
5200
5201static void
5202perf_event_read_event(struct perf_event *event,
5203 struct task_struct *task)
5204{
5205 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005206 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005207 struct perf_read_event read_event = {
5208 .header = {
5209 .type = PERF_RECORD_READ,
5210 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005211 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005212 },
5213 .pid = perf_event_pid(event, task),
5214 .tid = perf_event_tid(event, task),
5215 };
5216 int ret;
5217
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005218 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005219 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005220 if (ret)
5221 return;
5222
5223 perf_output_put(&handle, read_event);
5224 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005225 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005226
5227 perf_output_end(&handle);
5228}
5229
Jiri Olsa52d857a2013-05-06 18:27:18 +02005230typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5231
5232static void
5233perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005234 perf_event_aux_output_cb output,
5235 void *data)
5236{
5237 struct perf_event *event;
5238
5239 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5240 if (event->state < PERF_EVENT_STATE_INACTIVE)
5241 continue;
5242 if (!event_filter_match(event))
5243 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02005244 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005245 }
5246}
5247
5248static void
Jiri Olsa67516842013-07-09 18:56:31 +02005249perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005250 struct perf_event_context *task_ctx)
5251{
5252 struct perf_cpu_context *cpuctx;
5253 struct perf_event_context *ctx;
5254 struct pmu *pmu;
5255 int ctxn;
5256
5257 rcu_read_lock();
5258 list_for_each_entry_rcu(pmu, &pmus, entry) {
5259 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5260 if (cpuctx->unique_pmu != pmu)
5261 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02005262 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005263 if (task_ctx)
5264 goto next;
5265 ctxn = pmu->task_ctx_nr;
5266 if (ctxn < 0)
5267 goto next;
5268 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5269 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02005270 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005271next:
5272 put_cpu_ptr(pmu->pmu_cpu_context);
5273 }
5274
5275 if (task_ctx) {
5276 preempt_disable();
Jiri Olsa67516842013-07-09 18:56:31 +02005277 perf_event_aux_ctx(task_ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005278 preempt_enable();
5279 }
5280 rcu_read_unlock();
5281}
5282
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005283/*
5284 * task tracking -- fork/exit
5285 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02005286 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005287 */
5288
5289struct perf_task_event {
5290 struct task_struct *task;
5291 struct perf_event_context *task_ctx;
5292
5293 struct {
5294 struct perf_event_header header;
5295
5296 u32 pid;
5297 u32 ppid;
5298 u32 tid;
5299 u32 ptid;
5300 u64 time;
5301 } event_id;
5302};
5303
Jiri Olsa67516842013-07-09 18:56:31 +02005304static int perf_event_task_match(struct perf_event *event)
5305{
Stephane Eranian13d7a242013-08-21 12:10:24 +02005306 return event->attr.comm || event->attr.mmap ||
5307 event->attr.mmap2 || event->attr.mmap_data ||
5308 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02005309}
5310
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005311static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005312 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005313{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005314 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005315 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005316 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005317 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005318 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01005319
Jiri Olsa67516842013-07-09 18:56:31 +02005320 if (!perf_event_task_match(event))
5321 return;
5322
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005323 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005324
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005325 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005326 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02005327 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005328 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005329
5330 task_event->event_id.pid = perf_event_pid(event, task);
5331 task_event->event_id.ppid = perf_event_pid(event, current);
5332
5333 task_event->event_id.tid = perf_event_tid(event, task);
5334 task_event->event_id.ptid = perf_event_tid(event, current);
5335
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005336 perf_output_put(&handle, task_event->event_id);
5337
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005338 perf_event__output_id_sample(event, &handle, &sample);
5339
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005340 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005341out:
5342 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005343}
5344
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005345static void perf_event_task(struct task_struct *task,
5346 struct perf_event_context *task_ctx,
5347 int new)
5348{
5349 struct perf_task_event task_event;
5350
5351 if (!atomic_read(&nr_comm_events) &&
5352 !atomic_read(&nr_mmap_events) &&
5353 !atomic_read(&nr_task_events))
5354 return;
5355
5356 task_event = (struct perf_task_event){
5357 .task = task,
5358 .task_ctx = task_ctx,
5359 .event_id = {
5360 .header = {
5361 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5362 .misc = 0,
5363 .size = sizeof(task_event.event_id),
5364 },
5365 /* .pid */
5366 /* .ppid */
5367 /* .tid */
5368 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01005369 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005370 },
5371 };
5372
Jiri Olsa67516842013-07-09 18:56:31 +02005373 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005374 &task_event,
5375 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005376}
5377
5378void perf_event_fork(struct task_struct *task)
5379{
5380 perf_event_task(task, NULL, 1);
5381}
5382
5383/*
5384 * comm tracking
5385 */
5386
5387struct perf_comm_event {
5388 struct task_struct *task;
5389 char *comm;
5390 int comm_size;
5391
5392 struct {
5393 struct perf_event_header header;
5394
5395 u32 pid;
5396 u32 tid;
5397 } event_id;
5398};
5399
Jiri Olsa67516842013-07-09 18:56:31 +02005400static int perf_event_comm_match(struct perf_event *event)
5401{
5402 return event->attr.comm;
5403}
5404
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005405static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005406 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005407{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005408 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005409 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005410 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005411 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005412 int ret;
5413
Jiri Olsa67516842013-07-09 18:56:31 +02005414 if (!perf_event_comm_match(event))
5415 return;
5416
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005417 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5418 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005419 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005420
5421 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005422 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005423
5424 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5425 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5426
5427 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005428 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005429 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005430
5431 perf_event__output_id_sample(event, &handle, &sample);
5432
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005433 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005434out:
5435 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005436}
5437
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005438static void perf_event_comm_event(struct perf_comm_event *comm_event)
5439{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005440 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005441 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005442
5443 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005444 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005445 size = ALIGN(strlen(comm)+1, sizeof(u64));
5446
5447 comm_event->comm = comm;
5448 comm_event->comm_size = size;
5449
5450 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005451
Jiri Olsa67516842013-07-09 18:56:31 +02005452 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005453 comm_event,
5454 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005455}
5456
Adrian Hunter82b89772014-05-28 11:45:04 +03005457void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005458{
5459 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005460
5461 if (!atomic_read(&nr_comm_events))
5462 return;
5463
5464 comm_event = (struct perf_comm_event){
5465 .task = task,
5466 /* .comm */
5467 /* .comm_size */
5468 .event_id = {
5469 .header = {
5470 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005471 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005472 /* .size */
5473 },
5474 /* .pid */
5475 /* .tid */
5476 },
5477 };
5478
5479 perf_event_comm_event(&comm_event);
5480}
5481
5482/*
5483 * mmap tracking
5484 */
5485
5486struct perf_mmap_event {
5487 struct vm_area_struct *vma;
5488
5489 const char *file_name;
5490 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005491 int maj, min;
5492 u64 ino;
5493 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005494 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005495
5496 struct {
5497 struct perf_event_header header;
5498
5499 u32 pid;
5500 u32 tid;
5501 u64 start;
5502 u64 len;
5503 u64 pgoff;
5504 } event_id;
5505};
5506
Jiri Olsa67516842013-07-09 18:56:31 +02005507static int perf_event_mmap_match(struct perf_event *event,
5508 void *data)
5509{
5510 struct perf_mmap_event *mmap_event = data;
5511 struct vm_area_struct *vma = mmap_event->vma;
5512 int executable = vma->vm_flags & VM_EXEC;
5513
5514 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005515 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005516}
5517
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005518static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005519 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005520{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005521 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005522 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005523 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005524 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005525 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005526
Jiri Olsa67516842013-07-09 18:56:31 +02005527 if (!perf_event_mmap_match(event, data))
5528 return;
5529
Stephane Eranian13d7a242013-08-21 12:10:24 +02005530 if (event->attr.mmap2) {
5531 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5532 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5533 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5534 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005535 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005536 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
5537 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005538 }
5539
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005540 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5541 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005542 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005543 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005544 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005545
5546 mmap_event->event_id.pid = perf_event_pid(event, current);
5547 mmap_event->event_id.tid = perf_event_tid(event, current);
5548
5549 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005550
5551 if (event->attr.mmap2) {
5552 perf_output_put(&handle, mmap_event->maj);
5553 perf_output_put(&handle, mmap_event->min);
5554 perf_output_put(&handle, mmap_event->ino);
5555 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005556 perf_output_put(&handle, mmap_event->prot);
5557 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005558 }
5559
Frederic Weisbecker76369132011-05-19 19:55:04 +02005560 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005561 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005562
5563 perf_event__output_id_sample(event, &handle, &sample);
5564
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005565 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005566out:
5567 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005568}
5569
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005570static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5571{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005572 struct vm_area_struct *vma = mmap_event->vma;
5573 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005574 int maj = 0, min = 0;
5575 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005576 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005577 unsigned int size;
5578 char tmp[16];
5579 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005580 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005581
5582 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005583 struct inode *inode;
5584 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005585
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005586 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005587 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005588 name = "//enomem";
5589 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005590 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005591 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005592 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005593 * need to add enough zero bytes after the string to handle
5594 * the 64bit alignment we do later.
5595 */
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005596 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005597 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005598 name = "//toolong";
5599 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005600 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005601 inode = file_inode(vma->vm_file);
5602 dev = inode->i_sb->s_dev;
5603 ino = inode->i_ino;
5604 gen = inode->i_generation;
5605 maj = MAJOR(dev);
5606 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005607
5608 if (vma->vm_flags & VM_READ)
5609 prot |= PROT_READ;
5610 if (vma->vm_flags & VM_WRITE)
5611 prot |= PROT_WRITE;
5612 if (vma->vm_flags & VM_EXEC)
5613 prot |= PROT_EXEC;
5614
5615 if (vma->vm_flags & VM_MAYSHARE)
5616 flags = MAP_SHARED;
5617 else
5618 flags = MAP_PRIVATE;
5619
5620 if (vma->vm_flags & VM_DENYWRITE)
5621 flags |= MAP_DENYWRITE;
5622 if (vma->vm_flags & VM_MAYEXEC)
5623 flags |= MAP_EXECUTABLE;
5624 if (vma->vm_flags & VM_LOCKED)
5625 flags |= MAP_LOCKED;
5626 if (vma->vm_flags & VM_HUGETLB)
5627 flags |= MAP_HUGETLB;
5628
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005629 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005630 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02005631 if (vma->vm_ops && vma->vm_ops->name) {
5632 name = (char *) vma->vm_ops->name(vma);
5633 if (name)
5634 goto cpy_name;
5635 }
5636
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005637 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005638 if (name)
5639 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005640
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005641 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005642 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005643 name = "[heap]";
5644 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005645 }
5646 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005647 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005648 name = "[stack]";
5649 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005650 }
5651
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005652 name = "//anon";
5653 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005654 }
5655
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005656cpy_name:
5657 strlcpy(tmp, name, sizeof(tmp));
5658 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005659got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005660 /*
5661 * Since our buffer works in 8 byte units we need to align our string
5662 * size to a multiple of 8. However, we must guarantee the tail end is
5663 * zero'd out to avoid leaking random bits to userspace.
5664 */
5665 size = strlen(name)+1;
5666 while (!IS_ALIGNED(size, sizeof(u64)))
5667 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005668
5669 mmap_event->file_name = name;
5670 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005671 mmap_event->maj = maj;
5672 mmap_event->min = min;
5673 mmap_event->ino = ino;
5674 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005675 mmap_event->prot = prot;
5676 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005677
Stephane Eranian2fe85422013-01-24 16:10:39 +01005678 if (!(vma->vm_flags & VM_EXEC))
5679 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5680
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005681 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5682
Jiri Olsa67516842013-07-09 18:56:31 +02005683 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005684 mmap_event,
5685 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005686
5687 kfree(buf);
5688}
5689
Eric B Munson3af9e852010-05-18 15:30:49 +01005690void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005691{
5692 struct perf_mmap_event mmap_event;
5693
5694 if (!atomic_read(&nr_mmap_events))
5695 return;
5696
5697 mmap_event = (struct perf_mmap_event){
5698 .vma = vma,
5699 /* .file_name */
5700 /* .file_size */
5701 .event_id = {
5702 .header = {
5703 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005704 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005705 /* .size */
5706 },
5707 /* .pid */
5708 /* .tid */
5709 .start = vma->vm_start,
5710 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01005711 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005712 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02005713 /* .maj (attr_mmap2 only) */
5714 /* .min (attr_mmap2 only) */
5715 /* .ino (attr_mmap2 only) */
5716 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005717 /* .prot (attr_mmap2 only) */
5718 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005719 };
5720
5721 perf_event_mmap_event(&mmap_event);
5722}
5723
5724/*
5725 * IRQ throttle logging
5726 */
5727
5728static void perf_log_throttle(struct perf_event *event, int enable)
5729{
5730 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005731 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005732 int ret;
5733
5734 struct {
5735 struct perf_event_header header;
5736 u64 time;
5737 u64 id;
5738 u64 stream_id;
5739 } throttle_event = {
5740 .header = {
5741 .type = PERF_RECORD_THROTTLE,
5742 .misc = 0,
5743 .size = sizeof(throttle_event),
5744 },
5745 .time = perf_clock(),
5746 .id = primary_event_id(event),
5747 .stream_id = event->id,
5748 };
5749
5750 if (enable)
5751 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5752
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005753 perf_event_header__init_id(&throttle_event.header, &sample, event);
5754
5755 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005756 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005757 if (ret)
5758 return;
5759
5760 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005761 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005762 perf_output_end(&handle);
5763}
5764
5765/*
5766 * Generic event overflow handling, sampling.
5767 */
5768
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005769static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005770 int throttle, struct perf_sample_data *data,
5771 struct pt_regs *regs)
5772{
5773 int events = atomic_read(&event->event_limit);
5774 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005775 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005776 int ret = 0;
5777
Peter Zijlstra96398822010-11-24 18:55:29 +01005778 /*
5779 * Non-sampling counters might still use the PMI to fold short
5780 * hardware counters, ignore those.
5781 */
5782 if (unlikely(!is_sampling_event(event)))
5783 return 0;
5784
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005785 seq = __this_cpu_read(perf_throttled_seq);
5786 if (seq != hwc->interrupts_seq) {
5787 hwc->interrupts_seq = seq;
5788 hwc->interrupts = 1;
5789 } else {
5790 hwc->interrupts++;
5791 if (unlikely(throttle
5792 && hwc->interrupts >= max_samples_per_tick)) {
5793 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01005794 hwc->interrupts = MAX_INTERRUPTS;
5795 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02005796 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005797 ret = 1;
5798 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005799 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005800
5801 if (event->attr.freq) {
5802 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01005803 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005804
Peter Zijlstraabd50712010-01-26 18:50:16 +01005805 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005806
Peter Zijlstraabd50712010-01-26 18:50:16 +01005807 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01005808 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005809 }
5810
5811 /*
5812 * XXX event_limit might not quite work as expected on inherited
5813 * events
5814 */
5815
5816 event->pending_kill = POLL_IN;
5817 if (events && atomic_dec_and_test(&event->event_limit)) {
5818 ret = 1;
5819 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005820 event->pending_disable = 1;
5821 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005822 }
5823
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005824 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005825 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005826 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005827 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005828
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005829 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005830 event->pending_wakeup = 1;
5831 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005832 }
5833
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005834 return ret;
5835}
5836
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005837int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005838 struct perf_sample_data *data,
5839 struct pt_regs *regs)
5840{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005841 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005842}
5843
5844/*
5845 * Generic software event infrastructure
5846 */
5847
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005848struct swevent_htable {
5849 struct swevent_hlist *swevent_hlist;
5850 struct mutex hlist_mutex;
5851 int hlist_refcount;
5852
5853 /* Recursion avoidance in each contexts */
5854 int recursion[PERF_NR_CONTEXTS];
Jiri Olsa39af6b12014-04-07 11:04:08 +02005855
5856 /* Keeps track of cpu being initialized/exited */
5857 bool online;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005858};
5859
5860static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5861
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005862/*
5863 * We directly increment event->count and keep a second value in
5864 * event->hw.period_left to count intervals. This period event
5865 * is kept in the range [-sample_period, 0] so that we can use the
5866 * sign as trigger.
5867 */
5868
Jiri Olsaab573842013-05-01 17:25:44 +02005869u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005870{
5871 struct hw_perf_event *hwc = &event->hw;
5872 u64 period = hwc->last_period;
5873 u64 nr, offset;
5874 s64 old, val;
5875
5876 hwc->last_period = hwc->sample_period;
5877
5878again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005879 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005880 if (val < 0)
5881 return 0;
5882
5883 nr = div64_u64(period + val, period);
5884 offset = nr * period;
5885 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005886 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005887 goto again;
5888
5889 return nr;
5890}
5891
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005892static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005893 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005894 struct pt_regs *regs)
5895{
5896 struct hw_perf_event *hwc = &event->hw;
5897 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005898
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005899 if (!overflow)
5900 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005901
5902 if (hwc->interrupts == MAX_INTERRUPTS)
5903 return;
5904
5905 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005906 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005907 data, regs)) {
5908 /*
5909 * We inhibit the overflow from happening when
5910 * hwc->interrupts == MAX_INTERRUPTS.
5911 */
5912 break;
5913 }
5914 throttle = 1;
5915 }
5916}
5917
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005918static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005919 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005920 struct pt_regs *regs)
5921{
5922 struct hw_perf_event *hwc = &event->hw;
5923
Peter Zijlstrae7850592010-05-21 14:43:08 +02005924 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005925
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005926 if (!regs)
5927 return;
5928
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005929 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005930 return;
5931
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005932 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5933 data->period = nr;
5934 return perf_swevent_overflow(event, 1, data, regs);
5935 } else
5936 data->period = event->hw.last_period;
5937
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005938 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005939 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005940
Peter Zijlstrae7850592010-05-21 14:43:08 +02005941 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005942 return;
5943
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005944 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005945}
5946
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005947static int perf_exclude_event(struct perf_event *event,
5948 struct pt_regs *regs)
5949{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005950 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005951 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005952
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005953 if (regs) {
5954 if (event->attr.exclude_user && user_mode(regs))
5955 return 1;
5956
5957 if (event->attr.exclude_kernel && !user_mode(regs))
5958 return 1;
5959 }
5960
5961 return 0;
5962}
5963
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005964static int perf_swevent_match(struct perf_event *event,
5965 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005966 u32 event_id,
5967 struct perf_sample_data *data,
5968 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005969{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005970 if (event->attr.type != type)
5971 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005972
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005973 if (event->attr.config != event_id)
5974 return 0;
5975
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005976 if (perf_exclude_event(event, regs))
5977 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005978
5979 return 1;
5980}
5981
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005982static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005983{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005984 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005985
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005986 return hash_64(val, SWEVENT_HLIST_BITS);
5987}
5988
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005989static inline struct hlist_head *
5990__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005991{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005992 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005993
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005994 return &hlist->heads[hash];
5995}
5996
5997/* For the read side: events when they trigger */
5998static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005999find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006000{
6001 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006002
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006003 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006004 if (!hlist)
6005 return NULL;
6006
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006007 return __find_swevent_head(hlist, type, event_id);
6008}
6009
6010/* For the event head insertion and removal in the hlist */
6011static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006012find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006013{
6014 struct swevent_hlist *hlist;
6015 u32 event_id = event->attr.config;
6016 u64 type = event->attr.type;
6017
6018 /*
6019 * Event scheduling is always serialized against hlist allocation
6020 * and release. Which makes the protected version suitable here.
6021 * The context lock guarantees that.
6022 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006023 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006024 lockdep_is_held(&event->ctx->lock));
6025 if (!hlist)
6026 return NULL;
6027
6028 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006029}
6030
6031static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006032 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006033 struct perf_sample_data *data,
6034 struct pt_regs *regs)
6035{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006036 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006037 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006038 struct hlist_head *head;
6039
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006040 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006041 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006042 if (!head)
6043 goto end;
6044
Sasha Levinb67bfe02013-02-27 17:06:00 -08006045 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08006046 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006047 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006048 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006049end:
6050 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006051}
6052
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006053DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
6054
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006055int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006056{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006057 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006058
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006059 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006060}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01006061EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006062
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01006063inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006064{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006065 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006066
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006067 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006068}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006069
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006070void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006071{
Ingo Molnara4234bf2009-11-23 10:57:59 +01006072 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006073
6074 if (WARN_ON_ONCE(!regs))
6075 return;
6076
6077 perf_sample_data_init(&data, addr, 0);
6078 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
6079}
6080
6081void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
6082{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006083 int rctx;
6084
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006085 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006086 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006087 if (unlikely(rctx < 0))
6088 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006089
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006090 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006091
6092 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006093fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006094 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006095}
6096
6097static void perf_swevent_read(struct perf_event *event)
6098{
6099}
6100
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006101static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006102{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006103 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006104 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006105 struct hlist_head *head;
6106
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006107 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006108 hwc->last_period = hwc->sample_period;
6109 perf_swevent_set_period(event);
6110 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006111
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006112 hwc->state = !(flags & PERF_EF_START);
6113
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006114 head = find_swevent_head(swhash, event);
Jiri Olsa39af6b12014-04-07 11:04:08 +02006115 if (!head) {
6116 /*
6117 * We can race with cpu hotplug code. Do not
6118 * WARN if the cpu just got unplugged.
6119 */
6120 WARN_ON_ONCE(swhash->online);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006121 return -EINVAL;
Jiri Olsa39af6b12014-04-07 11:04:08 +02006122 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006123
6124 hlist_add_head_rcu(&event->hlist_entry, head);
6125
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006126 return 0;
6127}
6128
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006129static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006130{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006131 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006132}
6133
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006134static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006135{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006136 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006137}
6138
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006139static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006140{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006141 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006142}
6143
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006144/* Deref the hlist from the update side */
6145static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006146swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006147{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006148 return rcu_dereference_protected(swhash->swevent_hlist,
6149 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006150}
6151
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006152static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006153{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006154 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006155
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006156 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006157 return;
6158
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03006159 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08006160 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006161}
6162
6163static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
6164{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006165 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006166
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006167 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006168
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006169 if (!--swhash->hlist_refcount)
6170 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006171
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006172 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006173}
6174
6175static void swevent_hlist_put(struct perf_event *event)
6176{
6177 int cpu;
6178
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006179 for_each_possible_cpu(cpu)
6180 swevent_hlist_put_cpu(event, cpu);
6181}
6182
6183static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
6184{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006185 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006186 int err = 0;
6187
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006188 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006189
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006190 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006191 struct swevent_hlist *hlist;
6192
6193 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
6194 if (!hlist) {
6195 err = -ENOMEM;
6196 goto exit;
6197 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006198 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006199 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006200 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006201exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006202 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006203
6204 return err;
6205}
6206
6207static int swevent_hlist_get(struct perf_event *event)
6208{
6209 int err;
6210 int cpu, failed_cpu;
6211
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006212 get_online_cpus();
6213 for_each_possible_cpu(cpu) {
6214 err = swevent_hlist_get_cpu(event, cpu);
6215 if (err) {
6216 failed_cpu = cpu;
6217 goto fail;
6218 }
6219 }
6220 put_online_cpus();
6221
6222 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006223fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006224 for_each_possible_cpu(cpu) {
6225 if (cpu == failed_cpu)
6226 break;
6227 swevent_hlist_put_cpu(event, cpu);
6228 }
6229
6230 put_online_cpus();
6231 return err;
6232}
6233
Ingo Molnarc5905af2012-02-24 08:31:31 +01006234struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006235
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006236static void sw_perf_event_destroy(struct perf_event *event)
6237{
6238 u64 event_id = event->attr.config;
6239
6240 WARN_ON(event->parent);
6241
Ingo Molnarc5905af2012-02-24 08:31:31 +01006242 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006243 swevent_hlist_put(event);
6244}
6245
6246static int perf_swevent_init(struct perf_event *event)
6247{
Tommi Rantala8176cce2013-04-13 22:49:14 +03006248 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006249
6250 if (event->attr.type != PERF_TYPE_SOFTWARE)
6251 return -ENOENT;
6252
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006253 /*
6254 * no branch sampling for software events
6255 */
6256 if (has_branch_stack(event))
6257 return -EOPNOTSUPP;
6258
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006259 switch (event_id) {
6260 case PERF_COUNT_SW_CPU_CLOCK:
6261 case PERF_COUNT_SW_TASK_CLOCK:
6262 return -ENOENT;
6263
6264 default:
6265 break;
6266 }
6267
Dan Carpenterce677832010-10-24 21:50:42 +02006268 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006269 return -ENOENT;
6270
6271 if (!event->parent) {
6272 int err;
6273
6274 err = swevent_hlist_get(event);
6275 if (err)
6276 return err;
6277
Ingo Molnarc5905af2012-02-24 08:31:31 +01006278 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006279 event->destroy = sw_perf_event_destroy;
6280 }
6281
6282 return 0;
6283}
6284
6285static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006286 .task_ctx_nr = perf_sw_context,
6287
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006288 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006289 .add = perf_swevent_add,
6290 .del = perf_swevent_del,
6291 .start = perf_swevent_start,
6292 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006293 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006294};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006295
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006296#ifdef CONFIG_EVENT_TRACING
6297
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006298static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006299 struct perf_sample_data *data)
6300{
6301 void *record = data->raw->data;
6302
6303 if (likely(!event->filter) || filter_match_preds(event->filter, record))
6304 return 1;
6305 return 0;
6306}
6307
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006308static int perf_tp_event_match(struct perf_event *event,
6309 struct perf_sample_data *data,
6310 struct pt_regs *regs)
6311{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01006312 if (event->hw.state & PERF_HES_STOPPED)
6313 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02006314 /*
6315 * All tracepoints are from kernel-space.
6316 */
6317 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006318 return 0;
6319
6320 if (!perf_tp_filter_match(event, data))
6321 return 0;
6322
6323 return 1;
6324}
6325
6326void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006327 struct pt_regs *regs, struct hlist_head *head, int rctx,
6328 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006329{
6330 struct perf_sample_data data;
6331 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006332
6333 struct perf_raw_record raw = {
6334 .size = entry_size,
6335 .data = record,
6336 };
6337
Robert Richterfd0d0002012-04-02 20:19:08 +02006338 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006339 data.raw = &raw;
6340
Sasha Levinb67bfe02013-02-27 17:06:00 -08006341 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006342 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006343 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006344 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006345
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006346 /*
6347 * If we got specified a target task, also iterate its context and
6348 * deliver this event there too.
6349 */
6350 if (task && task != current) {
6351 struct perf_event_context *ctx;
6352 struct trace_entry *entry = record;
6353
6354 rcu_read_lock();
6355 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
6356 if (!ctx)
6357 goto unlock;
6358
6359 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
6360 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6361 continue;
6362 if (event->attr.config != entry->type)
6363 continue;
6364 if (perf_tp_event_match(event, &data, regs))
6365 perf_swevent_event(event, count, &data, regs);
6366 }
6367unlock:
6368 rcu_read_unlock();
6369 }
6370
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006371 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006372}
6373EXPORT_SYMBOL_GPL(perf_tp_event);
6374
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006375static void tp_perf_event_destroy(struct perf_event *event)
6376{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006377 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006378}
6379
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006380static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006381{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006382 int err;
6383
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006384 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6385 return -ENOENT;
6386
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006387 /*
6388 * no branch sampling for tracepoint events
6389 */
6390 if (has_branch_stack(event))
6391 return -EOPNOTSUPP;
6392
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006393 err = perf_trace_init(event);
6394 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006395 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006396
6397 event->destroy = tp_perf_event_destroy;
6398
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006399 return 0;
6400}
6401
6402static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006403 .task_ctx_nr = perf_sw_context,
6404
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006405 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006406 .add = perf_trace_add,
6407 .del = perf_trace_del,
6408 .start = perf_swevent_start,
6409 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006410 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006411};
6412
6413static inline void perf_tp_register(void)
6414{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006415 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006416}
Li Zefan6fb29152009-10-15 11:21:42 +08006417
6418static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6419{
6420 char *filter_str;
6421 int ret;
6422
6423 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6424 return -EINVAL;
6425
6426 filter_str = strndup_user(arg, PAGE_SIZE);
6427 if (IS_ERR(filter_str))
6428 return PTR_ERR(filter_str);
6429
6430 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
6431
6432 kfree(filter_str);
6433 return ret;
6434}
6435
6436static void perf_event_free_filter(struct perf_event *event)
6437{
6438 ftrace_profile_free_filter(event);
6439}
6440
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006441#else
Li Zefan6fb29152009-10-15 11:21:42 +08006442
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006443static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006444{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006445}
Li Zefan6fb29152009-10-15 11:21:42 +08006446
6447static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6448{
6449 return -ENOENT;
6450}
6451
6452static void perf_event_free_filter(struct perf_event *event)
6453{
6454}
6455
Li Zefan07b139c2009-12-21 14:27:35 +08006456#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006457
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006458#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006459void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006460{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006461 struct perf_sample_data sample;
6462 struct pt_regs *regs = data;
6463
Robert Richterfd0d0002012-04-02 20:19:08 +02006464 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006465
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006466 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006467 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006468}
6469#endif
6470
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006471/*
6472 * hrtimer based swevent callback
6473 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006474
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006475static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006476{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006477 enum hrtimer_restart ret = HRTIMER_RESTART;
6478 struct perf_sample_data data;
6479 struct pt_regs *regs;
6480 struct perf_event *event;
6481 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006482
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006483 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006484
6485 if (event->state != PERF_EVENT_STATE_ACTIVE)
6486 return HRTIMER_NORESTART;
6487
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006488 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006489
Robert Richterfd0d0002012-04-02 20:19:08 +02006490 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006491 regs = get_irq_regs();
6492
6493 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08006494 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02006495 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006496 ret = HRTIMER_NORESTART;
6497 }
6498
6499 period = max_t(u64, 10000, event->hw.sample_period);
6500 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
6501
6502 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006503}
6504
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006505static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006506{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006507 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006508 s64 period;
6509
6510 if (!is_sampling_event(event))
6511 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006512
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006513 period = local64_read(&hwc->period_left);
6514 if (period) {
6515 if (period < 0)
6516 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006517
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006518 local64_set(&hwc->period_left, 0);
6519 } else {
6520 period = max_t(u64, 10000, hwc->sample_period);
6521 }
6522 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006523 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006524 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006525}
6526
6527static void perf_swevent_cancel_hrtimer(struct perf_event *event)
6528{
6529 struct hw_perf_event *hwc = &event->hw;
6530
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006531 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006532 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006533 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006534
6535 hrtimer_cancel(&hwc->hrtimer);
6536 }
6537}
6538
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006539static void perf_swevent_init_hrtimer(struct perf_event *event)
6540{
6541 struct hw_perf_event *hwc = &event->hw;
6542
6543 if (!is_sampling_event(event))
6544 return;
6545
6546 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6547 hwc->hrtimer.function = perf_swevent_hrtimer;
6548
6549 /*
6550 * Since hrtimers have a fixed rate, we can do a static freq->period
6551 * mapping and avoid the whole period adjust feedback stuff.
6552 */
6553 if (event->attr.freq) {
6554 long freq = event->attr.sample_freq;
6555
6556 event->attr.sample_period = NSEC_PER_SEC / freq;
6557 hwc->sample_period = event->attr.sample_period;
6558 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09006559 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006560 event->attr.freq = 0;
6561 }
6562}
6563
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006564/*
6565 * Software event: cpu wall time clock
6566 */
6567
6568static void cpu_clock_event_update(struct perf_event *event)
6569{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006570 s64 prev;
6571 u64 now;
6572
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006573 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006574 prev = local64_xchg(&event->hw.prev_count, now);
6575 local64_add(now - prev, &event->count);
6576}
6577
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006578static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006579{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006580 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006581 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006582}
6583
6584static void cpu_clock_event_stop(struct perf_event *event, int flags)
6585{
6586 perf_swevent_cancel_hrtimer(event);
6587 cpu_clock_event_update(event);
6588}
6589
6590static int cpu_clock_event_add(struct perf_event *event, int flags)
6591{
6592 if (flags & PERF_EF_START)
6593 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006594
6595 return 0;
6596}
6597
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006598static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006599{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006600 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006601}
6602
6603static void cpu_clock_event_read(struct perf_event *event)
6604{
6605 cpu_clock_event_update(event);
6606}
6607
6608static int cpu_clock_event_init(struct perf_event *event)
6609{
6610 if (event->attr.type != PERF_TYPE_SOFTWARE)
6611 return -ENOENT;
6612
6613 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
6614 return -ENOENT;
6615
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006616 /*
6617 * no branch sampling for software events
6618 */
6619 if (has_branch_stack(event))
6620 return -EOPNOTSUPP;
6621
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006622 perf_swevent_init_hrtimer(event);
6623
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006624 return 0;
6625}
6626
6627static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006628 .task_ctx_nr = perf_sw_context,
6629
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006630 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006631 .add = cpu_clock_event_add,
6632 .del = cpu_clock_event_del,
6633 .start = cpu_clock_event_start,
6634 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006635 .read = cpu_clock_event_read,
6636};
6637
6638/*
6639 * Software event: task time clock
6640 */
6641
6642static void task_clock_event_update(struct perf_event *event, u64 now)
6643{
6644 u64 prev;
6645 s64 delta;
6646
6647 prev = local64_xchg(&event->hw.prev_count, now);
6648 delta = now - prev;
6649 local64_add(delta, &event->count);
6650}
6651
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006652static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006653{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006654 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006655 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006656}
6657
6658static void task_clock_event_stop(struct perf_event *event, int flags)
6659{
6660 perf_swevent_cancel_hrtimer(event);
6661 task_clock_event_update(event, event->ctx->time);
6662}
6663
6664static int task_clock_event_add(struct perf_event *event, int flags)
6665{
6666 if (flags & PERF_EF_START)
6667 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006668
6669 return 0;
6670}
6671
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006672static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006673{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006674 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006675}
6676
6677static void task_clock_event_read(struct perf_event *event)
6678{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01006679 u64 now = perf_clock();
6680 u64 delta = now - event->ctx->timestamp;
6681 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006682
6683 task_clock_event_update(event, time);
6684}
6685
6686static int task_clock_event_init(struct perf_event *event)
6687{
6688 if (event->attr.type != PERF_TYPE_SOFTWARE)
6689 return -ENOENT;
6690
6691 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6692 return -ENOENT;
6693
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006694 /*
6695 * no branch sampling for software events
6696 */
6697 if (has_branch_stack(event))
6698 return -EOPNOTSUPP;
6699
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006700 perf_swevent_init_hrtimer(event);
6701
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006702 return 0;
6703}
6704
6705static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006706 .task_ctx_nr = perf_sw_context,
6707
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006708 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006709 .add = task_clock_event_add,
6710 .del = task_clock_event_del,
6711 .start = task_clock_event_start,
6712 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006713 .read = task_clock_event_read,
6714};
6715
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006716static void perf_pmu_nop_void(struct pmu *pmu)
6717{
6718}
6719
6720static int perf_pmu_nop_int(struct pmu *pmu)
6721{
6722 return 0;
6723}
6724
6725static void perf_pmu_start_txn(struct pmu *pmu)
6726{
6727 perf_pmu_disable(pmu);
6728}
6729
6730static int perf_pmu_commit_txn(struct pmu *pmu)
6731{
6732 perf_pmu_enable(pmu);
6733 return 0;
6734}
6735
6736static void perf_pmu_cancel_txn(struct pmu *pmu)
6737{
6738 perf_pmu_enable(pmu);
6739}
6740
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006741static int perf_event_idx_default(struct perf_event *event)
6742{
Peter Zijlstrac719f562014-10-21 11:10:21 +02006743 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006744}
6745
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006746/*
6747 * Ensures all contexts with the same task_ctx_nr have the same
6748 * pmu_cpu_context too.
6749 */
Mark Rutland9e317042014-02-10 17:44:18 +00006750static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006751{
6752 struct pmu *pmu;
6753
6754 if (ctxn < 0)
6755 return NULL;
6756
6757 list_for_each_entry(pmu, &pmus, entry) {
6758 if (pmu->task_ctx_nr == ctxn)
6759 return pmu->pmu_cpu_context;
6760 }
6761
6762 return NULL;
6763}
6764
Peter Zijlstra51676952010-12-07 14:18:20 +01006765static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006766{
Peter Zijlstra51676952010-12-07 14:18:20 +01006767 int cpu;
6768
6769 for_each_possible_cpu(cpu) {
6770 struct perf_cpu_context *cpuctx;
6771
6772 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6773
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006774 if (cpuctx->unique_pmu == old_pmu)
6775 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01006776 }
6777}
6778
6779static void free_pmu_context(struct pmu *pmu)
6780{
6781 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006782
6783 mutex_lock(&pmus_lock);
6784 /*
6785 * Like a real lame refcount.
6786 */
Peter Zijlstra51676952010-12-07 14:18:20 +01006787 list_for_each_entry(i, &pmus, entry) {
6788 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6789 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006790 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01006791 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006792 }
6793
Peter Zijlstra51676952010-12-07 14:18:20 +01006794 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006795out:
6796 mutex_unlock(&pmus_lock);
6797}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006798static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006799
Peter Zijlstraabe43402010-11-17 23:17:37 +01006800static ssize_t
6801type_show(struct device *dev, struct device_attribute *attr, char *page)
6802{
6803 struct pmu *pmu = dev_get_drvdata(dev);
6804
6805 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6806}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006807static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006808
Stephane Eranian62b85632013-04-03 14:21:34 +02006809static ssize_t
6810perf_event_mux_interval_ms_show(struct device *dev,
6811 struct device_attribute *attr,
6812 char *page)
6813{
6814 struct pmu *pmu = dev_get_drvdata(dev);
6815
6816 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6817}
6818
6819static ssize_t
6820perf_event_mux_interval_ms_store(struct device *dev,
6821 struct device_attribute *attr,
6822 const char *buf, size_t count)
6823{
6824 struct pmu *pmu = dev_get_drvdata(dev);
6825 int timer, cpu, ret;
6826
6827 ret = kstrtoint(buf, 0, &timer);
6828 if (ret)
6829 return ret;
6830
6831 if (timer < 1)
6832 return -EINVAL;
6833
6834 /* same value, noting to do */
6835 if (timer == pmu->hrtimer_interval_ms)
6836 return count;
6837
6838 pmu->hrtimer_interval_ms = timer;
6839
6840 /* update all cpuctx for this PMU */
6841 for_each_possible_cpu(cpu) {
6842 struct perf_cpu_context *cpuctx;
6843 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6844 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6845
6846 if (hrtimer_active(&cpuctx->hrtimer))
6847 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6848 }
6849
6850 return count;
6851}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006852static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02006853
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006854static struct attribute *pmu_dev_attrs[] = {
6855 &dev_attr_type.attr,
6856 &dev_attr_perf_event_mux_interval_ms.attr,
6857 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006858};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006859ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006860
6861static int pmu_bus_running;
6862static struct bus_type pmu_bus = {
6863 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006864 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006865};
6866
6867static void pmu_dev_release(struct device *dev)
6868{
6869 kfree(dev);
6870}
6871
6872static int pmu_dev_alloc(struct pmu *pmu)
6873{
6874 int ret = -ENOMEM;
6875
6876 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6877 if (!pmu->dev)
6878 goto out;
6879
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01006880 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01006881 device_initialize(pmu->dev);
6882 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6883 if (ret)
6884 goto free_dev;
6885
6886 dev_set_drvdata(pmu->dev, pmu);
6887 pmu->dev->bus = &pmu_bus;
6888 pmu->dev->release = pmu_dev_release;
6889 ret = device_add(pmu->dev);
6890 if (ret)
6891 goto free_dev;
6892
6893out:
6894 return ret;
6895
6896free_dev:
6897 put_device(pmu->dev);
6898 goto out;
6899}
6900
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006901static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006902static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006903
Mischa Jonker03d8e802013-06-04 11:45:48 +02006904int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006905{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006906 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006907
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006908 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006909 ret = -ENOMEM;
6910 pmu->pmu_disable_count = alloc_percpu(int);
6911 if (!pmu->pmu_disable_count)
6912 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006913
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006914 pmu->type = -1;
6915 if (!name)
6916 goto skip_type;
6917 pmu->name = name;
6918
6919 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08006920 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6921 if (type < 0) {
6922 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006923 goto free_pdc;
6924 }
6925 }
6926 pmu->type = type;
6927
Peter Zijlstraabe43402010-11-17 23:17:37 +01006928 if (pmu_bus_running) {
6929 ret = pmu_dev_alloc(pmu);
6930 if (ret)
6931 goto free_idr;
6932 }
6933
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006934skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006935 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6936 if (pmu->pmu_cpu_context)
6937 goto got_cpu_context;
6938
Wei Yongjunc4814202013-04-12 11:05:54 +08006939 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006940 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6941 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006942 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006943
6944 for_each_possible_cpu(cpu) {
6945 struct perf_cpu_context *cpuctx;
6946
6947 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006948 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006949 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006950 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006951 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02006952
6953 __perf_cpu_hrtimer_init(cpuctx, cpu);
6954
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006955 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006956 }
6957
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006958got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006959 if (!pmu->start_txn) {
6960 if (pmu->pmu_enable) {
6961 /*
6962 * If we have pmu_enable/pmu_disable calls, install
6963 * transaction stubs that use that to try and batch
6964 * hardware accesses.
6965 */
6966 pmu->start_txn = perf_pmu_start_txn;
6967 pmu->commit_txn = perf_pmu_commit_txn;
6968 pmu->cancel_txn = perf_pmu_cancel_txn;
6969 } else {
6970 pmu->start_txn = perf_pmu_nop_void;
6971 pmu->commit_txn = perf_pmu_nop_int;
6972 pmu->cancel_txn = perf_pmu_nop_void;
6973 }
6974 }
6975
6976 if (!pmu->pmu_enable) {
6977 pmu->pmu_enable = perf_pmu_nop_void;
6978 pmu->pmu_disable = perf_pmu_nop_void;
6979 }
6980
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006981 if (!pmu->event_idx)
6982 pmu->event_idx = perf_event_idx_default;
6983
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006984 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006985 ret = 0;
6986unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006987 mutex_unlock(&pmus_lock);
6988
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006989 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006990
Peter Zijlstraabe43402010-11-17 23:17:37 +01006991free_dev:
6992 device_del(pmu->dev);
6993 put_device(pmu->dev);
6994
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006995free_idr:
6996 if (pmu->type >= PERF_TYPE_MAX)
6997 idr_remove(&pmu_idr, pmu->type);
6998
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006999free_pdc:
7000 free_percpu(pmu->pmu_disable_count);
7001 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007002}
Yan, Zhengc464c762014-03-18 16:56:41 +08007003EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007004
7005void perf_pmu_unregister(struct pmu *pmu)
7006{
7007 mutex_lock(&pmus_lock);
7008 list_del_rcu(&pmu->entry);
7009 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007010
7011 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02007012 * We dereference the pmu list under both SRCU and regular RCU, so
7013 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007014 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007015 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02007016 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007017
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007018 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007019 if (pmu->type >= PERF_TYPE_MAX)
7020 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007021 device_del(pmu->dev);
7022 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01007023 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007024}
Yan, Zhengc464c762014-03-18 16:56:41 +08007025EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007026
Mark Rutlandcc34b982015-01-07 14:56:51 +00007027static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
7028{
7029 int ret;
7030
7031 if (!try_module_get(pmu->module))
7032 return -ENODEV;
7033 event->pmu = pmu;
7034 ret = pmu->event_init(event);
7035 if (ret)
7036 module_put(pmu->module);
7037
7038 return ret;
7039}
7040
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007041struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007042{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007043 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007044 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08007045 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007046
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007047 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007048
7049 rcu_read_lock();
7050 pmu = idr_find(&pmu_idr, event->attr.type);
7051 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08007052 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007053 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08007054 if (ret)
7055 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007056 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08007057 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007058
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007059 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007060 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007061 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007062 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007063
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007064 if (ret != -ENOENT) {
7065 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007066 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007067 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007068 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007069 pmu = ERR_PTR(-ENOENT);
7070unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007071 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007072
7073 return pmu;
7074}
7075
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007076static void account_event_cpu(struct perf_event *event, int cpu)
7077{
7078 if (event->parent)
7079 return;
7080
7081 if (has_branch_stack(event)) {
7082 if (!(event->attach_state & PERF_ATTACH_TASK))
7083 atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
7084 }
7085 if (is_cgroup_event(event))
7086 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
7087}
7088
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007089static void account_event(struct perf_event *event)
7090{
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007091 if (event->parent)
7092 return;
7093
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007094 if (event->attach_state & PERF_ATTACH_TASK)
7095 static_key_slow_inc(&perf_sched_events.key);
7096 if (event->attr.mmap || event->attr.mmap_data)
7097 atomic_inc(&nr_mmap_events);
7098 if (event->attr.comm)
7099 atomic_inc(&nr_comm_events);
7100 if (event->attr.task)
7101 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02007102 if (event->attr.freq) {
7103 if (atomic_inc_return(&nr_freq_events) == 1)
7104 tick_nohz_full_kick_all();
7105 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007106 if (has_branch_stack(event))
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007107 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007108 if (is_cgroup_event(event))
7109 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007110
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007111 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007112}
7113
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007114/*
7115 * Allocate and initialize a event structure
7116 */
7117static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007118perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007119 struct task_struct *task,
7120 struct perf_event *group_leader,
7121 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007122 perf_overflow_handler_t overflow_handler,
7123 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007124{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007125 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007126 struct perf_event *event;
7127 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007128 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007129
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007130 if ((unsigned)cpu >= nr_cpu_ids) {
7131 if (!task || cpu != -1)
7132 return ERR_PTR(-EINVAL);
7133 }
7134
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007135 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007136 if (!event)
7137 return ERR_PTR(-ENOMEM);
7138
7139 /*
7140 * Single events are their own group leaders, with an
7141 * empty sibling list:
7142 */
7143 if (!group_leader)
7144 group_leader = event;
7145
7146 mutex_init(&event->child_mutex);
7147 INIT_LIST_HEAD(&event->child_list);
7148
7149 INIT_LIST_HEAD(&event->group_entry);
7150 INIT_LIST_HEAD(&event->event_entry);
7151 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007152 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01007153 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01007154 INIT_HLIST_NODE(&event->hlist_entry);
7155
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007156
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007157 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08007158 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007159
7160 mutex_init(&event->mmap_mutex);
7161
Al Viroa6fa9412012-08-20 14:59:25 +01007162 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007163 event->cpu = cpu;
7164 event->attr = *attr;
7165 event->group_leader = group_leader;
7166 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007167 event->oncpu = -1;
7168
7169 event->parent = parent_event;
7170
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08007171 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007172 event->id = atomic64_inc_return(&perf_event_id);
7173
7174 event->state = PERF_EVENT_STATE_INACTIVE;
7175
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007176 if (task) {
7177 event->attach_state = PERF_ATTACH_TASK;
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01007178
7179 if (attr->type == PERF_TYPE_TRACEPOINT)
7180 event->hw.tp_target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007181#ifdef CONFIG_HAVE_HW_BREAKPOINT
7182 /*
7183 * hw_breakpoint is a bit difficult here..
7184 */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01007185 else if (attr->type == PERF_TYPE_BREAKPOINT)
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007186 event->hw.bp_target = task;
7187#endif
7188 }
7189
Avi Kivity4dc0da82011-06-29 18:42:35 +03007190 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007191 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007192 context = parent_event->overflow_handler_context;
7193 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007194
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007195 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007196 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02007197
Jiri Olsa0231bb52013-02-01 11:23:45 +01007198 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007199
7200 pmu = NULL;
7201
7202 hwc = &event->hw;
7203 hwc->sample_period = attr->sample_period;
7204 if (attr->freq && attr->sample_freq)
7205 hwc->sample_period = 1;
7206 hwc->last_period = hwc->sample_period;
7207
Peter Zijlstrae7850592010-05-21 14:43:08 +02007208 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007209
7210 /*
7211 * we currently do not support PERF_FORMAT_GROUP on inherited events
7212 */
7213 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007214 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007215
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007216 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007217 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007218 goto err_ns;
7219 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007220 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007221 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007222 }
7223
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007224 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007225 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
7226 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007227 if (err)
7228 goto err_pmu;
Stephane Eraniand010b332012-02-09 23:21:00 +01007229 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007230 }
7231
7232 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007233
7234err_pmu:
7235 if (event->destroy)
7236 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08007237 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007238err_ns:
7239 if (event->ns)
7240 put_pid_ns(event->ns);
7241 kfree(event);
7242
7243 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007244}
7245
7246static int perf_copy_attr(struct perf_event_attr __user *uattr,
7247 struct perf_event_attr *attr)
7248{
7249 u32 size;
7250 int ret;
7251
7252 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
7253 return -EFAULT;
7254
7255 /*
7256 * zero the full structure, so that a short copy will be nice.
7257 */
7258 memset(attr, 0, sizeof(*attr));
7259
7260 ret = get_user(size, &uattr->size);
7261 if (ret)
7262 return ret;
7263
7264 if (size > PAGE_SIZE) /* silly large */
7265 goto err_size;
7266
7267 if (!size) /* abi compat */
7268 size = PERF_ATTR_SIZE_VER0;
7269
7270 if (size < PERF_ATTR_SIZE_VER0)
7271 goto err_size;
7272
7273 /*
7274 * If we're handed a bigger struct than we know of,
7275 * ensure all the unknown bits are 0 - i.e. new
7276 * user-space does not rely on any kernel feature
7277 * extensions we dont know about yet.
7278 */
7279 if (size > sizeof(*attr)) {
7280 unsigned char __user *addr;
7281 unsigned char __user *end;
7282 unsigned char val;
7283
7284 addr = (void __user *)uattr + sizeof(*attr);
7285 end = (void __user *)uattr + size;
7286
7287 for (; addr < end; addr++) {
7288 ret = get_user(val, addr);
7289 if (ret)
7290 return ret;
7291 if (val)
7292 goto err_size;
7293 }
7294 size = sizeof(*attr);
7295 }
7296
7297 ret = copy_from_user(attr, uattr, size);
7298 if (ret)
7299 return -EFAULT;
7300
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05307301 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007302 return -EINVAL;
7303
7304 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
7305 return -EINVAL;
7306
7307 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
7308 return -EINVAL;
7309
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007310 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
7311 u64 mask = attr->branch_sample_type;
7312
7313 /* only using defined bits */
7314 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
7315 return -EINVAL;
7316
7317 /* at least one branch bit must be set */
7318 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
7319 return -EINVAL;
7320
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007321 /* propagate priv level, when not set for branch */
7322 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
7323
7324 /* exclude_kernel checked on syscall entry */
7325 if (!attr->exclude_kernel)
7326 mask |= PERF_SAMPLE_BRANCH_KERNEL;
7327
7328 if (!attr->exclude_user)
7329 mask |= PERF_SAMPLE_BRANCH_USER;
7330
7331 if (!attr->exclude_hv)
7332 mask |= PERF_SAMPLE_BRANCH_HV;
7333 /*
7334 * adjust user setting (for HW filter setup)
7335 */
7336 attr->branch_sample_type = mask;
7337 }
Stephane Eraniane7122092013-06-06 11:02:04 +02007338 /* privileged levels capture (kernel, hv): check permissions */
7339 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02007340 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7341 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007342 }
Jiri Olsa40189942012-08-07 15:20:37 +02007343
Jiri Olsac5ebced2012-08-07 15:20:40 +02007344 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02007345 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02007346 if (ret)
7347 return ret;
7348 }
7349
7350 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
7351 if (!arch_perf_have_user_stack_dump())
7352 return -ENOSYS;
7353
7354 /*
7355 * We have __u32 type for the size, but so far
7356 * we can only use __u16 as maximum due to the
7357 * __u16 sample size limit.
7358 */
7359 if (attr->sample_stack_user >= USHRT_MAX)
7360 ret = -EINVAL;
7361 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
7362 ret = -EINVAL;
7363 }
Jiri Olsa40189942012-08-07 15:20:37 +02007364
Stephane Eranian60e23642014-09-24 13:48:37 +02007365 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
7366 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007367out:
7368 return ret;
7369
7370err_size:
7371 put_user(sizeof(*attr), &uattr->size);
7372 ret = -E2BIG;
7373 goto out;
7374}
7375
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007376static int
7377perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007378{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007379 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007380 int ret = -EINVAL;
7381
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007382 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007383 goto set;
7384
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007385 /* don't allow circular references */
7386 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007387 goto out;
7388
Peter Zijlstra0f139302010-05-20 14:35:15 +02007389 /*
7390 * Don't allow cross-cpu buffers
7391 */
7392 if (output_event->cpu != event->cpu)
7393 goto out;
7394
7395 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02007396 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02007397 */
7398 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
7399 goto out;
7400
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007401set:
7402 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007403 /* Can't redirect output if we've got an active mmap() */
7404 if (atomic_read(&event->mmap_count))
7405 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007406
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007407 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02007408 /* get the rb we want to redirect to */
7409 rb = ring_buffer_get(output_event);
7410 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007411 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007412 }
7413
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007414 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02007415
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007416 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007417unlock:
7418 mutex_unlock(&event->mmap_mutex);
7419
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007420out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007421 return ret;
7422}
7423
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007424static void mutex_lock_double(struct mutex *a, struct mutex *b)
7425{
7426 if (b < a)
7427 swap(a, b);
7428
7429 mutex_lock(a);
7430 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
7431}
7432
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007433/**
7434 * sys_perf_event_open - open a performance event, associate it to a task/cpu
7435 *
7436 * @attr_uptr: event_id type attributes for monitoring/sampling
7437 * @pid: target pid
7438 * @cpu: target cpu
7439 * @group_fd: group leader event fd
7440 */
7441SYSCALL_DEFINE5(perf_event_open,
7442 struct perf_event_attr __user *, attr_uptr,
7443 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
7444{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007445 struct perf_event *group_leader = NULL, *output_event = NULL;
7446 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007447 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007448 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007449 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04007450 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07007451 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007452 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04007453 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007454 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007455 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01007456 int f_flags = O_RDWR;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007457
7458 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02007459 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007460 return -EINVAL;
7461
7462 err = perf_copy_attr(attr_uptr, &attr);
7463 if (err)
7464 return err;
7465
7466 if (!attr.exclude_kernel) {
7467 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7468 return -EACCES;
7469 }
7470
7471 if (attr.freq) {
7472 if (attr.sample_freq > sysctl_perf_event_sample_rate)
7473 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02007474 } else {
7475 if (attr.sample_period & (1ULL << 63))
7476 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007477 }
7478
Stephane Eraniane5d13672011-02-14 11:20:01 +02007479 /*
7480 * In cgroup mode, the pid argument is used to pass the fd
7481 * opened to the cgroup directory in cgroupfs. The cpu argument
7482 * designates the cpu on which to monitor threads from that
7483 * cgroup.
7484 */
7485 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
7486 return -EINVAL;
7487
Yann Droneauda21b0b32014-01-05 21:36:33 +01007488 if (flags & PERF_FLAG_FD_CLOEXEC)
7489 f_flags |= O_CLOEXEC;
7490
7491 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007492 if (event_fd < 0)
7493 return event_fd;
7494
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007495 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04007496 err = perf_fget_light(group_fd, &group);
7497 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007498 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04007499 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007500 if (flags & PERF_FLAG_FD_OUTPUT)
7501 output_event = group_leader;
7502 if (flags & PERF_FLAG_FD_NO_GROUP)
7503 group_leader = NULL;
7504 }
7505
Stephane Eraniane5d13672011-02-14 11:20:01 +02007506 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007507 task = find_lively_task_by_vpid(pid);
7508 if (IS_ERR(task)) {
7509 err = PTR_ERR(task);
7510 goto err_group_fd;
7511 }
7512 }
7513
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007514 if (task && group_leader &&
7515 group_leader->attr.inherit != attr.inherit) {
7516 err = -EINVAL;
7517 goto err_task;
7518 }
7519
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007520 get_online_cpus();
7521
Avi Kivity4dc0da82011-06-29 18:42:35 +03007522 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
7523 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007524 if (IS_ERR(event)) {
7525 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007526 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007527 }
7528
Stephane Eraniane5d13672011-02-14 11:20:01 +02007529 if (flags & PERF_FLAG_PID_CGROUP) {
7530 err = perf_cgroup_connect(pid, event, &attr, group_leader);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007531 if (err) {
7532 __free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007533 goto err_cpus;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007534 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02007535 }
7536
Vince Weaver53b25332014-05-16 17:12:12 -04007537 if (is_sampling_event(event)) {
7538 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
7539 err = -ENOTSUPP;
7540 goto err_alloc;
7541 }
7542 }
7543
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007544 account_event(event);
7545
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007546 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007547 * Special case software events and allow them to be part of
7548 * any hardware group.
7549 */
7550 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007551
7552 if (group_leader &&
7553 (is_software_event(event) != is_software_event(group_leader))) {
7554 if (is_software_event(event)) {
7555 /*
7556 * If event and group_leader are not both a software
7557 * event, and event is, then group leader is not.
7558 *
7559 * Allow the addition of software events to !software
7560 * groups, this is safe because software events never
7561 * fail to schedule.
7562 */
7563 pmu = group_leader->pmu;
7564 } else if (is_software_event(group_leader) &&
7565 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
7566 /*
7567 * In case the group is a pure software group, and we
7568 * try to add a hardware event, move the whole group to
7569 * the hardware context.
7570 */
7571 move_group = 1;
7572 }
7573 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007574
7575 /*
7576 * Get the target context (task or percpu):
7577 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007578 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007579 if (IS_ERR(ctx)) {
7580 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007581 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007582 }
7583
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02007584 if (task) {
7585 put_task_struct(task);
7586 task = NULL;
7587 }
7588
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007589 /*
7590 * Look up the group leader (we will attach this event to it):
7591 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007592 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007593 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007594
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007595 /*
7596 * Do not allow a recursive hierarchy (this new sibling
7597 * becoming part of another group-sibling):
7598 */
7599 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007600 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007601 /*
7602 * Do not allow to attach to a group in a different
7603 * task or CPU context:
7604 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007605 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01007606 /*
7607 * Make sure we're both on the same task, or both
7608 * per-cpu events.
7609 */
7610 if (group_leader->ctx->task != ctx->task)
7611 goto err_context;
7612
7613 /*
7614 * Make sure we're both events for the same CPU;
7615 * grouping events for different CPUs is broken; since
7616 * you can never concurrently schedule them anyhow.
7617 */
7618 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007619 goto err_context;
7620 } else {
7621 if (group_leader->ctx != ctx)
7622 goto err_context;
7623 }
7624
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007625 /*
7626 * Only a group leader can be exclusive or pinned
7627 */
7628 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007629 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007630 }
7631
7632 if (output_event) {
7633 err = perf_event_set_output(event, output_event);
7634 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007635 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007636 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007637
Yann Droneauda21b0b32014-01-05 21:36:33 +01007638 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
7639 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007640 if (IS_ERR(event_file)) {
7641 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007642 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04007643 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007644
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007645 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007646 gctx = group_leader->ctx;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007647
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007648 /*
7649 * See perf_event_ctx_lock() for comments on the details
7650 * of swizzling perf_event::ctx.
7651 */
7652 mutex_lock_double(&gctx->mutex, &ctx->mutex);
7653
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007654 perf_remove_from_context(group_leader, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007655
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007656 list_for_each_entry(sibling, &group_leader->sibling_list,
7657 group_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007658 perf_remove_from_context(sibling, false);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007659 put_ctx(gctx);
7660 }
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007661 } else {
7662 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007663 }
7664
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007665 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007666
7667 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007668 /*
7669 * Wait for everybody to stop referencing the events through
7670 * the old lists, before installing it on new lists.
7671 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007672 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007673
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007674 /*
7675 * Install the group siblings before the group leader.
7676 *
7677 * Because a group leader will try and install the entire group
7678 * (through the sibling list, which is still in-tact), we can
7679 * end up with siblings installed in the wrong context.
7680 *
7681 * By installing siblings first we NO-OP because they're not
7682 * reachable through the group lists.
7683 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007684 list_for_each_entry(sibling, &group_leader->sibling_list,
7685 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007686 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01007687 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007688 get_ctx(ctx);
7689 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007690
7691 /*
7692 * Removing from the context ends up with disabled
7693 * event. What we want here is event in the initial
7694 * startup state, ready to be add into new context.
7695 */
7696 perf_event__state_init(group_leader);
7697 perf_install_in_context(ctx, group_leader, group_leader->cpu);
7698 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007699 }
7700
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007701 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007702 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007703
7704 if (move_group) {
7705 mutex_unlock(&gctx->mutex);
7706 put_ctx(gctx);
7707 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007708 mutex_unlock(&ctx->mutex);
7709
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007710 put_online_cpus();
7711
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007712 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01007713
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007714 mutex_lock(&current->perf_event_mutex);
7715 list_add_tail(&event->owner_entry, &current->perf_event_list);
7716 mutex_unlock(&current->perf_event_mutex);
7717
Peter Zijlstra8a495422010-05-27 15:47:49 +02007718 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007719 * Precalculate sample_data sizes
7720 */
7721 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007722 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007723
7724 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02007725 * Drop the reference on the group_event after placing the
7726 * new event on the sibling_list. This ensures destruction
7727 * of the group leader will find the pointer to itself in
7728 * perf_group_detach().
7729 */
Al Viro2903ff02012-08-28 12:52:22 -04007730 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007731 fd_install(event_fd, event_file);
7732 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007733
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007734err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007735 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04007736 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007737err_alloc:
7738 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007739err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007740 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007741err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007742 if (task)
7743 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007744err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04007745 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007746err_fd:
7747 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007748 return err;
7749}
7750
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007751/**
7752 * perf_event_create_kernel_counter
7753 *
7754 * @attr: attributes of the counter to create
7755 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07007756 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007757 */
7758struct perf_event *
7759perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07007760 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007761 perf_overflow_handler_t overflow_handler,
7762 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007763{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007764 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007765 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007766 int err;
7767
7768 /*
7769 * Get the target context (task or percpu):
7770 */
7771
Avi Kivity4dc0da82011-06-29 18:42:35 +03007772 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7773 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007774 if (IS_ERR(event)) {
7775 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007776 goto err;
7777 }
7778
Jiri Olsaf8697762014-08-01 14:33:01 +02007779 /* Mark owner so we could distinguish it from user events. */
7780 event->owner = EVENT_OWNER_KERNEL;
7781
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007782 account_event(event);
7783
Matt Helsley38a81da2010-09-13 13:01:20 -07007784 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007785 if (IS_ERR(ctx)) {
7786 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007787 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007788 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007789
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007790 WARN_ON_ONCE(ctx->parent_ctx);
7791 mutex_lock(&ctx->mutex);
7792 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007793 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007794 mutex_unlock(&ctx->mutex);
7795
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007796 return event;
7797
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007798err_free:
7799 free_event(event);
7800err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007801 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007802}
7803EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7804
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007805void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7806{
7807 struct perf_event_context *src_ctx;
7808 struct perf_event_context *dst_ctx;
7809 struct perf_event *event, *tmp;
7810 LIST_HEAD(events);
7811
7812 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7813 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7814
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007815 /*
7816 * See perf_event_ctx_lock() for comments on the details
7817 * of swizzling perf_event::ctx.
7818 */
7819 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007820 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7821 event_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007822 perf_remove_from_context(event, false);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007823 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007824 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02007825 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007826 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007827
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007828 /*
7829 * Wait for the events to quiesce before re-instating them.
7830 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007831 synchronize_rcu();
7832
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007833 /*
7834 * Re-instate events in 2 passes.
7835 *
7836 * Skip over group leaders and only install siblings on this first
7837 * pass, siblings will not get enabled without a leader, however a
7838 * leader will enable its siblings, even if those are still on the old
7839 * context.
7840 */
7841 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7842 if (event->group_leader == event)
7843 continue;
7844
7845 list_del(&event->migrate_entry);
7846 if (event->state >= PERF_EVENT_STATE_OFF)
7847 event->state = PERF_EVENT_STATE_INACTIVE;
7848 account_event_cpu(event, dst_cpu);
7849 perf_install_in_context(dst_ctx, event, dst_cpu);
7850 get_ctx(dst_ctx);
7851 }
7852
7853 /*
7854 * Once all the siblings are setup properly, install the group leaders
7855 * to make it go.
7856 */
Peter Zijlstra98861672013-10-03 16:02:23 +02007857 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7858 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007859 if (event->state >= PERF_EVENT_STATE_OFF)
7860 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007861 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007862 perf_install_in_context(dst_ctx, event, dst_cpu);
7863 get_ctx(dst_ctx);
7864 }
7865 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007866 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007867}
7868EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7869
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007870static void sync_child_event(struct perf_event *child_event,
7871 struct task_struct *child)
7872{
7873 struct perf_event *parent_event = child_event->parent;
7874 u64 child_val;
7875
7876 if (child_event->attr.inherit_stat)
7877 perf_event_read_event(child_event, child);
7878
Peter Zijlstrab5e58792010-05-21 14:43:12 +02007879 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007880
7881 /*
7882 * Add back the child's count to the parent's count:
7883 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02007884 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007885 atomic64_add(child_event->total_time_enabled,
7886 &parent_event->child_total_time_enabled);
7887 atomic64_add(child_event->total_time_running,
7888 &parent_event->child_total_time_running);
7889
7890 /*
7891 * Remove this event from the parent's list
7892 */
7893 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7894 mutex_lock(&parent_event->child_mutex);
7895 list_del_init(&child_event->child_list);
7896 mutex_unlock(&parent_event->child_mutex);
7897
7898 /*
Jiri Olsadc633982014-09-12 13:18:26 +02007899 * Make sure user/parent get notified, that we just
7900 * lost one event.
7901 */
7902 perf_event_wakeup(parent_event);
7903
7904 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007905 * Release the parent event, if this was the last
7906 * reference to it.
7907 */
Al Viroa6fa9412012-08-20 14:59:25 +01007908 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007909}
7910
7911static void
7912__perf_event_exit_task(struct perf_event *child_event,
7913 struct perf_event_context *child_ctx,
7914 struct task_struct *child)
7915{
Peter Zijlstra1903d502014-07-15 17:27:27 +02007916 /*
7917 * Do not destroy the 'original' grouping; because of the context
7918 * switch optimization the original events could've ended up in a
7919 * random child task.
7920 *
7921 * If we were to destroy the original group, all group related
7922 * operations would cease to function properly after this random
7923 * child dies.
7924 *
7925 * Do destroy all inherited groups, we don't care about those
7926 * and being thorough is better.
7927 */
7928 perf_remove_from_context(child_event, !!child_event->parent);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007929
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007930 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007931 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007932 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007933 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007934 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007935 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007936 sync_child_event(child_event, child);
7937 free_event(child_event);
Jiri Olsa179033b2014-08-07 11:48:26 -04007938 } else {
7939 child_event->state = PERF_EVENT_STATE_EXIT;
7940 perf_event_wakeup(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007941 }
7942}
7943
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007944static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007945{
Peter Zijlstraebf905f2014-05-29 19:00:24 +02007946 struct perf_event *child_event, *next;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02007947 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007948 unsigned long flags;
7949
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007950 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007951 perf_event_task(child, NULL, 0);
7952 return;
7953 }
7954
7955 local_irq_save(flags);
7956 /*
7957 * We can't reschedule here because interrupts are disabled,
7958 * and either child is current or it is a task that can't be
7959 * scheduled, so we are now safe from rescheduling changing
7960 * our context.
7961 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01007962 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007963
7964 /*
7965 * Take the context lock here so that if find_get_context is
7966 * reading child->perf_event_ctxp, we wait until it has
7967 * incremented the context's refcount before we do put_ctx below.
7968 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007969 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02007970 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007971 child->perf_event_ctxp[ctxn] = NULL;
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007972
7973 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007974 * If this context is a clone; unclone it so it can't get
7975 * swapped to another process while we're removing all
7976 * the events from it.
7977 */
Peter Zijlstra211de6e2014-09-30 19:23:08 +02007978 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01007979 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007980 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007981
Peter Zijlstra211de6e2014-09-30 19:23:08 +02007982 if (clone_ctx)
7983 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007984
7985 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007986 * Report the task dead after unscheduling the events so that we
7987 * won't get any samples after PERF_RECORD_EXIT. We can however still
7988 * get a few PERF_RECORD_READ events.
7989 */
7990 perf_event_task(child, child_ctx, 0);
7991
7992 /*
7993 * We can recurse on the same lock type through:
7994 *
7995 * __perf_event_exit_task()
7996 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01007997 * put_event()
7998 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007999 *
8000 * But since its the parent context it won't be the same instance.
8001 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02008002 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008003
Peter Zijlstraebf905f2014-05-29 19:00:24 +02008004 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008005 __perf_event_exit_task(child_event, child_ctx, child);
8006
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008007 mutex_unlock(&child_ctx->mutex);
8008
8009 put_ctx(child_ctx);
8010}
8011
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008012/*
8013 * When a child task exits, feed back event values to parent events.
8014 */
8015void perf_event_exit_task(struct task_struct *child)
8016{
Peter Zijlstra88821352010-11-09 19:01:43 +01008017 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008018 int ctxn;
8019
Peter Zijlstra88821352010-11-09 19:01:43 +01008020 mutex_lock(&child->perf_event_mutex);
8021 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
8022 owner_entry) {
8023 list_del_init(&event->owner_entry);
8024
8025 /*
8026 * Ensure the list deletion is visible before we clear
8027 * the owner, closes a race against perf_release() where
8028 * we need to serialize on the owner->perf_event_mutex.
8029 */
8030 smp_wmb();
8031 event->owner = NULL;
8032 }
8033 mutex_unlock(&child->perf_event_mutex);
8034
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008035 for_each_task_context_nr(ctxn)
8036 perf_event_exit_task_context(child, ctxn);
8037}
8038
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008039static void perf_free_event(struct perf_event *event,
8040 struct perf_event_context *ctx)
8041{
8042 struct perf_event *parent = event->parent;
8043
8044 if (WARN_ON_ONCE(!parent))
8045 return;
8046
8047 mutex_lock(&parent->child_mutex);
8048 list_del_init(&event->child_list);
8049 mutex_unlock(&parent->child_mutex);
8050
Al Viroa6fa9412012-08-20 14:59:25 +01008051 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008052
Peter Zijlstra652884f2015-01-23 11:20:10 +01008053 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02008054 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008055 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +01008056 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008057 free_event(event);
8058}
8059
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008060/*
Peter Zijlstra652884f2015-01-23 11:20:10 +01008061 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008062 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +01008063 *
8064 * Not all locks are strictly required, but take them anyway to be nice and
8065 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008066 */
8067void perf_event_free_task(struct task_struct *task)
8068{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008069 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008070 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008071 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008072
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008073 for_each_task_context_nr(ctxn) {
8074 ctx = task->perf_event_ctxp[ctxn];
8075 if (!ctx)
8076 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008077
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008078 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008079again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008080 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
8081 group_entry)
8082 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008083
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008084 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
8085 group_entry)
8086 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008087
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008088 if (!list_empty(&ctx->pinned_groups) ||
8089 !list_empty(&ctx->flexible_groups))
8090 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008091
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008092 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008093
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008094 put_ctx(ctx);
8095 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008096}
8097
Peter Zijlstra4e231c72010-09-09 21:01:59 +02008098void perf_event_delayed_put(struct task_struct *task)
8099{
8100 int ctxn;
8101
8102 for_each_task_context_nr(ctxn)
8103 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
8104}
8105
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008106/*
8107 * inherit a event from parent task to child task:
8108 */
8109static struct perf_event *
8110inherit_event(struct perf_event *parent_event,
8111 struct task_struct *parent,
8112 struct perf_event_context *parent_ctx,
8113 struct task_struct *child,
8114 struct perf_event *group_leader,
8115 struct perf_event_context *child_ctx)
8116{
Jiri Olsa1929def2014-09-12 13:18:27 +02008117 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008118 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02008119 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008120
8121 /*
8122 * Instead of creating recursive hierarchies of events,
8123 * we link inherited events back to the original parent,
8124 * which has a filp for sure, which we use as the reference
8125 * count:
8126 */
8127 if (parent_event->parent)
8128 parent_event = parent_event->parent;
8129
8130 child_event = perf_event_alloc(&parent_event->attr,
8131 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008132 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008133 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008134 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008135 if (IS_ERR(child_event))
8136 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01008137
Jiri Olsafadfe7b2014-08-01 14:33:02 +02008138 if (is_orphaned_event(parent_event) ||
8139 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Al Viroa6fa9412012-08-20 14:59:25 +01008140 free_event(child_event);
8141 return NULL;
8142 }
8143
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008144 get_ctx(child_ctx);
8145
8146 /*
8147 * Make the child state follow the state of the parent event,
8148 * not its attr.disabled bit. We hold the parent's mutex,
8149 * so we won't race with perf_event_{en, dis}able_family.
8150 */
Jiri Olsa1929def2014-09-12 13:18:27 +02008151 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008152 child_event->state = PERF_EVENT_STATE_INACTIVE;
8153 else
8154 child_event->state = PERF_EVENT_STATE_OFF;
8155
8156 if (parent_event->attr.freq) {
8157 u64 sample_period = parent_event->hw.sample_period;
8158 struct hw_perf_event *hwc = &child_event->hw;
8159
8160 hwc->sample_period = sample_period;
8161 hwc->last_period = sample_period;
8162
8163 local64_set(&hwc->period_left, sample_period);
8164 }
8165
8166 child_event->ctx = child_ctx;
8167 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03008168 child_event->overflow_handler_context
8169 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008170
8171 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02008172 * Precalculate sample_data sizes
8173 */
8174 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02008175 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02008176
8177 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008178 * Link it up in the child's context:
8179 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02008180 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008181 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02008182 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008183
8184 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008185 * Link this into the parent event's child list
8186 */
8187 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
8188 mutex_lock(&parent_event->child_mutex);
8189 list_add_tail(&child_event->child_list, &parent_event->child_list);
8190 mutex_unlock(&parent_event->child_mutex);
8191
8192 return child_event;
8193}
8194
8195static int inherit_group(struct perf_event *parent_event,
8196 struct task_struct *parent,
8197 struct perf_event_context *parent_ctx,
8198 struct task_struct *child,
8199 struct perf_event_context *child_ctx)
8200{
8201 struct perf_event *leader;
8202 struct perf_event *sub;
8203 struct perf_event *child_ctr;
8204
8205 leader = inherit_event(parent_event, parent, parent_ctx,
8206 child, NULL, child_ctx);
8207 if (IS_ERR(leader))
8208 return PTR_ERR(leader);
8209 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
8210 child_ctr = inherit_event(sub, parent, parent_ctx,
8211 child, leader, child_ctx);
8212 if (IS_ERR(child_ctr))
8213 return PTR_ERR(child_ctr);
8214 }
8215 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008216}
8217
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008218static int
8219inherit_task_group(struct perf_event *event, struct task_struct *parent,
8220 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008221 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008222 int *inherited_all)
8223{
8224 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008225 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008226
8227 if (!event->attr.inherit) {
8228 *inherited_all = 0;
8229 return 0;
8230 }
8231
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008232 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008233 if (!child_ctx) {
8234 /*
8235 * This is executed from the parent task context, so
8236 * inherit events that have been marked for cloning.
8237 * First allocate and initialize a context for the
8238 * child.
8239 */
8240
Jiri Olsa734df5a2013-07-09 17:44:10 +02008241 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008242 if (!child_ctx)
8243 return -ENOMEM;
8244
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008245 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008246 }
8247
8248 ret = inherit_group(event, parent, parent_ctx,
8249 child, child_ctx);
8250
8251 if (ret)
8252 *inherited_all = 0;
8253
8254 return ret;
8255}
8256
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008257/*
8258 * Initialize the perf_event context in task_struct
8259 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02008260static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008261{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008262 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008263 struct perf_event_context *cloned_ctx;
8264 struct perf_event *event;
8265 struct task_struct *parent = current;
8266 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008267 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008268 int ret = 0;
8269
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008270 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008271 return 0;
8272
8273 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008274 * If the parent's context is a clone, pin it so it won't get
8275 * swapped under us.
8276 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008277 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02008278 if (!parent_ctx)
8279 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008280
8281 /*
8282 * No need to check if parent_ctx != NULL here; since we saw
8283 * it non-NULL earlier, the only reason for it to become NULL
8284 * is if we exit, and since we're currently in the middle of
8285 * a fork we can't be exiting at the same time.
8286 */
8287
8288 /*
8289 * Lock the parent list. No need to lock the child - not PID
8290 * hashed yet and not running, so nobody can access it.
8291 */
8292 mutex_lock(&parent_ctx->mutex);
8293
8294 /*
8295 * We dont have to disable NMIs - we are only looking at
8296 * the list, not manipulating it:
8297 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008298 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008299 ret = inherit_task_group(event, parent, parent_ctx,
8300 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008301 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008302 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008303 }
8304
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008305 /*
8306 * We can't hold ctx->lock when iterating the ->flexible_group list due
8307 * to allocations, but we need to prevent rotation because
8308 * rotate_ctx() will change the list from interrupt context.
8309 */
8310 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
8311 parent_ctx->rotate_disable = 1;
8312 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
8313
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008314 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008315 ret = inherit_task_group(event, parent, parent_ctx,
8316 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008317 if (ret)
8318 break;
8319 }
8320
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008321 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
8322 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008323
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008324 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008325
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01008326 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008327 /*
8328 * Mark the child context as a clone of the parent
8329 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008330 *
8331 * Note that if the parent is a clone, the holding of
8332 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008333 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008334 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008335 if (cloned_ctx) {
8336 child_ctx->parent_ctx = cloned_ctx;
8337 child_ctx->parent_gen = parent_ctx->parent_gen;
8338 } else {
8339 child_ctx->parent_ctx = parent_ctx;
8340 child_ctx->parent_gen = parent_ctx->generation;
8341 }
8342 get_ctx(child_ctx->parent_ctx);
8343 }
8344
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008345 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008346 mutex_unlock(&parent_ctx->mutex);
8347
8348 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008349 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008350
8351 return ret;
8352}
8353
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008354/*
8355 * Initialize the perf_event context in task_struct
8356 */
8357int perf_event_init_task(struct task_struct *child)
8358{
8359 int ctxn, ret;
8360
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01008361 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
8362 mutex_init(&child->perf_event_mutex);
8363 INIT_LIST_HEAD(&child->perf_event_list);
8364
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008365 for_each_task_context_nr(ctxn) {
8366 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07008367 if (ret) {
8368 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008369 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07008370 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008371 }
8372
8373 return 0;
8374}
8375
Paul Mackerras220b1402010-03-10 20:45:52 +11008376static void __init perf_event_init_all_cpus(void)
8377{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008378 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11008379 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11008380
8381 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008382 swhash = &per_cpu(swevent_htable, cpu);
8383 mutex_init(&swhash->hlist_mutex);
Mark Rutland2fde4f92015-01-07 15:01:54 +00008384 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11008385 }
8386}
8387
Paul Gortmaker0db06282013-06-19 14:53:51 -04008388static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008389{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008390 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008391
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008392 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02008393 swhash->online = true;
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008394 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008395 struct swevent_hlist *hlist;
8396
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008397 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
8398 WARN_ON(!hlist);
8399 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008400 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008401 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008402}
8403
Peter Zijlstrac2774432010-12-08 15:29:02 +01008404#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008405static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008406{
Mark Rutland226424e2014-11-05 16:11:44 +00008407 struct remove_event re = { .detach_group = true };
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008408 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008409
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008410 rcu_read_lock();
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02008411 list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
8412 __perf_remove_from_context(&re);
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008413 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008414}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008415
8416static void perf_event_exit_cpu_context(int cpu)
8417{
8418 struct perf_event_context *ctx;
8419 struct pmu *pmu;
8420 int idx;
8421
8422 idx = srcu_read_lock(&pmus_srcu);
8423 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02008424 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008425
8426 mutex_lock(&ctx->mutex);
8427 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
8428 mutex_unlock(&ctx->mutex);
8429 }
8430 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008431}
8432
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008433static void perf_event_exit_cpu(int cpu)
8434{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008435 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008436
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008437 perf_event_exit_cpu_context(cpu);
8438
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008439 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02008440 swhash->online = false;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008441 swevent_hlist_release(swhash);
8442 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008443}
8444#else
8445static inline void perf_event_exit_cpu(int cpu) { }
8446#endif
8447
Peter Zijlstrac2774432010-12-08 15:29:02 +01008448static int
8449perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
8450{
8451 int cpu;
8452
8453 for_each_online_cpu(cpu)
8454 perf_event_exit_cpu(cpu);
8455
8456 return NOTIFY_OK;
8457}
8458
8459/*
8460 * Run the perf reboot notifier at the very last possible moment so that
8461 * the generic watchdog code runs as long as possible.
8462 */
8463static struct notifier_block perf_reboot_notifier = {
8464 .notifier_call = perf_reboot,
8465 .priority = INT_MIN,
8466};
8467
Paul Gortmaker0db06282013-06-19 14:53:51 -04008468static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008469perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
8470{
8471 unsigned int cpu = (long)hcpu;
8472
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008473 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008474
8475 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02008476 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008477 perf_event_init_cpu(cpu);
8478 break;
8479
Peter Zijlstra5e116372010-06-11 13:35:08 +02008480 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008481 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008482 perf_event_exit_cpu(cpu);
8483 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008484 default:
8485 break;
8486 }
8487
8488 return NOTIFY_OK;
8489}
8490
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008491void __init perf_event_init(void)
8492{
Jason Wessel3c502e72010-11-04 17:33:01 -05008493 int ret;
8494
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008495 idr_init(&pmu_idr);
8496
Paul Mackerras220b1402010-03-10 20:45:52 +11008497 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008498 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008499 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
8500 perf_pmu_register(&perf_cpu_clock, NULL, -1);
8501 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008502 perf_tp_register();
8503 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01008504 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05008505
8506 ret = init_hw_breakpoint();
8507 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02008508
8509 /* do not patch jump label more than once per second */
8510 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01008511
8512 /*
8513 * Build time assertion that we keep the data_head at the intended
8514 * location. IOW, validation we got the __reserved[] size right.
8515 */
8516 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
8517 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008518}
Peter Zijlstraabe43402010-11-17 23:17:37 +01008519
8520static int __init perf_event_sysfs_init(void)
8521{
8522 struct pmu *pmu;
8523 int ret;
8524
8525 mutex_lock(&pmus_lock);
8526
8527 ret = bus_register(&pmu_bus);
8528 if (ret)
8529 goto unlock;
8530
8531 list_for_each_entry(pmu, &pmus, entry) {
8532 if (!pmu->name || pmu->type < 0)
8533 continue;
8534
8535 ret = pmu_dev_alloc(pmu);
8536 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
8537 }
8538 pmu_bus_running = 1;
8539 ret = 0;
8540
8541unlock:
8542 mutex_unlock(&pmus_lock);
8543
8544 return ret;
8545}
8546device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008547
8548#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04008549static struct cgroup_subsys_state *
8550perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008551{
8552 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02008553
Li Zefan1b15d052011-03-03 14:26:06 +08008554 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008555 if (!jc)
8556 return ERR_PTR(-ENOMEM);
8557
Stephane Eraniane5d13672011-02-14 11:20:01 +02008558 jc->info = alloc_percpu(struct perf_cgroup_info);
8559 if (!jc->info) {
8560 kfree(jc);
8561 return ERR_PTR(-ENOMEM);
8562 }
8563
Stephane Eraniane5d13672011-02-14 11:20:01 +02008564 return &jc->css;
8565}
8566
Tejun Heoeb954192013-08-08 20:11:23 -04008567static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008568{
Tejun Heoeb954192013-08-08 20:11:23 -04008569 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
8570
Stephane Eraniane5d13672011-02-14 11:20:01 +02008571 free_percpu(jc->info);
8572 kfree(jc);
8573}
8574
8575static int __perf_cgroup_move(void *info)
8576{
8577 struct task_struct *task = info;
8578 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
8579 return 0;
8580}
8581
Tejun Heoeb954192013-08-08 20:11:23 -04008582static void perf_cgroup_attach(struct cgroup_subsys_state *css,
8583 struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008584{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008585 struct task_struct *task;
8586
Tejun Heo924f0d92014-02-13 06:58:41 -05008587 cgroup_taskset_for_each(task, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008588 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008589}
8590
Tejun Heoeb954192013-08-08 20:11:23 -04008591static void perf_cgroup_exit(struct cgroup_subsys_state *css,
8592 struct cgroup_subsys_state *old_css,
Li Zefan761b3ef2012-01-31 13:47:36 +08008593 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008594{
8595 /*
8596 * cgroup_exit() is called in the copy_process() failure path.
8597 * Ignore this case since the task hasn't ran yet, this avoids
8598 * trying to poke a half freed task state from generic code.
8599 */
8600 if (!(task->flags & PF_EXITING))
8601 return;
8602
Tejun Heobb9d97b2011-12-12 18:12:21 -08008603 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008604}
8605
Tejun Heo073219e2014-02-08 10:36:58 -05008606struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008607 .css_alloc = perf_cgroup_css_alloc,
8608 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008609 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008610 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02008611};
8612#endif /* CONFIG_CGROUP_PERF */