blob: 37cc20e8aa3b353f4319f3b599129a6ead681644 [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
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200875static DEFINE_PER_CPU(struct list_head, rotation_list);
876
877/*
878 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
879 * because they're strictly cpu affine and rotate_start is called with IRQs
880 * disabled, while rotate_context is called from IRQ context.
881 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200882static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200883{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200884 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500885 struct list_head *head = this_cpu_ptr(&rotation_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
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +0200889 if (list_empty(&cpuctx->rotation_list))
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200890 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200891}
892
893static void get_ctx(struct perf_event_context *ctx)
894{
895 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
896}
897
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200898static void put_ctx(struct perf_event_context *ctx)
899{
900 if (atomic_dec_and_test(&ctx->refcount)) {
901 if (ctx->parent_ctx)
902 put_ctx(ctx->parent_ctx);
903 if (ctx->task)
904 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800905 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200906 }
907}
908
Peter Zijlstra211de6e2014-09-30 19:23:08 +0200909/*
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100910 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
911 * perf_pmu_migrate_context() we need some magic.
912 *
913 * Those places that change perf_event::ctx will hold both
914 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
915 *
916 * Lock ordering is by mutex address. There is one other site where
917 * perf_event_context::mutex nests and that is put_event(). But remember that
918 * that is a parent<->child context relation, and migration does not affect
919 * children, therefore these two orderings should not interact.
920 *
921 * The change in perf_event::ctx does not affect children (as claimed above)
922 * because the sys_perf_event_open() case will install a new event and break
923 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
924 * concerned with cpuctx and that doesn't have children.
925 *
926 * The places that change perf_event::ctx will issue:
927 *
928 * perf_remove_from_context();
929 * synchronize_rcu();
930 * perf_install_in_context();
931 *
932 * to affect the change. The remove_from_context() + synchronize_rcu() should
933 * quiesce the event, after which we can install it in the new location. This
934 * means that only external vectors (perf_fops, prctl) can perturb the event
935 * while in transit. Therefore all such accessors should also acquire
936 * perf_event_context::mutex to serialize against this.
937 *
938 * However; because event->ctx can change while we're waiting to acquire
939 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
940 * function.
941 *
942 * Lock order:
943 * task_struct::perf_event_mutex
944 * perf_event_context::mutex
945 * perf_event_context::lock
946 * perf_event::child_mutex;
947 * perf_event::mmap_mutex
948 * mmap_sem
949 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +0100950static struct perf_event_context *
951perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100952{
953 struct perf_event_context *ctx;
954
955again:
956 rcu_read_lock();
957 ctx = ACCESS_ONCE(event->ctx);
958 if (!atomic_inc_not_zero(&ctx->refcount)) {
959 rcu_read_unlock();
960 goto again;
961 }
962 rcu_read_unlock();
963
Peter Zijlstraa83fe282015-01-29 14:44:34 +0100964 mutex_lock_nested(&ctx->mutex, nesting);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100965 if (event->ctx != ctx) {
966 mutex_unlock(&ctx->mutex);
967 put_ctx(ctx);
968 goto again;
969 }
970
971 return ctx;
972}
973
Peter Zijlstraa83fe282015-01-29 14:44:34 +0100974static inline struct perf_event_context *
975perf_event_ctx_lock(struct perf_event *event)
976{
977 return perf_event_ctx_lock_nested(event, 0);
978}
979
Peter Zijlstraf63a8da2015-01-23 12:24:14 +0100980static void perf_event_ctx_unlock(struct perf_event *event,
981 struct perf_event_context *ctx)
982{
983 mutex_unlock(&ctx->mutex);
984 put_ctx(ctx);
985}
986
987/*
Peter Zijlstra211de6e2014-09-30 19:23:08 +0200988 * This must be done under the ctx->lock, such as to serialize against
989 * context_equiv(), therefore we cannot call put_ctx() since that might end up
990 * calling scheduler related locks and ctx->lock nests inside those.
991 */
992static __must_check struct perf_event_context *
993unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200994{
Peter Zijlstra211de6e2014-09-30 19:23:08 +0200995 struct perf_event_context *parent_ctx = ctx->parent_ctx;
996
997 lockdep_assert_held(&ctx->lock);
998
999 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001000 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001001 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02001002
1003 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001004}
1005
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001006static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1007{
1008 /*
1009 * only top level events have the pid namespace they were created in
1010 */
1011 if (event->parent)
1012 event = event->parent;
1013
1014 return task_tgid_nr_ns(p, event->ns);
1015}
1016
1017static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1018{
1019 /*
1020 * only top level events have the pid namespace they were created in
1021 */
1022 if (event->parent)
1023 event = event->parent;
1024
1025 return task_pid_nr_ns(p, event->ns);
1026}
1027
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001028/*
1029 * If we inherit events we want to return the parent event id
1030 * to userspace.
1031 */
1032static u64 primary_event_id(struct perf_event *event)
1033{
1034 u64 id = event->id;
1035
1036 if (event->parent)
1037 id = event->parent->id;
1038
1039 return id;
1040}
1041
1042/*
1043 * Get the perf_event_context for a task and lock it.
1044 * This has to cope with with the fact that until it is locked,
1045 * the context could get moved to another task.
1046 */
1047static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001048perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001049{
1050 struct perf_event_context *ctx;
1051
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001052retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001053 /*
1054 * One of the few rules of preemptible RCU is that one cannot do
1055 * rcu_read_unlock() while holding a scheduler (or nested) lock when
1056 * part of the read side critical section was preemptible -- see
1057 * rcu_read_unlock_special().
1058 *
1059 * Since ctx->lock nests under rq->lock we must ensure the entire read
1060 * side critical section is non-preemptible.
1061 */
1062 preempt_disable();
1063 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001064 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001065 if (ctx) {
1066 /*
1067 * If this context is a clone of another, it might
1068 * get swapped for another underneath us by
1069 * perf_event_task_sched_out, though the
1070 * rcu_read_lock() protects us from any context
1071 * getting freed. Lock the context and check if it
1072 * got swapped before we could get the lock, and retry
1073 * if so. If we locked the right context, then it
1074 * can't get swapped on us any more.
1075 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001076 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001077 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001078 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001079 rcu_read_unlock();
1080 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001081 goto retry;
1082 }
1083
1084 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001085 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001086 ctx = NULL;
1087 }
1088 }
1089 rcu_read_unlock();
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001090 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001091 return ctx;
1092}
1093
1094/*
1095 * Get the context for a task and increment its pin_count so it
1096 * can't get swapped to another task. This also increments its
1097 * reference count so that the context can't get freed.
1098 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001099static struct perf_event_context *
1100perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001101{
1102 struct perf_event_context *ctx;
1103 unsigned long flags;
1104
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001105 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001106 if (ctx) {
1107 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001108 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001109 }
1110 return ctx;
1111}
1112
1113static void perf_unpin_context(struct perf_event_context *ctx)
1114{
1115 unsigned long flags;
1116
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001117 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001118 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001119 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001120}
1121
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001122/*
1123 * Update the record of the current time in a context.
1124 */
1125static void update_context_time(struct perf_event_context *ctx)
1126{
1127 u64 now = perf_clock();
1128
1129 ctx->time += now - ctx->timestamp;
1130 ctx->timestamp = now;
1131}
1132
Stephane Eranian41587552011-01-03 18:20:01 +02001133static u64 perf_event_time(struct perf_event *event)
1134{
1135 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001136
1137 if (is_cgroup_event(event))
1138 return perf_cgroup_event_time(event);
1139
Stephane Eranian41587552011-01-03 18:20:01 +02001140 return ctx ? ctx->time : 0;
1141}
1142
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001143/*
1144 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001145 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001146 */
1147static void update_event_times(struct perf_event *event)
1148{
1149 struct perf_event_context *ctx = event->ctx;
1150 u64 run_end;
1151
1152 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1153 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1154 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001155 /*
1156 * in cgroup mode, time_enabled represents
1157 * the time the event was enabled AND active
1158 * tasks were in the monitored cgroup. This is
1159 * independent of the activity of the context as
1160 * there may be a mix of cgroup and non-cgroup events.
1161 *
1162 * That is why we treat cgroup events differently
1163 * here.
1164 */
1165 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001166 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001167 else if (ctx->is_active)
1168 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001169 else
1170 run_end = event->tstamp_stopped;
1171
1172 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001173
1174 if (event->state == PERF_EVENT_STATE_INACTIVE)
1175 run_end = event->tstamp_stopped;
1176 else
Stephane Eranian41587552011-01-03 18:20:01 +02001177 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001178
1179 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001180
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001181}
1182
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001183/*
1184 * Update total_time_enabled and total_time_running for all events in a group.
1185 */
1186static void update_group_times(struct perf_event *leader)
1187{
1188 struct perf_event *event;
1189
1190 update_event_times(leader);
1191 list_for_each_entry(event, &leader->sibling_list, group_entry)
1192 update_event_times(event);
1193}
1194
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001195static struct list_head *
1196ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1197{
1198 if (event->attr.pinned)
1199 return &ctx->pinned_groups;
1200 else
1201 return &ctx->flexible_groups;
1202}
1203
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001204/*
1205 * Add a event from the lists for its context.
1206 * Must be called with ctx->mutex and ctx->lock held.
1207 */
1208static void
1209list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1210{
Peter Zijlstra8a495422010-05-27 15:47:49 +02001211 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1212 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001213
1214 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001215 * If we're a stand alone event or group leader, we go to the context
1216 * list, group events are kept attached to the group so that
1217 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001218 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001219 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001220 struct list_head *list;
1221
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001222 if (is_software_event(event))
1223 event->group_flags |= PERF_GROUP_SOFTWARE;
1224
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001225 list = ctx_group_list(event, ctx);
1226 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001227 }
1228
Peter Zijlstra08309372011-03-03 11:31:20 +01001229 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001230 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001231
Stephane Eraniand010b332012-02-09 23:21:00 +01001232 if (has_branch_stack(event))
1233 ctx->nr_branch_stack++;
1234
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001235 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001236 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001237 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001238 ctx->nr_events++;
1239 if (event->attr.inherit_stat)
1240 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001241
1242 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001243}
1244
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001245/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001246 * Initialize event state based on the perf_event_attr::disabled.
1247 */
1248static inline void perf_event__state_init(struct perf_event *event)
1249{
1250 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1251 PERF_EVENT_STATE_INACTIVE;
1252}
1253
1254/*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001255 * Called at perf_event creation and when events are attached/detached from a
1256 * group.
1257 */
1258static void perf_event__read_size(struct perf_event *event)
1259{
1260 int entry = sizeof(u64); /* value */
1261 int size = 0;
1262 int nr = 1;
1263
1264 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1265 size += sizeof(u64);
1266
1267 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1268 size += sizeof(u64);
1269
1270 if (event->attr.read_format & PERF_FORMAT_ID)
1271 entry += sizeof(u64);
1272
1273 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1274 nr += event->group_leader->nr_siblings;
1275 size += sizeof(u64);
1276 }
1277
1278 size += entry * nr;
1279 event->read_size = size;
1280}
1281
1282static void perf_event__header_size(struct perf_event *event)
1283{
1284 struct perf_sample_data *data;
1285 u64 sample_type = event->attr.sample_type;
1286 u16 size = 0;
1287
1288 perf_event__read_size(event);
1289
1290 if (sample_type & PERF_SAMPLE_IP)
1291 size += sizeof(data->ip);
1292
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001293 if (sample_type & PERF_SAMPLE_ADDR)
1294 size += sizeof(data->addr);
1295
1296 if (sample_type & PERF_SAMPLE_PERIOD)
1297 size += sizeof(data->period);
1298
Andi Kleenc3feedf2013-01-24 16:10:28 +01001299 if (sample_type & PERF_SAMPLE_WEIGHT)
1300 size += sizeof(data->weight);
1301
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001302 if (sample_type & PERF_SAMPLE_READ)
1303 size += event->read_size;
1304
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001305 if (sample_type & PERF_SAMPLE_DATA_SRC)
1306 size += sizeof(data->data_src.val);
1307
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001308 if (sample_type & PERF_SAMPLE_TRANSACTION)
1309 size += sizeof(data->txn);
1310
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001311 event->header_size = size;
1312}
1313
1314static void perf_event__id_header_size(struct perf_event *event)
1315{
1316 struct perf_sample_data *data;
1317 u64 sample_type = event->attr.sample_type;
1318 u16 size = 0;
1319
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001320 if (sample_type & PERF_SAMPLE_TID)
1321 size += sizeof(data->tid_entry);
1322
1323 if (sample_type & PERF_SAMPLE_TIME)
1324 size += sizeof(data->time);
1325
Adrian Hunterff3d5272013-08-27 11:23:07 +03001326 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1327 size += sizeof(data->id);
1328
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001329 if (sample_type & PERF_SAMPLE_ID)
1330 size += sizeof(data->id);
1331
1332 if (sample_type & PERF_SAMPLE_STREAM_ID)
1333 size += sizeof(data->stream_id);
1334
1335 if (sample_type & PERF_SAMPLE_CPU)
1336 size += sizeof(data->cpu_entry);
1337
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001338 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001339}
1340
Peter Zijlstra8a495422010-05-27 15:47:49 +02001341static void perf_group_attach(struct perf_event *event)
1342{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001343 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001344
Peter Zijlstra74c33372010-10-15 11:40:29 +02001345 /*
1346 * We can have double attach due to group movement in perf_event_open.
1347 */
1348 if (event->attach_state & PERF_ATTACH_GROUP)
1349 return;
1350
Peter Zijlstra8a495422010-05-27 15:47:49 +02001351 event->attach_state |= PERF_ATTACH_GROUP;
1352
1353 if (group_leader == event)
1354 return;
1355
Peter Zijlstra652884f2015-01-23 11:20:10 +01001356 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1357
Peter Zijlstra8a495422010-05-27 15:47:49 +02001358 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1359 !is_software_event(event))
1360 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1361
1362 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1363 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001364
1365 perf_event__header_size(group_leader);
1366
1367 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1368 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001369}
1370
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001371/*
1372 * Remove a event from the lists for its context.
1373 * Must be called with ctx->mutex and ctx->lock held.
1374 */
1375static void
1376list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1377{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001378 struct perf_cpu_context *cpuctx;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001379
1380 WARN_ON_ONCE(event->ctx != ctx);
1381 lockdep_assert_held(&ctx->lock);
1382
Peter Zijlstra8a495422010-05-27 15:47:49 +02001383 /*
1384 * We can have double detach due to exit/hot-unplug + close.
1385 */
1386 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001387 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001388
1389 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1390
Stephane Eranian68cacd22011-03-23 16:03:06 +01001391 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001392 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001393 cpuctx = __get_cpu_context(ctx);
1394 /*
1395 * if there are no more cgroup events
1396 * then cler cgrp to avoid stale pointer
1397 * in update_cgrp_time_from_cpuctx()
1398 */
1399 if (!ctx->nr_cgroups)
1400 cpuctx->cgrp = NULL;
1401 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001402
Stephane Eraniand010b332012-02-09 23:21:00 +01001403 if (has_branch_stack(event))
1404 ctx->nr_branch_stack--;
1405
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001406 ctx->nr_events--;
1407 if (event->attr.inherit_stat)
1408 ctx->nr_stat--;
1409
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001410 list_del_rcu(&event->event_entry);
1411
Peter Zijlstra8a495422010-05-27 15:47:49 +02001412 if (event->group_leader == event)
1413 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001414
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001415 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001416
1417 /*
1418 * If event was in error state, then keep it
1419 * that way, otherwise bogus counts will be
1420 * returned on read(). The only way to get out
1421 * of error state is by explicit re-enabling
1422 * of the event
1423 */
1424 if (event->state > PERF_EVENT_STATE_OFF)
1425 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001426
1427 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001428}
1429
Peter Zijlstra8a495422010-05-27 15:47:49 +02001430static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001431{
1432 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001433 struct list_head *list = NULL;
1434
1435 /*
1436 * We can have double detach due to exit/hot-unplug + close.
1437 */
1438 if (!(event->attach_state & PERF_ATTACH_GROUP))
1439 return;
1440
1441 event->attach_state &= ~PERF_ATTACH_GROUP;
1442
1443 /*
1444 * If this is a sibling, remove it from its group.
1445 */
1446 if (event->group_leader != event) {
1447 list_del_init(&event->group_entry);
1448 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001449 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001450 }
1451
1452 if (!list_empty(&event->group_entry))
1453 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001454
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001455 /*
1456 * If this was a group event with sibling events then
1457 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001458 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001459 */
1460 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001461 if (list)
1462 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001463 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001464
1465 /* Inherit group flags from the previous leader */
1466 sibling->group_flags = event->group_flags;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001467
1468 WARN_ON_ONCE(sibling->ctx != event->ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001469 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001470
1471out:
1472 perf_event__header_size(event->group_leader);
1473
1474 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1475 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001476}
1477
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001478/*
1479 * User event without the task.
1480 */
1481static bool is_orphaned_event(struct perf_event *event)
1482{
1483 return event && !is_kernel_event(event) && !event->owner;
1484}
1485
1486/*
1487 * Event has a parent but parent's task finished and it's
1488 * alive only because of children holding refference.
1489 */
1490static bool is_orphaned_child(struct perf_event *event)
1491{
1492 return is_orphaned_event(event->parent);
1493}
1494
1495static void orphans_remove_work(struct work_struct *work);
1496
1497static void schedule_orphans_remove(struct perf_event_context *ctx)
1498{
1499 if (!ctx->task || ctx->orphans_remove_sched || !perf_wq)
1500 return;
1501
1502 if (queue_delayed_work(perf_wq, &ctx->orphans_remove, 1)) {
1503 get_ctx(ctx);
1504 ctx->orphans_remove_sched = true;
1505 }
1506}
1507
1508static int __init perf_workqueue_init(void)
1509{
1510 perf_wq = create_singlethread_workqueue("perf");
1511 WARN(!perf_wq, "failed to create perf workqueue\n");
1512 return perf_wq ? 0 : -1;
1513}
1514
1515core_initcall(perf_workqueue_init);
1516
Stephane Eranianfa66f072010-08-26 16:40:01 +02001517static inline int
1518event_filter_match(struct perf_event *event)
1519{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001520 return (event->cpu == -1 || event->cpu == smp_processor_id())
1521 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001522}
1523
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001524static void
1525event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001526 struct perf_cpu_context *cpuctx,
1527 struct perf_event_context *ctx)
1528{
Stephane Eranian41587552011-01-03 18:20:01 +02001529 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001530 u64 delta;
Peter Zijlstra652884f2015-01-23 11:20:10 +01001531
1532 WARN_ON_ONCE(event->ctx != ctx);
1533 lockdep_assert_held(&ctx->lock);
1534
Stephane Eranianfa66f072010-08-26 16:40:01 +02001535 /*
1536 * An event which could not be activated because of
1537 * filter mismatch still needs to have its timings
1538 * maintained, otherwise bogus information is return
1539 * via read() for time_enabled, time_running:
1540 */
1541 if (event->state == PERF_EVENT_STATE_INACTIVE
1542 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001543 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001544 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001545 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001546 }
1547
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001548 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001549 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001550
Alexander Shishkin44377272013-12-16 14:17:36 +02001551 perf_pmu_disable(event->pmu);
1552
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001553 event->state = PERF_EVENT_STATE_INACTIVE;
1554 if (event->pending_disable) {
1555 event->pending_disable = 0;
1556 event->state = PERF_EVENT_STATE_OFF;
1557 }
Stephane Eranian41587552011-01-03 18:20:01 +02001558 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001559 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001560 event->oncpu = -1;
1561
1562 if (!is_software_event(event))
1563 cpuctx->active_oncpu--;
1564 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001565 if (event->attr.freq && event->attr.sample_freq)
1566 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001567 if (event->attr.exclusive || !cpuctx->active_oncpu)
1568 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001569
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001570 if (is_orphaned_child(event))
1571 schedule_orphans_remove(ctx);
1572
Alexander Shishkin44377272013-12-16 14:17:36 +02001573 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001574}
1575
1576static void
1577group_sched_out(struct perf_event *group_event,
1578 struct perf_cpu_context *cpuctx,
1579 struct perf_event_context *ctx)
1580{
1581 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001582 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001583
1584 event_sched_out(group_event, cpuctx, ctx);
1585
1586 /*
1587 * Schedule out siblings (if any):
1588 */
1589 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1590 event_sched_out(event, cpuctx, ctx);
1591
Stephane Eranianfa66f072010-08-26 16:40:01 +02001592 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001593 cpuctx->exclusive = 0;
1594}
1595
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001596struct remove_event {
1597 struct perf_event *event;
1598 bool detach_group;
1599};
1600
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001601/*
1602 * Cross CPU call to remove a performance event
1603 *
1604 * We disable the event on the hardware level first. After that we
1605 * remove it from the context list.
1606 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001607static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001608{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001609 struct remove_event *re = info;
1610 struct perf_event *event = re->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001611 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001612 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001613
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001614 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001615 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001616 if (re->detach_group)
1617 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001618 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001619 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1620 ctx->is_active = 0;
1621 cpuctx->task_ctx = NULL;
1622 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001623 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001624
1625 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001626}
1627
1628
1629/*
1630 * Remove the event from a task's (or a CPU's) list of events.
1631 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001632 * CPU events are removed with a smp call. For task events we only
1633 * call when the task is on a CPU.
1634 *
1635 * If event->ctx is a cloned context, callers must make sure that
1636 * every task struct that event->ctx->task could possibly point to
1637 * remains valid. This is OK when called from perf_release since
1638 * that only calls us on the top-level context, which can't be a clone.
1639 * When called from perf_event_exit_task, it's OK because the
1640 * context has been detached from its task.
1641 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001642static void perf_remove_from_context(struct perf_event *event, bool detach_group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001643{
1644 struct perf_event_context *ctx = event->ctx;
1645 struct task_struct *task = ctx->task;
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001646 struct remove_event re = {
1647 .event = event,
1648 .detach_group = detach_group,
1649 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001650
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001651 lockdep_assert_held(&ctx->mutex);
1652
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001653 if (!task) {
1654 /*
Mark Rutland226424e2014-11-05 16:11:44 +00001655 * Per cpu events are removed via an smp call. The removal can
1656 * fail if the CPU is currently offline, but in that case we
1657 * already called __perf_remove_from_context from
1658 * perf_event_exit_cpu.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001659 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001660 cpu_function_call(event->cpu, __perf_remove_from_context, &re);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001661 return;
1662 }
1663
1664retry:
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001665 if (!task_function_call(task, __perf_remove_from_context, &re))
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001666 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001667
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001668 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001669 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001670 * If we failed to find a running task, but find the context active now
1671 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001672 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001673 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001674 raw_spin_unlock_irq(&ctx->lock);
Cong Wang3577af72014-09-02 15:27:20 -07001675 /*
1676 * Reload the task pointer, it might have been changed by
1677 * a concurrent perf_event_context_sched_out().
1678 */
1679 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001680 goto retry;
1681 }
1682
1683 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001684 * Since the task isn't running, its safe to remove the event, us
1685 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001686 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001687 if (detach_group)
1688 perf_group_detach(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001689 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001690 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001691}
1692
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001693/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001694 * Cross CPU call to disable a performance event
1695 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301696int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001697{
1698 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001699 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001700 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001701
1702 /*
1703 * If this is a per-task event, need to check whether this
1704 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001705 *
1706 * Can trigger due to concurrent perf_event_context_sched_out()
1707 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001708 */
1709 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001710 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001711
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001712 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001713
1714 /*
1715 * If the event is on, turn it off.
1716 * If it is in error state, leave it in error state.
1717 */
1718 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1719 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001720 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001721 update_group_times(event);
1722 if (event == event->group_leader)
1723 group_sched_out(event, cpuctx, ctx);
1724 else
1725 event_sched_out(event, cpuctx, ctx);
1726 event->state = PERF_EVENT_STATE_OFF;
1727 }
1728
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001729 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001730
1731 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001732}
1733
1734/*
1735 * Disable a event.
1736 *
1737 * If event->ctx is a cloned context, callers must make sure that
1738 * every task struct that event->ctx->task could possibly point to
1739 * remains valid. This condition is satisifed when called through
1740 * perf_event_for_each_child or perf_event_for_each because they
1741 * hold the top-level event's child_mutex, so any descendant that
1742 * goes to exit will block in sync_child_event.
1743 * When called from perf_pending_event it's OK because event->ctx
1744 * is the current context on this CPU and preemption is disabled,
1745 * hence we can't get into perf_event_task_sched_out for this context.
1746 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001747static void _perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001748{
1749 struct perf_event_context *ctx = event->ctx;
1750 struct task_struct *task = ctx->task;
1751
1752 if (!task) {
1753 /*
1754 * Disable the event on the cpu that it's on
1755 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001756 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001757 return;
1758 }
1759
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001760retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001761 if (!task_function_call(task, __perf_event_disable, event))
1762 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001763
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001764 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001765 /*
1766 * If the event is still active, we need to retry the cross-call.
1767 */
1768 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001769 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001770 /*
1771 * Reload the task pointer, it might have been changed by
1772 * a concurrent perf_event_context_sched_out().
1773 */
1774 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001775 goto retry;
1776 }
1777
1778 /*
1779 * Since we have the lock this context can't be scheduled
1780 * in, so we can change the state safely.
1781 */
1782 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1783 update_group_times(event);
1784 event->state = PERF_EVENT_STATE_OFF;
1785 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001786 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001787}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01001788
1789/*
1790 * Strictly speaking kernel users cannot create groups and therefore this
1791 * interface does not need the perf_event_ctx_lock() magic.
1792 */
1793void perf_event_disable(struct perf_event *event)
1794{
1795 struct perf_event_context *ctx;
1796
1797 ctx = perf_event_ctx_lock(event);
1798 _perf_event_disable(event);
1799 perf_event_ctx_unlock(event, ctx);
1800}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001801EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001802
Stephane Eraniane5d13672011-02-14 11:20:01 +02001803static void perf_set_shadow_time(struct perf_event *event,
1804 struct perf_event_context *ctx,
1805 u64 tstamp)
1806{
1807 /*
1808 * use the correct time source for the time snapshot
1809 *
1810 * We could get by without this by leveraging the
1811 * fact that to get to this function, the caller
1812 * has most likely already called update_context_time()
1813 * and update_cgrp_time_xx() and thus both timestamp
1814 * are identical (or very close). Given that tstamp is,
1815 * already adjusted for cgroup, we could say that:
1816 * tstamp - ctx->timestamp
1817 * is equivalent to
1818 * tstamp - cgrp->timestamp.
1819 *
1820 * Then, in perf_output_read(), the calculation would
1821 * work with no changes because:
1822 * - event is guaranteed scheduled in
1823 * - no scheduled out in between
1824 * - thus the timestamp would be the same
1825 *
1826 * But this is a bit hairy.
1827 *
1828 * So instead, we have an explicit cgroup call to remain
1829 * within the time time source all along. We believe it
1830 * is cleaner and simpler to understand.
1831 */
1832 if (is_cgroup_event(event))
1833 perf_cgroup_set_shadow_time(event, tstamp);
1834 else
1835 event->shadow_ctx_time = tstamp - ctx->timestamp;
1836}
1837
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001838#define MAX_INTERRUPTS (~0ULL)
1839
1840static void perf_log_throttle(struct perf_event *event, int enable);
1841
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001842static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001843event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001844 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001845 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001846{
Stephane Eranian41587552011-01-03 18:20:01 +02001847 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001848 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001849
Peter Zijlstra63342412014-05-05 11:49:16 +02001850 lockdep_assert_held(&ctx->lock);
1851
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001852 if (event->state <= PERF_EVENT_STATE_OFF)
1853 return 0;
1854
1855 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001856 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001857
1858 /*
1859 * Unthrottle events, since we scheduled we might have missed several
1860 * ticks already, also for a heavily scheduling task there is little
1861 * guarantee it'll get a tick in a timely manner.
1862 */
1863 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1864 perf_log_throttle(event, 1);
1865 event->hw.interrupts = 0;
1866 }
1867
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001868 /*
1869 * The new state must be visible before we turn it on in the hardware:
1870 */
1871 smp_wmb();
1872
Alexander Shishkin44377272013-12-16 14:17:36 +02001873 perf_pmu_disable(event->pmu);
1874
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001875 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001876 event->state = PERF_EVENT_STATE_INACTIVE;
1877 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001878 ret = -EAGAIN;
1879 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001880 }
1881
Stephane Eranian41587552011-01-03 18:20:01 +02001882 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001883
Stephane Eraniane5d13672011-02-14 11:20:01 +02001884 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001885
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001886 if (!is_software_event(event))
1887 cpuctx->active_oncpu++;
1888 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001889 if (event->attr.freq && event->attr.sample_freq)
1890 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001891
1892 if (event->attr.exclusive)
1893 cpuctx->exclusive = 1;
1894
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001895 if (is_orphaned_child(event))
1896 schedule_orphans_remove(ctx);
1897
Alexander Shishkin44377272013-12-16 14:17:36 +02001898out:
1899 perf_pmu_enable(event->pmu);
1900
1901 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001902}
1903
1904static int
1905group_sched_in(struct perf_event *group_event,
1906 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001907 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001908{
Lin Ming6bde9b62010-04-23 13:56:00 +08001909 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001910 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001911 u64 now = ctx->time;
1912 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001913
1914 if (group_event->state == PERF_EVENT_STATE_OFF)
1915 return 0;
1916
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001917 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001918
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001919 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001920 pmu->cancel_txn(pmu);
Stephane Eranian9e630202013-04-03 14:21:33 +02001921 perf_cpu_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001922 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001923 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001924
1925 /*
1926 * Schedule in siblings as one group (if any):
1927 */
1928 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001929 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001930 partial_group = event;
1931 goto group_error;
1932 }
1933 }
1934
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001935 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001936 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001937
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001938group_error:
1939 /*
1940 * Groups can be scheduled in as one unit only, so undo any
1941 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001942 * The events up to the failed event are scheduled out normally,
1943 * tstamp_stopped will be updated.
1944 *
1945 * The failed events and the remaining siblings need to have
1946 * their timings updated as if they had gone thru event_sched_in()
1947 * and event_sched_out(). This is required to get consistent timings
1948 * across the group. This also takes care of the case where the group
1949 * could never be scheduled by ensuring tstamp_stopped is set to mark
1950 * the time the event was actually stopped, such that time delta
1951 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001952 */
1953 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1954 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001955 simulate = true;
1956
1957 if (simulate) {
1958 event->tstamp_running += now - event->tstamp_stopped;
1959 event->tstamp_stopped = now;
1960 } else {
1961 event_sched_out(event, cpuctx, ctx);
1962 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001963 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001964 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001965
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001966 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001967
Stephane Eranian9e630202013-04-03 14:21:33 +02001968 perf_cpu_hrtimer_restart(cpuctx);
1969
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001970 return -EAGAIN;
1971}
1972
1973/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001974 * Work out whether we can put this event group on the CPU now.
1975 */
1976static int group_can_go_on(struct perf_event *event,
1977 struct perf_cpu_context *cpuctx,
1978 int can_add_hw)
1979{
1980 /*
1981 * Groups consisting entirely of software events can always go on.
1982 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001983 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001984 return 1;
1985 /*
1986 * If an exclusive group is already on, no other hardware
1987 * events can go on.
1988 */
1989 if (cpuctx->exclusive)
1990 return 0;
1991 /*
1992 * If this group is exclusive and there are already
1993 * events on the CPU, it can't go on.
1994 */
1995 if (event->attr.exclusive && cpuctx->active_oncpu)
1996 return 0;
1997 /*
1998 * Otherwise, try to add it if all previous groups were able
1999 * to go on.
2000 */
2001 return can_add_hw;
2002}
2003
2004static void add_event_to_ctx(struct perf_event *event,
2005 struct perf_event_context *ctx)
2006{
Stephane Eranian41587552011-01-03 18:20:01 +02002007 u64 tstamp = perf_event_time(event);
2008
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002009 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002010 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02002011 event->tstamp_enabled = tstamp;
2012 event->tstamp_running = tstamp;
2013 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002014}
2015
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002016static void task_ctx_sched_out(struct perf_event_context *ctx);
2017static void
2018ctx_sched_in(struct perf_event_context *ctx,
2019 struct perf_cpu_context *cpuctx,
2020 enum event_type_t event_type,
2021 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002022
Peter Zijlstradce58552011-04-09 21:17:46 +02002023static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2024 struct perf_event_context *ctx,
2025 struct task_struct *task)
2026{
2027 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2028 if (ctx)
2029 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2030 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2031 if (ctx)
2032 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2033}
2034
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002035/*
2036 * Cross CPU call to install and enable a performance event
2037 *
2038 * Must be called with ctx->mutex held
2039 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002040static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002041{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002042 struct perf_event *event = info;
2043 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002044 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002045 struct perf_event_context *task_ctx = cpuctx->task_ctx;
2046 struct task_struct *task = current;
2047
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002048 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002049 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002050
2051 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002052 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002053 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002054 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002055 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002056
2057 /*
2058 * If the context we're installing events in is not the
2059 * active task_ctx, flip them.
2060 */
2061 if (ctx->task && task_ctx != ctx) {
2062 if (task_ctx)
2063 raw_spin_unlock(&task_ctx->lock);
2064 raw_spin_lock(&ctx->lock);
2065 task_ctx = ctx;
2066 }
2067
2068 if (task_ctx) {
2069 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002070 task = task_ctx->task;
2071 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02002072
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002073 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002074
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002075 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002076 /*
2077 * update cgrp time only if current cgrp
2078 * matches event->cgrp. Must be done before
2079 * calling add_event_to_ctx()
2080 */
2081 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002082
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002083 add_event_to_ctx(event, ctx);
2084
2085 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002086 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002087 */
Peter Zijlstradce58552011-04-09 21:17:46 +02002088 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002089
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02002090 perf_pmu_enable(cpuctx->ctx.pmu);
2091 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002092
2093 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002094}
2095
2096/*
2097 * Attach a performance event to a context
2098 *
2099 * First we add the event to the list with the hardware enable bit
2100 * in event->hw_config cleared.
2101 *
2102 * If the event is attached to a task which is on a CPU we use a smp
2103 * call to enable it in the task context. The task might have been
2104 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002105 */
2106static void
2107perf_install_in_context(struct perf_event_context *ctx,
2108 struct perf_event *event,
2109 int cpu)
2110{
2111 struct task_struct *task = ctx->task;
2112
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002113 lockdep_assert_held(&ctx->mutex);
2114
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002115 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002116 if (event->cpu != -1)
2117 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002118
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002119 if (!task) {
2120 /*
2121 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002122 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002123 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002124 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002125 return;
2126 }
2127
2128retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002129 if (!task_function_call(task, __perf_install_in_context, event))
2130 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002131
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002132 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002133 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002134 * If we failed to find a running task, but find the context active now
2135 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002136 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002137 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002138 raw_spin_unlock_irq(&ctx->lock);
Cong Wang3577af72014-09-02 15:27:20 -07002139 /*
2140 * Reload the task pointer, it might have been changed by
2141 * a concurrent perf_event_context_sched_out().
2142 */
2143 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002144 goto retry;
2145 }
2146
2147 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002148 * Since the task isn't running, its safe to add the event, us holding
2149 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002150 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002151 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002152 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002153}
2154
2155/*
2156 * Put a event into inactive state and update time fields.
2157 * Enabling the leader of a group effectively enables all
2158 * the group members that aren't explicitly disabled, so we
2159 * have to update their ->tstamp_enabled also.
2160 * Note: this works for group members as well as group leaders
2161 * since the non-leader members' sibling_lists will be empty.
2162 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002163static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002164{
2165 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002166 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002167
2168 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002169 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002170 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002171 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2172 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002173 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002174}
2175
2176/*
2177 * Cross CPU call to enable a performance event
2178 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002179static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002180{
2181 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002182 struct perf_event_context *ctx = event->ctx;
2183 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002184 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002185 int err;
2186
Jiri Olsa06f41792013-07-09 17:44:11 +02002187 /*
2188 * There's a time window between 'ctx->is_active' check
2189 * in perf_event_enable function and this place having:
2190 * - IRQs on
2191 * - ctx->lock unlocked
2192 *
2193 * where the task could be killed and 'ctx' deactivated
2194 * by perf_event_exit_task.
2195 */
2196 if (!ctx->is_active)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002197 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002198
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002199 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002200 update_context_time(ctx);
2201
2202 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2203 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002204
2205 /*
2206 * set current task's cgroup time reference point
2207 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002208 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002209
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002210 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002211
Stephane Eraniane5d13672011-02-14 11:20:01 +02002212 if (!event_filter_match(event)) {
2213 if (is_cgroup_event(event))
2214 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002215 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002216 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002217
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002218 /*
2219 * If the event is in a group and isn't the group leader,
2220 * then don't put it on unless the group is on.
2221 */
2222 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2223 goto unlock;
2224
2225 if (!group_can_go_on(event, cpuctx, 1)) {
2226 err = -EEXIST;
2227 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002228 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002229 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002230 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01002231 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002232 }
2233
2234 if (err) {
2235 /*
2236 * If this event can't go on and it's part of a
2237 * group, then the whole group has to come off.
2238 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002239 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002240 group_sched_out(leader, cpuctx, ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002241 perf_cpu_hrtimer_restart(cpuctx);
2242 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002243 if (leader->attr.pinned) {
2244 update_group_times(leader);
2245 leader->state = PERF_EVENT_STATE_ERROR;
2246 }
2247 }
2248
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002249unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002250 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002251
2252 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002253}
2254
2255/*
2256 * Enable a event.
2257 *
2258 * If event->ctx is a cloned context, callers must make sure that
2259 * every task struct that event->ctx->task could possibly point to
2260 * remains valid. This condition is satisfied when called through
2261 * perf_event_for_each_child or perf_event_for_each as described
2262 * for perf_event_disable.
2263 */
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002264static void _perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002265{
2266 struct perf_event_context *ctx = event->ctx;
2267 struct task_struct *task = ctx->task;
2268
2269 if (!task) {
2270 /*
2271 * Enable the event on the cpu that it's on
2272 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002273 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002274 return;
2275 }
2276
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002277 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002278 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2279 goto out;
2280
2281 /*
2282 * If the event is in error state, clear that first.
2283 * That way, if we see the event in error state below, we
2284 * know that it has gone back into error state, as distinct
2285 * from the task having been scheduled away before the
2286 * cross-call arrived.
2287 */
2288 if (event->state == PERF_EVENT_STATE_ERROR)
2289 event->state = PERF_EVENT_STATE_OFF;
2290
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002291retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002292 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002293 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002294 goto out;
2295 }
2296
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002297 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002298
2299 if (!task_function_call(task, __perf_event_enable, event))
2300 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002301
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002302 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002303
2304 /*
2305 * If the context is active and the event is still off,
2306 * we need to retry the cross-call.
2307 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002308 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2309 /*
2310 * task could have been flipped by a concurrent
2311 * perf_event_context_sched_out()
2312 */
2313 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002314 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002315 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002316
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002317out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002318 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002319}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002320
2321/*
2322 * See perf_event_disable();
2323 */
2324void perf_event_enable(struct perf_event *event)
2325{
2326 struct perf_event_context *ctx;
2327
2328 ctx = perf_event_ctx_lock(event);
2329 _perf_event_enable(event);
2330 perf_event_ctx_unlock(event, ctx);
2331}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002332EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002333
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002334static int _perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002335{
2336 /*
2337 * not supported on inherited events
2338 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002339 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002340 return -EINVAL;
2341
2342 atomic_add(refresh, &event->event_limit);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002343 _perf_event_enable(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002344
2345 return 0;
2346}
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01002347
2348/*
2349 * See perf_event_disable()
2350 */
2351int perf_event_refresh(struct perf_event *event, int refresh)
2352{
2353 struct perf_event_context *ctx;
2354 int ret;
2355
2356 ctx = perf_event_ctx_lock(event);
2357 ret = _perf_event_refresh(event, refresh);
2358 perf_event_ctx_unlock(event, ctx);
2359
2360 return ret;
2361}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002362EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002363
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002364static void ctx_sched_out(struct perf_event_context *ctx,
2365 struct perf_cpu_context *cpuctx,
2366 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002367{
2368 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002369 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002370
Peter Zijlstradb24d332011-04-09 21:17:45 +02002371 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002372 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002373 return;
2374
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002375 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002376 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002377 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002378 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002379
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002380 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002381 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002382 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2383 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002384 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002385
Peter Zijlstradb24d332011-04-09 21:17:45 +02002386 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002387 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002388 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002389 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002390 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002391}
2392
2393/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002394 * Test whether two contexts are equivalent, i.e. whether they have both been
2395 * cloned from the same version of the same context.
2396 *
2397 * Equivalence is measured using a generation number in the context that is
2398 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2399 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002400 */
2401static int context_equiv(struct perf_event_context *ctx1,
2402 struct perf_event_context *ctx2)
2403{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002404 lockdep_assert_held(&ctx1->lock);
2405 lockdep_assert_held(&ctx2->lock);
2406
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002407 /* Pinning disables the swap optimization */
2408 if (ctx1->pin_count || ctx2->pin_count)
2409 return 0;
2410
2411 /* If ctx1 is the parent of ctx2 */
2412 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2413 return 1;
2414
2415 /* If ctx2 is the parent of ctx1 */
2416 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2417 return 1;
2418
2419 /*
2420 * If ctx1 and ctx2 have the same parent; we flatten the parent
2421 * hierarchy, see perf_event_init_context().
2422 */
2423 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2424 ctx1->parent_gen == ctx2->parent_gen)
2425 return 1;
2426
2427 /* Unmatched */
2428 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002429}
2430
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002431static void __perf_event_sync_stat(struct perf_event *event,
2432 struct perf_event *next_event)
2433{
2434 u64 value;
2435
2436 if (!event->attr.inherit_stat)
2437 return;
2438
2439 /*
2440 * Update the event value, we cannot use perf_event_read()
2441 * because we're in the middle of a context switch and have IRQs
2442 * disabled, which upsets smp_call_function_single(), however
2443 * we know the event must be on the current CPU, therefore we
2444 * don't need to use it.
2445 */
2446 switch (event->state) {
2447 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002448 event->pmu->read(event);
2449 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002450
2451 case PERF_EVENT_STATE_INACTIVE:
2452 update_event_times(event);
2453 break;
2454
2455 default:
2456 break;
2457 }
2458
2459 /*
2460 * In order to keep per-task stats reliable we need to flip the event
2461 * values when we flip the contexts.
2462 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002463 value = local64_read(&next_event->count);
2464 value = local64_xchg(&event->count, value);
2465 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002466
2467 swap(event->total_time_enabled, next_event->total_time_enabled);
2468 swap(event->total_time_running, next_event->total_time_running);
2469
2470 /*
2471 * Since we swizzled the values, update the user visible data too.
2472 */
2473 perf_event_update_userpage(event);
2474 perf_event_update_userpage(next_event);
2475}
2476
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002477static void perf_event_sync_stat(struct perf_event_context *ctx,
2478 struct perf_event_context *next_ctx)
2479{
2480 struct perf_event *event, *next_event;
2481
2482 if (!ctx->nr_stat)
2483 return;
2484
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002485 update_context_time(ctx);
2486
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002487 event = list_first_entry(&ctx->event_list,
2488 struct perf_event, event_entry);
2489
2490 next_event = list_first_entry(&next_ctx->event_list,
2491 struct perf_event, event_entry);
2492
2493 while (&event->event_entry != &ctx->event_list &&
2494 &next_event->event_entry != &next_ctx->event_list) {
2495
2496 __perf_event_sync_stat(event, next_event);
2497
2498 event = list_next_entry(event, event_entry);
2499 next_event = list_next_entry(next_event, event_entry);
2500 }
2501}
2502
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002503static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2504 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002505{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002506 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002507 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002508 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002509 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002510 int do_switch = 1;
2511
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002512 if (likely(!ctx))
2513 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002514
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002515 cpuctx = __get_cpu_context(ctx);
2516 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002517 return;
2518
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002519 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002520 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002521 if (!next_ctx)
2522 goto unlock;
2523
2524 parent = rcu_dereference(ctx->parent_ctx);
2525 next_parent = rcu_dereference(next_ctx->parent_ctx);
2526
2527 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002528 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002529 goto unlock;
2530
2531 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002532 /*
2533 * Looks like the two contexts are clones, so we might be
2534 * able to optimize the context switch. We lock both
2535 * contexts and check that they are clones under the
2536 * lock (including re-checking that neither has been
2537 * uncloned in the meantime). It doesn't matter which
2538 * order we take the locks because no other cpu could
2539 * be trying to lock both of these tasks.
2540 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002541 raw_spin_lock(&ctx->lock);
2542 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002543 if (context_equiv(ctx, next_ctx)) {
2544 /*
2545 * XXX do we need a memory barrier of sorts
2546 * wrt to rcu_dereference() of perf_event_ctxp
2547 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002548 task->perf_event_ctxp[ctxn] = next_ctx;
2549 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002550 ctx->task = next;
2551 next_ctx->task = task;
2552 do_switch = 0;
2553
2554 perf_event_sync_stat(ctx, next_ctx);
2555 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002556 raw_spin_unlock(&next_ctx->lock);
2557 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002558 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002559unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002560 rcu_read_unlock();
2561
2562 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002563 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002564 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002565 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002566 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002567 }
2568}
2569
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002570#define for_each_task_context_nr(ctxn) \
2571 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2572
2573/*
2574 * Called from scheduler to remove the events of the current task,
2575 * with interrupts disabled.
2576 *
2577 * We stop each event and update the event value in event->count.
2578 *
2579 * This does not protect us against NMI, but disable()
2580 * sets the disabled bit in the control field of event _before_
2581 * accessing the event control register. If a NMI hits, then it will
2582 * not restart the event.
2583 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002584void __perf_event_task_sched_out(struct task_struct *task,
2585 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002586{
2587 int ctxn;
2588
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002589 for_each_task_context_nr(ctxn)
2590 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002591
2592 /*
2593 * if cgroup events exist on this CPU, then we need
2594 * to check if we have to switch out PMU state.
2595 * cgroup event are system-wide mode only
2596 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002597 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002598 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002599}
2600
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002601static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002602{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002603 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002604
2605 if (!cpuctx->task_ctx)
2606 return;
2607
2608 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2609 return;
2610
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002611 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002612 cpuctx->task_ctx = NULL;
2613}
2614
2615/*
2616 * Called with IRQs disabled
2617 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002618static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2619 enum event_type_t event_type)
2620{
2621 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002622}
2623
2624static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002625ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002626 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002627{
2628 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002629
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002630 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2631 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002632 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002633 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002634 continue;
2635
Stephane Eraniane5d13672011-02-14 11:20:01 +02002636 /* may need to reset tstamp_enabled */
2637 if (is_cgroup_event(event))
2638 perf_cgroup_mark_enabled(event, ctx);
2639
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002640 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002641 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002642
2643 /*
2644 * If this pinned group hasn't been scheduled,
2645 * put it in error state.
2646 */
2647 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2648 update_group_times(event);
2649 event->state = PERF_EVENT_STATE_ERROR;
2650 }
2651 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002652}
2653
2654static void
2655ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002656 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002657{
2658 struct perf_event *event;
2659 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002660
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002661 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2662 /* Ignore events in OFF or ERROR state */
2663 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002664 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002665 /*
2666 * Listen to the 'cpu' scheduling filter constraint
2667 * of events:
2668 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002669 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002670 continue;
2671
Stephane Eraniane5d13672011-02-14 11:20:01 +02002672 /* may need to reset tstamp_enabled */
2673 if (is_cgroup_event(event))
2674 perf_cgroup_mark_enabled(event, ctx);
2675
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002676 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002677 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002678 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002679 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002680 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002681}
2682
2683static void
2684ctx_sched_in(struct perf_event_context *ctx,
2685 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002686 enum event_type_t event_type,
2687 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002688{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002689 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002690 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002691
Peter Zijlstradb24d332011-04-09 21:17:45 +02002692 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002693 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002694 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002695
Stephane Eraniane5d13672011-02-14 11:20:01 +02002696 now = perf_clock();
2697 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002698 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002699 /*
2700 * First go through the list and put on any pinned groups
2701 * in order to give them the best chance of going on.
2702 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002703 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002704 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002705
2706 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002707 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002708 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002709}
2710
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002711static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002712 enum event_type_t event_type,
2713 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002714{
2715 struct perf_event_context *ctx = &cpuctx->ctx;
2716
Stephane Eraniane5d13672011-02-14 11:20:01 +02002717 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002718}
2719
Stephane Eraniane5d13672011-02-14 11:20:01 +02002720static void perf_event_context_sched_in(struct perf_event_context *ctx,
2721 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002722{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002723 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002724
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002725 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002726 if (cpuctx->task_ctx == ctx)
2727 return;
2728
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002729 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002730 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002731 /*
2732 * We want to keep the following priority order:
2733 * cpu pinned (that don't need to move), task pinned,
2734 * cpu flexible, task flexible.
2735 */
2736 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2737
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002738 if (ctx->nr_events)
2739 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002740
Gleb Natapov86b47c22011-11-22 16:08:21 +02002741 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2742
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002743 perf_pmu_enable(ctx->pmu);
2744 perf_ctx_unlock(cpuctx, ctx);
2745
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002746 /*
2747 * Since these rotations are per-cpu, we need to ensure the
2748 * cpu-context we got scheduled on is actually rotating.
2749 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002750 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002751}
2752
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002753/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002754 * When sampling the branck stack in system-wide, it may be necessary
2755 * to flush the stack on context switch. This happens when the branch
2756 * stack does not tag its entries with the pid of the current task.
2757 * Otherwise it becomes impossible to associate a branch entry with a
2758 * task. This ambiguity is more likely to appear when the branch stack
2759 * supports priv level filtering and the user sets it to monitor only
2760 * at the user level (which could be a useful measurement in system-wide
2761 * mode). In that case, the risk is high of having a branch stack with
2762 * branch from multiple tasks. Flushing may mean dropping the existing
2763 * entries or stashing them somewhere in the PMU specific code layer.
2764 *
2765 * This function provides the context switch callback to the lower code
2766 * layer. It is invoked ONLY when there is at least one system-wide context
2767 * with at least one active event using taken branch sampling.
2768 */
2769static void perf_branch_stack_sched_in(struct task_struct *prev,
2770 struct task_struct *task)
2771{
2772 struct perf_cpu_context *cpuctx;
2773 struct pmu *pmu;
2774 unsigned long flags;
2775
2776 /* no need to flush branch stack if not changing task */
2777 if (prev == task)
2778 return;
2779
2780 local_irq_save(flags);
2781
2782 rcu_read_lock();
2783
2784 list_for_each_entry_rcu(pmu, &pmus, entry) {
2785 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2786
2787 /*
2788 * check if the context has at least one
2789 * event using PERF_SAMPLE_BRANCH_STACK
2790 */
2791 if (cpuctx->ctx.nr_branch_stack > 0
2792 && pmu->flush_branch_stack) {
2793
Stephane Eraniand010b332012-02-09 23:21:00 +01002794 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2795
2796 perf_pmu_disable(pmu);
2797
2798 pmu->flush_branch_stack();
2799
2800 perf_pmu_enable(pmu);
2801
2802 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2803 }
2804 }
2805
2806 rcu_read_unlock();
2807
2808 local_irq_restore(flags);
2809}
2810
2811/*
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002812 * Called from scheduler to add the events of the current task
2813 * with interrupts disabled.
2814 *
2815 * We restore the event value and then enable it.
2816 *
2817 * This does not protect us against NMI, but enable()
2818 * sets the enabled bit in the control field of event _before_
2819 * accessing the event control register. If a NMI hits, then it will
2820 * keep the event running.
2821 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002822void __perf_event_task_sched_in(struct task_struct *prev,
2823 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002824{
2825 struct perf_event_context *ctx;
2826 int ctxn;
2827
2828 for_each_task_context_nr(ctxn) {
2829 ctx = task->perf_event_ctxp[ctxn];
2830 if (likely(!ctx))
2831 continue;
2832
Stephane Eraniane5d13672011-02-14 11:20:01 +02002833 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002834 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002835 /*
2836 * if cgroup events exist on this CPU, then we need
2837 * to check if we have to switch in PMU state.
2838 * cgroup event are system-wide mode only
2839 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002840 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002841 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002842
2843 /* check for system-wide branch_stack events */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002844 if (atomic_read(this_cpu_ptr(&perf_branch_stack_events)))
Stephane Eraniand010b332012-02-09 23:21:00 +01002845 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002846}
2847
Peter Zijlstraabd50712010-01-26 18:50:16 +01002848static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2849{
2850 u64 frequency = event->attr.sample_freq;
2851 u64 sec = NSEC_PER_SEC;
2852 u64 divisor, dividend;
2853
2854 int count_fls, nsec_fls, frequency_fls, sec_fls;
2855
2856 count_fls = fls64(count);
2857 nsec_fls = fls64(nsec);
2858 frequency_fls = fls64(frequency);
2859 sec_fls = 30;
2860
2861 /*
2862 * We got @count in @nsec, with a target of sample_freq HZ
2863 * the target period becomes:
2864 *
2865 * @count * 10^9
2866 * period = -------------------
2867 * @nsec * sample_freq
2868 *
2869 */
2870
2871 /*
2872 * Reduce accuracy by one bit such that @a and @b converge
2873 * to a similar magnitude.
2874 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002875#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002876do { \
2877 if (a##_fls > b##_fls) { \
2878 a >>= 1; \
2879 a##_fls--; \
2880 } else { \
2881 b >>= 1; \
2882 b##_fls--; \
2883 } \
2884} while (0)
2885
2886 /*
2887 * Reduce accuracy until either term fits in a u64, then proceed with
2888 * the other, so that finally we can do a u64/u64 division.
2889 */
2890 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2891 REDUCE_FLS(nsec, frequency);
2892 REDUCE_FLS(sec, count);
2893 }
2894
2895 if (count_fls + sec_fls > 64) {
2896 divisor = nsec * frequency;
2897
2898 while (count_fls + sec_fls > 64) {
2899 REDUCE_FLS(count, sec);
2900 divisor >>= 1;
2901 }
2902
2903 dividend = count * sec;
2904 } else {
2905 dividend = count * sec;
2906
2907 while (nsec_fls + frequency_fls > 64) {
2908 REDUCE_FLS(nsec, frequency);
2909 dividend >>= 1;
2910 }
2911
2912 divisor = nsec * frequency;
2913 }
2914
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002915 if (!divisor)
2916 return dividend;
2917
Peter Zijlstraabd50712010-01-26 18:50:16 +01002918 return div64_u64(dividend, divisor);
2919}
2920
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002921static DEFINE_PER_CPU(int, perf_throttled_count);
2922static DEFINE_PER_CPU(u64, perf_throttled_seq);
2923
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002924static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002925{
2926 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002927 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002928 s64 delta;
2929
Peter Zijlstraabd50712010-01-26 18:50:16 +01002930 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002931
2932 delta = (s64)(period - hwc->sample_period);
2933 delta = (delta + 7) / 8; /* low pass filter */
2934
2935 sample_period = hwc->sample_period + delta;
2936
2937 if (!sample_period)
2938 sample_period = 1;
2939
2940 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002941
Peter Zijlstrae7850592010-05-21 14:43:08 +02002942 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002943 if (disable)
2944 event->pmu->stop(event, PERF_EF_UPDATE);
2945
Peter Zijlstrae7850592010-05-21 14:43:08 +02002946 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002947
2948 if (disable)
2949 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002950 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002951}
2952
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002953/*
2954 * combine freq adjustment with unthrottling to avoid two passes over the
2955 * events. At the same time, make sure, having freq events does not change
2956 * the rate of unthrottling as that would introduce bias.
2957 */
2958static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2959 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002960{
2961 struct perf_event *event;
2962 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002963 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002964 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002965
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002966 /*
2967 * only need to iterate over all events iff:
2968 * - context have events in frequency mode (needs freq adjust)
2969 * - there are events to unthrottle on this cpu
2970 */
2971 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002972 return;
2973
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002974 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002975 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002976
Paul Mackerras03541f82009-10-14 16:58:03 +11002977 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002978 if (event->state != PERF_EVENT_STATE_ACTIVE)
2979 continue;
2980
Stephane Eranian5632ab12011-01-03 18:20:01 +02002981 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002982 continue;
2983
Alexander Shishkin44377272013-12-16 14:17:36 +02002984 perf_pmu_disable(event->pmu);
2985
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002986 hwc = &event->hw;
2987
Jiri Olsaae23bff2013-08-24 16:45:54 +02002988 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002989 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002990 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002991 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002992 }
2993
2994 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02002995 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002996
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002997 /*
2998 * stop the event and update event->count
2999 */
3000 event->pmu->stop(event, PERF_EF_UPDATE);
3001
Peter Zijlstrae7850592010-05-21 14:43:08 +02003002 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01003003 delta = now - hwc->freq_count_stamp;
3004 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003005
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003006 /*
3007 * restart the event
3008 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003009 * we have stopped the event so tell that
3010 * to perf_adjust_period() to avoid stopping it
3011 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003012 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01003013 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003014 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003015
3016 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02003017 next:
3018 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003019 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003020
Stephane Eranianf39d47f2012-02-07 14:39:57 +01003021 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003022 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003023}
3024
3025/*
3026 * Round-robin a context's events:
3027 */
3028static void rotate_ctx(struct perf_event_context *ctx)
3029{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01003030 /*
3031 * Rotate the first entry last of non-pinned groups. Rotation might be
3032 * disabled by the inheritance code.
3033 */
3034 if (!ctx->rotate_disable)
3035 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003036}
3037
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003038/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003039 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
3040 * because they're strictly cpu affine and rotate_start is called with IRQs
3041 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003042 */
Stephane Eranian9e630202013-04-03 14:21:33 +02003043static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003044{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003045 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003046 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003047
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003048 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003049 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003050 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3051 rotate = 1;
3052 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003053
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003054 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003055 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003056 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003057 if (ctx->nr_events != ctx->nr_active)
3058 rotate = 1;
3059 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003060
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003061 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003062 goto done;
3063
Peter Zijlstrafacc4302011-04-09 21:17:42 +02003064 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02003065 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003066
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003067 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3068 if (ctx)
3069 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01003070
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003071 rotate_ctx(&cpuctx->ctx);
3072 if (ctx)
3073 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003074
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003075 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01003076
3077 perf_pmu_enable(cpuctx->ctx.pmu);
3078 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02003079done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003080 if (remove)
3081 list_del_init(&cpuctx->rotation_list);
Stephane Eranian9e630202013-04-03 14:21:33 +02003082
3083 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003084}
3085
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003086#ifdef CONFIG_NO_HZ_FULL
3087bool perf_event_can_stop_tick(void)
3088{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003089 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003090 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003091 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02003092 else
3093 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02003094}
3095#endif
3096
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003097void perf_event_task_tick(void)
3098{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05003099 struct list_head *head = this_cpu_ptr(&rotation_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003100 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003101 struct perf_event_context *ctx;
3102 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003103
3104 WARN_ON(!irqs_disabled());
3105
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003106 __this_cpu_inc(perf_throttled_seq);
3107 throttled = __this_cpu_xchg(perf_throttled_count, 0);
3108
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003109 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01003110 ctx = &cpuctx->ctx;
3111 perf_adjust_freq_unthr_context(ctx, throttled);
3112
3113 ctx = cpuctx->task_ctx;
3114 if (ctx)
3115 perf_adjust_freq_unthr_context(ctx, throttled);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02003116 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003117}
3118
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003119static int event_enable_on_exec(struct perf_event *event,
3120 struct perf_event_context *ctx)
3121{
3122 if (!event->attr.enable_on_exec)
3123 return 0;
3124
3125 event->attr.enable_on_exec = 0;
3126 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3127 return 0;
3128
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01003129 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003130
3131 return 1;
3132}
3133
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003134/*
3135 * Enable all of a task's events that have been marked enable-on-exec.
3136 * This expects task == current.
3137 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003138static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003139{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003140 struct perf_event_context *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003141 struct perf_event *event;
3142 unsigned long flags;
3143 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003144 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003145
3146 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003147 if (!ctx || !ctx->nr_events)
3148 goto out;
3149
Stephane Eraniane566b762011-04-06 02:54:54 +02003150 /*
3151 * We must ctxsw out cgroup events to avoid conflict
3152 * when invoking perf_task_event_sched_in() later on
3153 * in this function. Otherwise we end up trying to
3154 * ctxswin cgroup events which are already scheduled
3155 * in.
3156 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003157 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003158
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003159 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02003160 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003161
Peter Zijlstrab79387e2011-11-22 11:25:43 +01003162 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003163 ret = event_enable_on_exec(event, ctx);
3164 if (ret)
3165 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003166 }
3167
3168 /*
3169 * Unclone this context if we enabled any event.
3170 */
3171 if (enabled)
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003172 clone_ctx = unclone_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003173
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003174 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003175
Stephane Eraniane566b762011-04-06 02:54:54 +02003176 /*
3177 * Also calls ctxswin for cgroup events, if any:
3178 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003179 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003180out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003181 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003182
3183 if (clone_ctx)
3184 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003185}
3186
Peter Zijlstrae041e322014-05-21 17:32:19 +02003187void perf_event_exec(void)
3188{
3189 struct perf_event_context *ctx;
3190 int ctxn;
3191
3192 rcu_read_lock();
3193 for_each_task_context_nr(ctxn) {
3194 ctx = current->perf_event_ctxp[ctxn];
3195 if (!ctx)
3196 continue;
3197
3198 perf_event_enable_on_exec(ctx);
3199 }
3200 rcu_read_unlock();
3201}
3202
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003203/*
3204 * Cross CPU call to read the hardware event
3205 */
3206static void __perf_event_read(void *info)
3207{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003208 struct perf_event *event = info;
3209 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003210 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003211
3212 /*
3213 * If this is a task context, we need to check whether it is
3214 * the current task context of this cpu. If not it has been
3215 * scheduled out before the smp call arrived. In that case
3216 * event->count would have been updated to a recent sample
3217 * when the event was scheduled out.
3218 */
3219 if (ctx->task && cpuctx->task_ctx != ctx)
3220 return;
3221
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003222 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003223 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003224 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003225 update_cgrp_time_from_event(event);
3226 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003227 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003228 if (event->state == PERF_EVENT_STATE_ACTIVE)
3229 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003230 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003231}
3232
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003233static inline u64 perf_event_count(struct perf_event *event)
3234{
Peter Zijlstrae7850592010-05-21 14:43:08 +02003235 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003236}
3237
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003238static u64 perf_event_read(struct perf_event *event)
3239{
3240 /*
3241 * If event is enabled and currently active on a CPU, update the
3242 * value in the event structure:
3243 */
3244 if (event->state == PERF_EVENT_STATE_ACTIVE) {
3245 smp_call_function_single(event->oncpu,
3246 __perf_event_read, event, 1);
3247 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003248 struct perf_event_context *ctx = event->ctx;
3249 unsigned long flags;
3250
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003251 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003252 /*
3253 * may read while context is not active
3254 * (e.g., thread is blocked), in that case
3255 * we cannot update context time
3256 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003257 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003258 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003259 update_cgrp_time_from_event(event);
3260 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003261 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003262 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003263 }
3264
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003265 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003266}
3267
3268/*
3269 * Initialize the perf_event context in a task_struct:
3270 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003271static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003272{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003273 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003274 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003275 INIT_LIST_HEAD(&ctx->pinned_groups);
3276 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003277 INIT_LIST_HEAD(&ctx->event_list);
3278 atomic_set(&ctx->refcount, 1);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003279 INIT_DELAYED_WORK(&ctx->orphans_remove, orphans_remove_work);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003280}
3281
Peter Zijlstraeb184472010-09-07 15:55:13 +02003282static struct perf_event_context *
3283alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003284{
3285 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003286
3287 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3288 if (!ctx)
3289 return NULL;
3290
3291 __perf_event_init_context(ctx);
3292 if (task) {
3293 ctx->task = task;
3294 get_task_struct(task);
3295 }
3296 ctx->pmu = pmu;
3297
3298 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003299}
3300
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003301static struct task_struct *
3302find_lively_task_by_vpid(pid_t vpid)
3303{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003304 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003305 int err;
3306
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003307 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003308 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003309 task = current;
3310 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003311 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003312 if (task)
3313 get_task_struct(task);
3314 rcu_read_unlock();
3315
3316 if (!task)
3317 return ERR_PTR(-ESRCH);
3318
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003319 /* Reuse ptrace permission checks for now. */
3320 err = -EACCES;
3321 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3322 goto errout;
3323
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003324 return task;
3325errout:
3326 put_task_struct(task);
3327 return ERR_PTR(err);
3328
3329}
3330
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003331/*
3332 * Returns a matching context with refcount and pincount.
3333 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003334static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07003335find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003336{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003337 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003338 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003339 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003340 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003341
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003342 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003343 /* Must be root to operate on a CPU event: */
3344 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3345 return ERR_PTR(-EACCES);
3346
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003347 /*
3348 * We could be clever and allow to attach a event to an
3349 * offline CPU and activate it when the CPU comes up, but
3350 * that's for later.
3351 */
3352 if (!cpu_online(cpu))
3353 return ERR_PTR(-ENODEV);
3354
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003355 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003356 ctx = &cpuctx->ctx;
3357 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003358 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003359
3360 return ctx;
3361 }
3362
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003363 err = -EINVAL;
3364 ctxn = pmu->task_ctx_nr;
3365 if (ctxn < 0)
3366 goto errout;
3367
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003368retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003369 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003370 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003371 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003372 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003373 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003374
3375 if (clone_ctx)
3376 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003377 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003378 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003379 err = -ENOMEM;
3380 if (!ctx)
3381 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003382
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003383 err = 0;
3384 mutex_lock(&task->perf_event_mutex);
3385 /*
3386 * If it has already passed perf_event_exit_task().
3387 * we must see PF_EXITING, it takes this mutex too.
3388 */
3389 if (task->flags & PF_EXITING)
3390 err = -ESRCH;
3391 else if (task->perf_event_ctxp[ctxn])
3392 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003393 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003394 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003395 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003396 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003397 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003398 mutex_unlock(&task->perf_event_mutex);
3399
3400 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003401 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003402
3403 if (err == -EAGAIN)
3404 goto retry;
3405 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003406 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003407 }
3408
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003409 return ctx;
3410
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003411errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003412 return ERR_PTR(err);
3413}
3414
Li Zefan6fb29152009-10-15 11:21:42 +08003415static void perf_event_free_filter(struct perf_event *event);
3416
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003417static void free_event_rcu(struct rcu_head *head)
3418{
3419 struct perf_event *event;
3420
3421 event = container_of(head, struct perf_event, rcu_head);
3422 if (event->ns)
3423 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003424 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003425 kfree(event);
3426}
3427
Frederic Weisbecker76369132011-05-19 19:55:04 +02003428static void ring_buffer_put(struct ring_buffer *rb);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003429static void ring_buffer_attach(struct perf_event *event,
3430 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003431
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003432static void unaccount_event_cpu(struct perf_event *event, int cpu)
3433{
3434 if (event->parent)
3435 return;
3436
3437 if (has_branch_stack(event)) {
3438 if (!(event->attach_state & PERF_ATTACH_TASK))
3439 atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
3440 }
3441 if (is_cgroup_event(event))
3442 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3443}
3444
3445static void unaccount_event(struct perf_event *event)
3446{
3447 if (event->parent)
3448 return;
3449
3450 if (event->attach_state & PERF_ATTACH_TASK)
3451 static_key_slow_dec_deferred(&perf_sched_events);
3452 if (event->attr.mmap || event->attr.mmap_data)
3453 atomic_dec(&nr_mmap_events);
3454 if (event->attr.comm)
3455 atomic_dec(&nr_comm_events);
3456 if (event->attr.task)
3457 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003458 if (event->attr.freq)
3459 atomic_dec(&nr_freq_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003460 if (is_cgroup_event(event))
3461 static_key_slow_dec_deferred(&perf_sched_events);
3462 if (has_branch_stack(event))
3463 static_key_slow_dec_deferred(&perf_sched_events);
3464
3465 unaccount_event_cpu(event, event->cpu);
3466}
3467
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003468static void __free_event(struct perf_event *event)
3469{
3470 if (!event->parent) {
3471 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3472 put_callchain_buffers();
3473 }
3474
3475 if (event->destroy)
3476 event->destroy(event);
3477
3478 if (event->ctx)
3479 put_ctx(event->ctx);
3480
Yan, Zhengc464c762014-03-18 16:56:41 +08003481 if (event->pmu)
3482 module_put(event->pmu->module);
3483
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003484 call_rcu(&event->rcu_head, free_event_rcu);
3485}
Peter Zijlstra683ede42014-05-05 12:11:24 +02003486
3487static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003488{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003489 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003490
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003491 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003492
Frederic Weisbecker76369132011-05-19 19:55:04 +02003493 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003494 /*
3495 * Can happen when we close an event with re-directed output.
3496 *
3497 * Since we have a 0 refcount, perf_mmap_close() will skip
3498 * over us; possibly making our ring_buffer_put() the last.
3499 */
3500 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003501 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003502 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003503 }
3504
Stephane Eraniane5d13672011-02-14 11:20:01 +02003505 if (is_cgroup_event(event))
3506 perf_detach_cgroup(event);
3507
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003508 __free_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003509}
3510
Peter Zijlstra683ede42014-05-05 12:11:24 +02003511/*
3512 * Used to free events which have a known refcount of 1, such as in error paths
3513 * where the event isn't exposed yet and inherited events.
3514 */
3515static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003516{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003517 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3518 "unexpected event refcount: %ld; ptr=%p\n",
3519 atomic_long_read(&event->refcount), event)) {
3520 /* leak to avoid use-after-free */
3521 return;
3522 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003523
Peter Zijlstra683ede42014-05-05 12:11:24 +02003524 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003525}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003526
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003527/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003528 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003529 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003530static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003531{
Peter Zijlstra88821352010-11-09 19:01:43 +01003532 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003533
Peter Zijlstra88821352010-11-09 19:01:43 +01003534 rcu_read_lock();
3535 owner = ACCESS_ONCE(event->owner);
3536 /*
3537 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3538 * !owner it means the list deletion is complete and we can indeed
3539 * free this event, otherwise we need to serialize on
3540 * owner->perf_event_mutex.
3541 */
3542 smp_read_barrier_depends();
3543 if (owner) {
3544 /*
3545 * Since delayed_put_task_struct() also drops the last
3546 * task reference we can safely take a new reference
3547 * while holding the rcu_read_lock().
3548 */
3549 get_task_struct(owner);
3550 }
3551 rcu_read_unlock();
3552
3553 if (owner) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003554 /*
3555 * If we're here through perf_event_exit_task() we're already
3556 * holding ctx->mutex which would be an inversion wrt. the
3557 * normal lock order.
3558 *
3559 * However we can safely take this lock because its the child
3560 * ctx->mutex.
3561 */
3562 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
3563
Peter Zijlstra88821352010-11-09 19:01:43 +01003564 /*
3565 * We have to re-check the event->owner field, if it is cleared
3566 * we raced with perf_event_exit_task(), acquiring the mutex
3567 * ensured they're done, and we can proceed with freeing the
3568 * event.
3569 */
3570 if (event->owner)
3571 list_del_init(&event->owner_entry);
3572 mutex_unlock(&owner->perf_event_mutex);
3573 put_task_struct(owner);
3574 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003575}
3576
3577/*
3578 * Called when the last reference to the file is gone.
3579 */
3580static void put_event(struct perf_event *event)
3581{
Peter Zijlstraa83fe282015-01-29 14:44:34 +01003582 struct perf_event_context *ctx;
Jiri Olsaf8697762014-08-01 14:33:01 +02003583
3584 if (!atomic_long_dec_and_test(&event->refcount))
3585 return;
3586
3587 if (!is_kernel_event(event))
3588 perf_remove_from_owner(event);
Peter Zijlstra88821352010-11-09 19:01:43 +01003589
Peter Zijlstra683ede42014-05-05 12:11:24 +02003590 /*
3591 * There are two ways this annotation is useful:
3592 *
3593 * 1) there is a lock recursion from perf_event_exit_task
3594 * see the comment there.
3595 *
3596 * 2) there is a lock-inversion with mmap_sem through
3597 * perf_event_read_group(), which takes faults while
3598 * holding ctx->mutex, however this is called after
3599 * the last filedesc died, so there is no possibility
3600 * to trigger the AB-BA case.
3601 */
Peter Zijlstraa83fe282015-01-29 14:44:34 +01003602 ctx = perf_event_ctx_lock_nested(event, SINGLE_DEPTH_NESTING);
3603 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstra683ede42014-05-05 12:11:24 +02003604 perf_remove_from_context(event, true);
3605 mutex_unlock(&ctx->mutex);
3606
3607 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003608}
3609
Peter Zijlstra683ede42014-05-05 12:11:24 +02003610int perf_event_release_kernel(struct perf_event *event)
3611{
3612 put_event(event);
3613 return 0;
3614}
3615EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3616
Al Viroa6fa9412012-08-20 14:59:25 +01003617static int perf_release(struct inode *inode, struct file *file)
3618{
3619 put_event(file->private_data);
3620 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003621}
3622
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003623/*
3624 * Remove all orphanes events from the context.
3625 */
3626static void orphans_remove_work(struct work_struct *work)
3627{
3628 struct perf_event_context *ctx;
3629 struct perf_event *event, *tmp;
3630
3631 ctx = container_of(work, struct perf_event_context,
3632 orphans_remove.work);
3633
3634 mutex_lock(&ctx->mutex);
3635 list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) {
3636 struct perf_event *parent_event = event->parent;
3637
3638 if (!is_orphaned_child(event))
3639 continue;
3640
3641 perf_remove_from_context(event, true);
3642
3643 mutex_lock(&parent_event->child_mutex);
3644 list_del_init(&event->child_list);
3645 mutex_unlock(&parent_event->child_mutex);
3646
3647 free_event(event);
3648 put_event(parent_event);
3649 }
3650
3651 raw_spin_lock_irq(&ctx->lock);
3652 ctx->orphans_remove_sched = false;
3653 raw_spin_unlock_irq(&ctx->lock);
3654 mutex_unlock(&ctx->mutex);
3655
3656 put_ctx(ctx);
3657}
3658
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003659u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003660{
3661 struct perf_event *child;
3662 u64 total = 0;
3663
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003664 *enabled = 0;
3665 *running = 0;
3666
Peter Zijlstra6f105812009-11-20 22:19:56 +01003667 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003668 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003669 *enabled += event->total_time_enabled +
3670 atomic64_read(&event->child_total_time_enabled);
3671 *running += event->total_time_running +
3672 atomic64_read(&event->child_total_time_running);
3673
3674 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003675 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003676 *enabled += child->total_time_enabled;
3677 *running += child->total_time_running;
3678 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003679 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003680
3681 return total;
3682}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003683EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003684
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003685static int perf_event_read_group(struct perf_event *event,
3686 u64 read_format, char __user *buf)
3687{
3688 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003689 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003690 int n = 0, size = 0, ret;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003691 u64 count, enabled, running;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003692 u64 values[5];
Peter Zijlstraabf48682009-11-20 22:19:49 +01003693
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003694 lockdep_assert_held(&ctx->mutex);
3695
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003696 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003697
3698 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003699 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3700 values[n++] = enabled;
3701 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3702 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003703 values[n++] = count;
3704 if (read_format & PERF_FORMAT_ID)
3705 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003706
3707 size = n * sizeof(u64);
3708
3709 if (copy_to_user(buf, values, size))
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003710 return -EFAULT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003711
Peter Zijlstra6f105812009-11-20 22:19:56 +01003712 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003713
3714 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003715 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003716
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003717 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003718 if (read_format & PERF_FORMAT_ID)
3719 values[n++] = primary_event_id(sub);
3720
3721 size = n * sizeof(u64);
3722
Stephane Eranian184d3da2009-11-23 21:40:49 -08003723 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003724 return -EFAULT;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003725 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003726
3727 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003728 }
3729
Peter Zijlstraabf48682009-11-20 22:19:49 +01003730 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003731}
3732
3733static int perf_event_read_one(struct perf_event *event,
3734 u64 read_format, char __user *buf)
3735{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003736 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003737 u64 values[4];
3738 int n = 0;
3739
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003740 values[n++] = perf_event_read_value(event, &enabled, &running);
3741 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3742 values[n++] = enabled;
3743 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3744 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003745 if (read_format & PERF_FORMAT_ID)
3746 values[n++] = primary_event_id(event);
3747
3748 if (copy_to_user(buf, values, n * sizeof(u64)))
3749 return -EFAULT;
3750
3751 return n * sizeof(u64);
3752}
3753
Jiri Olsadc633982014-09-12 13:18:26 +02003754static bool is_event_hup(struct perf_event *event)
3755{
3756 bool no_children;
3757
3758 if (event->state != PERF_EVENT_STATE_EXIT)
3759 return false;
3760
3761 mutex_lock(&event->child_mutex);
3762 no_children = list_empty(&event->child_list);
3763 mutex_unlock(&event->child_mutex);
3764 return no_children;
3765}
3766
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003767/*
3768 * Read the performance event - simple non blocking version for now
3769 */
3770static ssize_t
3771perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3772{
3773 u64 read_format = event->attr.read_format;
3774 int ret;
3775
3776 /*
3777 * Return end-of-file for a read on a event that is in
3778 * error state (i.e. because it was pinned but it couldn't be
3779 * scheduled on to the CPU at some point).
3780 */
3781 if (event->state == PERF_EVENT_STATE_ERROR)
3782 return 0;
3783
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003784 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003785 return -ENOSPC;
3786
3787 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003788 if (read_format & PERF_FORMAT_GROUP)
3789 ret = perf_event_read_group(event, read_format, buf);
3790 else
3791 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003792
3793 return ret;
3794}
3795
3796static ssize_t
3797perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3798{
3799 struct perf_event *event = file->private_data;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003800 struct perf_event_context *ctx;
3801 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003802
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003803 ctx = perf_event_ctx_lock(event);
3804 ret = perf_read_hw(event, buf, count);
3805 perf_event_ctx_unlock(event, ctx);
3806
3807 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003808}
3809
3810static unsigned int perf_poll(struct file *file, poll_table *wait)
3811{
3812 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003813 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02003814 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003815
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02003816 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04003817
Jiri Olsadc633982014-09-12 13:18:26 +02003818 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04003819 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003820
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003821 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003822 * Pin the event->rb by taking event->mmap_mutex; otherwise
3823 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003824 */
3825 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003826 rb = event->rb;
3827 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003828 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003829 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003830 return events;
3831}
3832
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003833static void _perf_event_reset(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003834{
3835 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003836 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003837 perf_event_update_userpage(event);
3838}
3839
3840/*
3841 * Holding the top-level event's child_mutex means that any
3842 * descendant process that has inherited this event will block
3843 * in sync_child_event if it goes to exit, thus satisfying the
3844 * task existence requirements of perf_event_enable/disable.
3845 */
3846static void perf_event_for_each_child(struct perf_event *event,
3847 void (*func)(struct perf_event *))
3848{
3849 struct perf_event *child;
3850
3851 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003852
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003853 mutex_lock(&event->child_mutex);
3854 func(event);
3855 list_for_each_entry(child, &event->child_list, child_list)
3856 func(child);
3857 mutex_unlock(&event->child_mutex);
3858}
3859
3860static void perf_event_for_each(struct perf_event *event,
3861 void (*func)(struct perf_event *))
3862{
3863 struct perf_event_context *ctx = event->ctx;
3864 struct perf_event *sibling;
3865
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003866 lockdep_assert_held(&ctx->mutex);
3867
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003868 event = event->group_leader;
3869
3870 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003871 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003872 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003873}
3874
3875static int perf_event_period(struct perf_event *event, u64 __user *arg)
3876{
3877 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrabad71922013-11-27 13:54:38 +00003878 int ret = 0, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003879 u64 value;
3880
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003881 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003882 return -EINVAL;
3883
John Blackwoodad0cf342010-09-28 18:03:11 -04003884 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003885 return -EFAULT;
3886
3887 if (!value)
3888 return -EINVAL;
3889
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003890 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003891 if (event->attr.freq) {
3892 if (value > sysctl_perf_event_sample_rate) {
3893 ret = -EINVAL;
3894 goto unlock;
3895 }
3896
3897 event->attr.sample_freq = value;
3898 } else {
3899 event->attr.sample_period = value;
3900 event->hw.sample_period = value;
3901 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00003902
3903 active = (event->state == PERF_EVENT_STATE_ACTIVE);
3904 if (active) {
3905 perf_pmu_disable(ctx->pmu);
3906 event->pmu->stop(event, PERF_EF_UPDATE);
3907 }
3908
3909 local64_set(&event->hw.period_left, 0);
3910
3911 if (active) {
3912 event->pmu->start(event, PERF_EF_RELOAD);
3913 perf_pmu_enable(ctx->pmu);
3914 }
3915
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003916unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003917 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003918
3919 return ret;
3920}
3921
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003922static const struct file_operations perf_fops;
3923
Al Viro2903ff02012-08-28 12:52:22 -04003924static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003925{
Al Viro2903ff02012-08-28 12:52:22 -04003926 struct fd f = fdget(fd);
3927 if (!f.file)
3928 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003929
Al Viro2903ff02012-08-28 12:52:22 -04003930 if (f.file->f_op != &perf_fops) {
3931 fdput(f);
3932 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003933 }
Al Viro2903ff02012-08-28 12:52:22 -04003934 *p = f;
3935 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003936}
3937
3938static int perf_event_set_output(struct perf_event *event,
3939 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003940static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003941
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003942static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003943{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003944 void (*func)(struct perf_event *);
3945 u32 flags = arg;
3946
3947 switch (cmd) {
3948 case PERF_EVENT_IOC_ENABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003949 func = _perf_event_enable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003950 break;
3951 case PERF_EVENT_IOC_DISABLE:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003952 func = _perf_event_disable;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003953 break;
3954 case PERF_EVENT_IOC_RESET:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003955 func = _perf_event_reset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003956 break;
3957
3958 case PERF_EVENT_IOC_REFRESH:
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01003959 return _perf_event_refresh(event, arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003960
3961 case PERF_EVENT_IOC_PERIOD:
3962 return perf_event_period(event, (u64 __user *)arg);
3963
Jiri Olsacf4957f2012-10-24 13:37:58 +02003964 case PERF_EVENT_IOC_ID:
3965 {
3966 u64 id = primary_event_id(event);
3967
3968 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
3969 return -EFAULT;
3970 return 0;
3971 }
3972
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003973 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003974 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003975 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003976 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003977 struct perf_event *output_event;
3978 struct fd output;
3979 ret = perf_fget_light(arg, &output);
3980 if (ret)
3981 return ret;
3982 output_event = output.file->private_data;
3983 ret = perf_event_set_output(event, output_event);
3984 fdput(output);
3985 } else {
3986 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003987 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003988 return ret;
3989 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003990
Li Zefan6fb29152009-10-15 11:21:42 +08003991 case PERF_EVENT_IOC_SET_FILTER:
3992 return perf_event_set_filter(event, (void __user *)arg);
3993
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003994 default:
3995 return -ENOTTY;
3996 }
3997
3998 if (flags & PERF_IOC_FLAG_GROUP)
3999 perf_event_for_each(event, func);
4000 else
4001 perf_event_for_each_child(event, func);
4002
4003 return 0;
4004}
4005
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004006static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4007{
4008 struct perf_event *event = file->private_data;
4009 struct perf_event_context *ctx;
4010 long ret;
4011
4012 ctx = perf_event_ctx_lock(event);
4013 ret = _perf_ioctl(event, cmd, arg);
4014 perf_event_ctx_unlock(event, ctx);
4015
4016 return ret;
4017}
4018
Pawel Mollb3f20782014-06-13 16:03:32 +01004019#ifdef CONFIG_COMPAT
4020static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4021 unsigned long arg)
4022{
4023 switch (_IOC_NR(cmd)) {
4024 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4025 case _IOC_NR(PERF_EVENT_IOC_ID):
4026 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4027 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4028 cmd &= ~IOCSIZE_MASK;
4029 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4030 }
4031 break;
4032 }
4033 return perf_ioctl(file, cmd, arg);
4034}
4035#else
4036# define perf_compat_ioctl NULL
4037#endif
4038
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004039int perf_event_task_enable(void)
4040{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004041 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004042 struct perf_event *event;
4043
4044 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004045 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4046 ctx = perf_event_ctx_lock(event);
4047 perf_event_for_each_child(event, _perf_event_enable);
4048 perf_event_ctx_unlock(event, ctx);
4049 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004050 mutex_unlock(&current->perf_event_mutex);
4051
4052 return 0;
4053}
4054
4055int perf_event_task_disable(void)
4056{
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004057 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004058 struct perf_event *event;
4059
4060 mutex_lock(&current->perf_event_mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01004061 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4062 ctx = perf_event_ctx_lock(event);
4063 perf_event_for_each_child(event, _perf_event_disable);
4064 perf_event_ctx_unlock(event, ctx);
4065 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004066 mutex_unlock(&current->perf_event_mutex);
4067
4068 return 0;
4069}
4070
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004071static int perf_event_index(struct perf_event *event)
4072{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004073 if (event->hw.state & PERF_HES_STOPPED)
4074 return 0;
4075
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004076 if (event->state != PERF_EVENT_STATE_ACTIVE)
4077 return 0;
4078
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01004079 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004080}
4081
Eric B Munsonc4794292011-06-23 16:34:38 -04004082static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004083 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04004084 u64 *enabled,
4085 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04004086{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004087 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04004088
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004089 *now = perf_clock();
4090 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04004091 *enabled = ctx_time - event->tstamp_enabled;
4092 *running = ctx_time - event->tstamp_running;
4093}
4094
Peter Zijlstrafa731582013-09-19 10:16:42 +02004095static void perf_event_init_userpage(struct perf_event *event)
4096{
4097 struct perf_event_mmap_page *userpg;
4098 struct ring_buffer *rb;
4099
4100 rcu_read_lock();
4101 rb = rcu_dereference(event->rb);
4102 if (!rb)
4103 goto unlock;
4104
4105 userpg = rb->user_page;
4106
4107 /* Allow new userspace to detect that bit 0 is deprecated */
4108 userpg->cap_bit0_is_deprecated = 1;
4109 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
4110
4111unlock:
4112 rcu_read_unlock();
4113}
4114
Peter Zijlstrac7206202012-03-22 17:26:36 +01004115void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004116{
4117}
4118
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004119/*
4120 * Callers need to ensure there can be no nesting of this function, otherwise
4121 * the seqlock logic goes bad. We can not serialize this because the arch
4122 * code calls this from NMI context.
4123 */
4124void perf_event_update_userpage(struct perf_event *event)
4125{
4126 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004127 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004128 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004129
4130 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02004131 rb = rcu_dereference(event->rb);
4132 if (!rb)
4133 goto unlock;
4134
Eric B Munson0d641202011-06-24 12:26:26 -04004135 /*
4136 * compute total_time_enabled, total_time_running
4137 * based on snapshot values taken when the event
4138 * was last scheduled in.
4139 *
4140 * we cannot simply called update_context_time()
4141 * because of locking issue as we can be called in
4142 * NMI context
4143 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004144 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004145
Frederic Weisbecker76369132011-05-19 19:55:04 +02004146 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004147 /*
4148 * Disable preemption so as to not let the corresponding user-space
4149 * spin too long if we get preempted.
4150 */
4151 preempt_disable();
4152 ++userpg->lock;
4153 barrier();
4154 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004155 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01004156 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02004157 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004158
Eric B Munson0d641202011-06-24 12:26:26 -04004159 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004160 atomic64_read(&event->child_total_time_enabled);
4161
Eric B Munson0d641202011-06-24 12:26:26 -04004162 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004163 atomic64_read(&event->child_total_time_running);
4164
Peter Zijlstrac7206202012-03-22 17:26:36 +01004165 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004166
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004167 barrier();
4168 ++userpg->lock;
4169 preempt_enable();
4170unlock:
4171 rcu_read_unlock();
4172}
4173
Peter Zijlstra906010b2009-09-21 16:08:49 +02004174static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4175{
4176 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004177 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004178 int ret = VM_FAULT_SIGBUS;
4179
4180 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4181 if (vmf->pgoff == 0)
4182 ret = 0;
4183 return ret;
4184 }
4185
4186 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004187 rb = rcu_dereference(event->rb);
4188 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004189 goto unlock;
4190
4191 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4192 goto unlock;
4193
Frederic Weisbecker76369132011-05-19 19:55:04 +02004194 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004195 if (!vmf->page)
4196 goto unlock;
4197
4198 get_page(vmf->page);
4199 vmf->page->mapping = vma->vm_file->f_mapping;
4200 vmf->page->index = vmf->pgoff;
4201
4202 ret = 0;
4203unlock:
4204 rcu_read_unlock();
4205
4206 return ret;
4207}
4208
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004209static void ring_buffer_attach(struct perf_event *event,
4210 struct ring_buffer *rb)
4211{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004212 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004213 unsigned long flags;
4214
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004215 if (event->rb) {
4216 /*
4217 * Should be impossible, we set this when removing
4218 * event->rb_entry and wait/clear when adding event->rb_entry.
4219 */
4220 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004221
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004222 old_rb = event->rb;
4223 event->rcu_batches = get_state_synchronize_rcu();
4224 event->rcu_pending = 1;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004225
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004226 spin_lock_irqsave(&old_rb->event_lock, flags);
4227 list_del_rcu(&event->rb_entry);
4228 spin_unlock_irqrestore(&old_rb->event_lock, flags);
4229 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004230
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004231 if (event->rcu_pending && rb) {
4232 cond_synchronize_rcu(event->rcu_batches);
4233 event->rcu_pending = 0;
4234 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004235
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004236 if (rb) {
4237 spin_lock_irqsave(&rb->event_lock, flags);
4238 list_add_rcu(&event->rb_entry, &rb->event_list);
4239 spin_unlock_irqrestore(&rb->event_lock, flags);
4240 }
4241
4242 rcu_assign_pointer(event->rb, rb);
4243
4244 if (old_rb) {
4245 ring_buffer_put(old_rb);
4246 /*
4247 * Since we detached before setting the new rb, so that we
4248 * could attach the new rb, we could have missed a wakeup.
4249 * Provide it now.
4250 */
4251 wake_up_all(&event->waitq);
4252 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004253}
4254
4255static void ring_buffer_wakeup(struct perf_event *event)
4256{
4257 struct ring_buffer *rb;
4258
4259 rcu_read_lock();
4260 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004261 if (rb) {
4262 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4263 wake_up_all(&event->waitq);
4264 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004265 rcu_read_unlock();
4266}
4267
Frederic Weisbecker76369132011-05-19 19:55:04 +02004268static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004269{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004270 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004271
Frederic Weisbecker76369132011-05-19 19:55:04 +02004272 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
4273 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004274}
4275
Frederic Weisbecker76369132011-05-19 19:55:04 +02004276static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004277{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004278 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004279
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004280 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004281 rb = rcu_dereference(event->rb);
4282 if (rb) {
4283 if (!atomic_inc_not_zero(&rb->refcount))
4284 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004285 }
4286 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004287
Frederic Weisbecker76369132011-05-19 19:55:04 +02004288 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004289}
4290
Frederic Weisbecker76369132011-05-19 19:55:04 +02004291static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004292{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004293 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004294 return;
4295
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004296 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004297
Frederic Weisbecker76369132011-05-19 19:55:04 +02004298 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004299}
4300
4301static void perf_mmap_open(struct vm_area_struct *vma)
4302{
4303 struct perf_event *event = vma->vm_file->private_data;
4304
4305 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004306 atomic_inc(&event->rb->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004307}
4308
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004309/*
4310 * A buffer can be mmap()ed multiple times; either directly through the same
4311 * event, or through other events by use of perf_event_set_output().
4312 *
4313 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4314 * the buffer here, where we still have a VM context. This means we need
4315 * to detach all events redirecting to us.
4316 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004317static void perf_mmap_close(struct vm_area_struct *vma)
4318{
4319 struct perf_event *event = vma->vm_file->private_data;
4320
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004321 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004322 struct user_struct *mmap_user = rb->mmap_user;
4323 int mmap_locked = rb->mmap_locked;
4324 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004325
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004326 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004327
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004328 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004329 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004330
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004331 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004332 mutex_unlock(&event->mmap_mutex);
4333
4334 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004335 if (atomic_read(&rb->mmap_count))
4336 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004337
4338 /*
4339 * No other mmap()s, detach from all other events that might redirect
4340 * into the now unreachable buffer. Somewhat complicated by the
4341 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4342 */
4343again:
4344 rcu_read_lock();
4345 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4346 if (!atomic_long_inc_not_zero(&event->refcount)) {
4347 /*
4348 * This event is en-route to free_event() which will
4349 * detach it and remove it from the list.
4350 */
4351 continue;
4352 }
4353 rcu_read_unlock();
4354
4355 mutex_lock(&event->mmap_mutex);
4356 /*
4357 * Check we didn't race with perf_event_set_output() which can
4358 * swizzle the rb from under us while we were waiting to
4359 * acquire mmap_mutex.
4360 *
4361 * If we find a different rb; ignore this event, a next
4362 * iteration will no longer find it on the list. We have to
4363 * still restart the iteration to make sure we're not now
4364 * iterating the wrong list.
4365 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004366 if (event->rb == rb)
4367 ring_buffer_attach(event, NULL);
4368
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004369 mutex_unlock(&event->mmap_mutex);
4370 put_event(event);
4371
4372 /*
4373 * Restart the iteration; either we're on the wrong list or
4374 * destroyed its integrity by doing a deletion.
4375 */
4376 goto again;
4377 }
4378 rcu_read_unlock();
4379
4380 /*
4381 * It could be there's still a few 0-ref events on the list; they'll
4382 * get cleaned up by free_event() -- they'll also still have their
4383 * ref on the rb and will free it whenever they are done with it.
4384 *
4385 * Aside from that, this buffer is 'fully' detached and unmapped,
4386 * undo the VM accounting.
4387 */
4388
4389 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4390 vma->vm_mm->pinned_vm -= mmap_locked;
4391 free_uid(mmap_user);
4392
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004393out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004394 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004395}
4396
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004397static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004398 .open = perf_mmap_open,
4399 .close = perf_mmap_close,
4400 .fault = perf_mmap_fault,
4401 .page_mkwrite = perf_mmap_fault,
4402};
4403
4404static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4405{
4406 struct perf_event *event = file->private_data;
4407 unsigned long user_locked, user_lock_limit;
4408 struct user_struct *user = current_user();
4409 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004410 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004411 unsigned long vma_size;
4412 unsigned long nr_pages;
4413 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004414 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004415
Peter Zijlstrac7920612010-05-18 10:33:24 +02004416 /*
4417 * Don't allow mmap() of inherited per-task counters. This would
4418 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004419 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004420 */
4421 if (event->cpu == -1 && event->attr.inherit)
4422 return -EINVAL;
4423
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004424 if (!(vma->vm_flags & VM_SHARED))
4425 return -EINVAL;
4426
4427 vma_size = vma->vm_end - vma->vm_start;
4428 nr_pages = (vma_size / PAGE_SIZE) - 1;
4429
4430 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004431 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004432 * can do bitmasks instead of modulo.
4433 */
4434 if (nr_pages != 0 && !is_power_of_2(nr_pages))
4435 return -EINVAL;
4436
4437 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4438 return -EINVAL;
4439
4440 if (vma->vm_pgoff != 0)
4441 return -EINVAL;
4442
4443 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004444again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004445 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004446 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004447 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004448 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004449 goto unlock;
4450 }
4451
4452 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4453 /*
4454 * Raced against perf_mmap_close() through
4455 * perf_event_set_output(). Try again, hope for better
4456 * luck.
4457 */
4458 mutex_unlock(&event->mmap_mutex);
4459 goto again;
4460 }
4461
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004462 goto unlock;
4463 }
4464
4465 user_extra = nr_pages + 1;
4466 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4467
4468 /*
4469 * Increase the limit linearly with more CPUs:
4470 */
4471 user_lock_limit *= num_online_cpus();
4472
4473 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4474
4475 extra = 0;
4476 if (user_locked > user_lock_limit)
4477 extra = user_locked - user_lock_limit;
4478
Jiri Slaby78d7d402010-03-05 13:42:54 -08004479 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004480 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004481 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004482
4483 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4484 !capable(CAP_IPC_LOCK)) {
4485 ret = -EPERM;
4486 goto unlock;
4487 }
4488
Frederic Weisbecker76369132011-05-19 19:55:04 +02004489 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004490
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004491 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004492 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004493
Vince Weaver4ec83632011-06-01 15:15:36 -04004494 rb = rb_alloc(nr_pages,
4495 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4496 event->cpu, flags);
4497
Frederic Weisbecker76369132011-05-19 19:55:04 +02004498 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004499 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004500 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004501 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004502
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004503 atomic_set(&rb->mmap_count, 1);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004504 rb->mmap_locked = extra;
4505 rb->mmap_user = get_current_user();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004506
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004507 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004508 vma->vm_mm->pinned_vm += extra;
4509
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004510 ring_buffer_attach(event, rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004511
Peter Zijlstrafa731582013-09-19 10:16:42 +02004512 perf_event_init_userpage(event);
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01004513 perf_event_update_userpage(event);
4514
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004515unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004516 if (!ret)
4517 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004518 mutex_unlock(&event->mmap_mutex);
4519
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004520 /*
4521 * Since pinned accounting is per vm we cannot allow fork() to copy our
4522 * vma.
4523 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004524 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004525 vma->vm_ops = &perf_mmap_vmops;
4526
4527 return ret;
4528}
4529
4530static int perf_fasync(int fd, struct file *filp, int on)
4531{
Al Viro496ad9a2013-01-23 17:07:38 -05004532 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004533 struct perf_event *event = filp->private_data;
4534 int retval;
4535
4536 mutex_lock(&inode->i_mutex);
4537 retval = fasync_helper(fd, filp, on, &event->fasync);
4538 mutex_unlock(&inode->i_mutex);
4539
4540 if (retval < 0)
4541 return retval;
4542
4543 return 0;
4544}
4545
4546static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004547 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004548 .release = perf_release,
4549 .read = perf_read,
4550 .poll = perf_poll,
4551 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01004552 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004553 .mmap = perf_mmap,
4554 .fasync = perf_fasync,
4555};
4556
4557/*
4558 * Perf event wakeup
4559 *
4560 * If there's data, ensure we set the poll() state and publish everything
4561 * to user-space before waking everybody up.
4562 */
4563
4564void perf_event_wakeup(struct perf_event *event)
4565{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004566 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004567
4568 if (event->pending_kill) {
4569 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4570 event->pending_kill = 0;
4571 }
4572}
4573
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004574static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004575{
4576 struct perf_event *event = container_of(entry,
4577 struct perf_event, pending);
4578
4579 if (event->pending_disable) {
4580 event->pending_disable = 0;
4581 __perf_event_disable(event);
4582 }
4583
4584 if (event->pending_wakeup) {
4585 event->pending_wakeup = 0;
4586 perf_event_wakeup(event);
4587 }
4588}
4589
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004590/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004591 * We assume there is only KVM supporting the callbacks.
4592 * Later on, we might change it to a list if there is
4593 * another virtualization implementation supporting the callbacks.
4594 */
4595struct perf_guest_info_callbacks *perf_guest_cbs;
4596
4597int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4598{
4599 perf_guest_cbs = cbs;
4600 return 0;
4601}
4602EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4603
4604int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4605{
4606 perf_guest_cbs = NULL;
4607 return 0;
4608}
4609EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4610
Jiri Olsa40189942012-08-07 15:20:37 +02004611static void
4612perf_output_sample_regs(struct perf_output_handle *handle,
4613 struct pt_regs *regs, u64 mask)
4614{
4615 int bit;
4616
4617 for_each_set_bit(bit, (const unsigned long *) &mask,
4618 sizeof(mask) * BITS_PER_BYTE) {
4619 u64 val;
4620
4621 val = perf_reg_value(regs, bit);
4622 perf_output_put(handle, val);
4623 }
4624}
4625
Stephane Eranian60e23642014-09-24 13:48:37 +02004626static void perf_sample_regs_user(struct perf_regs *regs_user,
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004627 struct pt_regs *regs,
4628 struct pt_regs *regs_user_copy)
Jiri Olsa40189942012-08-07 15:20:37 +02004629{
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004630 if (user_mode(regs)) {
4631 regs_user->abi = perf_reg_abi(current);
Peter Zijlstra25657112014-09-24 13:48:42 +02004632 regs_user->regs = regs;
Andy Lutomirski88a7c262015-01-04 10:36:19 -08004633 } else if (current->mm) {
4634 perf_get_regs_user(regs_user, regs, regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02004635 } else {
4636 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
4637 regs_user->regs = NULL;
Jiri Olsa40189942012-08-07 15:20:37 +02004638 }
4639}
4640
Stephane Eranian60e23642014-09-24 13:48:37 +02004641static void perf_sample_regs_intr(struct perf_regs *regs_intr,
4642 struct pt_regs *regs)
4643{
4644 regs_intr->regs = regs;
4645 regs_intr->abi = perf_reg_abi(current);
4646}
4647
4648
Jiri Olsac5ebced2012-08-07 15:20:40 +02004649/*
4650 * Get remaining task size from user stack pointer.
4651 *
4652 * It'd be better to take stack vma map and limit this more
4653 * precisly, but there's no way to get it safely under interrupt,
4654 * so using TASK_SIZE as limit.
4655 */
4656static u64 perf_ustack_task_size(struct pt_regs *regs)
4657{
4658 unsigned long addr = perf_user_stack_pointer(regs);
4659
4660 if (!addr || addr >= TASK_SIZE)
4661 return 0;
4662
4663 return TASK_SIZE - addr;
4664}
4665
4666static u16
4667perf_sample_ustack_size(u16 stack_size, u16 header_size,
4668 struct pt_regs *regs)
4669{
4670 u64 task_size;
4671
4672 /* No regs, no stack pointer, no dump. */
4673 if (!regs)
4674 return 0;
4675
4676 /*
4677 * Check if we fit in with the requested stack size into the:
4678 * - TASK_SIZE
4679 * If we don't, we limit the size to the TASK_SIZE.
4680 *
4681 * - remaining sample size
4682 * If we don't, we customize the stack size to
4683 * fit in to the remaining sample size.
4684 */
4685
4686 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4687 stack_size = min(stack_size, (u16) task_size);
4688
4689 /* Current header size plus static size and dynamic size. */
4690 header_size += 2 * sizeof(u64);
4691
4692 /* Do we fit in with the current stack dump size? */
4693 if ((u16) (header_size + stack_size) < header_size) {
4694 /*
4695 * If we overflow the maximum size for the sample,
4696 * we customize the stack dump size to fit in.
4697 */
4698 stack_size = USHRT_MAX - header_size - sizeof(u64);
4699 stack_size = round_up(stack_size, sizeof(u64));
4700 }
4701
4702 return stack_size;
4703}
4704
4705static void
4706perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4707 struct pt_regs *regs)
4708{
4709 /* Case of a kernel thread, nothing to dump */
4710 if (!regs) {
4711 u64 size = 0;
4712 perf_output_put(handle, size);
4713 } else {
4714 unsigned long sp;
4715 unsigned int rem;
4716 u64 dyn_size;
4717
4718 /*
4719 * We dump:
4720 * static size
4721 * - the size requested by user or the best one we can fit
4722 * in to the sample max size
4723 * data
4724 * - user stack dump data
4725 * dynamic size
4726 * - the actual dumped size
4727 */
4728
4729 /* Static size. */
4730 perf_output_put(handle, dump_size);
4731
4732 /* Data. */
4733 sp = perf_user_stack_pointer(regs);
4734 rem = __output_copy_user(handle, (void *) sp, dump_size);
4735 dyn_size = dump_size - rem;
4736
4737 perf_output_skip(handle, rem);
4738
4739 /* Dynamic size. */
4740 perf_output_put(handle, dyn_size);
4741 }
4742}
4743
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004744static void __perf_event_header__init_id(struct perf_event_header *header,
4745 struct perf_sample_data *data,
4746 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004747{
4748 u64 sample_type = event->attr.sample_type;
4749
4750 data->type = sample_type;
4751 header->size += event->id_header_size;
4752
4753 if (sample_type & PERF_SAMPLE_TID) {
4754 /* namespace issues */
4755 data->tid_entry.pid = perf_event_pid(event, current);
4756 data->tid_entry.tid = perf_event_tid(event, current);
4757 }
4758
4759 if (sample_type & PERF_SAMPLE_TIME)
4760 data->time = perf_clock();
4761
Adrian Hunterff3d5272013-08-27 11:23:07 +03004762 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004763 data->id = primary_event_id(event);
4764
4765 if (sample_type & PERF_SAMPLE_STREAM_ID)
4766 data->stream_id = event->id;
4767
4768 if (sample_type & PERF_SAMPLE_CPU) {
4769 data->cpu_entry.cpu = raw_smp_processor_id();
4770 data->cpu_entry.reserved = 0;
4771 }
4772}
4773
Frederic Weisbecker76369132011-05-19 19:55:04 +02004774void perf_event_header__init_id(struct perf_event_header *header,
4775 struct perf_sample_data *data,
4776 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004777{
4778 if (event->attr.sample_id_all)
4779 __perf_event_header__init_id(header, data, event);
4780}
4781
4782static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4783 struct perf_sample_data *data)
4784{
4785 u64 sample_type = data->type;
4786
4787 if (sample_type & PERF_SAMPLE_TID)
4788 perf_output_put(handle, data->tid_entry);
4789
4790 if (sample_type & PERF_SAMPLE_TIME)
4791 perf_output_put(handle, data->time);
4792
4793 if (sample_type & PERF_SAMPLE_ID)
4794 perf_output_put(handle, data->id);
4795
4796 if (sample_type & PERF_SAMPLE_STREAM_ID)
4797 perf_output_put(handle, data->stream_id);
4798
4799 if (sample_type & PERF_SAMPLE_CPU)
4800 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03004801
4802 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4803 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004804}
4805
Frederic Weisbecker76369132011-05-19 19:55:04 +02004806void perf_event__output_id_sample(struct perf_event *event,
4807 struct perf_output_handle *handle,
4808 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004809{
4810 if (event->attr.sample_id_all)
4811 __perf_event__output_id_sample(handle, sample);
4812}
4813
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004814static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004815 struct perf_event *event,
4816 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004817{
4818 u64 read_format = event->attr.read_format;
4819 u64 values[4];
4820 int n = 0;
4821
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004822 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004823 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004824 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004825 atomic64_read(&event->child_total_time_enabled);
4826 }
4827 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004828 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004829 atomic64_read(&event->child_total_time_running);
4830 }
4831 if (read_format & PERF_FORMAT_ID)
4832 values[n++] = primary_event_id(event);
4833
Frederic Weisbecker76369132011-05-19 19:55:04 +02004834 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004835}
4836
4837/*
4838 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4839 */
4840static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004841 struct perf_event *event,
4842 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004843{
4844 struct perf_event *leader = event->group_leader, *sub;
4845 u64 read_format = event->attr.read_format;
4846 u64 values[5];
4847 int n = 0;
4848
4849 values[n++] = 1 + leader->nr_siblings;
4850
4851 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004852 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004853
4854 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004855 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004856
4857 if (leader != event)
4858 leader->pmu->read(leader);
4859
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004860 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004861 if (read_format & PERF_FORMAT_ID)
4862 values[n++] = primary_event_id(leader);
4863
Frederic Weisbecker76369132011-05-19 19:55:04 +02004864 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004865
4866 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4867 n = 0;
4868
Jiri Olsa6f5ab002012-10-15 20:13:45 +02004869 if ((sub != event) &&
4870 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004871 sub->pmu->read(sub);
4872
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004873 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004874 if (read_format & PERF_FORMAT_ID)
4875 values[n++] = primary_event_id(sub);
4876
Frederic Weisbecker76369132011-05-19 19:55:04 +02004877 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004878 }
4879}
4880
Stephane Eranianeed01522010-10-26 16:08:01 +02004881#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4882 PERF_FORMAT_TOTAL_TIME_RUNNING)
4883
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004884static void perf_output_read(struct perf_output_handle *handle,
4885 struct perf_event *event)
4886{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004887 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004888 u64 read_format = event->attr.read_format;
4889
4890 /*
4891 * compute total_time_enabled, total_time_running
4892 * based on snapshot values taken when the event
4893 * was last scheduled in.
4894 *
4895 * we cannot simply called update_context_time()
4896 * because of locking issue as we are called in
4897 * NMI context
4898 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004899 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004900 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004901
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004902 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004903 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004904 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004905 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004906}
4907
4908void perf_output_sample(struct perf_output_handle *handle,
4909 struct perf_event_header *header,
4910 struct perf_sample_data *data,
4911 struct perf_event *event)
4912{
4913 u64 sample_type = data->type;
4914
4915 perf_output_put(handle, *header);
4916
Adrian Hunterff3d5272013-08-27 11:23:07 +03004917 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4918 perf_output_put(handle, data->id);
4919
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004920 if (sample_type & PERF_SAMPLE_IP)
4921 perf_output_put(handle, data->ip);
4922
4923 if (sample_type & PERF_SAMPLE_TID)
4924 perf_output_put(handle, data->tid_entry);
4925
4926 if (sample_type & PERF_SAMPLE_TIME)
4927 perf_output_put(handle, data->time);
4928
4929 if (sample_type & PERF_SAMPLE_ADDR)
4930 perf_output_put(handle, data->addr);
4931
4932 if (sample_type & PERF_SAMPLE_ID)
4933 perf_output_put(handle, data->id);
4934
4935 if (sample_type & PERF_SAMPLE_STREAM_ID)
4936 perf_output_put(handle, data->stream_id);
4937
4938 if (sample_type & PERF_SAMPLE_CPU)
4939 perf_output_put(handle, data->cpu_entry);
4940
4941 if (sample_type & PERF_SAMPLE_PERIOD)
4942 perf_output_put(handle, data->period);
4943
4944 if (sample_type & PERF_SAMPLE_READ)
4945 perf_output_read(handle, event);
4946
4947 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4948 if (data->callchain) {
4949 int size = 1;
4950
4951 if (data->callchain)
4952 size += data->callchain->nr;
4953
4954 size *= sizeof(u64);
4955
Frederic Weisbecker76369132011-05-19 19:55:04 +02004956 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004957 } else {
4958 u64 nr = 0;
4959 perf_output_put(handle, nr);
4960 }
4961 }
4962
4963 if (sample_type & PERF_SAMPLE_RAW) {
4964 if (data->raw) {
4965 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004966 __output_copy(handle, data->raw->data,
4967 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004968 } else {
4969 struct {
4970 u32 size;
4971 u32 data;
4972 } raw = {
4973 .size = sizeof(u32),
4974 .data = 0,
4975 };
4976 perf_output_put(handle, raw);
4977 }
4978 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004979
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004980 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4981 if (data->br_stack) {
4982 size_t size;
4983
4984 size = data->br_stack->nr
4985 * sizeof(struct perf_branch_entry);
4986
4987 perf_output_put(handle, data->br_stack->nr);
4988 perf_output_copy(handle, data->br_stack->entries, size);
4989 } else {
4990 /*
4991 * we always store at least the value of nr
4992 */
4993 u64 nr = 0;
4994 perf_output_put(handle, nr);
4995 }
4996 }
Jiri Olsa40189942012-08-07 15:20:37 +02004997
4998 if (sample_type & PERF_SAMPLE_REGS_USER) {
4999 u64 abi = data->regs_user.abi;
5000
5001 /*
5002 * If there are no regs to dump, notice it through
5003 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5004 */
5005 perf_output_put(handle, abi);
5006
5007 if (abi) {
5008 u64 mask = event->attr.sample_regs_user;
5009 perf_output_sample_regs(handle,
5010 data->regs_user.regs,
5011 mask);
5012 }
5013 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005014
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005015 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02005016 perf_output_sample_ustack(handle,
5017 data->stack_user_size,
5018 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005019 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01005020
5021 if (sample_type & PERF_SAMPLE_WEIGHT)
5022 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01005023
5024 if (sample_type & PERF_SAMPLE_DATA_SRC)
5025 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005026
Andi Kleenfdfbbd02013-09-20 07:40:39 -07005027 if (sample_type & PERF_SAMPLE_TRANSACTION)
5028 perf_output_put(handle, data->txn);
5029
Stephane Eranian60e23642014-09-24 13:48:37 +02005030 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5031 u64 abi = data->regs_intr.abi;
5032 /*
5033 * If there are no regs to dump, notice it through
5034 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5035 */
5036 perf_output_put(handle, abi);
5037
5038 if (abi) {
5039 u64 mask = event->attr.sample_regs_intr;
5040
5041 perf_output_sample_regs(handle,
5042 data->regs_intr.regs,
5043 mask);
5044 }
5045 }
5046
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02005047 if (!event->attr.watermark) {
5048 int wakeup_events = event->attr.wakeup_events;
5049
5050 if (wakeup_events) {
5051 struct ring_buffer *rb = handle->rb;
5052 int events = local_inc_return(&rb->events);
5053
5054 if (events >= wakeup_events) {
5055 local_sub(wakeup_events, &rb->events);
5056 local_inc(&rb->wakeup);
5057 }
5058 }
5059 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005060}
5061
5062void perf_prepare_sample(struct perf_event_header *header,
5063 struct perf_sample_data *data,
5064 struct perf_event *event,
5065 struct pt_regs *regs)
5066{
5067 u64 sample_type = event->attr.sample_type;
5068
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005069 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005070 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005071
5072 header->misc = 0;
5073 header->misc |= perf_misc_flags(regs);
5074
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005075 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005076
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005077 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005078 data->ip = perf_instruction_pointer(regs);
5079
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005080 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5081 int size = 1;
5082
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005083 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005084
5085 if (data->callchain)
5086 size += data->callchain->nr;
5087
5088 header->size += size * sizeof(u64);
5089 }
5090
5091 if (sample_type & PERF_SAMPLE_RAW) {
5092 int size = sizeof(u32);
5093
5094 if (data->raw)
5095 size += data->raw->size;
5096 else
5097 size += sizeof(u32);
5098
5099 WARN_ON_ONCE(size & (sizeof(u64)-1));
5100 header->size += size;
5101 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01005102
5103 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5104 int size = sizeof(u64); /* nr */
5105 if (data->br_stack) {
5106 size += data->br_stack->nr
5107 * sizeof(struct perf_branch_entry);
5108 }
5109 header->size += size;
5110 }
Jiri Olsa40189942012-08-07 15:20:37 +02005111
Peter Zijlstra25657112014-09-24 13:48:42 +02005112 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
Andy Lutomirski88a7c262015-01-04 10:36:19 -08005113 perf_sample_regs_user(&data->regs_user, regs,
5114 &data->regs_user_copy);
Peter Zijlstra25657112014-09-24 13:48:42 +02005115
Jiri Olsa40189942012-08-07 15:20:37 +02005116 if (sample_type & PERF_SAMPLE_REGS_USER) {
5117 /* regs dump ABI info */
5118 int size = sizeof(u64);
5119
Jiri Olsa40189942012-08-07 15:20:37 +02005120 if (data->regs_user.regs) {
5121 u64 mask = event->attr.sample_regs_user;
5122 size += hweight64(mask) * sizeof(u64);
5123 }
5124
5125 header->size += size;
5126 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02005127
5128 if (sample_type & PERF_SAMPLE_STACK_USER) {
5129 /*
5130 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5131 * processed as the last one or have additional check added
5132 * in case new sample type is added, because we could eat
5133 * up the rest of the sample size.
5134 */
Jiri Olsac5ebced2012-08-07 15:20:40 +02005135 u16 stack_size = event->attr.sample_stack_user;
5136 u16 size = sizeof(u64);
5137
Jiri Olsac5ebced2012-08-07 15:20:40 +02005138 stack_size = perf_sample_ustack_size(stack_size, header->size,
Peter Zijlstra25657112014-09-24 13:48:42 +02005139 data->regs_user.regs);
Jiri Olsac5ebced2012-08-07 15:20:40 +02005140
5141 /*
5142 * If there is something to dump, add space for the dump
5143 * itself and for the field that tells the dynamic size,
5144 * which is how many have been actually dumped.
5145 */
5146 if (stack_size)
5147 size += sizeof(u64) + stack_size;
5148
5149 data->stack_user_size = stack_size;
5150 header->size += size;
5151 }
Stephane Eranian60e23642014-09-24 13:48:37 +02005152
5153 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5154 /* regs dump ABI info */
5155 int size = sizeof(u64);
5156
5157 perf_sample_regs_intr(&data->regs_intr, regs);
5158
5159 if (data->regs_intr.regs) {
5160 u64 mask = event->attr.sample_regs_intr;
5161
5162 size += hweight64(mask) * sizeof(u64);
5163 }
5164
5165 header->size += size;
5166 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005167}
5168
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005169static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005170 struct perf_sample_data *data,
5171 struct pt_regs *regs)
5172{
5173 struct perf_output_handle handle;
5174 struct perf_event_header header;
5175
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005176 /* protect the callchain buffers */
5177 rcu_read_lock();
5178
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005179 perf_prepare_sample(&header, data, event, regs);
5180
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005181 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005182 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005183
5184 perf_output_sample(&handle, &header, data, event);
5185
5186 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005187
5188exit:
5189 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005190}
5191
5192/*
5193 * read event_id
5194 */
5195
5196struct perf_read_event {
5197 struct perf_event_header header;
5198
5199 u32 pid;
5200 u32 tid;
5201};
5202
5203static void
5204perf_event_read_event(struct perf_event *event,
5205 struct task_struct *task)
5206{
5207 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005208 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005209 struct perf_read_event read_event = {
5210 .header = {
5211 .type = PERF_RECORD_READ,
5212 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005213 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005214 },
5215 .pid = perf_event_pid(event, task),
5216 .tid = perf_event_tid(event, task),
5217 };
5218 int ret;
5219
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005220 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005221 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005222 if (ret)
5223 return;
5224
5225 perf_output_put(&handle, read_event);
5226 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005227 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005228
5229 perf_output_end(&handle);
5230}
5231
Jiri Olsa52d857a2013-05-06 18:27:18 +02005232typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5233
5234static void
5235perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005236 perf_event_aux_output_cb output,
5237 void *data)
5238{
5239 struct perf_event *event;
5240
5241 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5242 if (event->state < PERF_EVENT_STATE_INACTIVE)
5243 continue;
5244 if (!event_filter_match(event))
5245 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02005246 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005247 }
5248}
5249
5250static void
Jiri Olsa67516842013-07-09 18:56:31 +02005251perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005252 struct perf_event_context *task_ctx)
5253{
5254 struct perf_cpu_context *cpuctx;
5255 struct perf_event_context *ctx;
5256 struct pmu *pmu;
5257 int ctxn;
5258
5259 rcu_read_lock();
5260 list_for_each_entry_rcu(pmu, &pmus, entry) {
5261 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5262 if (cpuctx->unique_pmu != pmu)
5263 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02005264 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005265 if (task_ctx)
5266 goto next;
5267 ctxn = pmu->task_ctx_nr;
5268 if (ctxn < 0)
5269 goto next;
5270 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5271 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02005272 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005273next:
5274 put_cpu_ptr(pmu->pmu_cpu_context);
5275 }
5276
5277 if (task_ctx) {
5278 preempt_disable();
Jiri Olsa67516842013-07-09 18:56:31 +02005279 perf_event_aux_ctx(task_ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005280 preempt_enable();
5281 }
5282 rcu_read_unlock();
5283}
5284
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005285/*
5286 * task tracking -- fork/exit
5287 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02005288 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005289 */
5290
5291struct perf_task_event {
5292 struct task_struct *task;
5293 struct perf_event_context *task_ctx;
5294
5295 struct {
5296 struct perf_event_header header;
5297
5298 u32 pid;
5299 u32 ppid;
5300 u32 tid;
5301 u32 ptid;
5302 u64 time;
5303 } event_id;
5304};
5305
Jiri Olsa67516842013-07-09 18:56:31 +02005306static int perf_event_task_match(struct perf_event *event)
5307{
Stephane Eranian13d7a242013-08-21 12:10:24 +02005308 return event->attr.comm || event->attr.mmap ||
5309 event->attr.mmap2 || event->attr.mmap_data ||
5310 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02005311}
5312
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005313static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005314 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005315{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005316 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005317 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005318 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005319 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005320 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01005321
Jiri Olsa67516842013-07-09 18:56:31 +02005322 if (!perf_event_task_match(event))
5323 return;
5324
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005325 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005326
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005327 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005328 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02005329 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005330 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005331
5332 task_event->event_id.pid = perf_event_pid(event, task);
5333 task_event->event_id.ppid = perf_event_pid(event, current);
5334
5335 task_event->event_id.tid = perf_event_tid(event, task);
5336 task_event->event_id.ptid = perf_event_tid(event, current);
5337
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005338 perf_output_put(&handle, task_event->event_id);
5339
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005340 perf_event__output_id_sample(event, &handle, &sample);
5341
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005342 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005343out:
5344 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005345}
5346
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005347static void perf_event_task(struct task_struct *task,
5348 struct perf_event_context *task_ctx,
5349 int new)
5350{
5351 struct perf_task_event task_event;
5352
5353 if (!atomic_read(&nr_comm_events) &&
5354 !atomic_read(&nr_mmap_events) &&
5355 !atomic_read(&nr_task_events))
5356 return;
5357
5358 task_event = (struct perf_task_event){
5359 .task = task,
5360 .task_ctx = task_ctx,
5361 .event_id = {
5362 .header = {
5363 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5364 .misc = 0,
5365 .size = sizeof(task_event.event_id),
5366 },
5367 /* .pid */
5368 /* .ppid */
5369 /* .tid */
5370 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01005371 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005372 },
5373 };
5374
Jiri Olsa67516842013-07-09 18:56:31 +02005375 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005376 &task_event,
5377 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005378}
5379
5380void perf_event_fork(struct task_struct *task)
5381{
5382 perf_event_task(task, NULL, 1);
5383}
5384
5385/*
5386 * comm tracking
5387 */
5388
5389struct perf_comm_event {
5390 struct task_struct *task;
5391 char *comm;
5392 int comm_size;
5393
5394 struct {
5395 struct perf_event_header header;
5396
5397 u32 pid;
5398 u32 tid;
5399 } event_id;
5400};
5401
Jiri Olsa67516842013-07-09 18:56:31 +02005402static int perf_event_comm_match(struct perf_event *event)
5403{
5404 return event->attr.comm;
5405}
5406
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005407static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005408 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005409{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005410 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005411 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005412 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005413 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005414 int ret;
5415
Jiri Olsa67516842013-07-09 18:56:31 +02005416 if (!perf_event_comm_match(event))
5417 return;
5418
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005419 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5420 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005421 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005422
5423 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005424 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005425
5426 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5427 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5428
5429 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005430 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005431 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005432
5433 perf_event__output_id_sample(event, &handle, &sample);
5434
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005435 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005436out:
5437 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005438}
5439
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005440static void perf_event_comm_event(struct perf_comm_event *comm_event)
5441{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005442 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005443 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005444
5445 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005446 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005447 size = ALIGN(strlen(comm)+1, sizeof(u64));
5448
5449 comm_event->comm = comm;
5450 comm_event->comm_size = size;
5451
5452 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005453
Jiri Olsa67516842013-07-09 18:56:31 +02005454 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005455 comm_event,
5456 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005457}
5458
Adrian Hunter82b89772014-05-28 11:45:04 +03005459void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005460{
5461 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005462
5463 if (!atomic_read(&nr_comm_events))
5464 return;
5465
5466 comm_event = (struct perf_comm_event){
5467 .task = task,
5468 /* .comm */
5469 /* .comm_size */
5470 .event_id = {
5471 .header = {
5472 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005473 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005474 /* .size */
5475 },
5476 /* .pid */
5477 /* .tid */
5478 },
5479 };
5480
5481 perf_event_comm_event(&comm_event);
5482}
5483
5484/*
5485 * mmap tracking
5486 */
5487
5488struct perf_mmap_event {
5489 struct vm_area_struct *vma;
5490
5491 const char *file_name;
5492 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005493 int maj, min;
5494 u64 ino;
5495 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005496 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005497
5498 struct {
5499 struct perf_event_header header;
5500
5501 u32 pid;
5502 u32 tid;
5503 u64 start;
5504 u64 len;
5505 u64 pgoff;
5506 } event_id;
5507};
5508
Jiri Olsa67516842013-07-09 18:56:31 +02005509static int perf_event_mmap_match(struct perf_event *event,
5510 void *data)
5511{
5512 struct perf_mmap_event *mmap_event = data;
5513 struct vm_area_struct *vma = mmap_event->vma;
5514 int executable = vma->vm_flags & VM_EXEC;
5515
5516 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005517 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005518}
5519
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005520static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005521 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005522{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005523 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005524 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005525 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005526 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005527 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005528
Jiri Olsa67516842013-07-09 18:56:31 +02005529 if (!perf_event_mmap_match(event, data))
5530 return;
5531
Stephane Eranian13d7a242013-08-21 12:10:24 +02005532 if (event->attr.mmap2) {
5533 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5534 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5535 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5536 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005537 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005538 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
5539 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005540 }
5541
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005542 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5543 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005544 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005545 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005546 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005547
5548 mmap_event->event_id.pid = perf_event_pid(event, current);
5549 mmap_event->event_id.tid = perf_event_tid(event, current);
5550
5551 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005552
5553 if (event->attr.mmap2) {
5554 perf_output_put(&handle, mmap_event->maj);
5555 perf_output_put(&handle, mmap_event->min);
5556 perf_output_put(&handle, mmap_event->ino);
5557 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005558 perf_output_put(&handle, mmap_event->prot);
5559 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005560 }
5561
Frederic Weisbecker76369132011-05-19 19:55:04 +02005562 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005563 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005564
5565 perf_event__output_id_sample(event, &handle, &sample);
5566
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005567 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005568out:
5569 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005570}
5571
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005572static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5573{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005574 struct vm_area_struct *vma = mmap_event->vma;
5575 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005576 int maj = 0, min = 0;
5577 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005578 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005579 unsigned int size;
5580 char tmp[16];
5581 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005582 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005583
5584 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005585 struct inode *inode;
5586 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005587
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005588 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005589 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005590 name = "//enomem";
5591 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005592 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005593 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005594 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005595 * need to add enough zero bytes after the string to handle
5596 * the 64bit alignment we do later.
5597 */
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005598 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005599 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005600 name = "//toolong";
5601 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005602 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005603 inode = file_inode(vma->vm_file);
5604 dev = inode->i_sb->s_dev;
5605 ino = inode->i_ino;
5606 gen = inode->i_generation;
5607 maj = MAJOR(dev);
5608 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005609
5610 if (vma->vm_flags & VM_READ)
5611 prot |= PROT_READ;
5612 if (vma->vm_flags & VM_WRITE)
5613 prot |= PROT_WRITE;
5614 if (vma->vm_flags & VM_EXEC)
5615 prot |= PROT_EXEC;
5616
5617 if (vma->vm_flags & VM_MAYSHARE)
5618 flags = MAP_SHARED;
5619 else
5620 flags = MAP_PRIVATE;
5621
5622 if (vma->vm_flags & VM_DENYWRITE)
5623 flags |= MAP_DENYWRITE;
5624 if (vma->vm_flags & VM_MAYEXEC)
5625 flags |= MAP_EXECUTABLE;
5626 if (vma->vm_flags & VM_LOCKED)
5627 flags |= MAP_LOCKED;
5628 if (vma->vm_flags & VM_HUGETLB)
5629 flags |= MAP_HUGETLB;
5630
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005631 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005632 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02005633 if (vma->vm_ops && vma->vm_ops->name) {
5634 name = (char *) vma->vm_ops->name(vma);
5635 if (name)
5636 goto cpy_name;
5637 }
5638
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005639 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005640 if (name)
5641 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005642
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005643 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005644 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005645 name = "[heap]";
5646 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005647 }
5648 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005649 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005650 name = "[stack]";
5651 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005652 }
5653
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005654 name = "//anon";
5655 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005656 }
5657
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005658cpy_name:
5659 strlcpy(tmp, name, sizeof(tmp));
5660 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005661got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005662 /*
5663 * Since our buffer works in 8 byte units we need to align our string
5664 * size to a multiple of 8. However, we must guarantee the tail end is
5665 * zero'd out to avoid leaking random bits to userspace.
5666 */
5667 size = strlen(name)+1;
5668 while (!IS_ALIGNED(size, sizeof(u64)))
5669 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005670
5671 mmap_event->file_name = name;
5672 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005673 mmap_event->maj = maj;
5674 mmap_event->min = min;
5675 mmap_event->ino = ino;
5676 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005677 mmap_event->prot = prot;
5678 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005679
Stephane Eranian2fe85422013-01-24 16:10:39 +01005680 if (!(vma->vm_flags & VM_EXEC))
5681 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5682
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005683 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5684
Jiri Olsa67516842013-07-09 18:56:31 +02005685 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005686 mmap_event,
5687 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005688
5689 kfree(buf);
5690}
5691
Eric B Munson3af9e852010-05-18 15:30:49 +01005692void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005693{
5694 struct perf_mmap_event mmap_event;
5695
5696 if (!atomic_read(&nr_mmap_events))
5697 return;
5698
5699 mmap_event = (struct perf_mmap_event){
5700 .vma = vma,
5701 /* .file_name */
5702 /* .file_size */
5703 .event_id = {
5704 .header = {
5705 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005706 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005707 /* .size */
5708 },
5709 /* .pid */
5710 /* .tid */
5711 .start = vma->vm_start,
5712 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01005713 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005714 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02005715 /* .maj (attr_mmap2 only) */
5716 /* .min (attr_mmap2 only) */
5717 /* .ino (attr_mmap2 only) */
5718 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005719 /* .prot (attr_mmap2 only) */
5720 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005721 };
5722
5723 perf_event_mmap_event(&mmap_event);
5724}
5725
5726/*
5727 * IRQ throttle logging
5728 */
5729
5730static void perf_log_throttle(struct perf_event *event, int enable)
5731{
5732 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005733 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005734 int ret;
5735
5736 struct {
5737 struct perf_event_header header;
5738 u64 time;
5739 u64 id;
5740 u64 stream_id;
5741 } throttle_event = {
5742 .header = {
5743 .type = PERF_RECORD_THROTTLE,
5744 .misc = 0,
5745 .size = sizeof(throttle_event),
5746 },
5747 .time = perf_clock(),
5748 .id = primary_event_id(event),
5749 .stream_id = event->id,
5750 };
5751
5752 if (enable)
5753 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5754
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005755 perf_event_header__init_id(&throttle_event.header, &sample, event);
5756
5757 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005758 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005759 if (ret)
5760 return;
5761
5762 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005763 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005764 perf_output_end(&handle);
5765}
5766
5767/*
5768 * Generic event overflow handling, sampling.
5769 */
5770
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005771static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005772 int throttle, struct perf_sample_data *data,
5773 struct pt_regs *regs)
5774{
5775 int events = atomic_read(&event->event_limit);
5776 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005777 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005778 int ret = 0;
5779
Peter Zijlstra96398822010-11-24 18:55:29 +01005780 /*
5781 * Non-sampling counters might still use the PMI to fold short
5782 * hardware counters, ignore those.
5783 */
5784 if (unlikely(!is_sampling_event(event)))
5785 return 0;
5786
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005787 seq = __this_cpu_read(perf_throttled_seq);
5788 if (seq != hwc->interrupts_seq) {
5789 hwc->interrupts_seq = seq;
5790 hwc->interrupts = 1;
5791 } else {
5792 hwc->interrupts++;
5793 if (unlikely(throttle
5794 && hwc->interrupts >= max_samples_per_tick)) {
5795 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01005796 hwc->interrupts = MAX_INTERRUPTS;
5797 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02005798 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005799 ret = 1;
5800 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005801 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005802
5803 if (event->attr.freq) {
5804 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01005805 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005806
Peter Zijlstraabd50712010-01-26 18:50:16 +01005807 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005808
Peter Zijlstraabd50712010-01-26 18:50:16 +01005809 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01005810 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005811 }
5812
5813 /*
5814 * XXX event_limit might not quite work as expected on inherited
5815 * events
5816 */
5817
5818 event->pending_kill = POLL_IN;
5819 if (events && atomic_dec_and_test(&event->event_limit)) {
5820 ret = 1;
5821 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005822 event->pending_disable = 1;
5823 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005824 }
5825
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005826 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005827 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005828 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005829 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005830
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005831 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005832 event->pending_wakeup = 1;
5833 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005834 }
5835
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005836 return ret;
5837}
5838
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005839int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005840 struct perf_sample_data *data,
5841 struct pt_regs *regs)
5842{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005843 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005844}
5845
5846/*
5847 * Generic software event infrastructure
5848 */
5849
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005850struct swevent_htable {
5851 struct swevent_hlist *swevent_hlist;
5852 struct mutex hlist_mutex;
5853 int hlist_refcount;
5854
5855 /* Recursion avoidance in each contexts */
5856 int recursion[PERF_NR_CONTEXTS];
Jiri Olsa39af6b12014-04-07 11:04:08 +02005857
5858 /* Keeps track of cpu being initialized/exited */
5859 bool online;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005860};
5861
5862static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5863
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005864/*
5865 * We directly increment event->count and keep a second value in
5866 * event->hw.period_left to count intervals. This period event
5867 * is kept in the range [-sample_period, 0] so that we can use the
5868 * sign as trigger.
5869 */
5870
Jiri Olsaab573842013-05-01 17:25:44 +02005871u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005872{
5873 struct hw_perf_event *hwc = &event->hw;
5874 u64 period = hwc->last_period;
5875 u64 nr, offset;
5876 s64 old, val;
5877
5878 hwc->last_period = hwc->sample_period;
5879
5880again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005881 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005882 if (val < 0)
5883 return 0;
5884
5885 nr = div64_u64(period + val, period);
5886 offset = nr * period;
5887 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005888 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005889 goto again;
5890
5891 return nr;
5892}
5893
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005894static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005895 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005896 struct pt_regs *regs)
5897{
5898 struct hw_perf_event *hwc = &event->hw;
5899 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005900
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005901 if (!overflow)
5902 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005903
5904 if (hwc->interrupts == MAX_INTERRUPTS)
5905 return;
5906
5907 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005908 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005909 data, regs)) {
5910 /*
5911 * We inhibit the overflow from happening when
5912 * hwc->interrupts == MAX_INTERRUPTS.
5913 */
5914 break;
5915 }
5916 throttle = 1;
5917 }
5918}
5919
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005920static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005921 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005922 struct pt_regs *regs)
5923{
5924 struct hw_perf_event *hwc = &event->hw;
5925
Peter Zijlstrae7850592010-05-21 14:43:08 +02005926 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005927
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005928 if (!regs)
5929 return;
5930
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005931 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005932 return;
5933
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005934 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5935 data->period = nr;
5936 return perf_swevent_overflow(event, 1, data, regs);
5937 } else
5938 data->period = event->hw.last_period;
5939
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005940 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005941 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005942
Peter Zijlstrae7850592010-05-21 14:43:08 +02005943 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005944 return;
5945
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005946 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005947}
5948
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005949static int perf_exclude_event(struct perf_event *event,
5950 struct pt_regs *regs)
5951{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005952 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005953 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005954
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005955 if (regs) {
5956 if (event->attr.exclude_user && user_mode(regs))
5957 return 1;
5958
5959 if (event->attr.exclude_kernel && !user_mode(regs))
5960 return 1;
5961 }
5962
5963 return 0;
5964}
5965
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005966static int perf_swevent_match(struct perf_event *event,
5967 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005968 u32 event_id,
5969 struct perf_sample_data *data,
5970 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005971{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005972 if (event->attr.type != type)
5973 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005974
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005975 if (event->attr.config != event_id)
5976 return 0;
5977
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005978 if (perf_exclude_event(event, regs))
5979 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005980
5981 return 1;
5982}
5983
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005984static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005985{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005986 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005987
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005988 return hash_64(val, SWEVENT_HLIST_BITS);
5989}
5990
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005991static inline struct hlist_head *
5992__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005993{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005994 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005995
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005996 return &hlist->heads[hash];
5997}
5998
5999/* For the read side: events when they trigger */
6000static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006001find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006002{
6003 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006004
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006005 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006006 if (!hlist)
6007 return NULL;
6008
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006009 return __find_swevent_head(hlist, type, event_id);
6010}
6011
6012/* For the event head insertion and removal in the hlist */
6013static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006014find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006015{
6016 struct swevent_hlist *hlist;
6017 u32 event_id = event->attr.config;
6018 u64 type = event->attr.type;
6019
6020 /*
6021 * Event scheduling is always serialized against hlist allocation
6022 * and release. Which makes the protected version suitable here.
6023 * The context lock guarantees that.
6024 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006025 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006026 lockdep_is_held(&event->ctx->lock));
6027 if (!hlist)
6028 return NULL;
6029
6030 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006031}
6032
6033static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006034 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006035 struct perf_sample_data *data,
6036 struct pt_regs *regs)
6037{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006038 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006039 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006040 struct hlist_head *head;
6041
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006042 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006043 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006044 if (!head)
6045 goto end;
6046
Sasha Levinb67bfe02013-02-27 17:06:00 -08006047 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08006048 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006049 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006050 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006051end:
6052 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006053}
6054
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006055DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
6056
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006057int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006058{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006059 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006060
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006061 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006062}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01006063EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006064
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01006065inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006066{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006067 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006068
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006069 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01006070}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006071
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006072void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006073{
Ingo Molnara4234bf2009-11-23 10:57:59 +01006074 struct perf_sample_data data;
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006075
6076 if (WARN_ON_ONCE(!regs))
6077 return;
6078
6079 perf_sample_data_init(&data, addr, 0);
6080 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
6081}
6082
6083void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
6084{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006085 int rctx;
6086
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006087 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006088 rctx = perf_swevent_get_recursion_context();
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006089 if (unlikely(rctx < 0))
6090 goto fail;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006091
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006092 ___perf_sw_event(event_id, nr, regs, addr);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01006093
6094 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra (Intel)86038c52014-12-16 12:47:34 +01006095fail:
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006096 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006097}
6098
6099static void perf_swevent_read(struct perf_event *event)
6100{
6101}
6102
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006103static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006104{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05006105 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006106 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006107 struct hlist_head *head;
6108
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006109 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006110 hwc->last_period = hwc->sample_period;
6111 perf_swevent_set_period(event);
6112 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006113
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006114 hwc->state = !(flags & PERF_EF_START);
6115
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006116 head = find_swevent_head(swhash, event);
Jiri Olsa39af6b12014-04-07 11:04:08 +02006117 if (!head) {
6118 /*
6119 * We can race with cpu hotplug code. Do not
6120 * WARN if the cpu just got unplugged.
6121 */
6122 WARN_ON_ONCE(swhash->online);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006123 return -EINVAL;
Jiri Olsa39af6b12014-04-07 11:04:08 +02006124 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006125
6126 hlist_add_head_rcu(&event->hlist_entry, head);
6127
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006128 return 0;
6129}
6130
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006131static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006132{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006133 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006134}
6135
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006136static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006137{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006138 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006139}
6140
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006141static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006142{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006143 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02006144}
6145
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006146/* Deref the hlist from the update side */
6147static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006148swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006149{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006150 return rcu_dereference_protected(swhash->swevent_hlist,
6151 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006152}
6153
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006154static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006155{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006156 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006157
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02006158 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006159 return;
6160
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03006161 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08006162 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006163}
6164
6165static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
6166{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006167 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006168
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006169 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006170
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006171 if (!--swhash->hlist_refcount)
6172 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006173
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006174 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006175}
6176
6177static void swevent_hlist_put(struct perf_event *event)
6178{
6179 int cpu;
6180
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006181 for_each_possible_cpu(cpu)
6182 swevent_hlist_put_cpu(event, cpu);
6183}
6184
6185static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
6186{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006187 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006188 int err = 0;
6189
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006190 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006191
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006192 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006193 struct swevent_hlist *hlist;
6194
6195 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
6196 if (!hlist) {
6197 err = -ENOMEM;
6198 goto exit;
6199 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006200 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006201 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006202 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006203exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006204 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006205
6206 return err;
6207}
6208
6209static int swevent_hlist_get(struct perf_event *event)
6210{
6211 int err;
6212 int cpu, failed_cpu;
6213
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006214 get_online_cpus();
6215 for_each_possible_cpu(cpu) {
6216 err = swevent_hlist_get_cpu(event, cpu);
6217 if (err) {
6218 failed_cpu = cpu;
6219 goto fail;
6220 }
6221 }
6222 put_online_cpus();
6223
6224 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006225fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006226 for_each_possible_cpu(cpu) {
6227 if (cpu == failed_cpu)
6228 break;
6229 swevent_hlist_put_cpu(event, cpu);
6230 }
6231
6232 put_online_cpus();
6233 return err;
6234}
6235
Ingo Molnarc5905af2012-02-24 08:31:31 +01006236struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006237
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006238static void sw_perf_event_destroy(struct perf_event *event)
6239{
6240 u64 event_id = event->attr.config;
6241
6242 WARN_ON(event->parent);
6243
Ingo Molnarc5905af2012-02-24 08:31:31 +01006244 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006245 swevent_hlist_put(event);
6246}
6247
6248static int perf_swevent_init(struct perf_event *event)
6249{
Tommi Rantala8176cce2013-04-13 22:49:14 +03006250 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006251
6252 if (event->attr.type != PERF_TYPE_SOFTWARE)
6253 return -ENOENT;
6254
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006255 /*
6256 * no branch sampling for software events
6257 */
6258 if (has_branch_stack(event))
6259 return -EOPNOTSUPP;
6260
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006261 switch (event_id) {
6262 case PERF_COUNT_SW_CPU_CLOCK:
6263 case PERF_COUNT_SW_TASK_CLOCK:
6264 return -ENOENT;
6265
6266 default:
6267 break;
6268 }
6269
Dan Carpenterce677832010-10-24 21:50:42 +02006270 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006271 return -ENOENT;
6272
6273 if (!event->parent) {
6274 int err;
6275
6276 err = swevent_hlist_get(event);
6277 if (err)
6278 return err;
6279
Ingo Molnarc5905af2012-02-24 08:31:31 +01006280 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006281 event->destroy = sw_perf_event_destroy;
6282 }
6283
6284 return 0;
6285}
6286
6287static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006288 .task_ctx_nr = perf_sw_context,
6289
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006290 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006291 .add = perf_swevent_add,
6292 .del = perf_swevent_del,
6293 .start = perf_swevent_start,
6294 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006295 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006296};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006297
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006298#ifdef CONFIG_EVENT_TRACING
6299
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006300static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006301 struct perf_sample_data *data)
6302{
6303 void *record = data->raw->data;
6304
6305 if (likely(!event->filter) || filter_match_preds(event->filter, record))
6306 return 1;
6307 return 0;
6308}
6309
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006310static int perf_tp_event_match(struct perf_event *event,
6311 struct perf_sample_data *data,
6312 struct pt_regs *regs)
6313{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01006314 if (event->hw.state & PERF_HES_STOPPED)
6315 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02006316 /*
6317 * All tracepoints are from kernel-space.
6318 */
6319 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006320 return 0;
6321
6322 if (!perf_tp_filter_match(event, data))
6323 return 0;
6324
6325 return 1;
6326}
6327
6328void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006329 struct pt_regs *regs, struct hlist_head *head, int rctx,
6330 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006331{
6332 struct perf_sample_data data;
6333 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006334
6335 struct perf_raw_record raw = {
6336 .size = entry_size,
6337 .data = record,
6338 };
6339
Robert Richterfd0d0002012-04-02 20:19:08 +02006340 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006341 data.raw = &raw;
6342
Sasha Levinb67bfe02013-02-27 17:06:00 -08006343 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006344 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006345 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006346 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006347
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006348 /*
6349 * If we got specified a target task, also iterate its context and
6350 * deliver this event there too.
6351 */
6352 if (task && task != current) {
6353 struct perf_event_context *ctx;
6354 struct trace_entry *entry = record;
6355
6356 rcu_read_lock();
6357 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
6358 if (!ctx)
6359 goto unlock;
6360
6361 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
6362 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6363 continue;
6364 if (event->attr.config != entry->type)
6365 continue;
6366 if (perf_tp_event_match(event, &data, regs))
6367 perf_swevent_event(event, count, &data, regs);
6368 }
6369unlock:
6370 rcu_read_unlock();
6371 }
6372
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006373 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006374}
6375EXPORT_SYMBOL_GPL(perf_tp_event);
6376
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006377static void tp_perf_event_destroy(struct perf_event *event)
6378{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006379 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006380}
6381
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006382static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006383{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006384 int err;
6385
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006386 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6387 return -ENOENT;
6388
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006389 /*
6390 * no branch sampling for tracepoint events
6391 */
6392 if (has_branch_stack(event))
6393 return -EOPNOTSUPP;
6394
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006395 err = perf_trace_init(event);
6396 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006397 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006398
6399 event->destroy = tp_perf_event_destroy;
6400
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006401 return 0;
6402}
6403
6404static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006405 .task_ctx_nr = perf_sw_context,
6406
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006407 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006408 .add = perf_trace_add,
6409 .del = perf_trace_del,
6410 .start = perf_swevent_start,
6411 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006412 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006413};
6414
6415static inline void perf_tp_register(void)
6416{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006417 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006418}
Li Zefan6fb29152009-10-15 11:21:42 +08006419
6420static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6421{
6422 char *filter_str;
6423 int ret;
6424
6425 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6426 return -EINVAL;
6427
6428 filter_str = strndup_user(arg, PAGE_SIZE);
6429 if (IS_ERR(filter_str))
6430 return PTR_ERR(filter_str);
6431
6432 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
6433
6434 kfree(filter_str);
6435 return ret;
6436}
6437
6438static void perf_event_free_filter(struct perf_event *event)
6439{
6440 ftrace_profile_free_filter(event);
6441}
6442
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006443#else
Li Zefan6fb29152009-10-15 11:21:42 +08006444
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006445static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006446{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006447}
Li Zefan6fb29152009-10-15 11:21:42 +08006448
6449static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6450{
6451 return -ENOENT;
6452}
6453
6454static void perf_event_free_filter(struct perf_event *event)
6455{
6456}
6457
Li Zefan07b139c2009-12-21 14:27:35 +08006458#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006459
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006460#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006461void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006462{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006463 struct perf_sample_data sample;
6464 struct pt_regs *regs = data;
6465
Robert Richterfd0d0002012-04-02 20:19:08 +02006466 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006467
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006468 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006469 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006470}
6471#endif
6472
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006473/*
6474 * hrtimer based swevent callback
6475 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006476
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006477static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006478{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006479 enum hrtimer_restart ret = HRTIMER_RESTART;
6480 struct perf_sample_data data;
6481 struct pt_regs *regs;
6482 struct perf_event *event;
6483 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006484
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006485 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006486
6487 if (event->state != PERF_EVENT_STATE_ACTIVE)
6488 return HRTIMER_NORESTART;
6489
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006490 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006491
Robert Richterfd0d0002012-04-02 20:19:08 +02006492 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006493 regs = get_irq_regs();
6494
6495 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08006496 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02006497 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006498 ret = HRTIMER_NORESTART;
6499 }
6500
6501 period = max_t(u64, 10000, event->hw.sample_period);
6502 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
6503
6504 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006505}
6506
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006507static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006508{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006509 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006510 s64 period;
6511
6512 if (!is_sampling_event(event))
6513 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006514
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006515 period = local64_read(&hwc->period_left);
6516 if (period) {
6517 if (period < 0)
6518 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006519
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006520 local64_set(&hwc->period_left, 0);
6521 } else {
6522 period = max_t(u64, 10000, hwc->sample_period);
6523 }
6524 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006525 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006526 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006527}
6528
6529static void perf_swevent_cancel_hrtimer(struct perf_event *event)
6530{
6531 struct hw_perf_event *hwc = &event->hw;
6532
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006533 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006534 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006535 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006536
6537 hrtimer_cancel(&hwc->hrtimer);
6538 }
6539}
6540
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006541static void perf_swevent_init_hrtimer(struct perf_event *event)
6542{
6543 struct hw_perf_event *hwc = &event->hw;
6544
6545 if (!is_sampling_event(event))
6546 return;
6547
6548 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6549 hwc->hrtimer.function = perf_swevent_hrtimer;
6550
6551 /*
6552 * Since hrtimers have a fixed rate, we can do a static freq->period
6553 * mapping and avoid the whole period adjust feedback stuff.
6554 */
6555 if (event->attr.freq) {
6556 long freq = event->attr.sample_freq;
6557
6558 event->attr.sample_period = NSEC_PER_SEC / freq;
6559 hwc->sample_period = event->attr.sample_period;
6560 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09006561 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006562 event->attr.freq = 0;
6563 }
6564}
6565
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006566/*
6567 * Software event: cpu wall time clock
6568 */
6569
6570static void cpu_clock_event_update(struct perf_event *event)
6571{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006572 s64 prev;
6573 u64 now;
6574
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006575 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006576 prev = local64_xchg(&event->hw.prev_count, now);
6577 local64_add(now - prev, &event->count);
6578}
6579
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006580static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006581{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006582 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006583 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006584}
6585
6586static void cpu_clock_event_stop(struct perf_event *event, int flags)
6587{
6588 perf_swevent_cancel_hrtimer(event);
6589 cpu_clock_event_update(event);
6590}
6591
6592static int cpu_clock_event_add(struct perf_event *event, int flags)
6593{
6594 if (flags & PERF_EF_START)
6595 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006596
6597 return 0;
6598}
6599
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006600static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006601{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006602 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006603}
6604
6605static void cpu_clock_event_read(struct perf_event *event)
6606{
6607 cpu_clock_event_update(event);
6608}
6609
6610static int cpu_clock_event_init(struct perf_event *event)
6611{
6612 if (event->attr.type != PERF_TYPE_SOFTWARE)
6613 return -ENOENT;
6614
6615 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
6616 return -ENOENT;
6617
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006618 /*
6619 * no branch sampling for software events
6620 */
6621 if (has_branch_stack(event))
6622 return -EOPNOTSUPP;
6623
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006624 perf_swevent_init_hrtimer(event);
6625
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006626 return 0;
6627}
6628
6629static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006630 .task_ctx_nr = perf_sw_context,
6631
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006632 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006633 .add = cpu_clock_event_add,
6634 .del = cpu_clock_event_del,
6635 .start = cpu_clock_event_start,
6636 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006637 .read = cpu_clock_event_read,
6638};
6639
6640/*
6641 * Software event: task time clock
6642 */
6643
6644static void task_clock_event_update(struct perf_event *event, u64 now)
6645{
6646 u64 prev;
6647 s64 delta;
6648
6649 prev = local64_xchg(&event->hw.prev_count, now);
6650 delta = now - prev;
6651 local64_add(delta, &event->count);
6652}
6653
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006654static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006655{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006656 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006657 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006658}
6659
6660static void task_clock_event_stop(struct perf_event *event, int flags)
6661{
6662 perf_swevent_cancel_hrtimer(event);
6663 task_clock_event_update(event, event->ctx->time);
6664}
6665
6666static int task_clock_event_add(struct perf_event *event, int flags)
6667{
6668 if (flags & PERF_EF_START)
6669 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006670
6671 return 0;
6672}
6673
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006674static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006675{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006676 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006677}
6678
6679static void task_clock_event_read(struct perf_event *event)
6680{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01006681 u64 now = perf_clock();
6682 u64 delta = now - event->ctx->timestamp;
6683 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006684
6685 task_clock_event_update(event, time);
6686}
6687
6688static int task_clock_event_init(struct perf_event *event)
6689{
6690 if (event->attr.type != PERF_TYPE_SOFTWARE)
6691 return -ENOENT;
6692
6693 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6694 return -ENOENT;
6695
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006696 /*
6697 * no branch sampling for software events
6698 */
6699 if (has_branch_stack(event))
6700 return -EOPNOTSUPP;
6701
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006702 perf_swevent_init_hrtimer(event);
6703
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006704 return 0;
6705}
6706
6707static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006708 .task_ctx_nr = perf_sw_context,
6709
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006710 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006711 .add = task_clock_event_add,
6712 .del = task_clock_event_del,
6713 .start = task_clock_event_start,
6714 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006715 .read = task_clock_event_read,
6716};
6717
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006718static void perf_pmu_nop_void(struct pmu *pmu)
6719{
6720}
6721
6722static int perf_pmu_nop_int(struct pmu *pmu)
6723{
6724 return 0;
6725}
6726
6727static void perf_pmu_start_txn(struct pmu *pmu)
6728{
6729 perf_pmu_disable(pmu);
6730}
6731
6732static int perf_pmu_commit_txn(struct pmu *pmu)
6733{
6734 perf_pmu_enable(pmu);
6735 return 0;
6736}
6737
6738static void perf_pmu_cancel_txn(struct pmu *pmu)
6739{
6740 perf_pmu_enable(pmu);
6741}
6742
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006743static int perf_event_idx_default(struct perf_event *event)
6744{
Peter Zijlstrac719f562014-10-21 11:10:21 +02006745 return 0;
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006746}
6747
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006748/*
6749 * Ensures all contexts with the same task_ctx_nr have the same
6750 * pmu_cpu_context too.
6751 */
Mark Rutland9e317042014-02-10 17:44:18 +00006752static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006753{
6754 struct pmu *pmu;
6755
6756 if (ctxn < 0)
6757 return NULL;
6758
6759 list_for_each_entry(pmu, &pmus, entry) {
6760 if (pmu->task_ctx_nr == ctxn)
6761 return pmu->pmu_cpu_context;
6762 }
6763
6764 return NULL;
6765}
6766
Peter Zijlstra51676952010-12-07 14:18:20 +01006767static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006768{
Peter Zijlstra51676952010-12-07 14:18:20 +01006769 int cpu;
6770
6771 for_each_possible_cpu(cpu) {
6772 struct perf_cpu_context *cpuctx;
6773
6774 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6775
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006776 if (cpuctx->unique_pmu == old_pmu)
6777 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01006778 }
6779}
6780
6781static void free_pmu_context(struct pmu *pmu)
6782{
6783 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006784
6785 mutex_lock(&pmus_lock);
6786 /*
6787 * Like a real lame refcount.
6788 */
Peter Zijlstra51676952010-12-07 14:18:20 +01006789 list_for_each_entry(i, &pmus, entry) {
6790 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6791 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006792 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01006793 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006794 }
6795
Peter Zijlstra51676952010-12-07 14:18:20 +01006796 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006797out:
6798 mutex_unlock(&pmus_lock);
6799}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006800static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006801
Peter Zijlstraabe43402010-11-17 23:17:37 +01006802static ssize_t
6803type_show(struct device *dev, struct device_attribute *attr, char *page)
6804{
6805 struct pmu *pmu = dev_get_drvdata(dev);
6806
6807 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6808}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006809static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006810
Stephane Eranian62b85632013-04-03 14:21:34 +02006811static ssize_t
6812perf_event_mux_interval_ms_show(struct device *dev,
6813 struct device_attribute *attr,
6814 char *page)
6815{
6816 struct pmu *pmu = dev_get_drvdata(dev);
6817
6818 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6819}
6820
6821static ssize_t
6822perf_event_mux_interval_ms_store(struct device *dev,
6823 struct device_attribute *attr,
6824 const char *buf, size_t count)
6825{
6826 struct pmu *pmu = dev_get_drvdata(dev);
6827 int timer, cpu, ret;
6828
6829 ret = kstrtoint(buf, 0, &timer);
6830 if (ret)
6831 return ret;
6832
6833 if (timer < 1)
6834 return -EINVAL;
6835
6836 /* same value, noting to do */
6837 if (timer == pmu->hrtimer_interval_ms)
6838 return count;
6839
6840 pmu->hrtimer_interval_ms = timer;
6841
6842 /* update all cpuctx for this PMU */
6843 for_each_possible_cpu(cpu) {
6844 struct perf_cpu_context *cpuctx;
6845 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6846 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6847
6848 if (hrtimer_active(&cpuctx->hrtimer))
6849 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6850 }
6851
6852 return count;
6853}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006854static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02006855
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006856static struct attribute *pmu_dev_attrs[] = {
6857 &dev_attr_type.attr,
6858 &dev_attr_perf_event_mux_interval_ms.attr,
6859 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006860};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006861ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006862
6863static int pmu_bus_running;
6864static struct bus_type pmu_bus = {
6865 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006866 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006867};
6868
6869static void pmu_dev_release(struct device *dev)
6870{
6871 kfree(dev);
6872}
6873
6874static int pmu_dev_alloc(struct pmu *pmu)
6875{
6876 int ret = -ENOMEM;
6877
6878 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6879 if (!pmu->dev)
6880 goto out;
6881
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01006882 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01006883 device_initialize(pmu->dev);
6884 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6885 if (ret)
6886 goto free_dev;
6887
6888 dev_set_drvdata(pmu->dev, pmu);
6889 pmu->dev->bus = &pmu_bus;
6890 pmu->dev->release = pmu_dev_release;
6891 ret = device_add(pmu->dev);
6892 if (ret)
6893 goto free_dev;
6894
6895out:
6896 return ret;
6897
6898free_dev:
6899 put_device(pmu->dev);
6900 goto out;
6901}
6902
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006903static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006904static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006905
Mischa Jonker03d8e802013-06-04 11:45:48 +02006906int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006907{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006908 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006909
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006910 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006911 ret = -ENOMEM;
6912 pmu->pmu_disable_count = alloc_percpu(int);
6913 if (!pmu->pmu_disable_count)
6914 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006915
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006916 pmu->type = -1;
6917 if (!name)
6918 goto skip_type;
6919 pmu->name = name;
6920
6921 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08006922 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6923 if (type < 0) {
6924 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006925 goto free_pdc;
6926 }
6927 }
6928 pmu->type = type;
6929
Peter Zijlstraabe43402010-11-17 23:17:37 +01006930 if (pmu_bus_running) {
6931 ret = pmu_dev_alloc(pmu);
6932 if (ret)
6933 goto free_idr;
6934 }
6935
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006936skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006937 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6938 if (pmu->pmu_cpu_context)
6939 goto got_cpu_context;
6940
Wei Yongjunc4814202013-04-12 11:05:54 +08006941 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006942 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6943 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006944 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006945
6946 for_each_possible_cpu(cpu) {
6947 struct perf_cpu_context *cpuctx;
6948
6949 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006950 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006951 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006952 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006953 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02006954
6955 __perf_cpu_hrtimer_init(cpuctx, cpu);
6956
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006957 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006958 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006959 }
6960
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006961got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006962 if (!pmu->start_txn) {
6963 if (pmu->pmu_enable) {
6964 /*
6965 * If we have pmu_enable/pmu_disable calls, install
6966 * transaction stubs that use that to try and batch
6967 * hardware accesses.
6968 */
6969 pmu->start_txn = perf_pmu_start_txn;
6970 pmu->commit_txn = perf_pmu_commit_txn;
6971 pmu->cancel_txn = perf_pmu_cancel_txn;
6972 } else {
6973 pmu->start_txn = perf_pmu_nop_void;
6974 pmu->commit_txn = perf_pmu_nop_int;
6975 pmu->cancel_txn = perf_pmu_nop_void;
6976 }
6977 }
6978
6979 if (!pmu->pmu_enable) {
6980 pmu->pmu_enable = perf_pmu_nop_void;
6981 pmu->pmu_disable = perf_pmu_nop_void;
6982 }
6983
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006984 if (!pmu->event_idx)
6985 pmu->event_idx = perf_event_idx_default;
6986
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006987 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006988 ret = 0;
6989unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006990 mutex_unlock(&pmus_lock);
6991
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006992 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006993
Peter Zijlstraabe43402010-11-17 23:17:37 +01006994free_dev:
6995 device_del(pmu->dev);
6996 put_device(pmu->dev);
6997
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006998free_idr:
6999 if (pmu->type >= PERF_TYPE_MAX)
7000 idr_remove(&pmu_idr, pmu->type);
7001
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007002free_pdc:
7003 free_percpu(pmu->pmu_disable_count);
7004 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007005}
Yan, Zhengc464c762014-03-18 16:56:41 +08007006EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007007
7008void perf_pmu_unregister(struct pmu *pmu)
7009{
7010 mutex_lock(&pmus_lock);
7011 list_del_rcu(&pmu->entry);
7012 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007013
7014 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02007015 * We dereference the pmu list under both SRCU and regular RCU, so
7016 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007017 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007018 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02007019 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007020
Peter Zijlstra33696fc2010-06-14 08:49:00 +02007021 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007022 if (pmu->type >= PERF_TYPE_MAX)
7023 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01007024 device_del(pmu->dev);
7025 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01007026 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007027}
Yan, Zhengc464c762014-03-18 16:56:41 +08007028EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007029
Mark Rutlandcc34b982015-01-07 14:56:51 +00007030static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
7031{
7032 int ret;
7033
7034 if (!try_module_get(pmu->module))
7035 return -ENODEV;
7036 event->pmu = pmu;
7037 ret = pmu->event_init(event);
7038 if (ret)
7039 module_put(pmu->module);
7040
7041 return ret;
7042}
7043
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007044struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007045{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007046 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007047 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08007048 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007049
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007050 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007051
7052 rcu_read_lock();
7053 pmu = idr_find(&pmu_idr, event->attr.type);
7054 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08007055 if (pmu) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007056 ret = perf_try_init_event(pmu, event);
Lin Ming940c5b22011-02-27 21:13:31 +08007057 if (ret)
7058 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007059 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08007060 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007061
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007062 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutlandcc34b982015-01-07 14:56:51 +00007063 ret = perf_try_init_event(pmu, event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007064 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007065 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007066
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007067 if (ret != -ENOENT) {
7068 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007069 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007070 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007071 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02007072 pmu = ERR_PTR(-ENOENT);
7073unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007074 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007075
7076 return pmu;
7077}
7078
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007079static void account_event_cpu(struct perf_event *event, int cpu)
7080{
7081 if (event->parent)
7082 return;
7083
7084 if (has_branch_stack(event)) {
7085 if (!(event->attach_state & PERF_ATTACH_TASK))
7086 atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
7087 }
7088 if (is_cgroup_event(event))
7089 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
7090}
7091
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007092static void account_event(struct perf_event *event)
7093{
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007094 if (event->parent)
7095 return;
7096
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007097 if (event->attach_state & PERF_ATTACH_TASK)
7098 static_key_slow_inc(&perf_sched_events.key);
7099 if (event->attr.mmap || event->attr.mmap_data)
7100 atomic_inc(&nr_mmap_events);
7101 if (event->attr.comm)
7102 atomic_inc(&nr_comm_events);
7103 if (event->attr.task)
7104 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02007105 if (event->attr.freq) {
7106 if (atomic_inc_return(&nr_freq_events) == 1)
7107 tick_nohz_full_kick_all();
7108 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007109 if (has_branch_stack(event))
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007110 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007111 if (is_cgroup_event(event))
7112 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007113
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02007114 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007115}
7116
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007117/*
7118 * Allocate and initialize a event structure
7119 */
7120static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007121perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007122 struct task_struct *task,
7123 struct perf_event *group_leader,
7124 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007125 perf_overflow_handler_t overflow_handler,
7126 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007127{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02007128 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007129 struct perf_event *event;
7130 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007131 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007132
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007133 if ((unsigned)cpu >= nr_cpu_ids) {
7134 if (!task || cpu != -1)
7135 return ERR_PTR(-EINVAL);
7136 }
7137
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007138 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007139 if (!event)
7140 return ERR_PTR(-ENOMEM);
7141
7142 /*
7143 * Single events are their own group leaders, with an
7144 * empty sibling list:
7145 */
7146 if (!group_leader)
7147 group_leader = event;
7148
7149 mutex_init(&event->child_mutex);
7150 INIT_LIST_HEAD(&event->child_list);
7151
7152 INIT_LIST_HEAD(&event->group_entry);
7153 INIT_LIST_HEAD(&event->event_entry);
7154 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007155 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01007156 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01007157 INIT_HLIST_NODE(&event->hlist_entry);
7158
Peter Zijlstra10c6db12011-11-26 02:47:31 +01007159
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007160 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08007161 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007162
7163 mutex_init(&event->mmap_mutex);
7164
Al Viroa6fa9412012-08-20 14:59:25 +01007165 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007166 event->cpu = cpu;
7167 event->attr = *attr;
7168 event->group_leader = group_leader;
7169 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007170 event->oncpu = -1;
7171
7172 event->parent = parent_event;
7173
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08007174 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007175 event->id = atomic64_inc_return(&perf_event_id);
7176
7177 event->state = PERF_EVENT_STATE_INACTIVE;
7178
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007179 if (task) {
7180 event->attach_state = PERF_ATTACH_TASK;
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01007181
7182 if (attr->type == PERF_TYPE_TRACEPOINT)
7183 event->hw.tp_target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007184#ifdef CONFIG_HAVE_HW_BREAKPOINT
7185 /*
7186 * hw_breakpoint is a bit difficult here..
7187 */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01007188 else if (attr->type == PERF_TYPE_BREAKPOINT)
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007189 event->hw.bp_target = task;
7190#endif
7191 }
7192
Avi Kivity4dc0da82011-06-29 18:42:35 +03007193 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007194 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007195 context = parent_event->overflow_handler_context;
7196 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01007197
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01007198 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007199 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02007200
Jiri Olsa0231bb52013-02-01 11:23:45 +01007201 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007202
7203 pmu = NULL;
7204
7205 hwc = &event->hw;
7206 hwc->sample_period = attr->sample_period;
7207 if (attr->freq && attr->sample_freq)
7208 hwc->sample_period = 1;
7209 hwc->last_period = hwc->sample_period;
7210
Peter Zijlstrae7850592010-05-21 14:43:08 +02007211 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007212
7213 /*
7214 * we currently do not support PERF_FORMAT_GROUP on inherited events
7215 */
7216 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007217 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007218
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007219 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007220 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007221 goto err_ns;
7222 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007223 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007224 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007225 }
7226
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007227 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007228 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
7229 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007230 if (err)
7231 goto err_pmu;
Stephane Eraniand010b332012-02-09 23:21:00 +01007232 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007233 }
7234
7235 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007236
7237err_pmu:
7238 if (event->destroy)
7239 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08007240 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007241err_ns:
7242 if (event->ns)
7243 put_pid_ns(event->ns);
7244 kfree(event);
7245
7246 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007247}
7248
7249static int perf_copy_attr(struct perf_event_attr __user *uattr,
7250 struct perf_event_attr *attr)
7251{
7252 u32 size;
7253 int ret;
7254
7255 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
7256 return -EFAULT;
7257
7258 /*
7259 * zero the full structure, so that a short copy will be nice.
7260 */
7261 memset(attr, 0, sizeof(*attr));
7262
7263 ret = get_user(size, &uattr->size);
7264 if (ret)
7265 return ret;
7266
7267 if (size > PAGE_SIZE) /* silly large */
7268 goto err_size;
7269
7270 if (!size) /* abi compat */
7271 size = PERF_ATTR_SIZE_VER0;
7272
7273 if (size < PERF_ATTR_SIZE_VER0)
7274 goto err_size;
7275
7276 /*
7277 * If we're handed a bigger struct than we know of,
7278 * ensure all the unknown bits are 0 - i.e. new
7279 * user-space does not rely on any kernel feature
7280 * extensions we dont know about yet.
7281 */
7282 if (size > sizeof(*attr)) {
7283 unsigned char __user *addr;
7284 unsigned char __user *end;
7285 unsigned char val;
7286
7287 addr = (void __user *)uattr + sizeof(*attr);
7288 end = (void __user *)uattr + size;
7289
7290 for (; addr < end; addr++) {
7291 ret = get_user(val, addr);
7292 if (ret)
7293 return ret;
7294 if (val)
7295 goto err_size;
7296 }
7297 size = sizeof(*attr);
7298 }
7299
7300 ret = copy_from_user(attr, uattr, size);
7301 if (ret)
7302 return -EFAULT;
7303
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05307304 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007305 return -EINVAL;
7306
7307 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
7308 return -EINVAL;
7309
7310 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
7311 return -EINVAL;
7312
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007313 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
7314 u64 mask = attr->branch_sample_type;
7315
7316 /* only using defined bits */
7317 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
7318 return -EINVAL;
7319
7320 /* at least one branch bit must be set */
7321 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
7322 return -EINVAL;
7323
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007324 /* propagate priv level, when not set for branch */
7325 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
7326
7327 /* exclude_kernel checked on syscall entry */
7328 if (!attr->exclude_kernel)
7329 mask |= PERF_SAMPLE_BRANCH_KERNEL;
7330
7331 if (!attr->exclude_user)
7332 mask |= PERF_SAMPLE_BRANCH_USER;
7333
7334 if (!attr->exclude_hv)
7335 mask |= PERF_SAMPLE_BRANCH_HV;
7336 /*
7337 * adjust user setting (for HW filter setup)
7338 */
7339 attr->branch_sample_type = mask;
7340 }
Stephane Eraniane7122092013-06-06 11:02:04 +02007341 /* privileged levels capture (kernel, hv): check permissions */
7342 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02007343 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7344 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007345 }
Jiri Olsa40189942012-08-07 15:20:37 +02007346
Jiri Olsac5ebced2012-08-07 15:20:40 +02007347 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02007348 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02007349 if (ret)
7350 return ret;
7351 }
7352
7353 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
7354 if (!arch_perf_have_user_stack_dump())
7355 return -ENOSYS;
7356
7357 /*
7358 * We have __u32 type for the size, but so far
7359 * we can only use __u16 as maximum due to the
7360 * __u16 sample size limit.
7361 */
7362 if (attr->sample_stack_user >= USHRT_MAX)
7363 ret = -EINVAL;
7364 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
7365 ret = -EINVAL;
7366 }
Jiri Olsa40189942012-08-07 15:20:37 +02007367
Stephane Eranian60e23642014-09-24 13:48:37 +02007368 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
7369 ret = perf_reg_validate(attr->sample_regs_intr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007370out:
7371 return ret;
7372
7373err_size:
7374 put_user(sizeof(*attr), &uattr->size);
7375 ret = -E2BIG;
7376 goto out;
7377}
7378
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007379static int
7380perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007381{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007382 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007383 int ret = -EINVAL;
7384
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007385 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007386 goto set;
7387
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007388 /* don't allow circular references */
7389 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007390 goto out;
7391
Peter Zijlstra0f139302010-05-20 14:35:15 +02007392 /*
7393 * Don't allow cross-cpu buffers
7394 */
7395 if (output_event->cpu != event->cpu)
7396 goto out;
7397
7398 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02007399 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02007400 */
7401 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
7402 goto out;
7403
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007404set:
7405 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007406 /* Can't redirect output if we've got an active mmap() */
7407 if (atomic_read(&event->mmap_count))
7408 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007409
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007410 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02007411 /* get the rb we want to redirect to */
7412 rb = ring_buffer_get(output_event);
7413 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007414 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007415 }
7416
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007417 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02007418
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007419 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007420unlock:
7421 mutex_unlock(&event->mmap_mutex);
7422
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007423out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007424 return ret;
7425}
7426
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007427static void mutex_lock_double(struct mutex *a, struct mutex *b)
7428{
7429 if (b < a)
7430 swap(a, b);
7431
7432 mutex_lock(a);
7433 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
7434}
7435
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007436/**
7437 * sys_perf_event_open - open a performance event, associate it to a task/cpu
7438 *
7439 * @attr_uptr: event_id type attributes for monitoring/sampling
7440 * @pid: target pid
7441 * @cpu: target cpu
7442 * @group_fd: group leader event fd
7443 */
7444SYSCALL_DEFINE5(perf_event_open,
7445 struct perf_event_attr __user *, attr_uptr,
7446 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
7447{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007448 struct perf_event *group_leader = NULL, *output_event = NULL;
7449 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007450 struct perf_event_attr attr;
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007451 struct perf_event_context *ctx, *uninitialized_var(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007452 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04007453 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07007454 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007455 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04007456 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007457 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007458 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01007459 int f_flags = O_RDWR;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007460
7461 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02007462 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007463 return -EINVAL;
7464
7465 err = perf_copy_attr(attr_uptr, &attr);
7466 if (err)
7467 return err;
7468
7469 if (!attr.exclude_kernel) {
7470 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7471 return -EACCES;
7472 }
7473
7474 if (attr.freq) {
7475 if (attr.sample_freq > sysctl_perf_event_sample_rate)
7476 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02007477 } else {
7478 if (attr.sample_period & (1ULL << 63))
7479 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007480 }
7481
Stephane Eraniane5d13672011-02-14 11:20:01 +02007482 /*
7483 * In cgroup mode, the pid argument is used to pass the fd
7484 * opened to the cgroup directory in cgroupfs. The cpu argument
7485 * designates the cpu on which to monitor threads from that
7486 * cgroup.
7487 */
7488 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
7489 return -EINVAL;
7490
Yann Droneauda21b0b32014-01-05 21:36:33 +01007491 if (flags & PERF_FLAG_FD_CLOEXEC)
7492 f_flags |= O_CLOEXEC;
7493
7494 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007495 if (event_fd < 0)
7496 return event_fd;
7497
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007498 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04007499 err = perf_fget_light(group_fd, &group);
7500 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007501 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04007502 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007503 if (flags & PERF_FLAG_FD_OUTPUT)
7504 output_event = group_leader;
7505 if (flags & PERF_FLAG_FD_NO_GROUP)
7506 group_leader = NULL;
7507 }
7508
Stephane Eraniane5d13672011-02-14 11:20:01 +02007509 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007510 task = find_lively_task_by_vpid(pid);
7511 if (IS_ERR(task)) {
7512 err = PTR_ERR(task);
7513 goto err_group_fd;
7514 }
7515 }
7516
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007517 if (task && group_leader &&
7518 group_leader->attr.inherit != attr.inherit) {
7519 err = -EINVAL;
7520 goto err_task;
7521 }
7522
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007523 get_online_cpus();
7524
Avi Kivity4dc0da82011-06-29 18:42:35 +03007525 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
7526 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007527 if (IS_ERR(event)) {
7528 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007529 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007530 }
7531
Stephane Eraniane5d13672011-02-14 11:20:01 +02007532 if (flags & PERF_FLAG_PID_CGROUP) {
7533 err = perf_cgroup_connect(pid, event, &attr, group_leader);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007534 if (err) {
7535 __free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007536 goto err_cpus;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007537 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02007538 }
7539
Vince Weaver53b25332014-05-16 17:12:12 -04007540 if (is_sampling_event(event)) {
7541 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
7542 err = -ENOTSUPP;
7543 goto err_alloc;
7544 }
7545 }
7546
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007547 account_event(event);
7548
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007549 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007550 * Special case software events and allow them to be part of
7551 * any hardware group.
7552 */
7553 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007554
7555 if (group_leader &&
7556 (is_software_event(event) != is_software_event(group_leader))) {
7557 if (is_software_event(event)) {
7558 /*
7559 * If event and group_leader are not both a software
7560 * event, and event is, then group leader is not.
7561 *
7562 * Allow the addition of software events to !software
7563 * groups, this is safe because software events never
7564 * fail to schedule.
7565 */
7566 pmu = group_leader->pmu;
7567 } else if (is_software_event(group_leader) &&
7568 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
7569 /*
7570 * In case the group is a pure software group, and we
7571 * try to add a hardware event, move the whole group to
7572 * the hardware context.
7573 */
7574 move_group = 1;
7575 }
7576 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007577
7578 /*
7579 * Get the target context (task or percpu):
7580 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007581 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007582 if (IS_ERR(ctx)) {
7583 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007584 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007585 }
7586
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02007587 if (task) {
7588 put_task_struct(task);
7589 task = NULL;
7590 }
7591
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007592 /*
7593 * Look up the group leader (we will attach this event to it):
7594 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007595 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007596 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007597
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007598 /*
7599 * Do not allow a recursive hierarchy (this new sibling
7600 * becoming part of another group-sibling):
7601 */
7602 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007603 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007604 /*
7605 * Do not allow to attach to a group in a different
7606 * task or CPU context:
7607 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007608 if (move_group) {
Peter Zijlstrac3c87e72015-01-23 11:19:48 +01007609 /*
7610 * Make sure we're both on the same task, or both
7611 * per-cpu events.
7612 */
7613 if (group_leader->ctx->task != ctx->task)
7614 goto err_context;
7615
7616 /*
7617 * Make sure we're both events for the same CPU;
7618 * grouping events for different CPUs is broken; since
7619 * you can never concurrently schedule them anyhow.
7620 */
7621 if (group_leader->cpu != event->cpu)
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007622 goto err_context;
7623 } else {
7624 if (group_leader->ctx != ctx)
7625 goto err_context;
7626 }
7627
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007628 /*
7629 * Only a group leader can be exclusive or pinned
7630 */
7631 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007632 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007633 }
7634
7635 if (output_event) {
7636 err = perf_event_set_output(event, output_event);
7637 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007638 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007639 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007640
Yann Droneauda21b0b32014-01-05 21:36:33 +01007641 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
7642 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007643 if (IS_ERR(event_file)) {
7644 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007645 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04007646 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007647
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007648 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007649 gctx = group_leader->ctx;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007650
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007651 /*
7652 * See perf_event_ctx_lock() for comments on the details
7653 * of swizzling perf_event::ctx.
7654 */
7655 mutex_lock_double(&gctx->mutex, &ctx->mutex);
7656
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007657 perf_remove_from_context(group_leader, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007658
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007659 list_for_each_entry(sibling, &group_leader->sibling_list,
7660 group_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007661 perf_remove_from_context(sibling, false);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007662 put_ctx(gctx);
7663 }
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007664 } else {
7665 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007666 }
7667
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007668 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007669
7670 if (move_group) {
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007671 /*
7672 * Wait for everybody to stop referencing the events through
7673 * the old lists, before installing it on new lists.
7674 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007675 synchronize_rcu();
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007676
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007677 /*
7678 * Install the group siblings before the group leader.
7679 *
7680 * Because a group leader will try and install the entire group
7681 * (through the sibling list, which is still in-tact), we can
7682 * end up with siblings installed in the wrong context.
7683 *
7684 * By installing siblings first we NO-OP because they're not
7685 * reachable through the group lists.
7686 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007687 list_for_each_entry(sibling, &group_leader->sibling_list,
7688 group_entry) {
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007689 perf_event__state_init(sibling);
Jiri Olsa9fc81d82014-12-10 21:23:51 +01007690 perf_install_in_context(ctx, sibling, sibling->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007691 get_ctx(ctx);
7692 }
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007693
7694 /*
7695 * Removing from the context ends up with disabled
7696 * event. What we want here is event in the initial
7697 * startup state, ready to be add into new context.
7698 */
7699 perf_event__state_init(group_leader);
7700 perf_install_in_context(ctx, group_leader, group_leader->cpu);
7701 get_ctx(ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007702 }
7703
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007704 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007705 perf_unpin_context(ctx);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007706
7707 if (move_group) {
7708 mutex_unlock(&gctx->mutex);
7709 put_ctx(gctx);
7710 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007711 mutex_unlock(&ctx->mutex);
7712
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007713 put_online_cpus();
7714
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007715 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01007716
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007717 mutex_lock(&current->perf_event_mutex);
7718 list_add_tail(&event->owner_entry, &current->perf_event_list);
7719 mutex_unlock(&current->perf_event_mutex);
7720
Peter Zijlstra8a495422010-05-27 15:47:49 +02007721 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007722 * Precalculate sample_data sizes
7723 */
7724 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007725 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007726
7727 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02007728 * Drop the reference on the group_event after placing the
7729 * new event on the sibling_list. This ensures destruction
7730 * of the group leader will find the pointer to itself in
7731 * perf_group_detach().
7732 */
Al Viro2903ff02012-08-28 12:52:22 -04007733 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007734 fd_install(event_fd, event_file);
7735 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007736
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007737err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007738 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04007739 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007740err_alloc:
7741 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007742err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007743 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007744err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007745 if (task)
7746 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007747err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04007748 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007749err_fd:
7750 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007751 return err;
7752}
7753
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007754/**
7755 * perf_event_create_kernel_counter
7756 *
7757 * @attr: attributes of the counter to create
7758 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07007759 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007760 */
7761struct perf_event *
7762perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07007763 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007764 perf_overflow_handler_t overflow_handler,
7765 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007766{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007767 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007768 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007769 int err;
7770
7771 /*
7772 * Get the target context (task or percpu):
7773 */
7774
Avi Kivity4dc0da82011-06-29 18:42:35 +03007775 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7776 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007777 if (IS_ERR(event)) {
7778 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007779 goto err;
7780 }
7781
Jiri Olsaf8697762014-08-01 14:33:01 +02007782 /* Mark owner so we could distinguish it from user events. */
7783 event->owner = EVENT_OWNER_KERNEL;
7784
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007785 account_event(event);
7786
Matt Helsley38a81da2010-09-13 13:01:20 -07007787 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007788 if (IS_ERR(ctx)) {
7789 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007790 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007791 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007792
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007793 WARN_ON_ONCE(ctx->parent_ctx);
7794 mutex_lock(&ctx->mutex);
7795 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007796 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007797 mutex_unlock(&ctx->mutex);
7798
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007799 return event;
7800
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007801err_free:
7802 free_event(event);
7803err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007804 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007805}
7806EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7807
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007808void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7809{
7810 struct perf_event_context *src_ctx;
7811 struct perf_event_context *dst_ctx;
7812 struct perf_event *event, *tmp;
7813 LIST_HEAD(events);
7814
7815 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7816 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7817
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007818 /*
7819 * See perf_event_ctx_lock() for comments on the details
7820 * of swizzling perf_event::ctx.
7821 */
7822 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007823 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7824 event_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007825 perf_remove_from_context(event, false);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007826 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007827 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02007828 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007829 }
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007830
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007831 /*
7832 * Wait for the events to quiesce before re-instating them.
7833 */
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007834 synchronize_rcu();
7835
Peter Zijlstra (Intel)8f95b432015-01-27 11:53:12 +01007836 /*
7837 * Re-instate events in 2 passes.
7838 *
7839 * Skip over group leaders and only install siblings on this first
7840 * pass, siblings will not get enabled without a leader, however a
7841 * leader will enable its siblings, even if those are still on the old
7842 * context.
7843 */
7844 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7845 if (event->group_leader == event)
7846 continue;
7847
7848 list_del(&event->migrate_entry);
7849 if (event->state >= PERF_EVENT_STATE_OFF)
7850 event->state = PERF_EVENT_STATE_INACTIVE;
7851 account_event_cpu(event, dst_cpu);
7852 perf_install_in_context(dst_ctx, event, dst_cpu);
7853 get_ctx(dst_ctx);
7854 }
7855
7856 /*
7857 * Once all the siblings are setup properly, install the group leaders
7858 * to make it go.
7859 */
Peter Zijlstra98861672013-10-03 16:02:23 +02007860 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7861 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007862 if (event->state >= PERF_EVENT_STATE_OFF)
7863 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007864 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007865 perf_install_in_context(dst_ctx, event, dst_cpu);
7866 get_ctx(dst_ctx);
7867 }
7868 mutex_unlock(&dst_ctx->mutex);
Peter Zijlstraf63a8da2015-01-23 12:24:14 +01007869 mutex_unlock(&src_ctx->mutex);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007870}
7871EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7872
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007873static void sync_child_event(struct perf_event *child_event,
7874 struct task_struct *child)
7875{
7876 struct perf_event *parent_event = child_event->parent;
7877 u64 child_val;
7878
7879 if (child_event->attr.inherit_stat)
7880 perf_event_read_event(child_event, child);
7881
Peter Zijlstrab5e58792010-05-21 14:43:12 +02007882 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007883
7884 /*
7885 * Add back the child's count to the parent's count:
7886 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02007887 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007888 atomic64_add(child_event->total_time_enabled,
7889 &parent_event->child_total_time_enabled);
7890 atomic64_add(child_event->total_time_running,
7891 &parent_event->child_total_time_running);
7892
7893 /*
7894 * Remove this event from the parent's list
7895 */
7896 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7897 mutex_lock(&parent_event->child_mutex);
7898 list_del_init(&child_event->child_list);
7899 mutex_unlock(&parent_event->child_mutex);
7900
7901 /*
Jiri Olsadc633982014-09-12 13:18:26 +02007902 * Make sure user/parent get notified, that we just
7903 * lost one event.
7904 */
7905 perf_event_wakeup(parent_event);
7906
7907 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007908 * Release the parent event, if this was the last
7909 * reference to it.
7910 */
Al Viroa6fa9412012-08-20 14:59:25 +01007911 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007912}
7913
7914static void
7915__perf_event_exit_task(struct perf_event *child_event,
7916 struct perf_event_context *child_ctx,
7917 struct task_struct *child)
7918{
Peter Zijlstra1903d502014-07-15 17:27:27 +02007919 /*
7920 * Do not destroy the 'original' grouping; because of the context
7921 * switch optimization the original events could've ended up in a
7922 * random child task.
7923 *
7924 * If we were to destroy the original group, all group related
7925 * operations would cease to function properly after this random
7926 * child dies.
7927 *
7928 * Do destroy all inherited groups, we don't care about those
7929 * and being thorough is better.
7930 */
7931 perf_remove_from_context(child_event, !!child_event->parent);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007932
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007933 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007934 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007935 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007936 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007937 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007938 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007939 sync_child_event(child_event, child);
7940 free_event(child_event);
Jiri Olsa179033b2014-08-07 11:48:26 -04007941 } else {
7942 child_event->state = PERF_EVENT_STATE_EXIT;
7943 perf_event_wakeup(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007944 }
7945}
7946
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007947static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007948{
Peter Zijlstraebf905f2014-05-29 19:00:24 +02007949 struct perf_event *child_event, *next;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02007950 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007951 unsigned long flags;
7952
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007953 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007954 perf_event_task(child, NULL, 0);
7955 return;
7956 }
7957
7958 local_irq_save(flags);
7959 /*
7960 * We can't reschedule here because interrupts are disabled,
7961 * and either child is current or it is a task that can't be
7962 * scheduled, so we are now safe from rescheduling changing
7963 * our context.
7964 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01007965 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007966
7967 /*
7968 * Take the context lock here so that if find_get_context is
7969 * reading child->perf_event_ctxp, we wait until it has
7970 * incremented the context's refcount before we do put_ctx below.
7971 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007972 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02007973 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007974 child->perf_event_ctxp[ctxn] = NULL;
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007975
7976 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007977 * If this context is a clone; unclone it so it can't get
7978 * swapped to another process while we're removing all
7979 * the events from it.
7980 */
Peter Zijlstra211de6e2014-09-30 19:23:08 +02007981 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01007982 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007983 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007984
Peter Zijlstra211de6e2014-09-30 19:23:08 +02007985 if (clone_ctx)
7986 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007987
7988 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007989 * Report the task dead after unscheduling the events so that we
7990 * won't get any samples after PERF_RECORD_EXIT. We can however still
7991 * get a few PERF_RECORD_READ events.
7992 */
7993 perf_event_task(child, child_ctx, 0);
7994
7995 /*
7996 * We can recurse on the same lock type through:
7997 *
7998 * __perf_event_exit_task()
7999 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01008000 * put_event()
8001 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008002 *
8003 * But since its the parent context it won't be the same instance.
8004 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02008005 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008006
Peter Zijlstraebf905f2014-05-29 19:00:24 +02008007 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008008 __perf_event_exit_task(child_event, child_ctx, child);
8009
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008010 mutex_unlock(&child_ctx->mutex);
8011
8012 put_ctx(child_ctx);
8013}
8014
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008015/*
8016 * When a child task exits, feed back event values to parent events.
8017 */
8018void perf_event_exit_task(struct task_struct *child)
8019{
Peter Zijlstra88821352010-11-09 19:01:43 +01008020 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008021 int ctxn;
8022
Peter Zijlstra88821352010-11-09 19:01:43 +01008023 mutex_lock(&child->perf_event_mutex);
8024 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
8025 owner_entry) {
8026 list_del_init(&event->owner_entry);
8027
8028 /*
8029 * Ensure the list deletion is visible before we clear
8030 * the owner, closes a race against perf_release() where
8031 * we need to serialize on the owner->perf_event_mutex.
8032 */
8033 smp_wmb();
8034 event->owner = NULL;
8035 }
8036 mutex_unlock(&child->perf_event_mutex);
8037
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008038 for_each_task_context_nr(ctxn)
8039 perf_event_exit_task_context(child, ctxn);
8040}
8041
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008042static void perf_free_event(struct perf_event *event,
8043 struct perf_event_context *ctx)
8044{
8045 struct perf_event *parent = event->parent;
8046
8047 if (WARN_ON_ONCE(!parent))
8048 return;
8049
8050 mutex_lock(&parent->child_mutex);
8051 list_del_init(&event->child_list);
8052 mutex_unlock(&parent->child_mutex);
8053
Al Viroa6fa9412012-08-20 14:59:25 +01008054 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008055
Peter Zijlstra652884f2015-01-23 11:20:10 +01008056 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02008057 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008058 list_del_event(event, ctx);
Peter Zijlstra652884f2015-01-23 11:20:10 +01008059 raw_spin_unlock_irq(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008060 free_event(event);
8061}
8062
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008063/*
Peter Zijlstra652884f2015-01-23 11:20:10 +01008064 * Free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008065 * perf_event_init_task below, used by fork() in case of fail.
Peter Zijlstra652884f2015-01-23 11:20:10 +01008066 *
8067 * Not all locks are strictly required, but take them anyway to be nice and
8068 * help out with the lockdep assertions.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008069 */
8070void perf_event_free_task(struct task_struct *task)
8071{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008072 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008073 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008074 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008075
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008076 for_each_task_context_nr(ctxn) {
8077 ctx = task->perf_event_ctxp[ctxn];
8078 if (!ctx)
8079 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008080
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008081 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008082again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008083 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
8084 group_entry)
8085 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008086
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008087 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
8088 group_entry)
8089 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008090
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008091 if (!list_empty(&ctx->pinned_groups) ||
8092 !list_empty(&ctx->flexible_groups))
8093 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008094
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008095 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008096
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008097 put_ctx(ctx);
8098 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008099}
8100
Peter Zijlstra4e231c72010-09-09 21:01:59 +02008101void perf_event_delayed_put(struct task_struct *task)
8102{
8103 int ctxn;
8104
8105 for_each_task_context_nr(ctxn)
8106 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
8107}
8108
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008109/*
8110 * inherit a event from parent task to child task:
8111 */
8112static struct perf_event *
8113inherit_event(struct perf_event *parent_event,
8114 struct task_struct *parent,
8115 struct perf_event_context *parent_ctx,
8116 struct task_struct *child,
8117 struct perf_event *group_leader,
8118 struct perf_event_context *child_ctx)
8119{
Jiri Olsa1929def2014-09-12 13:18:27 +02008120 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008121 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02008122 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008123
8124 /*
8125 * Instead of creating recursive hierarchies of events,
8126 * we link inherited events back to the original parent,
8127 * which has a filp for sure, which we use as the reference
8128 * count:
8129 */
8130 if (parent_event->parent)
8131 parent_event = parent_event->parent;
8132
8133 child_event = perf_event_alloc(&parent_event->attr,
8134 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02008135 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008136 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03008137 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008138 if (IS_ERR(child_event))
8139 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01008140
Jiri Olsafadfe7b2014-08-01 14:33:02 +02008141 if (is_orphaned_event(parent_event) ||
8142 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Al Viroa6fa9412012-08-20 14:59:25 +01008143 free_event(child_event);
8144 return NULL;
8145 }
8146
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008147 get_ctx(child_ctx);
8148
8149 /*
8150 * Make the child state follow the state of the parent event,
8151 * not its attr.disabled bit. We hold the parent's mutex,
8152 * so we won't race with perf_event_{en, dis}able_family.
8153 */
Jiri Olsa1929def2014-09-12 13:18:27 +02008154 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008155 child_event->state = PERF_EVENT_STATE_INACTIVE;
8156 else
8157 child_event->state = PERF_EVENT_STATE_OFF;
8158
8159 if (parent_event->attr.freq) {
8160 u64 sample_period = parent_event->hw.sample_period;
8161 struct hw_perf_event *hwc = &child_event->hw;
8162
8163 hwc->sample_period = sample_period;
8164 hwc->last_period = sample_period;
8165
8166 local64_set(&hwc->period_left, sample_period);
8167 }
8168
8169 child_event->ctx = child_ctx;
8170 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03008171 child_event->overflow_handler_context
8172 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008173
8174 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02008175 * Precalculate sample_data sizes
8176 */
8177 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02008178 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02008179
8180 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008181 * Link it up in the child's context:
8182 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02008183 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008184 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02008185 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008186
8187 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02008188 * Link this into the parent event's child list
8189 */
8190 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
8191 mutex_lock(&parent_event->child_mutex);
8192 list_add_tail(&child_event->child_list, &parent_event->child_list);
8193 mutex_unlock(&parent_event->child_mutex);
8194
8195 return child_event;
8196}
8197
8198static int inherit_group(struct perf_event *parent_event,
8199 struct task_struct *parent,
8200 struct perf_event_context *parent_ctx,
8201 struct task_struct *child,
8202 struct perf_event_context *child_ctx)
8203{
8204 struct perf_event *leader;
8205 struct perf_event *sub;
8206 struct perf_event *child_ctr;
8207
8208 leader = inherit_event(parent_event, parent, parent_ctx,
8209 child, NULL, child_ctx);
8210 if (IS_ERR(leader))
8211 return PTR_ERR(leader);
8212 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
8213 child_ctr = inherit_event(sub, parent, parent_ctx,
8214 child, leader, child_ctx);
8215 if (IS_ERR(child_ctr))
8216 return PTR_ERR(child_ctr);
8217 }
8218 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008219}
8220
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008221static int
8222inherit_task_group(struct perf_event *event, struct task_struct *parent,
8223 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008224 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008225 int *inherited_all)
8226{
8227 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008228 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008229
8230 if (!event->attr.inherit) {
8231 *inherited_all = 0;
8232 return 0;
8233 }
8234
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008235 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008236 if (!child_ctx) {
8237 /*
8238 * This is executed from the parent task context, so
8239 * inherit events that have been marked for cloning.
8240 * First allocate and initialize a context for the
8241 * child.
8242 */
8243
Jiri Olsa734df5a2013-07-09 17:44:10 +02008244 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008245 if (!child_ctx)
8246 return -ENOMEM;
8247
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008248 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008249 }
8250
8251 ret = inherit_group(event, parent, parent_ctx,
8252 child, child_ctx);
8253
8254 if (ret)
8255 *inherited_all = 0;
8256
8257 return ret;
8258}
8259
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008260/*
8261 * Initialize the perf_event context in task_struct
8262 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02008263static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008264{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008265 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008266 struct perf_event_context *cloned_ctx;
8267 struct perf_event *event;
8268 struct task_struct *parent = current;
8269 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008270 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008271 int ret = 0;
8272
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008273 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008274 return 0;
8275
8276 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008277 * If the parent's context is a clone, pin it so it won't get
8278 * swapped under us.
8279 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008280 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02008281 if (!parent_ctx)
8282 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008283
8284 /*
8285 * No need to check if parent_ctx != NULL here; since we saw
8286 * it non-NULL earlier, the only reason for it to become NULL
8287 * is if we exit, and since we're currently in the middle of
8288 * a fork we can't be exiting at the same time.
8289 */
8290
8291 /*
8292 * Lock the parent list. No need to lock the child - not PID
8293 * hashed yet and not running, so nobody can access it.
8294 */
8295 mutex_lock(&parent_ctx->mutex);
8296
8297 /*
8298 * We dont have to disable NMIs - we are only looking at
8299 * the list, not manipulating it:
8300 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008301 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008302 ret = inherit_task_group(event, parent, parent_ctx,
8303 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008304 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008305 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008306 }
8307
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008308 /*
8309 * We can't hold ctx->lock when iterating the ->flexible_group list due
8310 * to allocations, but we need to prevent rotation because
8311 * rotate_ctx() will change the list from interrupt context.
8312 */
8313 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
8314 parent_ctx->rotate_disable = 1;
8315 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
8316
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008317 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008318 ret = inherit_task_group(event, parent, parent_ctx,
8319 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008320 if (ret)
8321 break;
8322 }
8323
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008324 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
8325 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008326
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008327 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008328
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01008329 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008330 /*
8331 * Mark the child context as a clone of the parent
8332 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008333 *
8334 * Note that if the parent is a clone, the holding of
8335 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008336 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008337 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008338 if (cloned_ctx) {
8339 child_ctx->parent_ctx = cloned_ctx;
8340 child_ctx->parent_gen = parent_ctx->parent_gen;
8341 } else {
8342 child_ctx->parent_ctx = parent_ctx;
8343 child_ctx->parent_gen = parent_ctx->generation;
8344 }
8345 get_ctx(child_ctx->parent_ctx);
8346 }
8347
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008348 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008349 mutex_unlock(&parent_ctx->mutex);
8350
8351 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008352 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008353
8354 return ret;
8355}
8356
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008357/*
8358 * Initialize the perf_event context in task_struct
8359 */
8360int perf_event_init_task(struct task_struct *child)
8361{
8362 int ctxn, ret;
8363
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01008364 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
8365 mutex_init(&child->perf_event_mutex);
8366 INIT_LIST_HEAD(&child->perf_event_list);
8367
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008368 for_each_task_context_nr(ctxn) {
8369 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07008370 if (ret) {
8371 perf_event_free_task(child);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008372 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07008373 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008374 }
8375
8376 return 0;
8377}
8378
Paul Mackerras220b1402010-03-10 20:45:52 +11008379static void __init perf_event_init_all_cpus(void)
8380{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008381 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11008382 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11008383
8384 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008385 swhash = &per_cpu(swevent_htable, cpu);
8386 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02008387 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11008388 }
8389}
8390
Paul Gortmaker0db06282013-06-19 14:53:51 -04008391static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008392{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008393 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008394
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008395 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02008396 swhash->online = true;
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008397 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008398 struct swevent_hlist *hlist;
8399
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008400 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
8401 WARN_ON(!hlist);
8402 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008403 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008404 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008405}
8406
Peter Zijlstrac2774432010-12-08 15:29:02 +01008407#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02008408static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008409{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02008410 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
8411
8412 WARN_ON(!irqs_disabled());
8413
8414 list_del_init(&cpuctx->rotation_list);
8415}
8416
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008417static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008418{
Mark Rutland226424e2014-11-05 16:11:44 +00008419 struct remove_event re = { .detach_group = true };
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008420 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008421
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008422 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02008423
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008424 rcu_read_lock();
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02008425 list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
8426 __perf_remove_from_context(&re);
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008427 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008428}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008429
8430static void perf_event_exit_cpu_context(int cpu)
8431{
8432 struct perf_event_context *ctx;
8433 struct pmu *pmu;
8434 int idx;
8435
8436 idx = srcu_read_lock(&pmus_srcu);
8437 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02008438 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008439
8440 mutex_lock(&ctx->mutex);
8441 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
8442 mutex_unlock(&ctx->mutex);
8443 }
8444 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008445}
8446
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008447static void perf_event_exit_cpu(int cpu)
8448{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008449 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008450
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008451 perf_event_exit_cpu_context(cpu);
8452
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008453 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02008454 swhash->online = false;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008455 swevent_hlist_release(swhash);
8456 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008457}
8458#else
8459static inline void perf_event_exit_cpu(int cpu) { }
8460#endif
8461
Peter Zijlstrac2774432010-12-08 15:29:02 +01008462static int
8463perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
8464{
8465 int cpu;
8466
8467 for_each_online_cpu(cpu)
8468 perf_event_exit_cpu(cpu);
8469
8470 return NOTIFY_OK;
8471}
8472
8473/*
8474 * Run the perf reboot notifier at the very last possible moment so that
8475 * the generic watchdog code runs as long as possible.
8476 */
8477static struct notifier_block perf_reboot_notifier = {
8478 .notifier_call = perf_reboot,
8479 .priority = INT_MIN,
8480};
8481
Paul Gortmaker0db06282013-06-19 14:53:51 -04008482static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008483perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
8484{
8485 unsigned int cpu = (long)hcpu;
8486
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008487 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008488
8489 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02008490 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008491 perf_event_init_cpu(cpu);
8492 break;
8493
Peter Zijlstra5e116372010-06-11 13:35:08 +02008494 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008495 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008496 perf_event_exit_cpu(cpu);
8497 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008498 default:
8499 break;
8500 }
8501
8502 return NOTIFY_OK;
8503}
8504
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008505void __init perf_event_init(void)
8506{
Jason Wessel3c502e72010-11-04 17:33:01 -05008507 int ret;
8508
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008509 idr_init(&pmu_idr);
8510
Paul Mackerras220b1402010-03-10 20:45:52 +11008511 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008512 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008513 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
8514 perf_pmu_register(&perf_cpu_clock, NULL, -1);
8515 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008516 perf_tp_register();
8517 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01008518 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05008519
8520 ret = init_hw_breakpoint();
8521 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02008522
8523 /* do not patch jump label more than once per second */
8524 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01008525
8526 /*
8527 * Build time assertion that we keep the data_head at the intended
8528 * location. IOW, validation we got the __reserved[] size right.
8529 */
8530 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
8531 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008532}
Peter Zijlstraabe43402010-11-17 23:17:37 +01008533
8534static int __init perf_event_sysfs_init(void)
8535{
8536 struct pmu *pmu;
8537 int ret;
8538
8539 mutex_lock(&pmus_lock);
8540
8541 ret = bus_register(&pmu_bus);
8542 if (ret)
8543 goto unlock;
8544
8545 list_for_each_entry(pmu, &pmus, entry) {
8546 if (!pmu->name || pmu->type < 0)
8547 continue;
8548
8549 ret = pmu_dev_alloc(pmu);
8550 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
8551 }
8552 pmu_bus_running = 1;
8553 ret = 0;
8554
8555unlock:
8556 mutex_unlock(&pmus_lock);
8557
8558 return ret;
8559}
8560device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008561
8562#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04008563static struct cgroup_subsys_state *
8564perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008565{
8566 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02008567
Li Zefan1b15d052011-03-03 14:26:06 +08008568 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008569 if (!jc)
8570 return ERR_PTR(-ENOMEM);
8571
Stephane Eraniane5d13672011-02-14 11:20:01 +02008572 jc->info = alloc_percpu(struct perf_cgroup_info);
8573 if (!jc->info) {
8574 kfree(jc);
8575 return ERR_PTR(-ENOMEM);
8576 }
8577
Stephane Eraniane5d13672011-02-14 11:20:01 +02008578 return &jc->css;
8579}
8580
Tejun Heoeb954192013-08-08 20:11:23 -04008581static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008582{
Tejun Heoeb954192013-08-08 20:11:23 -04008583 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
8584
Stephane Eraniane5d13672011-02-14 11:20:01 +02008585 free_percpu(jc->info);
8586 kfree(jc);
8587}
8588
8589static int __perf_cgroup_move(void *info)
8590{
8591 struct task_struct *task = info;
8592 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
8593 return 0;
8594}
8595
Tejun Heoeb954192013-08-08 20:11:23 -04008596static void perf_cgroup_attach(struct cgroup_subsys_state *css,
8597 struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008598{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008599 struct task_struct *task;
8600
Tejun Heo924f0d92014-02-13 06:58:41 -05008601 cgroup_taskset_for_each(task, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008602 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008603}
8604
Tejun Heoeb954192013-08-08 20:11:23 -04008605static void perf_cgroup_exit(struct cgroup_subsys_state *css,
8606 struct cgroup_subsys_state *old_css,
Li Zefan761b3ef2012-01-31 13:47:36 +08008607 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008608{
8609 /*
8610 * cgroup_exit() is called in the copy_process() failure path.
8611 * Ignore this case since the task hasn't ran yet, this avoids
8612 * trying to poke a half freed task state from generic code.
8613 */
8614 if (!(task->flags & PF_EXITING))
8615 return;
8616
Tejun Heobb9d97b2011-12-12 18:12:21 -08008617 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008618}
8619
Tejun Heo073219e2014-02-08 10:36:58 -05008620struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008621 .css_alloc = perf_cgroup_css_alloc,
8622 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008623 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008624 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02008625};
8626#endif /* CONFIG_CGROUP_PERF */