blob: 1833bc5a84a73049b6e1f27caed87adfbdf44b2c [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>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020042
Frederic Weisbecker76369132011-05-19 19:55:04 +020043#include "internal.h"
44
Ingo Molnarcdd6c482009-09-21 12:02:48 +020045#include <asm/irq_regs.h>
46
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010047struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020048 struct task_struct *p;
49 int (*func)(void *info);
50 void *info;
51 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010052};
53
54static void remote_function(void *data)
55{
56 struct remote_function_call *tfc = data;
57 struct task_struct *p = tfc->p;
58
59 if (p) {
60 tfc->ret = -EAGAIN;
61 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
62 return;
63 }
64
65 tfc->ret = tfc->func(tfc->info);
66}
67
68/**
69 * task_function_call - call a function on the cpu on which a task runs
70 * @p: the task to evaluate
71 * @func: the function to be called
72 * @info: the function call argument
73 *
74 * Calls the function @func when the task is currently running. This might
75 * be on the current CPU, which just calls the function directly
76 *
77 * returns: @func return value, or
78 * -ESRCH - when the process isn't running
79 * -EAGAIN - when the process moved away
80 */
81static int
82task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
83{
84 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020085 .p = p,
86 .func = func,
87 .info = info,
88 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010089 };
90
91 if (task_curr(p))
92 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
93
94 return data.ret;
95}
96
97/**
98 * cpu_function_call - call a function on the cpu
99 * @func: the function to be called
100 * @info: the function call argument
101 *
102 * Calls the function @func on the remote cpu.
103 *
104 * returns: @func return value or -ENXIO when the cpu is offline
105 */
106static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
107{
108 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200109 .p = NULL,
110 .func = func,
111 .info = info,
112 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100113 };
114
115 smp_call_function_single(cpu, remote_function, &data, 1);
116
117 return data.ret;
118}
119
Stephane Eraniane5d13672011-02-14 11:20:01 +0200120#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
121 PERF_FLAG_FD_OUTPUT |\
122 PERF_FLAG_PID_CGROUP)
123
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100124/*
125 * branch priv levels that need permission checks
126 */
127#define PERF_SAMPLE_BRANCH_PERM_PLM \
128 (PERF_SAMPLE_BRANCH_KERNEL |\
129 PERF_SAMPLE_BRANCH_HV)
130
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200131enum event_type_t {
132 EVENT_FLEXIBLE = 0x1,
133 EVENT_PINNED = 0x2,
134 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
135};
136
Stephane Eraniane5d13672011-02-14 11:20:01 +0200137/*
138 * perf_sched_events : >0 events exist
139 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
140 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100141struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200142static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Stephane Eraniand010b332012-02-09 23:21:00 +0100143static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200144
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200145static atomic_t nr_mmap_events __read_mostly;
146static atomic_t nr_comm_events __read_mostly;
147static atomic_t nr_task_events __read_mostly;
148
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200149static LIST_HEAD(pmus);
150static DEFINE_MUTEX(pmus_lock);
151static struct srcu_struct pmus_srcu;
152
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200153/*
154 * perf event paranoia level:
155 * -1 - not paranoid at all
156 * 0 - disallow raw tracepoint access for unpriv
157 * 1 - disallow cpu events for unpriv
158 * 2 - disallow kernel profiling for unpriv
159 */
160int sysctl_perf_event_paranoid __read_mostly = 1;
161
Frederic Weisbecker20443382011-03-31 03:33:29 +0200162/* Minimum for 512 kiB + 1 user control page */
163int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200164
165/*
166 * max perf event sample rate
167 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700168#define DEFAULT_MAX_SAMPLE_RATE 100000
169#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
170#define DEFAULT_CPU_TIME_MAX_PERCENT 25
171
172int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
173
174static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
175static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
176
177static atomic_t perf_sample_allowed_ns __read_mostly =
178 ATOMIC_INIT( DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100);
179
180void update_perf_cpu_limits(void)
181{
182 u64 tmp = perf_sample_period_ns;
183
184 tmp *= sysctl_perf_cpu_time_max_percent;
Stephane Eraniane5302922013-07-05 00:30:11 +0200185 do_div(tmp, 100);
Dave Hansen14c63f12013-06-21 08:51:36 -0700186 atomic_set(&perf_sample_allowed_ns, tmp);
187}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100188
Stephane Eranian9e630202013-04-03 14:21:33 +0200189static int perf_rotate_context(struct perf_cpu_context *cpuctx);
190
Peter Zijlstra163ec432011-02-16 11:22:34 +0100191int perf_proc_update_handler(struct ctl_table *table, int write,
192 void __user *buffer, size_t *lenp,
193 loff_t *ppos)
194{
195 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
196
197 if (ret || !write)
198 return ret;
199
200 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700201 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
202 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100203
204 return 0;
205}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200206
Dave Hansen14c63f12013-06-21 08:51:36 -0700207int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
208
209int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
210 void __user *buffer, size_t *lenp,
211 loff_t *ppos)
212{
213 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
214
215 if (ret || !write)
216 return ret;
217
218 update_perf_cpu_limits();
219
220 return 0;
221}
222
223/*
224 * perf samples are done in some very critical code paths (NMIs).
225 * If they take too much CPU time, the system can lock up and not
226 * get any real work done. This will drop the sample rate when
227 * we detect that events are taking too long.
228 */
229#define NR_ACCUMULATED_SAMPLES 128
230DEFINE_PER_CPU(u64, running_sample_length);
231
232void perf_sample_event_took(u64 sample_len_ns)
233{
234 u64 avg_local_sample_len;
Stephane Eraniane5302922013-07-05 00:30:11 +0200235 u64 local_samples_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700236
237 if (atomic_read(&perf_sample_allowed_ns) == 0)
238 return;
239
240 /* decay the counter by 1 average sample */
241 local_samples_len = __get_cpu_var(running_sample_length);
242 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
243 local_samples_len += sample_len_ns;
244 __get_cpu_var(running_sample_length) = local_samples_len;
245
246 /*
247 * note: this will be biased artifically low until we have
248 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
249 * from having to maintain a count.
250 */
251 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
252
253 if (avg_local_sample_len <= atomic_read(&perf_sample_allowed_ns))
254 return;
255
256 if (max_samples_per_tick <= 1)
257 return;
258
259 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
260 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
261 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
262
263 printk_ratelimited(KERN_WARNING
264 "perf samples too long (%lld > %d), lowering "
265 "kernel.perf_event_max_sample_rate to %d\n",
266 avg_local_sample_len,
267 atomic_read(&perf_sample_allowed_ns),
268 sysctl_perf_event_sample_rate);
269
270 update_perf_cpu_limits();
271}
272
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200273static atomic64_t perf_event_id;
274
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200275static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
276 enum event_type_t event_type);
277
278static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200279 enum event_type_t event_type,
280 struct task_struct *task);
281
282static void update_context_time(struct perf_event_context *ctx);
283static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200284
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200285void __weak perf_event_print_debug(void) { }
286
Matt Fleming84c79912010-10-03 21:41:13 +0100287extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200288{
Matt Fleming84c79912010-10-03 21:41:13 +0100289 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200290}
291
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200292static inline u64 perf_clock(void)
293{
294 return local_clock();
295}
296
Stephane Eraniane5d13672011-02-14 11:20:01 +0200297static inline struct perf_cpu_context *
298__get_cpu_context(struct perf_event_context *ctx)
299{
300 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
301}
302
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200303static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
304 struct perf_event_context *ctx)
305{
306 raw_spin_lock(&cpuctx->ctx.lock);
307 if (ctx)
308 raw_spin_lock(&ctx->lock);
309}
310
311static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
312 struct perf_event_context *ctx)
313{
314 if (ctx)
315 raw_spin_unlock(&ctx->lock);
316 raw_spin_unlock(&cpuctx->ctx.lock);
317}
318
Stephane Eraniane5d13672011-02-14 11:20:01 +0200319#ifdef CONFIG_CGROUP_PERF
320
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200321/*
Li Zefan877c6852013-03-05 11:38:08 +0800322 * perf_cgroup_info keeps track of time_enabled for a cgroup.
323 * This is a per-cpu dynamically allocated data structure.
324 */
325struct perf_cgroup_info {
326 u64 time;
327 u64 timestamp;
328};
329
330struct perf_cgroup {
331 struct cgroup_subsys_state css;
Namhyung Kim86e213e2013-03-18 18:56:34 +0900332 struct perf_cgroup_info __percpu *info;
Li Zefan877c6852013-03-05 11:38:08 +0800333};
334
335/*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200336 * Must ensure cgroup is pinned (css_get) before calling
337 * this function. In other words, we cannot call this function
338 * if there is no cgroup event for the current CPU context.
339 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200340static inline struct perf_cgroup *
341perf_cgroup_from_task(struct task_struct *task)
342{
343 return container_of(task_subsys_state(task, perf_subsys_id),
344 struct perf_cgroup, css);
345}
346
347static inline bool
348perf_cgroup_match(struct perf_event *event)
349{
350 struct perf_event_context *ctx = event->ctx;
351 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
352
Tejun Heoef824fa2013-04-08 19:00:38 -0700353 /* @event doesn't care about cgroup */
354 if (!event->cgrp)
355 return true;
356
357 /* wants specific cgroup scope but @cpuctx isn't associated with any */
358 if (!cpuctx->cgrp)
359 return false;
360
361 /*
362 * Cgroup scoping is recursive. An event enabled for a cgroup is
363 * also enabled for all its descendant cgroups. If @cpuctx's
364 * cgroup is a descendant of @event's (the test covers identity
365 * case), it's a match.
366 */
367 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
368 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200369}
370
Salman Qazi9c5da092012-06-14 15:31:09 -0700371static inline bool perf_tryget_cgroup(struct perf_event *event)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200372{
Salman Qazi9c5da092012-06-14 15:31:09 -0700373 return css_tryget(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200374}
375
376static inline void perf_put_cgroup(struct perf_event *event)
377{
378 css_put(&event->cgrp->css);
379}
380
381static inline void perf_detach_cgroup(struct perf_event *event)
382{
383 perf_put_cgroup(event);
384 event->cgrp = NULL;
385}
386
387static inline int is_cgroup_event(struct perf_event *event)
388{
389 return event->cgrp != NULL;
390}
391
392static inline u64 perf_cgroup_event_time(struct perf_event *event)
393{
394 struct perf_cgroup_info *t;
395
396 t = per_cpu_ptr(event->cgrp->info, event->cpu);
397 return t->time;
398}
399
400static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
401{
402 struct perf_cgroup_info *info;
403 u64 now;
404
405 now = perf_clock();
406
407 info = this_cpu_ptr(cgrp->info);
408
409 info->time += now - info->timestamp;
410 info->timestamp = now;
411}
412
413static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
414{
415 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
416 if (cgrp_out)
417 __update_cgrp_time(cgrp_out);
418}
419
420static inline void update_cgrp_time_from_event(struct perf_event *event)
421{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200422 struct perf_cgroup *cgrp;
423
Stephane Eraniane5d13672011-02-14 11:20:01 +0200424 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200425 * ensure we access cgroup data only when needed and
426 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200427 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200428 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200429 return;
430
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200431 cgrp = perf_cgroup_from_task(current);
432 /*
433 * Do not update time when cgroup is not active
434 */
435 if (cgrp == event->cgrp)
436 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200437}
438
439static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200440perf_cgroup_set_timestamp(struct task_struct *task,
441 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200442{
443 struct perf_cgroup *cgrp;
444 struct perf_cgroup_info *info;
445
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200446 /*
447 * ctx->lock held by caller
448 * ensure we do not access cgroup data
449 * unless we have the cgroup pinned (css_get)
450 */
451 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200452 return;
453
454 cgrp = perf_cgroup_from_task(task);
455 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200456 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200457}
458
459#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
460#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
461
462/*
463 * reschedule events based on the cgroup constraint of task.
464 *
465 * mode SWOUT : schedule out everything
466 * mode SWIN : schedule in based on cgroup for next
467 */
468void perf_cgroup_switch(struct task_struct *task, int mode)
469{
470 struct perf_cpu_context *cpuctx;
471 struct pmu *pmu;
472 unsigned long flags;
473
474 /*
475 * disable interrupts to avoid geting nr_cgroup
476 * changes via __perf_event_disable(). Also
477 * avoids preemption.
478 */
479 local_irq_save(flags);
480
481 /*
482 * we reschedule only in the presence of cgroup
483 * constrained events.
484 */
485 rcu_read_lock();
486
487 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200488 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200489 if (cpuctx->unique_pmu != pmu)
490 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200491
Stephane Eraniane5d13672011-02-14 11:20:01 +0200492 /*
493 * perf_cgroup_events says at least one
494 * context on this CPU has cgroup events.
495 *
496 * ctx->nr_cgroups reports the number of cgroup
497 * events for a context.
498 */
499 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200500 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
501 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200502
503 if (mode & PERF_CGROUP_SWOUT) {
504 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
505 /*
506 * must not be done before ctxswout due
507 * to event_filter_match() in event_sched_out()
508 */
509 cpuctx->cgrp = NULL;
510 }
511
512 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200513 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200514 /*
515 * set cgrp before ctxsw in to allow
516 * event_filter_match() to not have to pass
517 * task around
Stephane Eraniane5d13672011-02-14 11:20:01 +0200518 */
519 cpuctx->cgrp = perf_cgroup_from_task(task);
520 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
521 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200522 perf_pmu_enable(cpuctx->ctx.pmu);
523 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200524 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200525 }
526
527 rcu_read_unlock();
528
529 local_irq_restore(flags);
530}
531
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200532static inline void perf_cgroup_sched_out(struct task_struct *task,
533 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200534{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200535 struct perf_cgroup *cgrp1;
536 struct perf_cgroup *cgrp2 = NULL;
537
538 /*
539 * we come here when we know perf_cgroup_events > 0
540 */
541 cgrp1 = perf_cgroup_from_task(task);
542
543 /*
544 * next is NULL when called from perf_event_enable_on_exec()
545 * that will systematically cause a cgroup_switch()
546 */
547 if (next)
548 cgrp2 = perf_cgroup_from_task(next);
549
550 /*
551 * only schedule out current cgroup events if we know
552 * that we are switching to a different cgroup. Otherwise,
553 * do no touch the cgroup events.
554 */
555 if (cgrp1 != cgrp2)
556 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200557}
558
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200559static inline void perf_cgroup_sched_in(struct task_struct *prev,
560 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200561{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200562 struct perf_cgroup *cgrp1;
563 struct perf_cgroup *cgrp2 = NULL;
564
565 /*
566 * we come here when we know perf_cgroup_events > 0
567 */
568 cgrp1 = perf_cgroup_from_task(task);
569
570 /* prev can never be NULL */
571 cgrp2 = perf_cgroup_from_task(prev);
572
573 /*
574 * only need to schedule in cgroup events if we are changing
575 * cgroup during ctxsw. Cgroup events were not scheduled
576 * out of ctxsw out if that was not the case.
577 */
578 if (cgrp1 != cgrp2)
579 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200580}
581
582static inline int perf_cgroup_connect(int fd, struct perf_event *event,
583 struct perf_event_attr *attr,
584 struct perf_event *group_leader)
585{
586 struct perf_cgroup *cgrp;
587 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400588 struct fd f = fdget(fd);
589 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200590
Al Viro2903ff02012-08-28 12:52:22 -0400591 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200592 return -EBADF;
593
Al Viro2903ff02012-08-28 12:52:22 -0400594 css = cgroup_css_from_dir(f.file, perf_subsys_id);
Li Zefan3db272c2011-03-03 14:25:37 +0800595 if (IS_ERR(css)) {
596 ret = PTR_ERR(css);
597 goto out;
598 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200599
600 cgrp = container_of(css, struct perf_cgroup, css);
601 event->cgrp = cgrp;
602
Li Zefanf75e18c2011-03-03 14:25:50 +0800603 /* must be done before we fput() the file */
Salman Qazi9c5da092012-06-14 15:31:09 -0700604 if (!perf_tryget_cgroup(event)) {
605 event->cgrp = NULL;
606 ret = -ENOENT;
607 goto out;
608 }
Li Zefanf75e18c2011-03-03 14:25:50 +0800609
Stephane Eraniane5d13672011-02-14 11:20:01 +0200610 /*
611 * all events in a group must monitor
612 * the same cgroup because a task belongs
613 * to only one perf cgroup at a time
614 */
615 if (group_leader && group_leader->cgrp != cgrp) {
616 perf_detach_cgroup(event);
617 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200618 }
Li Zefan3db272c2011-03-03 14:25:37 +0800619out:
Al Viro2903ff02012-08-28 12:52:22 -0400620 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200621 return ret;
622}
623
624static inline void
625perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
626{
627 struct perf_cgroup_info *t;
628 t = per_cpu_ptr(event->cgrp->info, event->cpu);
629 event->shadow_ctx_time = now - t->timestamp;
630}
631
632static inline void
633perf_cgroup_defer_enabled(struct perf_event *event)
634{
635 /*
636 * when the current task's perf cgroup does not match
637 * the event's, we need to remember to call the
638 * perf_mark_enable() function the first time a task with
639 * a matching perf cgroup is scheduled in.
640 */
641 if (is_cgroup_event(event) && !perf_cgroup_match(event))
642 event->cgrp_defer_enabled = 1;
643}
644
645static inline void
646perf_cgroup_mark_enabled(struct perf_event *event,
647 struct perf_event_context *ctx)
648{
649 struct perf_event *sub;
650 u64 tstamp = perf_event_time(event);
651
652 if (!event->cgrp_defer_enabled)
653 return;
654
655 event->cgrp_defer_enabled = 0;
656
657 event->tstamp_enabled = tstamp - event->total_time_enabled;
658 list_for_each_entry(sub, &event->sibling_list, group_entry) {
659 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
660 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
661 sub->cgrp_defer_enabled = 0;
662 }
663 }
664}
665#else /* !CONFIG_CGROUP_PERF */
666
667static inline bool
668perf_cgroup_match(struct perf_event *event)
669{
670 return true;
671}
672
673static inline void perf_detach_cgroup(struct perf_event *event)
674{}
675
676static inline int is_cgroup_event(struct perf_event *event)
677{
678 return 0;
679}
680
681static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
682{
683 return 0;
684}
685
686static inline void update_cgrp_time_from_event(struct perf_event *event)
687{
688}
689
690static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
691{
692}
693
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200694static inline void perf_cgroup_sched_out(struct task_struct *task,
695 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200696{
697}
698
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200699static inline void perf_cgroup_sched_in(struct task_struct *prev,
700 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200701{
702}
703
704static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
705 struct perf_event_attr *attr,
706 struct perf_event *group_leader)
707{
708 return -EINVAL;
709}
710
711static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200712perf_cgroup_set_timestamp(struct task_struct *task,
713 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200714{
715}
716
717void
718perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
719{
720}
721
722static inline void
723perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
724{
725}
726
727static inline u64 perf_cgroup_event_time(struct perf_event *event)
728{
729 return 0;
730}
731
732static inline void
733perf_cgroup_defer_enabled(struct perf_event *event)
734{
735}
736
737static inline void
738perf_cgroup_mark_enabled(struct perf_event *event,
739 struct perf_event_context *ctx)
740{
741}
742#endif
743
Stephane Eranian9e630202013-04-03 14:21:33 +0200744/*
745 * set default to be dependent on timer tick just
746 * like original code
747 */
748#define PERF_CPU_HRTIMER (1000 / HZ)
749/*
750 * function must be called with interrupts disbled
751 */
752static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
753{
754 struct perf_cpu_context *cpuctx;
755 enum hrtimer_restart ret = HRTIMER_NORESTART;
756 int rotations = 0;
757
758 WARN_ON(!irqs_disabled());
759
760 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
761
762 rotations = perf_rotate_context(cpuctx);
763
764 /*
765 * arm timer if needed
766 */
767 if (rotations) {
768 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
769 ret = HRTIMER_RESTART;
770 }
771
772 return ret;
773}
774
775/* CPU is going down */
776void perf_cpu_hrtimer_cancel(int cpu)
777{
778 struct perf_cpu_context *cpuctx;
779 struct pmu *pmu;
780 unsigned long flags;
781
782 if (WARN_ON(cpu != smp_processor_id()))
783 return;
784
785 local_irq_save(flags);
786
787 rcu_read_lock();
788
789 list_for_each_entry_rcu(pmu, &pmus, entry) {
790 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
791
792 if (pmu->task_ctx_nr == perf_sw_context)
793 continue;
794
795 hrtimer_cancel(&cpuctx->hrtimer);
796 }
797
798 rcu_read_unlock();
799
800 local_irq_restore(flags);
801}
802
803static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
804{
805 struct hrtimer *hr = &cpuctx->hrtimer;
806 struct pmu *pmu = cpuctx->ctx.pmu;
Stephane Eranian62b85632013-04-03 14:21:34 +0200807 int timer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200808
809 /* no multiplexing needed for SW PMU */
810 if (pmu->task_ctx_nr == perf_sw_context)
811 return;
812
Stephane Eranian62b85632013-04-03 14:21:34 +0200813 /*
814 * check default is sane, if not set then force to
815 * default interval (1/tick)
816 */
817 timer = pmu->hrtimer_interval_ms;
818 if (timer < 1)
819 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
820
821 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200822
823 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
824 hr->function = perf_cpu_hrtimer_handler;
825}
826
827static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
828{
829 struct hrtimer *hr = &cpuctx->hrtimer;
830 struct pmu *pmu = cpuctx->ctx.pmu;
831
832 /* not for SW PMU */
833 if (pmu->task_ctx_nr == perf_sw_context)
834 return;
835
836 if (hrtimer_active(hr))
837 return;
838
839 if (!hrtimer_callback_running(hr))
840 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
841 0, HRTIMER_MODE_REL_PINNED, 0);
842}
843
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200844void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200845{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200846 int *count = this_cpu_ptr(pmu->pmu_disable_count);
847 if (!(*count)++)
848 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200849}
850
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200851void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200852{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200853 int *count = this_cpu_ptr(pmu->pmu_disable_count);
854 if (!--(*count))
855 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200856}
857
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200858static DEFINE_PER_CPU(struct list_head, rotation_list);
859
860/*
861 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
862 * because they're strictly cpu affine and rotate_start is called with IRQs
863 * disabled, while rotate_context is called from IRQ context.
864 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200865static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200866{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200867 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200868 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200869
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200870 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200871
Frederic Weisbecker12351ef2013-04-20 15:48:22 +0200872 if (list_empty(&cpuctx->rotation_list)) {
873 int was_empty = list_empty(head);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200874 list_add(&cpuctx->rotation_list, head);
Frederic Weisbecker12351ef2013-04-20 15:48:22 +0200875 if (was_empty)
876 tick_nohz_full_kick();
877 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200878}
879
880static void get_ctx(struct perf_event_context *ctx)
881{
882 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
883}
884
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200885static void put_ctx(struct perf_event_context *ctx)
886{
887 if (atomic_dec_and_test(&ctx->refcount)) {
888 if (ctx->parent_ctx)
889 put_ctx(ctx->parent_ctx);
890 if (ctx->task)
891 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800892 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200893 }
894}
895
896static void unclone_ctx(struct perf_event_context *ctx)
897{
898 if (ctx->parent_ctx) {
899 put_ctx(ctx->parent_ctx);
900 ctx->parent_ctx = NULL;
901 }
902}
903
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200904static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
905{
906 /*
907 * only top level events have the pid namespace they were created in
908 */
909 if (event->parent)
910 event = event->parent;
911
912 return task_tgid_nr_ns(p, event->ns);
913}
914
915static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
916{
917 /*
918 * only top level events have the pid namespace they were created in
919 */
920 if (event->parent)
921 event = event->parent;
922
923 return task_pid_nr_ns(p, event->ns);
924}
925
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200926/*
927 * If we inherit events we want to return the parent event id
928 * to userspace.
929 */
930static u64 primary_event_id(struct perf_event *event)
931{
932 u64 id = event->id;
933
934 if (event->parent)
935 id = event->parent->id;
936
937 return id;
938}
939
940/*
941 * Get the perf_event_context for a task and lock it.
942 * This has to cope with with the fact that until it is locked,
943 * the context could get moved to another task.
944 */
945static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200946perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200947{
948 struct perf_event_context *ctx;
949
950 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200951retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200952 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200953 if (ctx) {
954 /*
955 * If this context is a clone of another, it might
956 * get swapped for another underneath us by
957 * perf_event_task_sched_out, though the
958 * rcu_read_lock() protects us from any context
959 * getting freed. Lock the context and check if it
960 * got swapped before we could get the lock, and retry
961 * if so. If we locked the right context, then it
962 * can't get swapped on us any more.
963 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100964 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200965 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100966 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200967 goto retry;
968 }
969
970 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100971 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200972 ctx = NULL;
973 }
974 }
975 rcu_read_unlock();
976 return ctx;
977}
978
979/*
980 * Get the context for a task and increment its pin_count so it
981 * can't get swapped to another task. This also increments its
982 * reference count so that the context can't get freed.
983 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200984static struct perf_event_context *
985perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200986{
987 struct perf_event_context *ctx;
988 unsigned long flags;
989
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200990 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200991 if (ctx) {
992 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100993 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200994 }
995 return ctx;
996}
997
998static void perf_unpin_context(struct perf_event_context *ctx)
999{
1000 unsigned long flags;
1001
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001002 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001003 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001004 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001005}
1006
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001007/*
1008 * Update the record of the current time in a context.
1009 */
1010static void update_context_time(struct perf_event_context *ctx)
1011{
1012 u64 now = perf_clock();
1013
1014 ctx->time += now - ctx->timestamp;
1015 ctx->timestamp = now;
1016}
1017
Stephane Eranian41587552011-01-03 18:20:01 +02001018static u64 perf_event_time(struct perf_event *event)
1019{
1020 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001021
1022 if (is_cgroup_event(event))
1023 return perf_cgroup_event_time(event);
1024
Stephane Eranian41587552011-01-03 18:20:01 +02001025 return ctx ? ctx->time : 0;
1026}
1027
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001028/*
1029 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001030 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001031 */
1032static void update_event_times(struct perf_event *event)
1033{
1034 struct perf_event_context *ctx = event->ctx;
1035 u64 run_end;
1036
1037 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1038 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1039 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001040 /*
1041 * in cgroup mode, time_enabled represents
1042 * the time the event was enabled AND active
1043 * tasks were in the monitored cgroup. This is
1044 * independent of the activity of the context as
1045 * there may be a mix of cgroup and non-cgroup events.
1046 *
1047 * That is why we treat cgroup events differently
1048 * here.
1049 */
1050 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001051 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001052 else if (ctx->is_active)
1053 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001054 else
1055 run_end = event->tstamp_stopped;
1056
1057 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001058
1059 if (event->state == PERF_EVENT_STATE_INACTIVE)
1060 run_end = event->tstamp_stopped;
1061 else
Stephane Eranian41587552011-01-03 18:20:01 +02001062 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001063
1064 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001065
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001066}
1067
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001068/*
1069 * Update total_time_enabled and total_time_running for all events in a group.
1070 */
1071static void update_group_times(struct perf_event *leader)
1072{
1073 struct perf_event *event;
1074
1075 update_event_times(leader);
1076 list_for_each_entry(event, &leader->sibling_list, group_entry)
1077 update_event_times(event);
1078}
1079
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001080static struct list_head *
1081ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1082{
1083 if (event->attr.pinned)
1084 return &ctx->pinned_groups;
1085 else
1086 return &ctx->flexible_groups;
1087}
1088
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001089/*
1090 * Add a event from the lists for its context.
1091 * Must be called with ctx->mutex and ctx->lock held.
1092 */
1093static void
1094list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1095{
Peter Zijlstra8a495422010-05-27 15:47:49 +02001096 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1097 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001098
1099 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001100 * If we're a stand alone event or group leader, we go to the context
1101 * list, group events are kept attached to the group so that
1102 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001103 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001104 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001105 struct list_head *list;
1106
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001107 if (is_software_event(event))
1108 event->group_flags |= PERF_GROUP_SOFTWARE;
1109
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001110 list = ctx_group_list(event, ctx);
1111 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001112 }
1113
Peter Zijlstra08309372011-03-03 11:31:20 +01001114 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001115 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001116
Stephane Eraniand010b332012-02-09 23:21:00 +01001117 if (has_branch_stack(event))
1118 ctx->nr_branch_stack++;
1119
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001120 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001121 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001122 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001123 ctx->nr_events++;
1124 if (event->attr.inherit_stat)
1125 ctx->nr_stat++;
1126}
1127
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001128/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001129 * Initialize event state based on the perf_event_attr::disabled.
1130 */
1131static inline void perf_event__state_init(struct perf_event *event)
1132{
1133 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1134 PERF_EVENT_STATE_INACTIVE;
1135}
1136
1137/*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001138 * Called at perf_event creation and when events are attached/detached from a
1139 * group.
1140 */
1141static void perf_event__read_size(struct perf_event *event)
1142{
1143 int entry = sizeof(u64); /* value */
1144 int size = 0;
1145 int nr = 1;
1146
1147 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1148 size += sizeof(u64);
1149
1150 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1151 size += sizeof(u64);
1152
1153 if (event->attr.read_format & PERF_FORMAT_ID)
1154 entry += sizeof(u64);
1155
1156 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1157 nr += event->group_leader->nr_siblings;
1158 size += sizeof(u64);
1159 }
1160
1161 size += entry * nr;
1162 event->read_size = size;
1163}
1164
1165static void perf_event__header_size(struct perf_event *event)
1166{
1167 struct perf_sample_data *data;
1168 u64 sample_type = event->attr.sample_type;
1169 u16 size = 0;
1170
1171 perf_event__read_size(event);
1172
1173 if (sample_type & PERF_SAMPLE_IP)
1174 size += sizeof(data->ip);
1175
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001176 if (sample_type & PERF_SAMPLE_ADDR)
1177 size += sizeof(data->addr);
1178
1179 if (sample_type & PERF_SAMPLE_PERIOD)
1180 size += sizeof(data->period);
1181
Andi Kleenc3feedf2013-01-24 16:10:28 +01001182 if (sample_type & PERF_SAMPLE_WEIGHT)
1183 size += sizeof(data->weight);
1184
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001185 if (sample_type & PERF_SAMPLE_READ)
1186 size += event->read_size;
1187
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001188 if (sample_type & PERF_SAMPLE_DATA_SRC)
1189 size += sizeof(data->data_src.val);
1190
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001191 event->header_size = size;
1192}
1193
1194static void perf_event__id_header_size(struct perf_event *event)
1195{
1196 struct perf_sample_data *data;
1197 u64 sample_type = event->attr.sample_type;
1198 u16 size = 0;
1199
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001200 if (sample_type & PERF_SAMPLE_TID)
1201 size += sizeof(data->tid_entry);
1202
1203 if (sample_type & PERF_SAMPLE_TIME)
1204 size += sizeof(data->time);
1205
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001206 if (sample_type & PERF_SAMPLE_ID)
1207 size += sizeof(data->id);
1208
1209 if (sample_type & PERF_SAMPLE_STREAM_ID)
1210 size += sizeof(data->stream_id);
1211
1212 if (sample_type & PERF_SAMPLE_CPU)
1213 size += sizeof(data->cpu_entry);
1214
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001215 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001216}
1217
Peter Zijlstra8a495422010-05-27 15:47:49 +02001218static void perf_group_attach(struct perf_event *event)
1219{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001220 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001221
Peter Zijlstra74c33372010-10-15 11:40:29 +02001222 /*
1223 * We can have double attach due to group movement in perf_event_open.
1224 */
1225 if (event->attach_state & PERF_ATTACH_GROUP)
1226 return;
1227
Peter Zijlstra8a495422010-05-27 15:47:49 +02001228 event->attach_state |= PERF_ATTACH_GROUP;
1229
1230 if (group_leader == event)
1231 return;
1232
1233 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1234 !is_software_event(event))
1235 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1236
1237 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1238 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001239
1240 perf_event__header_size(group_leader);
1241
1242 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1243 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001244}
1245
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001246/*
1247 * Remove a event from the lists for its context.
1248 * Must be called with ctx->mutex and ctx->lock held.
1249 */
1250static void
1251list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1252{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001253 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001254 /*
1255 * We can have double detach due to exit/hot-unplug + close.
1256 */
1257 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001258 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001259
1260 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1261
Stephane Eranian68cacd22011-03-23 16:03:06 +01001262 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001263 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001264 cpuctx = __get_cpu_context(ctx);
1265 /*
1266 * if there are no more cgroup events
1267 * then cler cgrp to avoid stale pointer
1268 * in update_cgrp_time_from_cpuctx()
1269 */
1270 if (!ctx->nr_cgroups)
1271 cpuctx->cgrp = NULL;
1272 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001273
Stephane Eraniand010b332012-02-09 23:21:00 +01001274 if (has_branch_stack(event))
1275 ctx->nr_branch_stack--;
1276
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001277 ctx->nr_events--;
1278 if (event->attr.inherit_stat)
1279 ctx->nr_stat--;
1280
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001281 list_del_rcu(&event->event_entry);
1282
Peter Zijlstra8a495422010-05-27 15:47:49 +02001283 if (event->group_leader == event)
1284 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001285
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001286 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001287
1288 /*
1289 * If event was in error state, then keep it
1290 * that way, otherwise bogus counts will be
1291 * returned on read(). The only way to get out
1292 * of error state is by explicit re-enabling
1293 * of the event
1294 */
1295 if (event->state > PERF_EVENT_STATE_OFF)
1296 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001297}
1298
Peter Zijlstra8a495422010-05-27 15:47:49 +02001299static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001300{
1301 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001302 struct list_head *list = NULL;
1303
1304 /*
1305 * We can have double detach due to exit/hot-unplug + close.
1306 */
1307 if (!(event->attach_state & PERF_ATTACH_GROUP))
1308 return;
1309
1310 event->attach_state &= ~PERF_ATTACH_GROUP;
1311
1312 /*
1313 * If this is a sibling, remove it from its group.
1314 */
1315 if (event->group_leader != event) {
1316 list_del_init(&event->group_entry);
1317 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001318 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001319 }
1320
1321 if (!list_empty(&event->group_entry))
1322 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001323
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001324 /*
1325 * If this was a group event with sibling events then
1326 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001327 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001328 */
1329 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001330 if (list)
1331 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001332 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001333
1334 /* Inherit group flags from the previous leader */
1335 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001336 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001337
1338out:
1339 perf_event__header_size(event->group_leader);
1340
1341 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1342 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001343}
1344
Stephane Eranianfa66f072010-08-26 16:40:01 +02001345static inline int
1346event_filter_match(struct perf_event *event)
1347{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001348 return (event->cpu == -1 || event->cpu == smp_processor_id())
1349 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001350}
1351
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001352static void
1353event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001354 struct perf_cpu_context *cpuctx,
1355 struct perf_event_context *ctx)
1356{
Stephane Eranian41587552011-01-03 18:20:01 +02001357 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001358 u64 delta;
1359 /*
1360 * An event which could not be activated because of
1361 * filter mismatch still needs to have its timings
1362 * maintained, otherwise bogus information is return
1363 * via read() for time_enabled, time_running:
1364 */
1365 if (event->state == PERF_EVENT_STATE_INACTIVE
1366 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001367 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001368 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001369 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001370 }
1371
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001372 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001373 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001374
1375 event->state = PERF_EVENT_STATE_INACTIVE;
1376 if (event->pending_disable) {
1377 event->pending_disable = 0;
1378 event->state = PERF_EVENT_STATE_OFF;
1379 }
Stephane Eranian41587552011-01-03 18:20:01 +02001380 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001381 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001382 event->oncpu = -1;
1383
1384 if (!is_software_event(event))
1385 cpuctx->active_oncpu--;
1386 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001387 if (event->attr.freq && event->attr.sample_freq)
1388 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001389 if (event->attr.exclusive || !cpuctx->active_oncpu)
1390 cpuctx->exclusive = 0;
1391}
1392
1393static void
1394group_sched_out(struct perf_event *group_event,
1395 struct perf_cpu_context *cpuctx,
1396 struct perf_event_context *ctx)
1397{
1398 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001399 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001400
1401 event_sched_out(group_event, cpuctx, ctx);
1402
1403 /*
1404 * Schedule out siblings (if any):
1405 */
1406 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1407 event_sched_out(event, cpuctx, ctx);
1408
Stephane Eranianfa66f072010-08-26 16:40:01 +02001409 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001410 cpuctx->exclusive = 0;
1411}
1412
1413/*
1414 * Cross CPU call to remove a performance event
1415 *
1416 * We disable the event on the hardware level first. After that we
1417 * remove it from the context list.
1418 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001419static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001420{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001421 struct perf_event *event = info;
1422 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001423 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001424
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001425 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001426 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001427 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001428 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1429 ctx->is_active = 0;
1430 cpuctx->task_ctx = NULL;
1431 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001432 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001433
1434 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001435}
1436
1437
1438/*
1439 * Remove the event from a task's (or a CPU's) list of events.
1440 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001441 * CPU events are removed with a smp call. For task events we only
1442 * call when the task is on a CPU.
1443 *
1444 * If event->ctx is a cloned context, callers must make sure that
1445 * every task struct that event->ctx->task could possibly point to
1446 * remains valid. This is OK when called from perf_release since
1447 * that only calls us on the top-level context, which can't be a clone.
1448 * When called from perf_event_exit_task, it's OK because the
1449 * context has been detached from its task.
1450 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001451static void perf_remove_from_context(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001452{
1453 struct perf_event_context *ctx = event->ctx;
1454 struct task_struct *task = ctx->task;
1455
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001456 lockdep_assert_held(&ctx->mutex);
1457
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001458 if (!task) {
1459 /*
1460 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001461 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001462 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001463 cpu_function_call(event->cpu, __perf_remove_from_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001464 return;
1465 }
1466
1467retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001468 if (!task_function_call(task, __perf_remove_from_context, event))
1469 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001470
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001471 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001472 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001473 * If we failed to find a running task, but find the context active now
1474 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001475 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001476 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001477 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001478 goto retry;
1479 }
1480
1481 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001482 * Since the task isn't running, its safe to remove the event, us
1483 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001484 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001485 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001486 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001487}
1488
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001489/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001490 * Cross CPU call to disable a performance event
1491 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301492int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001493{
1494 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001495 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001496 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001497
1498 /*
1499 * If this is a per-task event, need to check whether this
1500 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001501 *
1502 * Can trigger due to concurrent perf_event_context_sched_out()
1503 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001504 */
1505 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001506 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001507
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001508 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001509
1510 /*
1511 * If the event is on, turn it off.
1512 * If it is in error state, leave it in error state.
1513 */
1514 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1515 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001516 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001517 update_group_times(event);
1518 if (event == event->group_leader)
1519 group_sched_out(event, cpuctx, ctx);
1520 else
1521 event_sched_out(event, cpuctx, ctx);
1522 event->state = PERF_EVENT_STATE_OFF;
1523 }
1524
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001525 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001526
1527 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001528}
1529
1530/*
1531 * Disable a event.
1532 *
1533 * If event->ctx is a cloned context, callers must make sure that
1534 * every task struct that event->ctx->task could possibly point to
1535 * remains valid. This condition is satisifed when called through
1536 * perf_event_for_each_child or perf_event_for_each because they
1537 * hold the top-level event's child_mutex, so any descendant that
1538 * goes to exit will block in sync_child_event.
1539 * When called from perf_pending_event it's OK because event->ctx
1540 * is the current context on this CPU and preemption is disabled,
1541 * hence we can't get into perf_event_task_sched_out for this context.
1542 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001543void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001544{
1545 struct perf_event_context *ctx = event->ctx;
1546 struct task_struct *task = ctx->task;
1547
1548 if (!task) {
1549 /*
1550 * Disable the event on the cpu that it's on
1551 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001552 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001553 return;
1554 }
1555
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001556retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001557 if (!task_function_call(task, __perf_event_disable, event))
1558 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001559
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001560 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001561 /*
1562 * If the event is still active, we need to retry the cross-call.
1563 */
1564 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001565 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001566 /*
1567 * Reload the task pointer, it might have been changed by
1568 * a concurrent perf_event_context_sched_out().
1569 */
1570 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001571 goto retry;
1572 }
1573
1574 /*
1575 * Since we have the lock this context can't be scheduled
1576 * in, so we can change the state safely.
1577 */
1578 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1579 update_group_times(event);
1580 event->state = PERF_EVENT_STATE_OFF;
1581 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001582 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001583}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001584EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001585
Stephane Eraniane5d13672011-02-14 11:20:01 +02001586static void perf_set_shadow_time(struct perf_event *event,
1587 struct perf_event_context *ctx,
1588 u64 tstamp)
1589{
1590 /*
1591 * use the correct time source for the time snapshot
1592 *
1593 * We could get by without this by leveraging the
1594 * fact that to get to this function, the caller
1595 * has most likely already called update_context_time()
1596 * and update_cgrp_time_xx() and thus both timestamp
1597 * are identical (or very close). Given that tstamp is,
1598 * already adjusted for cgroup, we could say that:
1599 * tstamp - ctx->timestamp
1600 * is equivalent to
1601 * tstamp - cgrp->timestamp.
1602 *
1603 * Then, in perf_output_read(), the calculation would
1604 * work with no changes because:
1605 * - event is guaranteed scheduled in
1606 * - no scheduled out in between
1607 * - thus the timestamp would be the same
1608 *
1609 * But this is a bit hairy.
1610 *
1611 * So instead, we have an explicit cgroup call to remain
1612 * within the time time source all along. We believe it
1613 * is cleaner and simpler to understand.
1614 */
1615 if (is_cgroup_event(event))
1616 perf_cgroup_set_shadow_time(event, tstamp);
1617 else
1618 event->shadow_ctx_time = tstamp - ctx->timestamp;
1619}
1620
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001621#define MAX_INTERRUPTS (~0ULL)
1622
1623static void perf_log_throttle(struct perf_event *event, int enable);
1624
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001625static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001626event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001627 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001628 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001629{
Stephane Eranian41587552011-01-03 18:20:01 +02001630 u64 tstamp = perf_event_time(event);
1631
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001632 if (event->state <= PERF_EVENT_STATE_OFF)
1633 return 0;
1634
1635 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001636 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001637
1638 /*
1639 * Unthrottle events, since we scheduled we might have missed several
1640 * ticks already, also for a heavily scheduling task there is little
1641 * guarantee it'll get a tick in a timely manner.
1642 */
1643 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1644 perf_log_throttle(event, 1);
1645 event->hw.interrupts = 0;
1646 }
1647
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001648 /*
1649 * The new state must be visible before we turn it on in the hardware:
1650 */
1651 smp_wmb();
1652
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001653 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001654 event->state = PERF_EVENT_STATE_INACTIVE;
1655 event->oncpu = -1;
1656 return -EAGAIN;
1657 }
1658
Stephane Eranian41587552011-01-03 18:20:01 +02001659 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001660
Stephane Eraniane5d13672011-02-14 11:20:01 +02001661 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001662
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001663 if (!is_software_event(event))
1664 cpuctx->active_oncpu++;
1665 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001666 if (event->attr.freq && event->attr.sample_freq)
1667 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001668
1669 if (event->attr.exclusive)
1670 cpuctx->exclusive = 1;
1671
1672 return 0;
1673}
1674
1675static int
1676group_sched_in(struct perf_event *group_event,
1677 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001678 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001679{
Lin Ming6bde9b62010-04-23 13:56:00 +08001680 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001681 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001682 u64 now = ctx->time;
1683 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001684
1685 if (group_event->state == PERF_EVENT_STATE_OFF)
1686 return 0;
1687
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001688 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001689
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001690 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001691 pmu->cancel_txn(pmu);
Stephane Eranian9e630202013-04-03 14:21:33 +02001692 perf_cpu_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001693 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001694 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001695
1696 /*
1697 * Schedule in siblings as one group (if any):
1698 */
1699 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001700 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001701 partial_group = event;
1702 goto group_error;
1703 }
1704 }
1705
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001706 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001707 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001708
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001709group_error:
1710 /*
1711 * Groups can be scheduled in as one unit only, so undo any
1712 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001713 * The events up to the failed event are scheduled out normally,
1714 * tstamp_stopped will be updated.
1715 *
1716 * The failed events and the remaining siblings need to have
1717 * their timings updated as if they had gone thru event_sched_in()
1718 * and event_sched_out(). This is required to get consistent timings
1719 * across the group. This also takes care of the case where the group
1720 * could never be scheduled by ensuring tstamp_stopped is set to mark
1721 * the time the event was actually stopped, such that time delta
1722 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001723 */
1724 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1725 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001726 simulate = true;
1727
1728 if (simulate) {
1729 event->tstamp_running += now - event->tstamp_stopped;
1730 event->tstamp_stopped = now;
1731 } else {
1732 event_sched_out(event, cpuctx, ctx);
1733 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001734 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001735 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001736
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001737 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001738
Stephane Eranian9e630202013-04-03 14:21:33 +02001739 perf_cpu_hrtimer_restart(cpuctx);
1740
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001741 return -EAGAIN;
1742}
1743
1744/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001745 * Work out whether we can put this event group on the CPU now.
1746 */
1747static int group_can_go_on(struct perf_event *event,
1748 struct perf_cpu_context *cpuctx,
1749 int can_add_hw)
1750{
1751 /*
1752 * Groups consisting entirely of software events can always go on.
1753 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001754 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001755 return 1;
1756 /*
1757 * If an exclusive group is already on, no other hardware
1758 * events can go on.
1759 */
1760 if (cpuctx->exclusive)
1761 return 0;
1762 /*
1763 * If this group is exclusive and there are already
1764 * events on the CPU, it can't go on.
1765 */
1766 if (event->attr.exclusive && cpuctx->active_oncpu)
1767 return 0;
1768 /*
1769 * Otherwise, try to add it if all previous groups were able
1770 * to go on.
1771 */
1772 return can_add_hw;
1773}
1774
1775static void add_event_to_ctx(struct perf_event *event,
1776 struct perf_event_context *ctx)
1777{
Stephane Eranian41587552011-01-03 18:20:01 +02001778 u64 tstamp = perf_event_time(event);
1779
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001780 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001781 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001782 event->tstamp_enabled = tstamp;
1783 event->tstamp_running = tstamp;
1784 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001785}
1786
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001787static void task_ctx_sched_out(struct perf_event_context *ctx);
1788static void
1789ctx_sched_in(struct perf_event_context *ctx,
1790 struct perf_cpu_context *cpuctx,
1791 enum event_type_t event_type,
1792 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001793
Peter Zijlstradce58552011-04-09 21:17:46 +02001794static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1795 struct perf_event_context *ctx,
1796 struct task_struct *task)
1797{
1798 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1799 if (ctx)
1800 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1801 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1802 if (ctx)
1803 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1804}
1805
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001806/*
1807 * Cross CPU call to install and enable a performance event
1808 *
1809 * Must be called with ctx->mutex held
1810 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001811static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001812{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001813 struct perf_event *event = info;
1814 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001815 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001816 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1817 struct task_struct *task = current;
1818
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001819 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001820 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001821
1822 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001823 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001824 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001825 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001826 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001827
1828 /*
1829 * If the context we're installing events in is not the
1830 * active task_ctx, flip them.
1831 */
1832 if (ctx->task && task_ctx != ctx) {
1833 if (task_ctx)
1834 raw_spin_unlock(&task_ctx->lock);
1835 raw_spin_lock(&ctx->lock);
1836 task_ctx = ctx;
1837 }
1838
1839 if (task_ctx) {
1840 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001841 task = task_ctx->task;
1842 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001843
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001844 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001845
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001846 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001847 /*
1848 * update cgrp time only if current cgrp
1849 * matches event->cgrp. Must be done before
1850 * calling add_event_to_ctx()
1851 */
1852 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001853
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001854 add_event_to_ctx(event, ctx);
1855
1856 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001857 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001858 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001859 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001860
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001861 perf_pmu_enable(cpuctx->ctx.pmu);
1862 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001863
1864 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001865}
1866
1867/*
1868 * Attach a performance event to a context
1869 *
1870 * First we add the event to the list with the hardware enable bit
1871 * in event->hw_config cleared.
1872 *
1873 * If the event is attached to a task which is on a CPU we use a smp
1874 * call to enable it in the task context. The task might have been
1875 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001876 */
1877static void
1878perf_install_in_context(struct perf_event_context *ctx,
1879 struct perf_event *event,
1880 int cpu)
1881{
1882 struct task_struct *task = ctx->task;
1883
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001884 lockdep_assert_held(&ctx->mutex);
1885
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001886 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08001887 if (event->cpu != -1)
1888 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001889
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001890 if (!task) {
1891 /*
1892 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001893 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001894 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001895 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001896 return;
1897 }
1898
1899retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001900 if (!task_function_call(task, __perf_install_in_context, event))
1901 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001902
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001903 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001904 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001905 * If we failed to find a running task, but find the context active now
1906 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001907 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001908 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001909 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001910 goto retry;
1911 }
1912
1913 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001914 * Since the task isn't running, its safe to add the event, us holding
1915 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001916 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001917 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001918 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001919}
1920
1921/*
1922 * Put a event into inactive state and update time fields.
1923 * Enabling the leader of a group effectively enables all
1924 * the group members that aren't explicitly disabled, so we
1925 * have to update their ->tstamp_enabled also.
1926 * Note: this works for group members as well as group leaders
1927 * since the non-leader members' sibling_lists will be empty.
1928 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001929static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001930{
1931 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001932 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001933
1934 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001935 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001936 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001937 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1938 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001939 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001940}
1941
1942/*
1943 * Cross CPU call to enable a performance event
1944 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001945static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001946{
1947 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001948 struct perf_event_context *ctx = event->ctx;
1949 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001950 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001951 int err;
1952
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001953 if (WARN_ON_ONCE(!ctx->is_active))
1954 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001955
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001956 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001957 update_context_time(ctx);
1958
1959 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1960 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001961
1962 /*
1963 * set current task's cgroup time reference point
1964 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001965 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001966
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001967 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001968
Stephane Eraniane5d13672011-02-14 11:20:01 +02001969 if (!event_filter_match(event)) {
1970 if (is_cgroup_event(event))
1971 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001972 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001973 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001974
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001975 /*
1976 * If the event is in a group and isn't the group leader,
1977 * then don't put it on unless the group is on.
1978 */
1979 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1980 goto unlock;
1981
1982 if (!group_can_go_on(event, cpuctx, 1)) {
1983 err = -EEXIST;
1984 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001985 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001986 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001987 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001988 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001989 }
1990
1991 if (err) {
1992 /*
1993 * If this event can't go on and it's part of a
1994 * group, then the whole group has to come off.
1995 */
Stephane Eranian9e630202013-04-03 14:21:33 +02001996 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001997 group_sched_out(leader, cpuctx, ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02001998 perf_cpu_hrtimer_restart(cpuctx);
1999 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002000 if (leader->attr.pinned) {
2001 update_group_times(leader);
2002 leader->state = PERF_EVENT_STATE_ERROR;
2003 }
2004 }
2005
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002006unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002007 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002008
2009 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002010}
2011
2012/*
2013 * Enable a event.
2014 *
2015 * If event->ctx is a cloned context, callers must make sure that
2016 * every task struct that event->ctx->task could possibly point to
2017 * remains valid. This condition is satisfied when called through
2018 * perf_event_for_each_child or perf_event_for_each as described
2019 * for perf_event_disable.
2020 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01002021void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002022{
2023 struct perf_event_context *ctx = event->ctx;
2024 struct task_struct *task = ctx->task;
2025
2026 if (!task) {
2027 /*
2028 * Enable the event on the cpu that it's on
2029 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002030 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002031 return;
2032 }
2033
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002034 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002035 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2036 goto out;
2037
2038 /*
2039 * If the event is in error state, clear that first.
2040 * That way, if we see the event in error state below, we
2041 * know that it has gone back into error state, as distinct
2042 * from the task having been scheduled away before the
2043 * cross-call arrived.
2044 */
2045 if (event->state == PERF_EVENT_STATE_ERROR)
2046 event->state = PERF_EVENT_STATE_OFF;
2047
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002048retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002049 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002050 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002051 goto out;
2052 }
2053
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002054 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002055
2056 if (!task_function_call(task, __perf_event_enable, event))
2057 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002058
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002059 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002060
2061 /*
2062 * If the context is active and the event is still off,
2063 * we need to retry the cross-call.
2064 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002065 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2066 /*
2067 * task could have been flipped by a concurrent
2068 * perf_event_context_sched_out()
2069 */
2070 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002071 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002072 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002073
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002074out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002075 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002076}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002077EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002078
Avi Kivity26ca5c12011-06-29 18:42:37 +03002079int perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002080{
2081 /*
2082 * not supported on inherited events
2083 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002084 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002085 return -EINVAL;
2086
2087 atomic_add(refresh, &event->event_limit);
2088 perf_event_enable(event);
2089
2090 return 0;
2091}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002092EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002093
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002094static void ctx_sched_out(struct perf_event_context *ctx,
2095 struct perf_cpu_context *cpuctx,
2096 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002097{
2098 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002099 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002100
Peter Zijlstradb24d332011-04-09 21:17:45 +02002101 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002102 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002103 return;
2104
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002105 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002106 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002107 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002108 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002109
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002110 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002111 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002112 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2113 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002114 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002115
Peter Zijlstradb24d332011-04-09 21:17:45 +02002116 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002117 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002118 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002119 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002120 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002121}
2122
2123/*
2124 * Test whether two contexts are equivalent, i.e. whether they
2125 * have both been cloned from the same version of the same context
2126 * and they both have the same number of enabled events.
2127 * If the number of enabled events is the same, then the set
2128 * of enabled events should be the same, because these are both
2129 * inherited contexts, therefore we can't access individual events
2130 * in them directly with an fd; we can only enable/disable all
2131 * events via prctl, or enable/disable all events in a family
2132 * via ioctl, which will have the same effect on both contexts.
2133 */
2134static int context_equiv(struct perf_event_context *ctx1,
2135 struct perf_event_context *ctx2)
2136{
2137 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
2138 && ctx1->parent_gen == ctx2->parent_gen
2139 && !ctx1->pin_count && !ctx2->pin_count;
2140}
2141
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002142static void __perf_event_sync_stat(struct perf_event *event,
2143 struct perf_event *next_event)
2144{
2145 u64 value;
2146
2147 if (!event->attr.inherit_stat)
2148 return;
2149
2150 /*
2151 * Update the event value, we cannot use perf_event_read()
2152 * because we're in the middle of a context switch and have IRQs
2153 * disabled, which upsets smp_call_function_single(), however
2154 * we know the event must be on the current CPU, therefore we
2155 * don't need to use it.
2156 */
2157 switch (event->state) {
2158 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002159 event->pmu->read(event);
2160 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002161
2162 case PERF_EVENT_STATE_INACTIVE:
2163 update_event_times(event);
2164 break;
2165
2166 default:
2167 break;
2168 }
2169
2170 /*
2171 * In order to keep per-task stats reliable we need to flip the event
2172 * values when we flip the contexts.
2173 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002174 value = local64_read(&next_event->count);
2175 value = local64_xchg(&event->count, value);
2176 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002177
2178 swap(event->total_time_enabled, next_event->total_time_enabled);
2179 swap(event->total_time_running, next_event->total_time_running);
2180
2181 /*
2182 * Since we swizzled the values, update the user visible data too.
2183 */
2184 perf_event_update_userpage(event);
2185 perf_event_update_userpage(next_event);
2186}
2187
2188#define list_next_entry(pos, member) \
2189 list_entry(pos->member.next, typeof(*pos), member)
2190
2191static void perf_event_sync_stat(struct perf_event_context *ctx,
2192 struct perf_event_context *next_ctx)
2193{
2194 struct perf_event *event, *next_event;
2195
2196 if (!ctx->nr_stat)
2197 return;
2198
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002199 update_context_time(ctx);
2200
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002201 event = list_first_entry(&ctx->event_list,
2202 struct perf_event, event_entry);
2203
2204 next_event = list_first_entry(&next_ctx->event_list,
2205 struct perf_event, event_entry);
2206
2207 while (&event->event_entry != &ctx->event_list &&
2208 &next_event->event_entry != &next_ctx->event_list) {
2209
2210 __perf_event_sync_stat(event, next_event);
2211
2212 event = list_next_entry(event, event_entry);
2213 next_event = list_next_entry(next_event, event_entry);
2214 }
2215}
2216
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002217static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2218 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002219{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002220 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002221 struct perf_event_context *next_ctx;
2222 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002223 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002224 int do_switch = 1;
2225
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002226 if (likely(!ctx))
2227 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002228
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002229 cpuctx = __get_cpu_context(ctx);
2230 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002231 return;
2232
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002233 rcu_read_lock();
2234 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002235 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002236 if (parent && next_ctx &&
2237 rcu_dereference(next_ctx->parent_ctx) == parent) {
2238 /*
2239 * Looks like the two contexts are clones, so we might be
2240 * able to optimize the context switch. We lock both
2241 * contexts and check that they are clones under the
2242 * lock (including re-checking that neither has been
2243 * uncloned in the meantime). It doesn't matter which
2244 * order we take the locks because no other cpu could
2245 * be trying to lock both of these tasks.
2246 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002247 raw_spin_lock(&ctx->lock);
2248 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002249 if (context_equiv(ctx, next_ctx)) {
2250 /*
2251 * XXX do we need a memory barrier of sorts
2252 * wrt to rcu_dereference() of perf_event_ctxp
2253 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002254 task->perf_event_ctxp[ctxn] = next_ctx;
2255 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002256 ctx->task = next;
2257 next_ctx->task = task;
2258 do_switch = 0;
2259
2260 perf_event_sync_stat(ctx, next_ctx);
2261 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002262 raw_spin_unlock(&next_ctx->lock);
2263 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002264 }
2265 rcu_read_unlock();
2266
2267 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002268 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002269 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002270 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002271 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002272 }
2273}
2274
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002275#define for_each_task_context_nr(ctxn) \
2276 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2277
2278/*
2279 * Called from scheduler to remove the events of the current task,
2280 * with interrupts disabled.
2281 *
2282 * We stop each event and update the event value in event->count.
2283 *
2284 * This does not protect us against NMI, but disable()
2285 * sets the disabled bit in the control field of event _before_
2286 * accessing the event control register. If a NMI hits, then it will
2287 * not restart the event.
2288 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002289void __perf_event_task_sched_out(struct task_struct *task,
2290 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002291{
2292 int ctxn;
2293
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002294 for_each_task_context_nr(ctxn)
2295 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002296
2297 /*
2298 * if cgroup events exist on this CPU, then we need
2299 * to check if we have to switch out PMU state.
2300 * cgroup event are system-wide mode only
2301 */
2302 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002303 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002304}
2305
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002306static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002307{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002308 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002309
2310 if (!cpuctx->task_ctx)
2311 return;
2312
2313 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2314 return;
2315
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002316 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002317 cpuctx->task_ctx = NULL;
2318}
2319
2320/*
2321 * Called with IRQs disabled
2322 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002323static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2324 enum event_type_t event_type)
2325{
2326 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002327}
2328
2329static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002330ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002331 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002332{
2333 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002334
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002335 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2336 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002337 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002338 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002339 continue;
2340
Stephane Eraniane5d13672011-02-14 11:20:01 +02002341 /* may need to reset tstamp_enabled */
2342 if (is_cgroup_event(event))
2343 perf_cgroup_mark_enabled(event, ctx);
2344
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002345 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002346 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002347
2348 /*
2349 * If this pinned group hasn't been scheduled,
2350 * put it in error state.
2351 */
2352 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2353 update_group_times(event);
2354 event->state = PERF_EVENT_STATE_ERROR;
2355 }
2356 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002357}
2358
2359static void
2360ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002361 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002362{
2363 struct perf_event *event;
2364 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002365
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002366 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2367 /* Ignore events in OFF or ERROR state */
2368 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002369 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002370 /*
2371 * Listen to the 'cpu' scheduling filter constraint
2372 * of events:
2373 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002374 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002375 continue;
2376
Stephane Eraniane5d13672011-02-14 11:20:01 +02002377 /* may need to reset tstamp_enabled */
2378 if (is_cgroup_event(event))
2379 perf_cgroup_mark_enabled(event, ctx);
2380
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002381 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002382 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002383 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002384 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002385 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002386}
2387
2388static void
2389ctx_sched_in(struct perf_event_context *ctx,
2390 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002391 enum event_type_t event_type,
2392 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002393{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002394 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002395 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002396
Peter Zijlstradb24d332011-04-09 21:17:45 +02002397 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002398 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002399 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002400
Stephane Eraniane5d13672011-02-14 11:20:01 +02002401 now = perf_clock();
2402 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002403 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002404 /*
2405 * First go through the list and put on any pinned groups
2406 * in order to give them the best chance of going on.
2407 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002408 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002409 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002410
2411 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002412 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002413 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002414}
2415
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002416static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002417 enum event_type_t event_type,
2418 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002419{
2420 struct perf_event_context *ctx = &cpuctx->ctx;
2421
Stephane Eraniane5d13672011-02-14 11:20:01 +02002422 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002423}
2424
Stephane Eraniane5d13672011-02-14 11:20:01 +02002425static void perf_event_context_sched_in(struct perf_event_context *ctx,
2426 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002427{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002428 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002429
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002430 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002431 if (cpuctx->task_ctx == ctx)
2432 return;
2433
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002434 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002435 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002436 /*
2437 * We want to keep the following priority order:
2438 * cpu pinned (that don't need to move), task pinned,
2439 * cpu flexible, task flexible.
2440 */
2441 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2442
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002443 if (ctx->nr_events)
2444 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002445
Gleb Natapov86b47c22011-11-22 16:08:21 +02002446 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2447
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002448 perf_pmu_enable(ctx->pmu);
2449 perf_ctx_unlock(cpuctx, ctx);
2450
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002451 /*
2452 * Since these rotations are per-cpu, we need to ensure the
2453 * cpu-context we got scheduled on is actually rotating.
2454 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002455 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002456}
2457
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002458/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002459 * When sampling the branck stack in system-wide, it may be necessary
2460 * to flush the stack on context switch. This happens when the branch
2461 * stack does not tag its entries with the pid of the current task.
2462 * Otherwise it becomes impossible to associate a branch entry with a
2463 * task. This ambiguity is more likely to appear when the branch stack
2464 * supports priv level filtering and the user sets it to monitor only
2465 * at the user level (which could be a useful measurement in system-wide
2466 * mode). In that case, the risk is high of having a branch stack with
2467 * branch from multiple tasks. Flushing may mean dropping the existing
2468 * entries or stashing them somewhere in the PMU specific code layer.
2469 *
2470 * This function provides the context switch callback to the lower code
2471 * layer. It is invoked ONLY when there is at least one system-wide context
2472 * with at least one active event using taken branch sampling.
2473 */
2474static void perf_branch_stack_sched_in(struct task_struct *prev,
2475 struct task_struct *task)
2476{
2477 struct perf_cpu_context *cpuctx;
2478 struct pmu *pmu;
2479 unsigned long flags;
2480
2481 /* no need to flush branch stack if not changing task */
2482 if (prev == task)
2483 return;
2484
2485 local_irq_save(flags);
2486
2487 rcu_read_lock();
2488
2489 list_for_each_entry_rcu(pmu, &pmus, entry) {
2490 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2491
2492 /*
2493 * check if the context has at least one
2494 * event using PERF_SAMPLE_BRANCH_STACK
2495 */
2496 if (cpuctx->ctx.nr_branch_stack > 0
2497 && pmu->flush_branch_stack) {
2498
2499 pmu = cpuctx->ctx.pmu;
2500
2501 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2502
2503 perf_pmu_disable(pmu);
2504
2505 pmu->flush_branch_stack();
2506
2507 perf_pmu_enable(pmu);
2508
2509 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2510 }
2511 }
2512
2513 rcu_read_unlock();
2514
2515 local_irq_restore(flags);
2516}
2517
2518/*
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002519 * Called from scheduler to add the events of the current task
2520 * with interrupts disabled.
2521 *
2522 * We restore the event value and then enable it.
2523 *
2524 * This does not protect us against NMI, but enable()
2525 * sets the enabled bit in the control field of event _before_
2526 * accessing the event control register. If a NMI hits, then it will
2527 * keep the event running.
2528 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002529void __perf_event_task_sched_in(struct task_struct *prev,
2530 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002531{
2532 struct perf_event_context *ctx;
2533 int ctxn;
2534
2535 for_each_task_context_nr(ctxn) {
2536 ctx = task->perf_event_ctxp[ctxn];
2537 if (likely(!ctx))
2538 continue;
2539
Stephane Eraniane5d13672011-02-14 11:20:01 +02002540 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002541 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002542 /*
2543 * if cgroup events exist on this CPU, then we need
2544 * to check if we have to switch in PMU state.
2545 * cgroup event are system-wide mode only
2546 */
2547 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002548 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002549
2550 /* check for system-wide branch_stack events */
2551 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2552 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002553}
2554
Peter Zijlstraabd50712010-01-26 18:50:16 +01002555static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2556{
2557 u64 frequency = event->attr.sample_freq;
2558 u64 sec = NSEC_PER_SEC;
2559 u64 divisor, dividend;
2560
2561 int count_fls, nsec_fls, frequency_fls, sec_fls;
2562
2563 count_fls = fls64(count);
2564 nsec_fls = fls64(nsec);
2565 frequency_fls = fls64(frequency);
2566 sec_fls = 30;
2567
2568 /*
2569 * We got @count in @nsec, with a target of sample_freq HZ
2570 * the target period becomes:
2571 *
2572 * @count * 10^9
2573 * period = -------------------
2574 * @nsec * sample_freq
2575 *
2576 */
2577
2578 /*
2579 * Reduce accuracy by one bit such that @a and @b converge
2580 * to a similar magnitude.
2581 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002582#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002583do { \
2584 if (a##_fls > b##_fls) { \
2585 a >>= 1; \
2586 a##_fls--; \
2587 } else { \
2588 b >>= 1; \
2589 b##_fls--; \
2590 } \
2591} while (0)
2592
2593 /*
2594 * Reduce accuracy until either term fits in a u64, then proceed with
2595 * the other, so that finally we can do a u64/u64 division.
2596 */
2597 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2598 REDUCE_FLS(nsec, frequency);
2599 REDUCE_FLS(sec, count);
2600 }
2601
2602 if (count_fls + sec_fls > 64) {
2603 divisor = nsec * frequency;
2604
2605 while (count_fls + sec_fls > 64) {
2606 REDUCE_FLS(count, sec);
2607 divisor >>= 1;
2608 }
2609
2610 dividend = count * sec;
2611 } else {
2612 dividend = count * sec;
2613
2614 while (nsec_fls + frequency_fls > 64) {
2615 REDUCE_FLS(nsec, frequency);
2616 dividend >>= 1;
2617 }
2618
2619 divisor = nsec * frequency;
2620 }
2621
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002622 if (!divisor)
2623 return dividend;
2624
Peter Zijlstraabd50712010-01-26 18:50:16 +01002625 return div64_u64(dividend, divisor);
2626}
2627
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002628static DEFINE_PER_CPU(int, perf_throttled_count);
2629static DEFINE_PER_CPU(u64, perf_throttled_seq);
2630
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002631static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002632{
2633 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002634 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002635 s64 delta;
2636
Peter Zijlstraabd50712010-01-26 18:50:16 +01002637 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002638
2639 delta = (s64)(period - hwc->sample_period);
2640 delta = (delta + 7) / 8; /* low pass filter */
2641
2642 sample_period = hwc->sample_period + delta;
2643
2644 if (!sample_period)
2645 sample_period = 1;
2646
2647 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002648
Peter Zijlstrae7850592010-05-21 14:43:08 +02002649 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002650 if (disable)
2651 event->pmu->stop(event, PERF_EF_UPDATE);
2652
Peter Zijlstrae7850592010-05-21 14:43:08 +02002653 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002654
2655 if (disable)
2656 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002657 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002658}
2659
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002660/*
2661 * combine freq adjustment with unthrottling to avoid two passes over the
2662 * events. At the same time, make sure, having freq events does not change
2663 * the rate of unthrottling as that would introduce bias.
2664 */
2665static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2666 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002667{
2668 struct perf_event *event;
2669 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002670 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002671 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002672
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002673 /*
2674 * only need to iterate over all events iff:
2675 * - context have events in frequency mode (needs freq adjust)
2676 * - there are events to unthrottle on this cpu
2677 */
2678 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002679 return;
2680
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002681 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002682 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002683
Paul Mackerras03541f82009-10-14 16:58:03 +11002684 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002685 if (event->state != PERF_EVENT_STATE_ACTIVE)
2686 continue;
2687
Stephane Eranian5632ab12011-01-03 18:20:01 +02002688 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002689 continue;
2690
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002691 hwc = &event->hw;
2692
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002693 if (needs_unthr && hwc->interrupts == MAX_INTERRUPTS) {
2694 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002695 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002696 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002697 }
2698
2699 if (!event->attr.freq || !event->attr.sample_freq)
2700 continue;
2701
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002702 /*
2703 * stop the event and update event->count
2704 */
2705 event->pmu->stop(event, PERF_EF_UPDATE);
2706
Peter Zijlstrae7850592010-05-21 14:43:08 +02002707 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002708 delta = now - hwc->freq_count_stamp;
2709 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002710
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002711 /*
2712 * restart the event
2713 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002714 * we have stopped the event so tell that
2715 * to perf_adjust_period() to avoid stopping it
2716 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002717 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002718 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002719 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002720
2721 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002722 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002723
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002724 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002725 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002726}
2727
2728/*
2729 * Round-robin a context's events:
2730 */
2731static void rotate_ctx(struct perf_event_context *ctx)
2732{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002733 /*
2734 * Rotate the first entry last of non-pinned groups. Rotation might be
2735 * disabled by the inheritance code.
2736 */
2737 if (!ctx->rotate_disable)
2738 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002739}
2740
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002741/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002742 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2743 * because they're strictly cpu affine and rotate_start is called with IRQs
2744 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002745 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002746static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002747{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002748 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002749 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002750
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002751 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002752 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002753 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2754 rotate = 1;
2755 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002756
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002757 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002758 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002759 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002760 if (ctx->nr_events != ctx->nr_active)
2761 rotate = 1;
2762 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002763
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002764 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002765 goto done;
2766
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002767 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002768 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002769
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002770 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2771 if (ctx)
2772 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002773
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002774 rotate_ctx(&cpuctx->ctx);
2775 if (ctx)
2776 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002777
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002778 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002779
2780 perf_pmu_enable(cpuctx->ctx.pmu);
2781 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002782done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002783 if (remove)
2784 list_del_init(&cpuctx->rotation_list);
Stephane Eranian9e630202013-04-03 14:21:33 +02002785
2786 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002787}
2788
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002789#ifdef CONFIG_NO_HZ_FULL
2790bool perf_event_can_stop_tick(void)
2791{
2792 if (list_empty(&__get_cpu_var(rotation_list)))
2793 return true;
2794 else
2795 return false;
2796}
2797#endif
2798
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002799void perf_event_task_tick(void)
2800{
2801 struct list_head *head = &__get_cpu_var(rotation_list);
2802 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002803 struct perf_event_context *ctx;
2804 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002805
2806 WARN_ON(!irqs_disabled());
2807
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002808 __this_cpu_inc(perf_throttled_seq);
2809 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2810
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002811 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002812 ctx = &cpuctx->ctx;
2813 perf_adjust_freq_unthr_context(ctx, throttled);
2814
2815 ctx = cpuctx->task_ctx;
2816 if (ctx)
2817 perf_adjust_freq_unthr_context(ctx, throttled);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002818 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002819}
2820
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002821static int event_enable_on_exec(struct perf_event *event,
2822 struct perf_event_context *ctx)
2823{
2824 if (!event->attr.enable_on_exec)
2825 return 0;
2826
2827 event->attr.enable_on_exec = 0;
2828 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2829 return 0;
2830
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002831 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002832
2833 return 1;
2834}
2835
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002836/*
2837 * Enable all of a task's events that have been marked enable-on-exec.
2838 * This expects task == current.
2839 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002840static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002841{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002842 struct perf_event *event;
2843 unsigned long flags;
2844 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002845 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002846
2847 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002848 if (!ctx || !ctx->nr_events)
2849 goto out;
2850
Stephane Eraniane566b762011-04-06 02:54:54 +02002851 /*
2852 * We must ctxsw out cgroup events to avoid conflict
2853 * when invoking perf_task_event_sched_in() later on
2854 * in this function. Otherwise we end up trying to
2855 * ctxswin cgroup events which are already scheduled
2856 * in.
2857 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002858 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002859
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002860 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002861 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002862
Peter Zijlstrab79387e2011-11-22 11:25:43 +01002863 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002864 ret = event_enable_on_exec(event, ctx);
2865 if (ret)
2866 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002867 }
2868
2869 /*
2870 * Unclone this context if we enabled any event.
2871 */
2872 if (enabled)
2873 unclone_ctx(ctx);
2874
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002875 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002876
Stephane Eraniane566b762011-04-06 02:54:54 +02002877 /*
2878 * Also calls ctxswin for cgroup events, if any:
2879 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002880 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002881out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002882 local_irq_restore(flags);
2883}
2884
2885/*
2886 * Cross CPU call to read the hardware event
2887 */
2888static void __perf_event_read(void *info)
2889{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002890 struct perf_event *event = info;
2891 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002892 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002893
2894 /*
2895 * If this is a task context, we need to check whether it is
2896 * the current task context of this cpu. If not it has been
2897 * scheduled out before the smp call arrived. In that case
2898 * event->count would have been updated to a recent sample
2899 * when the event was scheduled out.
2900 */
2901 if (ctx->task && cpuctx->task_ctx != ctx)
2902 return;
2903
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002904 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002905 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002906 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002907 update_cgrp_time_from_event(event);
2908 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002909 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002910 if (event->state == PERF_EVENT_STATE_ACTIVE)
2911 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002912 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002913}
2914
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002915static inline u64 perf_event_count(struct perf_event *event)
2916{
Peter Zijlstrae7850592010-05-21 14:43:08 +02002917 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002918}
2919
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002920static u64 perf_event_read(struct perf_event *event)
2921{
2922 /*
2923 * If event is enabled and currently active on a CPU, update the
2924 * value in the event structure:
2925 */
2926 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2927 smp_call_function_single(event->oncpu,
2928 __perf_event_read, event, 1);
2929 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01002930 struct perf_event_context *ctx = event->ctx;
2931 unsigned long flags;
2932
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002933 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002934 /*
2935 * may read while context is not active
2936 * (e.g., thread is blocked), in that case
2937 * we cannot update context time
2938 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002939 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002940 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002941 update_cgrp_time_from_event(event);
2942 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002943 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002944 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002945 }
2946
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002947 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002948}
2949
2950/*
2951 * Initialize the perf_event context in a task_struct:
2952 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002953static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002954{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002955 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002956 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002957 INIT_LIST_HEAD(&ctx->pinned_groups);
2958 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002959 INIT_LIST_HEAD(&ctx->event_list);
2960 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002961}
2962
Peter Zijlstraeb184472010-09-07 15:55:13 +02002963static struct perf_event_context *
2964alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002965{
2966 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002967
2968 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2969 if (!ctx)
2970 return NULL;
2971
2972 __perf_event_init_context(ctx);
2973 if (task) {
2974 ctx->task = task;
2975 get_task_struct(task);
2976 }
2977 ctx->pmu = pmu;
2978
2979 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002980}
2981
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002982static struct task_struct *
2983find_lively_task_by_vpid(pid_t vpid)
2984{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002985 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002986 int err;
2987
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002988 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002989 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002990 task = current;
2991 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002992 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002993 if (task)
2994 get_task_struct(task);
2995 rcu_read_unlock();
2996
2997 if (!task)
2998 return ERR_PTR(-ESRCH);
2999
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003000 /* Reuse ptrace permission checks for now. */
3001 err = -EACCES;
3002 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3003 goto errout;
3004
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003005 return task;
3006errout:
3007 put_task_struct(task);
3008 return ERR_PTR(err);
3009
3010}
3011
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003012/*
3013 * Returns a matching context with refcount and pincount.
3014 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003015static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07003016find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003017{
3018 struct perf_event_context *ctx;
3019 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003020 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003021 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003022
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003023 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024 /* Must be root to operate on a CPU event: */
3025 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3026 return ERR_PTR(-EACCES);
3027
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003028 /*
3029 * We could be clever and allow to attach a event to an
3030 * offline CPU and activate it when the CPU comes up, but
3031 * that's for later.
3032 */
3033 if (!cpu_online(cpu))
3034 return ERR_PTR(-ENODEV);
3035
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003036 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003037 ctx = &cpuctx->ctx;
3038 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003039 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003040
3041 return ctx;
3042 }
3043
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003044 err = -EINVAL;
3045 ctxn = pmu->task_ctx_nr;
3046 if (ctxn < 0)
3047 goto errout;
3048
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003049retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003050 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003051 if (ctx) {
3052 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003053 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003054 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003055 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003056 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003057 err = -ENOMEM;
3058 if (!ctx)
3059 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003060
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003061 err = 0;
3062 mutex_lock(&task->perf_event_mutex);
3063 /*
3064 * If it has already passed perf_event_exit_task().
3065 * we must see PF_EXITING, it takes this mutex too.
3066 */
3067 if (task->flags & PF_EXITING)
3068 err = -ESRCH;
3069 else if (task->perf_event_ctxp[ctxn])
3070 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003071 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003072 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003073 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003074 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003075 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003076 mutex_unlock(&task->perf_event_mutex);
3077
3078 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003079 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003080
3081 if (err == -EAGAIN)
3082 goto retry;
3083 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003084 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003085 }
3086
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003087 return ctx;
3088
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003089errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003090 return ERR_PTR(err);
3091}
3092
Li Zefan6fb29152009-10-15 11:21:42 +08003093static void perf_event_free_filter(struct perf_event *event);
3094
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003095static void free_event_rcu(struct rcu_head *head)
3096{
3097 struct perf_event *event;
3098
3099 event = container_of(head, struct perf_event, rcu_head);
3100 if (event->ns)
3101 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003102 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003103 kfree(event);
3104}
3105
Frederic Weisbecker76369132011-05-19 19:55:04 +02003106static void ring_buffer_put(struct ring_buffer *rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003107static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003108
3109static void free_event(struct perf_event *event)
3110{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003111 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003112
3113 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02003114 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarc5905af2012-02-24 08:31:31 +01003115 static_key_slow_dec_deferred(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01003116 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003117 atomic_dec(&nr_mmap_events);
3118 if (event->attr.comm)
3119 atomic_dec(&nr_comm_events);
3120 if (event->attr.task)
3121 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003122 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3123 put_callchain_buffers();
Peter Zijlstra08309372011-03-03 11:31:20 +01003124 if (is_cgroup_event(event)) {
3125 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
Ingo Molnarc5905af2012-02-24 08:31:31 +01003126 static_key_slow_dec_deferred(&perf_sched_events);
Peter Zijlstra08309372011-03-03 11:31:20 +01003127 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003128
3129 if (has_branch_stack(event)) {
3130 static_key_slow_dec_deferred(&perf_sched_events);
3131 /* is system-wide event */
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003132 if (!(event->attach_state & PERF_ATTACH_TASK)) {
Stephane Eraniand010b332012-02-09 23:21:00 +01003133 atomic_dec(&per_cpu(perf_branch_stack_events,
3134 event->cpu));
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003135 }
Stephane Eraniand010b332012-02-09 23:21:00 +01003136 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003137 }
3138
Frederic Weisbecker76369132011-05-19 19:55:04 +02003139 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003140 struct ring_buffer *rb;
3141
3142 /*
3143 * Can happen when we close an event with re-directed output.
3144 *
3145 * Since we have a 0 refcount, perf_mmap_close() will skip
3146 * over us; possibly making our ring_buffer_put() the last.
3147 */
3148 mutex_lock(&event->mmap_mutex);
3149 rb = event->rb;
3150 if (rb) {
3151 rcu_assign_pointer(event->rb, NULL);
3152 ring_buffer_detach(event, rb);
3153 ring_buffer_put(rb); /* could be last */
3154 }
3155 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003156 }
3157
Stephane Eraniane5d13672011-02-14 11:20:01 +02003158 if (is_cgroup_event(event))
3159 perf_detach_cgroup(event);
3160
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003161 if (event->destroy)
3162 event->destroy(event);
3163
Peter Zijlstra0c67b402010-09-13 11:15:58 +02003164 if (event->ctx)
3165 put_ctx(event->ctx);
3166
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003167 call_rcu(&event->rcu_head, free_event_rcu);
3168}
3169
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003170int perf_event_release_kernel(struct perf_event *event)
3171{
3172 struct perf_event_context *ctx = event->ctx;
3173
3174 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02003175 /*
3176 * There are two ways this annotation is useful:
3177 *
3178 * 1) there is a lock recursion from perf_event_exit_task
3179 * see the comment there.
3180 *
3181 * 2) there is a lock-inversion with mmap_sem through
3182 * perf_event_read_group(), which takes faults while
3183 * holding ctx->mutex, however this is called after
3184 * the last filedesc died, so there is no possibility
3185 * to trigger the AB-BA case.
3186 */
3187 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02003188 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02003189 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02003190 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrae03a9a52011-04-09 21:17:47 +02003191 perf_remove_from_context(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003192 mutex_unlock(&ctx->mutex);
3193
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003194 free_event(event);
3195
3196 return 0;
3197}
3198EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3199
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003200/*
3201 * Called when the last reference to the file is gone.
3202 */
Al Viroa6fa9412012-08-20 14:59:25 +01003203static void put_event(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003204{
Peter Zijlstra88821352010-11-09 19:01:43 +01003205 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003206
Al Viroa6fa9412012-08-20 14:59:25 +01003207 if (!atomic_long_dec_and_test(&event->refcount))
3208 return;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003209
Peter Zijlstra88821352010-11-09 19:01:43 +01003210 rcu_read_lock();
3211 owner = ACCESS_ONCE(event->owner);
3212 /*
3213 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3214 * !owner it means the list deletion is complete and we can indeed
3215 * free this event, otherwise we need to serialize on
3216 * owner->perf_event_mutex.
3217 */
3218 smp_read_barrier_depends();
3219 if (owner) {
3220 /*
3221 * Since delayed_put_task_struct() also drops the last
3222 * task reference we can safely take a new reference
3223 * while holding the rcu_read_lock().
3224 */
3225 get_task_struct(owner);
3226 }
3227 rcu_read_unlock();
3228
3229 if (owner) {
3230 mutex_lock(&owner->perf_event_mutex);
3231 /*
3232 * We have to re-check the event->owner field, if it is cleared
3233 * we raced with perf_event_exit_task(), acquiring the mutex
3234 * ensured they're done, and we can proceed with freeing the
3235 * event.
3236 */
3237 if (event->owner)
3238 list_del_init(&event->owner_entry);
3239 mutex_unlock(&owner->perf_event_mutex);
3240 put_task_struct(owner);
3241 }
3242
Al Viroa6fa9412012-08-20 14:59:25 +01003243 perf_event_release_kernel(event);
3244}
3245
3246static int perf_release(struct inode *inode, struct file *file)
3247{
3248 put_event(file->private_data);
3249 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003250}
3251
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003252u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003253{
3254 struct perf_event *child;
3255 u64 total = 0;
3256
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003257 *enabled = 0;
3258 *running = 0;
3259
Peter Zijlstra6f105812009-11-20 22:19:56 +01003260 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003261 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003262 *enabled += event->total_time_enabled +
3263 atomic64_read(&event->child_total_time_enabled);
3264 *running += event->total_time_running +
3265 atomic64_read(&event->child_total_time_running);
3266
3267 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003268 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003269 *enabled += child->total_time_enabled;
3270 *running += child->total_time_running;
3271 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003272 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003273
3274 return total;
3275}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003276EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003277
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003278static int perf_event_read_group(struct perf_event *event,
3279 u64 read_format, char __user *buf)
3280{
3281 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003282 int n = 0, size = 0, ret = -EFAULT;
3283 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003284 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003285 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003286
Peter Zijlstra6f105812009-11-20 22:19:56 +01003287 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003288 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003289
3290 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003291 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3292 values[n++] = enabled;
3293 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3294 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003295 values[n++] = count;
3296 if (read_format & PERF_FORMAT_ID)
3297 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003298
3299 size = n * sizeof(u64);
3300
3301 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003302 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003303
Peter Zijlstra6f105812009-11-20 22:19:56 +01003304 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003305
3306 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003307 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003308
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003309 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003310 if (read_format & PERF_FORMAT_ID)
3311 values[n++] = primary_event_id(sub);
3312
3313 size = n * sizeof(u64);
3314
Stephane Eranian184d3da2009-11-23 21:40:49 -08003315 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003316 ret = -EFAULT;
3317 goto unlock;
3318 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003319
3320 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003321 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003322unlock:
3323 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003324
Peter Zijlstraabf48682009-11-20 22:19:49 +01003325 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003326}
3327
3328static int perf_event_read_one(struct perf_event *event,
3329 u64 read_format, char __user *buf)
3330{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003331 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003332 u64 values[4];
3333 int n = 0;
3334
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003335 values[n++] = perf_event_read_value(event, &enabled, &running);
3336 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3337 values[n++] = enabled;
3338 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3339 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003340 if (read_format & PERF_FORMAT_ID)
3341 values[n++] = primary_event_id(event);
3342
3343 if (copy_to_user(buf, values, n * sizeof(u64)))
3344 return -EFAULT;
3345
3346 return n * sizeof(u64);
3347}
3348
3349/*
3350 * Read the performance event - simple non blocking version for now
3351 */
3352static ssize_t
3353perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3354{
3355 u64 read_format = event->attr.read_format;
3356 int ret;
3357
3358 /*
3359 * Return end-of-file for a read on a event that is in
3360 * error state (i.e. because it was pinned but it couldn't be
3361 * scheduled on to the CPU at some point).
3362 */
3363 if (event->state == PERF_EVENT_STATE_ERROR)
3364 return 0;
3365
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003366 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003367 return -ENOSPC;
3368
3369 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003370 if (read_format & PERF_FORMAT_GROUP)
3371 ret = perf_event_read_group(event, read_format, buf);
3372 else
3373 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003374
3375 return ret;
3376}
3377
3378static ssize_t
3379perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3380{
3381 struct perf_event *event = file->private_data;
3382
3383 return perf_read_hw(event, buf, count);
3384}
3385
3386static unsigned int perf_poll(struct file *file, poll_table *wait)
3387{
3388 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003389 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003390 unsigned int events = POLL_HUP;
3391
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003392 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003393 * Pin the event->rb by taking event->mmap_mutex; otherwise
3394 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003395 */
3396 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003397 rb = event->rb;
3398 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003399 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003400 mutex_unlock(&event->mmap_mutex);
3401
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402 poll_wait(file, &event->waitq, wait);
3403
3404 return events;
3405}
3406
3407static void perf_event_reset(struct perf_event *event)
3408{
3409 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003410 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003411 perf_event_update_userpage(event);
3412}
3413
3414/*
3415 * Holding the top-level event's child_mutex means that any
3416 * descendant process that has inherited this event will block
3417 * in sync_child_event if it goes to exit, thus satisfying the
3418 * task existence requirements of perf_event_enable/disable.
3419 */
3420static void perf_event_for_each_child(struct perf_event *event,
3421 void (*func)(struct perf_event *))
3422{
3423 struct perf_event *child;
3424
3425 WARN_ON_ONCE(event->ctx->parent_ctx);
3426 mutex_lock(&event->child_mutex);
3427 func(event);
3428 list_for_each_entry(child, &event->child_list, child_list)
3429 func(child);
3430 mutex_unlock(&event->child_mutex);
3431}
3432
3433static void perf_event_for_each(struct perf_event *event,
3434 void (*func)(struct perf_event *))
3435{
3436 struct perf_event_context *ctx = event->ctx;
3437 struct perf_event *sibling;
3438
3439 WARN_ON_ONCE(ctx->parent_ctx);
3440 mutex_lock(&ctx->mutex);
3441 event = event->group_leader;
3442
3443 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003444 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003445 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003446 mutex_unlock(&ctx->mutex);
3447}
3448
3449static int perf_event_period(struct perf_event *event, u64 __user *arg)
3450{
3451 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003452 int ret = 0;
3453 u64 value;
3454
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003455 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003456 return -EINVAL;
3457
John Blackwoodad0cf342010-09-28 18:03:11 -04003458 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003459 return -EFAULT;
3460
3461 if (!value)
3462 return -EINVAL;
3463
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003464 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003465 if (event->attr.freq) {
3466 if (value > sysctl_perf_event_sample_rate) {
3467 ret = -EINVAL;
3468 goto unlock;
3469 }
3470
3471 event->attr.sample_freq = value;
3472 } else {
3473 event->attr.sample_period = value;
3474 event->hw.sample_period = value;
3475 }
3476unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003477 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003478
3479 return ret;
3480}
3481
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003482static const struct file_operations perf_fops;
3483
Al Viro2903ff02012-08-28 12:52:22 -04003484static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003485{
Al Viro2903ff02012-08-28 12:52:22 -04003486 struct fd f = fdget(fd);
3487 if (!f.file)
3488 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003489
Al Viro2903ff02012-08-28 12:52:22 -04003490 if (f.file->f_op != &perf_fops) {
3491 fdput(f);
3492 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003493 }
Al Viro2903ff02012-08-28 12:52:22 -04003494 *p = f;
3495 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003496}
3497
3498static int perf_event_set_output(struct perf_event *event,
3499 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003500static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003501
3502static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3503{
3504 struct perf_event *event = file->private_data;
3505 void (*func)(struct perf_event *);
3506 u32 flags = arg;
3507
3508 switch (cmd) {
3509 case PERF_EVENT_IOC_ENABLE:
3510 func = perf_event_enable;
3511 break;
3512 case PERF_EVENT_IOC_DISABLE:
3513 func = perf_event_disable;
3514 break;
3515 case PERF_EVENT_IOC_RESET:
3516 func = perf_event_reset;
3517 break;
3518
3519 case PERF_EVENT_IOC_REFRESH:
3520 return perf_event_refresh(event, arg);
3521
3522 case PERF_EVENT_IOC_PERIOD:
3523 return perf_event_period(event, (u64 __user *)arg);
3524
3525 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003526 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003527 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003528 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003529 struct perf_event *output_event;
3530 struct fd output;
3531 ret = perf_fget_light(arg, &output);
3532 if (ret)
3533 return ret;
3534 output_event = output.file->private_data;
3535 ret = perf_event_set_output(event, output_event);
3536 fdput(output);
3537 } else {
3538 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003539 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003540 return ret;
3541 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003542
Li Zefan6fb29152009-10-15 11:21:42 +08003543 case PERF_EVENT_IOC_SET_FILTER:
3544 return perf_event_set_filter(event, (void __user *)arg);
3545
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003546 default:
3547 return -ENOTTY;
3548 }
3549
3550 if (flags & PERF_IOC_FLAG_GROUP)
3551 perf_event_for_each(event, func);
3552 else
3553 perf_event_for_each_child(event, func);
3554
3555 return 0;
3556}
3557
3558int perf_event_task_enable(void)
3559{
3560 struct perf_event *event;
3561
3562 mutex_lock(&current->perf_event_mutex);
3563 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3564 perf_event_for_each_child(event, perf_event_enable);
3565 mutex_unlock(&current->perf_event_mutex);
3566
3567 return 0;
3568}
3569
3570int perf_event_task_disable(void)
3571{
3572 struct perf_event *event;
3573
3574 mutex_lock(&current->perf_event_mutex);
3575 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3576 perf_event_for_each_child(event, perf_event_disable);
3577 mutex_unlock(&current->perf_event_mutex);
3578
3579 return 0;
3580}
3581
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003582static int perf_event_index(struct perf_event *event)
3583{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003584 if (event->hw.state & PERF_HES_STOPPED)
3585 return 0;
3586
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003587 if (event->state != PERF_EVENT_STATE_ACTIVE)
3588 return 0;
3589
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01003590 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003591}
3592
Eric B Munsonc4794292011-06-23 16:34:38 -04003593static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003594 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04003595 u64 *enabled,
3596 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04003597{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003598 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04003599
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003600 *now = perf_clock();
3601 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04003602 *enabled = ctx_time - event->tstamp_enabled;
3603 *running = ctx_time - event->tstamp_running;
3604}
3605
Peter Zijlstrac7206202012-03-22 17:26:36 +01003606void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003607{
3608}
3609
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003610/*
3611 * Callers need to ensure there can be no nesting of this function, otherwise
3612 * the seqlock logic goes bad. We can not serialize this because the arch
3613 * code calls this from NMI context.
3614 */
3615void perf_event_update_userpage(struct perf_event *event)
3616{
3617 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003618 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003619 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003620
3621 rcu_read_lock();
Eric B Munson0d641202011-06-24 12:26:26 -04003622 /*
3623 * compute total_time_enabled, total_time_running
3624 * based on snapshot values taken when the event
3625 * was last scheduled in.
3626 *
3627 * we cannot simply called update_context_time()
3628 * because of locking issue as we can be called in
3629 * NMI context
3630 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003631 calc_timer_values(event, &now, &enabled, &running);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003632 rb = rcu_dereference(event->rb);
3633 if (!rb)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003634 goto unlock;
3635
Frederic Weisbecker76369132011-05-19 19:55:04 +02003636 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003637
3638 /*
3639 * Disable preemption so as to not let the corresponding user-space
3640 * spin too long if we get preempted.
3641 */
3642 preempt_disable();
3643 ++userpg->lock;
3644 barrier();
3645 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003646 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01003647 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003648 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003649
Eric B Munson0d641202011-06-24 12:26:26 -04003650 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003651 atomic64_read(&event->child_total_time_enabled);
3652
Eric B Munson0d641202011-06-24 12:26:26 -04003653 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003654 atomic64_read(&event->child_total_time_running);
3655
Peter Zijlstrac7206202012-03-22 17:26:36 +01003656 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003657
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003658 barrier();
3659 ++userpg->lock;
3660 preempt_enable();
3661unlock:
3662 rcu_read_unlock();
3663}
3664
Peter Zijlstra906010b2009-09-21 16:08:49 +02003665static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3666{
3667 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003668 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003669 int ret = VM_FAULT_SIGBUS;
3670
3671 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3672 if (vmf->pgoff == 0)
3673 ret = 0;
3674 return ret;
3675 }
3676
3677 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003678 rb = rcu_dereference(event->rb);
3679 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003680 goto unlock;
3681
3682 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3683 goto unlock;
3684
Frederic Weisbecker76369132011-05-19 19:55:04 +02003685 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003686 if (!vmf->page)
3687 goto unlock;
3688
3689 get_page(vmf->page);
3690 vmf->page->mapping = vma->vm_file->f_mapping;
3691 vmf->page->index = vmf->pgoff;
3692
3693 ret = 0;
3694unlock:
3695 rcu_read_unlock();
3696
3697 return ret;
3698}
3699
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003700static void ring_buffer_attach(struct perf_event *event,
3701 struct ring_buffer *rb)
3702{
3703 unsigned long flags;
3704
3705 if (!list_empty(&event->rb_entry))
3706 return;
3707
3708 spin_lock_irqsave(&rb->event_lock, flags);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003709 if (list_empty(&event->rb_entry))
3710 list_add(&event->rb_entry, &rb->event_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003711 spin_unlock_irqrestore(&rb->event_lock, flags);
3712}
3713
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003714static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb)
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003715{
3716 unsigned long flags;
3717
3718 if (list_empty(&event->rb_entry))
3719 return;
3720
3721 spin_lock_irqsave(&rb->event_lock, flags);
3722 list_del_init(&event->rb_entry);
3723 wake_up_all(&event->waitq);
3724 spin_unlock_irqrestore(&rb->event_lock, flags);
3725}
3726
3727static void ring_buffer_wakeup(struct perf_event *event)
3728{
3729 struct ring_buffer *rb;
3730
3731 rcu_read_lock();
3732 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003733 if (rb) {
3734 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3735 wake_up_all(&event->waitq);
3736 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003737 rcu_read_unlock();
3738}
3739
Frederic Weisbecker76369132011-05-19 19:55:04 +02003740static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003741{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003742 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003743
Frederic Weisbecker76369132011-05-19 19:55:04 +02003744 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3745 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003746}
3747
Frederic Weisbecker76369132011-05-19 19:55:04 +02003748static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003749{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003750 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003751
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003752 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003753 rb = rcu_dereference(event->rb);
3754 if (rb) {
3755 if (!atomic_inc_not_zero(&rb->refcount))
3756 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003757 }
3758 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003759
Frederic Weisbecker76369132011-05-19 19:55:04 +02003760 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003761}
3762
Frederic Weisbecker76369132011-05-19 19:55:04 +02003763static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003764{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003765 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003766 return;
3767
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003768 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003769
Frederic Weisbecker76369132011-05-19 19:55:04 +02003770 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003771}
3772
3773static void perf_mmap_open(struct vm_area_struct *vma)
3774{
3775 struct perf_event *event = vma->vm_file->private_data;
3776
3777 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003778 atomic_inc(&event->rb->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003779}
3780
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003781/*
3782 * A buffer can be mmap()ed multiple times; either directly through the same
3783 * event, or through other events by use of perf_event_set_output().
3784 *
3785 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3786 * the buffer here, where we still have a VM context. This means we need
3787 * to detach all events redirecting to us.
3788 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003789static void perf_mmap_close(struct vm_area_struct *vma)
3790{
3791 struct perf_event *event = vma->vm_file->private_data;
3792
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003793 struct ring_buffer *rb = event->rb;
3794 struct user_struct *mmap_user = rb->mmap_user;
3795 int mmap_locked = rb->mmap_locked;
3796 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003797
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003798 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003799
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003800 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
3801 return;
3802
3803 /* Detach current event from the buffer. */
3804 rcu_assign_pointer(event->rb, NULL);
3805 ring_buffer_detach(event, rb);
3806 mutex_unlock(&event->mmap_mutex);
3807
3808 /* If there's still other mmap()s of this buffer, we're done. */
3809 if (atomic_read(&rb->mmap_count)) {
3810 ring_buffer_put(rb); /* can't be last */
3811 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003812 }
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003813
3814 /*
3815 * No other mmap()s, detach from all other events that might redirect
3816 * into the now unreachable buffer. Somewhat complicated by the
3817 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3818 */
3819again:
3820 rcu_read_lock();
3821 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
3822 if (!atomic_long_inc_not_zero(&event->refcount)) {
3823 /*
3824 * This event is en-route to free_event() which will
3825 * detach it and remove it from the list.
3826 */
3827 continue;
3828 }
3829 rcu_read_unlock();
3830
3831 mutex_lock(&event->mmap_mutex);
3832 /*
3833 * Check we didn't race with perf_event_set_output() which can
3834 * swizzle the rb from under us while we were waiting to
3835 * acquire mmap_mutex.
3836 *
3837 * If we find a different rb; ignore this event, a next
3838 * iteration will no longer find it on the list. We have to
3839 * still restart the iteration to make sure we're not now
3840 * iterating the wrong list.
3841 */
3842 if (event->rb == rb) {
3843 rcu_assign_pointer(event->rb, NULL);
3844 ring_buffer_detach(event, rb);
3845 ring_buffer_put(rb); /* can't be last, we still have one */
3846 }
3847 mutex_unlock(&event->mmap_mutex);
3848 put_event(event);
3849
3850 /*
3851 * Restart the iteration; either we're on the wrong list or
3852 * destroyed its integrity by doing a deletion.
3853 */
3854 goto again;
3855 }
3856 rcu_read_unlock();
3857
3858 /*
3859 * It could be there's still a few 0-ref events on the list; they'll
3860 * get cleaned up by free_event() -- they'll also still have their
3861 * ref on the rb and will free it whenever they are done with it.
3862 *
3863 * Aside from that, this buffer is 'fully' detached and unmapped,
3864 * undo the VM accounting.
3865 */
3866
3867 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
3868 vma->vm_mm->pinned_vm -= mmap_locked;
3869 free_uid(mmap_user);
3870
3871 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003872}
3873
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003874static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003875 .open = perf_mmap_open,
3876 .close = perf_mmap_close,
3877 .fault = perf_mmap_fault,
3878 .page_mkwrite = perf_mmap_fault,
3879};
3880
3881static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3882{
3883 struct perf_event *event = file->private_data;
3884 unsigned long user_locked, user_lock_limit;
3885 struct user_struct *user = current_user();
3886 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003887 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003888 unsigned long vma_size;
3889 unsigned long nr_pages;
3890 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003891 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003892
Peter Zijlstrac7920612010-05-18 10:33:24 +02003893 /*
3894 * Don't allow mmap() of inherited per-task counters. This would
3895 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02003896 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02003897 */
3898 if (event->cpu == -1 && event->attr.inherit)
3899 return -EINVAL;
3900
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003901 if (!(vma->vm_flags & VM_SHARED))
3902 return -EINVAL;
3903
3904 vma_size = vma->vm_end - vma->vm_start;
3905 nr_pages = (vma_size / PAGE_SIZE) - 1;
3906
3907 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02003908 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003909 * can do bitmasks instead of modulo.
3910 */
3911 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3912 return -EINVAL;
3913
3914 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3915 return -EINVAL;
3916
3917 if (vma->vm_pgoff != 0)
3918 return -EINVAL;
3919
3920 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003921again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003922 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02003923 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003924 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003925 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003926 goto unlock;
3927 }
3928
3929 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
3930 /*
3931 * Raced against perf_mmap_close() through
3932 * perf_event_set_output(). Try again, hope for better
3933 * luck.
3934 */
3935 mutex_unlock(&event->mmap_mutex);
3936 goto again;
3937 }
3938
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003939 goto unlock;
3940 }
3941
3942 user_extra = nr_pages + 1;
3943 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3944
3945 /*
3946 * Increase the limit linearly with more CPUs:
3947 */
3948 user_lock_limit *= num_online_cpus();
3949
3950 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3951
3952 extra = 0;
3953 if (user_locked > user_lock_limit)
3954 extra = user_locked - user_lock_limit;
3955
Jiri Slaby78d7d402010-03-05 13:42:54 -08003956 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003957 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07003958 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003959
3960 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3961 !capable(CAP_IPC_LOCK)) {
3962 ret = -EPERM;
3963 goto unlock;
3964 }
3965
Frederic Weisbecker76369132011-05-19 19:55:04 +02003966 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003967
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003968 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003969 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003970
Vince Weaver4ec83632011-06-01 15:15:36 -04003971 rb = rb_alloc(nr_pages,
3972 event->attr.watermark ? event->attr.wakeup_watermark : 0,
3973 event->cpu, flags);
3974
Frederic Weisbecker76369132011-05-19 19:55:04 +02003975 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003976 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003977 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003978 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02003979
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003980 atomic_set(&rb->mmap_count, 1);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02003981 rb->mmap_locked = extra;
3982 rb->mmap_user = get_current_user();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003983
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003984 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02003985 vma->vm_mm->pinned_vm += extra;
3986
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003987 ring_buffer_attach(event, rb);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02003988 rcu_assign_pointer(event->rb, rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003989
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01003990 perf_event_update_userpage(event);
3991
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003992unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003993 if (!ret)
3994 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003995 mutex_unlock(&event->mmap_mutex);
3996
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003997 /*
3998 * Since pinned accounting is per vm we cannot allow fork() to copy our
3999 * vma.
4000 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004001 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004002 vma->vm_ops = &perf_mmap_vmops;
4003
4004 return ret;
4005}
4006
4007static int perf_fasync(int fd, struct file *filp, int on)
4008{
Al Viro496ad9a2013-01-23 17:07:38 -05004009 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004010 struct perf_event *event = filp->private_data;
4011 int retval;
4012
4013 mutex_lock(&inode->i_mutex);
4014 retval = fasync_helper(fd, filp, on, &event->fasync);
4015 mutex_unlock(&inode->i_mutex);
4016
4017 if (retval < 0)
4018 return retval;
4019
4020 return 0;
4021}
4022
4023static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004024 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004025 .release = perf_release,
4026 .read = perf_read,
4027 .poll = perf_poll,
4028 .unlocked_ioctl = perf_ioctl,
4029 .compat_ioctl = perf_ioctl,
4030 .mmap = perf_mmap,
4031 .fasync = perf_fasync,
4032};
4033
4034/*
4035 * Perf event wakeup
4036 *
4037 * If there's data, ensure we set the poll() state and publish everything
4038 * to user-space before waking everybody up.
4039 */
4040
4041void perf_event_wakeup(struct perf_event *event)
4042{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004043 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004044
4045 if (event->pending_kill) {
4046 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4047 event->pending_kill = 0;
4048 }
4049}
4050
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004051static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004052{
4053 struct perf_event *event = container_of(entry,
4054 struct perf_event, pending);
4055
4056 if (event->pending_disable) {
4057 event->pending_disable = 0;
4058 __perf_event_disable(event);
4059 }
4060
4061 if (event->pending_wakeup) {
4062 event->pending_wakeup = 0;
4063 perf_event_wakeup(event);
4064 }
4065}
4066
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004067/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004068 * We assume there is only KVM supporting the callbacks.
4069 * Later on, we might change it to a list if there is
4070 * another virtualization implementation supporting the callbacks.
4071 */
4072struct perf_guest_info_callbacks *perf_guest_cbs;
4073
4074int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4075{
4076 perf_guest_cbs = cbs;
4077 return 0;
4078}
4079EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4080
4081int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4082{
4083 perf_guest_cbs = NULL;
4084 return 0;
4085}
4086EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4087
Jiri Olsa40189942012-08-07 15:20:37 +02004088static void
4089perf_output_sample_regs(struct perf_output_handle *handle,
4090 struct pt_regs *regs, u64 mask)
4091{
4092 int bit;
4093
4094 for_each_set_bit(bit, (const unsigned long *) &mask,
4095 sizeof(mask) * BITS_PER_BYTE) {
4096 u64 val;
4097
4098 val = perf_reg_value(regs, bit);
4099 perf_output_put(handle, val);
4100 }
4101}
4102
4103static void perf_sample_regs_user(struct perf_regs_user *regs_user,
4104 struct pt_regs *regs)
4105{
4106 if (!user_mode(regs)) {
4107 if (current->mm)
4108 regs = task_pt_regs(current);
4109 else
4110 regs = NULL;
4111 }
4112
4113 if (regs) {
4114 regs_user->regs = regs;
4115 regs_user->abi = perf_reg_abi(current);
4116 }
4117}
4118
Jiri Olsac5ebced2012-08-07 15:20:40 +02004119/*
4120 * Get remaining task size from user stack pointer.
4121 *
4122 * It'd be better to take stack vma map and limit this more
4123 * precisly, but there's no way to get it safely under interrupt,
4124 * so using TASK_SIZE as limit.
4125 */
4126static u64 perf_ustack_task_size(struct pt_regs *regs)
4127{
4128 unsigned long addr = perf_user_stack_pointer(regs);
4129
4130 if (!addr || addr >= TASK_SIZE)
4131 return 0;
4132
4133 return TASK_SIZE - addr;
4134}
4135
4136static u16
4137perf_sample_ustack_size(u16 stack_size, u16 header_size,
4138 struct pt_regs *regs)
4139{
4140 u64 task_size;
4141
4142 /* No regs, no stack pointer, no dump. */
4143 if (!regs)
4144 return 0;
4145
4146 /*
4147 * Check if we fit in with the requested stack size into the:
4148 * - TASK_SIZE
4149 * If we don't, we limit the size to the TASK_SIZE.
4150 *
4151 * - remaining sample size
4152 * If we don't, we customize the stack size to
4153 * fit in to the remaining sample size.
4154 */
4155
4156 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4157 stack_size = min(stack_size, (u16) task_size);
4158
4159 /* Current header size plus static size and dynamic size. */
4160 header_size += 2 * sizeof(u64);
4161
4162 /* Do we fit in with the current stack dump size? */
4163 if ((u16) (header_size + stack_size) < header_size) {
4164 /*
4165 * If we overflow the maximum size for the sample,
4166 * we customize the stack dump size to fit in.
4167 */
4168 stack_size = USHRT_MAX - header_size - sizeof(u64);
4169 stack_size = round_up(stack_size, sizeof(u64));
4170 }
4171
4172 return stack_size;
4173}
4174
4175static void
4176perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4177 struct pt_regs *regs)
4178{
4179 /* Case of a kernel thread, nothing to dump */
4180 if (!regs) {
4181 u64 size = 0;
4182 perf_output_put(handle, size);
4183 } else {
4184 unsigned long sp;
4185 unsigned int rem;
4186 u64 dyn_size;
4187
4188 /*
4189 * We dump:
4190 * static size
4191 * - the size requested by user or the best one we can fit
4192 * in to the sample max size
4193 * data
4194 * - user stack dump data
4195 * dynamic size
4196 * - the actual dumped size
4197 */
4198
4199 /* Static size. */
4200 perf_output_put(handle, dump_size);
4201
4202 /* Data. */
4203 sp = perf_user_stack_pointer(regs);
4204 rem = __output_copy_user(handle, (void *) sp, dump_size);
4205 dyn_size = dump_size - rem;
4206
4207 perf_output_skip(handle, rem);
4208
4209 /* Dynamic size. */
4210 perf_output_put(handle, dyn_size);
4211 }
4212}
4213
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004214static void __perf_event_header__init_id(struct perf_event_header *header,
4215 struct perf_sample_data *data,
4216 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004217{
4218 u64 sample_type = event->attr.sample_type;
4219
4220 data->type = sample_type;
4221 header->size += event->id_header_size;
4222
4223 if (sample_type & PERF_SAMPLE_TID) {
4224 /* namespace issues */
4225 data->tid_entry.pid = perf_event_pid(event, current);
4226 data->tid_entry.tid = perf_event_tid(event, current);
4227 }
4228
4229 if (sample_type & PERF_SAMPLE_TIME)
4230 data->time = perf_clock();
4231
4232 if (sample_type & PERF_SAMPLE_ID)
4233 data->id = primary_event_id(event);
4234
4235 if (sample_type & PERF_SAMPLE_STREAM_ID)
4236 data->stream_id = event->id;
4237
4238 if (sample_type & PERF_SAMPLE_CPU) {
4239 data->cpu_entry.cpu = raw_smp_processor_id();
4240 data->cpu_entry.reserved = 0;
4241 }
4242}
4243
Frederic Weisbecker76369132011-05-19 19:55:04 +02004244void perf_event_header__init_id(struct perf_event_header *header,
4245 struct perf_sample_data *data,
4246 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004247{
4248 if (event->attr.sample_id_all)
4249 __perf_event_header__init_id(header, data, event);
4250}
4251
4252static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4253 struct perf_sample_data *data)
4254{
4255 u64 sample_type = data->type;
4256
4257 if (sample_type & PERF_SAMPLE_TID)
4258 perf_output_put(handle, data->tid_entry);
4259
4260 if (sample_type & PERF_SAMPLE_TIME)
4261 perf_output_put(handle, data->time);
4262
4263 if (sample_type & PERF_SAMPLE_ID)
4264 perf_output_put(handle, data->id);
4265
4266 if (sample_type & PERF_SAMPLE_STREAM_ID)
4267 perf_output_put(handle, data->stream_id);
4268
4269 if (sample_type & PERF_SAMPLE_CPU)
4270 perf_output_put(handle, data->cpu_entry);
4271}
4272
Frederic Weisbecker76369132011-05-19 19:55:04 +02004273void perf_event__output_id_sample(struct perf_event *event,
4274 struct perf_output_handle *handle,
4275 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004276{
4277 if (event->attr.sample_id_all)
4278 __perf_event__output_id_sample(handle, sample);
4279}
4280
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004281static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004282 struct perf_event *event,
4283 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004284{
4285 u64 read_format = event->attr.read_format;
4286 u64 values[4];
4287 int n = 0;
4288
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004289 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004290 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004291 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004292 atomic64_read(&event->child_total_time_enabled);
4293 }
4294 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004295 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004296 atomic64_read(&event->child_total_time_running);
4297 }
4298 if (read_format & PERF_FORMAT_ID)
4299 values[n++] = primary_event_id(event);
4300
Frederic Weisbecker76369132011-05-19 19:55:04 +02004301 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004302}
4303
4304/*
4305 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4306 */
4307static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004308 struct perf_event *event,
4309 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004310{
4311 struct perf_event *leader = event->group_leader, *sub;
4312 u64 read_format = event->attr.read_format;
4313 u64 values[5];
4314 int n = 0;
4315
4316 values[n++] = 1 + leader->nr_siblings;
4317
4318 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004319 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004320
4321 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004322 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004323
4324 if (leader != event)
4325 leader->pmu->read(leader);
4326
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004327 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004328 if (read_format & PERF_FORMAT_ID)
4329 values[n++] = primary_event_id(leader);
4330
Frederic Weisbecker76369132011-05-19 19:55:04 +02004331 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004332
4333 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4334 n = 0;
4335
4336 if (sub != event)
4337 sub->pmu->read(sub);
4338
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004339 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004340 if (read_format & PERF_FORMAT_ID)
4341 values[n++] = primary_event_id(sub);
4342
Frederic Weisbecker76369132011-05-19 19:55:04 +02004343 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004344 }
4345}
4346
Stephane Eranianeed01522010-10-26 16:08:01 +02004347#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4348 PERF_FORMAT_TOTAL_TIME_RUNNING)
4349
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004350static void perf_output_read(struct perf_output_handle *handle,
4351 struct perf_event *event)
4352{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004353 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004354 u64 read_format = event->attr.read_format;
4355
4356 /*
4357 * compute total_time_enabled, total_time_running
4358 * based on snapshot values taken when the event
4359 * was last scheduled in.
4360 *
4361 * we cannot simply called update_context_time()
4362 * because of locking issue as we are called in
4363 * NMI context
4364 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004365 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004366 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004367
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004368 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004369 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004370 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004371 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004372}
4373
4374void perf_output_sample(struct perf_output_handle *handle,
4375 struct perf_event_header *header,
4376 struct perf_sample_data *data,
4377 struct perf_event *event)
4378{
4379 u64 sample_type = data->type;
4380
4381 perf_output_put(handle, *header);
4382
4383 if (sample_type & PERF_SAMPLE_IP)
4384 perf_output_put(handle, data->ip);
4385
4386 if (sample_type & PERF_SAMPLE_TID)
4387 perf_output_put(handle, data->tid_entry);
4388
4389 if (sample_type & PERF_SAMPLE_TIME)
4390 perf_output_put(handle, data->time);
4391
4392 if (sample_type & PERF_SAMPLE_ADDR)
4393 perf_output_put(handle, data->addr);
4394
4395 if (sample_type & PERF_SAMPLE_ID)
4396 perf_output_put(handle, data->id);
4397
4398 if (sample_type & PERF_SAMPLE_STREAM_ID)
4399 perf_output_put(handle, data->stream_id);
4400
4401 if (sample_type & PERF_SAMPLE_CPU)
4402 perf_output_put(handle, data->cpu_entry);
4403
4404 if (sample_type & PERF_SAMPLE_PERIOD)
4405 perf_output_put(handle, data->period);
4406
4407 if (sample_type & PERF_SAMPLE_READ)
4408 perf_output_read(handle, event);
4409
4410 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4411 if (data->callchain) {
4412 int size = 1;
4413
4414 if (data->callchain)
4415 size += data->callchain->nr;
4416
4417 size *= sizeof(u64);
4418
Frederic Weisbecker76369132011-05-19 19:55:04 +02004419 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004420 } else {
4421 u64 nr = 0;
4422 perf_output_put(handle, nr);
4423 }
4424 }
4425
4426 if (sample_type & PERF_SAMPLE_RAW) {
4427 if (data->raw) {
4428 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004429 __output_copy(handle, data->raw->data,
4430 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004431 } else {
4432 struct {
4433 u32 size;
4434 u32 data;
4435 } raw = {
4436 .size = sizeof(u32),
4437 .data = 0,
4438 };
4439 perf_output_put(handle, raw);
4440 }
4441 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004442
4443 if (!event->attr.watermark) {
4444 int wakeup_events = event->attr.wakeup_events;
4445
4446 if (wakeup_events) {
4447 struct ring_buffer *rb = handle->rb;
4448 int events = local_inc_return(&rb->events);
4449
4450 if (events >= wakeup_events) {
4451 local_sub(wakeup_events, &rb->events);
4452 local_inc(&rb->wakeup);
4453 }
4454 }
4455 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004456
4457 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4458 if (data->br_stack) {
4459 size_t size;
4460
4461 size = data->br_stack->nr
4462 * sizeof(struct perf_branch_entry);
4463
4464 perf_output_put(handle, data->br_stack->nr);
4465 perf_output_copy(handle, data->br_stack->entries, size);
4466 } else {
4467 /*
4468 * we always store at least the value of nr
4469 */
4470 u64 nr = 0;
4471 perf_output_put(handle, nr);
4472 }
4473 }
Jiri Olsa40189942012-08-07 15:20:37 +02004474
4475 if (sample_type & PERF_SAMPLE_REGS_USER) {
4476 u64 abi = data->regs_user.abi;
4477
4478 /*
4479 * If there are no regs to dump, notice it through
4480 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4481 */
4482 perf_output_put(handle, abi);
4483
4484 if (abi) {
4485 u64 mask = event->attr.sample_regs_user;
4486 perf_output_sample_regs(handle,
4487 data->regs_user.regs,
4488 mask);
4489 }
4490 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004491
4492 if (sample_type & PERF_SAMPLE_STACK_USER)
4493 perf_output_sample_ustack(handle,
4494 data->stack_user_size,
4495 data->regs_user.regs);
Andi Kleenc3feedf2013-01-24 16:10:28 +01004496
4497 if (sample_type & PERF_SAMPLE_WEIGHT)
4498 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01004499
4500 if (sample_type & PERF_SAMPLE_DATA_SRC)
4501 perf_output_put(handle, data->data_src.val);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004502}
4503
4504void perf_prepare_sample(struct perf_event_header *header,
4505 struct perf_sample_data *data,
4506 struct perf_event *event,
4507 struct pt_regs *regs)
4508{
4509 u64 sample_type = event->attr.sample_type;
4510
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004511 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004512 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004513
4514 header->misc = 0;
4515 header->misc |= perf_misc_flags(regs);
4516
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004517 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004518
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004519 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004520 data->ip = perf_instruction_pointer(regs);
4521
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004522 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4523 int size = 1;
4524
Andrew Vagine6dab5f2012-07-11 18:14:58 +04004525 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004526
4527 if (data->callchain)
4528 size += data->callchain->nr;
4529
4530 header->size += size * sizeof(u64);
4531 }
4532
4533 if (sample_type & PERF_SAMPLE_RAW) {
4534 int size = sizeof(u32);
4535
4536 if (data->raw)
4537 size += data->raw->size;
4538 else
4539 size += sizeof(u32);
4540
4541 WARN_ON_ONCE(size & (sizeof(u64)-1));
4542 header->size += size;
4543 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004544
4545 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4546 int size = sizeof(u64); /* nr */
4547 if (data->br_stack) {
4548 size += data->br_stack->nr
4549 * sizeof(struct perf_branch_entry);
4550 }
4551 header->size += size;
4552 }
Jiri Olsa40189942012-08-07 15:20:37 +02004553
4554 if (sample_type & PERF_SAMPLE_REGS_USER) {
4555 /* regs dump ABI info */
4556 int size = sizeof(u64);
4557
4558 perf_sample_regs_user(&data->regs_user, regs);
4559
4560 if (data->regs_user.regs) {
4561 u64 mask = event->attr.sample_regs_user;
4562 size += hweight64(mask) * sizeof(u64);
4563 }
4564
4565 header->size += size;
4566 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004567
4568 if (sample_type & PERF_SAMPLE_STACK_USER) {
4569 /*
4570 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4571 * processed as the last one or have additional check added
4572 * in case new sample type is added, because we could eat
4573 * up the rest of the sample size.
4574 */
4575 struct perf_regs_user *uregs = &data->regs_user;
4576 u16 stack_size = event->attr.sample_stack_user;
4577 u16 size = sizeof(u64);
4578
4579 if (!uregs->abi)
4580 perf_sample_regs_user(uregs, regs);
4581
4582 stack_size = perf_sample_ustack_size(stack_size, header->size,
4583 uregs->regs);
4584
4585 /*
4586 * If there is something to dump, add space for the dump
4587 * itself and for the field that tells the dynamic size,
4588 * which is how many have been actually dumped.
4589 */
4590 if (stack_size)
4591 size += sizeof(u64) + stack_size;
4592
4593 data->stack_user_size = stack_size;
4594 header->size += size;
4595 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004596}
4597
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004598static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004599 struct perf_sample_data *data,
4600 struct pt_regs *regs)
4601{
4602 struct perf_output_handle handle;
4603 struct perf_event_header header;
4604
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004605 /* protect the callchain buffers */
4606 rcu_read_lock();
4607
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004608 perf_prepare_sample(&header, data, event, regs);
4609
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004610 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004611 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004612
4613 perf_output_sample(&handle, &header, data, event);
4614
4615 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004616
4617exit:
4618 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004619}
4620
4621/*
4622 * read event_id
4623 */
4624
4625struct perf_read_event {
4626 struct perf_event_header header;
4627
4628 u32 pid;
4629 u32 tid;
4630};
4631
4632static void
4633perf_event_read_event(struct perf_event *event,
4634 struct task_struct *task)
4635{
4636 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004637 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004638 struct perf_read_event read_event = {
4639 .header = {
4640 .type = PERF_RECORD_READ,
4641 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004642 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004643 },
4644 .pid = perf_event_pid(event, task),
4645 .tid = perf_event_tid(event, task),
4646 };
4647 int ret;
4648
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004649 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004650 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004651 if (ret)
4652 return;
4653
4654 perf_output_put(&handle, read_event);
4655 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004656 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004657
4658 perf_output_end(&handle);
4659}
4660
Jiri Olsa52d857a2013-05-06 18:27:18 +02004661typedef int (perf_event_aux_match_cb)(struct perf_event *event, void *data);
4662typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
4663
4664static void
4665perf_event_aux_ctx(struct perf_event_context *ctx,
4666 perf_event_aux_match_cb match,
4667 perf_event_aux_output_cb output,
4668 void *data)
4669{
4670 struct perf_event *event;
4671
4672 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4673 if (event->state < PERF_EVENT_STATE_INACTIVE)
4674 continue;
4675 if (!event_filter_match(event))
4676 continue;
4677 if (match(event, data))
4678 output(event, data);
4679 }
4680}
4681
4682static void
4683perf_event_aux(perf_event_aux_match_cb match,
4684 perf_event_aux_output_cb output,
4685 void *data,
4686 struct perf_event_context *task_ctx)
4687{
4688 struct perf_cpu_context *cpuctx;
4689 struct perf_event_context *ctx;
4690 struct pmu *pmu;
4691 int ctxn;
4692
4693 rcu_read_lock();
4694 list_for_each_entry_rcu(pmu, &pmus, entry) {
4695 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4696 if (cpuctx->unique_pmu != pmu)
4697 goto next;
4698 perf_event_aux_ctx(&cpuctx->ctx, match, output, data);
4699 if (task_ctx)
4700 goto next;
4701 ctxn = pmu->task_ctx_nr;
4702 if (ctxn < 0)
4703 goto next;
4704 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4705 if (ctx)
4706 perf_event_aux_ctx(ctx, match, output, data);
4707next:
4708 put_cpu_ptr(pmu->pmu_cpu_context);
4709 }
4710
4711 if (task_ctx) {
4712 preempt_disable();
4713 perf_event_aux_ctx(task_ctx, match, output, data);
4714 preempt_enable();
4715 }
4716 rcu_read_unlock();
4717}
4718
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004719/*
4720 * task tracking -- fork/exit
4721 *
Eric B Munson3af9e852010-05-18 15:30:49 +01004722 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004723 */
4724
4725struct perf_task_event {
4726 struct task_struct *task;
4727 struct perf_event_context *task_ctx;
4728
4729 struct {
4730 struct perf_event_header header;
4731
4732 u32 pid;
4733 u32 ppid;
4734 u32 tid;
4735 u32 ptid;
4736 u64 time;
4737 } event_id;
4738};
4739
4740static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004741 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004742{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004743 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004744 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004745 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004746 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004747 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004748
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004749 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004750
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004751 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004752 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02004753 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004754 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004755
4756 task_event->event_id.pid = perf_event_pid(event, task);
4757 task_event->event_id.ppid = perf_event_pid(event, current);
4758
4759 task_event->event_id.tid = perf_event_tid(event, task);
4760 task_event->event_id.ptid = perf_event_tid(event, current);
4761
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004762 perf_output_put(&handle, task_event->event_id);
4763
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004764 perf_event__output_id_sample(event, &handle, &sample);
4765
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004766 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004767out:
4768 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004769}
4770
Jiri Olsa52d857a2013-05-06 18:27:18 +02004771static int perf_event_task_match(struct perf_event *event,
4772 void *data __maybe_unused)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004773{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004774 return event->attr.comm || event->attr.mmap ||
4775 event->attr.mmap_data || event->attr.task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004776}
4777
4778static void perf_event_task(struct task_struct *task,
4779 struct perf_event_context *task_ctx,
4780 int new)
4781{
4782 struct perf_task_event task_event;
4783
4784 if (!atomic_read(&nr_comm_events) &&
4785 !atomic_read(&nr_mmap_events) &&
4786 !atomic_read(&nr_task_events))
4787 return;
4788
4789 task_event = (struct perf_task_event){
4790 .task = task,
4791 .task_ctx = task_ctx,
4792 .event_id = {
4793 .header = {
4794 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4795 .misc = 0,
4796 .size = sizeof(task_event.event_id),
4797 },
4798 /* .pid */
4799 /* .ppid */
4800 /* .tid */
4801 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004802 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004803 },
4804 };
4805
Jiri Olsa52d857a2013-05-06 18:27:18 +02004806 perf_event_aux(perf_event_task_match,
4807 perf_event_task_output,
4808 &task_event,
4809 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004810}
4811
4812void perf_event_fork(struct task_struct *task)
4813{
4814 perf_event_task(task, NULL, 1);
4815}
4816
4817/*
4818 * comm tracking
4819 */
4820
4821struct perf_comm_event {
4822 struct task_struct *task;
4823 char *comm;
4824 int comm_size;
4825
4826 struct {
4827 struct perf_event_header header;
4828
4829 u32 pid;
4830 u32 tid;
4831 } event_id;
4832};
4833
4834static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004835 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004836{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004837 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004838 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004839 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004840 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004841 int ret;
4842
4843 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4844 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004845 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004846
4847 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004848 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004849
4850 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4851 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4852
4853 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004854 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004855 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004856
4857 perf_event__output_id_sample(event, &handle, &sample);
4858
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004859 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004860out:
4861 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004862}
4863
Jiri Olsa52d857a2013-05-06 18:27:18 +02004864static int perf_event_comm_match(struct perf_event *event,
4865 void *data __maybe_unused)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004866{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004867 return event->attr.comm;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004868}
4869
4870static void perf_event_comm_event(struct perf_comm_event *comm_event)
4871{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004872 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004873 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004874
4875 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01004876 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004877 size = ALIGN(strlen(comm)+1, sizeof(u64));
4878
4879 comm_event->comm = comm;
4880 comm_event->comm_size = size;
4881
4882 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004883
Jiri Olsa52d857a2013-05-06 18:27:18 +02004884 perf_event_aux(perf_event_comm_match,
4885 perf_event_comm_output,
4886 comm_event,
4887 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004888}
4889
4890void perf_event_comm(struct task_struct *task)
4891{
4892 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004893 struct perf_event_context *ctx;
4894 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004895
Paul E. McKenneyc79aa0d92013-04-19 12:01:24 -07004896 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004897 for_each_task_context_nr(ctxn) {
4898 ctx = task->perf_event_ctxp[ctxn];
4899 if (!ctx)
4900 continue;
4901
4902 perf_event_enable_on_exec(ctx);
4903 }
Paul E. McKenneyc79aa0d92013-04-19 12:01:24 -07004904 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004905
4906 if (!atomic_read(&nr_comm_events))
4907 return;
4908
4909 comm_event = (struct perf_comm_event){
4910 .task = task,
4911 /* .comm */
4912 /* .comm_size */
4913 .event_id = {
4914 .header = {
4915 .type = PERF_RECORD_COMM,
4916 .misc = 0,
4917 /* .size */
4918 },
4919 /* .pid */
4920 /* .tid */
4921 },
4922 };
4923
4924 perf_event_comm_event(&comm_event);
4925}
4926
4927/*
4928 * mmap tracking
4929 */
4930
4931struct perf_mmap_event {
4932 struct vm_area_struct *vma;
4933
4934 const char *file_name;
4935 int file_size;
4936
4937 struct {
4938 struct perf_event_header header;
4939
4940 u32 pid;
4941 u32 tid;
4942 u64 start;
4943 u64 len;
4944 u64 pgoff;
4945 } event_id;
4946};
4947
4948static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004949 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004950{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004951 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004952 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004953 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004954 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004955 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004956
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004957 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4958 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004959 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004960 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004961 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004962
4963 mmap_event->event_id.pid = perf_event_pid(event, current);
4964 mmap_event->event_id.tid = perf_event_tid(event, current);
4965
4966 perf_output_put(&handle, mmap_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004967 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004968 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004969
4970 perf_event__output_id_sample(event, &handle, &sample);
4971
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004972 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004973out:
4974 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004975}
4976
4977static int perf_event_mmap_match(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004978 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004979{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004980 struct perf_mmap_event *mmap_event = data;
4981 struct vm_area_struct *vma = mmap_event->vma;
4982 int executable = vma->vm_flags & VM_EXEC;
Peter Zijlstra22e19082010-01-18 09:12:32 +01004983
Jiri Olsa52d857a2013-05-06 18:27:18 +02004984 return (!executable && event->attr.mmap_data) ||
4985 (executable && event->attr.mmap);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004986}
4987
4988static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4989{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004990 struct vm_area_struct *vma = mmap_event->vma;
4991 struct file *file = vma->vm_file;
4992 unsigned int size;
4993 char tmp[16];
4994 char *buf = NULL;
4995 const char *name;
4996
4997 memset(tmp, 0, sizeof(tmp));
4998
4999 if (file) {
5000 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02005001 * d_path works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005002 * need to add enough zero bytes after the string to handle
5003 * the 64bit alignment we do later.
5004 */
5005 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
5006 if (!buf) {
5007 name = strncpy(tmp, "//enomem", sizeof(tmp));
5008 goto got_name;
5009 }
5010 name = d_path(&file->f_path, buf, PATH_MAX);
5011 if (IS_ERR(name)) {
5012 name = strncpy(tmp, "//toolong", sizeof(tmp));
5013 goto got_name;
5014 }
5015 } else {
5016 if (arch_vma_name(mmap_event->vma)) {
5017 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
Chen Gangc97847d2013-04-08 11:48:27 +08005018 sizeof(tmp) - 1);
5019 tmp[sizeof(tmp) - 1] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005020 goto got_name;
5021 }
5022
5023 if (!vma->vm_mm) {
5024 name = strncpy(tmp, "[vdso]", sizeof(tmp));
5025 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01005026 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
5027 vma->vm_end >= vma->vm_mm->brk) {
5028 name = strncpy(tmp, "[heap]", sizeof(tmp));
5029 goto got_name;
5030 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
5031 vma->vm_end >= vma->vm_mm->start_stack) {
5032 name = strncpy(tmp, "[stack]", sizeof(tmp));
5033 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005034 }
5035
5036 name = strncpy(tmp, "//anon", sizeof(tmp));
5037 goto got_name;
5038 }
5039
5040got_name:
5041 size = ALIGN(strlen(name)+1, sizeof(u64));
5042
5043 mmap_event->file_name = name;
5044 mmap_event->file_size = size;
5045
Stephane Eranian2fe85422013-01-24 16:10:39 +01005046 if (!(vma->vm_flags & VM_EXEC))
5047 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5048
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005049 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5050
Jiri Olsa52d857a2013-05-06 18:27:18 +02005051 perf_event_aux(perf_event_mmap_match,
5052 perf_event_mmap_output,
5053 mmap_event,
5054 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005055
5056 kfree(buf);
5057}
5058
Eric B Munson3af9e852010-05-18 15:30:49 +01005059void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005060{
5061 struct perf_mmap_event mmap_event;
5062
5063 if (!atomic_read(&nr_mmap_events))
5064 return;
5065
5066 mmap_event = (struct perf_mmap_event){
5067 .vma = vma,
5068 /* .file_name */
5069 /* .file_size */
5070 .event_id = {
5071 .header = {
5072 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005073 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005074 /* .size */
5075 },
5076 /* .pid */
5077 /* .tid */
5078 .start = vma->vm_start,
5079 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01005080 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005081 },
5082 };
5083
5084 perf_event_mmap_event(&mmap_event);
5085}
5086
5087/*
5088 * IRQ throttle logging
5089 */
5090
5091static void perf_log_throttle(struct perf_event *event, int enable)
5092{
5093 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005094 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005095 int ret;
5096
5097 struct {
5098 struct perf_event_header header;
5099 u64 time;
5100 u64 id;
5101 u64 stream_id;
5102 } throttle_event = {
5103 .header = {
5104 .type = PERF_RECORD_THROTTLE,
5105 .misc = 0,
5106 .size = sizeof(throttle_event),
5107 },
5108 .time = perf_clock(),
5109 .id = primary_event_id(event),
5110 .stream_id = event->id,
5111 };
5112
5113 if (enable)
5114 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5115
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005116 perf_event_header__init_id(&throttle_event.header, &sample, event);
5117
5118 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005119 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005120 if (ret)
5121 return;
5122
5123 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005124 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005125 perf_output_end(&handle);
5126}
5127
5128/*
5129 * Generic event overflow handling, sampling.
5130 */
5131
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005132static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005133 int throttle, struct perf_sample_data *data,
5134 struct pt_regs *regs)
5135{
5136 int events = atomic_read(&event->event_limit);
5137 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005138 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005139 int ret = 0;
5140
Peter Zijlstra96398822010-11-24 18:55:29 +01005141 /*
5142 * Non-sampling counters might still use the PMI to fold short
5143 * hardware counters, ignore those.
5144 */
5145 if (unlikely(!is_sampling_event(event)))
5146 return 0;
5147
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005148 seq = __this_cpu_read(perf_throttled_seq);
5149 if (seq != hwc->interrupts_seq) {
5150 hwc->interrupts_seq = seq;
5151 hwc->interrupts = 1;
5152 } else {
5153 hwc->interrupts++;
5154 if (unlikely(throttle
5155 && hwc->interrupts >= max_samples_per_tick)) {
5156 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01005157 hwc->interrupts = MAX_INTERRUPTS;
5158 perf_log_throttle(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005159 ret = 1;
5160 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005161 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005162
5163 if (event->attr.freq) {
5164 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01005165 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005166
Peter Zijlstraabd50712010-01-26 18:50:16 +01005167 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005168
Peter Zijlstraabd50712010-01-26 18:50:16 +01005169 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01005170 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005171 }
5172
5173 /*
5174 * XXX event_limit might not quite work as expected on inherited
5175 * events
5176 */
5177
5178 event->pending_kill = POLL_IN;
5179 if (events && atomic_dec_and_test(&event->event_limit)) {
5180 ret = 1;
5181 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005182 event->pending_disable = 1;
5183 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005184 }
5185
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005186 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005187 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005188 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005189 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005190
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005191 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005192 event->pending_wakeup = 1;
5193 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005194 }
5195
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005196 return ret;
5197}
5198
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005199int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005200 struct perf_sample_data *data,
5201 struct pt_regs *regs)
5202{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005203 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005204}
5205
5206/*
5207 * Generic software event infrastructure
5208 */
5209
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005210struct swevent_htable {
5211 struct swevent_hlist *swevent_hlist;
5212 struct mutex hlist_mutex;
5213 int hlist_refcount;
5214
5215 /* Recursion avoidance in each contexts */
5216 int recursion[PERF_NR_CONTEXTS];
5217};
5218
5219static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5220
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005221/*
5222 * We directly increment event->count and keep a second value in
5223 * event->hw.period_left to count intervals. This period event
5224 * is kept in the range [-sample_period, 0] so that we can use the
5225 * sign as trigger.
5226 */
5227
Jiri Olsaab573842013-05-01 17:25:44 +02005228u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005229{
5230 struct hw_perf_event *hwc = &event->hw;
5231 u64 period = hwc->last_period;
5232 u64 nr, offset;
5233 s64 old, val;
5234
5235 hwc->last_period = hwc->sample_period;
5236
5237again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005238 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005239 if (val < 0)
5240 return 0;
5241
5242 nr = div64_u64(period + val, period);
5243 offset = nr * period;
5244 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005245 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005246 goto again;
5247
5248 return nr;
5249}
5250
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005251static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005252 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005253 struct pt_regs *regs)
5254{
5255 struct hw_perf_event *hwc = &event->hw;
5256 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005257
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005258 if (!overflow)
5259 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005260
5261 if (hwc->interrupts == MAX_INTERRUPTS)
5262 return;
5263
5264 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005265 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005266 data, regs)) {
5267 /*
5268 * We inhibit the overflow from happening when
5269 * hwc->interrupts == MAX_INTERRUPTS.
5270 */
5271 break;
5272 }
5273 throttle = 1;
5274 }
5275}
5276
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005277static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005278 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005279 struct pt_regs *regs)
5280{
5281 struct hw_perf_event *hwc = &event->hw;
5282
Peter Zijlstrae7850592010-05-21 14:43:08 +02005283 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005284
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005285 if (!regs)
5286 return;
5287
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005288 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005289 return;
5290
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005291 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5292 data->period = nr;
5293 return perf_swevent_overflow(event, 1, data, regs);
5294 } else
5295 data->period = event->hw.last_period;
5296
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005297 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005298 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005299
Peter Zijlstrae7850592010-05-21 14:43:08 +02005300 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005301 return;
5302
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005303 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005304}
5305
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005306static int perf_exclude_event(struct perf_event *event,
5307 struct pt_regs *regs)
5308{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005309 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005310 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005311
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005312 if (regs) {
5313 if (event->attr.exclude_user && user_mode(regs))
5314 return 1;
5315
5316 if (event->attr.exclude_kernel && !user_mode(regs))
5317 return 1;
5318 }
5319
5320 return 0;
5321}
5322
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005323static int perf_swevent_match(struct perf_event *event,
5324 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005325 u32 event_id,
5326 struct perf_sample_data *data,
5327 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005328{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005329 if (event->attr.type != type)
5330 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005331
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005332 if (event->attr.config != event_id)
5333 return 0;
5334
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005335 if (perf_exclude_event(event, regs))
5336 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005337
5338 return 1;
5339}
5340
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005341static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005342{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005343 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005344
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005345 return hash_64(val, SWEVENT_HLIST_BITS);
5346}
5347
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005348static inline struct hlist_head *
5349__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005350{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005351 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005352
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005353 return &hlist->heads[hash];
5354}
5355
5356/* For the read side: events when they trigger */
5357static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005358find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005359{
5360 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005361
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005362 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005363 if (!hlist)
5364 return NULL;
5365
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005366 return __find_swevent_head(hlist, type, event_id);
5367}
5368
5369/* For the event head insertion and removal in the hlist */
5370static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005371find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005372{
5373 struct swevent_hlist *hlist;
5374 u32 event_id = event->attr.config;
5375 u64 type = event->attr.type;
5376
5377 /*
5378 * Event scheduling is always serialized against hlist allocation
5379 * and release. Which makes the protected version suitable here.
5380 * The context lock guarantees that.
5381 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005382 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005383 lockdep_is_held(&event->ctx->lock));
5384 if (!hlist)
5385 return NULL;
5386
5387 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005388}
5389
5390static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005391 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005392 struct perf_sample_data *data,
5393 struct pt_regs *regs)
5394{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005395 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005396 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005397 struct hlist_head *head;
5398
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005399 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005400 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005401 if (!head)
5402 goto end;
5403
Sasha Levinb67bfe02013-02-27 17:06:00 -08005404 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005405 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005406 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005407 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005408end:
5409 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005410}
5411
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005412int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005413{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005414 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005415
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005416 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005417}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005418EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005419
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005420inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005421{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005422 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005423
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005424 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005425}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005426
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005427void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005428{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005429 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005430 int rctx;
5431
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005432 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005433 rctx = perf_swevent_get_recursion_context();
5434 if (rctx < 0)
5435 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005436
Robert Richterfd0d0002012-04-02 20:19:08 +02005437 perf_sample_data_init(&data, addr, 0);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005438
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005439 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005440
5441 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005442 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005443}
5444
5445static void perf_swevent_read(struct perf_event *event)
5446{
5447}
5448
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005449static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005450{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005451 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005452 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005453 struct hlist_head *head;
5454
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005455 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005456 hwc->last_period = hwc->sample_period;
5457 perf_swevent_set_period(event);
5458 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005459
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005460 hwc->state = !(flags & PERF_EF_START);
5461
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005462 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005463 if (WARN_ON_ONCE(!head))
5464 return -EINVAL;
5465
5466 hlist_add_head_rcu(&event->hlist_entry, head);
5467
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005468 return 0;
5469}
5470
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005471static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005472{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005473 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005474}
5475
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005476static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005477{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005478 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005479}
5480
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005481static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005482{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005483 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005484}
5485
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005486/* Deref the hlist from the update side */
5487static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005488swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005489{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005490 return rcu_dereference_protected(swhash->swevent_hlist,
5491 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005492}
5493
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005494static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005495{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005496 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005497
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005498 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005499 return;
5500
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005501 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08005502 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005503}
5504
5505static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5506{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005507 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005508
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005509 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005510
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005511 if (!--swhash->hlist_refcount)
5512 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005513
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005514 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005515}
5516
5517static void swevent_hlist_put(struct perf_event *event)
5518{
5519 int cpu;
5520
5521 if (event->cpu != -1) {
5522 swevent_hlist_put_cpu(event, event->cpu);
5523 return;
5524 }
5525
5526 for_each_possible_cpu(cpu)
5527 swevent_hlist_put_cpu(event, cpu);
5528}
5529
5530static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5531{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005532 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005533 int err = 0;
5534
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005535 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005536
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005537 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005538 struct swevent_hlist *hlist;
5539
5540 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5541 if (!hlist) {
5542 err = -ENOMEM;
5543 goto exit;
5544 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005545 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005546 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005547 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005548exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005549 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005550
5551 return err;
5552}
5553
5554static int swevent_hlist_get(struct perf_event *event)
5555{
5556 int err;
5557 int cpu, failed_cpu;
5558
5559 if (event->cpu != -1)
5560 return swevent_hlist_get_cpu(event, event->cpu);
5561
5562 get_online_cpus();
5563 for_each_possible_cpu(cpu) {
5564 err = swevent_hlist_get_cpu(event, cpu);
5565 if (err) {
5566 failed_cpu = cpu;
5567 goto fail;
5568 }
5569 }
5570 put_online_cpus();
5571
5572 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005573fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005574 for_each_possible_cpu(cpu) {
5575 if (cpu == failed_cpu)
5576 break;
5577 swevent_hlist_put_cpu(event, cpu);
5578 }
5579
5580 put_online_cpus();
5581 return err;
5582}
5583
Ingo Molnarc5905af2012-02-24 08:31:31 +01005584struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005585
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005586static void sw_perf_event_destroy(struct perf_event *event)
5587{
5588 u64 event_id = event->attr.config;
5589
5590 WARN_ON(event->parent);
5591
Ingo Molnarc5905af2012-02-24 08:31:31 +01005592 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005593 swevent_hlist_put(event);
5594}
5595
5596static int perf_swevent_init(struct perf_event *event)
5597{
Tommi Rantala8176cce2013-04-13 22:49:14 +03005598 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005599
5600 if (event->attr.type != PERF_TYPE_SOFTWARE)
5601 return -ENOENT;
5602
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005603 /*
5604 * no branch sampling for software events
5605 */
5606 if (has_branch_stack(event))
5607 return -EOPNOTSUPP;
5608
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005609 switch (event_id) {
5610 case PERF_COUNT_SW_CPU_CLOCK:
5611 case PERF_COUNT_SW_TASK_CLOCK:
5612 return -ENOENT;
5613
5614 default:
5615 break;
5616 }
5617
Dan Carpenterce677832010-10-24 21:50:42 +02005618 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005619 return -ENOENT;
5620
5621 if (!event->parent) {
5622 int err;
5623
5624 err = swevent_hlist_get(event);
5625 if (err)
5626 return err;
5627
Ingo Molnarc5905af2012-02-24 08:31:31 +01005628 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005629 event->destroy = sw_perf_event_destroy;
5630 }
5631
5632 return 0;
5633}
5634
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005635static int perf_swevent_event_idx(struct perf_event *event)
5636{
5637 return 0;
5638}
5639
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005640static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005641 .task_ctx_nr = perf_sw_context,
5642
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005643 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005644 .add = perf_swevent_add,
5645 .del = perf_swevent_del,
5646 .start = perf_swevent_start,
5647 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005648 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005649
5650 .event_idx = perf_swevent_event_idx,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005651};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005652
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005653#ifdef CONFIG_EVENT_TRACING
5654
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005655static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005656 struct perf_sample_data *data)
5657{
5658 void *record = data->raw->data;
5659
5660 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5661 return 1;
5662 return 0;
5663}
5664
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005665static int perf_tp_event_match(struct perf_event *event,
5666 struct perf_sample_data *data,
5667 struct pt_regs *regs)
5668{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005669 if (event->hw.state & PERF_HES_STOPPED)
5670 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005671 /*
5672 * All tracepoints are from kernel-space.
5673 */
5674 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005675 return 0;
5676
5677 if (!perf_tp_filter_match(event, data))
5678 return 0;
5679
5680 return 1;
5681}
5682
5683void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005684 struct pt_regs *regs, struct hlist_head *head, int rctx,
5685 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005686{
5687 struct perf_sample_data data;
5688 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005689
5690 struct perf_raw_record raw = {
5691 .size = entry_size,
5692 .data = record,
5693 };
5694
Robert Richterfd0d0002012-04-02 20:19:08 +02005695 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005696 data.raw = &raw;
5697
Sasha Levinb67bfe02013-02-27 17:06:00 -08005698 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005699 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005700 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005701 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005702
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005703 /*
5704 * If we got specified a target task, also iterate its context and
5705 * deliver this event there too.
5706 */
5707 if (task && task != current) {
5708 struct perf_event_context *ctx;
5709 struct trace_entry *entry = record;
5710
5711 rcu_read_lock();
5712 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5713 if (!ctx)
5714 goto unlock;
5715
5716 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5717 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5718 continue;
5719 if (event->attr.config != entry->type)
5720 continue;
5721 if (perf_tp_event_match(event, &data, regs))
5722 perf_swevent_event(event, count, &data, regs);
5723 }
5724unlock:
5725 rcu_read_unlock();
5726 }
5727
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005728 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005729}
5730EXPORT_SYMBOL_GPL(perf_tp_event);
5731
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005732static void tp_perf_event_destroy(struct perf_event *event)
5733{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005734 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005735}
5736
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005737static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005738{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005739 int err;
5740
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005741 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5742 return -ENOENT;
5743
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005744 /*
5745 * no branch sampling for tracepoint events
5746 */
5747 if (has_branch_stack(event))
5748 return -EOPNOTSUPP;
5749
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005750 err = perf_trace_init(event);
5751 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005752 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005753
5754 event->destroy = tp_perf_event_destroy;
5755
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005756 return 0;
5757}
5758
5759static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005760 .task_ctx_nr = perf_sw_context,
5761
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005762 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005763 .add = perf_trace_add,
5764 .del = perf_trace_del,
5765 .start = perf_swevent_start,
5766 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005767 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005768
5769 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005770};
5771
5772static inline void perf_tp_register(void)
5773{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005774 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005775}
Li Zefan6fb29152009-10-15 11:21:42 +08005776
5777static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5778{
5779 char *filter_str;
5780 int ret;
5781
5782 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5783 return -EINVAL;
5784
5785 filter_str = strndup_user(arg, PAGE_SIZE);
5786 if (IS_ERR(filter_str))
5787 return PTR_ERR(filter_str);
5788
5789 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5790
5791 kfree(filter_str);
5792 return ret;
5793}
5794
5795static void perf_event_free_filter(struct perf_event *event)
5796{
5797 ftrace_profile_free_filter(event);
5798}
5799
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005800#else
Li Zefan6fb29152009-10-15 11:21:42 +08005801
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005802static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005803{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005804}
Li Zefan6fb29152009-10-15 11:21:42 +08005805
5806static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5807{
5808 return -ENOENT;
5809}
5810
5811static void perf_event_free_filter(struct perf_event *event)
5812{
5813}
5814
Li Zefan07b139c2009-12-21 14:27:35 +08005815#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005816
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005817#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005818void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005819{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005820 struct perf_sample_data sample;
5821 struct pt_regs *regs = data;
5822
Robert Richterfd0d0002012-04-02 20:19:08 +02005823 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005824
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005825 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005826 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005827}
5828#endif
5829
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005830/*
5831 * hrtimer based swevent callback
5832 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005833
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005834static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005835{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005836 enum hrtimer_restart ret = HRTIMER_RESTART;
5837 struct perf_sample_data data;
5838 struct pt_regs *regs;
5839 struct perf_event *event;
5840 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005841
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005842 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005843
5844 if (event->state != PERF_EVENT_STATE_ACTIVE)
5845 return HRTIMER_NORESTART;
5846
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005847 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005848
Robert Richterfd0d0002012-04-02 20:19:08 +02005849 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005850 regs = get_irq_regs();
5851
5852 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08005853 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02005854 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005855 ret = HRTIMER_NORESTART;
5856 }
5857
5858 period = max_t(u64, 10000, event->hw.sample_period);
5859 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5860
5861 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005862}
5863
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005864static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005865{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005866 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005867 s64 period;
5868
5869 if (!is_sampling_event(event))
5870 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005871
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005872 period = local64_read(&hwc->period_left);
5873 if (period) {
5874 if (period < 0)
5875 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005876
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005877 local64_set(&hwc->period_left, 0);
5878 } else {
5879 period = max_t(u64, 10000, hwc->sample_period);
5880 }
5881 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005882 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02005883 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005884}
5885
5886static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5887{
5888 struct hw_perf_event *hwc = &event->hw;
5889
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005890 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005891 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005892 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005893
5894 hrtimer_cancel(&hwc->hrtimer);
5895 }
5896}
5897
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005898static void perf_swevent_init_hrtimer(struct perf_event *event)
5899{
5900 struct hw_perf_event *hwc = &event->hw;
5901
5902 if (!is_sampling_event(event))
5903 return;
5904
5905 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5906 hwc->hrtimer.function = perf_swevent_hrtimer;
5907
5908 /*
5909 * Since hrtimers have a fixed rate, we can do a static freq->period
5910 * mapping and avoid the whole period adjust feedback stuff.
5911 */
5912 if (event->attr.freq) {
5913 long freq = event->attr.sample_freq;
5914
5915 event->attr.sample_period = NSEC_PER_SEC / freq;
5916 hwc->sample_period = event->attr.sample_period;
5917 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09005918 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005919 event->attr.freq = 0;
5920 }
5921}
5922
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005923/*
5924 * Software event: cpu wall time clock
5925 */
5926
5927static void cpu_clock_event_update(struct perf_event *event)
5928{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005929 s64 prev;
5930 u64 now;
5931
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005932 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005933 prev = local64_xchg(&event->hw.prev_count, now);
5934 local64_add(now - prev, &event->count);
5935}
5936
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005937static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005938{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005939 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005940 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005941}
5942
5943static void cpu_clock_event_stop(struct perf_event *event, int flags)
5944{
5945 perf_swevent_cancel_hrtimer(event);
5946 cpu_clock_event_update(event);
5947}
5948
5949static int cpu_clock_event_add(struct perf_event *event, int flags)
5950{
5951 if (flags & PERF_EF_START)
5952 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005953
5954 return 0;
5955}
5956
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005957static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005958{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005959 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005960}
5961
5962static void cpu_clock_event_read(struct perf_event *event)
5963{
5964 cpu_clock_event_update(event);
5965}
5966
5967static int cpu_clock_event_init(struct perf_event *event)
5968{
5969 if (event->attr.type != PERF_TYPE_SOFTWARE)
5970 return -ENOENT;
5971
5972 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5973 return -ENOENT;
5974
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005975 /*
5976 * no branch sampling for software events
5977 */
5978 if (has_branch_stack(event))
5979 return -EOPNOTSUPP;
5980
Peter Zijlstraba3dd362011-02-15 12:41:46 +01005981 perf_swevent_init_hrtimer(event);
5982
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005983 return 0;
5984}
5985
5986static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005987 .task_ctx_nr = perf_sw_context,
5988
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005989 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005990 .add = cpu_clock_event_add,
5991 .del = cpu_clock_event_del,
5992 .start = cpu_clock_event_start,
5993 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005994 .read = cpu_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005995
5996 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005997};
5998
5999/*
6000 * Software event: task time clock
6001 */
6002
6003static void task_clock_event_update(struct perf_event *event, u64 now)
6004{
6005 u64 prev;
6006 s64 delta;
6007
6008 prev = local64_xchg(&event->hw.prev_count, now);
6009 delta = now - prev;
6010 local64_add(delta, &event->count);
6011}
6012
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006013static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006014{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006015 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006016 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006017}
6018
6019static void task_clock_event_stop(struct perf_event *event, int flags)
6020{
6021 perf_swevent_cancel_hrtimer(event);
6022 task_clock_event_update(event, event->ctx->time);
6023}
6024
6025static int task_clock_event_add(struct perf_event *event, int flags)
6026{
6027 if (flags & PERF_EF_START)
6028 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006029
6030 return 0;
6031}
6032
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006033static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006034{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006035 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006036}
6037
6038static void task_clock_event_read(struct perf_event *event)
6039{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01006040 u64 now = perf_clock();
6041 u64 delta = now - event->ctx->timestamp;
6042 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006043
6044 task_clock_event_update(event, time);
6045}
6046
6047static int task_clock_event_init(struct perf_event *event)
6048{
6049 if (event->attr.type != PERF_TYPE_SOFTWARE)
6050 return -ENOENT;
6051
6052 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6053 return -ENOENT;
6054
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006055 /*
6056 * no branch sampling for software events
6057 */
6058 if (has_branch_stack(event))
6059 return -EOPNOTSUPP;
6060
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006061 perf_swevent_init_hrtimer(event);
6062
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006063 return 0;
6064}
6065
6066static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006067 .task_ctx_nr = perf_sw_context,
6068
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006069 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006070 .add = task_clock_event_add,
6071 .del = task_clock_event_del,
6072 .start = task_clock_event_start,
6073 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006074 .read = task_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006075
6076 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006077};
6078
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006079static void perf_pmu_nop_void(struct pmu *pmu)
6080{
6081}
6082
6083static int perf_pmu_nop_int(struct pmu *pmu)
6084{
6085 return 0;
6086}
6087
6088static void perf_pmu_start_txn(struct pmu *pmu)
6089{
6090 perf_pmu_disable(pmu);
6091}
6092
6093static int perf_pmu_commit_txn(struct pmu *pmu)
6094{
6095 perf_pmu_enable(pmu);
6096 return 0;
6097}
6098
6099static void perf_pmu_cancel_txn(struct pmu *pmu)
6100{
6101 perf_pmu_enable(pmu);
6102}
6103
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006104static int perf_event_idx_default(struct perf_event *event)
6105{
6106 return event->hw.idx + 1;
6107}
6108
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006109/*
6110 * Ensures all contexts with the same task_ctx_nr have the same
6111 * pmu_cpu_context too.
6112 */
6113static void *find_pmu_context(int ctxn)
6114{
6115 struct pmu *pmu;
6116
6117 if (ctxn < 0)
6118 return NULL;
6119
6120 list_for_each_entry(pmu, &pmus, entry) {
6121 if (pmu->task_ctx_nr == ctxn)
6122 return pmu->pmu_cpu_context;
6123 }
6124
6125 return NULL;
6126}
6127
Peter Zijlstra51676952010-12-07 14:18:20 +01006128static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006129{
Peter Zijlstra51676952010-12-07 14:18:20 +01006130 int cpu;
6131
6132 for_each_possible_cpu(cpu) {
6133 struct perf_cpu_context *cpuctx;
6134
6135 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6136
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006137 if (cpuctx->unique_pmu == old_pmu)
6138 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01006139 }
6140}
6141
6142static void free_pmu_context(struct pmu *pmu)
6143{
6144 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006145
6146 mutex_lock(&pmus_lock);
6147 /*
6148 * Like a real lame refcount.
6149 */
Peter Zijlstra51676952010-12-07 14:18:20 +01006150 list_for_each_entry(i, &pmus, entry) {
6151 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6152 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006153 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01006154 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006155 }
6156
Peter Zijlstra51676952010-12-07 14:18:20 +01006157 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006158out:
6159 mutex_unlock(&pmus_lock);
6160}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006161static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006162
Peter Zijlstraabe43402010-11-17 23:17:37 +01006163static ssize_t
6164type_show(struct device *dev, struct device_attribute *attr, char *page)
6165{
6166 struct pmu *pmu = dev_get_drvdata(dev);
6167
6168 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6169}
6170
Stephane Eranian62b85632013-04-03 14:21:34 +02006171static ssize_t
6172perf_event_mux_interval_ms_show(struct device *dev,
6173 struct device_attribute *attr,
6174 char *page)
6175{
6176 struct pmu *pmu = dev_get_drvdata(dev);
6177
6178 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6179}
6180
6181static ssize_t
6182perf_event_mux_interval_ms_store(struct device *dev,
6183 struct device_attribute *attr,
6184 const char *buf, size_t count)
6185{
6186 struct pmu *pmu = dev_get_drvdata(dev);
6187 int timer, cpu, ret;
6188
6189 ret = kstrtoint(buf, 0, &timer);
6190 if (ret)
6191 return ret;
6192
6193 if (timer < 1)
6194 return -EINVAL;
6195
6196 /* same value, noting to do */
6197 if (timer == pmu->hrtimer_interval_ms)
6198 return count;
6199
6200 pmu->hrtimer_interval_ms = timer;
6201
6202 /* update all cpuctx for this PMU */
6203 for_each_possible_cpu(cpu) {
6204 struct perf_cpu_context *cpuctx;
6205 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6206 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6207
6208 if (hrtimer_active(&cpuctx->hrtimer))
6209 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6210 }
6211
6212 return count;
6213}
6214
6215#define __ATTR_RW(attr) __ATTR(attr, 0644, attr##_show, attr##_store)
6216
Peter Zijlstraabe43402010-11-17 23:17:37 +01006217static struct device_attribute pmu_dev_attrs[] = {
Stephane Eranian62b85632013-04-03 14:21:34 +02006218 __ATTR_RO(type),
6219 __ATTR_RW(perf_event_mux_interval_ms),
6220 __ATTR_NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006221};
6222
6223static int pmu_bus_running;
6224static struct bus_type pmu_bus = {
6225 .name = "event_source",
6226 .dev_attrs = pmu_dev_attrs,
6227};
6228
6229static void pmu_dev_release(struct device *dev)
6230{
6231 kfree(dev);
6232}
6233
6234static int pmu_dev_alloc(struct pmu *pmu)
6235{
6236 int ret = -ENOMEM;
6237
6238 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6239 if (!pmu->dev)
6240 goto out;
6241
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01006242 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01006243 device_initialize(pmu->dev);
6244 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6245 if (ret)
6246 goto free_dev;
6247
6248 dev_set_drvdata(pmu->dev, pmu);
6249 pmu->dev->bus = &pmu_bus;
6250 pmu->dev->release = pmu_dev_release;
6251 ret = device_add(pmu->dev);
6252 if (ret)
6253 goto free_dev;
6254
6255out:
6256 return ret;
6257
6258free_dev:
6259 put_device(pmu->dev);
6260 goto out;
6261}
6262
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006263static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006264static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006265
Mischa Jonker03d8e802013-06-04 11:45:48 +02006266int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006267{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006268 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006269
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006270 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006271 ret = -ENOMEM;
6272 pmu->pmu_disable_count = alloc_percpu(int);
6273 if (!pmu->pmu_disable_count)
6274 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006275
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006276 pmu->type = -1;
6277 if (!name)
6278 goto skip_type;
6279 pmu->name = name;
6280
6281 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08006282 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6283 if (type < 0) {
6284 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006285 goto free_pdc;
6286 }
6287 }
6288 pmu->type = type;
6289
Peter Zijlstraabe43402010-11-17 23:17:37 +01006290 if (pmu_bus_running) {
6291 ret = pmu_dev_alloc(pmu);
6292 if (ret)
6293 goto free_idr;
6294 }
6295
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006296skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006297 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6298 if (pmu->pmu_cpu_context)
6299 goto got_cpu_context;
6300
Wei Yongjunc4814202013-04-12 11:05:54 +08006301 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006302 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6303 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006304 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006305
6306 for_each_possible_cpu(cpu) {
6307 struct perf_cpu_context *cpuctx;
6308
6309 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006310 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006311 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006312 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006313 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006314 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02006315
6316 __perf_cpu_hrtimer_init(cpuctx, cpu);
6317
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006318 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006319 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006320 }
6321
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006322got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006323 if (!pmu->start_txn) {
6324 if (pmu->pmu_enable) {
6325 /*
6326 * If we have pmu_enable/pmu_disable calls, install
6327 * transaction stubs that use that to try and batch
6328 * hardware accesses.
6329 */
6330 pmu->start_txn = perf_pmu_start_txn;
6331 pmu->commit_txn = perf_pmu_commit_txn;
6332 pmu->cancel_txn = perf_pmu_cancel_txn;
6333 } else {
6334 pmu->start_txn = perf_pmu_nop_void;
6335 pmu->commit_txn = perf_pmu_nop_int;
6336 pmu->cancel_txn = perf_pmu_nop_void;
6337 }
6338 }
6339
6340 if (!pmu->pmu_enable) {
6341 pmu->pmu_enable = perf_pmu_nop_void;
6342 pmu->pmu_disable = perf_pmu_nop_void;
6343 }
6344
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006345 if (!pmu->event_idx)
6346 pmu->event_idx = perf_event_idx_default;
6347
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006348 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006349 ret = 0;
6350unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006351 mutex_unlock(&pmus_lock);
6352
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006353 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006354
Peter Zijlstraabe43402010-11-17 23:17:37 +01006355free_dev:
6356 device_del(pmu->dev);
6357 put_device(pmu->dev);
6358
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006359free_idr:
6360 if (pmu->type >= PERF_TYPE_MAX)
6361 idr_remove(&pmu_idr, pmu->type);
6362
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006363free_pdc:
6364 free_percpu(pmu->pmu_disable_count);
6365 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006366}
6367
6368void perf_pmu_unregister(struct pmu *pmu)
6369{
6370 mutex_lock(&pmus_lock);
6371 list_del_rcu(&pmu->entry);
6372 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006373
6374 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006375 * We dereference the pmu list under both SRCU and regular RCU, so
6376 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006377 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006378 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006379 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006380
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006381 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006382 if (pmu->type >= PERF_TYPE_MAX)
6383 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006384 device_del(pmu->dev);
6385 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006386 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006387}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006388
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006389struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006390{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006391 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006392 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08006393 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006394
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006395 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006396
6397 rcu_read_lock();
6398 pmu = idr_find(&pmu_idr, event->attr.type);
6399 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08006400 if (pmu) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006401 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006402 ret = pmu->event_init(event);
6403 if (ret)
6404 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006405 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08006406 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006407
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006408 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006409 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006410 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006411 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006412 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006413
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006414 if (ret != -ENOENT) {
6415 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006416 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006417 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006418 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006419 pmu = ERR_PTR(-ENOENT);
6420unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006421 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006422
6423 return pmu;
6424}
6425
6426/*
6427 * Allocate and initialize a event structure
6428 */
6429static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006430perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006431 struct task_struct *task,
6432 struct perf_event *group_leader,
6433 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006434 perf_overflow_handler_t overflow_handler,
6435 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006436{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006437 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006438 struct perf_event *event;
6439 struct hw_perf_event *hwc;
6440 long err;
6441
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006442 if ((unsigned)cpu >= nr_cpu_ids) {
6443 if (!task || cpu != -1)
6444 return ERR_PTR(-EINVAL);
6445 }
6446
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006447 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006448 if (!event)
6449 return ERR_PTR(-ENOMEM);
6450
6451 /*
6452 * Single events are their own group leaders, with an
6453 * empty sibling list:
6454 */
6455 if (!group_leader)
6456 group_leader = event;
6457
6458 mutex_init(&event->child_mutex);
6459 INIT_LIST_HEAD(&event->child_list);
6460
6461 INIT_LIST_HEAD(&event->group_entry);
6462 INIT_LIST_HEAD(&event->event_entry);
6463 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006464 INIT_LIST_HEAD(&event->rb_entry);
6465
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006466 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006467 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006468
6469 mutex_init(&event->mmap_mutex);
6470
Al Viroa6fa9412012-08-20 14:59:25 +01006471 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006472 event->cpu = cpu;
6473 event->attr = *attr;
6474 event->group_leader = group_leader;
6475 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006476 event->oncpu = -1;
6477
6478 event->parent = parent_event;
6479
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08006480 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006481 event->id = atomic64_inc_return(&perf_event_id);
6482
6483 event->state = PERF_EVENT_STATE_INACTIVE;
6484
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006485 if (task) {
6486 event->attach_state = PERF_ATTACH_TASK;
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006487
6488 if (attr->type == PERF_TYPE_TRACEPOINT)
6489 event->hw.tp_target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006490#ifdef CONFIG_HAVE_HW_BREAKPOINT
6491 /*
6492 * hw_breakpoint is a bit difficult here..
6493 */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006494 else if (attr->type == PERF_TYPE_BREAKPOINT)
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006495 event->hw.bp_target = task;
6496#endif
6497 }
6498
Avi Kivity4dc0da82011-06-29 18:42:35 +03006499 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006500 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006501 context = parent_event->overflow_handler_context;
6502 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006503
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006504 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006505 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006506
Jiri Olsa0231bb52013-02-01 11:23:45 +01006507 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006508
6509 pmu = NULL;
6510
6511 hwc = &event->hw;
6512 hwc->sample_period = attr->sample_period;
6513 if (attr->freq && attr->sample_freq)
6514 hwc->sample_period = 1;
6515 hwc->last_period = hwc->sample_period;
6516
Peter Zijlstrae7850592010-05-21 14:43:08 +02006517 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006518
6519 /*
6520 * we currently do not support PERF_FORMAT_GROUP on inherited events
6521 */
6522 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6523 goto done;
6524
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006525 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006526
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006527done:
6528 err = 0;
6529 if (!pmu)
6530 err = -EINVAL;
6531 else if (IS_ERR(pmu))
6532 err = PTR_ERR(pmu);
6533
6534 if (err) {
6535 if (event->ns)
6536 put_pid_ns(event->ns);
6537 kfree(event);
6538 return ERR_PTR(err);
6539 }
6540
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006541 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02006542 if (event->attach_state & PERF_ATTACH_TASK)
Ingo Molnarc5905af2012-02-24 08:31:31 +01006543 static_key_slow_inc(&perf_sched_events.key);
Eric B Munson3af9e852010-05-18 15:30:49 +01006544 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006545 atomic_inc(&nr_mmap_events);
6546 if (event->attr.comm)
6547 atomic_inc(&nr_comm_events);
6548 if (event->attr.task)
6549 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006550 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6551 err = get_callchain_buffers();
6552 if (err) {
6553 free_event(event);
6554 return ERR_PTR(err);
6555 }
6556 }
Stephane Eraniand010b332012-02-09 23:21:00 +01006557 if (has_branch_stack(event)) {
6558 static_key_slow_inc(&perf_sched_events.key);
6559 if (!(event->attach_state & PERF_ATTACH_TASK))
6560 atomic_inc(&per_cpu(perf_branch_stack_events,
6561 event->cpu));
6562 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006563 }
6564
6565 return event;
6566}
6567
6568static int perf_copy_attr(struct perf_event_attr __user *uattr,
6569 struct perf_event_attr *attr)
6570{
6571 u32 size;
6572 int ret;
6573
6574 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6575 return -EFAULT;
6576
6577 /*
6578 * zero the full structure, so that a short copy will be nice.
6579 */
6580 memset(attr, 0, sizeof(*attr));
6581
6582 ret = get_user(size, &uattr->size);
6583 if (ret)
6584 return ret;
6585
6586 if (size > PAGE_SIZE) /* silly large */
6587 goto err_size;
6588
6589 if (!size) /* abi compat */
6590 size = PERF_ATTR_SIZE_VER0;
6591
6592 if (size < PERF_ATTR_SIZE_VER0)
6593 goto err_size;
6594
6595 /*
6596 * If we're handed a bigger struct than we know of,
6597 * ensure all the unknown bits are 0 - i.e. new
6598 * user-space does not rely on any kernel feature
6599 * extensions we dont know about yet.
6600 */
6601 if (size > sizeof(*attr)) {
6602 unsigned char __user *addr;
6603 unsigned char __user *end;
6604 unsigned char val;
6605
6606 addr = (void __user *)uattr + sizeof(*attr);
6607 end = (void __user *)uattr + size;
6608
6609 for (; addr < end; addr++) {
6610 ret = get_user(val, addr);
6611 if (ret)
6612 return ret;
6613 if (val)
6614 goto err_size;
6615 }
6616 size = sizeof(*attr);
6617 }
6618
6619 ret = copy_from_user(attr, uattr, size);
6620 if (ret)
6621 return -EFAULT;
6622
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306623 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006624 return -EINVAL;
6625
6626 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6627 return -EINVAL;
6628
6629 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6630 return -EINVAL;
6631
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006632 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6633 u64 mask = attr->branch_sample_type;
6634
6635 /* only using defined bits */
6636 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6637 return -EINVAL;
6638
6639 /* at least one branch bit must be set */
6640 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6641 return -EINVAL;
6642
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006643 /* propagate priv level, when not set for branch */
6644 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6645
6646 /* exclude_kernel checked on syscall entry */
6647 if (!attr->exclude_kernel)
6648 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6649
6650 if (!attr->exclude_user)
6651 mask |= PERF_SAMPLE_BRANCH_USER;
6652
6653 if (!attr->exclude_hv)
6654 mask |= PERF_SAMPLE_BRANCH_HV;
6655 /*
6656 * adjust user setting (for HW filter setup)
6657 */
6658 attr->branch_sample_type = mask;
6659 }
Stephane Eraniane7122092013-06-06 11:02:04 +02006660 /* privileged levels capture (kernel, hv): check permissions */
6661 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02006662 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6663 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006664 }
Jiri Olsa40189942012-08-07 15:20:37 +02006665
Jiri Olsac5ebced2012-08-07 15:20:40 +02006666 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02006667 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006668 if (ret)
6669 return ret;
6670 }
6671
6672 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
6673 if (!arch_perf_have_user_stack_dump())
6674 return -ENOSYS;
6675
6676 /*
6677 * We have __u32 type for the size, but so far
6678 * we can only use __u16 as maximum due to the
6679 * __u16 sample size limit.
6680 */
6681 if (attr->sample_stack_user >= USHRT_MAX)
6682 ret = -EINVAL;
6683 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
6684 ret = -EINVAL;
6685 }
Jiri Olsa40189942012-08-07 15:20:37 +02006686
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006687out:
6688 return ret;
6689
6690err_size:
6691 put_user(sizeof(*attr), &uattr->size);
6692 ret = -E2BIG;
6693 goto out;
6694}
6695
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006696static int
6697perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006698{
Frederic Weisbecker76369132011-05-19 19:55:04 +02006699 struct ring_buffer *rb = NULL, *old_rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006700 int ret = -EINVAL;
6701
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006702 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006703 goto set;
6704
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006705 /* don't allow circular references */
6706 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006707 goto out;
6708
Peter Zijlstra0f139302010-05-20 14:35:15 +02006709 /*
6710 * Don't allow cross-cpu buffers
6711 */
6712 if (output_event->cpu != event->cpu)
6713 goto out;
6714
6715 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02006716 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02006717 */
6718 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6719 goto out;
6720
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006721set:
6722 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006723 /* Can't redirect output if we've got an active mmap() */
6724 if (atomic_read(&event->mmap_count))
6725 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006726
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006727 old_rb = event->rb;
6728
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006729 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02006730 /* get the rb we want to redirect to */
6731 rb = ring_buffer_get(output_event);
6732 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006733 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006734 }
6735
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006736 if (old_rb)
6737 ring_buffer_detach(event, old_rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006738
6739 if (rb)
6740 ring_buffer_attach(event, rb);
6741
6742 rcu_assign_pointer(event->rb, rb);
6743
6744 if (old_rb) {
6745 ring_buffer_put(old_rb);
6746 /*
6747 * Since we detached before setting the new rb, so that we
6748 * could attach the new rb, we could have missed a wakeup.
6749 * Provide it now.
6750 */
6751 wake_up_all(&event->waitq);
6752 }
6753
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006754 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006755unlock:
6756 mutex_unlock(&event->mmap_mutex);
6757
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006758out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006759 return ret;
6760}
6761
6762/**
6763 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6764 *
6765 * @attr_uptr: event_id type attributes for monitoring/sampling
6766 * @pid: target pid
6767 * @cpu: target cpu
6768 * @group_fd: group leader event fd
6769 */
6770SYSCALL_DEFINE5(perf_event_open,
6771 struct perf_event_attr __user *, attr_uptr,
6772 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6773{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006774 struct perf_event *group_leader = NULL, *output_event = NULL;
6775 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006776 struct perf_event_attr attr;
6777 struct perf_event_context *ctx;
6778 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04006779 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07006780 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006781 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04006782 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006783 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006784 int err;
6785
6786 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02006787 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006788 return -EINVAL;
6789
6790 err = perf_copy_attr(attr_uptr, &attr);
6791 if (err)
6792 return err;
6793
6794 if (!attr.exclude_kernel) {
6795 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6796 return -EACCES;
6797 }
6798
6799 if (attr.freq) {
6800 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6801 return -EINVAL;
6802 }
6803
Stephane Eraniane5d13672011-02-14 11:20:01 +02006804 /*
6805 * In cgroup mode, the pid argument is used to pass the fd
6806 * opened to the cgroup directory in cgroupfs. The cpu argument
6807 * designates the cpu on which to monitor threads from that
6808 * cgroup.
6809 */
6810 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6811 return -EINVAL;
6812
Al Viroab72a702012-08-21 09:40:46 -04006813 event_fd = get_unused_fd();
Al Viroea635c62010-05-26 17:40:29 -04006814 if (event_fd < 0)
6815 return event_fd;
6816
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006817 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04006818 err = perf_fget_light(group_fd, &group);
6819 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006820 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04006821 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006822 if (flags & PERF_FLAG_FD_OUTPUT)
6823 output_event = group_leader;
6824 if (flags & PERF_FLAG_FD_NO_GROUP)
6825 group_leader = NULL;
6826 }
6827
Stephane Eraniane5d13672011-02-14 11:20:01 +02006828 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006829 task = find_lively_task_by_vpid(pid);
6830 if (IS_ERR(task)) {
6831 err = PTR_ERR(task);
6832 goto err_group_fd;
6833 }
6834 }
6835
Yan, Zhengfbfc6232012-06-15 14:31:31 +08006836 get_online_cpus();
6837
Avi Kivity4dc0da82011-06-29 18:42:35 +03006838 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
6839 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006840 if (IS_ERR(event)) {
6841 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006842 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006843 }
6844
Stephane Eraniane5d13672011-02-14 11:20:01 +02006845 if (flags & PERF_FLAG_PID_CGROUP) {
6846 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6847 if (err)
6848 goto err_alloc;
Peter Zijlstra08309372011-03-03 11:31:20 +01006849 /*
6850 * one more event:
6851 * - that has cgroup constraint on event->cpu
6852 * - that may need work on context switch
6853 */
6854 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
Ingo Molnarc5905af2012-02-24 08:31:31 +01006855 static_key_slow_inc(&perf_sched_events.key);
Stephane Eraniane5d13672011-02-14 11:20:01 +02006856 }
6857
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006858 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006859 * Special case software events and allow them to be part of
6860 * any hardware group.
6861 */
6862 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006863
6864 if (group_leader &&
6865 (is_software_event(event) != is_software_event(group_leader))) {
6866 if (is_software_event(event)) {
6867 /*
6868 * If event and group_leader are not both a software
6869 * event, and event is, then group leader is not.
6870 *
6871 * Allow the addition of software events to !software
6872 * groups, this is safe because software events never
6873 * fail to schedule.
6874 */
6875 pmu = group_leader->pmu;
6876 } else if (is_software_event(group_leader) &&
6877 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6878 /*
6879 * In case the group is a pure software group, and we
6880 * try to add a hardware event, move the whole group to
6881 * the hardware context.
6882 */
6883 move_group = 1;
6884 }
6885 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006886
6887 /*
6888 * Get the target context (task or percpu):
6889 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006890 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006891 if (IS_ERR(ctx)) {
6892 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006893 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006894 }
6895
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02006896 if (task) {
6897 put_task_struct(task);
6898 task = NULL;
6899 }
6900
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006901 /*
6902 * Look up the group leader (we will attach this event to it):
6903 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006904 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006905 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006906
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006907 /*
6908 * Do not allow a recursive hierarchy (this new sibling
6909 * becoming part of another group-sibling):
6910 */
6911 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006912 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006913 /*
6914 * Do not allow to attach to a group in a different
6915 * task or CPU context:
6916 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006917 if (move_group) {
6918 if (group_leader->ctx->type != ctx->type)
6919 goto err_context;
6920 } else {
6921 if (group_leader->ctx != ctx)
6922 goto err_context;
6923 }
6924
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006925 /*
6926 * Only a group leader can be exclusive or pinned
6927 */
6928 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006929 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006930 }
6931
6932 if (output_event) {
6933 err = perf_event_set_output(event, output_event);
6934 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006935 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006936 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006937
Al Viroea635c62010-05-26 17:40:29 -04006938 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6939 if (IS_ERR(event_file)) {
6940 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006941 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04006942 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006943
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006944 if (move_group) {
6945 struct perf_event_context *gctx = group_leader->ctx;
6946
6947 mutex_lock(&gctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006948 perf_remove_from_context(group_leader);
Jiri Olsa0231bb52013-02-01 11:23:45 +01006949
6950 /*
6951 * Removing from the context ends up with disabled
6952 * event. What we want here is event in the initial
6953 * startup state, ready to be add into new context.
6954 */
6955 perf_event__state_init(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006956 list_for_each_entry(sibling, &group_leader->sibling_list,
6957 group_entry) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006958 perf_remove_from_context(sibling);
Jiri Olsa0231bb52013-02-01 11:23:45 +01006959 perf_event__state_init(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006960 put_ctx(gctx);
6961 }
6962 mutex_unlock(&gctx->mutex);
6963 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006964 }
6965
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006966 WARN_ON_ONCE(ctx->parent_ctx);
6967 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006968
6969 if (move_group) {
Yan, Zheng0cda4c02012-06-15 14:31:33 +08006970 synchronize_rcu();
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006971 perf_install_in_context(ctx, group_leader, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006972 get_ctx(ctx);
6973 list_for_each_entry(sibling, &group_leader->sibling_list,
6974 group_entry) {
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006975 perf_install_in_context(ctx, sibling, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006976 get_ctx(ctx);
6977 }
6978 }
6979
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08006980 perf_install_in_context(ctx, event, event->cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006981 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006982 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006983 mutex_unlock(&ctx->mutex);
6984
Yan, Zhengfbfc6232012-06-15 14:31:31 +08006985 put_online_cpus();
6986
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006987 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01006988
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006989 mutex_lock(&current->perf_event_mutex);
6990 list_add_tail(&event->owner_entry, &current->perf_event_list);
6991 mutex_unlock(&current->perf_event_mutex);
6992
Peter Zijlstra8a495422010-05-27 15:47:49 +02006993 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006994 * Precalculate sample_data sizes
6995 */
6996 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006997 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006998
6999 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02007000 * Drop the reference on the group_event after placing the
7001 * new event on the sibling_list. This ensures destruction
7002 * of the group leader will find the pointer to itself in
7003 * perf_group_detach().
7004 */
Al Viro2903ff02012-08-28 12:52:22 -04007005 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007006 fd_install(event_fd, event_file);
7007 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007008
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007009err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007010 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04007011 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007012err_alloc:
7013 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007014err_task:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007015 put_online_cpus();
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007016 if (task)
7017 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007018err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04007019 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007020err_fd:
7021 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007022 return err;
7023}
7024
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007025/**
7026 * perf_event_create_kernel_counter
7027 *
7028 * @attr: attributes of the counter to create
7029 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07007030 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007031 */
7032struct perf_event *
7033perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07007034 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007035 perf_overflow_handler_t overflow_handler,
7036 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007037{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007038 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007039 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007040 int err;
7041
7042 /*
7043 * Get the target context (task or percpu):
7044 */
7045
Avi Kivity4dc0da82011-06-29 18:42:35 +03007046 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7047 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007048 if (IS_ERR(event)) {
7049 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007050 goto err;
7051 }
7052
Matt Helsley38a81da2010-09-13 13:01:20 -07007053 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007054 if (IS_ERR(ctx)) {
7055 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007056 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007057 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007058
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007059 WARN_ON_ONCE(ctx->parent_ctx);
7060 mutex_lock(&ctx->mutex);
7061 perf_install_in_context(ctx, event, cpu);
7062 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007063 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007064 mutex_unlock(&ctx->mutex);
7065
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007066 return event;
7067
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007068err_free:
7069 free_event(event);
7070err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007071 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007072}
7073EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7074
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007075void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7076{
7077 struct perf_event_context *src_ctx;
7078 struct perf_event_context *dst_ctx;
7079 struct perf_event *event, *tmp;
7080 LIST_HEAD(events);
7081
7082 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7083 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7084
7085 mutex_lock(&src_ctx->mutex);
7086 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7087 event_entry) {
7088 perf_remove_from_context(event);
7089 put_ctx(src_ctx);
7090 list_add(&event->event_entry, &events);
7091 }
7092 mutex_unlock(&src_ctx->mutex);
7093
7094 synchronize_rcu();
7095
7096 mutex_lock(&dst_ctx->mutex);
7097 list_for_each_entry_safe(event, tmp, &events, event_entry) {
7098 list_del(&event->event_entry);
7099 if (event->state >= PERF_EVENT_STATE_OFF)
7100 event->state = PERF_EVENT_STATE_INACTIVE;
7101 perf_install_in_context(dst_ctx, event, dst_cpu);
7102 get_ctx(dst_ctx);
7103 }
7104 mutex_unlock(&dst_ctx->mutex);
7105}
7106EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7107
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007108static void sync_child_event(struct perf_event *child_event,
7109 struct task_struct *child)
7110{
7111 struct perf_event *parent_event = child_event->parent;
7112 u64 child_val;
7113
7114 if (child_event->attr.inherit_stat)
7115 perf_event_read_event(child_event, child);
7116
Peter Zijlstrab5e58792010-05-21 14:43:12 +02007117 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007118
7119 /*
7120 * Add back the child's count to the parent's count:
7121 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02007122 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007123 atomic64_add(child_event->total_time_enabled,
7124 &parent_event->child_total_time_enabled);
7125 atomic64_add(child_event->total_time_running,
7126 &parent_event->child_total_time_running);
7127
7128 /*
7129 * Remove this event from the parent's list
7130 */
7131 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7132 mutex_lock(&parent_event->child_mutex);
7133 list_del_init(&child_event->child_list);
7134 mutex_unlock(&parent_event->child_mutex);
7135
7136 /*
7137 * Release the parent event, if this was the last
7138 * reference to it.
7139 */
Al Viroa6fa9412012-08-20 14:59:25 +01007140 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007141}
7142
7143static void
7144__perf_event_exit_task(struct perf_event *child_event,
7145 struct perf_event_context *child_ctx,
7146 struct task_struct *child)
7147{
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007148 if (child_event->parent) {
7149 raw_spin_lock_irq(&child_ctx->lock);
7150 perf_group_detach(child_event);
7151 raw_spin_unlock_irq(&child_ctx->lock);
7152 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007153
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007154 perf_remove_from_context(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007155
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007156 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007157 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007158 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007159 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007160 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007161 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007162 sync_child_event(child_event, child);
7163 free_event(child_event);
7164 }
7165}
7166
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007167static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007168{
7169 struct perf_event *child_event, *tmp;
7170 struct perf_event_context *child_ctx;
7171 unsigned long flags;
7172
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007173 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007174 perf_event_task(child, NULL, 0);
7175 return;
7176 }
7177
7178 local_irq_save(flags);
7179 /*
7180 * We can't reschedule here because interrupts are disabled,
7181 * and either child is current or it is a task that can't be
7182 * scheduled, so we are now safe from rescheduling changing
7183 * our context.
7184 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01007185 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007186
7187 /*
7188 * Take the context lock here so that if find_get_context is
7189 * reading child->perf_event_ctxp, we wait until it has
7190 * incremented the context's refcount before we do put_ctx below.
7191 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007192 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02007193 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007194 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007195 /*
7196 * If this context is a clone; unclone it so it can't get
7197 * swapped to another process while we're removing all
7198 * the events from it.
7199 */
7200 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01007201 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007202 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007203
7204 /*
7205 * Report the task dead after unscheduling the events so that we
7206 * won't get any samples after PERF_RECORD_EXIT. We can however still
7207 * get a few PERF_RECORD_READ events.
7208 */
7209 perf_event_task(child, child_ctx, 0);
7210
7211 /*
7212 * We can recurse on the same lock type through:
7213 *
7214 * __perf_event_exit_task()
7215 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01007216 * put_event()
7217 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007218 *
7219 * But since its the parent context it won't be the same instance.
7220 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02007221 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007222
7223again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007224 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
7225 group_entry)
7226 __perf_event_exit_task(child_event, child_ctx, child);
7227
7228 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007229 group_entry)
7230 __perf_event_exit_task(child_event, child_ctx, child);
7231
7232 /*
7233 * If the last event was a group event, it will have appended all
7234 * its siblings to the list, but we obtained 'tmp' before that which
7235 * will still point to the list head terminating the iteration.
7236 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007237 if (!list_empty(&child_ctx->pinned_groups) ||
7238 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007239 goto again;
7240
7241 mutex_unlock(&child_ctx->mutex);
7242
7243 put_ctx(child_ctx);
7244}
7245
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007246/*
7247 * When a child task exits, feed back event values to parent events.
7248 */
7249void perf_event_exit_task(struct task_struct *child)
7250{
Peter Zijlstra88821352010-11-09 19:01:43 +01007251 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007252 int ctxn;
7253
Peter Zijlstra88821352010-11-09 19:01:43 +01007254 mutex_lock(&child->perf_event_mutex);
7255 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
7256 owner_entry) {
7257 list_del_init(&event->owner_entry);
7258
7259 /*
7260 * Ensure the list deletion is visible before we clear
7261 * the owner, closes a race against perf_release() where
7262 * we need to serialize on the owner->perf_event_mutex.
7263 */
7264 smp_wmb();
7265 event->owner = NULL;
7266 }
7267 mutex_unlock(&child->perf_event_mutex);
7268
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007269 for_each_task_context_nr(ctxn)
7270 perf_event_exit_task_context(child, ctxn);
7271}
7272
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007273static void perf_free_event(struct perf_event *event,
7274 struct perf_event_context *ctx)
7275{
7276 struct perf_event *parent = event->parent;
7277
7278 if (WARN_ON_ONCE(!parent))
7279 return;
7280
7281 mutex_lock(&parent->child_mutex);
7282 list_del_init(&event->child_list);
7283 mutex_unlock(&parent->child_mutex);
7284
Al Viroa6fa9412012-08-20 14:59:25 +01007285 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007286
Peter Zijlstra8a495422010-05-27 15:47:49 +02007287 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007288 list_del_event(event, ctx);
7289 free_event(event);
7290}
7291
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007292/*
7293 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007294 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007295 */
7296void perf_event_free_task(struct task_struct *task)
7297{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007298 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007299 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007300 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007301
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007302 for_each_task_context_nr(ctxn) {
7303 ctx = task->perf_event_ctxp[ctxn];
7304 if (!ctx)
7305 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007306
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007307 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007308again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007309 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7310 group_entry)
7311 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007312
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007313 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7314 group_entry)
7315 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007316
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007317 if (!list_empty(&ctx->pinned_groups) ||
7318 !list_empty(&ctx->flexible_groups))
7319 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007320
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007321 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007322
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007323 put_ctx(ctx);
7324 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007325}
7326
Peter Zijlstra4e231c72010-09-09 21:01:59 +02007327void perf_event_delayed_put(struct task_struct *task)
7328{
7329 int ctxn;
7330
7331 for_each_task_context_nr(ctxn)
7332 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7333}
7334
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007335/*
7336 * inherit a event from parent task to child task:
7337 */
7338static struct perf_event *
7339inherit_event(struct perf_event *parent_event,
7340 struct task_struct *parent,
7341 struct perf_event_context *parent_ctx,
7342 struct task_struct *child,
7343 struct perf_event *group_leader,
7344 struct perf_event_context *child_ctx)
7345{
7346 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02007347 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007348
7349 /*
7350 * Instead of creating recursive hierarchies of events,
7351 * we link inherited events back to the original parent,
7352 * which has a filp for sure, which we use as the reference
7353 * count:
7354 */
7355 if (parent_event->parent)
7356 parent_event = parent_event->parent;
7357
7358 child_event = perf_event_alloc(&parent_event->attr,
7359 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007360 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007361 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007362 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007363 if (IS_ERR(child_event))
7364 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01007365
7366 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7367 free_event(child_event);
7368 return NULL;
7369 }
7370
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007371 get_ctx(child_ctx);
7372
7373 /*
7374 * Make the child state follow the state of the parent event,
7375 * not its attr.disabled bit. We hold the parent's mutex,
7376 * so we won't race with perf_event_{en, dis}able_family.
7377 */
7378 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7379 child_event->state = PERF_EVENT_STATE_INACTIVE;
7380 else
7381 child_event->state = PERF_EVENT_STATE_OFF;
7382
7383 if (parent_event->attr.freq) {
7384 u64 sample_period = parent_event->hw.sample_period;
7385 struct hw_perf_event *hwc = &child_event->hw;
7386
7387 hwc->sample_period = sample_period;
7388 hwc->last_period = sample_period;
7389
7390 local64_set(&hwc->period_left, sample_period);
7391 }
7392
7393 child_event->ctx = child_ctx;
7394 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007395 child_event->overflow_handler_context
7396 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007397
7398 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02007399 * Precalculate sample_data sizes
7400 */
7401 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007402 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02007403
7404 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007405 * Link it up in the child's context:
7406 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02007407 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007408 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02007409 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007410
7411 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007412 * Link this into the parent event's child list
7413 */
7414 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7415 mutex_lock(&parent_event->child_mutex);
7416 list_add_tail(&child_event->child_list, &parent_event->child_list);
7417 mutex_unlock(&parent_event->child_mutex);
7418
7419 return child_event;
7420}
7421
7422static int inherit_group(struct perf_event *parent_event,
7423 struct task_struct *parent,
7424 struct perf_event_context *parent_ctx,
7425 struct task_struct *child,
7426 struct perf_event_context *child_ctx)
7427{
7428 struct perf_event *leader;
7429 struct perf_event *sub;
7430 struct perf_event *child_ctr;
7431
7432 leader = inherit_event(parent_event, parent, parent_ctx,
7433 child, NULL, child_ctx);
7434 if (IS_ERR(leader))
7435 return PTR_ERR(leader);
7436 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7437 child_ctr = inherit_event(sub, parent, parent_ctx,
7438 child, leader, child_ctx);
7439 if (IS_ERR(child_ctr))
7440 return PTR_ERR(child_ctr);
7441 }
7442 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007443}
7444
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007445static int
7446inherit_task_group(struct perf_event *event, struct task_struct *parent,
7447 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007448 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007449 int *inherited_all)
7450{
7451 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007452 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007453
7454 if (!event->attr.inherit) {
7455 *inherited_all = 0;
7456 return 0;
7457 }
7458
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007459 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007460 if (!child_ctx) {
7461 /*
7462 * This is executed from the parent task context, so
7463 * inherit events that have been marked for cloning.
7464 * First allocate and initialize a context for the
7465 * child.
7466 */
7467
Peter Zijlstraeb184472010-09-07 15:55:13 +02007468 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007469 if (!child_ctx)
7470 return -ENOMEM;
7471
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007472 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007473 }
7474
7475 ret = inherit_group(event, parent, parent_ctx,
7476 child, child_ctx);
7477
7478 if (ret)
7479 *inherited_all = 0;
7480
7481 return ret;
7482}
7483
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007484/*
7485 * Initialize the perf_event context in task_struct
7486 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007487int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007488{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007489 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007490 struct perf_event_context *cloned_ctx;
7491 struct perf_event *event;
7492 struct task_struct *parent = current;
7493 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007494 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007495 int ret = 0;
7496
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007497 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007498 return 0;
7499
7500 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007501 * If the parent's context is a clone, pin it so it won't get
7502 * swapped under us.
7503 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007504 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007505
7506 /*
7507 * No need to check if parent_ctx != NULL here; since we saw
7508 * it non-NULL earlier, the only reason for it to become NULL
7509 * is if we exit, and since we're currently in the middle of
7510 * a fork we can't be exiting at the same time.
7511 */
7512
7513 /*
7514 * Lock the parent list. No need to lock the child - not PID
7515 * hashed yet and not running, so nobody can access it.
7516 */
7517 mutex_lock(&parent_ctx->mutex);
7518
7519 /*
7520 * We dont have to disable NMIs - we are only looking at
7521 * the list, not manipulating it:
7522 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007523 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007524 ret = inherit_task_group(event, parent, parent_ctx,
7525 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007526 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007527 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007528 }
7529
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007530 /*
7531 * We can't hold ctx->lock when iterating the ->flexible_group list due
7532 * to allocations, but we need to prevent rotation because
7533 * rotate_ctx() will change the list from interrupt context.
7534 */
7535 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7536 parent_ctx->rotate_disable = 1;
7537 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7538
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007539 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007540 ret = inherit_task_group(event, parent, parent_ctx,
7541 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007542 if (ret)
7543 break;
7544 }
7545
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007546 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7547 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007548
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007549 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007550
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01007551 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007552 /*
7553 * Mark the child context as a clone of the parent
7554 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007555 *
7556 * Note that if the parent is a clone, the holding of
7557 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007558 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007559 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007560 if (cloned_ctx) {
7561 child_ctx->parent_ctx = cloned_ctx;
7562 child_ctx->parent_gen = parent_ctx->parent_gen;
7563 } else {
7564 child_ctx->parent_ctx = parent_ctx;
7565 child_ctx->parent_gen = parent_ctx->generation;
7566 }
7567 get_ctx(child_ctx->parent_ctx);
7568 }
7569
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007570 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007571 mutex_unlock(&parent_ctx->mutex);
7572
7573 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007574 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007575
7576 return ret;
7577}
7578
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007579/*
7580 * Initialize the perf_event context in task_struct
7581 */
7582int perf_event_init_task(struct task_struct *child)
7583{
7584 int ctxn, ret;
7585
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01007586 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7587 mutex_init(&child->perf_event_mutex);
7588 INIT_LIST_HEAD(&child->perf_event_list);
7589
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007590 for_each_task_context_nr(ctxn) {
7591 ret = perf_event_init_context(child, ctxn);
7592 if (ret)
7593 return ret;
7594 }
7595
7596 return 0;
7597}
7598
Paul Mackerras220b1402010-03-10 20:45:52 +11007599static void __init perf_event_init_all_cpus(void)
7600{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007601 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11007602 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11007603
7604 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007605 swhash = &per_cpu(swevent_htable, cpu);
7606 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007607 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11007608 }
7609}
7610
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007611static void __cpuinit perf_event_init_cpu(int cpu)
7612{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007613 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007614
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007615 mutex_lock(&swhash->hlist_mutex);
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007616 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007617 struct swevent_hlist *hlist;
7618
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007619 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7620 WARN_ON(!hlist);
7621 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007622 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007623 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007624}
7625
Peter Zijlstrac2774432010-12-08 15:29:02 +01007626#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007627static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007628{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007629 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7630
7631 WARN_ON(!irqs_disabled());
7632
7633 list_del_init(&cpuctx->rotation_list);
7634}
7635
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007636static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007637{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007638 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007639 struct perf_event *event, *tmp;
7640
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007641 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02007642
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007643 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007644 __perf_remove_from_context(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007645 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007646 __perf_remove_from_context(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007647}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007648
7649static void perf_event_exit_cpu_context(int cpu)
7650{
7651 struct perf_event_context *ctx;
7652 struct pmu *pmu;
7653 int idx;
7654
7655 idx = srcu_read_lock(&pmus_srcu);
7656 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02007657 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007658
7659 mutex_lock(&ctx->mutex);
7660 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7661 mutex_unlock(&ctx->mutex);
7662 }
7663 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007664}
7665
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007666static void perf_event_exit_cpu(int cpu)
7667{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007668 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007669
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007670 mutex_lock(&swhash->hlist_mutex);
7671 swevent_hlist_release(swhash);
7672 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007673
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007674 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007675}
7676#else
7677static inline void perf_event_exit_cpu(int cpu) { }
7678#endif
7679
Peter Zijlstrac2774432010-12-08 15:29:02 +01007680static int
7681perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7682{
7683 int cpu;
7684
7685 for_each_online_cpu(cpu)
7686 perf_event_exit_cpu(cpu);
7687
7688 return NOTIFY_OK;
7689}
7690
7691/*
7692 * Run the perf reboot notifier at the very last possible moment so that
7693 * the generic watchdog code runs as long as possible.
7694 */
7695static struct notifier_block perf_reboot_notifier = {
7696 .notifier_call = perf_reboot,
7697 .priority = INT_MIN,
7698};
7699
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007700static int __cpuinit
7701perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7702{
7703 unsigned int cpu = (long)hcpu;
7704
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007705 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007706
7707 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02007708 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007709 perf_event_init_cpu(cpu);
7710 break;
7711
Peter Zijlstra5e116372010-06-11 13:35:08 +02007712 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007713 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007714 perf_event_exit_cpu(cpu);
7715 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007716 default:
7717 break;
7718 }
7719
7720 return NOTIFY_OK;
7721}
7722
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007723void __init perf_event_init(void)
7724{
Jason Wessel3c502e72010-11-04 17:33:01 -05007725 int ret;
7726
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007727 idr_init(&pmu_idr);
7728
Paul Mackerras220b1402010-03-10 20:45:52 +11007729 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007730 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007731 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7732 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7733 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007734 perf_tp_register();
7735 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01007736 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05007737
7738 ret = init_hw_breakpoint();
7739 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02007740
7741 /* do not patch jump label more than once per second */
7742 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01007743
7744 /*
7745 * Build time assertion that we keep the data_head at the intended
7746 * location. IOW, validation we got the __reserved[] size right.
7747 */
7748 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
7749 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007750}
Peter Zijlstraabe43402010-11-17 23:17:37 +01007751
7752static int __init perf_event_sysfs_init(void)
7753{
7754 struct pmu *pmu;
7755 int ret;
7756
7757 mutex_lock(&pmus_lock);
7758
7759 ret = bus_register(&pmu_bus);
7760 if (ret)
7761 goto unlock;
7762
7763 list_for_each_entry(pmu, &pmus, entry) {
7764 if (!pmu->name || pmu->type < 0)
7765 continue;
7766
7767 ret = pmu_dev_alloc(pmu);
7768 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7769 }
7770 pmu_bus_running = 1;
7771 ret = 0;
7772
7773unlock:
7774 mutex_unlock(&pmus_lock);
7775
7776 return ret;
7777}
7778device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007779
7780#ifdef CONFIG_CGROUP_PERF
Tejun Heo92fb9742012-11-19 08:13:38 -08007781static struct cgroup_subsys_state *perf_cgroup_css_alloc(struct cgroup *cont)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007782{
7783 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02007784
Li Zefan1b15d052011-03-03 14:26:06 +08007785 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007786 if (!jc)
7787 return ERR_PTR(-ENOMEM);
7788
Stephane Eraniane5d13672011-02-14 11:20:01 +02007789 jc->info = alloc_percpu(struct perf_cgroup_info);
7790 if (!jc->info) {
7791 kfree(jc);
7792 return ERR_PTR(-ENOMEM);
7793 }
7794
Stephane Eraniane5d13672011-02-14 11:20:01 +02007795 return &jc->css;
7796}
7797
Tejun Heo92fb9742012-11-19 08:13:38 -08007798static void perf_cgroup_css_free(struct cgroup *cont)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007799{
7800 struct perf_cgroup *jc;
7801 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7802 struct perf_cgroup, css);
7803 free_percpu(jc->info);
7804 kfree(jc);
7805}
7806
7807static int __perf_cgroup_move(void *info)
7808{
7809 struct task_struct *task = info;
7810 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7811 return 0;
7812}
7813
Li Zefan761b3ef2012-01-31 13:47:36 +08007814static void perf_cgroup_attach(struct cgroup *cgrp, struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007815{
Tejun Heobb9d97b2011-12-12 18:12:21 -08007816 struct task_struct *task;
7817
7818 cgroup_taskset_for_each(task, cgrp, tset)
7819 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007820}
7821
Li Zefan761b3ef2012-01-31 13:47:36 +08007822static void perf_cgroup_exit(struct cgroup *cgrp, struct cgroup *old_cgrp,
7823 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007824{
7825 /*
7826 * cgroup_exit() is called in the copy_process() failure path.
7827 * Ignore this case since the task hasn't ran yet, this avoids
7828 * trying to poke a half freed task state from generic code.
7829 */
7830 if (!(task->flags & PF_EXITING))
7831 return;
7832
Tejun Heobb9d97b2011-12-12 18:12:21 -08007833 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007834}
7835
7836struct cgroup_subsys perf_subsys = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +02007837 .name = "perf_event",
7838 .subsys_id = perf_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08007839 .css_alloc = perf_cgroup_css_alloc,
7840 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02007841 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08007842 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02007843};
7844#endif /* CONFIG_CGROUP_PERF */