blob: bbb3ca22f07c2d008a15d202c3c7ac16a82a1996 [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>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020044
Frederic Weisbecker76369132011-05-19 19:55:04 +020045#include "internal.h"
46
Ingo Molnarcdd6c482009-09-21 12:02:48 +020047#include <asm/irq_regs.h>
48
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010049struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020050 struct task_struct *p;
51 int (*func)(void *info);
52 void *info;
53 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010054};
55
56static void remote_function(void *data)
57{
58 struct remote_function_call *tfc = data;
59 struct task_struct *p = tfc->p;
60
61 if (p) {
62 tfc->ret = -EAGAIN;
63 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
64 return;
65 }
66
67 tfc->ret = tfc->func(tfc->info);
68}
69
70/**
71 * task_function_call - call a function on the cpu on which a task runs
72 * @p: the task to evaluate
73 * @func: the function to be called
74 * @info: the function call argument
75 *
76 * Calls the function @func when the task is currently running. This might
77 * be on the current CPU, which just calls the function directly
78 *
79 * returns: @func return value, or
80 * -ESRCH - when the process isn't running
81 * -EAGAIN - when the process moved away
82 */
83static int
84task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
85{
86 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020087 .p = p,
88 .func = func,
89 .info = info,
90 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010091 };
92
93 if (task_curr(p))
94 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
95
96 return data.ret;
97}
98
99/**
100 * cpu_function_call - call a function on the cpu
101 * @func: the function to be called
102 * @info: the function call argument
103 *
104 * Calls the function @func on the remote cpu.
105 *
106 * returns: @func return value or -ENXIO when the cpu is offline
107 */
108static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
109{
110 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200111 .p = NULL,
112 .func = func,
113 .info = info,
114 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100115 };
116
117 smp_call_function_single(cpu, remote_function, &data, 1);
118
119 return data.ret;
120}
121
Jiri Olsaf8697762014-08-01 14:33:01 +0200122#define EVENT_OWNER_KERNEL ((void *) -1)
123
124static bool is_kernel_event(struct perf_event *event)
125{
126 return event->owner == EVENT_OWNER_KERNEL;
127}
128
Stephane Eraniane5d13672011-02-14 11:20:01 +0200129#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
130 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100131 PERF_FLAG_PID_CGROUP |\
132 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200133
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100134/*
135 * branch priv levels that need permission checks
136 */
137#define PERF_SAMPLE_BRANCH_PERM_PLM \
138 (PERF_SAMPLE_BRANCH_KERNEL |\
139 PERF_SAMPLE_BRANCH_HV)
140
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200141enum event_type_t {
142 EVENT_FLEXIBLE = 0x1,
143 EVENT_PINNED = 0x2,
144 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
145};
146
Stephane Eraniane5d13672011-02-14 11:20:01 +0200147/*
148 * perf_sched_events : >0 events exist
149 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
150 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100151struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200152static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Stephane Eraniand010b332012-02-09 23:21:00 +0100153static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200154
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200155static atomic_t nr_mmap_events __read_mostly;
156static atomic_t nr_comm_events __read_mostly;
157static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200158static atomic_t nr_freq_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200159
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200160static LIST_HEAD(pmus);
161static DEFINE_MUTEX(pmus_lock);
162static struct srcu_struct pmus_srcu;
163
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200164/*
165 * perf event paranoia level:
166 * -1 - not paranoid at all
167 * 0 - disallow raw tracepoint access for unpriv
168 * 1 - disallow cpu events for unpriv
169 * 2 - disallow kernel profiling for unpriv
170 */
171int sysctl_perf_event_paranoid __read_mostly = 1;
172
Frederic Weisbecker20443382011-03-31 03:33:29 +0200173/* Minimum for 512 kiB + 1 user control page */
174int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200175
176/*
177 * max perf event sample rate
178 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700179#define DEFAULT_MAX_SAMPLE_RATE 100000
180#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
181#define DEFAULT_CPU_TIME_MAX_PERCENT 25
182
183int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
184
185static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
186static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
187
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200188static int perf_sample_allowed_ns __read_mostly =
189 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700190
191void update_perf_cpu_limits(void)
192{
193 u64 tmp = perf_sample_period_ns;
194
195 tmp *= sysctl_perf_cpu_time_max_percent;
Stephane Eraniane5302922013-07-05 00:30:11 +0200196 do_div(tmp, 100);
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200197 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
Dave Hansen14c63f12013-06-21 08:51:36 -0700198}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100199
Stephane Eranian9e630202013-04-03 14:21:33 +0200200static int perf_rotate_context(struct perf_cpu_context *cpuctx);
201
Peter Zijlstra163ec432011-02-16 11:22:34 +0100202int perf_proc_update_handler(struct ctl_table *table, int write,
203 void __user *buffer, size_t *lenp,
204 loff_t *ppos)
205{
Knut Petersen723478c2013-09-25 14:29:37 +0200206 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100207
208 if (ret || !write)
209 return ret;
210
211 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700212 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
213 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100214
215 return 0;
216}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200217
Dave Hansen14c63f12013-06-21 08:51:36 -0700218int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
219
220int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
221 void __user *buffer, size_t *lenp,
222 loff_t *ppos)
223{
224 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
225
226 if (ret || !write)
227 return ret;
228
229 update_perf_cpu_limits();
230
231 return 0;
232}
233
234/*
235 * perf samples are done in some very critical code paths (NMIs).
236 * If they take too much CPU time, the system can lock up and not
237 * get any real work done. This will drop the sample rate when
238 * we detect that events are taking too long.
239 */
240#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200241static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700242
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100243static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700244{
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100245 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Dave Hansen14c63f12013-06-21 08:51:36 -0700246 u64 avg_local_sample_len;
Stephane Eraniane5302922013-07-05 00:30:11 +0200247 u64 local_samples_len;
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100248
249 local_samples_len = __get_cpu_var(running_sample_length);
250 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
251
252 printk_ratelimited(KERN_WARNING
253 "perf interrupt took too long (%lld > %lld), lowering "
254 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100255 avg_local_sample_len, allowed_ns >> 1,
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100256 sysctl_perf_event_sample_rate);
257}
258
259static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
260
261void perf_sample_event_took(u64 sample_len_ns)
262{
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200263 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100264 u64 avg_local_sample_len;
265 u64 local_samples_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700266
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200267 if (allowed_ns == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700268 return;
269
270 /* decay the counter by 1 average sample */
271 local_samples_len = __get_cpu_var(running_sample_length);
272 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
273 local_samples_len += sample_len_ns;
274 __get_cpu_var(running_sample_length) = local_samples_len;
275
276 /*
277 * note: this will be biased artifically low until we have
278 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
279 * from having to maintain a count.
280 */
281 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
282
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200283 if (avg_local_sample_len <= allowed_ns)
Dave Hansen14c63f12013-06-21 08:51:36 -0700284 return;
285
286 if (max_samples_per_tick <= 1)
287 return;
288
289 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
290 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
291 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
292
Dave Hansen14c63f12013-06-21 08:51:36 -0700293 update_perf_cpu_limits();
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100294
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100295 if (!irq_work_queue(&perf_duration_work)) {
296 early_printk("perf interrupt took too long (%lld > %lld), lowering "
297 "kernel.perf_event_max_sample_rate to %d\n",
298 avg_local_sample_len, allowed_ns >> 1,
299 sysctl_perf_event_sample_rate);
300 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700301}
302
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200303static atomic64_t perf_event_id;
304
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200305static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
306 enum event_type_t event_type);
307
308static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200309 enum event_type_t event_type,
310 struct task_struct *task);
311
312static void update_context_time(struct perf_event_context *ctx);
313static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200314
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200315void __weak perf_event_print_debug(void) { }
316
Matt Fleming84c79912010-10-03 21:41:13 +0100317extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200318{
Matt Fleming84c79912010-10-03 21:41:13 +0100319 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200320}
321
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200322static inline u64 perf_clock(void)
323{
324 return local_clock();
325}
326
Stephane Eraniane5d13672011-02-14 11:20:01 +0200327static inline struct perf_cpu_context *
328__get_cpu_context(struct perf_event_context *ctx)
329{
330 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
331}
332
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200333static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
334 struct perf_event_context *ctx)
335{
336 raw_spin_lock(&cpuctx->ctx.lock);
337 if (ctx)
338 raw_spin_lock(&ctx->lock);
339}
340
341static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
342 struct perf_event_context *ctx)
343{
344 if (ctx)
345 raw_spin_unlock(&ctx->lock);
346 raw_spin_unlock(&cpuctx->ctx.lock);
347}
348
Stephane Eraniane5d13672011-02-14 11:20:01 +0200349#ifdef CONFIG_CGROUP_PERF
350
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200351/*
Li Zefan877c6852013-03-05 11:38:08 +0800352 * perf_cgroup_info keeps track of time_enabled for a cgroup.
353 * This is a per-cpu dynamically allocated data structure.
354 */
355struct perf_cgroup_info {
356 u64 time;
357 u64 timestamp;
358};
359
360struct perf_cgroup {
361 struct cgroup_subsys_state css;
Namhyung Kim86e213e2013-03-18 18:56:34 +0900362 struct perf_cgroup_info __percpu *info;
Li Zefan877c6852013-03-05 11:38:08 +0800363};
364
365/*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200366 * Must ensure cgroup is pinned (css_get) before calling
367 * this function. In other words, we cannot call this function
368 * if there is no cgroup event for the current CPU context.
369 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200370static inline struct perf_cgroup *
371perf_cgroup_from_task(struct task_struct *task)
372{
Tejun Heo073219e2014-02-08 10:36:58 -0500373 return container_of(task_css(task, perf_event_cgrp_id),
Tejun Heo8af01f52013-08-08 20:11:22 -0400374 struct perf_cgroup, css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200375}
376
377static inline bool
378perf_cgroup_match(struct perf_event *event)
379{
380 struct perf_event_context *ctx = event->ctx;
381 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
382
Tejun Heoef824fa2013-04-08 19:00:38 -0700383 /* @event doesn't care about cgroup */
384 if (!event->cgrp)
385 return true;
386
387 /* wants specific cgroup scope but @cpuctx isn't associated with any */
388 if (!cpuctx->cgrp)
389 return false;
390
391 /*
392 * Cgroup scoping is recursive. An event enabled for a cgroup is
393 * also enabled for all its descendant cgroups. If @cpuctx's
394 * cgroup is a descendant of @event's (the test covers identity
395 * case), it's a match.
396 */
397 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
398 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200399}
400
Stephane Eraniane5d13672011-02-14 11:20:01 +0200401static inline void perf_put_cgroup(struct perf_event *event)
402{
403 css_put(&event->cgrp->css);
404}
405
406static inline void perf_detach_cgroup(struct perf_event *event)
407{
408 perf_put_cgroup(event);
409 event->cgrp = NULL;
410}
411
412static inline int is_cgroup_event(struct perf_event *event)
413{
414 return event->cgrp != NULL;
415}
416
417static inline u64 perf_cgroup_event_time(struct perf_event *event)
418{
419 struct perf_cgroup_info *t;
420
421 t = per_cpu_ptr(event->cgrp->info, event->cpu);
422 return t->time;
423}
424
425static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
426{
427 struct perf_cgroup_info *info;
428 u64 now;
429
430 now = perf_clock();
431
432 info = this_cpu_ptr(cgrp->info);
433
434 info->time += now - info->timestamp;
435 info->timestamp = now;
436}
437
438static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
439{
440 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
441 if (cgrp_out)
442 __update_cgrp_time(cgrp_out);
443}
444
445static inline void update_cgrp_time_from_event(struct perf_event *event)
446{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200447 struct perf_cgroup *cgrp;
448
Stephane Eraniane5d13672011-02-14 11:20:01 +0200449 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200450 * ensure we access cgroup data only when needed and
451 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200452 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200453 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200454 return;
455
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200456 cgrp = perf_cgroup_from_task(current);
457 /*
458 * Do not update time when cgroup is not active
459 */
460 if (cgrp == event->cgrp)
461 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200462}
463
464static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200465perf_cgroup_set_timestamp(struct task_struct *task,
466 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200467{
468 struct perf_cgroup *cgrp;
469 struct perf_cgroup_info *info;
470
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200471 /*
472 * ctx->lock held by caller
473 * ensure we do not access cgroup data
474 * unless we have the cgroup pinned (css_get)
475 */
476 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200477 return;
478
479 cgrp = perf_cgroup_from_task(task);
480 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200481 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200482}
483
484#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
485#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
486
487/*
488 * reschedule events based on the cgroup constraint of task.
489 *
490 * mode SWOUT : schedule out everything
491 * mode SWIN : schedule in based on cgroup for next
492 */
493void perf_cgroup_switch(struct task_struct *task, int mode)
494{
495 struct perf_cpu_context *cpuctx;
496 struct pmu *pmu;
497 unsigned long flags;
498
499 /*
500 * disable interrupts to avoid geting nr_cgroup
501 * changes via __perf_event_disable(). Also
502 * avoids preemption.
503 */
504 local_irq_save(flags);
505
506 /*
507 * we reschedule only in the presence of cgroup
508 * constrained events.
509 */
510 rcu_read_lock();
511
512 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200513 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200514 if (cpuctx->unique_pmu != pmu)
515 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200516
Stephane Eraniane5d13672011-02-14 11:20:01 +0200517 /*
518 * perf_cgroup_events says at least one
519 * context on this CPU has cgroup events.
520 *
521 * ctx->nr_cgroups reports the number of cgroup
522 * events for a context.
523 */
524 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200525 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
526 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200527
528 if (mode & PERF_CGROUP_SWOUT) {
529 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
530 /*
531 * must not be done before ctxswout due
532 * to event_filter_match() in event_sched_out()
533 */
534 cpuctx->cgrp = NULL;
535 }
536
537 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200538 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200539 /*
540 * set cgrp before ctxsw in to allow
541 * event_filter_match() to not have to pass
542 * task around
Stephane Eraniane5d13672011-02-14 11:20:01 +0200543 */
544 cpuctx->cgrp = perf_cgroup_from_task(task);
545 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
546 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200547 perf_pmu_enable(cpuctx->ctx.pmu);
548 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200549 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200550 }
551
552 rcu_read_unlock();
553
554 local_irq_restore(flags);
555}
556
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200557static inline void perf_cgroup_sched_out(struct task_struct *task,
558 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200559{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200560 struct perf_cgroup *cgrp1;
561 struct perf_cgroup *cgrp2 = NULL;
562
563 /*
564 * we come here when we know perf_cgroup_events > 0
565 */
566 cgrp1 = perf_cgroup_from_task(task);
567
568 /*
569 * next is NULL when called from perf_event_enable_on_exec()
570 * that will systematically cause a cgroup_switch()
571 */
572 if (next)
573 cgrp2 = perf_cgroup_from_task(next);
574
575 /*
576 * only schedule out current cgroup events if we know
577 * that we are switching to a different cgroup. Otherwise,
578 * do no touch the cgroup events.
579 */
580 if (cgrp1 != cgrp2)
581 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200582}
583
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200584static inline void perf_cgroup_sched_in(struct task_struct *prev,
585 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200586{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200587 struct perf_cgroup *cgrp1;
588 struct perf_cgroup *cgrp2 = NULL;
589
590 /*
591 * we come here when we know perf_cgroup_events > 0
592 */
593 cgrp1 = perf_cgroup_from_task(task);
594
595 /* prev can never be NULL */
596 cgrp2 = perf_cgroup_from_task(prev);
597
598 /*
599 * only need to schedule in cgroup events if we are changing
600 * cgroup during ctxsw. Cgroup events were not scheduled
601 * out of ctxsw out if that was not the case.
602 */
603 if (cgrp1 != cgrp2)
604 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200605}
606
607static inline int perf_cgroup_connect(int fd, struct perf_event *event,
608 struct perf_event_attr *attr,
609 struct perf_event *group_leader)
610{
611 struct perf_cgroup *cgrp;
612 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400613 struct fd f = fdget(fd);
614 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200615
Al Viro2903ff02012-08-28 12:52:22 -0400616 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200617 return -EBADF;
618
Tejun Heoec903c02014-05-13 12:11:01 -0400619 css = css_tryget_online_from_dir(f.file->f_dentry,
620 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800621 if (IS_ERR(css)) {
622 ret = PTR_ERR(css);
623 goto out;
624 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200625
626 cgrp = container_of(css, struct perf_cgroup, css);
627 event->cgrp = cgrp;
628
629 /*
630 * all events in a group must monitor
631 * the same cgroup because a task belongs
632 * to only one perf cgroup at a time
633 */
634 if (group_leader && group_leader->cgrp != cgrp) {
635 perf_detach_cgroup(event);
636 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200637 }
Li Zefan3db272c2011-03-03 14:25:37 +0800638out:
Al Viro2903ff02012-08-28 12:52:22 -0400639 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200640 return ret;
641}
642
643static inline void
644perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
645{
646 struct perf_cgroup_info *t;
647 t = per_cpu_ptr(event->cgrp->info, event->cpu);
648 event->shadow_ctx_time = now - t->timestamp;
649}
650
651static inline void
652perf_cgroup_defer_enabled(struct perf_event *event)
653{
654 /*
655 * when the current task's perf cgroup does not match
656 * the event's, we need to remember to call the
657 * perf_mark_enable() function the first time a task with
658 * a matching perf cgroup is scheduled in.
659 */
660 if (is_cgroup_event(event) && !perf_cgroup_match(event))
661 event->cgrp_defer_enabled = 1;
662}
663
664static inline void
665perf_cgroup_mark_enabled(struct perf_event *event,
666 struct perf_event_context *ctx)
667{
668 struct perf_event *sub;
669 u64 tstamp = perf_event_time(event);
670
671 if (!event->cgrp_defer_enabled)
672 return;
673
674 event->cgrp_defer_enabled = 0;
675
676 event->tstamp_enabled = tstamp - event->total_time_enabled;
677 list_for_each_entry(sub, &event->sibling_list, group_entry) {
678 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
679 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
680 sub->cgrp_defer_enabled = 0;
681 }
682 }
683}
684#else /* !CONFIG_CGROUP_PERF */
685
686static inline bool
687perf_cgroup_match(struct perf_event *event)
688{
689 return true;
690}
691
692static inline void perf_detach_cgroup(struct perf_event *event)
693{}
694
695static inline int is_cgroup_event(struct perf_event *event)
696{
697 return 0;
698}
699
700static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
701{
702 return 0;
703}
704
705static inline void update_cgrp_time_from_event(struct perf_event *event)
706{
707}
708
709static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
710{
711}
712
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200713static inline void perf_cgroup_sched_out(struct task_struct *task,
714 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200715{
716}
717
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200718static inline void perf_cgroup_sched_in(struct task_struct *prev,
719 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200720{
721}
722
723static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
724 struct perf_event_attr *attr,
725 struct perf_event *group_leader)
726{
727 return -EINVAL;
728}
729
730static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200731perf_cgroup_set_timestamp(struct task_struct *task,
732 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200733{
734}
735
736void
737perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
738{
739}
740
741static inline void
742perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
743{
744}
745
746static inline u64 perf_cgroup_event_time(struct perf_event *event)
747{
748 return 0;
749}
750
751static inline void
752perf_cgroup_defer_enabled(struct perf_event *event)
753{
754}
755
756static inline void
757perf_cgroup_mark_enabled(struct perf_event *event,
758 struct perf_event_context *ctx)
759{
760}
761#endif
762
Stephane Eranian9e630202013-04-03 14:21:33 +0200763/*
764 * set default to be dependent on timer tick just
765 * like original code
766 */
767#define PERF_CPU_HRTIMER (1000 / HZ)
768/*
769 * function must be called with interrupts disbled
770 */
771static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
772{
773 struct perf_cpu_context *cpuctx;
774 enum hrtimer_restart ret = HRTIMER_NORESTART;
775 int rotations = 0;
776
777 WARN_ON(!irqs_disabled());
778
779 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
780
781 rotations = perf_rotate_context(cpuctx);
782
783 /*
784 * arm timer if needed
785 */
786 if (rotations) {
787 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
788 ret = HRTIMER_RESTART;
789 }
790
791 return ret;
792}
793
794/* CPU is going down */
795void perf_cpu_hrtimer_cancel(int cpu)
796{
797 struct perf_cpu_context *cpuctx;
798 struct pmu *pmu;
799 unsigned long flags;
800
801 if (WARN_ON(cpu != smp_processor_id()))
802 return;
803
804 local_irq_save(flags);
805
806 rcu_read_lock();
807
808 list_for_each_entry_rcu(pmu, &pmus, entry) {
809 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
810
811 if (pmu->task_ctx_nr == perf_sw_context)
812 continue;
813
814 hrtimer_cancel(&cpuctx->hrtimer);
815 }
816
817 rcu_read_unlock();
818
819 local_irq_restore(flags);
820}
821
822static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
823{
824 struct hrtimer *hr = &cpuctx->hrtimer;
825 struct pmu *pmu = cpuctx->ctx.pmu;
Stephane Eranian62b85632013-04-03 14:21:34 +0200826 int timer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200827
828 /* no multiplexing needed for SW PMU */
829 if (pmu->task_ctx_nr == perf_sw_context)
830 return;
831
Stephane Eranian62b85632013-04-03 14:21:34 +0200832 /*
833 * check default is sane, if not set then force to
834 * default interval (1/tick)
835 */
836 timer = pmu->hrtimer_interval_ms;
837 if (timer < 1)
838 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
839
840 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200841
842 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
843 hr->function = perf_cpu_hrtimer_handler;
844}
845
846static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
847{
848 struct hrtimer *hr = &cpuctx->hrtimer;
849 struct pmu *pmu = cpuctx->ctx.pmu;
850
851 /* not for SW PMU */
852 if (pmu->task_ctx_nr == perf_sw_context)
853 return;
854
855 if (hrtimer_active(hr))
856 return;
857
858 if (!hrtimer_callback_running(hr))
859 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
860 0, HRTIMER_MODE_REL_PINNED, 0);
861}
862
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200863void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200864{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200865 int *count = this_cpu_ptr(pmu->pmu_disable_count);
866 if (!(*count)++)
867 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200868}
869
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200870void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200871{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200872 int *count = this_cpu_ptr(pmu->pmu_disable_count);
873 if (!--(*count))
874 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200875}
876
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200877static DEFINE_PER_CPU(struct list_head, rotation_list);
878
879/*
880 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
881 * because they're strictly cpu affine and rotate_start is called with IRQs
882 * disabled, while rotate_context is called from IRQ context.
883 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200884static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200885{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200886 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200887 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200888
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200889 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200890
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +0200891 if (list_empty(&cpuctx->rotation_list))
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200892 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200893}
894
895static void get_ctx(struct perf_event_context *ctx)
896{
897 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
898}
899
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200900static void put_ctx(struct perf_event_context *ctx)
901{
902 if (atomic_dec_and_test(&ctx->refcount)) {
903 if (ctx->parent_ctx)
904 put_ctx(ctx->parent_ctx);
905 if (ctx->task)
906 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800907 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200908 }
909}
910
911static void unclone_ctx(struct perf_event_context *ctx)
912{
913 if (ctx->parent_ctx) {
914 put_ctx(ctx->parent_ctx);
915 ctx->parent_ctx = NULL;
916 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +0200917 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200918}
919
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200920static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
921{
922 /*
923 * only top level events have the pid namespace they were created in
924 */
925 if (event->parent)
926 event = event->parent;
927
928 return task_tgid_nr_ns(p, event->ns);
929}
930
931static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
932{
933 /*
934 * only top level events have the pid namespace they were created in
935 */
936 if (event->parent)
937 event = event->parent;
938
939 return task_pid_nr_ns(p, event->ns);
940}
941
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200942/*
943 * If we inherit events we want to return the parent event id
944 * to userspace.
945 */
946static u64 primary_event_id(struct perf_event *event)
947{
948 u64 id = event->id;
949
950 if (event->parent)
951 id = event->parent->id;
952
953 return id;
954}
955
956/*
957 * Get the perf_event_context for a task and lock it.
958 * This has to cope with with the fact that until it is locked,
959 * the context could get moved to another task.
960 */
961static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200962perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200963{
964 struct perf_event_context *ctx;
965
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200966retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200967 /*
968 * One of the few rules of preemptible RCU is that one cannot do
969 * rcu_read_unlock() while holding a scheduler (or nested) lock when
970 * part of the read side critical section was preemptible -- see
971 * rcu_read_unlock_special().
972 *
973 * Since ctx->lock nests under rq->lock we must ensure the entire read
974 * side critical section is non-preemptible.
975 */
976 preempt_disable();
977 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200978 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200979 if (ctx) {
980 /*
981 * If this context is a clone of another, it might
982 * get swapped for another underneath us by
983 * perf_event_task_sched_out, though the
984 * rcu_read_lock() protects us from any context
985 * getting freed. Lock the context and check if it
986 * got swapped before we could get the lock, and retry
987 * if so. If we locked the right context, then it
988 * can't get swapped on us any more.
989 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100990 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200991 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100992 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200993 rcu_read_unlock();
994 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200995 goto retry;
996 }
997
998 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100999 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001000 ctx = NULL;
1001 }
1002 }
1003 rcu_read_unlock();
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001004 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001005 return ctx;
1006}
1007
1008/*
1009 * Get the context for a task and increment its pin_count so it
1010 * can't get swapped to another task. This also increments its
1011 * reference count so that the context can't get freed.
1012 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001013static struct perf_event_context *
1014perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001015{
1016 struct perf_event_context *ctx;
1017 unsigned long flags;
1018
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001019 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001020 if (ctx) {
1021 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001022 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001023 }
1024 return ctx;
1025}
1026
1027static void perf_unpin_context(struct perf_event_context *ctx)
1028{
1029 unsigned long flags;
1030
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001031 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001032 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001033 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001034}
1035
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001036/*
1037 * Update the record of the current time in a context.
1038 */
1039static void update_context_time(struct perf_event_context *ctx)
1040{
1041 u64 now = perf_clock();
1042
1043 ctx->time += now - ctx->timestamp;
1044 ctx->timestamp = now;
1045}
1046
Stephane Eranian41587552011-01-03 18:20:01 +02001047static u64 perf_event_time(struct perf_event *event)
1048{
1049 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001050
1051 if (is_cgroup_event(event))
1052 return perf_cgroup_event_time(event);
1053
Stephane Eranian41587552011-01-03 18:20:01 +02001054 return ctx ? ctx->time : 0;
1055}
1056
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001057/*
1058 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001059 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001060 */
1061static void update_event_times(struct perf_event *event)
1062{
1063 struct perf_event_context *ctx = event->ctx;
1064 u64 run_end;
1065
1066 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1067 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1068 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001069 /*
1070 * in cgroup mode, time_enabled represents
1071 * the time the event was enabled AND active
1072 * tasks were in the monitored cgroup. This is
1073 * independent of the activity of the context as
1074 * there may be a mix of cgroup and non-cgroup events.
1075 *
1076 * That is why we treat cgroup events differently
1077 * here.
1078 */
1079 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001080 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001081 else if (ctx->is_active)
1082 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001083 else
1084 run_end = event->tstamp_stopped;
1085
1086 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001087
1088 if (event->state == PERF_EVENT_STATE_INACTIVE)
1089 run_end = event->tstamp_stopped;
1090 else
Stephane Eranian41587552011-01-03 18:20:01 +02001091 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001092
1093 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001094
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001095}
1096
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001097/*
1098 * Update total_time_enabled and total_time_running for all events in a group.
1099 */
1100static void update_group_times(struct perf_event *leader)
1101{
1102 struct perf_event *event;
1103
1104 update_event_times(leader);
1105 list_for_each_entry(event, &leader->sibling_list, group_entry)
1106 update_event_times(event);
1107}
1108
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001109static struct list_head *
1110ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1111{
1112 if (event->attr.pinned)
1113 return &ctx->pinned_groups;
1114 else
1115 return &ctx->flexible_groups;
1116}
1117
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001118/*
1119 * Add a event from the lists for its context.
1120 * Must be called with ctx->mutex and ctx->lock held.
1121 */
1122static void
1123list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1124{
Peter Zijlstra8a495422010-05-27 15:47:49 +02001125 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1126 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001127
1128 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001129 * If we're a stand alone event or group leader, we go to the context
1130 * list, group events are kept attached to the group so that
1131 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001132 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001133 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001134 struct list_head *list;
1135
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001136 if (is_software_event(event))
1137 event->group_flags |= PERF_GROUP_SOFTWARE;
1138
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001139 list = ctx_group_list(event, ctx);
1140 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001141 }
1142
Peter Zijlstra08309372011-03-03 11:31:20 +01001143 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001144 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001145
Stephane Eraniand010b332012-02-09 23:21:00 +01001146 if (has_branch_stack(event))
1147 ctx->nr_branch_stack++;
1148
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001149 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001150 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001151 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001152 ctx->nr_events++;
1153 if (event->attr.inherit_stat)
1154 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001155
1156 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001157}
1158
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001159/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001160 * Initialize event state based on the perf_event_attr::disabled.
1161 */
1162static inline void perf_event__state_init(struct perf_event *event)
1163{
1164 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1165 PERF_EVENT_STATE_INACTIVE;
1166}
1167
1168/*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001169 * Called at perf_event creation and when events are attached/detached from a
1170 * group.
1171 */
1172static void perf_event__read_size(struct perf_event *event)
1173{
1174 int entry = sizeof(u64); /* value */
1175 int size = 0;
1176 int nr = 1;
1177
1178 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1179 size += sizeof(u64);
1180
1181 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1182 size += sizeof(u64);
1183
1184 if (event->attr.read_format & PERF_FORMAT_ID)
1185 entry += sizeof(u64);
1186
1187 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1188 nr += event->group_leader->nr_siblings;
1189 size += sizeof(u64);
1190 }
1191
1192 size += entry * nr;
1193 event->read_size = size;
1194}
1195
1196static void perf_event__header_size(struct perf_event *event)
1197{
1198 struct perf_sample_data *data;
1199 u64 sample_type = event->attr.sample_type;
1200 u16 size = 0;
1201
1202 perf_event__read_size(event);
1203
1204 if (sample_type & PERF_SAMPLE_IP)
1205 size += sizeof(data->ip);
1206
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001207 if (sample_type & PERF_SAMPLE_ADDR)
1208 size += sizeof(data->addr);
1209
1210 if (sample_type & PERF_SAMPLE_PERIOD)
1211 size += sizeof(data->period);
1212
Andi Kleenc3feedf2013-01-24 16:10:28 +01001213 if (sample_type & PERF_SAMPLE_WEIGHT)
1214 size += sizeof(data->weight);
1215
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001216 if (sample_type & PERF_SAMPLE_READ)
1217 size += event->read_size;
1218
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001219 if (sample_type & PERF_SAMPLE_DATA_SRC)
1220 size += sizeof(data->data_src.val);
1221
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001222 if (sample_type & PERF_SAMPLE_TRANSACTION)
1223 size += sizeof(data->txn);
1224
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001225 event->header_size = size;
1226}
1227
1228static void perf_event__id_header_size(struct perf_event *event)
1229{
1230 struct perf_sample_data *data;
1231 u64 sample_type = event->attr.sample_type;
1232 u16 size = 0;
1233
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001234 if (sample_type & PERF_SAMPLE_TID)
1235 size += sizeof(data->tid_entry);
1236
1237 if (sample_type & PERF_SAMPLE_TIME)
1238 size += sizeof(data->time);
1239
Adrian Hunterff3d5272013-08-27 11:23:07 +03001240 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1241 size += sizeof(data->id);
1242
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001243 if (sample_type & PERF_SAMPLE_ID)
1244 size += sizeof(data->id);
1245
1246 if (sample_type & PERF_SAMPLE_STREAM_ID)
1247 size += sizeof(data->stream_id);
1248
1249 if (sample_type & PERF_SAMPLE_CPU)
1250 size += sizeof(data->cpu_entry);
1251
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001252 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001253}
1254
Peter Zijlstra8a495422010-05-27 15:47:49 +02001255static void perf_group_attach(struct perf_event *event)
1256{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001257 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001258
Peter Zijlstra74c33372010-10-15 11:40:29 +02001259 /*
1260 * We can have double attach due to group movement in perf_event_open.
1261 */
1262 if (event->attach_state & PERF_ATTACH_GROUP)
1263 return;
1264
Peter Zijlstra8a495422010-05-27 15:47:49 +02001265 event->attach_state |= PERF_ATTACH_GROUP;
1266
1267 if (group_leader == event)
1268 return;
1269
1270 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1271 !is_software_event(event))
1272 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1273
1274 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1275 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001276
1277 perf_event__header_size(group_leader);
1278
1279 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1280 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001281}
1282
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001283/*
1284 * Remove a event from the lists for its context.
1285 * Must be called with ctx->mutex and ctx->lock held.
1286 */
1287static void
1288list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1289{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001290 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001291 /*
1292 * We can have double detach due to exit/hot-unplug + close.
1293 */
1294 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001295 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001296
1297 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1298
Stephane Eranian68cacd22011-03-23 16:03:06 +01001299 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001300 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001301 cpuctx = __get_cpu_context(ctx);
1302 /*
1303 * if there are no more cgroup events
1304 * then cler cgrp to avoid stale pointer
1305 * in update_cgrp_time_from_cpuctx()
1306 */
1307 if (!ctx->nr_cgroups)
1308 cpuctx->cgrp = NULL;
1309 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001310
Stephane Eraniand010b332012-02-09 23:21:00 +01001311 if (has_branch_stack(event))
1312 ctx->nr_branch_stack--;
1313
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001314 ctx->nr_events--;
1315 if (event->attr.inherit_stat)
1316 ctx->nr_stat--;
1317
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001318 list_del_rcu(&event->event_entry);
1319
Peter Zijlstra8a495422010-05-27 15:47:49 +02001320 if (event->group_leader == event)
1321 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001322
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001323 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001324
1325 /*
1326 * If event was in error state, then keep it
1327 * that way, otherwise bogus counts will be
1328 * returned on read(). The only way to get out
1329 * of error state is by explicit re-enabling
1330 * of the event
1331 */
1332 if (event->state > PERF_EVENT_STATE_OFF)
1333 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001334
1335 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001336}
1337
Peter Zijlstra8a495422010-05-27 15:47:49 +02001338static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001339{
1340 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001341 struct list_head *list = NULL;
1342
1343 /*
1344 * We can have double detach due to exit/hot-unplug + close.
1345 */
1346 if (!(event->attach_state & PERF_ATTACH_GROUP))
1347 return;
1348
1349 event->attach_state &= ~PERF_ATTACH_GROUP;
1350
1351 /*
1352 * If this is a sibling, remove it from its group.
1353 */
1354 if (event->group_leader != event) {
1355 list_del_init(&event->group_entry);
1356 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001357 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001358 }
1359
1360 if (!list_empty(&event->group_entry))
1361 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001362
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001363 /*
1364 * If this was a group event with sibling events then
1365 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001366 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001367 */
1368 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001369 if (list)
1370 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001371 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001372
1373 /* Inherit group flags from the previous leader */
1374 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001375 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001376
1377out:
1378 perf_event__header_size(event->group_leader);
1379
1380 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1381 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001382}
1383
Stephane Eranianfa66f072010-08-26 16:40:01 +02001384static inline int
1385event_filter_match(struct perf_event *event)
1386{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001387 return (event->cpu == -1 || event->cpu == smp_processor_id())
1388 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001389}
1390
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001391static void
1392event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001393 struct perf_cpu_context *cpuctx,
1394 struct perf_event_context *ctx)
1395{
Stephane Eranian41587552011-01-03 18:20:01 +02001396 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001397 u64 delta;
1398 /*
1399 * An event which could not be activated because of
1400 * filter mismatch still needs to have its timings
1401 * maintained, otherwise bogus information is return
1402 * via read() for time_enabled, time_running:
1403 */
1404 if (event->state == PERF_EVENT_STATE_INACTIVE
1405 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001406 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001407 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001408 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001409 }
1410
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001411 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001412 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001413
Alexander Shishkin44377272013-12-16 14:17:36 +02001414 perf_pmu_disable(event->pmu);
1415
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001416 event->state = PERF_EVENT_STATE_INACTIVE;
1417 if (event->pending_disable) {
1418 event->pending_disable = 0;
1419 event->state = PERF_EVENT_STATE_OFF;
1420 }
Stephane Eranian41587552011-01-03 18:20:01 +02001421 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001422 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001423 event->oncpu = -1;
1424
1425 if (!is_software_event(event))
1426 cpuctx->active_oncpu--;
1427 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001428 if (event->attr.freq && event->attr.sample_freq)
1429 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001430 if (event->attr.exclusive || !cpuctx->active_oncpu)
1431 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001432
1433 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001434}
1435
1436static void
1437group_sched_out(struct perf_event *group_event,
1438 struct perf_cpu_context *cpuctx,
1439 struct perf_event_context *ctx)
1440{
1441 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001442 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001443
1444 event_sched_out(group_event, cpuctx, ctx);
1445
1446 /*
1447 * Schedule out siblings (if any):
1448 */
1449 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1450 event_sched_out(event, cpuctx, ctx);
1451
Stephane Eranianfa66f072010-08-26 16:40:01 +02001452 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001453 cpuctx->exclusive = 0;
1454}
1455
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001456struct remove_event {
1457 struct perf_event *event;
1458 bool detach_group;
1459};
1460
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001461/*
1462 * Cross CPU call to remove a performance event
1463 *
1464 * We disable the event on the hardware level first. After that we
1465 * remove it from the context list.
1466 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001467static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001468{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001469 struct remove_event *re = info;
1470 struct perf_event *event = re->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001471 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001472 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001473
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001474 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001475 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001476 if (re->detach_group)
1477 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001478 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001479 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1480 ctx->is_active = 0;
1481 cpuctx->task_ctx = NULL;
1482 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001483 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001484
1485 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001486}
1487
1488
1489/*
1490 * Remove the event from a task's (or a CPU's) list of events.
1491 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001492 * CPU events are removed with a smp call. For task events we only
1493 * call when the task is on a CPU.
1494 *
1495 * If event->ctx is a cloned context, callers must make sure that
1496 * every task struct that event->ctx->task could possibly point to
1497 * remains valid. This is OK when called from perf_release since
1498 * that only calls us on the top-level context, which can't be a clone.
1499 * When called from perf_event_exit_task, it's OK because the
1500 * context has been detached from its task.
1501 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001502static void perf_remove_from_context(struct perf_event *event, bool detach_group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001503{
1504 struct perf_event_context *ctx = event->ctx;
1505 struct task_struct *task = ctx->task;
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001506 struct remove_event re = {
1507 .event = event,
1508 .detach_group = detach_group,
1509 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001510
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001511 lockdep_assert_held(&ctx->mutex);
1512
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001513 if (!task) {
1514 /*
1515 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001516 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001517 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001518 cpu_function_call(event->cpu, __perf_remove_from_context, &re);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001519 return;
1520 }
1521
1522retry:
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001523 if (!task_function_call(task, __perf_remove_from_context, &re))
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001524 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001525
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001526 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001527 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001528 * If we failed to find a running task, but find the context active now
1529 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001530 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001531 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001532 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001533 goto retry;
1534 }
1535
1536 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001537 * Since the task isn't running, its safe to remove the event, us
1538 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001539 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001540 if (detach_group)
1541 perf_group_detach(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001542 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001543 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001544}
1545
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001546/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001547 * Cross CPU call to disable a performance event
1548 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301549int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001550{
1551 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001552 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001553 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001554
1555 /*
1556 * If this is a per-task event, need to check whether this
1557 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001558 *
1559 * Can trigger due to concurrent perf_event_context_sched_out()
1560 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001561 */
1562 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001563 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001564
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001565 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001566
1567 /*
1568 * If the event is on, turn it off.
1569 * If it is in error state, leave it in error state.
1570 */
1571 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1572 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001573 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001574 update_group_times(event);
1575 if (event == event->group_leader)
1576 group_sched_out(event, cpuctx, ctx);
1577 else
1578 event_sched_out(event, cpuctx, ctx);
1579 event->state = PERF_EVENT_STATE_OFF;
1580 }
1581
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001582 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001583
1584 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001585}
1586
1587/*
1588 * Disable a event.
1589 *
1590 * If event->ctx is a cloned context, callers must make sure that
1591 * every task struct that event->ctx->task could possibly point to
1592 * remains valid. This condition is satisifed when called through
1593 * perf_event_for_each_child or perf_event_for_each because they
1594 * hold the top-level event's child_mutex, so any descendant that
1595 * goes to exit will block in sync_child_event.
1596 * When called from perf_pending_event it's OK because event->ctx
1597 * is the current context on this CPU and preemption is disabled,
1598 * hence we can't get into perf_event_task_sched_out for this context.
1599 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001600void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001601{
1602 struct perf_event_context *ctx = event->ctx;
1603 struct task_struct *task = ctx->task;
1604
1605 if (!task) {
1606 /*
1607 * Disable the event on the cpu that it's on
1608 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001609 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001610 return;
1611 }
1612
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001613retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001614 if (!task_function_call(task, __perf_event_disable, event))
1615 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001616
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001617 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001618 /*
1619 * If the event is still active, we need to retry the cross-call.
1620 */
1621 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001622 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001623 /*
1624 * Reload the task pointer, it might have been changed by
1625 * a concurrent perf_event_context_sched_out().
1626 */
1627 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001628 goto retry;
1629 }
1630
1631 /*
1632 * Since we have the lock this context can't be scheduled
1633 * in, so we can change the state safely.
1634 */
1635 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1636 update_group_times(event);
1637 event->state = PERF_EVENT_STATE_OFF;
1638 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001639 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001640}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001641EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001642
Stephane Eraniane5d13672011-02-14 11:20:01 +02001643static void perf_set_shadow_time(struct perf_event *event,
1644 struct perf_event_context *ctx,
1645 u64 tstamp)
1646{
1647 /*
1648 * use the correct time source for the time snapshot
1649 *
1650 * We could get by without this by leveraging the
1651 * fact that to get to this function, the caller
1652 * has most likely already called update_context_time()
1653 * and update_cgrp_time_xx() and thus both timestamp
1654 * are identical (or very close). Given that tstamp is,
1655 * already adjusted for cgroup, we could say that:
1656 * tstamp - ctx->timestamp
1657 * is equivalent to
1658 * tstamp - cgrp->timestamp.
1659 *
1660 * Then, in perf_output_read(), the calculation would
1661 * work with no changes because:
1662 * - event is guaranteed scheduled in
1663 * - no scheduled out in between
1664 * - thus the timestamp would be the same
1665 *
1666 * But this is a bit hairy.
1667 *
1668 * So instead, we have an explicit cgroup call to remain
1669 * within the time time source all along. We believe it
1670 * is cleaner and simpler to understand.
1671 */
1672 if (is_cgroup_event(event))
1673 perf_cgroup_set_shadow_time(event, tstamp);
1674 else
1675 event->shadow_ctx_time = tstamp - ctx->timestamp;
1676}
1677
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001678#define MAX_INTERRUPTS (~0ULL)
1679
1680static void perf_log_throttle(struct perf_event *event, int enable);
1681
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001682static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001683event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001684 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001685 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001686{
Stephane Eranian41587552011-01-03 18:20:01 +02001687 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001688 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001689
Peter Zijlstra63342412014-05-05 11:49:16 +02001690 lockdep_assert_held(&ctx->lock);
1691
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001692 if (event->state <= PERF_EVENT_STATE_OFF)
1693 return 0;
1694
1695 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001696 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001697
1698 /*
1699 * Unthrottle events, since we scheduled we might have missed several
1700 * ticks already, also for a heavily scheduling task there is little
1701 * guarantee it'll get a tick in a timely manner.
1702 */
1703 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1704 perf_log_throttle(event, 1);
1705 event->hw.interrupts = 0;
1706 }
1707
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001708 /*
1709 * The new state must be visible before we turn it on in the hardware:
1710 */
1711 smp_wmb();
1712
Alexander Shishkin44377272013-12-16 14:17:36 +02001713 perf_pmu_disable(event->pmu);
1714
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001715 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001716 event->state = PERF_EVENT_STATE_INACTIVE;
1717 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001718 ret = -EAGAIN;
1719 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001720 }
1721
Stephane Eranian41587552011-01-03 18:20:01 +02001722 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001723
Stephane Eraniane5d13672011-02-14 11:20:01 +02001724 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001725
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001726 if (!is_software_event(event))
1727 cpuctx->active_oncpu++;
1728 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001729 if (event->attr.freq && event->attr.sample_freq)
1730 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001731
1732 if (event->attr.exclusive)
1733 cpuctx->exclusive = 1;
1734
Alexander Shishkin44377272013-12-16 14:17:36 +02001735out:
1736 perf_pmu_enable(event->pmu);
1737
1738 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001739}
1740
1741static int
1742group_sched_in(struct perf_event *group_event,
1743 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001744 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001745{
Lin Ming6bde9b62010-04-23 13:56:00 +08001746 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001747 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001748 u64 now = ctx->time;
1749 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001750
1751 if (group_event->state == PERF_EVENT_STATE_OFF)
1752 return 0;
1753
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001754 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001755
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001756 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001757 pmu->cancel_txn(pmu);
Stephane Eranian9e630202013-04-03 14:21:33 +02001758 perf_cpu_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001759 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001760 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001761
1762 /*
1763 * Schedule in siblings as one group (if any):
1764 */
1765 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001766 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001767 partial_group = event;
1768 goto group_error;
1769 }
1770 }
1771
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001772 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001773 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001774
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001775group_error:
1776 /*
1777 * Groups can be scheduled in as one unit only, so undo any
1778 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001779 * The events up to the failed event are scheduled out normally,
1780 * tstamp_stopped will be updated.
1781 *
1782 * The failed events and the remaining siblings need to have
1783 * their timings updated as if they had gone thru event_sched_in()
1784 * and event_sched_out(). This is required to get consistent timings
1785 * across the group. This also takes care of the case where the group
1786 * could never be scheduled by ensuring tstamp_stopped is set to mark
1787 * the time the event was actually stopped, such that time delta
1788 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001789 */
1790 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1791 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001792 simulate = true;
1793
1794 if (simulate) {
1795 event->tstamp_running += now - event->tstamp_stopped;
1796 event->tstamp_stopped = now;
1797 } else {
1798 event_sched_out(event, cpuctx, ctx);
1799 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001800 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001801 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001802
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001803 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001804
Stephane Eranian9e630202013-04-03 14:21:33 +02001805 perf_cpu_hrtimer_restart(cpuctx);
1806
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001807 return -EAGAIN;
1808}
1809
1810/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001811 * Work out whether we can put this event group on the CPU now.
1812 */
1813static int group_can_go_on(struct perf_event *event,
1814 struct perf_cpu_context *cpuctx,
1815 int can_add_hw)
1816{
1817 /*
1818 * Groups consisting entirely of software events can always go on.
1819 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001820 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001821 return 1;
1822 /*
1823 * If an exclusive group is already on, no other hardware
1824 * events can go on.
1825 */
1826 if (cpuctx->exclusive)
1827 return 0;
1828 /*
1829 * If this group is exclusive and there are already
1830 * events on the CPU, it can't go on.
1831 */
1832 if (event->attr.exclusive && cpuctx->active_oncpu)
1833 return 0;
1834 /*
1835 * Otherwise, try to add it if all previous groups were able
1836 * to go on.
1837 */
1838 return can_add_hw;
1839}
1840
1841static void add_event_to_ctx(struct perf_event *event,
1842 struct perf_event_context *ctx)
1843{
Stephane Eranian41587552011-01-03 18:20:01 +02001844 u64 tstamp = perf_event_time(event);
1845
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001846 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001847 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001848 event->tstamp_enabled = tstamp;
1849 event->tstamp_running = tstamp;
1850 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001851}
1852
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001853static void task_ctx_sched_out(struct perf_event_context *ctx);
1854static void
1855ctx_sched_in(struct perf_event_context *ctx,
1856 struct perf_cpu_context *cpuctx,
1857 enum event_type_t event_type,
1858 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001859
Peter Zijlstradce58552011-04-09 21:17:46 +02001860static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1861 struct perf_event_context *ctx,
1862 struct task_struct *task)
1863{
1864 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1865 if (ctx)
1866 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1867 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1868 if (ctx)
1869 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1870}
1871
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001872/*
1873 * Cross CPU call to install and enable a performance event
1874 *
1875 * Must be called with ctx->mutex held
1876 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001877static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001878{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001879 struct perf_event *event = info;
1880 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001881 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001882 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1883 struct task_struct *task = current;
1884
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001885 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001886 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001887
1888 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001889 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001890 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001891 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001892 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001893
1894 /*
1895 * If the context we're installing events in is not the
1896 * active task_ctx, flip them.
1897 */
1898 if (ctx->task && task_ctx != ctx) {
1899 if (task_ctx)
1900 raw_spin_unlock(&task_ctx->lock);
1901 raw_spin_lock(&ctx->lock);
1902 task_ctx = ctx;
1903 }
1904
1905 if (task_ctx) {
1906 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001907 task = task_ctx->task;
1908 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001909
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001910 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001911
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001912 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001913 /*
1914 * update cgrp time only if current cgrp
1915 * matches event->cgrp. Must be done before
1916 * calling add_event_to_ctx()
1917 */
1918 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001919
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001920 add_event_to_ctx(event, ctx);
1921
1922 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001923 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001924 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001925 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001926
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001927 perf_pmu_enable(cpuctx->ctx.pmu);
1928 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001929
1930 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001931}
1932
1933/*
1934 * Attach a performance event to a context
1935 *
1936 * First we add the event to the list with the hardware enable bit
1937 * in event->hw_config cleared.
1938 *
1939 * If the event is attached to a task which is on a CPU we use a smp
1940 * call to enable it in the task context. The task might have been
1941 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001942 */
1943static void
1944perf_install_in_context(struct perf_event_context *ctx,
1945 struct perf_event *event,
1946 int cpu)
1947{
1948 struct task_struct *task = ctx->task;
1949
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001950 lockdep_assert_held(&ctx->mutex);
1951
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001952 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08001953 if (event->cpu != -1)
1954 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001955
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001956 if (!task) {
1957 /*
1958 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001959 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001960 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001961 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001962 return;
1963 }
1964
1965retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001966 if (!task_function_call(task, __perf_install_in_context, event))
1967 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001968
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001969 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001970 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001971 * If we failed to find a running task, but find the context active now
1972 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001973 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001974 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001975 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001976 goto retry;
1977 }
1978
1979 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001980 * Since the task isn't running, its safe to add the event, us holding
1981 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001982 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001983 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001984 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001985}
1986
1987/*
1988 * Put a event into inactive state and update time fields.
1989 * Enabling the leader of a group effectively enables all
1990 * the group members that aren't explicitly disabled, so we
1991 * have to update their ->tstamp_enabled also.
1992 * Note: this works for group members as well as group leaders
1993 * since the non-leader members' sibling_lists will be empty.
1994 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001995static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001996{
1997 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001998 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001999
2000 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002001 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002002 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002003 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2004 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002005 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002006}
2007
2008/*
2009 * Cross CPU call to enable a performance event
2010 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002011static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002012{
2013 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002014 struct perf_event_context *ctx = event->ctx;
2015 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002016 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002017 int err;
2018
Jiri Olsa06f41792013-07-09 17:44:11 +02002019 /*
2020 * There's a time window between 'ctx->is_active' check
2021 * in perf_event_enable function and this place having:
2022 * - IRQs on
2023 * - ctx->lock unlocked
2024 *
2025 * where the task could be killed and 'ctx' deactivated
2026 * by perf_event_exit_task.
2027 */
2028 if (!ctx->is_active)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002029 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002030
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002031 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002032 update_context_time(ctx);
2033
2034 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2035 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002036
2037 /*
2038 * set current task's cgroup time reference point
2039 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002040 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002041
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002042 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002043
Stephane Eraniane5d13672011-02-14 11:20:01 +02002044 if (!event_filter_match(event)) {
2045 if (is_cgroup_event(event))
2046 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002047 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002048 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002049
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002050 /*
2051 * If the event is in a group and isn't the group leader,
2052 * then don't put it on unless the group is on.
2053 */
2054 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2055 goto unlock;
2056
2057 if (!group_can_go_on(event, cpuctx, 1)) {
2058 err = -EEXIST;
2059 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002060 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002061 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002062 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01002063 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002064 }
2065
2066 if (err) {
2067 /*
2068 * If this event can't go on and it's part of a
2069 * group, then the whole group has to come off.
2070 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002071 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002072 group_sched_out(leader, cpuctx, ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002073 perf_cpu_hrtimer_restart(cpuctx);
2074 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002075 if (leader->attr.pinned) {
2076 update_group_times(leader);
2077 leader->state = PERF_EVENT_STATE_ERROR;
2078 }
2079 }
2080
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002081unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002082 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002083
2084 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002085}
2086
2087/*
2088 * Enable a event.
2089 *
2090 * If event->ctx is a cloned context, callers must make sure that
2091 * every task struct that event->ctx->task could possibly point to
2092 * remains valid. This condition is satisfied when called through
2093 * perf_event_for_each_child or perf_event_for_each as described
2094 * for perf_event_disable.
2095 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01002096void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002097{
2098 struct perf_event_context *ctx = event->ctx;
2099 struct task_struct *task = ctx->task;
2100
2101 if (!task) {
2102 /*
2103 * Enable the event on the cpu that it's on
2104 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002105 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002106 return;
2107 }
2108
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002109 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002110 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2111 goto out;
2112
2113 /*
2114 * If the event is in error state, clear that first.
2115 * That way, if we see the event in error state below, we
2116 * know that it has gone back into error state, as distinct
2117 * from the task having been scheduled away before the
2118 * cross-call arrived.
2119 */
2120 if (event->state == PERF_EVENT_STATE_ERROR)
2121 event->state = PERF_EVENT_STATE_OFF;
2122
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002123retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002124 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002125 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002126 goto out;
2127 }
2128
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002129 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002130
2131 if (!task_function_call(task, __perf_event_enable, event))
2132 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002133
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002134 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002135
2136 /*
2137 * If the context is active and the event is still off,
2138 * we need to retry the cross-call.
2139 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002140 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2141 /*
2142 * task could have been flipped by a concurrent
2143 * perf_event_context_sched_out()
2144 */
2145 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002146 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002147 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002148
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002149out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002150 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002151}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002152EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002153
Avi Kivity26ca5c12011-06-29 18:42:37 +03002154int perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002155{
2156 /*
2157 * not supported on inherited events
2158 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002159 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002160 return -EINVAL;
2161
2162 atomic_add(refresh, &event->event_limit);
2163 perf_event_enable(event);
2164
2165 return 0;
2166}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002167EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002168
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002169static void ctx_sched_out(struct perf_event_context *ctx,
2170 struct perf_cpu_context *cpuctx,
2171 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002172{
2173 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002174 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002175
Peter Zijlstradb24d332011-04-09 21:17:45 +02002176 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002177 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002178 return;
2179
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002180 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002181 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002182 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002183 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002184
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002185 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002186 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002187 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2188 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002189 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002190
Peter Zijlstradb24d332011-04-09 21:17:45 +02002191 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002192 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002193 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002194 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002195 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002196}
2197
2198/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002199 * Test whether two contexts are equivalent, i.e. whether they have both been
2200 * cloned from the same version of the same context.
2201 *
2202 * Equivalence is measured using a generation number in the context that is
2203 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2204 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002205 */
2206static int context_equiv(struct perf_event_context *ctx1,
2207 struct perf_event_context *ctx2)
2208{
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002209 /* Pinning disables the swap optimization */
2210 if (ctx1->pin_count || ctx2->pin_count)
2211 return 0;
2212
2213 /* If ctx1 is the parent of ctx2 */
2214 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2215 return 1;
2216
2217 /* If ctx2 is the parent of ctx1 */
2218 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2219 return 1;
2220
2221 /*
2222 * If ctx1 and ctx2 have the same parent; we flatten the parent
2223 * hierarchy, see perf_event_init_context().
2224 */
2225 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2226 ctx1->parent_gen == ctx2->parent_gen)
2227 return 1;
2228
2229 /* Unmatched */
2230 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002231}
2232
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002233static void __perf_event_sync_stat(struct perf_event *event,
2234 struct perf_event *next_event)
2235{
2236 u64 value;
2237
2238 if (!event->attr.inherit_stat)
2239 return;
2240
2241 /*
2242 * Update the event value, we cannot use perf_event_read()
2243 * because we're in the middle of a context switch and have IRQs
2244 * disabled, which upsets smp_call_function_single(), however
2245 * we know the event must be on the current CPU, therefore we
2246 * don't need to use it.
2247 */
2248 switch (event->state) {
2249 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002250 event->pmu->read(event);
2251 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002252
2253 case PERF_EVENT_STATE_INACTIVE:
2254 update_event_times(event);
2255 break;
2256
2257 default:
2258 break;
2259 }
2260
2261 /*
2262 * In order to keep per-task stats reliable we need to flip the event
2263 * values when we flip the contexts.
2264 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002265 value = local64_read(&next_event->count);
2266 value = local64_xchg(&event->count, value);
2267 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002268
2269 swap(event->total_time_enabled, next_event->total_time_enabled);
2270 swap(event->total_time_running, next_event->total_time_running);
2271
2272 /*
2273 * Since we swizzled the values, update the user visible data too.
2274 */
2275 perf_event_update_userpage(event);
2276 perf_event_update_userpage(next_event);
2277}
2278
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002279static void perf_event_sync_stat(struct perf_event_context *ctx,
2280 struct perf_event_context *next_ctx)
2281{
2282 struct perf_event *event, *next_event;
2283
2284 if (!ctx->nr_stat)
2285 return;
2286
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002287 update_context_time(ctx);
2288
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002289 event = list_first_entry(&ctx->event_list,
2290 struct perf_event, event_entry);
2291
2292 next_event = list_first_entry(&next_ctx->event_list,
2293 struct perf_event, event_entry);
2294
2295 while (&event->event_entry != &ctx->event_list &&
2296 &next_event->event_entry != &next_ctx->event_list) {
2297
2298 __perf_event_sync_stat(event, next_event);
2299
2300 event = list_next_entry(event, event_entry);
2301 next_event = list_next_entry(next_event, event_entry);
2302 }
2303}
2304
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002305static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2306 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002307{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002308 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002309 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002310 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002311 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002312 int do_switch = 1;
2313
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002314 if (likely(!ctx))
2315 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002316
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002317 cpuctx = __get_cpu_context(ctx);
2318 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002319 return;
2320
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002321 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002322 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002323 if (!next_ctx)
2324 goto unlock;
2325
2326 parent = rcu_dereference(ctx->parent_ctx);
2327 next_parent = rcu_dereference(next_ctx->parent_ctx);
2328
2329 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa1f9a7262014-06-24 10:20:25 +02002330 if (!parent || !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002331 goto unlock;
2332
2333 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002334 /*
2335 * Looks like the two contexts are clones, so we might be
2336 * able to optimize the context switch. We lock both
2337 * contexts and check that they are clones under the
2338 * lock (including re-checking that neither has been
2339 * uncloned in the meantime). It doesn't matter which
2340 * order we take the locks because no other cpu could
2341 * be trying to lock both of these tasks.
2342 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002343 raw_spin_lock(&ctx->lock);
2344 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002345 if (context_equiv(ctx, next_ctx)) {
2346 /*
2347 * XXX do we need a memory barrier of sorts
2348 * wrt to rcu_dereference() of perf_event_ctxp
2349 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002350 task->perf_event_ctxp[ctxn] = next_ctx;
2351 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002352 ctx->task = next;
2353 next_ctx->task = task;
2354 do_switch = 0;
2355
2356 perf_event_sync_stat(ctx, next_ctx);
2357 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002358 raw_spin_unlock(&next_ctx->lock);
2359 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002360 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002361unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002362 rcu_read_unlock();
2363
2364 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002365 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002366 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002367 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002368 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002369 }
2370}
2371
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002372#define for_each_task_context_nr(ctxn) \
2373 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2374
2375/*
2376 * Called from scheduler to remove the events of the current task,
2377 * with interrupts disabled.
2378 *
2379 * We stop each event and update the event value in event->count.
2380 *
2381 * This does not protect us against NMI, but disable()
2382 * sets the disabled bit in the control field of event _before_
2383 * accessing the event control register. If a NMI hits, then it will
2384 * not restart the event.
2385 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002386void __perf_event_task_sched_out(struct task_struct *task,
2387 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002388{
2389 int ctxn;
2390
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002391 for_each_task_context_nr(ctxn)
2392 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002393
2394 /*
2395 * if cgroup events exist on this CPU, then we need
2396 * to check if we have to switch out PMU state.
2397 * cgroup event are system-wide mode only
2398 */
2399 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002400 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002401}
2402
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002403static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002404{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002405 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002406
2407 if (!cpuctx->task_ctx)
2408 return;
2409
2410 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2411 return;
2412
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002413 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002414 cpuctx->task_ctx = NULL;
2415}
2416
2417/*
2418 * Called with IRQs disabled
2419 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002420static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2421 enum event_type_t event_type)
2422{
2423 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002424}
2425
2426static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002427ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002428 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002429{
2430 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002431
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002432 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2433 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002434 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002435 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002436 continue;
2437
Stephane Eraniane5d13672011-02-14 11:20:01 +02002438 /* may need to reset tstamp_enabled */
2439 if (is_cgroup_event(event))
2440 perf_cgroup_mark_enabled(event, ctx);
2441
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002442 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002443 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002444
2445 /*
2446 * If this pinned group hasn't been scheduled,
2447 * put it in error state.
2448 */
2449 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2450 update_group_times(event);
2451 event->state = PERF_EVENT_STATE_ERROR;
2452 }
2453 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002454}
2455
2456static void
2457ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002458 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002459{
2460 struct perf_event *event;
2461 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002462
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002463 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2464 /* Ignore events in OFF or ERROR state */
2465 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002466 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002467 /*
2468 * Listen to the 'cpu' scheduling filter constraint
2469 * of events:
2470 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002471 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002472 continue;
2473
Stephane Eraniane5d13672011-02-14 11:20:01 +02002474 /* may need to reset tstamp_enabled */
2475 if (is_cgroup_event(event))
2476 perf_cgroup_mark_enabled(event, ctx);
2477
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002478 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002479 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002480 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002481 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002482 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002483}
2484
2485static void
2486ctx_sched_in(struct perf_event_context *ctx,
2487 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002488 enum event_type_t event_type,
2489 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002490{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002491 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002492 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002493
Peter Zijlstradb24d332011-04-09 21:17:45 +02002494 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002495 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002496 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002497
Stephane Eraniane5d13672011-02-14 11:20:01 +02002498 now = perf_clock();
2499 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002500 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002501 /*
2502 * First go through the list and put on any pinned groups
2503 * in order to give them the best chance of going on.
2504 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002505 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002506 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002507
2508 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002509 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002510 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002511}
2512
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002513static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002514 enum event_type_t event_type,
2515 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002516{
2517 struct perf_event_context *ctx = &cpuctx->ctx;
2518
Stephane Eraniane5d13672011-02-14 11:20:01 +02002519 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002520}
2521
Stephane Eraniane5d13672011-02-14 11:20:01 +02002522static void perf_event_context_sched_in(struct perf_event_context *ctx,
2523 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002524{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002525 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002526
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002527 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002528 if (cpuctx->task_ctx == ctx)
2529 return;
2530
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002531 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002532 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002533 /*
2534 * We want to keep the following priority order:
2535 * cpu pinned (that don't need to move), task pinned,
2536 * cpu flexible, task flexible.
2537 */
2538 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2539
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002540 if (ctx->nr_events)
2541 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002542
Gleb Natapov86b47c22011-11-22 16:08:21 +02002543 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2544
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002545 perf_pmu_enable(ctx->pmu);
2546 perf_ctx_unlock(cpuctx, ctx);
2547
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002548 /*
2549 * Since these rotations are per-cpu, we need to ensure the
2550 * cpu-context we got scheduled on is actually rotating.
2551 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002552 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002553}
2554
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002555/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002556 * When sampling the branck stack in system-wide, it may be necessary
2557 * to flush the stack on context switch. This happens when the branch
2558 * stack does not tag its entries with the pid of the current task.
2559 * Otherwise it becomes impossible to associate a branch entry with a
2560 * task. This ambiguity is more likely to appear when the branch stack
2561 * supports priv level filtering and the user sets it to monitor only
2562 * at the user level (which could be a useful measurement in system-wide
2563 * mode). In that case, the risk is high of having a branch stack with
2564 * branch from multiple tasks. Flushing may mean dropping the existing
2565 * entries or stashing them somewhere in the PMU specific code layer.
2566 *
2567 * This function provides the context switch callback to the lower code
2568 * layer. It is invoked ONLY when there is at least one system-wide context
2569 * with at least one active event using taken branch sampling.
2570 */
2571static void perf_branch_stack_sched_in(struct task_struct *prev,
2572 struct task_struct *task)
2573{
2574 struct perf_cpu_context *cpuctx;
2575 struct pmu *pmu;
2576 unsigned long flags;
2577
2578 /* no need to flush branch stack if not changing task */
2579 if (prev == task)
2580 return;
2581
2582 local_irq_save(flags);
2583
2584 rcu_read_lock();
2585
2586 list_for_each_entry_rcu(pmu, &pmus, entry) {
2587 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2588
2589 /*
2590 * check if the context has at least one
2591 * event using PERF_SAMPLE_BRANCH_STACK
2592 */
2593 if (cpuctx->ctx.nr_branch_stack > 0
2594 && pmu->flush_branch_stack) {
2595
Stephane Eraniand010b332012-02-09 23:21:00 +01002596 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2597
2598 perf_pmu_disable(pmu);
2599
2600 pmu->flush_branch_stack();
2601
2602 perf_pmu_enable(pmu);
2603
2604 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2605 }
2606 }
2607
2608 rcu_read_unlock();
2609
2610 local_irq_restore(flags);
2611}
2612
2613/*
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002614 * Called from scheduler to add the events of the current task
2615 * with interrupts disabled.
2616 *
2617 * We restore the event value and then enable it.
2618 *
2619 * This does not protect us against NMI, but enable()
2620 * sets the enabled bit in the control field of event _before_
2621 * accessing the event control register. If a NMI hits, then it will
2622 * keep the event running.
2623 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002624void __perf_event_task_sched_in(struct task_struct *prev,
2625 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002626{
2627 struct perf_event_context *ctx;
2628 int ctxn;
2629
2630 for_each_task_context_nr(ctxn) {
2631 ctx = task->perf_event_ctxp[ctxn];
2632 if (likely(!ctx))
2633 continue;
2634
Stephane Eraniane5d13672011-02-14 11:20:01 +02002635 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002636 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002637 /*
2638 * if cgroup events exist on this CPU, then we need
2639 * to check if we have to switch in PMU state.
2640 * cgroup event are system-wide mode only
2641 */
2642 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002643 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002644
2645 /* check for system-wide branch_stack events */
2646 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2647 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002648}
2649
Peter Zijlstraabd50712010-01-26 18:50:16 +01002650static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2651{
2652 u64 frequency = event->attr.sample_freq;
2653 u64 sec = NSEC_PER_SEC;
2654 u64 divisor, dividend;
2655
2656 int count_fls, nsec_fls, frequency_fls, sec_fls;
2657
2658 count_fls = fls64(count);
2659 nsec_fls = fls64(nsec);
2660 frequency_fls = fls64(frequency);
2661 sec_fls = 30;
2662
2663 /*
2664 * We got @count in @nsec, with a target of sample_freq HZ
2665 * the target period becomes:
2666 *
2667 * @count * 10^9
2668 * period = -------------------
2669 * @nsec * sample_freq
2670 *
2671 */
2672
2673 /*
2674 * Reduce accuracy by one bit such that @a and @b converge
2675 * to a similar magnitude.
2676 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002677#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002678do { \
2679 if (a##_fls > b##_fls) { \
2680 a >>= 1; \
2681 a##_fls--; \
2682 } else { \
2683 b >>= 1; \
2684 b##_fls--; \
2685 } \
2686} while (0)
2687
2688 /*
2689 * Reduce accuracy until either term fits in a u64, then proceed with
2690 * the other, so that finally we can do a u64/u64 division.
2691 */
2692 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2693 REDUCE_FLS(nsec, frequency);
2694 REDUCE_FLS(sec, count);
2695 }
2696
2697 if (count_fls + sec_fls > 64) {
2698 divisor = nsec * frequency;
2699
2700 while (count_fls + sec_fls > 64) {
2701 REDUCE_FLS(count, sec);
2702 divisor >>= 1;
2703 }
2704
2705 dividend = count * sec;
2706 } else {
2707 dividend = count * sec;
2708
2709 while (nsec_fls + frequency_fls > 64) {
2710 REDUCE_FLS(nsec, frequency);
2711 dividend >>= 1;
2712 }
2713
2714 divisor = nsec * frequency;
2715 }
2716
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002717 if (!divisor)
2718 return dividend;
2719
Peter Zijlstraabd50712010-01-26 18:50:16 +01002720 return div64_u64(dividend, divisor);
2721}
2722
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002723static DEFINE_PER_CPU(int, perf_throttled_count);
2724static DEFINE_PER_CPU(u64, perf_throttled_seq);
2725
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002726static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002727{
2728 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002729 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002730 s64 delta;
2731
Peter Zijlstraabd50712010-01-26 18:50:16 +01002732 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002733
2734 delta = (s64)(period - hwc->sample_period);
2735 delta = (delta + 7) / 8; /* low pass filter */
2736
2737 sample_period = hwc->sample_period + delta;
2738
2739 if (!sample_period)
2740 sample_period = 1;
2741
2742 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002743
Peter Zijlstrae7850592010-05-21 14:43:08 +02002744 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002745 if (disable)
2746 event->pmu->stop(event, PERF_EF_UPDATE);
2747
Peter Zijlstrae7850592010-05-21 14:43:08 +02002748 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002749
2750 if (disable)
2751 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002752 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002753}
2754
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002755/*
2756 * combine freq adjustment with unthrottling to avoid two passes over the
2757 * events. At the same time, make sure, having freq events does not change
2758 * the rate of unthrottling as that would introduce bias.
2759 */
2760static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2761 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002762{
2763 struct perf_event *event;
2764 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002765 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002766 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002767
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002768 /*
2769 * only need to iterate over all events iff:
2770 * - context have events in frequency mode (needs freq adjust)
2771 * - there are events to unthrottle on this cpu
2772 */
2773 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002774 return;
2775
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002776 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002777 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002778
Paul Mackerras03541f82009-10-14 16:58:03 +11002779 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002780 if (event->state != PERF_EVENT_STATE_ACTIVE)
2781 continue;
2782
Stephane Eranian5632ab12011-01-03 18:20:01 +02002783 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002784 continue;
2785
Alexander Shishkin44377272013-12-16 14:17:36 +02002786 perf_pmu_disable(event->pmu);
2787
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002788 hwc = &event->hw;
2789
Jiri Olsaae23bff2013-08-24 16:45:54 +02002790 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002791 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002792 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002793 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002794 }
2795
2796 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02002797 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002798
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002799 /*
2800 * stop the event and update event->count
2801 */
2802 event->pmu->stop(event, PERF_EF_UPDATE);
2803
Peter Zijlstrae7850592010-05-21 14:43:08 +02002804 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002805 delta = now - hwc->freq_count_stamp;
2806 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002807
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002808 /*
2809 * restart the event
2810 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002811 * we have stopped the event so tell that
2812 * to perf_adjust_period() to avoid stopping it
2813 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002814 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002815 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002816 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002817
2818 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02002819 next:
2820 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002821 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002822
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002823 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002824 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002825}
2826
2827/*
2828 * Round-robin a context's events:
2829 */
2830static void rotate_ctx(struct perf_event_context *ctx)
2831{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002832 /*
2833 * Rotate the first entry last of non-pinned groups. Rotation might be
2834 * disabled by the inheritance code.
2835 */
2836 if (!ctx->rotate_disable)
2837 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002838}
2839
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002840/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002841 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2842 * because they're strictly cpu affine and rotate_start is called with IRQs
2843 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002844 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002845static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002846{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002847 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002848 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002849
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002850 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002851 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002852 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2853 rotate = 1;
2854 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002855
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002856 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002857 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002858 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002859 if (ctx->nr_events != ctx->nr_active)
2860 rotate = 1;
2861 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002862
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002863 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002864 goto done;
2865
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002866 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002867 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002868
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002869 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2870 if (ctx)
2871 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002872
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002873 rotate_ctx(&cpuctx->ctx);
2874 if (ctx)
2875 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002876
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002877 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002878
2879 perf_pmu_enable(cpuctx->ctx.pmu);
2880 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002881done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002882 if (remove)
2883 list_del_init(&cpuctx->rotation_list);
Stephane Eranian9e630202013-04-03 14:21:33 +02002884
2885 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002886}
2887
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002888#ifdef CONFIG_NO_HZ_FULL
2889bool perf_event_can_stop_tick(void)
2890{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02002891 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002892 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002893 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002894 else
2895 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002896}
2897#endif
2898
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002899void perf_event_task_tick(void)
2900{
2901 struct list_head *head = &__get_cpu_var(rotation_list);
2902 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002903 struct perf_event_context *ctx;
2904 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002905
2906 WARN_ON(!irqs_disabled());
2907
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002908 __this_cpu_inc(perf_throttled_seq);
2909 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2910
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002911 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002912 ctx = &cpuctx->ctx;
2913 perf_adjust_freq_unthr_context(ctx, throttled);
2914
2915 ctx = cpuctx->task_ctx;
2916 if (ctx)
2917 perf_adjust_freq_unthr_context(ctx, throttled);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002918 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002919}
2920
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002921static int event_enable_on_exec(struct perf_event *event,
2922 struct perf_event_context *ctx)
2923{
2924 if (!event->attr.enable_on_exec)
2925 return 0;
2926
2927 event->attr.enable_on_exec = 0;
2928 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2929 return 0;
2930
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002931 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002932
2933 return 1;
2934}
2935
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002936/*
2937 * Enable all of a task's events that have been marked enable-on-exec.
2938 * This expects task == current.
2939 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002940static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002941{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002942 struct perf_event *event;
2943 unsigned long flags;
2944 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002945 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002946
2947 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002948 if (!ctx || !ctx->nr_events)
2949 goto out;
2950
Stephane Eraniane566b762011-04-06 02:54:54 +02002951 /*
2952 * We must ctxsw out cgroup events to avoid conflict
2953 * when invoking perf_task_event_sched_in() later on
2954 * in this function. Otherwise we end up trying to
2955 * ctxswin cgroup events which are already scheduled
2956 * in.
2957 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002958 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002959
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002960 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002961 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002962
Peter Zijlstrab79387e2011-11-22 11:25:43 +01002963 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002964 ret = event_enable_on_exec(event, ctx);
2965 if (ret)
2966 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002967 }
2968
2969 /*
2970 * Unclone this context if we enabled any event.
2971 */
2972 if (enabled)
2973 unclone_ctx(ctx);
2974
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002975 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002976
Stephane Eraniane566b762011-04-06 02:54:54 +02002977 /*
2978 * Also calls ctxswin for cgroup events, if any:
2979 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002980 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002981out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002982 local_irq_restore(flags);
2983}
2984
Peter Zijlstrae041e322014-05-21 17:32:19 +02002985void perf_event_exec(void)
2986{
2987 struct perf_event_context *ctx;
2988 int ctxn;
2989
2990 rcu_read_lock();
2991 for_each_task_context_nr(ctxn) {
2992 ctx = current->perf_event_ctxp[ctxn];
2993 if (!ctx)
2994 continue;
2995
2996 perf_event_enable_on_exec(ctx);
2997 }
2998 rcu_read_unlock();
2999}
3000
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003001/*
3002 * Cross CPU call to read the hardware event
3003 */
3004static void __perf_event_read(void *info)
3005{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003006 struct perf_event *event = info;
3007 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003008 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003009
3010 /*
3011 * If this is a task context, we need to check whether it is
3012 * the current task context of this cpu. If not it has been
3013 * scheduled out before the smp call arrived. In that case
3014 * event->count would have been updated to a recent sample
3015 * when the event was scheduled out.
3016 */
3017 if (ctx->task && cpuctx->task_ctx != ctx)
3018 return;
3019
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003020 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003021 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003022 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003023 update_cgrp_time_from_event(event);
3024 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003025 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003026 if (event->state == PERF_EVENT_STATE_ACTIVE)
3027 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003028 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003029}
3030
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003031static inline u64 perf_event_count(struct perf_event *event)
3032{
Peter Zijlstrae7850592010-05-21 14:43:08 +02003033 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003034}
3035
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003036static u64 perf_event_read(struct perf_event *event)
3037{
3038 /*
3039 * If event is enabled and currently active on a CPU, update the
3040 * value in the event structure:
3041 */
3042 if (event->state == PERF_EVENT_STATE_ACTIVE) {
3043 smp_call_function_single(event->oncpu,
3044 __perf_event_read, event, 1);
3045 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003046 struct perf_event_context *ctx = event->ctx;
3047 unsigned long flags;
3048
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003049 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003050 /*
3051 * may read while context is not active
3052 * (e.g., thread is blocked), in that case
3053 * we cannot update context time
3054 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003055 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003056 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003057 update_cgrp_time_from_event(event);
3058 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003059 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003060 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003061 }
3062
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003063 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003064}
3065
3066/*
3067 * Initialize the perf_event context in a task_struct:
3068 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003069static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003070{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003071 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003072 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003073 INIT_LIST_HEAD(&ctx->pinned_groups);
3074 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003075 INIT_LIST_HEAD(&ctx->event_list);
3076 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003077}
3078
Peter Zijlstraeb184472010-09-07 15:55:13 +02003079static struct perf_event_context *
3080alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003081{
3082 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003083
3084 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3085 if (!ctx)
3086 return NULL;
3087
3088 __perf_event_init_context(ctx);
3089 if (task) {
3090 ctx->task = task;
3091 get_task_struct(task);
3092 }
3093 ctx->pmu = pmu;
3094
3095 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003096}
3097
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003098static struct task_struct *
3099find_lively_task_by_vpid(pid_t vpid)
3100{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003101 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003102 int err;
3103
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003104 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003105 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003106 task = current;
3107 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003108 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003109 if (task)
3110 get_task_struct(task);
3111 rcu_read_unlock();
3112
3113 if (!task)
3114 return ERR_PTR(-ESRCH);
3115
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003116 /* Reuse ptrace permission checks for now. */
3117 err = -EACCES;
3118 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3119 goto errout;
3120
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003121 return task;
3122errout:
3123 put_task_struct(task);
3124 return ERR_PTR(err);
3125
3126}
3127
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003128/*
3129 * Returns a matching context with refcount and pincount.
3130 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003131static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07003132find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003133{
3134 struct perf_event_context *ctx;
3135 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003136 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003137 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003138
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003139 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003140 /* Must be root to operate on a CPU event: */
3141 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3142 return ERR_PTR(-EACCES);
3143
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003144 /*
3145 * We could be clever and allow to attach a event to an
3146 * offline CPU and activate it when the CPU comes up, but
3147 * that's for later.
3148 */
3149 if (!cpu_online(cpu))
3150 return ERR_PTR(-ENODEV);
3151
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003152 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003153 ctx = &cpuctx->ctx;
3154 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003155 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003156
3157 return ctx;
3158 }
3159
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003160 err = -EINVAL;
3161 ctxn = pmu->task_ctx_nr;
3162 if (ctxn < 0)
3163 goto errout;
3164
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003165retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003166 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003167 if (ctx) {
3168 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003169 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003170 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003171 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003172 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003173 err = -ENOMEM;
3174 if (!ctx)
3175 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003176
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003177 err = 0;
3178 mutex_lock(&task->perf_event_mutex);
3179 /*
3180 * If it has already passed perf_event_exit_task().
3181 * we must see PF_EXITING, it takes this mutex too.
3182 */
3183 if (task->flags & PF_EXITING)
3184 err = -ESRCH;
3185 else if (task->perf_event_ctxp[ctxn])
3186 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003187 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003188 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003189 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003190 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003191 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003192 mutex_unlock(&task->perf_event_mutex);
3193
3194 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003195 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003196
3197 if (err == -EAGAIN)
3198 goto retry;
3199 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003200 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003201 }
3202
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003203 return ctx;
3204
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003205errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003206 return ERR_PTR(err);
3207}
3208
Li Zefan6fb29152009-10-15 11:21:42 +08003209static void perf_event_free_filter(struct perf_event *event);
3210
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003211static void free_event_rcu(struct rcu_head *head)
3212{
3213 struct perf_event *event;
3214
3215 event = container_of(head, struct perf_event, rcu_head);
3216 if (event->ns)
3217 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003218 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003219 kfree(event);
3220}
3221
Frederic Weisbecker76369132011-05-19 19:55:04 +02003222static void ring_buffer_put(struct ring_buffer *rb);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003223static void ring_buffer_attach(struct perf_event *event,
3224 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003225
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003226static void unaccount_event_cpu(struct perf_event *event, int cpu)
3227{
3228 if (event->parent)
3229 return;
3230
3231 if (has_branch_stack(event)) {
3232 if (!(event->attach_state & PERF_ATTACH_TASK))
3233 atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
3234 }
3235 if (is_cgroup_event(event))
3236 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3237}
3238
3239static void unaccount_event(struct perf_event *event)
3240{
3241 if (event->parent)
3242 return;
3243
3244 if (event->attach_state & PERF_ATTACH_TASK)
3245 static_key_slow_dec_deferred(&perf_sched_events);
3246 if (event->attr.mmap || event->attr.mmap_data)
3247 atomic_dec(&nr_mmap_events);
3248 if (event->attr.comm)
3249 atomic_dec(&nr_comm_events);
3250 if (event->attr.task)
3251 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003252 if (event->attr.freq)
3253 atomic_dec(&nr_freq_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003254 if (is_cgroup_event(event))
3255 static_key_slow_dec_deferred(&perf_sched_events);
3256 if (has_branch_stack(event))
3257 static_key_slow_dec_deferred(&perf_sched_events);
3258
3259 unaccount_event_cpu(event, event->cpu);
3260}
3261
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003262static void __free_event(struct perf_event *event)
3263{
3264 if (!event->parent) {
3265 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3266 put_callchain_buffers();
3267 }
3268
3269 if (event->destroy)
3270 event->destroy(event);
3271
3272 if (event->ctx)
3273 put_ctx(event->ctx);
3274
Yan, Zhengc464c762014-03-18 16:56:41 +08003275 if (event->pmu)
3276 module_put(event->pmu->module);
3277
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003278 call_rcu(&event->rcu_head, free_event_rcu);
3279}
Peter Zijlstra683ede42014-05-05 12:11:24 +02003280
3281static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003282{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003283 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003284
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003285 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003286
Frederic Weisbecker76369132011-05-19 19:55:04 +02003287 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003288 /*
3289 * Can happen when we close an event with re-directed output.
3290 *
3291 * Since we have a 0 refcount, perf_mmap_close() will skip
3292 * over us; possibly making our ring_buffer_put() the last.
3293 */
3294 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003295 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003296 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003297 }
3298
Stephane Eraniane5d13672011-02-14 11:20:01 +02003299 if (is_cgroup_event(event))
3300 perf_detach_cgroup(event);
3301
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003302 __free_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003303}
3304
Peter Zijlstra683ede42014-05-05 12:11:24 +02003305/*
3306 * Used to free events which have a known refcount of 1, such as in error paths
3307 * where the event isn't exposed yet and inherited events.
3308 */
3309static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003310{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003311 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3312 "unexpected event refcount: %ld; ptr=%p\n",
3313 atomic_long_read(&event->refcount), event)) {
3314 /* leak to avoid use-after-free */
3315 return;
3316 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003317
Peter Zijlstra683ede42014-05-05 12:11:24 +02003318 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003319}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003320
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003321/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003322 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003323 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003324static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003325{
Peter Zijlstra88821352010-11-09 19:01:43 +01003326 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003327
Peter Zijlstra88821352010-11-09 19:01:43 +01003328 rcu_read_lock();
3329 owner = ACCESS_ONCE(event->owner);
3330 /*
3331 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3332 * !owner it means the list deletion is complete and we can indeed
3333 * free this event, otherwise we need to serialize on
3334 * owner->perf_event_mutex.
3335 */
3336 smp_read_barrier_depends();
3337 if (owner) {
3338 /*
3339 * Since delayed_put_task_struct() also drops the last
3340 * task reference we can safely take a new reference
3341 * while holding the rcu_read_lock().
3342 */
3343 get_task_struct(owner);
3344 }
3345 rcu_read_unlock();
3346
3347 if (owner) {
3348 mutex_lock(&owner->perf_event_mutex);
3349 /*
3350 * We have to re-check the event->owner field, if it is cleared
3351 * we raced with perf_event_exit_task(), acquiring the mutex
3352 * ensured they're done, and we can proceed with freeing the
3353 * event.
3354 */
3355 if (event->owner)
3356 list_del_init(&event->owner_entry);
3357 mutex_unlock(&owner->perf_event_mutex);
3358 put_task_struct(owner);
3359 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003360}
3361
3362/*
3363 * Called when the last reference to the file is gone.
3364 */
3365static void put_event(struct perf_event *event)
3366{
3367 struct perf_event_context *ctx = event->ctx;
3368
3369 if (!atomic_long_dec_and_test(&event->refcount))
3370 return;
3371
3372 if (!is_kernel_event(event))
3373 perf_remove_from_owner(event);
Peter Zijlstra88821352010-11-09 19:01:43 +01003374
Peter Zijlstra683ede42014-05-05 12:11:24 +02003375 WARN_ON_ONCE(ctx->parent_ctx);
3376 /*
3377 * There are two ways this annotation is useful:
3378 *
3379 * 1) there is a lock recursion from perf_event_exit_task
3380 * see the comment there.
3381 *
3382 * 2) there is a lock-inversion with mmap_sem through
3383 * perf_event_read_group(), which takes faults while
3384 * holding ctx->mutex, however this is called after
3385 * the last filedesc died, so there is no possibility
3386 * to trigger the AB-BA case.
3387 */
3388 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3389 perf_remove_from_context(event, true);
3390 mutex_unlock(&ctx->mutex);
3391
3392 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003393}
3394
Peter Zijlstra683ede42014-05-05 12:11:24 +02003395int perf_event_release_kernel(struct perf_event *event)
3396{
3397 put_event(event);
3398 return 0;
3399}
3400EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3401
Al Viroa6fa9412012-08-20 14:59:25 +01003402static int perf_release(struct inode *inode, struct file *file)
3403{
3404 put_event(file->private_data);
3405 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003406}
3407
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003408u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003409{
3410 struct perf_event *child;
3411 u64 total = 0;
3412
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003413 *enabled = 0;
3414 *running = 0;
3415
Peter Zijlstra6f105812009-11-20 22:19:56 +01003416 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003417 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003418 *enabled += event->total_time_enabled +
3419 atomic64_read(&event->child_total_time_enabled);
3420 *running += event->total_time_running +
3421 atomic64_read(&event->child_total_time_running);
3422
3423 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003424 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003425 *enabled += child->total_time_enabled;
3426 *running += child->total_time_running;
3427 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003428 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003429
3430 return total;
3431}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003432EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003433
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003434static int perf_event_read_group(struct perf_event *event,
3435 u64 read_format, char __user *buf)
3436{
3437 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003438 int n = 0, size = 0, ret = -EFAULT;
3439 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003440 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003441 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003442
Peter Zijlstra6f105812009-11-20 22:19:56 +01003443 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003444 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003445
3446 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003447 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3448 values[n++] = enabled;
3449 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3450 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003451 values[n++] = count;
3452 if (read_format & PERF_FORMAT_ID)
3453 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003454
3455 size = n * sizeof(u64);
3456
3457 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003458 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003459
Peter Zijlstra6f105812009-11-20 22:19:56 +01003460 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003461
3462 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003463 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003464
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003465 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003466 if (read_format & PERF_FORMAT_ID)
3467 values[n++] = primary_event_id(sub);
3468
3469 size = n * sizeof(u64);
3470
Stephane Eranian184d3da2009-11-23 21:40:49 -08003471 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003472 ret = -EFAULT;
3473 goto unlock;
3474 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003475
3476 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003477 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003478unlock:
3479 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003480
Peter Zijlstraabf48682009-11-20 22:19:49 +01003481 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003482}
3483
3484static int perf_event_read_one(struct perf_event *event,
3485 u64 read_format, char __user *buf)
3486{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003487 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003488 u64 values[4];
3489 int n = 0;
3490
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003491 values[n++] = perf_event_read_value(event, &enabled, &running);
3492 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3493 values[n++] = enabled;
3494 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3495 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003496 if (read_format & PERF_FORMAT_ID)
3497 values[n++] = primary_event_id(event);
3498
3499 if (copy_to_user(buf, values, n * sizeof(u64)))
3500 return -EFAULT;
3501
3502 return n * sizeof(u64);
3503}
3504
3505/*
3506 * Read the performance event - simple non blocking version for now
3507 */
3508static ssize_t
3509perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3510{
3511 u64 read_format = event->attr.read_format;
3512 int ret;
3513
3514 /*
3515 * Return end-of-file for a read on a event that is in
3516 * error state (i.e. because it was pinned but it couldn't be
3517 * scheduled on to the CPU at some point).
3518 */
3519 if (event->state == PERF_EVENT_STATE_ERROR)
3520 return 0;
3521
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003522 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003523 return -ENOSPC;
3524
3525 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003526 if (read_format & PERF_FORMAT_GROUP)
3527 ret = perf_event_read_group(event, read_format, buf);
3528 else
3529 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003530
3531 return ret;
3532}
3533
3534static ssize_t
3535perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3536{
3537 struct perf_event *event = file->private_data;
3538
3539 return perf_read_hw(event, buf, count);
3540}
3541
3542static unsigned int perf_poll(struct file *file, poll_table *wait)
3543{
3544 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003545 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003546 unsigned int events = POLL_HUP;
3547
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003548 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003549 * Pin the event->rb by taking event->mmap_mutex; otherwise
3550 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003551 */
3552 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003553 rb = event->rb;
3554 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003555 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003556 mutex_unlock(&event->mmap_mutex);
3557
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003558 poll_wait(file, &event->waitq, wait);
3559
3560 return events;
3561}
3562
3563static void perf_event_reset(struct perf_event *event)
3564{
3565 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003566 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003567 perf_event_update_userpage(event);
3568}
3569
3570/*
3571 * Holding the top-level event's child_mutex means that any
3572 * descendant process that has inherited this event will block
3573 * in sync_child_event if it goes to exit, thus satisfying the
3574 * task existence requirements of perf_event_enable/disable.
3575 */
3576static void perf_event_for_each_child(struct perf_event *event,
3577 void (*func)(struct perf_event *))
3578{
3579 struct perf_event *child;
3580
3581 WARN_ON_ONCE(event->ctx->parent_ctx);
3582 mutex_lock(&event->child_mutex);
3583 func(event);
3584 list_for_each_entry(child, &event->child_list, child_list)
3585 func(child);
3586 mutex_unlock(&event->child_mutex);
3587}
3588
3589static void perf_event_for_each(struct perf_event *event,
3590 void (*func)(struct perf_event *))
3591{
3592 struct perf_event_context *ctx = event->ctx;
3593 struct perf_event *sibling;
3594
3595 WARN_ON_ONCE(ctx->parent_ctx);
3596 mutex_lock(&ctx->mutex);
3597 event = event->group_leader;
3598
3599 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003600 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003601 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003602 mutex_unlock(&ctx->mutex);
3603}
3604
3605static int perf_event_period(struct perf_event *event, u64 __user *arg)
3606{
3607 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrabad71922013-11-27 13:54:38 +00003608 int ret = 0, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003609 u64 value;
3610
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003611 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003612 return -EINVAL;
3613
John Blackwoodad0cf342010-09-28 18:03:11 -04003614 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003615 return -EFAULT;
3616
3617 if (!value)
3618 return -EINVAL;
3619
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003620 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003621 if (event->attr.freq) {
3622 if (value > sysctl_perf_event_sample_rate) {
3623 ret = -EINVAL;
3624 goto unlock;
3625 }
3626
3627 event->attr.sample_freq = value;
3628 } else {
3629 event->attr.sample_period = value;
3630 event->hw.sample_period = value;
3631 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00003632
3633 active = (event->state == PERF_EVENT_STATE_ACTIVE);
3634 if (active) {
3635 perf_pmu_disable(ctx->pmu);
3636 event->pmu->stop(event, PERF_EF_UPDATE);
3637 }
3638
3639 local64_set(&event->hw.period_left, 0);
3640
3641 if (active) {
3642 event->pmu->start(event, PERF_EF_RELOAD);
3643 perf_pmu_enable(ctx->pmu);
3644 }
3645
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003646unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003647 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003648
3649 return ret;
3650}
3651
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003652static const struct file_operations perf_fops;
3653
Al Viro2903ff02012-08-28 12:52:22 -04003654static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003655{
Al Viro2903ff02012-08-28 12:52:22 -04003656 struct fd f = fdget(fd);
3657 if (!f.file)
3658 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003659
Al Viro2903ff02012-08-28 12:52:22 -04003660 if (f.file->f_op != &perf_fops) {
3661 fdput(f);
3662 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003663 }
Al Viro2903ff02012-08-28 12:52:22 -04003664 *p = f;
3665 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003666}
3667
3668static int perf_event_set_output(struct perf_event *event,
3669 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003670static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003671
3672static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3673{
3674 struct perf_event *event = file->private_data;
3675 void (*func)(struct perf_event *);
3676 u32 flags = arg;
3677
3678 switch (cmd) {
3679 case PERF_EVENT_IOC_ENABLE:
3680 func = perf_event_enable;
3681 break;
3682 case PERF_EVENT_IOC_DISABLE:
3683 func = perf_event_disable;
3684 break;
3685 case PERF_EVENT_IOC_RESET:
3686 func = perf_event_reset;
3687 break;
3688
3689 case PERF_EVENT_IOC_REFRESH:
3690 return perf_event_refresh(event, arg);
3691
3692 case PERF_EVENT_IOC_PERIOD:
3693 return perf_event_period(event, (u64 __user *)arg);
3694
Jiri Olsacf4957f2012-10-24 13:37:58 +02003695 case PERF_EVENT_IOC_ID:
3696 {
3697 u64 id = primary_event_id(event);
3698
3699 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
3700 return -EFAULT;
3701 return 0;
3702 }
3703
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003704 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003705 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003706 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003707 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003708 struct perf_event *output_event;
3709 struct fd output;
3710 ret = perf_fget_light(arg, &output);
3711 if (ret)
3712 return ret;
3713 output_event = output.file->private_data;
3714 ret = perf_event_set_output(event, output_event);
3715 fdput(output);
3716 } else {
3717 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003718 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003719 return ret;
3720 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003721
Li Zefan6fb29152009-10-15 11:21:42 +08003722 case PERF_EVENT_IOC_SET_FILTER:
3723 return perf_event_set_filter(event, (void __user *)arg);
3724
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003725 default:
3726 return -ENOTTY;
3727 }
3728
3729 if (flags & PERF_IOC_FLAG_GROUP)
3730 perf_event_for_each(event, func);
3731 else
3732 perf_event_for_each_child(event, func);
3733
3734 return 0;
3735}
3736
3737int perf_event_task_enable(void)
3738{
3739 struct perf_event *event;
3740
3741 mutex_lock(&current->perf_event_mutex);
3742 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3743 perf_event_for_each_child(event, perf_event_enable);
3744 mutex_unlock(&current->perf_event_mutex);
3745
3746 return 0;
3747}
3748
3749int perf_event_task_disable(void)
3750{
3751 struct perf_event *event;
3752
3753 mutex_lock(&current->perf_event_mutex);
3754 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3755 perf_event_for_each_child(event, perf_event_disable);
3756 mutex_unlock(&current->perf_event_mutex);
3757
3758 return 0;
3759}
3760
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003761static int perf_event_index(struct perf_event *event)
3762{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003763 if (event->hw.state & PERF_HES_STOPPED)
3764 return 0;
3765
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003766 if (event->state != PERF_EVENT_STATE_ACTIVE)
3767 return 0;
3768
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01003769 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003770}
3771
Eric B Munsonc4794292011-06-23 16:34:38 -04003772static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003773 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04003774 u64 *enabled,
3775 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04003776{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003777 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04003778
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003779 *now = perf_clock();
3780 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04003781 *enabled = ctx_time - event->tstamp_enabled;
3782 *running = ctx_time - event->tstamp_running;
3783}
3784
Peter Zijlstrafa731582013-09-19 10:16:42 +02003785static void perf_event_init_userpage(struct perf_event *event)
3786{
3787 struct perf_event_mmap_page *userpg;
3788 struct ring_buffer *rb;
3789
3790 rcu_read_lock();
3791 rb = rcu_dereference(event->rb);
3792 if (!rb)
3793 goto unlock;
3794
3795 userpg = rb->user_page;
3796
3797 /* Allow new userspace to detect that bit 0 is deprecated */
3798 userpg->cap_bit0_is_deprecated = 1;
3799 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
3800
3801unlock:
3802 rcu_read_unlock();
3803}
3804
Peter Zijlstrac7206202012-03-22 17:26:36 +01003805void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003806{
3807}
3808
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003809/*
3810 * Callers need to ensure there can be no nesting of this function, otherwise
3811 * the seqlock logic goes bad. We can not serialize this because the arch
3812 * code calls this from NMI context.
3813 */
3814void perf_event_update_userpage(struct perf_event *event)
3815{
3816 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003817 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003818 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003819
3820 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02003821 rb = rcu_dereference(event->rb);
3822 if (!rb)
3823 goto unlock;
3824
Eric B Munson0d641202011-06-24 12:26:26 -04003825 /*
3826 * compute total_time_enabled, total_time_running
3827 * based on snapshot values taken when the event
3828 * was last scheduled in.
3829 *
3830 * we cannot simply called update_context_time()
3831 * because of locking issue as we can be called in
3832 * NMI context
3833 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003834 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003835
Frederic Weisbecker76369132011-05-19 19:55:04 +02003836 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003837 /*
3838 * Disable preemption so as to not let the corresponding user-space
3839 * spin too long if we get preempted.
3840 */
3841 preempt_disable();
3842 ++userpg->lock;
3843 barrier();
3844 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003845 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01003846 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003847 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003848
Eric B Munson0d641202011-06-24 12:26:26 -04003849 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003850 atomic64_read(&event->child_total_time_enabled);
3851
Eric B Munson0d641202011-06-24 12:26:26 -04003852 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003853 atomic64_read(&event->child_total_time_running);
3854
Peter Zijlstrac7206202012-03-22 17:26:36 +01003855 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003856
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003857 barrier();
3858 ++userpg->lock;
3859 preempt_enable();
3860unlock:
3861 rcu_read_unlock();
3862}
3863
Peter Zijlstra906010b2009-09-21 16:08:49 +02003864static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3865{
3866 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003867 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003868 int ret = VM_FAULT_SIGBUS;
3869
3870 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3871 if (vmf->pgoff == 0)
3872 ret = 0;
3873 return ret;
3874 }
3875
3876 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003877 rb = rcu_dereference(event->rb);
3878 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003879 goto unlock;
3880
3881 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3882 goto unlock;
3883
Frederic Weisbecker76369132011-05-19 19:55:04 +02003884 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003885 if (!vmf->page)
3886 goto unlock;
3887
3888 get_page(vmf->page);
3889 vmf->page->mapping = vma->vm_file->f_mapping;
3890 vmf->page->index = vmf->pgoff;
3891
3892 ret = 0;
3893unlock:
3894 rcu_read_unlock();
3895
3896 return ret;
3897}
3898
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003899static void ring_buffer_attach(struct perf_event *event,
3900 struct ring_buffer *rb)
3901{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003902 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003903 unsigned long flags;
3904
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003905 if (event->rb) {
3906 /*
3907 * Should be impossible, we set this when removing
3908 * event->rb_entry and wait/clear when adding event->rb_entry.
3909 */
3910 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003911
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003912 old_rb = event->rb;
3913 event->rcu_batches = get_state_synchronize_rcu();
3914 event->rcu_pending = 1;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003915
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003916 spin_lock_irqsave(&old_rb->event_lock, flags);
3917 list_del_rcu(&event->rb_entry);
3918 spin_unlock_irqrestore(&old_rb->event_lock, flags);
3919 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003920
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003921 if (event->rcu_pending && rb) {
3922 cond_synchronize_rcu(event->rcu_batches);
3923 event->rcu_pending = 0;
3924 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003925
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003926 if (rb) {
3927 spin_lock_irqsave(&rb->event_lock, flags);
3928 list_add_rcu(&event->rb_entry, &rb->event_list);
3929 spin_unlock_irqrestore(&rb->event_lock, flags);
3930 }
3931
3932 rcu_assign_pointer(event->rb, rb);
3933
3934 if (old_rb) {
3935 ring_buffer_put(old_rb);
3936 /*
3937 * Since we detached before setting the new rb, so that we
3938 * could attach the new rb, we could have missed a wakeup.
3939 * Provide it now.
3940 */
3941 wake_up_all(&event->waitq);
3942 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003943}
3944
3945static void ring_buffer_wakeup(struct perf_event *event)
3946{
3947 struct ring_buffer *rb;
3948
3949 rcu_read_lock();
3950 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003951 if (rb) {
3952 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3953 wake_up_all(&event->waitq);
3954 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003955 rcu_read_unlock();
3956}
3957
Frederic Weisbecker76369132011-05-19 19:55:04 +02003958static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003959{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003960 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003961
Frederic Weisbecker76369132011-05-19 19:55:04 +02003962 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3963 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003964}
3965
Frederic Weisbecker76369132011-05-19 19:55:04 +02003966static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003967{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003968 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003969
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003970 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003971 rb = rcu_dereference(event->rb);
3972 if (rb) {
3973 if (!atomic_inc_not_zero(&rb->refcount))
3974 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003975 }
3976 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003977
Frederic Weisbecker76369132011-05-19 19:55:04 +02003978 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003979}
3980
Frederic Weisbecker76369132011-05-19 19:55:04 +02003981static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003982{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003983 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003984 return;
3985
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003986 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003987
Frederic Weisbecker76369132011-05-19 19:55:04 +02003988 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003989}
3990
3991static void perf_mmap_open(struct vm_area_struct *vma)
3992{
3993 struct perf_event *event = vma->vm_file->private_data;
3994
3995 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003996 atomic_inc(&event->rb->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003997}
3998
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003999/*
4000 * A buffer can be mmap()ed multiple times; either directly through the same
4001 * event, or through other events by use of perf_event_set_output().
4002 *
4003 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4004 * the buffer here, where we still have a VM context. This means we need
4005 * to detach all events redirecting to us.
4006 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004007static void perf_mmap_close(struct vm_area_struct *vma)
4008{
4009 struct perf_event *event = vma->vm_file->private_data;
4010
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004011 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004012 struct user_struct *mmap_user = rb->mmap_user;
4013 int mmap_locked = rb->mmap_locked;
4014 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004015
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004016 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004017
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004018 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004019 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004020
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004021 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004022 mutex_unlock(&event->mmap_mutex);
4023
4024 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004025 if (atomic_read(&rb->mmap_count))
4026 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004027
4028 /*
4029 * No other mmap()s, detach from all other events that might redirect
4030 * into the now unreachable buffer. Somewhat complicated by the
4031 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4032 */
4033again:
4034 rcu_read_lock();
4035 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4036 if (!atomic_long_inc_not_zero(&event->refcount)) {
4037 /*
4038 * This event is en-route to free_event() which will
4039 * detach it and remove it from the list.
4040 */
4041 continue;
4042 }
4043 rcu_read_unlock();
4044
4045 mutex_lock(&event->mmap_mutex);
4046 /*
4047 * Check we didn't race with perf_event_set_output() which can
4048 * swizzle the rb from under us while we were waiting to
4049 * acquire mmap_mutex.
4050 *
4051 * If we find a different rb; ignore this event, a next
4052 * iteration will no longer find it on the list. We have to
4053 * still restart the iteration to make sure we're not now
4054 * iterating the wrong list.
4055 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004056 if (event->rb == rb)
4057 ring_buffer_attach(event, NULL);
4058
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004059 mutex_unlock(&event->mmap_mutex);
4060 put_event(event);
4061
4062 /*
4063 * Restart the iteration; either we're on the wrong list or
4064 * destroyed its integrity by doing a deletion.
4065 */
4066 goto again;
4067 }
4068 rcu_read_unlock();
4069
4070 /*
4071 * It could be there's still a few 0-ref events on the list; they'll
4072 * get cleaned up by free_event() -- they'll also still have their
4073 * ref on the rb and will free it whenever they are done with it.
4074 *
4075 * Aside from that, this buffer is 'fully' detached and unmapped,
4076 * undo the VM accounting.
4077 */
4078
4079 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4080 vma->vm_mm->pinned_vm -= mmap_locked;
4081 free_uid(mmap_user);
4082
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004083out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004084 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004085}
4086
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004087static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004088 .open = perf_mmap_open,
4089 .close = perf_mmap_close,
4090 .fault = perf_mmap_fault,
4091 .page_mkwrite = perf_mmap_fault,
4092};
4093
4094static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4095{
4096 struct perf_event *event = file->private_data;
4097 unsigned long user_locked, user_lock_limit;
4098 struct user_struct *user = current_user();
4099 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004100 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004101 unsigned long vma_size;
4102 unsigned long nr_pages;
4103 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004104 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004105
Peter Zijlstrac7920612010-05-18 10:33:24 +02004106 /*
4107 * Don't allow mmap() of inherited per-task counters. This would
4108 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004109 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004110 */
4111 if (event->cpu == -1 && event->attr.inherit)
4112 return -EINVAL;
4113
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004114 if (!(vma->vm_flags & VM_SHARED))
4115 return -EINVAL;
4116
4117 vma_size = vma->vm_end - vma->vm_start;
4118 nr_pages = (vma_size / PAGE_SIZE) - 1;
4119
4120 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004121 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004122 * can do bitmasks instead of modulo.
4123 */
4124 if (nr_pages != 0 && !is_power_of_2(nr_pages))
4125 return -EINVAL;
4126
4127 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4128 return -EINVAL;
4129
4130 if (vma->vm_pgoff != 0)
4131 return -EINVAL;
4132
4133 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004134again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004135 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004136 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004137 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004138 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004139 goto unlock;
4140 }
4141
4142 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4143 /*
4144 * Raced against perf_mmap_close() through
4145 * perf_event_set_output(). Try again, hope for better
4146 * luck.
4147 */
4148 mutex_unlock(&event->mmap_mutex);
4149 goto again;
4150 }
4151
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004152 goto unlock;
4153 }
4154
4155 user_extra = nr_pages + 1;
4156 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4157
4158 /*
4159 * Increase the limit linearly with more CPUs:
4160 */
4161 user_lock_limit *= num_online_cpus();
4162
4163 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4164
4165 extra = 0;
4166 if (user_locked > user_lock_limit)
4167 extra = user_locked - user_lock_limit;
4168
Jiri Slaby78d7d402010-03-05 13:42:54 -08004169 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004170 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004171 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004172
4173 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4174 !capable(CAP_IPC_LOCK)) {
4175 ret = -EPERM;
4176 goto unlock;
4177 }
4178
Frederic Weisbecker76369132011-05-19 19:55:04 +02004179 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004180
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004181 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004182 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004183
Vince Weaver4ec83632011-06-01 15:15:36 -04004184 rb = rb_alloc(nr_pages,
4185 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4186 event->cpu, flags);
4187
Frederic Weisbecker76369132011-05-19 19:55:04 +02004188 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004189 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004190 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004191 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004192
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004193 atomic_set(&rb->mmap_count, 1);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004194 rb->mmap_locked = extra;
4195 rb->mmap_user = get_current_user();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004196
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004197 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004198 vma->vm_mm->pinned_vm += extra;
4199
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004200 ring_buffer_attach(event, rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004201
Peter Zijlstrafa731582013-09-19 10:16:42 +02004202 perf_event_init_userpage(event);
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01004203 perf_event_update_userpage(event);
4204
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004205unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004206 if (!ret)
4207 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004208 mutex_unlock(&event->mmap_mutex);
4209
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004210 /*
4211 * Since pinned accounting is per vm we cannot allow fork() to copy our
4212 * vma.
4213 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004214 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004215 vma->vm_ops = &perf_mmap_vmops;
4216
4217 return ret;
4218}
4219
4220static int perf_fasync(int fd, struct file *filp, int on)
4221{
Al Viro496ad9a2013-01-23 17:07:38 -05004222 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004223 struct perf_event *event = filp->private_data;
4224 int retval;
4225
4226 mutex_lock(&inode->i_mutex);
4227 retval = fasync_helper(fd, filp, on, &event->fasync);
4228 mutex_unlock(&inode->i_mutex);
4229
4230 if (retval < 0)
4231 return retval;
4232
4233 return 0;
4234}
4235
4236static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004237 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004238 .release = perf_release,
4239 .read = perf_read,
4240 .poll = perf_poll,
4241 .unlocked_ioctl = perf_ioctl,
4242 .compat_ioctl = perf_ioctl,
4243 .mmap = perf_mmap,
4244 .fasync = perf_fasync,
4245};
4246
4247/*
4248 * Perf event wakeup
4249 *
4250 * If there's data, ensure we set the poll() state and publish everything
4251 * to user-space before waking everybody up.
4252 */
4253
4254void perf_event_wakeup(struct perf_event *event)
4255{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004256 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004257
4258 if (event->pending_kill) {
4259 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4260 event->pending_kill = 0;
4261 }
4262}
4263
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004264static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004265{
4266 struct perf_event *event = container_of(entry,
4267 struct perf_event, pending);
4268
4269 if (event->pending_disable) {
4270 event->pending_disable = 0;
4271 __perf_event_disable(event);
4272 }
4273
4274 if (event->pending_wakeup) {
4275 event->pending_wakeup = 0;
4276 perf_event_wakeup(event);
4277 }
4278}
4279
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004280/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004281 * We assume there is only KVM supporting the callbacks.
4282 * Later on, we might change it to a list if there is
4283 * another virtualization implementation supporting the callbacks.
4284 */
4285struct perf_guest_info_callbacks *perf_guest_cbs;
4286
4287int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4288{
4289 perf_guest_cbs = cbs;
4290 return 0;
4291}
4292EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4293
4294int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4295{
4296 perf_guest_cbs = NULL;
4297 return 0;
4298}
4299EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4300
Jiri Olsa40189942012-08-07 15:20:37 +02004301static void
4302perf_output_sample_regs(struct perf_output_handle *handle,
4303 struct pt_regs *regs, u64 mask)
4304{
4305 int bit;
4306
4307 for_each_set_bit(bit, (const unsigned long *) &mask,
4308 sizeof(mask) * BITS_PER_BYTE) {
4309 u64 val;
4310
4311 val = perf_reg_value(regs, bit);
4312 perf_output_put(handle, val);
4313 }
4314}
4315
4316static void perf_sample_regs_user(struct perf_regs_user *regs_user,
4317 struct pt_regs *regs)
4318{
4319 if (!user_mode(regs)) {
4320 if (current->mm)
4321 regs = task_pt_regs(current);
4322 else
4323 regs = NULL;
4324 }
4325
4326 if (regs) {
4327 regs_user->regs = regs;
4328 regs_user->abi = perf_reg_abi(current);
4329 }
4330}
4331
Jiri Olsac5ebced2012-08-07 15:20:40 +02004332/*
4333 * Get remaining task size from user stack pointer.
4334 *
4335 * It'd be better to take stack vma map and limit this more
4336 * precisly, but there's no way to get it safely under interrupt,
4337 * so using TASK_SIZE as limit.
4338 */
4339static u64 perf_ustack_task_size(struct pt_regs *regs)
4340{
4341 unsigned long addr = perf_user_stack_pointer(regs);
4342
4343 if (!addr || addr >= TASK_SIZE)
4344 return 0;
4345
4346 return TASK_SIZE - addr;
4347}
4348
4349static u16
4350perf_sample_ustack_size(u16 stack_size, u16 header_size,
4351 struct pt_regs *regs)
4352{
4353 u64 task_size;
4354
4355 /* No regs, no stack pointer, no dump. */
4356 if (!regs)
4357 return 0;
4358
4359 /*
4360 * Check if we fit in with the requested stack size into the:
4361 * - TASK_SIZE
4362 * If we don't, we limit the size to the TASK_SIZE.
4363 *
4364 * - remaining sample size
4365 * If we don't, we customize the stack size to
4366 * fit in to the remaining sample size.
4367 */
4368
4369 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4370 stack_size = min(stack_size, (u16) task_size);
4371
4372 /* Current header size plus static size and dynamic size. */
4373 header_size += 2 * sizeof(u64);
4374
4375 /* Do we fit in with the current stack dump size? */
4376 if ((u16) (header_size + stack_size) < header_size) {
4377 /*
4378 * If we overflow the maximum size for the sample,
4379 * we customize the stack dump size to fit in.
4380 */
4381 stack_size = USHRT_MAX - header_size - sizeof(u64);
4382 stack_size = round_up(stack_size, sizeof(u64));
4383 }
4384
4385 return stack_size;
4386}
4387
4388static void
4389perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4390 struct pt_regs *regs)
4391{
4392 /* Case of a kernel thread, nothing to dump */
4393 if (!regs) {
4394 u64 size = 0;
4395 perf_output_put(handle, size);
4396 } else {
4397 unsigned long sp;
4398 unsigned int rem;
4399 u64 dyn_size;
4400
4401 /*
4402 * We dump:
4403 * static size
4404 * - the size requested by user or the best one we can fit
4405 * in to the sample max size
4406 * data
4407 * - user stack dump data
4408 * dynamic size
4409 * - the actual dumped size
4410 */
4411
4412 /* Static size. */
4413 perf_output_put(handle, dump_size);
4414
4415 /* Data. */
4416 sp = perf_user_stack_pointer(regs);
4417 rem = __output_copy_user(handle, (void *) sp, dump_size);
4418 dyn_size = dump_size - rem;
4419
4420 perf_output_skip(handle, rem);
4421
4422 /* Dynamic size. */
4423 perf_output_put(handle, dyn_size);
4424 }
4425}
4426
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004427static void __perf_event_header__init_id(struct perf_event_header *header,
4428 struct perf_sample_data *data,
4429 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004430{
4431 u64 sample_type = event->attr.sample_type;
4432
4433 data->type = sample_type;
4434 header->size += event->id_header_size;
4435
4436 if (sample_type & PERF_SAMPLE_TID) {
4437 /* namespace issues */
4438 data->tid_entry.pid = perf_event_pid(event, current);
4439 data->tid_entry.tid = perf_event_tid(event, current);
4440 }
4441
4442 if (sample_type & PERF_SAMPLE_TIME)
4443 data->time = perf_clock();
4444
Adrian Hunterff3d5272013-08-27 11:23:07 +03004445 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004446 data->id = primary_event_id(event);
4447
4448 if (sample_type & PERF_SAMPLE_STREAM_ID)
4449 data->stream_id = event->id;
4450
4451 if (sample_type & PERF_SAMPLE_CPU) {
4452 data->cpu_entry.cpu = raw_smp_processor_id();
4453 data->cpu_entry.reserved = 0;
4454 }
4455}
4456
Frederic Weisbecker76369132011-05-19 19:55:04 +02004457void perf_event_header__init_id(struct perf_event_header *header,
4458 struct perf_sample_data *data,
4459 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004460{
4461 if (event->attr.sample_id_all)
4462 __perf_event_header__init_id(header, data, event);
4463}
4464
4465static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4466 struct perf_sample_data *data)
4467{
4468 u64 sample_type = data->type;
4469
4470 if (sample_type & PERF_SAMPLE_TID)
4471 perf_output_put(handle, data->tid_entry);
4472
4473 if (sample_type & PERF_SAMPLE_TIME)
4474 perf_output_put(handle, data->time);
4475
4476 if (sample_type & PERF_SAMPLE_ID)
4477 perf_output_put(handle, data->id);
4478
4479 if (sample_type & PERF_SAMPLE_STREAM_ID)
4480 perf_output_put(handle, data->stream_id);
4481
4482 if (sample_type & PERF_SAMPLE_CPU)
4483 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03004484
4485 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4486 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004487}
4488
Frederic Weisbecker76369132011-05-19 19:55:04 +02004489void perf_event__output_id_sample(struct perf_event *event,
4490 struct perf_output_handle *handle,
4491 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004492{
4493 if (event->attr.sample_id_all)
4494 __perf_event__output_id_sample(handle, sample);
4495}
4496
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004497static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004498 struct perf_event *event,
4499 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004500{
4501 u64 read_format = event->attr.read_format;
4502 u64 values[4];
4503 int n = 0;
4504
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004505 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004506 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004507 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004508 atomic64_read(&event->child_total_time_enabled);
4509 }
4510 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004511 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004512 atomic64_read(&event->child_total_time_running);
4513 }
4514 if (read_format & PERF_FORMAT_ID)
4515 values[n++] = primary_event_id(event);
4516
Frederic Weisbecker76369132011-05-19 19:55:04 +02004517 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004518}
4519
4520/*
4521 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4522 */
4523static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004524 struct perf_event *event,
4525 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004526{
4527 struct perf_event *leader = event->group_leader, *sub;
4528 u64 read_format = event->attr.read_format;
4529 u64 values[5];
4530 int n = 0;
4531
4532 values[n++] = 1 + leader->nr_siblings;
4533
4534 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004535 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004536
4537 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004538 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004539
4540 if (leader != event)
4541 leader->pmu->read(leader);
4542
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004543 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004544 if (read_format & PERF_FORMAT_ID)
4545 values[n++] = primary_event_id(leader);
4546
Frederic Weisbecker76369132011-05-19 19:55:04 +02004547 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004548
4549 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4550 n = 0;
4551
Jiri Olsa6f5ab002012-10-15 20:13:45 +02004552 if ((sub != event) &&
4553 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004554 sub->pmu->read(sub);
4555
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004556 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004557 if (read_format & PERF_FORMAT_ID)
4558 values[n++] = primary_event_id(sub);
4559
Frederic Weisbecker76369132011-05-19 19:55:04 +02004560 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004561 }
4562}
4563
Stephane Eranianeed01522010-10-26 16:08:01 +02004564#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4565 PERF_FORMAT_TOTAL_TIME_RUNNING)
4566
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004567static void perf_output_read(struct perf_output_handle *handle,
4568 struct perf_event *event)
4569{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004570 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004571 u64 read_format = event->attr.read_format;
4572
4573 /*
4574 * compute total_time_enabled, total_time_running
4575 * based on snapshot values taken when the event
4576 * was last scheduled in.
4577 *
4578 * we cannot simply called update_context_time()
4579 * because of locking issue as we are called in
4580 * NMI context
4581 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004582 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004583 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004584
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004585 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004586 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004587 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004588 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004589}
4590
4591void perf_output_sample(struct perf_output_handle *handle,
4592 struct perf_event_header *header,
4593 struct perf_sample_data *data,
4594 struct perf_event *event)
4595{
4596 u64 sample_type = data->type;
4597
4598 perf_output_put(handle, *header);
4599
Adrian Hunterff3d5272013-08-27 11:23:07 +03004600 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4601 perf_output_put(handle, data->id);
4602
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004603 if (sample_type & PERF_SAMPLE_IP)
4604 perf_output_put(handle, data->ip);
4605
4606 if (sample_type & PERF_SAMPLE_TID)
4607 perf_output_put(handle, data->tid_entry);
4608
4609 if (sample_type & PERF_SAMPLE_TIME)
4610 perf_output_put(handle, data->time);
4611
4612 if (sample_type & PERF_SAMPLE_ADDR)
4613 perf_output_put(handle, data->addr);
4614
4615 if (sample_type & PERF_SAMPLE_ID)
4616 perf_output_put(handle, data->id);
4617
4618 if (sample_type & PERF_SAMPLE_STREAM_ID)
4619 perf_output_put(handle, data->stream_id);
4620
4621 if (sample_type & PERF_SAMPLE_CPU)
4622 perf_output_put(handle, data->cpu_entry);
4623
4624 if (sample_type & PERF_SAMPLE_PERIOD)
4625 perf_output_put(handle, data->period);
4626
4627 if (sample_type & PERF_SAMPLE_READ)
4628 perf_output_read(handle, event);
4629
4630 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4631 if (data->callchain) {
4632 int size = 1;
4633
4634 if (data->callchain)
4635 size += data->callchain->nr;
4636
4637 size *= sizeof(u64);
4638
Frederic Weisbecker76369132011-05-19 19:55:04 +02004639 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004640 } else {
4641 u64 nr = 0;
4642 perf_output_put(handle, nr);
4643 }
4644 }
4645
4646 if (sample_type & PERF_SAMPLE_RAW) {
4647 if (data->raw) {
4648 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004649 __output_copy(handle, data->raw->data,
4650 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004651 } else {
4652 struct {
4653 u32 size;
4654 u32 data;
4655 } raw = {
4656 .size = sizeof(u32),
4657 .data = 0,
4658 };
4659 perf_output_put(handle, raw);
4660 }
4661 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004662
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004663 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4664 if (data->br_stack) {
4665 size_t size;
4666
4667 size = data->br_stack->nr
4668 * sizeof(struct perf_branch_entry);
4669
4670 perf_output_put(handle, data->br_stack->nr);
4671 perf_output_copy(handle, data->br_stack->entries, size);
4672 } else {
4673 /*
4674 * we always store at least the value of nr
4675 */
4676 u64 nr = 0;
4677 perf_output_put(handle, nr);
4678 }
4679 }
Jiri Olsa40189942012-08-07 15:20:37 +02004680
4681 if (sample_type & PERF_SAMPLE_REGS_USER) {
4682 u64 abi = data->regs_user.abi;
4683
4684 /*
4685 * If there are no regs to dump, notice it through
4686 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4687 */
4688 perf_output_put(handle, abi);
4689
4690 if (abi) {
4691 u64 mask = event->attr.sample_regs_user;
4692 perf_output_sample_regs(handle,
4693 data->regs_user.regs,
4694 mask);
4695 }
4696 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004697
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004698 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02004699 perf_output_sample_ustack(handle,
4700 data->stack_user_size,
4701 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004702 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01004703
4704 if (sample_type & PERF_SAMPLE_WEIGHT)
4705 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01004706
4707 if (sample_type & PERF_SAMPLE_DATA_SRC)
4708 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004709
Andi Kleenfdfbbd02013-09-20 07:40:39 -07004710 if (sample_type & PERF_SAMPLE_TRANSACTION)
4711 perf_output_put(handle, data->txn);
4712
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004713 if (!event->attr.watermark) {
4714 int wakeup_events = event->attr.wakeup_events;
4715
4716 if (wakeup_events) {
4717 struct ring_buffer *rb = handle->rb;
4718 int events = local_inc_return(&rb->events);
4719
4720 if (events >= wakeup_events) {
4721 local_sub(wakeup_events, &rb->events);
4722 local_inc(&rb->wakeup);
4723 }
4724 }
4725 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004726}
4727
4728void perf_prepare_sample(struct perf_event_header *header,
4729 struct perf_sample_data *data,
4730 struct perf_event *event,
4731 struct pt_regs *regs)
4732{
4733 u64 sample_type = event->attr.sample_type;
4734
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004735 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004736 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004737
4738 header->misc = 0;
4739 header->misc |= perf_misc_flags(regs);
4740
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004741 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004742
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004743 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004744 data->ip = perf_instruction_pointer(regs);
4745
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004746 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4747 int size = 1;
4748
Andrew Vagine6dab5f2012-07-11 18:14:58 +04004749 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004750
4751 if (data->callchain)
4752 size += data->callchain->nr;
4753
4754 header->size += size * sizeof(u64);
4755 }
4756
4757 if (sample_type & PERF_SAMPLE_RAW) {
4758 int size = sizeof(u32);
4759
4760 if (data->raw)
4761 size += data->raw->size;
4762 else
4763 size += sizeof(u32);
4764
4765 WARN_ON_ONCE(size & (sizeof(u64)-1));
4766 header->size += size;
4767 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004768
4769 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4770 int size = sizeof(u64); /* nr */
4771 if (data->br_stack) {
4772 size += data->br_stack->nr
4773 * sizeof(struct perf_branch_entry);
4774 }
4775 header->size += size;
4776 }
Jiri Olsa40189942012-08-07 15:20:37 +02004777
4778 if (sample_type & PERF_SAMPLE_REGS_USER) {
4779 /* regs dump ABI info */
4780 int size = sizeof(u64);
4781
4782 perf_sample_regs_user(&data->regs_user, regs);
4783
4784 if (data->regs_user.regs) {
4785 u64 mask = event->attr.sample_regs_user;
4786 size += hweight64(mask) * sizeof(u64);
4787 }
4788
4789 header->size += size;
4790 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004791
4792 if (sample_type & PERF_SAMPLE_STACK_USER) {
4793 /*
4794 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4795 * processed as the last one or have additional check added
4796 * in case new sample type is added, because we could eat
4797 * up the rest of the sample size.
4798 */
4799 struct perf_regs_user *uregs = &data->regs_user;
4800 u16 stack_size = event->attr.sample_stack_user;
4801 u16 size = sizeof(u64);
4802
4803 if (!uregs->abi)
4804 perf_sample_regs_user(uregs, regs);
4805
4806 stack_size = perf_sample_ustack_size(stack_size, header->size,
4807 uregs->regs);
4808
4809 /*
4810 * If there is something to dump, add space for the dump
4811 * itself and for the field that tells the dynamic size,
4812 * which is how many have been actually dumped.
4813 */
4814 if (stack_size)
4815 size += sizeof(u64) + stack_size;
4816
4817 data->stack_user_size = stack_size;
4818 header->size += size;
4819 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004820}
4821
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004822static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004823 struct perf_sample_data *data,
4824 struct pt_regs *regs)
4825{
4826 struct perf_output_handle handle;
4827 struct perf_event_header header;
4828
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004829 /* protect the callchain buffers */
4830 rcu_read_lock();
4831
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004832 perf_prepare_sample(&header, data, event, regs);
4833
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004834 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004835 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004836
4837 perf_output_sample(&handle, &header, data, event);
4838
4839 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004840
4841exit:
4842 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004843}
4844
4845/*
4846 * read event_id
4847 */
4848
4849struct perf_read_event {
4850 struct perf_event_header header;
4851
4852 u32 pid;
4853 u32 tid;
4854};
4855
4856static void
4857perf_event_read_event(struct perf_event *event,
4858 struct task_struct *task)
4859{
4860 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004861 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004862 struct perf_read_event read_event = {
4863 .header = {
4864 .type = PERF_RECORD_READ,
4865 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004866 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004867 },
4868 .pid = perf_event_pid(event, task),
4869 .tid = perf_event_tid(event, task),
4870 };
4871 int ret;
4872
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004873 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004874 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004875 if (ret)
4876 return;
4877
4878 perf_output_put(&handle, read_event);
4879 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004880 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004881
4882 perf_output_end(&handle);
4883}
4884
Jiri Olsa52d857a2013-05-06 18:27:18 +02004885typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
4886
4887static void
4888perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004889 perf_event_aux_output_cb output,
4890 void *data)
4891{
4892 struct perf_event *event;
4893
4894 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4895 if (event->state < PERF_EVENT_STATE_INACTIVE)
4896 continue;
4897 if (!event_filter_match(event))
4898 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02004899 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004900 }
4901}
4902
4903static void
Jiri Olsa67516842013-07-09 18:56:31 +02004904perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004905 struct perf_event_context *task_ctx)
4906{
4907 struct perf_cpu_context *cpuctx;
4908 struct perf_event_context *ctx;
4909 struct pmu *pmu;
4910 int ctxn;
4911
4912 rcu_read_lock();
4913 list_for_each_entry_rcu(pmu, &pmus, entry) {
4914 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4915 if (cpuctx->unique_pmu != pmu)
4916 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02004917 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004918 if (task_ctx)
4919 goto next;
4920 ctxn = pmu->task_ctx_nr;
4921 if (ctxn < 0)
4922 goto next;
4923 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4924 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02004925 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004926next:
4927 put_cpu_ptr(pmu->pmu_cpu_context);
4928 }
4929
4930 if (task_ctx) {
4931 preempt_disable();
Jiri Olsa67516842013-07-09 18:56:31 +02004932 perf_event_aux_ctx(task_ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004933 preempt_enable();
4934 }
4935 rcu_read_unlock();
4936}
4937
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004938/*
4939 * task tracking -- fork/exit
4940 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02004941 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004942 */
4943
4944struct perf_task_event {
4945 struct task_struct *task;
4946 struct perf_event_context *task_ctx;
4947
4948 struct {
4949 struct perf_event_header header;
4950
4951 u32 pid;
4952 u32 ppid;
4953 u32 tid;
4954 u32 ptid;
4955 u64 time;
4956 } event_id;
4957};
4958
Jiri Olsa67516842013-07-09 18:56:31 +02004959static int perf_event_task_match(struct perf_event *event)
4960{
Stephane Eranian13d7a242013-08-21 12:10:24 +02004961 return event->attr.comm || event->attr.mmap ||
4962 event->attr.mmap2 || event->attr.mmap_data ||
4963 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02004964}
4965
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004966static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004967 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004968{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004969 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004970 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004971 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004972 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004973 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004974
Jiri Olsa67516842013-07-09 18:56:31 +02004975 if (!perf_event_task_match(event))
4976 return;
4977
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004978 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004979
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004980 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004981 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02004982 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004983 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004984
4985 task_event->event_id.pid = perf_event_pid(event, task);
4986 task_event->event_id.ppid = perf_event_pid(event, current);
4987
4988 task_event->event_id.tid = perf_event_tid(event, task);
4989 task_event->event_id.ptid = perf_event_tid(event, current);
4990
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004991 perf_output_put(&handle, task_event->event_id);
4992
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004993 perf_event__output_id_sample(event, &handle, &sample);
4994
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004995 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004996out:
4997 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004998}
4999
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005000static void perf_event_task(struct task_struct *task,
5001 struct perf_event_context *task_ctx,
5002 int new)
5003{
5004 struct perf_task_event task_event;
5005
5006 if (!atomic_read(&nr_comm_events) &&
5007 !atomic_read(&nr_mmap_events) &&
5008 !atomic_read(&nr_task_events))
5009 return;
5010
5011 task_event = (struct perf_task_event){
5012 .task = task,
5013 .task_ctx = task_ctx,
5014 .event_id = {
5015 .header = {
5016 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5017 .misc = 0,
5018 .size = sizeof(task_event.event_id),
5019 },
5020 /* .pid */
5021 /* .ppid */
5022 /* .tid */
5023 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01005024 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005025 },
5026 };
5027
Jiri Olsa67516842013-07-09 18:56:31 +02005028 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005029 &task_event,
5030 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005031}
5032
5033void perf_event_fork(struct task_struct *task)
5034{
5035 perf_event_task(task, NULL, 1);
5036}
5037
5038/*
5039 * comm tracking
5040 */
5041
5042struct perf_comm_event {
5043 struct task_struct *task;
5044 char *comm;
5045 int comm_size;
5046
5047 struct {
5048 struct perf_event_header header;
5049
5050 u32 pid;
5051 u32 tid;
5052 } event_id;
5053};
5054
Jiri Olsa67516842013-07-09 18:56:31 +02005055static int perf_event_comm_match(struct perf_event *event)
5056{
5057 return event->attr.comm;
5058}
5059
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005060static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005061 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005062{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005063 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005064 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005065 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005066 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005067 int ret;
5068
Jiri Olsa67516842013-07-09 18:56:31 +02005069 if (!perf_event_comm_match(event))
5070 return;
5071
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005072 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5073 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005074 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005075
5076 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005077 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005078
5079 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5080 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5081
5082 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005083 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005084 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005085
5086 perf_event__output_id_sample(event, &handle, &sample);
5087
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005088 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005089out:
5090 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005091}
5092
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005093static void perf_event_comm_event(struct perf_comm_event *comm_event)
5094{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005095 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005096 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005097
5098 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005099 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005100 size = ALIGN(strlen(comm)+1, sizeof(u64));
5101
5102 comm_event->comm = comm;
5103 comm_event->comm_size = size;
5104
5105 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005106
Jiri Olsa67516842013-07-09 18:56:31 +02005107 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005108 comm_event,
5109 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005110}
5111
Adrian Hunter82b89772014-05-28 11:45:04 +03005112void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005113{
5114 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005115
5116 if (!atomic_read(&nr_comm_events))
5117 return;
5118
5119 comm_event = (struct perf_comm_event){
5120 .task = task,
5121 /* .comm */
5122 /* .comm_size */
5123 .event_id = {
5124 .header = {
5125 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005126 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005127 /* .size */
5128 },
5129 /* .pid */
5130 /* .tid */
5131 },
5132 };
5133
5134 perf_event_comm_event(&comm_event);
5135}
5136
5137/*
5138 * mmap tracking
5139 */
5140
5141struct perf_mmap_event {
5142 struct vm_area_struct *vma;
5143
5144 const char *file_name;
5145 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005146 int maj, min;
5147 u64 ino;
5148 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005149 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005150
5151 struct {
5152 struct perf_event_header header;
5153
5154 u32 pid;
5155 u32 tid;
5156 u64 start;
5157 u64 len;
5158 u64 pgoff;
5159 } event_id;
5160};
5161
Jiri Olsa67516842013-07-09 18:56:31 +02005162static int perf_event_mmap_match(struct perf_event *event,
5163 void *data)
5164{
5165 struct perf_mmap_event *mmap_event = data;
5166 struct vm_area_struct *vma = mmap_event->vma;
5167 int executable = vma->vm_flags & VM_EXEC;
5168
5169 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005170 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005171}
5172
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005173static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005174 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005175{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005176 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005177 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005178 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005179 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005180 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005181
Jiri Olsa67516842013-07-09 18:56:31 +02005182 if (!perf_event_mmap_match(event, data))
5183 return;
5184
Stephane Eranian13d7a242013-08-21 12:10:24 +02005185 if (event->attr.mmap2) {
5186 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5187 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5188 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5189 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005190 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005191 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
5192 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005193 }
5194
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005195 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5196 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005197 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005198 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005199 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005200
5201 mmap_event->event_id.pid = perf_event_pid(event, current);
5202 mmap_event->event_id.tid = perf_event_tid(event, current);
5203
5204 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005205
5206 if (event->attr.mmap2) {
5207 perf_output_put(&handle, mmap_event->maj);
5208 perf_output_put(&handle, mmap_event->min);
5209 perf_output_put(&handle, mmap_event->ino);
5210 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005211 perf_output_put(&handle, mmap_event->prot);
5212 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005213 }
5214
Frederic Weisbecker76369132011-05-19 19:55:04 +02005215 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005216 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005217
5218 perf_event__output_id_sample(event, &handle, &sample);
5219
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005220 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005221out:
5222 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005223}
5224
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005225static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5226{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005227 struct vm_area_struct *vma = mmap_event->vma;
5228 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005229 int maj = 0, min = 0;
5230 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005231 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005232 unsigned int size;
5233 char tmp[16];
5234 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005235 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005236
5237 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005238 struct inode *inode;
5239 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005240
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005241 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005242 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005243 name = "//enomem";
5244 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005245 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005246 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005247 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005248 * need to add enough zero bytes after the string to handle
5249 * the 64bit alignment we do later.
5250 */
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005251 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005252 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005253 name = "//toolong";
5254 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005255 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005256 inode = file_inode(vma->vm_file);
5257 dev = inode->i_sb->s_dev;
5258 ino = inode->i_ino;
5259 gen = inode->i_generation;
5260 maj = MAJOR(dev);
5261 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005262
5263 if (vma->vm_flags & VM_READ)
5264 prot |= PROT_READ;
5265 if (vma->vm_flags & VM_WRITE)
5266 prot |= PROT_WRITE;
5267 if (vma->vm_flags & VM_EXEC)
5268 prot |= PROT_EXEC;
5269
5270 if (vma->vm_flags & VM_MAYSHARE)
5271 flags = MAP_SHARED;
5272 else
5273 flags = MAP_PRIVATE;
5274
5275 if (vma->vm_flags & VM_DENYWRITE)
5276 flags |= MAP_DENYWRITE;
5277 if (vma->vm_flags & VM_MAYEXEC)
5278 flags |= MAP_EXECUTABLE;
5279 if (vma->vm_flags & VM_LOCKED)
5280 flags |= MAP_LOCKED;
5281 if (vma->vm_flags & VM_HUGETLB)
5282 flags |= MAP_HUGETLB;
5283
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005284 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005285 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02005286 if (vma->vm_ops && vma->vm_ops->name) {
5287 name = (char *) vma->vm_ops->name(vma);
5288 if (name)
5289 goto cpy_name;
5290 }
5291
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005292 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005293 if (name)
5294 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005295
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005296 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005297 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005298 name = "[heap]";
5299 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005300 }
5301 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005302 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005303 name = "[stack]";
5304 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005305 }
5306
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005307 name = "//anon";
5308 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005309 }
5310
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005311cpy_name:
5312 strlcpy(tmp, name, sizeof(tmp));
5313 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005314got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005315 /*
5316 * Since our buffer works in 8 byte units we need to align our string
5317 * size to a multiple of 8. However, we must guarantee the tail end is
5318 * zero'd out to avoid leaking random bits to userspace.
5319 */
5320 size = strlen(name)+1;
5321 while (!IS_ALIGNED(size, sizeof(u64)))
5322 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005323
5324 mmap_event->file_name = name;
5325 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005326 mmap_event->maj = maj;
5327 mmap_event->min = min;
5328 mmap_event->ino = ino;
5329 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005330 mmap_event->prot = prot;
5331 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005332
Stephane Eranian2fe85422013-01-24 16:10:39 +01005333 if (!(vma->vm_flags & VM_EXEC))
5334 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5335
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005336 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5337
Jiri Olsa67516842013-07-09 18:56:31 +02005338 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005339 mmap_event,
5340 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005341
5342 kfree(buf);
5343}
5344
Eric B Munson3af9e852010-05-18 15:30:49 +01005345void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005346{
5347 struct perf_mmap_event mmap_event;
5348
5349 if (!atomic_read(&nr_mmap_events))
5350 return;
5351
5352 mmap_event = (struct perf_mmap_event){
5353 .vma = vma,
5354 /* .file_name */
5355 /* .file_size */
5356 .event_id = {
5357 .header = {
5358 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005359 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005360 /* .size */
5361 },
5362 /* .pid */
5363 /* .tid */
5364 .start = vma->vm_start,
5365 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01005366 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005367 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02005368 /* .maj (attr_mmap2 only) */
5369 /* .min (attr_mmap2 only) */
5370 /* .ino (attr_mmap2 only) */
5371 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005372 /* .prot (attr_mmap2 only) */
5373 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005374 };
5375
5376 perf_event_mmap_event(&mmap_event);
5377}
5378
5379/*
5380 * IRQ throttle logging
5381 */
5382
5383static void perf_log_throttle(struct perf_event *event, int enable)
5384{
5385 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005386 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005387 int ret;
5388
5389 struct {
5390 struct perf_event_header header;
5391 u64 time;
5392 u64 id;
5393 u64 stream_id;
5394 } throttle_event = {
5395 .header = {
5396 .type = PERF_RECORD_THROTTLE,
5397 .misc = 0,
5398 .size = sizeof(throttle_event),
5399 },
5400 .time = perf_clock(),
5401 .id = primary_event_id(event),
5402 .stream_id = event->id,
5403 };
5404
5405 if (enable)
5406 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5407
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005408 perf_event_header__init_id(&throttle_event.header, &sample, event);
5409
5410 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005411 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005412 if (ret)
5413 return;
5414
5415 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005416 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005417 perf_output_end(&handle);
5418}
5419
5420/*
5421 * Generic event overflow handling, sampling.
5422 */
5423
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005424static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005425 int throttle, struct perf_sample_data *data,
5426 struct pt_regs *regs)
5427{
5428 int events = atomic_read(&event->event_limit);
5429 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005430 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005431 int ret = 0;
5432
Peter Zijlstra96398822010-11-24 18:55:29 +01005433 /*
5434 * Non-sampling counters might still use the PMI to fold short
5435 * hardware counters, ignore those.
5436 */
5437 if (unlikely(!is_sampling_event(event)))
5438 return 0;
5439
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005440 seq = __this_cpu_read(perf_throttled_seq);
5441 if (seq != hwc->interrupts_seq) {
5442 hwc->interrupts_seq = seq;
5443 hwc->interrupts = 1;
5444 } else {
5445 hwc->interrupts++;
5446 if (unlikely(throttle
5447 && hwc->interrupts >= max_samples_per_tick)) {
5448 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01005449 hwc->interrupts = MAX_INTERRUPTS;
5450 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02005451 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005452 ret = 1;
5453 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005454 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005455
5456 if (event->attr.freq) {
5457 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01005458 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005459
Peter Zijlstraabd50712010-01-26 18:50:16 +01005460 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005461
Peter Zijlstraabd50712010-01-26 18:50:16 +01005462 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01005463 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005464 }
5465
5466 /*
5467 * XXX event_limit might not quite work as expected on inherited
5468 * events
5469 */
5470
5471 event->pending_kill = POLL_IN;
5472 if (events && atomic_dec_and_test(&event->event_limit)) {
5473 ret = 1;
5474 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005475 event->pending_disable = 1;
5476 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005477 }
5478
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005479 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005480 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005481 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005482 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005483
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005484 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005485 event->pending_wakeup = 1;
5486 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005487 }
5488
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005489 return ret;
5490}
5491
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005492int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005493 struct perf_sample_data *data,
5494 struct pt_regs *regs)
5495{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005496 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005497}
5498
5499/*
5500 * Generic software event infrastructure
5501 */
5502
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005503struct swevent_htable {
5504 struct swevent_hlist *swevent_hlist;
5505 struct mutex hlist_mutex;
5506 int hlist_refcount;
5507
5508 /* Recursion avoidance in each contexts */
5509 int recursion[PERF_NR_CONTEXTS];
Jiri Olsa39af6b12014-04-07 11:04:08 +02005510
5511 /* Keeps track of cpu being initialized/exited */
5512 bool online;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005513};
5514
5515static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5516
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005517/*
5518 * We directly increment event->count and keep a second value in
5519 * event->hw.period_left to count intervals. This period event
5520 * is kept in the range [-sample_period, 0] so that we can use the
5521 * sign as trigger.
5522 */
5523
Jiri Olsaab573842013-05-01 17:25:44 +02005524u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005525{
5526 struct hw_perf_event *hwc = &event->hw;
5527 u64 period = hwc->last_period;
5528 u64 nr, offset;
5529 s64 old, val;
5530
5531 hwc->last_period = hwc->sample_period;
5532
5533again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005534 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005535 if (val < 0)
5536 return 0;
5537
5538 nr = div64_u64(period + val, period);
5539 offset = nr * period;
5540 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005541 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005542 goto again;
5543
5544 return nr;
5545}
5546
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005547static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005548 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005549 struct pt_regs *regs)
5550{
5551 struct hw_perf_event *hwc = &event->hw;
5552 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005553
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005554 if (!overflow)
5555 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005556
5557 if (hwc->interrupts == MAX_INTERRUPTS)
5558 return;
5559
5560 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005561 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005562 data, regs)) {
5563 /*
5564 * We inhibit the overflow from happening when
5565 * hwc->interrupts == MAX_INTERRUPTS.
5566 */
5567 break;
5568 }
5569 throttle = 1;
5570 }
5571}
5572
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005573static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005574 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005575 struct pt_regs *regs)
5576{
5577 struct hw_perf_event *hwc = &event->hw;
5578
Peter Zijlstrae7850592010-05-21 14:43:08 +02005579 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005580
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005581 if (!regs)
5582 return;
5583
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005584 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005585 return;
5586
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005587 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5588 data->period = nr;
5589 return perf_swevent_overflow(event, 1, data, regs);
5590 } else
5591 data->period = event->hw.last_period;
5592
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005593 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005594 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005595
Peter Zijlstrae7850592010-05-21 14:43:08 +02005596 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005597 return;
5598
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005599 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005600}
5601
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005602static int perf_exclude_event(struct perf_event *event,
5603 struct pt_regs *regs)
5604{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005605 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005606 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005607
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005608 if (regs) {
5609 if (event->attr.exclude_user && user_mode(regs))
5610 return 1;
5611
5612 if (event->attr.exclude_kernel && !user_mode(regs))
5613 return 1;
5614 }
5615
5616 return 0;
5617}
5618
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005619static int perf_swevent_match(struct perf_event *event,
5620 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005621 u32 event_id,
5622 struct perf_sample_data *data,
5623 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005624{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005625 if (event->attr.type != type)
5626 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005627
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005628 if (event->attr.config != event_id)
5629 return 0;
5630
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005631 if (perf_exclude_event(event, regs))
5632 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005633
5634 return 1;
5635}
5636
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005637static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005638{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005639 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005640
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005641 return hash_64(val, SWEVENT_HLIST_BITS);
5642}
5643
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005644static inline struct hlist_head *
5645__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005646{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005647 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005648
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005649 return &hlist->heads[hash];
5650}
5651
5652/* For the read side: events when they trigger */
5653static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005654find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005655{
5656 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005657
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005658 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005659 if (!hlist)
5660 return NULL;
5661
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005662 return __find_swevent_head(hlist, type, event_id);
5663}
5664
5665/* For the event head insertion and removal in the hlist */
5666static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005667find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005668{
5669 struct swevent_hlist *hlist;
5670 u32 event_id = event->attr.config;
5671 u64 type = event->attr.type;
5672
5673 /*
5674 * Event scheduling is always serialized against hlist allocation
5675 * and release. Which makes the protected version suitable here.
5676 * The context lock guarantees that.
5677 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005678 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005679 lockdep_is_held(&event->ctx->lock));
5680 if (!hlist)
5681 return NULL;
5682
5683 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005684}
5685
5686static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005687 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005688 struct perf_sample_data *data,
5689 struct pt_regs *regs)
5690{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005691 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005692 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005693 struct hlist_head *head;
5694
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005695 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005696 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005697 if (!head)
5698 goto end;
5699
Sasha Levinb67bfe02013-02-27 17:06:00 -08005700 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005701 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005702 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005703 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005704end:
5705 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005706}
5707
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005708int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005709{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005710 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005711
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005712 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005713}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005714EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005715
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005716inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005717{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005718 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005719
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005720 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005721}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005722
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005723void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005724{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005725 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005726 int rctx;
5727
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005728 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005729 rctx = perf_swevent_get_recursion_context();
5730 if (rctx < 0)
5731 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005732
Robert Richterfd0d0002012-04-02 20:19:08 +02005733 perf_sample_data_init(&data, addr, 0);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005734
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005735 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005736
5737 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005738 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005739}
5740
5741static void perf_swevent_read(struct perf_event *event)
5742{
5743}
5744
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005745static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005746{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005747 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005748 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005749 struct hlist_head *head;
5750
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005751 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005752 hwc->last_period = hwc->sample_period;
5753 perf_swevent_set_period(event);
5754 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005755
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005756 hwc->state = !(flags & PERF_EF_START);
5757
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005758 head = find_swevent_head(swhash, event);
Jiri Olsa39af6b12014-04-07 11:04:08 +02005759 if (!head) {
5760 /*
5761 * We can race with cpu hotplug code. Do not
5762 * WARN if the cpu just got unplugged.
5763 */
5764 WARN_ON_ONCE(swhash->online);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005765 return -EINVAL;
Jiri Olsa39af6b12014-04-07 11:04:08 +02005766 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005767
5768 hlist_add_head_rcu(&event->hlist_entry, head);
5769
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005770 return 0;
5771}
5772
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005773static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005774{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005775 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005776}
5777
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005778static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005779{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005780 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005781}
5782
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005783static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005784{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005785 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005786}
5787
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005788/* Deref the hlist from the update side */
5789static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005790swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005791{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005792 return rcu_dereference_protected(swhash->swevent_hlist,
5793 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005794}
5795
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005796static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005797{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005798 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005799
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005800 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005801 return;
5802
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005803 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08005804 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005805}
5806
5807static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5808{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005809 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005810
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005811 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005812
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005813 if (!--swhash->hlist_refcount)
5814 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005815
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005816 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005817}
5818
5819static void swevent_hlist_put(struct perf_event *event)
5820{
5821 int cpu;
5822
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005823 for_each_possible_cpu(cpu)
5824 swevent_hlist_put_cpu(event, cpu);
5825}
5826
5827static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5828{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005829 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005830 int err = 0;
5831
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005832 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005833
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005834 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005835 struct swevent_hlist *hlist;
5836
5837 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5838 if (!hlist) {
5839 err = -ENOMEM;
5840 goto exit;
5841 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005842 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005843 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005844 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005845exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005846 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005847
5848 return err;
5849}
5850
5851static int swevent_hlist_get(struct perf_event *event)
5852{
5853 int err;
5854 int cpu, failed_cpu;
5855
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005856 get_online_cpus();
5857 for_each_possible_cpu(cpu) {
5858 err = swevent_hlist_get_cpu(event, cpu);
5859 if (err) {
5860 failed_cpu = cpu;
5861 goto fail;
5862 }
5863 }
5864 put_online_cpus();
5865
5866 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005867fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005868 for_each_possible_cpu(cpu) {
5869 if (cpu == failed_cpu)
5870 break;
5871 swevent_hlist_put_cpu(event, cpu);
5872 }
5873
5874 put_online_cpus();
5875 return err;
5876}
5877
Ingo Molnarc5905af2012-02-24 08:31:31 +01005878struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005879
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005880static void sw_perf_event_destroy(struct perf_event *event)
5881{
5882 u64 event_id = event->attr.config;
5883
5884 WARN_ON(event->parent);
5885
Ingo Molnarc5905af2012-02-24 08:31:31 +01005886 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005887 swevent_hlist_put(event);
5888}
5889
5890static int perf_swevent_init(struct perf_event *event)
5891{
Tommi Rantala8176cce2013-04-13 22:49:14 +03005892 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005893
5894 if (event->attr.type != PERF_TYPE_SOFTWARE)
5895 return -ENOENT;
5896
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005897 /*
5898 * no branch sampling for software events
5899 */
5900 if (has_branch_stack(event))
5901 return -EOPNOTSUPP;
5902
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005903 switch (event_id) {
5904 case PERF_COUNT_SW_CPU_CLOCK:
5905 case PERF_COUNT_SW_TASK_CLOCK:
5906 return -ENOENT;
5907
5908 default:
5909 break;
5910 }
5911
Dan Carpenterce677832010-10-24 21:50:42 +02005912 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005913 return -ENOENT;
5914
5915 if (!event->parent) {
5916 int err;
5917
5918 err = swevent_hlist_get(event);
5919 if (err)
5920 return err;
5921
Ingo Molnarc5905af2012-02-24 08:31:31 +01005922 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005923 event->destroy = sw_perf_event_destroy;
5924 }
5925
5926 return 0;
5927}
5928
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005929static int perf_swevent_event_idx(struct perf_event *event)
5930{
5931 return 0;
5932}
5933
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005934static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005935 .task_ctx_nr = perf_sw_context,
5936
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005937 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005938 .add = perf_swevent_add,
5939 .del = perf_swevent_del,
5940 .start = perf_swevent_start,
5941 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005942 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005943
5944 .event_idx = perf_swevent_event_idx,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005945};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005946
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005947#ifdef CONFIG_EVENT_TRACING
5948
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005949static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005950 struct perf_sample_data *data)
5951{
5952 void *record = data->raw->data;
5953
5954 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5955 return 1;
5956 return 0;
5957}
5958
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005959static int perf_tp_event_match(struct perf_event *event,
5960 struct perf_sample_data *data,
5961 struct pt_regs *regs)
5962{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005963 if (event->hw.state & PERF_HES_STOPPED)
5964 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005965 /*
5966 * All tracepoints are from kernel-space.
5967 */
5968 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005969 return 0;
5970
5971 if (!perf_tp_filter_match(event, data))
5972 return 0;
5973
5974 return 1;
5975}
5976
5977void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005978 struct pt_regs *regs, struct hlist_head *head, int rctx,
5979 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005980{
5981 struct perf_sample_data data;
5982 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005983
5984 struct perf_raw_record raw = {
5985 .size = entry_size,
5986 .data = record,
5987 };
5988
Robert Richterfd0d0002012-04-02 20:19:08 +02005989 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005990 data.raw = &raw;
5991
Sasha Levinb67bfe02013-02-27 17:06:00 -08005992 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005993 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005994 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005995 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005996
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005997 /*
5998 * If we got specified a target task, also iterate its context and
5999 * deliver this event there too.
6000 */
6001 if (task && task != current) {
6002 struct perf_event_context *ctx;
6003 struct trace_entry *entry = record;
6004
6005 rcu_read_lock();
6006 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
6007 if (!ctx)
6008 goto unlock;
6009
6010 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
6011 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6012 continue;
6013 if (event->attr.config != entry->type)
6014 continue;
6015 if (perf_tp_event_match(event, &data, regs))
6016 perf_swevent_event(event, count, &data, regs);
6017 }
6018unlock:
6019 rcu_read_unlock();
6020 }
6021
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006022 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006023}
6024EXPORT_SYMBOL_GPL(perf_tp_event);
6025
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006026static void tp_perf_event_destroy(struct perf_event *event)
6027{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006028 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006029}
6030
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006031static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006032{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006033 int err;
6034
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006035 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6036 return -ENOENT;
6037
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006038 /*
6039 * no branch sampling for tracepoint events
6040 */
6041 if (has_branch_stack(event))
6042 return -EOPNOTSUPP;
6043
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006044 err = perf_trace_init(event);
6045 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006046 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006047
6048 event->destroy = tp_perf_event_destroy;
6049
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006050 return 0;
6051}
6052
6053static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006054 .task_ctx_nr = perf_sw_context,
6055
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006056 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006057 .add = perf_trace_add,
6058 .del = perf_trace_del,
6059 .start = perf_swevent_start,
6060 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006061 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006062
6063 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006064};
6065
6066static inline void perf_tp_register(void)
6067{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006068 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006069}
Li Zefan6fb29152009-10-15 11:21:42 +08006070
6071static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6072{
6073 char *filter_str;
6074 int ret;
6075
6076 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6077 return -EINVAL;
6078
6079 filter_str = strndup_user(arg, PAGE_SIZE);
6080 if (IS_ERR(filter_str))
6081 return PTR_ERR(filter_str);
6082
6083 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
6084
6085 kfree(filter_str);
6086 return ret;
6087}
6088
6089static void perf_event_free_filter(struct perf_event *event)
6090{
6091 ftrace_profile_free_filter(event);
6092}
6093
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006094#else
Li Zefan6fb29152009-10-15 11:21:42 +08006095
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006096static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006097{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006098}
Li Zefan6fb29152009-10-15 11:21:42 +08006099
6100static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6101{
6102 return -ENOENT;
6103}
6104
6105static void perf_event_free_filter(struct perf_event *event)
6106{
6107}
6108
Li Zefan07b139c2009-12-21 14:27:35 +08006109#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006110
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006111#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006112void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006113{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006114 struct perf_sample_data sample;
6115 struct pt_regs *regs = data;
6116
Robert Richterfd0d0002012-04-02 20:19:08 +02006117 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006118
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006119 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006120 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006121}
6122#endif
6123
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006124/*
6125 * hrtimer based swevent callback
6126 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006127
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006128static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006129{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006130 enum hrtimer_restart ret = HRTIMER_RESTART;
6131 struct perf_sample_data data;
6132 struct pt_regs *regs;
6133 struct perf_event *event;
6134 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006135
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006136 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006137
6138 if (event->state != PERF_EVENT_STATE_ACTIVE)
6139 return HRTIMER_NORESTART;
6140
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006141 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006142
Robert Richterfd0d0002012-04-02 20:19:08 +02006143 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006144 regs = get_irq_regs();
6145
6146 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08006147 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02006148 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006149 ret = HRTIMER_NORESTART;
6150 }
6151
6152 period = max_t(u64, 10000, event->hw.sample_period);
6153 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
6154
6155 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006156}
6157
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006158static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006159{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006160 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006161 s64 period;
6162
6163 if (!is_sampling_event(event))
6164 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006165
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006166 period = local64_read(&hwc->period_left);
6167 if (period) {
6168 if (period < 0)
6169 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006170
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006171 local64_set(&hwc->period_left, 0);
6172 } else {
6173 period = max_t(u64, 10000, hwc->sample_period);
6174 }
6175 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006176 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006177 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006178}
6179
6180static void perf_swevent_cancel_hrtimer(struct perf_event *event)
6181{
6182 struct hw_perf_event *hwc = &event->hw;
6183
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006184 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006185 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006186 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006187
6188 hrtimer_cancel(&hwc->hrtimer);
6189 }
6190}
6191
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006192static void perf_swevent_init_hrtimer(struct perf_event *event)
6193{
6194 struct hw_perf_event *hwc = &event->hw;
6195
6196 if (!is_sampling_event(event))
6197 return;
6198
6199 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6200 hwc->hrtimer.function = perf_swevent_hrtimer;
6201
6202 /*
6203 * Since hrtimers have a fixed rate, we can do a static freq->period
6204 * mapping and avoid the whole period adjust feedback stuff.
6205 */
6206 if (event->attr.freq) {
6207 long freq = event->attr.sample_freq;
6208
6209 event->attr.sample_period = NSEC_PER_SEC / freq;
6210 hwc->sample_period = event->attr.sample_period;
6211 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09006212 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006213 event->attr.freq = 0;
6214 }
6215}
6216
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006217/*
6218 * Software event: cpu wall time clock
6219 */
6220
6221static void cpu_clock_event_update(struct perf_event *event)
6222{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006223 s64 prev;
6224 u64 now;
6225
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006226 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006227 prev = local64_xchg(&event->hw.prev_count, now);
6228 local64_add(now - prev, &event->count);
6229}
6230
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006231static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006232{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006233 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006234 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006235}
6236
6237static void cpu_clock_event_stop(struct perf_event *event, int flags)
6238{
6239 perf_swevent_cancel_hrtimer(event);
6240 cpu_clock_event_update(event);
6241}
6242
6243static int cpu_clock_event_add(struct perf_event *event, int flags)
6244{
6245 if (flags & PERF_EF_START)
6246 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006247
6248 return 0;
6249}
6250
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006251static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006252{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006253 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006254}
6255
6256static void cpu_clock_event_read(struct perf_event *event)
6257{
6258 cpu_clock_event_update(event);
6259}
6260
6261static int cpu_clock_event_init(struct perf_event *event)
6262{
6263 if (event->attr.type != PERF_TYPE_SOFTWARE)
6264 return -ENOENT;
6265
6266 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
6267 return -ENOENT;
6268
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006269 /*
6270 * no branch sampling for software events
6271 */
6272 if (has_branch_stack(event))
6273 return -EOPNOTSUPP;
6274
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006275 perf_swevent_init_hrtimer(event);
6276
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006277 return 0;
6278}
6279
6280static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006281 .task_ctx_nr = perf_sw_context,
6282
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006283 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006284 .add = cpu_clock_event_add,
6285 .del = cpu_clock_event_del,
6286 .start = cpu_clock_event_start,
6287 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006288 .read = cpu_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006289
6290 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006291};
6292
6293/*
6294 * Software event: task time clock
6295 */
6296
6297static void task_clock_event_update(struct perf_event *event, u64 now)
6298{
6299 u64 prev;
6300 s64 delta;
6301
6302 prev = local64_xchg(&event->hw.prev_count, now);
6303 delta = now - prev;
6304 local64_add(delta, &event->count);
6305}
6306
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006307static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006308{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006309 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006310 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006311}
6312
6313static void task_clock_event_stop(struct perf_event *event, int flags)
6314{
6315 perf_swevent_cancel_hrtimer(event);
6316 task_clock_event_update(event, event->ctx->time);
6317}
6318
6319static int task_clock_event_add(struct perf_event *event, int flags)
6320{
6321 if (flags & PERF_EF_START)
6322 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006323
6324 return 0;
6325}
6326
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006327static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006328{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006329 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006330}
6331
6332static void task_clock_event_read(struct perf_event *event)
6333{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01006334 u64 now = perf_clock();
6335 u64 delta = now - event->ctx->timestamp;
6336 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006337
6338 task_clock_event_update(event, time);
6339}
6340
6341static int task_clock_event_init(struct perf_event *event)
6342{
6343 if (event->attr.type != PERF_TYPE_SOFTWARE)
6344 return -ENOENT;
6345
6346 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6347 return -ENOENT;
6348
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006349 /*
6350 * no branch sampling for software events
6351 */
6352 if (has_branch_stack(event))
6353 return -EOPNOTSUPP;
6354
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006355 perf_swevent_init_hrtimer(event);
6356
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006357 return 0;
6358}
6359
6360static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006361 .task_ctx_nr = perf_sw_context,
6362
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006363 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006364 .add = task_clock_event_add,
6365 .del = task_clock_event_del,
6366 .start = task_clock_event_start,
6367 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006368 .read = task_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006369
6370 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006371};
6372
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006373static void perf_pmu_nop_void(struct pmu *pmu)
6374{
6375}
6376
6377static int perf_pmu_nop_int(struct pmu *pmu)
6378{
6379 return 0;
6380}
6381
6382static void perf_pmu_start_txn(struct pmu *pmu)
6383{
6384 perf_pmu_disable(pmu);
6385}
6386
6387static int perf_pmu_commit_txn(struct pmu *pmu)
6388{
6389 perf_pmu_enable(pmu);
6390 return 0;
6391}
6392
6393static void perf_pmu_cancel_txn(struct pmu *pmu)
6394{
6395 perf_pmu_enable(pmu);
6396}
6397
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006398static int perf_event_idx_default(struct perf_event *event)
6399{
6400 return event->hw.idx + 1;
6401}
6402
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006403/*
6404 * Ensures all contexts with the same task_ctx_nr have the same
6405 * pmu_cpu_context too.
6406 */
Mark Rutland9e317042014-02-10 17:44:18 +00006407static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006408{
6409 struct pmu *pmu;
6410
6411 if (ctxn < 0)
6412 return NULL;
6413
6414 list_for_each_entry(pmu, &pmus, entry) {
6415 if (pmu->task_ctx_nr == ctxn)
6416 return pmu->pmu_cpu_context;
6417 }
6418
6419 return NULL;
6420}
6421
Peter Zijlstra51676952010-12-07 14:18:20 +01006422static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006423{
Peter Zijlstra51676952010-12-07 14:18:20 +01006424 int cpu;
6425
6426 for_each_possible_cpu(cpu) {
6427 struct perf_cpu_context *cpuctx;
6428
6429 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6430
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006431 if (cpuctx->unique_pmu == old_pmu)
6432 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01006433 }
6434}
6435
6436static void free_pmu_context(struct pmu *pmu)
6437{
6438 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006439
6440 mutex_lock(&pmus_lock);
6441 /*
6442 * Like a real lame refcount.
6443 */
Peter Zijlstra51676952010-12-07 14:18:20 +01006444 list_for_each_entry(i, &pmus, entry) {
6445 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6446 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006447 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01006448 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006449 }
6450
Peter Zijlstra51676952010-12-07 14:18:20 +01006451 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006452out:
6453 mutex_unlock(&pmus_lock);
6454}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006455static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006456
Peter Zijlstraabe43402010-11-17 23:17:37 +01006457static ssize_t
6458type_show(struct device *dev, struct device_attribute *attr, char *page)
6459{
6460 struct pmu *pmu = dev_get_drvdata(dev);
6461
6462 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6463}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006464static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006465
Stephane Eranian62b85632013-04-03 14:21:34 +02006466static ssize_t
6467perf_event_mux_interval_ms_show(struct device *dev,
6468 struct device_attribute *attr,
6469 char *page)
6470{
6471 struct pmu *pmu = dev_get_drvdata(dev);
6472
6473 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6474}
6475
6476static ssize_t
6477perf_event_mux_interval_ms_store(struct device *dev,
6478 struct device_attribute *attr,
6479 const char *buf, size_t count)
6480{
6481 struct pmu *pmu = dev_get_drvdata(dev);
6482 int timer, cpu, ret;
6483
6484 ret = kstrtoint(buf, 0, &timer);
6485 if (ret)
6486 return ret;
6487
6488 if (timer < 1)
6489 return -EINVAL;
6490
6491 /* same value, noting to do */
6492 if (timer == pmu->hrtimer_interval_ms)
6493 return count;
6494
6495 pmu->hrtimer_interval_ms = timer;
6496
6497 /* update all cpuctx for this PMU */
6498 for_each_possible_cpu(cpu) {
6499 struct perf_cpu_context *cpuctx;
6500 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6501 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6502
6503 if (hrtimer_active(&cpuctx->hrtimer))
6504 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6505 }
6506
6507 return count;
6508}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006509static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02006510
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006511static struct attribute *pmu_dev_attrs[] = {
6512 &dev_attr_type.attr,
6513 &dev_attr_perf_event_mux_interval_ms.attr,
6514 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006515};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006516ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006517
6518static int pmu_bus_running;
6519static struct bus_type pmu_bus = {
6520 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006521 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006522};
6523
6524static void pmu_dev_release(struct device *dev)
6525{
6526 kfree(dev);
6527}
6528
6529static int pmu_dev_alloc(struct pmu *pmu)
6530{
6531 int ret = -ENOMEM;
6532
6533 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6534 if (!pmu->dev)
6535 goto out;
6536
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01006537 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01006538 device_initialize(pmu->dev);
6539 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6540 if (ret)
6541 goto free_dev;
6542
6543 dev_set_drvdata(pmu->dev, pmu);
6544 pmu->dev->bus = &pmu_bus;
6545 pmu->dev->release = pmu_dev_release;
6546 ret = device_add(pmu->dev);
6547 if (ret)
6548 goto free_dev;
6549
6550out:
6551 return ret;
6552
6553free_dev:
6554 put_device(pmu->dev);
6555 goto out;
6556}
6557
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006558static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006559static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006560
Mischa Jonker03d8e802013-06-04 11:45:48 +02006561int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006562{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006563 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006564
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006565 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006566 ret = -ENOMEM;
6567 pmu->pmu_disable_count = alloc_percpu(int);
6568 if (!pmu->pmu_disable_count)
6569 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006570
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006571 pmu->type = -1;
6572 if (!name)
6573 goto skip_type;
6574 pmu->name = name;
6575
6576 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08006577 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6578 if (type < 0) {
6579 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006580 goto free_pdc;
6581 }
6582 }
6583 pmu->type = type;
6584
Peter Zijlstraabe43402010-11-17 23:17:37 +01006585 if (pmu_bus_running) {
6586 ret = pmu_dev_alloc(pmu);
6587 if (ret)
6588 goto free_idr;
6589 }
6590
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006591skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006592 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6593 if (pmu->pmu_cpu_context)
6594 goto got_cpu_context;
6595
Wei Yongjunc4814202013-04-12 11:05:54 +08006596 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006597 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6598 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006599 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006600
6601 for_each_possible_cpu(cpu) {
6602 struct perf_cpu_context *cpuctx;
6603
6604 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006605 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006606 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006607 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006608 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006609 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02006610
6611 __perf_cpu_hrtimer_init(cpuctx, cpu);
6612
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006613 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006614 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006615 }
6616
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006617got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006618 if (!pmu->start_txn) {
6619 if (pmu->pmu_enable) {
6620 /*
6621 * If we have pmu_enable/pmu_disable calls, install
6622 * transaction stubs that use that to try and batch
6623 * hardware accesses.
6624 */
6625 pmu->start_txn = perf_pmu_start_txn;
6626 pmu->commit_txn = perf_pmu_commit_txn;
6627 pmu->cancel_txn = perf_pmu_cancel_txn;
6628 } else {
6629 pmu->start_txn = perf_pmu_nop_void;
6630 pmu->commit_txn = perf_pmu_nop_int;
6631 pmu->cancel_txn = perf_pmu_nop_void;
6632 }
6633 }
6634
6635 if (!pmu->pmu_enable) {
6636 pmu->pmu_enable = perf_pmu_nop_void;
6637 pmu->pmu_disable = perf_pmu_nop_void;
6638 }
6639
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006640 if (!pmu->event_idx)
6641 pmu->event_idx = perf_event_idx_default;
6642
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006643 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006644 ret = 0;
6645unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006646 mutex_unlock(&pmus_lock);
6647
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006648 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006649
Peter Zijlstraabe43402010-11-17 23:17:37 +01006650free_dev:
6651 device_del(pmu->dev);
6652 put_device(pmu->dev);
6653
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006654free_idr:
6655 if (pmu->type >= PERF_TYPE_MAX)
6656 idr_remove(&pmu_idr, pmu->type);
6657
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006658free_pdc:
6659 free_percpu(pmu->pmu_disable_count);
6660 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006661}
Yan, Zhengc464c762014-03-18 16:56:41 +08006662EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006663
6664void perf_pmu_unregister(struct pmu *pmu)
6665{
6666 mutex_lock(&pmus_lock);
6667 list_del_rcu(&pmu->entry);
6668 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006669
6670 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006671 * We dereference the pmu list under both SRCU and regular RCU, so
6672 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006673 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006674 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006675 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006676
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006677 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006678 if (pmu->type >= PERF_TYPE_MAX)
6679 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006680 device_del(pmu->dev);
6681 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006682 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006683}
Yan, Zhengc464c762014-03-18 16:56:41 +08006684EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006685
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006686struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006687{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006688 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006689 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08006690 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006691
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006692 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006693
6694 rcu_read_lock();
6695 pmu = idr_find(&pmu_idr, event->attr.type);
6696 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08006697 if (pmu) {
Yan, Zhengc464c762014-03-18 16:56:41 +08006698 if (!try_module_get(pmu->module)) {
6699 pmu = ERR_PTR(-ENODEV);
6700 goto unlock;
6701 }
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006702 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006703 ret = pmu->event_init(event);
6704 if (ret)
6705 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006706 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08006707 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006708
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006709 list_for_each_entry_rcu(pmu, &pmus, entry) {
Yan, Zhengc464c762014-03-18 16:56:41 +08006710 if (!try_module_get(pmu->module)) {
6711 pmu = ERR_PTR(-ENODEV);
6712 goto unlock;
6713 }
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006714 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006715 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006716 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006717 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006718
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006719 if (ret != -ENOENT) {
6720 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006721 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006722 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006723 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006724 pmu = ERR_PTR(-ENOENT);
6725unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006726 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006727
6728 return pmu;
6729}
6730
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006731static void account_event_cpu(struct perf_event *event, int cpu)
6732{
6733 if (event->parent)
6734 return;
6735
6736 if (has_branch_stack(event)) {
6737 if (!(event->attach_state & PERF_ATTACH_TASK))
6738 atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
6739 }
6740 if (is_cgroup_event(event))
6741 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
6742}
6743
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006744static void account_event(struct perf_event *event)
6745{
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006746 if (event->parent)
6747 return;
6748
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006749 if (event->attach_state & PERF_ATTACH_TASK)
6750 static_key_slow_inc(&perf_sched_events.key);
6751 if (event->attr.mmap || event->attr.mmap_data)
6752 atomic_inc(&nr_mmap_events);
6753 if (event->attr.comm)
6754 atomic_inc(&nr_comm_events);
6755 if (event->attr.task)
6756 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02006757 if (event->attr.freq) {
6758 if (atomic_inc_return(&nr_freq_events) == 1)
6759 tick_nohz_full_kick_all();
6760 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006761 if (has_branch_stack(event))
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006762 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006763 if (is_cgroup_event(event))
6764 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006765
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006766 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006767}
6768
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006769/*
6770 * Allocate and initialize a event structure
6771 */
6772static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006773perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006774 struct task_struct *task,
6775 struct perf_event *group_leader,
6776 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006777 perf_overflow_handler_t overflow_handler,
6778 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006779{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006780 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006781 struct perf_event *event;
6782 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006783 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006784
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006785 if ((unsigned)cpu >= nr_cpu_ids) {
6786 if (!task || cpu != -1)
6787 return ERR_PTR(-EINVAL);
6788 }
6789
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006790 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006791 if (!event)
6792 return ERR_PTR(-ENOMEM);
6793
6794 /*
6795 * Single events are their own group leaders, with an
6796 * empty sibling list:
6797 */
6798 if (!group_leader)
6799 group_leader = event;
6800
6801 mutex_init(&event->child_mutex);
6802 INIT_LIST_HEAD(&event->child_list);
6803
6804 INIT_LIST_HEAD(&event->group_entry);
6805 INIT_LIST_HEAD(&event->event_entry);
6806 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006807 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01006808 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01006809 INIT_HLIST_NODE(&event->hlist_entry);
6810
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006811
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006812 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006813 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006814
6815 mutex_init(&event->mmap_mutex);
6816
Al Viroa6fa9412012-08-20 14:59:25 +01006817 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006818 event->cpu = cpu;
6819 event->attr = *attr;
6820 event->group_leader = group_leader;
6821 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006822 event->oncpu = -1;
6823
6824 event->parent = parent_event;
6825
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08006826 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006827 event->id = atomic64_inc_return(&perf_event_id);
6828
6829 event->state = PERF_EVENT_STATE_INACTIVE;
6830
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006831 if (task) {
6832 event->attach_state = PERF_ATTACH_TASK;
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006833
6834 if (attr->type == PERF_TYPE_TRACEPOINT)
6835 event->hw.tp_target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006836#ifdef CONFIG_HAVE_HW_BREAKPOINT
6837 /*
6838 * hw_breakpoint is a bit difficult here..
6839 */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006840 else if (attr->type == PERF_TYPE_BREAKPOINT)
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006841 event->hw.bp_target = task;
6842#endif
6843 }
6844
Avi Kivity4dc0da82011-06-29 18:42:35 +03006845 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006846 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006847 context = parent_event->overflow_handler_context;
6848 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006849
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006850 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006851 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006852
Jiri Olsa0231bb52013-02-01 11:23:45 +01006853 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006854
6855 pmu = NULL;
6856
6857 hwc = &event->hw;
6858 hwc->sample_period = attr->sample_period;
6859 if (attr->freq && attr->sample_freq)
6860 hwc->sample_period = 1;
6861 hwc->last_period = hwc->sample_period;
6862
Peter Zijlstrae7850592010-05-21 14:43:08 +02006863 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006864
6865 /*
6866 * we currently do not support PERF_FORMAT_GROUP on inherited events
6867 */
6868 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006869 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006870
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006871 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006872 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006873 goto err_ns;
6874 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006875 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006876 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006877 }
6878
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006879 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006880 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6881 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006882 if (err)
6883 goto err_pmu;
Stephane Eraniand010b332012-02-09 23:21:00 +01006884 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006885 }
6886
6887 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006888
6889err_pmu:
6890 if (event->destroy)
6891 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08006892 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006893err_ns:
6894 if (event->ns)
6895 put_pid_ns(event->ns);
6896 kfree(event);
6897
6898 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006899}
6900
6901static int perf_copy_attr(struct perf_event_attr __user *uattr,
6902 struct perf_event_attr *attr)
6903{
6904 u32 size;
6905 int ret;
6906
6907 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6908 return -EFAULT;
6909
6910 /*
6911 * zero the full structure, so that a short copy will be nice.
6912 */
6913 memset(attr, 0, sizeof(*attr));
6914
6915 ret = get_user(size, &uattr->size);
6916 if (ret)
6917 return ret;
6918
6919 if (size > PAGE_SIZE) /* silly large */
6920 goto err_size;
6921
6922 if (!size) /* abi compat */
6923 size = PERF_ATTR_SIZE_VER0;
6924
6925 if (size < PERF_ATTR_SIZE_VER0)
6926 goto err_size;
6927
6928 /*
6929 * If we're handed a bigger struct than we know of,
6930 * ensure all the unknown bits are 0 - i.e. new
6931 * user-space does not rely on any kernel feature
6932 * extensions we dont know about yet.
6933 */
6934 if (size > sizeof(*attr)) {
6935 unsigned char __user *addr;
6936 unsigned char __user *end;
6937 unsigned char val;
6938
6939 addr = (void __user *)uattr + sizeof(*attr);
6940 end = (void __user *)uattr + size;
6941
6942 for (; addr < end; addr++) {
6943 ret = get_user(val, addr);
6944 if (ret)
6945 return ret;
6946 if (val)
6947 goto err_size;
6948 }
6949 size = sizeof(*attr);
6950 }
6951
6952 ret = copy_from_user(attr, uattr, size);
6953 if (ret)
6954 return -EFAULT;
6955
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306956 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006957 return -EINVAL;
6958
6959 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6960 return -EINVAL;
6961
6962 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6963 return -EINVAL;
6964
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006965 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6966 u64 mask = attr->branch_sample_type;
6967
6968 /* only using defined bits */
6969 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6970 return -EINVAL;
6971
6972 /* at least one branch bit must be set */
6973 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6974 return -EINVAL;
6975
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006976 /* propagate priv level, when not set for branch */
6977 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6978
6979 /* exclude_kernel checked on syscall entry */
6980 if (!attr->exclude_kernel)
6981 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6982
6983 if (!attr->exclude_user)
6984 mask |= PERF_SAMPLE_BRANCH_USER;
6985
6986 if (!attr->exclude_hv)
6987 mask |= PERF_SAMPLE_BRANCH_HV;
6988 /*
6989 * adjust user setting (for HW filter setup)
6990 */
6991 attr->branch_sample_type = mask;
6992 }
Stephane Eraniane7122092013-06-06 11:02:04 +02006993 /* privileged levels capture (kernel, hv): check permissions */
6994 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02006995 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6996 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006997 }
Jiri Olsa40189942012-08-07 15:20:37 +02006998
Jiri Olsac5ebced2012-08-07 15:20:40 +02006999 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02007000 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02007001 if (ret)
7002 return ret;
7003 }
7004
7005 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
7006 if (!arch_perf_have_user_stack_dump())
7007 return -ENOSYS;
7008
7009 /*
7010 * We have __u32 type for the size, but so far
7011 * we can only use __u16 as maximum due to the
7012 * __u16 sample size limit.
7013 */
7014 if (attr->sample_stack_user >= USHRT_MAX)
7015 ret = -EINVAL;
7016 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
7017 ret = -EINVAL;
7018 }
Jiri Olsa40189942012-08-07 15:20:37 +02007019
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007020out:
7021 return ret;
7022
7023err_size:
7024 put_user(sizeof(*attr), &uattr->size);
7025 ret = -E2BIG;
7026 goto out;
7027}
7028
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007029static int
7030perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007031{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007032 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007033 int ret = -EINVAL;
7034
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007035 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007036 goto set;
7037
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007038 /* don't allow circular references */
7039 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007040 goto out;
7041
Peter Zijlstra0f139302010-05-20 14:35:15 +02007042 /*
7043 * Don't allow cross-cpu buffers
7044 */
7045 if (output_event->cpu != event->cpu)
7046 goto out;
7047
7048 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02007049 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02007050 */
7051 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
7052 goto out;
7053
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007054set:
7055 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007056 /* Can't redirect output if we've got an active mmap() */
7057 if (atomic_read(&event->mmap_count))
7058 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007059
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007060 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02007061 /* get the rb we want to redirect to */
7062 rb = ring_buffer_get(output_event);
7063 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007064 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007065 }
7066
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007067 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02007068
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007069 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007070unlock:
7071 mutex_unlock(&event->mmap_mutex);
7072
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007073out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007074 return ret;
7075}
7076
7077/**
7078 * sys_perf_event_open - open a performance event, associate it to a task/cpu
7079 *
7080 * @attr_uptr: event_id type attributes for monitoring/sampling
7081 * @pid: target pid
7082 * @cpu: target cpu
7083 * @group_fd: group leader event fd
7084 */
7085SYSCALL_DEFINE5(perf_event_open,
7086 struct perf_event_attr __user *, attr_uptr,
7087 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
7088{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007089 struct perf_event *group_leader = NULL, *output_event = NULL;
7090 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007091 struct perf_event_attr attr;
7092 struct perf_event_context *ctx;
7093 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04007094 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07007095 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007096 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04007097 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007098 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007099 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01007100 int f_flags = O_RDWR;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007101
7102 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02007103 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007104 return -EINVAL;
7105
7106 err = perf_copy_attr(attr_uptr, &attr);
7107 if (err)
7108 return err;
7109
7110 if (!attr.exclude_kernel) {
7111 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7112 return -EACCES;
7113 }
7114
7115 if (attr.freq) {
7116 if (attr.sample_freq > sysctl_perf_event_sample_rate)
7117 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02007118 } else {
7119 if (attr.sample_period & (1ULL << 63))
7120 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007121 }
7122
Stephane Eraniane5d13672011-02-14 11:20:01 +02007123 /*
7124 * In cgroup mode, the pid argument is used to pass the fd
7125 * opened to the cgroup directory in cgroupfs. The cpu argument
7126 * designates the cpu on which to monitor threads from that
7127 * cgroup.
7128 */
7129 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
7130 return -EINVAL;
7131
Yann Droneauda21b0b32014-01-05 21:36:33 +01007132 if (flags & PERF_FLAG_FD_CLOEXEC)
7133 f_flags |= O_CLOEXEC;
7134
7135 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007136 if (event_fd < 0)
7137 return event_fd;
7138
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007139 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04007140 err = perf_fget_light(group_fd, &group);
7141 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007142 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04007143 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007144 if (flags & PERF_FLAG_FD_OUTPUT)
7145 output_event = group_leader;
7146 if (flags & PERF_FLAG_FD_NO_GROUP)
7147 group_leader = NULL;
7148 }
7149
Stephane Eraniane5d13672011-02-14 11:20:01 +02007150 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007151 task = find_lively_task_by_vpid(pid);
7152 if (IS_ERR(task)) {
7153 err = PTR_ERR(task);
7154 goto err_group_fd;
7155 }
7156 }
7157
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007158 if (task && group_leader &&
7159 group_leader->attr.inherit != attr.inherit) {
7160 err = -EINVAL;
7161 goto err_task;
7162 }
7163
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007164 get_online_cpus();
7165
Avi Kivity4dc0da82011-06-29 18:42:35 +03007166 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
7167 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007168 if (IS_ERR(event)) {
7169 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007170 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007171 }
7172
Stephane Eraniane5d13672011-02-14 11:20:01 +02007173 if (flags & PERF_FLAG_PID_CGROUP) {
7174 err = perf_cgroup_connect(pid, event, &attr, group_leader);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007175 if (err) {
7176 __free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007177 goto err_cpus;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007178 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02007179 }
7180
Vince Weaver53b25332014-05-16 17:12:12 -04007181 if (is_sampling_event(event)) {
7182 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
7183 err = -ENOTSUPP;
7184 goto err_alloc;
7185 }
7186 }
7187
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007188 account_event(event);
7189
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007190 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007191 * Special case software events and allow them to be part of
7192 * any hardware group.
7193 */
7194 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007195
7196 if (group_leader &&
7197 (is_software_event(event) != is_software_event(group_leader))) {
7198 if (is_software_event(event)) {
7199 /*
7200 * If event and group_leader are not both a software
7201 * event, and event is, then group leader is not.
7202 *
7203 * Allow the addition of software events to !software
7204 * groups, this is safe because software events never
7205 * fail to schedule.
7206 */
7207 pmu = group_leader->pmu;
7208 } else if (is_software_event(group_leader) &&
7209 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
7210 /*
7211 * In case the group is a pure software group, and we
7212 * try to add a hardware event, move the whole group to
7213 * the hardware context.
7214 */
7215 move_group = 1;
7216 }
7217 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007218
7219 /*
7220 * Get the target context (task or percpu):
7221 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007222 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007223 if (IS_ERR(ctx)) {
7224 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007225 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007226 }
7227
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02007228 if (task) {
7229 put_task_struct(task);
7230 task = NULL;
7231 }
7232
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007233 /*
7234 * Look up the group leader (we will attach this event to it):
7235 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007236 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007237 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007238
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007239 /*
7240 * Do not allow a recursive hierarchy (this new sibling
7241 * becoming part of another group-sibling):
7242 */
7243 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007244 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007245 /*
7246 * Do not allow to attach to a group in a different
7247 * task or CPU context:
7248 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007249 if (move_group) {
7250 if (group_leader->ctx->type != ctx->type)
7251 goto err_context;
7252 } else {
7253 if (group_leader->ctx != ctx)
7254 goto err_context;
7255 }
7256
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007257 /*
7258 * Only a group leader can be exclusive or pinned
7259 */
7260 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007261 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007262 }
7263
7264 if (output_event) {
7265 err = perf_event_set_output(event, output_event);
7266 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007267 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007268 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007269
Yann Droneauda21b0b32014-01-05 21:36:33 +01007270 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
7271 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007272 if (IS_ERR(event_file)) {
7273 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007274 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04007275 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007276
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007277 if (move_group) {
7278 struct perf_event_context *gctx = group_leader->ctx;
7279
7280 mutex_lock(&gctx->mutex);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007281 perf_remove_from_context(group_leader, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007282
7283 /*
7284 * Removing from the context ends up with disabled
7285 * event. What we want here is event in the initial
7286 * startup state, ready to be add into new context.
7287 */
7288 perf_event__state_init(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007289 list_for_each_entry(sibling, &group_leader->sibling_list,
7290 group_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007291 perf_remove_from_context(sibling, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007292 perf_event__state_init(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007293 put_ctx(gctx);
7294 }
7295 mutex_unlock(&gctx->mutex);
7296 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007297 }
7298
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007299 WARN_ON_ONCE(ctx->parent_ctx);
7300 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007301
7302 if (move_group) {
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007303 synchronize_rcu();
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007304 perf_install_in_context(ctx, group_leader, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007305 get_ctx(ctx);
7306 list_for_each_entry(sibling, &group_leader->sibling_list,
7307 group_entry) {
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007308 perf_install_in_context(ctx, sibling, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007309 get_ctx(ctx);
7310 }
7311 }
7312
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007313 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007314 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007315 mutex_unlock(&ctx->mutex);
7316
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007317 put_online_cpus();
7318
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007319 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01007320
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007321 mutex_lock(&current->perf_event_mutex);
7322 list_add_tail(&event->owner_entry, &current->perf_event_list);
7323 mutex_unlock(&current->perf_event_mutex);
7324
Peter Zijlstra8a495422010-05-27 15:47:49 +02007325 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007326 * Precalculate sample_data sizes
7327 */
7328 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007329 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007330
7331 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02007332 * Drop the reference on the group_event after placing the
7333 * new event on the sibling_list. This ensures destruction
7334 * of the group leader will find the pointer to itself in
7335 * perf_group_detach().
7336 */
Al Viro2903ff02012-08-28 12:52:22 -04007337 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007338 fd_install(event_fd, event_file);
7339 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007340
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007341err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007342 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04007343 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007344err_alloc:
7345 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007346err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007347 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007348err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007349 if (task)
7350 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007351err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04007352 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007353err_fd:
7354 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007355 return err;
7356}
7357
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007358/**
7359 * perf_event_create_kernel_counter
7360 *
7361 * @attr: attributes of the counter to create
7362 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07007363 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007364 */
7365struct perf_event *
7366perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07007367 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007368 perf_overflow_handler_t overflow_handler,
7369 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007370{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007371 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007372 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007373 int err;
7374
7375 /*
7376 * Get the target context (task or percpu):
7377 */
7378
Avi Kivity4dc0da82011-06-29 18:42:35 +03007379 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7380 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007381 if (IS_ERR(event)) {
7382 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007383 goto err;
7384 }
7385
Jiri Olsaf8697762014-08-01 14:33:01 +02007386 /* Mark owner so we could distinguish it from user events. */
7387 event->owner = EVENT_OWNER_KERNEL;
7388
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007389 account_event(event);
7390
Matt Helsley38a81da2010-09-13 13:01:20 -07007391 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007392 if (IS_ERR(ctx)) {
7393 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007394 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007395 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007396
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007397 WARN_ON_ONCE(ctx->parent_ctx);
7398 mutex_lock(&ctx->mutex);
7399 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007400 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007401 mutex_unlock(&ctx->mutex);
7402
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007403 return event;
7404
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007405err_free:
7406 free_event(event);
7407err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007408 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007409}
7410EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7411
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007412void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7413{
7414 struct perf_event_context *src_ctx;
7415 struct perf_event_context *dst_ctx;
7416 struct perf_event *event, *tmp;
7417 LIST_HEAD(events);
7418
7419 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7420 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7421
7422 mutex_lock(&src_ctx->mutex);
7423 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7424 event_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007425 perf_remove_from_context(event, false);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007426 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007427 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02007428 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007429 }
7430 mutex_unlock(&src_ctx->mutex);
7431
7432 synchronize_rcu();
7433
7434 mutex_lock(&dst_ctx->mutex);
Peter Zijlstra98861672013-10-03 16:02:23 +02007435 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7436 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007437 if (event->state >= PERF_EVENT_STATE_OFF)
7438 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007439 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007440 perf_install_in_context(dst_ctx, event, dst_cpu);
7441 get_ctx(dst_ctx);
7442 }
7443 mutex_unlock(&dst_ctx->mutex);
7444}
7445EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7446
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007447static void sync_child_event(struct perf_event *child_event,
7448 struct task_struct *child)
7449{
7450 struct perf_event *parent_event = child_event->parent;
7451 u64 child_val;
7452
7453 if (child_event->attr.inherit_stat)
7454 perf_event_read_event(child_event, child);
7455
Peter Zijlstrab5e58792010-05-21 14:43:12 +02007456 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007457
7458 /*
7459 * Add back the child's count to the parent's count:
7460 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02007461 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007462 atomic64_add(child_event->total_time_enabled,
7463 &parent_event->child_total_time_enabled);
7464 atomic64_add(child_event->total_time_running,
7465 &parent_event->child_total_time_running);
7466
7467 /*
7468 * Remove this event from the parent's list
7469 */
7470 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7471 mutex_lock(&parent_event->child_mutex);
7472 list_del_init(&child_event->child_list);
7473 mutex_unlock(&parent_event->child_mutex);
7474
7475 /*
7476 * Release the parent event, if this was the last
7477 * reference to it.
7478 */
Al Viroa6fa9412012-08-20 14:59:25 +01007479 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007480}
7481
7482static void
7483__perf_event_exit_task(struct perf_event *child_event,
7484 struct perf_event_context *child_ctx,
7485 struct task_struct *child)
7486{
Peter Zijlstra1903d502014-07-15 17:27:27 +02007487 /*
7488 * Do not destroy the 'original' grouping; because of the context
7489 * switch optimization the original events could've ended up in a
7490 * random child task.
7491 *
7492 * If we were to destroy the original group, all group related
7493 * operations would cease to function properly after this random
7494 * child dies.
7495 *
7496 * Do destroy all inherited groups, we don't care about those
7497 * and being thorough is better.
7498 */
7499 perf_remove_from_context(child_event, !!child_event->parent);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007500
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007501 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007502 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007503 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007504 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007505 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007506 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007507 sync_child_event(child_event, child);
7508 free_event(child_event);
7509 }
7510}
7511
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007512static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007513{
Peter Zijlstraebf905f2014-05-29 19:00:24 +02007514 struct perf_event *child_event, *next;
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007515 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007516 unsigned long flags;
7517
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007518 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007519 perf_event_task(child, NULL, 0);
7520 return;
7521 }
7522
7523 local_irq_save(flags);
7524 /*
7525 * We can't reschedule here because interrupts are disabled,
7526 * and either child is current or it is a task that can't be
7527 * scheduled, so we are now safe from rescheduling changing
7528 * our context.
7529 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01007530 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007531
7532 /*
7533 * Take the context lock here so that if find_get_context is
7534 * reading child->perf_event_ctxp, we wait until it has
7535 * incremented the context's refcount before we do put_ctx below.
7536 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007537 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02007538 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007539 child->perf_event_ctxp[ctxn] = NULL;
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007540
7541 /*
7542 * In order to avoid freeing: child_ctx->parent_ctx->task
7543 * under perf_event_context::lock, grab another reference.
7544 */
7545 parent_ctx = child_ctx->parent_ctx;
7546 if (parent_ctx)
7547 get_ctx(parent_ctx);
7548
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007549 /*
7550 * If this context is a clone; unclone it so it can't get
7551 * swapped to another process while we're removing all
7552 * the events from it.
7553 */
7554 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01007555 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007556 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007557
7558 /*
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007559 * Now that we no longer hold perf_event_context::lock, drop
7560 * our extra child_ctx->parent_ctx reference.
7561 */
7562 if (parent_ctx)
7563 put_ctx(parent_ctx);
7564
7565 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007566 * Report the task dead after unscheduling the events so that we
7567 * won't get any samples after PERF_RECORD_EXIT. We can however still
7568 * get a few PERF_RECORD_READ events.
7569 */
7570 perf_event_task(child, child_ctx, 0);
7571
7572 /*
7573 * We can recurse on the same lock type through:
7574 *
7575 * __perf_event_exit_task()
7576 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01007577 * put_event()
7578 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007579 *
7580 * But since its the parent context it won't be the same instance.
7581 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02007582 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007583
Peter Zijlstraebf905f2014-05-29 19:00:24 +02007584 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007585 __perf_event_exit_task(child_event, child_ctx, child);
7586
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007587 mutex_unlock(&child_ctx->mutex);
7588
7589 put_ctx(child_ctx);
7590}
7591
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007592/*
7593 * When a child task exits, feed back event values to parent events.
7594 */
7595void perf_event_exit_task(struct task_struct *child)
7596{
Peter Zijlstra88821352010-11-09 19:01:43 +01007597 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007598 int ctxn;
7599
Peter Zijlstra88821352010-11-09 19:01:43 +01007600 mutex_lock(&child->perf_event_mutex);
7601 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
7602 owner_entry) {
7603 list_del_init(&event->owner_entry);
7604
7605 /*
7606 * Ensure the list deletion is visible before we clear
7607 * the owner, closes a race against perf_release() where
7608 * we need to serialize on the owner->perf_event_mutex.
7609 */
7610 smp_wmb();
7611 event->owner = NULL;
7612 }
7613 mutex_unlock(&child->perf_event_mutex);
7614
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007615 for_each_task_context_nr(ctxn)
7616 perf_event_exit_task_context(child, ctxn);
7617}
7618
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007619static void perf_free_event(struct perf_event *event,
7620 struct perf_event_context *ctx)
7621{
7622 struct perf_event *parent = event->parent;
7623
7624 if (WARN_ON_ONCE(!parent))
7625 return;
7626
7627 mutex_lock(&parent->child_mutex);
7628 list_del_init(&event->child_list);
7629 mutex_unlock(&parent->child_mutex);
7630
Al Viroa6fa9412012-08-20 14:59:25 +01007631 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007632
Peter Zijlstra8a495422010-05-27 15:47:49 +02007633 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007634 list_del_event(event, ctx);
7635 free_event(event);
7636}
7637
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007638/*
7639 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007640 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007641 */
7642void perf_event_free_task(struct task_struct *task)
7643{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007644 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007645 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007646 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007647
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007648 for_each_task_context_nr(ctxn) {
7649 ctx = task->perf_event_ctxp[ctxn];
7650 if (!ctx)
7651 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007652
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007653 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007654again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007655 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7656 group_entry)
7657 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007658
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007659 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7660 group_entry)
7661 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007662
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007663 if (!list_empty(&ctx->pinned_groups) ||
7664 !list_empty(&ctx->flexible_groups))
7665 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007666
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007667 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007668
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007669 put_ctx(ctx);
7670 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007671}
7672
Peter Zijlstra4e231c72010-09-09 21:01:59 +02007673void perf_event_delayed_put(struct task_struct *task)
7674{
7675 int ctxn;
7676
7677 for_each_task_context_nr(ctxn)
7678 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7679}
7680
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007681/*
7682 * inherit a event from parent task to child task:
7683 */
7684static struct perf_event *
7685inherit_event(struct perf_event *parent_event,
7686 struct task_struct *parent,
7687 struct perf_event_context *parent_ctx,
7688 struct task_struct *child,
7689 struct perf_event *group_leader,
7690 struct perf_event_context *child_ctx)
7691{
7692 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02007693 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007694
7695 /*
7696 * Instead of creating recursive hierarchies of events,
7697 * we link inherited events back to the original parent,
7698 * which has a filp for sure, which we use as the reference
7699 * count:
7700 */
7701 if (parent_event->parent)
7702 parent_event = parent_event->parent;
7703
7704 child_event = perf_event_alloc(&parent_event->attr,
7705 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007706 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007707 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007708 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007709 if (IS_ERR(child_event))
7710 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01007711
7712 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7713 free_event(child_event);
7714 return NULL;
7715 }
7716
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007717 get_ctx(child_ctx);
7718
7719 /*
7720 * Make the child state follow the state of the parent event,
7721 * not its attr.disabled bit. We hold the parent's mutex,
7722 * so we won't race with perf_event_{en, dis}able_family.
7723 */
7724 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7725 child_event->state = PERF_EVENT_STATE_INACTIVE;
7726 else
7727 child_event->state = PERF_EVENT_STATE_OFF;
7728
7729 if (parent_event->attr.freq) {
7730 u64 sample_period = parent_event->hw.sample_period;
7731 struct hw_perf_event *hwc = &child_event->hw;
7732
7733 hwc->sample_period = sample_period;
7734 hwc->last_period = sample_period;
7735
7736 local64_set(&hwc->period_left, sample_period);
7737 }
7738
7739 child_event->ctx = child_ctx;
7740 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007741 child_event->overflow_handler_context
7742 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007743
7744 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02007745 * Precalculate sample_data sizes
7746 */
7747 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007748 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02007749
7750 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007751 * Link it up in the child's context:
7752 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02007753 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007754 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02007755 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007756
7757 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007758 * Link this into the parent event's child list
7759 */
7760 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7761 mutex_lock(&parent_event->child_mutex);
7762 list_add_tail(&child_event->child_list, &parent_event->child_list);
7763 mutex_unlock(&parent_event->child_mutex);
7764
7765 return child_event;
7766}
7767
7768static int inherit_group(struct perf_event *parent_event,
7769 struct task_struct *parent,
7770 struct perf_event_context *parent_ctx,
7771 struct task_struct *child,
7772 struct perf_event_context *child_ctx)
7773{
7774 struct perf_event *leader;
7775 struct perf_event *sub;
7776 struct perf_event *child_ctr;
7777
7778 leader = inherit_event(parent_event, parent, parent_ctx,
7779 child, NULL, child_ctx);
7780 if (IS_ERR(leader))
7781 return PTR_ERR(leader);
7782 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7783 child_ctr = inherit_event(sub, parent, parent_ctx,
7784 child, leader, child_ctx);
7785 if (IS_ERR(child_ctr))
7786 return PTR_ERR(child_ctr);
7787 }
7788 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007789}
7790
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007791static int
7792inherit_task_group(struct perf_event *event, struct task_struct *parent,
7793 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007794 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007795 int *inherited_all)
7796{
7797 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007798 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007799
7800 if (!event->attr.inherit) {
7801 *inherited_all = 0;
7802 return 0;
7803 }
7804
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007805 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007806 if (!child_ctx) {
7807 /*
7808 * This is executed from the parent task context, so
7809 * inherit events that have been marked for cloning.
7810 * First allocate and initialize a context for the
7811 * child.
7812 */
7813
Jiri Olsa734df5a2013-07-09 17:44:10 +02007814 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007815 if (!child_ctx)
7816 return -ENOMEM;
7817
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007818 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007819 }
7820
7821 ret = inherit_group(event, parent, parent_ctx,
7822 child, child_ctx);
7823
7824 if (ret)
7825 *inherited_all = 0;
7826
7827 return ret;
7828}
7829
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007830/*
7831 * Initialize the perf_event context in task_struct
7832 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02007833static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007834{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007835 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007836 struct perf_event_context *cloned_ctx;
7837 struct perf_event *event;
7838 struct task_struct *parent = current;
7839 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007840 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007841 int ret = 0;
7842
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007843 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007844 return 0;
7845
7846 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007847 * If the parent's context is a clone, pin it so it won't get
7848 * swapped under us.
7849 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007850 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02007851 if (!parent_ctx)
7852 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007853
7854 /*
7855 * No need to check if parent_ctx != NULL here; since we saw
7856 * it non-NULL earlier, the only reason for it to become NULL
7857 * is if we exit, and since we're currently in the middle of
7858 * a fork we can't be exiting at the same time.
7859 */
7860
7861 /*
7862 * Lock the parent list. No need to lock the child - not PID
7863 * hashed yet and not running, so nobody can access it.
7864 */
7865 mutex_lock(&parent_ctx->mutex);
7866
7867 /*
7868 * We dont have to disable NMIs - we are only looking at
7869 * the list, not manipulating it:
7870 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007871 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007872 ret = inherit_task_group(event, parent, parent_ctx,
7873 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007874 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007875 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007876 }
7877
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007878 /*
7879 * We can't hold ctx->lock when iterating the ->flexible_group list due
7880 * to allocations, but we need to prevent rotation because
7881 * rotate_ctx() will change the list from interrupt context.
7882 */
7883 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7884 parent_ctx->rotate_disable = 1;
7885 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7886
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007887 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007888 ret = inherit_task_group(event, parent, parent_ctx,
7889 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007890 if (ret)
7891 break;
7892 }
7893
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007894 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7895 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007896
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007897 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007898
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01007899 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007900 /*
7901 * Mark the child context as a clone of the parent
7902 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007903 *
7904 * Note that if the parent is a clone, the holding of
7905 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007906 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007907 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007908 if (cloned_ctx) {
7909 child_ctx->parent_ctx = cloned_ctx;
7910 child_ctx->parent_gen = parent_ctx->parent_gen;
7911 } else {
7912 child_ctx->parent_ctx = parent_ctx;
7913 child_ctx->parent_gen = parent_ctx->generation;
7914 }
7915 get_ctx(child_ctx->parent_ctx);
7916 }
7917
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007918 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007919 mutex_unlock(&parent_ctx->mutex);
7920
7921 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007922 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007923
7924 return ret;
7925}
7926
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007927/*
7928 * Initialize the perf_event context in task_struct
7929 */
7930int perf_event_init_task(struct task_struct *child)
7931{
7932 int ctxn, ret;
7933
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01007934 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7935 mutex_init(&child->perf_event_mutex);
7936 INIT_LIST_HEAD(&child->perf_event_list);
7937
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007938 for_each_task_context_nr(ctxn) {
7939 ret = perf_event_init_context(child, ctxn);
7940 if (ret)
7941 return ret;
7942 }
7943
7944 return 0;
7945}
7946
Paul Mackerras220b1402010-03-10 20:45:52 +11007947static void __init perf_event_init_all_cpus(void)
7948{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007949 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11007950 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11007951
7952 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007953 swhash = &per_cpu(swevent_htable, cpu);
7954 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007955 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11007956 }
7957}
7958
Paul Gortmaker0db06282013-06-19 14:53:51 -04007959static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007960{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007961 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007962
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007963 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02007964 swhash->online = true;
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007965 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007966 struct swevent_hlist *hlist;
7967
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007968 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7969 WARN_ON(!hlist);
7970 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007971 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007972 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007973}
7974
Peter Zijlstrac2774432010-12-08 15:29:02 +01007975#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007976static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007977{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007978 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7979
7980 WARN_ON(!irqs_disabled());
7981
7982 list_del_init(&cpuctx->rotation_list);
7983}
7984
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007985static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007986{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007987 struct remove_event re = { .detach_group = false };
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007988 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007989
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007990 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02007991
Peter Zijlstrae3703f82014-02-24 12:06:12 +01007992 rcu_read_lock();
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007993 list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
7994 __perf_remove_from_context(&re);
Peter Zijlstrae3703f82014-02-24 12:06:12 +01007995 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007996}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007997
7998static void perf_event_exit_cpu_context(int cpu)
7999{
8000 struct perf_event_context *ctx;
8001 struct pmu *pmu;
8002 int idx;
8003
8004 idx = srcu_read_lock(&pmus_srcu);
8005 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02008006 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008007
8008 mutex_lock(&ctx->mutex);
8009 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
8010 mutex_unlock(&ctx->mutex);
8011 }
8012 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008013}
8014
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008015static void perf_event_exit_cpu(int cpu)
8016{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008017 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008018
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008019 perf_event_exit_cpu_context(cpu);
8020
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008021 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02008022 swhash->online = false;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008023 swevent_hlist_release(swhash);
8024 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008025}
8026#else
8027static inline void perf_event_exit_cpu(int cpu) { }
8028#endif
8029
Peter Zijlstrac2774432010-12-08 15:29:02 +01008030static int
8031perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
8032{
8033 int cpu;
8034
8035 for_each_online_cpu(cpu)
8036 perf_event_exit_cpu(cpu);
8037
8038 return NOTIFY_OK;
8039}
8040
8041/*
8042 * Run the perf reboot notifier at the very last possible moment so that
8043 * the generic watchdog code runs as long as possible.
8044 */
8045static struct notifier_block perf_reboot_notifier = {
8046 .notifier_call = perf_reboot,
8047 .priority = INT_MIN,
8048};
8049
Paul Gortmaker0db06282013-06-19 14:53:51 -04008050static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008051perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
8052{
8053 unsigned int cpu = (long)hcpu;
8054
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008055 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008056
8057 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02008058 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008059 perf_event_init_cpu(cpu);
8060 break;
8061
Peter Zijlstra5e116372010-06-11 13:35:08 +02008062 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008063 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008064 perf_event_exit_cpu(cpu);
8065 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008066 default:
8067 break;
8068 }
8069
8070 return NOTIFY_OK;
8071}
8072
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008073void __init perf_event_init(void)
8074{
Jason Wessel3c502e72010-11-04 17:33:01 -05008075 int ret;
8076
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008077 idr_init(&pmu_idr);
8078
Paul Mackerras220b1402010-03-10 20:45:52 +11008079 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008080 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008081 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
8082 perf_pmu_register(&perf_cpu_clock, NULL, -1);
8083 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008084 perf_tp_register();
8085 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01008086 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05008087
8088 ret = init_hw_breakpoint();
8089 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02008090
8091 /* do not patch jump label more than once per second */
8092 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01008093
8094 /*
8095 * Build time assertion that we keep the data_head at the intended
8096 * location. IOW, validation we got the __reserved[] size right.
8097 */
8098 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
8099 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008100}
Peter Zijlstraabe43402010-11-17 23:17:37 +01008101
8102static int __init perf_event_sysfs_init(void)
8103{
8104 struct pmu *pmu;
8105 int ret;
8106
8107 mutex_lock(&pmus_lock);
8108
8109 ret = bus_register(&pmu_bus);
8110 if (ret)
8111 goto unlock;
8112
8113 list_for_each_entry(pmu, &pmus, entry) {
8114 if (!pmu->name || pmu->type < 0)
8115 continue;
8116
8117 ret = pmu_dev_alloc(pmu);
8118 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
8119 }
8120 pmu_bus_running = 1;
8121 ret = 0;
8122
8123unlock:
8124 mutex_unlock(&pmus_lock);
8125
8126 return ret;
8127}
8128device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008129
8130#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04008131static struct cgroup_subsys_state *
8132perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008133{
8134 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02008135
Li Zefan1b15d052011-03-03 14:26:06 +08008136 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008137 if (!jc)
8138 return ERR_PTR(-ENOMEM);
8139
Stephane Eraniane5d13672011-02-14 11:20:01 +02008140 jc->info = alloc_percpu(struct perf_cgroup_info);
8141 if (!jc->info) {
8142 kfree(jc);
8143 return ERR_PTR(-ENOMEM);
8144 }
8145
Stephane Eraniane5d13672011-02-14 11:20:01 +02008146 return &jc->css;
8147}
8148
Tejun Heoeb954192013-08-08 20:11:23 -04008149static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008150{
Tejun Heoeb954192013-08-08 20:11:23 -04008151 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
8152
Stephane Eraniane5d13672011-02-14 11:20:01 +02008153 free_percpu(jc->info);
8154 kfree(jc);
8155}
8156
8157static int __perf_cgroup_move(void *info)
8158{
8159 struct task_struct *task = info;
8160 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
8161 return 0;
8162}
8163
Tejun Heoeb954192013-08-08 20:11:23 -04008164static void perf_cgroup_attach(struct cgroup_subsys_state *css,
8165 struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008166{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008167 struct task_struct *task;
8168
Tejun Heo924f0d92014-02-13 06:58:41 -05008169 cgroup_taskset_for_each(task, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008170 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008171}
8172
Tejun Heoeb954192013-08-08 20:11:23 -04008173static void perf_cgroup_exit(struct cgroup_subsys_state *css,
8174 struct cgroup_subsys_state *old_css,
Li Zefan761b3ef2012-01-31 13:47:36 +08008175 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008176{
8177 /*
8178 * cgroup_exit() is called in the copy_process() failure path.
8179 * Ignore this case since the task hasn't ran yet, this avoids
8180 * trying to poke a half freed task state from generic code.
8181 */
8182 if (!(task->flags & PF_EXITING))
8183 return;
8184
Tejun Heobb9d97b2011-12-12 18:12:21 -08008185 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008186}
8187
Tejun Heo073219e2014-02-08 10:36:58 -05008188struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008189 .css_alloc = perf_cgroup_css_alloc,
8190 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008191 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008192 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02008193};
8194#endif /* CONFIG_CGROUP_PERF */