blob: 1425d07018de50987b5373dec88bff3438787426 [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
Ingo Molnare7e7ee22011-05-04 08:42:29 +02005 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Al Virod36b6912011-12-29 17:09:01 -05007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008 *
Ingo Molnar57c0c152009-09-21 12:20:38 +02009 * For licensing details see kernel-base/COPYING
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010 */
11
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include <linux/cpu.h>
15#include <linux/smp.h>
Peter Zijlstra2e80a822010-11-17 23:17:36 +010016#include <linux/idr.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/file.h>
18#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020020#include <linux/hash.h>
Frederic Weisbecker12351ef2013-04-20 15:48:22 +020021#include <linux/tick.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020022#include <linux/sysfs.h>
23#include <linux/dcache.h>
24#include <linux/percpu.h>
25#include <linux/ptrace.h>
Peter Zijlstrac2774432010-12-08 15:29:02 +010026#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020027#include <linux/vmstat.h>
Peter Zijlstraabe43402010-11-17 23:17:37 +010028#include <linux/device.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040029#include <linux/export.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020030#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020031#include <linux/hardirq.h>
32#include <linux/rculist.h>
33#include <linux/uaccess.h>
34#include <linux/syscalls.h>
35#include <linux/anon_inodes.h>
36#include <linux/kernel_stat.h>
37#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080038#include <linux/ftrace_event.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050039#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020040#include <linux/mm_types.h>
Li Zefan877c6852013-03-05 11:38:08 +080041#include <linux/cgroup.h>
Yan, Zhengc464c762014-03-18 16:56:41 +080042#include <linux/module.h>
Peter Zijlstraf972eb62014-05-19 15:13:47 -040043#include <linux/mman.h>
Pawel Mollb3f20782014-06-13 16:03:32 +010044#include <linux/compat.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020045
Frederic Weisbecker76369132011-05-19 19:55:04 +020046#include "internal.h"
47
Ingo Molnarcdd6c482009-09-21 12:02:48 +020048#include <asm/irq_regs.h>
49
Jiri Olsafadfe7b2014-08-01 14:33:02 +020050static struct workqueue_struct *perf_wq;
51
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010052struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020053 struct task_struct *p;
54 int (*func)(void *info);
55 void *info;
56 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010057};
58
59static void remote_function(void *data)
60{
61 struct remote_function_call *tfc = data;
62 struct task_struct *p = tfc->p;
63
64 if (p) {
65 tfc->ret = -EAGAIN;
66 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
67 return;
68 }
69
70 tfc->ret = tfc->func(tfc->info);
71}
72
73/**
74 * task_function_call - call a function on the cpu on which a task runs
75 * @p: the task to evaluate
76 * @func: the function to be called
77 * @info: the function call argument
78 *
79 * Calls the function @func when the task is currently running. This might
80 * be on the current CPU, which just calls the function directly
81 *
82 * returns: @func return value, or
83 * -ESRCH - when the process isn't running
84 * -EAGAIN - when the process moved away
85 */
86static int
87task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
88{
89 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020090 .p = p,
91 .func = func,
92 .info = info,
93 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010094 };
95
96 if (task_curr(p))
97 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
98
99 return data.ret;
100}
101
102/**
103 * cpu_function_call - call a function on the cpu
104 * @func: the function to be called
105 * @info: the function call argument
106 *
107 * Calls the function @func on the remote cpu.
108 *
109 * returns: @func return value or -ENXIO when the cpu is offline
110 */
111static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
112{
113 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200114 .p = NULL,
115 .func = func,
116 .info = info,
117 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100118 };
119
120 smp_call_function_single(cpu, remote_function, &data, 1);
121
122 return data.ret;
123}
124
Jiri Olsaf8697762014-08-01 14:33:01 +0200125#define EVENT_OWNER_KERNEL ((void *) -1)
126
127static bool is_kernel_event(struct perf_event *event)
128{
129 return event->owner == EVENT_OWNER_KERNEL;
130}
131
Stephane Eraniane5d13672011-02-14 11:20:01 +0200132#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
133 PERF_FLAG_FD_OUTPUT |\
Yann Droneauda21b0b32014-01-05 21:36:33 +0100134 PERF_FLAG_PID_CGROUP |\
135 PERF_FLAG_FD_CLOEXEC)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200136
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100137/*
138 * branch priv levels that need permission checks
139 */
140#define PERF_SAMPLE_BRANCH_PERM_PLM \
141 (PERF_SAMPLE_BRANCH_KERNEL |\
142 PERF_SAMPLE_BRANCH_HV)
143
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200144enum event_type_t {
145 EVENT_FLEXIBLE = 0x1,
146 EVENT_PINNED = 0x2,
147 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
148};
149
Stephane Eraniane5d13672011-02-14 11:20:01 +0200150/*
151 * perf_sched_events : >0 events exist
152 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
153 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100154struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200155static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Stephane Eraniand010b332012-02-09 23:21:00 +0100156static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200157
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200158static atomic_t nr_mmap_events __read_mostly;
159static atomic_t nr_comm_events __read_mostly;
160static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200161static atomic_t nr_freq_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200162
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200163static LIST_HEAD(pmus);
164static DEFINE_MUTEX(pmus_lock);
165static struct srcu_struct pmus_srcu;
166
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200167/*
168 * perf event paranoia level:
169 * -1 - not paranoid at all
170 * 0 - disallow raw tracepoint access for unpriv
171 * 1 - disallow cpu events for unpriv
172 * 2 - disallow kernel profiling for unpriv
173 */
174int sysctl_perf_event_paranoid __read_mostly = 1;
175
Frederic Weisbecker20443382011-03-31 03:33:29 +0200176/* Minimum for 512 kiB + 1 user control page */
177int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200178
179/*
180 * max perf event sample rate
181 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700182#define DEFAULT_MAX_SAMPLE_RATE 100000
183#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
184#define DEFAULT_CPU_TIME_MAX_PERCENT 25
185
186int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
187
188static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
189static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
190
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200191static int perf_sample_allowed_ns __read_mostly =
192 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700193
194void update_perf_cpu_limits(void)
195{
196 u64 tmp = perf_sample_period_ns;
197
198 tmp *= sysctl_perf_cpu_time_max_percent;
Stephane Eraniane5302922013-07-05 00:30:11 +0200199 do_div(tmp, 100);
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200200 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
Dave Hansen14c63f12013-06-21 08:51:36 -0700201}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100202
Stephane Eranian9e630202013-04-03 14:21:33 +0200203static int perf_rotate_context(struct perf_cpu_context *cpuctx);
204
Peter Zijlstra163ec432011-02-16 11:22:34 +0100205int perf_proc_update_handler(struct ctl_table *table, int write,
206 void __user *buffer, size_t *lenp,
207 loff_t *ppos)
208{
Knut Petersen723478c2013-09-25 14:29:37 +0200209 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100210
211 if (ret || !write)
212 return ret;
213
214 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700215 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
216 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100217
218 return 0;
219}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200220
Dave Hansen14c63f12013-06-21 08:51:36 -0700221int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
222
223int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
224 void __user *buffer, size_t *lenp,
225 loff_t *ppos)
226{
227 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
228
229 if (ret || !write)
230 return ret;
231
232 update_perf_cpu_limits();
233
234 return 0;
235}
236
237/*
238 * perf samples are done in some very critical code paths (NMIs).
239 * If they take too much CPU time, the system can lock up and not
240 * get any real work done. This will drop the sample rate when
241 * we detect that events are taking too long.
242 */
243#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200244static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700245
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100246static void perf_duration_warn(struct irq_work *w)
Dave Hansen14c63f12013-06-21 08:51:36 -0700247{
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100248 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Dave Hansen14c63f12013-06-21 08:51:36 -0700249 u64 avg_local_sample_len;
Stephane Eraniane5302922013-07-05 00:30:11 +0200250 u64 local_samples_len;
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100251
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500252 local_samples_len = __this_cpu_read(running_sample_length);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100253 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
254
255 printk_ratelimited(KERN_WARNING
256 "perf interrupt took too long (%lld > %lld), lowering "
257 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100258 avg_local_sample_len, allowed_ns >> 1,
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100259 sysctl_perf_event_sample_rate);
260}
261
262static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
263
264void perf_sample_event_took(u64 sample_len_ns)
265{
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200266 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100267 u64 avg_local_sample_len;
268 u64 local_samples_len;
Dave Hansen14c63f12013-06-21 08:51:36 -0700269
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200270 if (allowed_ns == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700271 return;
272
273 /* decay the counter by 1 average sample */
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500274 local_samples_len = __this_cpu_read(running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700275 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
276 local_samples_len += sample_len_ns;
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500277 __this_cpu_write(running_sample_length, local_samples_len);
Dave Hansen14c63f12013-06-21 08:51:36 -0700278
279 /*
280 * note: this will be biased artifically low until we have
281 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
282 * from having to maintain a count.
283 */
284 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
285
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200286 if (avg_local_sample_len <= allowed_ns)
Dave Hansen14c63f12013-06-21 08:51:36 -0700287 return;
288
289 if (max_samples_per_tick <= 1)
290 return;
291
292 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
293 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
294 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
295
Dave Hansen14c63f12013-06-21 08:51:36 -0700296 update_perf_cpu_limits();
Peter Zijlstra6a02ad662014-02-03 18:11:08 +0100297
Peter Zijlstracd578ab2014-02-11 16:01:16 +0100298 if (!irq_work_queue(&perf_duration_work)) {
299 early_printk("perf interrupt took too long (%lld > %lld), lowering "
300 "kernel.perf_event_max_sample_rate to %d\n",
301 avg_local_sample_len, allowed_ns >> 1,
302 sysctl_perf_event_sample_rate);
303 }
Dave Hansen14c63f12013-06-21 08:51:36 -0700304}
305
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200306static atomic64_t perf_event_id;
307
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200308static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
309 enum event_type_t event_type);
310
311static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200312 enum event_type_t event_type,
313 struct task_struct *task);
314
315static void update_context_time(struct perf_event_context *ctx);
316static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200317
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200318void __weak perf_event_print_debug(void) { }
319
Matt Fleming84c79912010-10-03 21:41:13 +0100320extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200321{
Matt Fleming84c79912010-10-03 21:41:13 +0100322 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200323}
324
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200325static inline u64 perf_clock(void)
326{
327 return local_clock();
328}
329
Stephane Eraniane5d13672011-02-14 11:20:01 +0200330static inline struct perf_cpu_context *
331__get_cpu_context(struct perf_event_context *ctx)
332{
333 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
334}
335
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200336static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
337 struct perf_event_context *ctx)
338{
339 raw_spin_lock(&cpuctx->ctx.lock);
340 if (ctx)
341 raw_spin_lock(&ctx->lock);
342}
343
344static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
345 struct perf_event_context *ctx)
346{
347 if (ctx)
348 raw_spin_unlock(&ctx->lock);
349 raw_spin_unlock(&cpuctx->ctx.lock);
350}
351
Stephane Eraniane5d13672011-02-14 11:20:01 +0200352#ifdef CONFIG_CGROUP_PERF
353
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200354/*
Li Zefan877c6852013-03-05 11:38:08 +0800355 * perf_cgroup_info keeps track of time_enabled for a cgroup.
356 * This is a per-cpu dynamically allocated data structure.
357 */
358struct perf_cgroup_info {
359 u64 time;
360 u64 timestamp;
361};
362
363struct perf_cgroup {
364 struct cgroup_subsys_state css;
Namhyung Kim86e213e2013-03-18 18:56:34 +0900365 struct perf_cgroup_info __percpu *info;
Li Zefan877c6852013-03-05 11:38:08 +0800366};
367
368/*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200369 * Must ensure cgroup is pinned (css_get) before calling
370 * this function. In other words, we cannot call this function
371 * if there is no cgroup event for the current CPU context.
372 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200373static inline struct perf_cgroup *
374perf_cgroup_from_task(struct task_struct *task)
375{
Tejun Heo073219e2014-02-08 10:36:58 -0500376 return container_of(task_css(task, perf_event_cgrp_id),
Tejun Heo8af01f52013-08-08 20:11:22 -0400377 struct perf_cgroup, css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200378}
379
380static inline bool
381perf_cgroup_match(struct perf_event *event)
382{
383 struct perf_event_context *ctx = event->ctx;
384 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
385
Tejun Heoef824fa2013-04-08 19:00:38 -0700386 /* @event doesn't care about cgroup */
387 if (!event->cgrp)
388 return true;
389
390 /* wants specific cgroup scope but @cpuctx isn't associated with any */
391 if (!cpuctx->cgrp)
392 return false;
393
394 /*
395 * Cgroup scoping is recursive. An event enabled for a cgroup is
396 * also enabled for all its descendant cgroups. If @cpuctx's
397 * cgroup is a descendant of @event's (the test covers identity
398 * case), it's a match.
399 */
400 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
401 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200402}
403
Stephane Eraniane5d13672011-02-14 11:20:01 +0200404static inline void perf_detach_cgroup(struct perf_event *event)
405{
Zefan Li4e2ba652014-09-19 16:53:14 +0800406 css_put(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200407 event->cgrp = NULL;
408}
409
410static inline int is_cgroup_event(struct perf_event *event)
411{
412 return event->cgrp != NULL;
413}
414
415static inline u64 perf_cgroup_event_time(struct perf_event *event)
416{
417 struct perf_cgroup_info *t;
418
419 t = per_cpu_ptr(event->cgrp->info, event->cpu);
420 return t->time;
421}
422
423static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
424{
425 struct perf_cgroup_info *info;
426 u64 now;
427
428 now = perf_clock();
429
430 info = this_cpu_ptr(cgrp->info);
431
432 info->time += now - info->timestamp;
433 info->timestamp = now;
434}
435
436static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
437{
438 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
439 if (cgrp_out)
440 __update_cgrp_time(cgrp_out);
441}
442
443static inline void update_cgrp_time_from_event(struct perf_event *event)
444{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200445 struct perf_cgroup *cgrp;
446
Stephane Eraniane5d13672011-02-14 11:20:01 +0200447 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200448 * ensure we access cgroup data only when needed and
449 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200450 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200451 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200452 return;
453
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200454 cgrp = perf_cgroup_from_task(current);
455 /*
456 * Do not update time when cgroup is not active
457 */
458 if (cgrp == event->cgrp)
459 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200460}
461
462static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200463perf_cgroup_set_timestamp(struct task_struct *task,
464 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200465{
466 struct perf_cgroup *cgrp;
467 struct perf_cgroup_info *info;
468
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200469 /*
470 * ctx->lock held by caller
471 * ensure we do not access cgroup data
472 * unless we have the cgroup pinned (css_get)
473 */
474 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200475 return;
476
477 cgrp = perf_cgroup_from_task(task);
478 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200479 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200480}
481
482#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
483#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
484
485/*
486 * reschedule events based on the cgroup constraint of task.
487 *
488 * mode SWOUT : schedule out everything
489 * mode SWIN : schedule in based on cgroup for next
490 */
491void perf_cgroup_switch(struct task_struct *task, int mode)
492{
493 struct perf_cpu_context *cpuctx;
494 struct pmu *pmu;
495 unsigned long flags;
496
497 /*
498 * disable interrupts to avoid geting nr_cgroup
499 * changes via __perf_event_disable(). Also
500 * avoids preemption.
501 */
502 local_irq_save(flags);
503
504 /*
505 * we reschedule only in the presence of cgroup
506 * constrained events.
507 */
508 rcu_read_lock();
509
510 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200511 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200512 if (cpuctx->unique_pmu != pmu)
513 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200514
Stephane Eraniane5d13672011-02-14 11:20:01 +0200515 /*
516 * perf_cgroup_events says at least one
517 * context on this CPU has cgroup events.
518 *
519 * ctx->nr_cgroups reports the number of cgroup
520 * events for a context.
521 */
522 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200523 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
524 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200525
526 if (mode & PERF_CGROUP_SWOUT) {
527 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
528 /*
529 * must not be done before ctxswout due
530 * to event_filter_match() in event_sched_out()
531 */
532 cpuctx->cgrp = NULL;
533 }
534
535 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200536 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200537 /*
538 * set cgrp before ctxsw in to allow
539 * event_filter_match() to not have to pass
540 * task around
Stephane Eraniane5d13672011-02-14 11:20:01 +0200541 */
542 cpuctx->cgrp = perf_cgroup_from_task(task);
543 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
544 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200545 perf_pmu_enable(cpuctx->ctx.pmu);
546 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200547 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200548 }
549
550 rcu_read_unlock();
551
552 local_irq_restore(flags);
553}
554
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200555static inline void perf_cgroup_sched_out(struct task_struct *task,
556 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200557{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200558 struct perf_cgroup *cgrp1;
559 struct perf_cgroup *cgrp2 = NULL;
560
561 /*
562 * we come here when we know perf_cgroup_events > 0
563 */
564 cgrp1 = perf_cgroup_from_task(task);
565
566 /*
567 * next is NULL when called from perf_event_enable_on_exec()
568 * that will systematically cause a cgroup_switch()
569 */
570 if (next)
571 cgrp2 = perf_cgroup_from_task(next);
572
573 /*
574 * only schedule out current cgroup events if we know
575 * that we are switching to a different cgroup. Otherwise,
576 * do no touch the cgroup events.
577 */
578 if (cgrp1 != cgrp2)
579 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200580}
581
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200582static inline void perf_cgroup_sched_in(struct task_struct *prev,
583 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200584{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200585 struct perf_cgroup *cgrp1;
586 struct perf_cgroup *cgrp2 = NULL;
587
588 /*
589 * we come here when we know perf_cgroup_events > 0
590 */
591 cgrp1 = perf_cgroup_from_task(task);
592
593 /* prev can never be NULL */
594 cgrp2 = perf_cgroup_from_task(prev);
595
596 /*
597 * only need to schedule in cgroup events if we are changing
598 * cgroup during ctxsw. Cgroup events were not scheduled
599 * out of ctxsw out if that was not the case.
600 */
601 if (cgrp1 != cgrp2)
602 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200603}
604
605static inline int perf_cgroup_connect(int fd, struct perf_event *event,
606 struct perf_event_attr *attr,
607 struct perf_event *group_leader)
608{
609 struct perf_cgroup *cgrp;
610 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400611 struct fd f = fdget(fd);
612 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200613
Al Viro2903ff02012-08-28 12:52:22 -0400614 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200615 return -EBADF;
616
Tejun Heoec903c02014-05-13 12:11:01 -0400617 css = css_tryget_online_from_dir(f.file->f_dentry,
618 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800619 if (IS_ERR(css)) {
620 ret = PTR_ERR(css);
621 goto out;
622 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200623
624 cgrp = container_of(css, struct perf_cgroup, css);
625 event->cgrp = cgrp;
626
627 /*
628 * all events in a group must monitor
629 * the same cgroup because a task belongs
630 * to only one perf cgroup at a time
631 */
632 if (group_leader && group_leader->cgrp != cgrp) {
633 perf_detach_cgroup(event);
634 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200635 }
Li Zefan3db272c2011-03-03 14:25:37 +0800636out:
Al Viro2903ff02012-08-28 12:52:22 -0400637 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200638 return ret;
639}
640
641static inline void
642perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
643{
644 struct perf_cgroup_info *t;
645 t = per_cpu_ptr(event->cgrp->info, event->cpu);
646 event->shadow_ctx_time = now - t->timestamp;
647}
648
649static inline void
650perf_cgroup_defer_enabled(struct perf_event *event)
651{
652 /*
653 * when the current task's perf cgroup does not match
654 * the event's, we need to remember to call the
655 * perf_mark_enable() function the first time a task with
656 * a matching perf cgroup is scheduled in.
657 */
658 if (is_cgroup_event(event) && !perf_cgroup_match(event))
659 event->cgrp_defer_enabled = 1;
660}
661
662static inline void
663perf_cgroup_mark_enabled(struct perf_event *event,
664 struct perf_event_context *ctx)
665{
666 struct perf_event *sub;
667 u64 tstamp = perf_event_time(event);
668
669 if (!event->cgrp_defer_enabled)
670 return;
671
672 event->cgrp_defer_enabled = 0;
673
674 event->tstamp_enabled = tstamp - event->total_time_enabled;
675 list_for_each_entry(sub, &event->sibling_list, group_entry) {
676 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
677 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
678 sub->cgrp_defer_enabled = 0;
679 }
680 }
681}
682#else /* !CONFIG_CGROUP_PERF */
683
684static inline bool
685perf_cgroup_match(struct perf_event *event)
686{
687 return true;
688}
689
690static inline void perf_detach_cgroup(struct perf_event *event)
691{}
692
693static inline int is_cgroup_event(struct perf_event *event)
694{
695 return 0;
696}
697
698static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
699{
700 return 0;
701}
702
703static inline void update_cgrp_time_from_event(struct perf_event *event)
704{
705}
706
707static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
708{
709}
710
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200711static inline void perf_cgroup_sched_out(struct task_struct *task,
712 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200713{
714}
715
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200716static inline void perf_cgroup_sched_in(struct task_struct *prev,
717 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200718{
719}
720
721static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
722 struct perf_event_attr *attr,
723 struct perf_event *group_leader)
724{
725 return -EINVAL;
726}
727
728static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200729perf_cgroup_set_timestamp(struct task_struct *task,
730 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200731{
732}
733
734void
735perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
736{
737}
738
739static inline void
740perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
741{
742}
743
744static inline u64 perf_cgroup_event_time(struct perf_event *event)
745{
746 return 0;
747}
748
749static inline void
750perf_cgroup_defer_enabled(struct perf_event *event)
751{
752}
753
754static inline void
755perf_cgroup_mark_enabled(struct perf_event *event,
756 struct perf_event_context *ctx)
757{
758}
759#endif
760
Stephane Eranian9e630202013-04-03 14:21:33 +0200761/*
762 * set default to be dependent on timer tick just
763 * like original code
764 */
765#define PERF_CPU_HRTIMER (1000 / HZ)
766/*
767 * function must be called with interrupts disbled
768 */
769static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
770{
771 struct perf_cpu_context *cpuctx;
772 enum hrtimer_restart ret = HRTIMER_NORESTART;
773 int rotations = 0;
774
775 WARN_ON(!irqs_disabled());
776
777 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
778
779 rotations = perf_rotate_context(cpuctx);
780
781 /*
782 * arm timer if needed
783 */
784 if (rotations) {
785 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
786 ret = HRTIMER_RESTART;
787 }
788
789 return ret;
790}
791
792/* CPU is going down */
793void perf_cpu_hrtimer_cancel(int cpu)
794{
795 struct perf_cpu_context *cpuctx;
796 struct pmu *pmu;
797 unsigned long flags;
798
799 if (WARN_ON(cpu != smp_processor_id()))
800 return;
801
802 local_irq_save(flags);
803
804 rcu_read_lock();
805
806 list_for_each_entry_rcu(pmu, &pmus, entry) {
807 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
808
809 if (pmu->task_ctx_nr == perf_sw_context)
810 continue;
811
812 hrtimer_cancel(&cpuctx->hrtimer);
813 }
814
815 rcu_read_unlock();
816
817 local_irq_restore(flags);
818}
819
820static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
821{
822 struct hrtimer *hr = &cpuctx->hrtimer;
823 struct pmu *pmu = cpuctx->ctx.pmu;
Stephane Eranian62b85632013-04-03 14:21:34 +0200824 int timer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200825
826 /* no multiplexing needed for SW PMU */
827 if (pmu->task_ctx_nr == perf_sw_context)
828 return;
829
Stephane Eranian62b85632013-04-03 14:21:34 +0200830 /*
831 * check default is sane, if not set then force to
832 * default interval (1/tick)
833 */
834 timer = pmu->hrtimer_interval_ms;
835 if (timer < 1)
836 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
837
838 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200839
840 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
841 hr->function = perf_cpu_hrtimer_handler;
842}
843
844static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
845{
846 struct hrtimer *hr = &cpuctx->hrtimer;
847 struct pmu *pmu = cpuctx->ctx.pmu;
848
849 /* not for SW PMU */
850 if (pmu->task_ctx_nr == perf_sw_context)
851 return;
852
853 if (hrtimer_active(hr))
854 return;
855
856 if (!hrtimer_callback_running(hr))
857 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
858 0, HRTIMER_MODE_REL_PINNED, 0);
859}
860
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200861void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200862{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200863 int *count = this_cpu_ptr(pmu->pmu_disable_count);
864 if (!(*count)++)
865 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200866}
867
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200868void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200869{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200870 int *count = this_cpu_ptr(pmu->pmu_disable_count);
871 if (!--(*count))
872 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200873}
874
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200875static DEFINE_PER_CPU(struct list_head, rotation_list);
876
877/*
878 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
879 * because they're strictly cpu affine and rotate_start is called with IRQs
880 * disabled, while rotate_context is called from IRQ context.
881 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200882static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200883{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200884 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Christoph Lameter4a32fea2014-08-17 12:30:27 -0500885 struct list_head *head = this_cpu_ptr(&rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200886
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200887 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200888
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +0200889 if (list_empty(&cpuctx->rotation_list))
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200890 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200891}
892
893static void get_ctx(struct perf_event_context *ctx)
894{
895 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
896}
897
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200898static void put_ctx(struct perf_event_context *ctx)
899{
900 if (atomic_dec_and_test(&ctx->refcount)) {
901 if (ctx->parent_ctx)
902 put_ctx(ctx->parent_ctx);
903 if (ctx->task)
904 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800905 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200906 }
907}
908
Peter Zijlstra211de6e2014-09-30 19:23:08 +0200909/*
910 * This must be done under the ctx->lock, such as to serialize against
911 * context_equiv(), therefore we cannot call put_ctx() since that might end up
912 * calling scheduler related locks and ctx->lock nests inside those.
913 */
914static __must_check struct perf_event_context *
915unclone_ctx(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200916{
Peter Zijlstra211de6e2014-09-30 19:23:08 +0200917 struct perf_event_context *parent_ctx = ctx->parent_ctx;
918
919 lockdep_assert_held(&ctx->lock);
920
921 if (parent_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200922 ctx->parent_ctx = NULL;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +0200923 ctx->generation++;
Peter Zijlstra211de6e2014-09-30 19:23:08 +0200924
925 return parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200926}
927
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200928static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
929{
930 /*
931 * only top level events have the pid namespace they were created in
932 */
933 if (event->parent)
934 event = event->parent;
935
936 return task_tgid_nr_ns(p, event->ns);
937}
938
939static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
940{
941 /*
942 * only top level events have the pid namespace they were created in
943 */
944 if (event->parent)
945 event = event->parent;
946
947 return task_pid_nr_ns(p, event->ns);
948}
949
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200950/*
951 * If we inherit events we want to return the parent event id
952 * to userspace.
953 */
954static u64 primary_event_id(struct perf_event *event)
955{
956 u64 id = event->id;
957
958 if (event->parent)
959 id = event->parent->id;
960
961 return id;
962}
963
964/*
965 * Get the perf_event_context for a task and lock it.
966 * This has to cope with with the fact that until it is locked,
967 * the context could get moved to another task.
968 */
969static struct perf_event_context *
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200970perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200971{
972 struct perf_event_context *ctx;
973
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200974retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200975 /*
976 * One of the few rules of preemptible RCU is that one cannot do
977 * rcu_read_unlock() while holding a scheduler (or nested) lock when
978 * part of the read side critical section was preemptible -- see
979 * rcu_read_unlock_special().
980 *
981 * Since ctx->lock nests under rq->lock we must ensure the entire read
982 * side critical section is non-preemptible.
983 */
984 preempt_disable();
985 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200986 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200987 if (ctx) {
988 /*
989 * If this context is a clone of another, it might
990 * get swapped for another underneath us by
991 * perf_event_task_sched_out, though the
992 * rcu_read_lock() protects us from any context
993 * getting freed. Lock the context and check if it
994 * got swapped before we could get the lock, and retry
995 * if so. If we locked the right context, then it
996 * can't get swapped on us any more.
997 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100998 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200999 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001000 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001001 rcu_read_unlock();
1002 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001003 goto retry;
1004 }
1005
1006 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001007 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001008 ctx = NULL;
1009 }
1010 }
1011 rcu_read_unlock();
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001012 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001013 return ctx;
1014}
1015
1016/*
1017 * Get the context for a task and increment its pin_count so it
1018 * can't get swapped to another task. This also increments its
1019 * reference count so that the context can't get freed.
1020 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001021static struct perf_event_context *
1022perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001023{
1024 struct perf_event_context *ctx;
1025 unsigned long flags;
1026
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001027 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001028 if (ctx) {
1029 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001030 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001031 }
1032 return ctx;
1033}
1034
1035static void perf_unpin_context(struct perf_event_context *ctx)
1036{
1037 unsigned long flags;
1038
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001039 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001040 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001041 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001042}
1043
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001044/*
1045 * Update the record of the current time in a context.
1046 */
1047static void update_context_time(struct perf_event_context *ctx)
1048{
1049 u64 now = perf_clock();
1050
1051 ctx->time += now - ctx->timestamp;
1052 ctx->timestamp = now;
1053}
1054
Stephane Eranian41587552011-01-03 18:20:01 +02001055static u64 perf_event_time(struct perf_event *event)
1056{
1057 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001058
1059 if (is_cgroup_event(event))
1060 return perf_cgroup_event_time(event);
1061
Stephane Eranian41587552011-01-03 18:20:01 +02001062 return ctx ? ctx->time : 0;
1063}
1064
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001065/*
1066 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001067 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001068 */
1069static void update_event_times(struct perf_event *event)
1070{
1071 struct perf_event_context *ctx = event->ctx;
1072 u64 run_end;
1073
1074 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1075 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1076 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001077 /*
1078 * in cgroup mode, time_enabled represents
1079 * the time the event was enabled AND active
1080 * tasks were in the monitored cgroup. This is
1081 * independent of the activity of the context as
1082 * there may be a mix of cgroup and non-cgroup events.
1083 *
1084 * That is why we treat cgroup events differently
1085 * here.
1086 */
1087 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001088 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001089 else if (ctx->is_active)
1090 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001091 else
1092 run_end = event->tstamp_stopped;
1093
1094 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001095
1096 if (event->state == PERF_EVENT_STATE_INACTIVE)
1097 run_end = event->tstamp_stopped;
1098 else
Stephane Eranian41587552011-01-03 18:20:01 +02001099 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001100
1101 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001102
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001103}
1104
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001105/*
1106 * Update total_time_enabled and total_time_running for all events in a group.
1107 */
1108static void update_group_times(struct perf_event *leader)
1109{
1110 struct perf_event *event;
1111
1112 update_event_times(leader);
1113 list_for_each_entry(event, &leader->sibling_list, group_entry)
1114 update_event_times(event);
1115}
1116
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001117static struct list_head *
1118ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1119{
1120 if (event->attr.pinned)
1121 return &ctx->pinned_groups;
1122 else
1123 return &ctx->flexible_groups;
1124}
1125
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001126/*
1127 * Add a event from the lists for its context.
1128 * Must be called with ctx->mutex and ctx->lock held.
1129 */
1130static void
1131list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1132{
Peter Zijlstra8a495422010-05-27 15:47:49 +02001133 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1134 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001135
1136 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001137 * If we're a stand alone event or group leader, we go to the context
1138 * list, group events are kept attached to the group so that
1139 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001140 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001141 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001142 struct list_head *list;
1143
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001144 if (is_software_event(event))
1145 event->group_flags |= PERF_GROUP_SOFTWARE;
1146
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001147 list = ctx_group_list(event, ctx);
1148 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001149 }
1150
Peter Zijlstra08309372011-03-03 11:31:20 +01001151 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001152 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001153
Stephane Eraniand010b332012-02-09 23:21:00 +01001154 if (has_branch_stack(event))
1155 ctx->nr_branch_stack++;
1156
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001157 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001158 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001159 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001160 ctx->nr_events++;
1161 if (event->attr.inherit_stat)
1162 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001163
1164 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001165}
1166
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001167/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001168 * Initialize event state based on the perf_event_attr::disabled.
1169 */
1170static inline void perf_event__state_init(struct perf_event *event)
1171{
1172 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1173 PERF_EVENT_STATE_INACTIVE;
1174}
1175
1176/*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001177 * Called at perf_event creation and when events are attached/detached from a
1178 * group.
1179 */
1180static void perf_event__read_size(struct perf_event *event)
1181{
1182 int entry = sizeof(u64); /* value */
1183 int size = 0;
1184 int nr = 1;
1185
1186 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1187 size += sizeof(u64);
1188
1189 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1190 size += sizeof(u64);
1191
1192 if (event->attr.read_format & PERF_FORMAT_ID)
1193 entry += sizeof(u64);
1194
1195 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1196 nr += event->group_leader->nr_siblings;
1197 size += sizeof(u64);
1198 }
1199
1200 size += entry * nr;
1201 event->read_size = size;
1202}
1203
1204static void perf_event__header_size(struct perf_event *event)
1205{
1206 struct perf_sample_data *data;
1207 u64 sample_type = event->attr.sample_type;
1208 u16 size = 0;
1209
1210 perf_event__read_size(event);
1211
1212 if (sample_type & PERF_SAMPLE_IP)
1213 size += sizeof(data->ip);
1214
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001215 if (sample_type & PERF_SAMPLE_ADDR)
1216 size += sizeof(data->addr);
1217
1218 if (sample_type & PERF_SAMPLE_PERIOD)
1219 size += sizeof(data->period);
1220
Andi Kleenc3feedf2013-01-24 16:10:28 +01001221 if (sample_type & PERF_SAMPLE_WEIGHT)
1222 size += sizeof(data->weight);
1223
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001224 if (sample_type & PERF_SAMPLE_READ)
1225 size += event->read_size;
1226
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001227 if (sample_type & PERF_SAMPLE_DATA_SRC)
1228 size += sizeof(data->data_src.val);
1229
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001230 if (sample_type & PERF_SAMPLE_TRANSACTION)
1231 size += sizeof(data->txn);
1232
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001233 event->header_size = size;
1234}
1235
1236static void perf_event__id_header_size(struct perf_event *event)
1237{
1238 struct perf_sample_data *data;
1239 u64 sample_type = event->attr.sample_type;
1240 u16 size = 0;
1241
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001242 if (sample_type & PERF_SAMPLE_TID)
1243 size += sizeof(data->tid_entry);
1244
1245 if (sample_type & PERF_SAMPLE_TIME)
1246 size += sizeof(data->time);
1247
Adrian Hunterff3d5272013-08-27 11:23:07 +03001248 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1249 size += sizeof(data->id);
1250
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001251 if (sample_type & PERF_SAMPLE_ID)
1252 size += sizeof(data->id);
1253
1254 if (sample_type & PERF_SAMPLE_STREAM_ID)
1255 size += sizeof(data->stream_id);
1256
1257 if (sample_type & PERF_SAMPLE_CPU)
1258 size += sizeof(data->cpu_entry);
1259
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001260 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001261}
1262
Peter Zijlstra8a495422010-05-27 15:47:49 +02001263static void perf_group_attach(struct perf_event *event)
1264{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001265 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001266
Peter Zijlstra74c33372010-10-15 11:40:29 +02001267 /*
1268 * We can have double attach due to group movement in perf_event_open.
1269 */
1270 if (event->attach_state & PERF_ATTACH_GROUP)
1271 return;
1272
Peter Zijlstra8a495422010-05-27 15:47:49 +02001273 event->attach_state |= PERF_ATTACH_GROUP;
1274
1275 if (group_leader == event)
1276 return;
1277
1278 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1279 !is_software_event(event))
1280 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1281
1282 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1283 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001284
1285 perf_event__header_size(group_leader);
1286
1287 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1288 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001289}
1290
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001291/*
1292 * Remove a event from the lists for its context.
1293 * Must be called with ctx->mutex and ctx->lock held.
1294 */
1295static void
1296list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1297{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001298 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001299 /*
1300 * We can have double detach due to exit/hot-unplug + close.
1301 */
1302 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001303 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001304
1305 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1306
Stephane Eranian68cacd22011-03-23 16:03:06 +01001307 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001308 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001309 cpuctx = __get_cpu_context(ctx);
1310 /*
1311 * if there are no more cgroup events
1312 * then cler cgrp to avoid stale pointer
1313 * in update_cgrp_time_from_cpuctx()
1314 */
1315 if (!ctx->nr_cgroups)
1316 cpuctx->cgrp = NULL;
1317 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001318
Stephane Eraniand010b332012-02-09 23:21:00 +01001319 if (has_branch_stack(event))
1320 ctx->nr_branch_stack--;
1321
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001322 ctx->nr_events--;
1323 if (event->attr.inherit_stat)
1324 ctx->nr_stat--;
1325
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001326 list_del_rcu(&event->event_entry);
1327
Peter Zijlstra8a495422010-05-27 15:47:49 +02001328 if (event->group_leader == event)
1329 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001330
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001331 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001332
1333 /*
1334 * If event was in error state, then keep it
1335 * that way, otherwise bogus counts will be
1336 * returned on read(). The only way to get out
1337 * of error state is by explicit re-enabling
1338 * of the event
1339 */
1340 if (event->state > PERF_EVENT_STATE_OFF)
1341 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001342
1343 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001344}
1345
Peter Zijlstra8a495422010-05-27 15:47:49 +02001346static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001347{
1348 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001349 struct list_head *list = NULL;
1350
1351 /*
1352 * We can have double detach due to exit/hot-unplug + close.
1353 */
1354 if (!(event->attach_state & PERF_ATTACH_GROUP))
1355 return;
1356
1357 event->attach_state &= ~PERF_ATTACH_GROUP;
1358
1359 /*
1360 * If this is a sibling, remove it from its group.
1361 */
1362 if (event->group_leader != event) {
1363 list_del_init(&event->group_entry);
1364 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001365 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001366 }
1367
1368 if (!list_empty(&event->group_entry))
1369 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001370
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001371 /*
1372 * If this was a group event with sibling events then
1373 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001374 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001375 */
1376 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001377 if (list)
1378 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001379 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001380
1381 /* Inherit group flags from the previous leader */
1382 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001383 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001384
1385out:
1386 perf_event__header_size(event->group_leader);
1387
1388 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1389 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001390}
1391
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001392/*
1393 * User event without the task.
1394 */
1395static bool is_orphaned_event(struct perf_event *event)
1396{
1397 return event && !is_kernel_event(event) && !event->owner;
1398}
1399
1400/*
1401 * Event has a parent but parent's task finished and it's
1402 * alive only because of children holding refference.
1403 */
1404static bool is_orphaned_child(struct perf_event *event)
1405{
1406 return is_orphaned_event(event->parent);
1407}
1408
1409static void orphans_remove_work(struct work_struct *work);
1410
1411static void schedule_orphans_remove(struct perf_event_context *ctx)
1412{
1413 if (!ctx->task || ctx->orphans_remove_sched || !perf_wq)
1414 return;
1415
1416 if (queue_delayed_work(perf_wq, &ctx->orphans_remove, 1)) {
1417 get_ctx(ctx);
1418 ctx->orphans_remove_sched = true;
1419 }
1420}
1421
1422static int __init perf_workqueue_init(void)
1423{
1424 perf_wq = create_singlethread_workqueue("perf");
1425 WARN(!perf_wq, "failed to create perf workqueue\n");
1426 return perf_wq ? 0 : -1;
1427}
1428
1429core_initcall(perf_workqueue_init);
1430
Stephane Eranianfa66f072010-08-26 16:40:01 +02001431static inline int
1432event_filter_match(struct perf_event *event)
1433{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001434 return (event->cpu == -1 || event->cpu == smp_processor_id())
1435 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001436}
1437
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001438static void
1439event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001440 struct perf_cpu_context *cpuctx,
1441 struct perf_event_context *ctx)
1442{
Stephane Eranian41587552011-01-03 18:20:01 +02001443 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001444 u64 delta;
1445 /*
1446 * An event which could not be activated because of
1447 * filter mismatch still needs to have its timings
1448 * maintained, otherwise bogus information is return
1449 * via read() for time_enabled, time_running:
1450 */
1451 if (event->state == PERF_EVENT_STATE_INACTIVE
1452 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001453 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001454 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001455 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001456 }
1457
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001458 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001459 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001460
Alexander Shishkin44377272013-12-16 14:17:36 +02001461 perf_pmu_disable(event->pmu);
1462
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001463 event->state = PERF_EVENT_STATE_INACTIVE;
1464 if (event->pending_disable) {
1465 event->pending_disable = 0;
1466 event->state = PERF_EVENT_STATE_OFF;
1467 }
Stephane Eranian41587552011-01-03 18:20:01 +02001468 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001469 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001470 event->oncpu = -1;
1471
1472 if (!is_software_event(event))
1473 cpuctx->active_oncpu--;
1474 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001475 if (event->attr.freq && event->attr.sample_freq)
1476 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001477 if (event->attr.exclusive || !cpuctx->active_oncpu)
1478 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001479
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001480 if (is_orphaned_child(event))
1481 schedule_orphans_remove(ctx);
1482
Alexander Shishkin44377272013-12-16 14:17:36 +02001483 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001484}
1485
1486static void
1487group_sched_out(struct perf_event *group_event,
1488 struct perf_cpu_context *cpuctx,
1489 struct perf_event_context *ctx)
1490{
1491 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001492 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001493
1494 event_sched_out(group_event, cpuctx, ctx);
1495
1496 /*
1497 * Schedule out siblings (if any):
1498 */
1499 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1500 event_sched_out(event, cpuctx, ctx);
1501
Stephane Eranianfa66f072010-08-26 16:40:01 +02001502 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001503 cpuctx->exclusive = 0;
1504}
1505
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001506struct remove_event {
1507 struct perf_event *event;
1508 bool detach_group;
1509};
1510
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001511/*
1512 * Cross CPU call to remove a performance event
1513 *
1514 * We disable the event on the hardware level first. After that we
1515 * remove it from the context list.
1516 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001517static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001518{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001519 struct remove_event *re = info;
1520 struct perf_event *event = re->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001521 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001522 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001523
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001524 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001525 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001526 if (re->detach_group)
1527 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001528 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001529 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1530 ctx->is_active = 0;
1531 cpuctx->task_ctx = NULL;
1532 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001533 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001534
1535 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001536}
1537
1538
1539/*
1540 * Remove the event from a task's (or a CPU's) list of events.
1541 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001542 * CPU events are removed with a smp call. For task events we only
1543 * call when the task is on a CPU.
1544 *
1545 * If event->ctx is a cloned context, callers must make sure that
1546 * every task struct that event->ctx->task could possibly point to
1547 * remains valid. This is OK when called from perf_release since
1548 * that only calls us on the top-level context, which can't be a clone.
1549 * When called from perf_event_exit_task, it's OK because the
1550 * context has been detached from its task.
1551 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001552static void perf_remove_from_context(struct perf_event *event, bool detach_group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001553{
1554 struct perf_event_context *ctx = event->ctx;
1555 struct task_struct *task = ctx->task;
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001556 struct remove_event re = {
1557 .event = event,
1558 .detach_group = detach_group,
1559 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001560
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001561 lockdep_assert_held(&ctx->mutex);
1562
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001563 if (!task) {
1564 /*
1565 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001566 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001567 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001568 cpu_function_call(event->cpu, __perf_remove_from_context, &re);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001569 return;
1570 }
1571
1572retry:
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001573 if (!task_function_call(task, __perf_remove_from_context, &re))
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001574 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001575
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001576 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001577 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001578 * If we failed to find a running task, but find the context active now
1579 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001580 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001581 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001582 raw_spin_unlock_irq(&ctx->lock);
Cong Wang3577af72014-09-02 15:27:20 -07001583 /*
1584 * Reload the task pointer, it might have been changed by
1585 * a concurrent perf_event_context_sched_out().
1586 */
1587 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001588 goto retry;
1589 }
1590
1591 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001592 * Since the task isn't running, its safe to remove the event, us
1593 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001594 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001595 if (detach_group)
1596 perf_group_detach(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001597 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001598 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001599}
1600
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001601/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001602 * Cross CPU call to disable a performance event
1603 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301604int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001605{
1606 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001607 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001608 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001609
1610 /*
1611 * If this is a per-task event, need to check whether this
1612 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001613 *
1614 * Can trigger due to concurrent perf_event_context_sched_out()
1615 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001616 */
1617 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001618 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001619
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001620 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001621
1622 /*
1623 * If the event is on, turn it off.
1624 * If it is in error state, leave it in error state.
1625 */
1626 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1627 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001628 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001629 update_group_times(event);
1630 if (event == event->group_leader)
1631 group_sched_out(event, cpuctx, ctx);
1632 else
1633 event_sched_out(event, cpuctx, ctx);
1634 event->state = PERF_EVENT_STATE_OFF;
1635 }
1636
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001637 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001638
1639 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001640}
1641
1642/*
1643 * Disable a event.
1644 *
1645 * If event->ctx is a cloned context, callers must make sure that
1646 * every task struct that event->ctx->task could possibly point to
1647 * remains valid. This condition is satisifed when called through
1648 * perf_event_for_each_child or perf_event_for_each because they
1649 * hold the top-level event's child_mutex, so any descendant that
1650 * goes to exit will block in sync_child_event.
1651 * When called from perf_pending_event it's OK because event->ctx
1652 * is the current context on this CPU and preemption is disabled,
1653 * hence we can't get into perf_event_task_sched_out for this context.
1654 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001655void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001656{
1657 struct perf_event_context *ctx = event->ctx;
1658 struct task_struct *task = ctx->task;
1659
1660 if (!task) {
1661 /*
1662 * Disable the event on the cpu that it's on
1663 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001664 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001665 return;
1666 }
1667
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001668retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001669 if (!task_function_call(task, __perf_event_disable, event))
1670 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001671
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001672 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001673 /*
1674 * If the event is still active, we need to retry the cross-call.
1675 */
1676 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001677 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001678 /*
1679 * Reload the task pointer, it might have been changed by
1680 * a concurrent perf_event_context_sched_out().
1681 */
1682 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001683 goto retry;
1684 }
1685
1686 /*
1687 * Since we have the lock this context can't be scheduled
1688 * in, so we can change the state safely.
1689 */
1690 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1691 update_group_times(event);
1692 event->state = PERF_EVENT_STATE_OFF;
1693 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001694 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001695}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001696EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001697
Stephane Eraniane5d13672011-02-14 11:20:01 +02001698static void perf_set_shadow_time(struct perf_event *event,
1699 struct perf_event_context *ctx,
1700 u64 tstamp)
1701{
1702 /*
1703 * use the correct time source for the time snapshot
1704 *
1705 * We could get by without this by leveraging the
1706 * fact that to get to this function, the caller
1707 * has most likely already called update_context_time()
1708 * and update_cgrp_time_xx() and thus both timestamp
1709 * are identical (or very close). Given that tstamp is,
1710 * already adjusted for cgroup, we could say that:
1711 * tstamp - ctx->timestamp
1712 * is equivalent to
1713 * tstamp - cgrp->timestamp.
1714 *
1715 * Then, in perf_output_read(), the calculation would
1716 * work with no changes because:
1717 * - event is guaranteed scheduled in
1718 * - no scheduled out in between
1719 * - thus the timestamp would be the same
1720 *
1721 * But this is a bit hairy.
1722 *
1723 * So instead, we have an explicit cgroup call to remain
1724 * within the time time source all along. We believe it
1725 * is cleaner and simpler to understand.
1726 */
1727 if (is_cgroup_event(event))
1728 perf_cgroup_set_shadow_time(event, tstamp);
1729 else
1730 event->shadow_ctx_time = tstamp - ctx->timestamp;
1731}
1732
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001733#define MAX_INTERRUPTS (~0ULL)
1734
1735static void perf_log_throttle(struct perf_event *event, int enable);
1736
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001737static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001738event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001739 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001740 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001741{
Stephane Eranian41587552011-01-03 18:20:01 +02001742 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001743 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001744
Peter Zijlstra63342412014-05-05 11:49:16 +02001745 lockdep_assert_held(&ctx->lock);
1746
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001747 if (event->state <= PERF_EVENT_STATE_OFF)
1748 return 0;
1749
1750 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001751 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001752
1753 /*
1754 * Unthrottle events, since we scheduled we might have missed several
1755 * ticks already, also for a heavily scheduling task there is little
1756 * guarantee it'll get a tick in a timely manner.
1757 */
1758 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1759 perf_log_throttle(event, 1);
1760 event->hw.interrupts = 0;
1761 }
1762
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001763 /*
1764 * The new state must be visible before we turn it on in the hardware:
1765 */
1766 smp_wmb();
1767
Alexander Shishkin44377272013-12-16 14:17:36 +02001768 perf_pmu_disable(event->pmu);
1769
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001770 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001771 event->state = PERF_EVENT_STATE_INACTIVE;
1772 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001773 ret = -EAGAIN;
1774 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001775 }
1776
Stephane Eranian41587552011-01-03 18:20:01 +02001777 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001778
Stephane Eraniane5d13672011-02-14 11:20:01 +02001779 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001780
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001781 if (!is_software_event(event))
1782 cpuctx->active_oncpu++;
1783 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001784 if (event->attr.freq && event->attr.sample_freq)
1785 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001786
1787 if (event->attr.exclusive)
1788 cpuctx->exclusive = 1;
1789
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001790 if (is_orphaned_child(event))
1791 schedule_orphans_remove(ctx);
1792
Alexander Shishkin44377272013-12-16 14:17:36 +02001793out:
1794 perf_pmu_enable(event->pmu);
1795
1796 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001797}
1798
1799static int
1800group_sched_in(struct perf_event *group_event,
1801 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001802 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001803{
Lin Ming6bde9b62010-04-23 13:56:00 +08001804 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001805 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001806 u64 now = ctx->time;
1807 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001808
1809 if (group_event->state == PERF_EVENT_STATE_OFF)
1810 return 0;
1811
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001812 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001813
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001814 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001815 pmu->cancel_txn(pmu);
Stephane Eranian9e630202013-04-03 14:21:33 +02001816 perf_cpu_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001817 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001818 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001819
1820 /*
1821 * Schedule in siblings as one group (if any):
1822 */
1823 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001824 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001825 partial_group = event;
1826 goto group_error;
1827 }
1828 }
1829
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001830 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001831 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001832
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001833group_error:
1834 /*
1835 * Groups can be scheduled in as one unit only, so undo any
1836 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001837 * The events up to the failed event are scheduled out normally,
1838 * tstamp_stopped will be updated.
1839 *
1840 * The failed events and the remaining siblings need to have
1841 * their timings updated as if they had gone thru event_sched_in()
1842 * and event_sched_out(). This is required to get consistent timings
1843 * across the group. This also takes care of the case where the group
1844 * could never be scheduled by ensuring tstamp_stopped is set to mark
1845 * the time the event was actually stopped, such that time delta
1846 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001847 */
1848 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1849 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001850 simulate = true;
1851
1852 if (simulate) {
1853 event->tstamp_running += now - event->tstamp_stopped;
1854 event->tstamp_stopped = now;
1855 } else {
1856 event_sched_out(event, cpuctx, ctx);
1857 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001858 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001859 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001860
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001861 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001862
Stephane Eranian9e630202013-04-03 14:21:33 +02001863 perf_cpu_hrtimer_restart(cpuctx);
1864
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001865 return -EAGAIN;
1866}
1867
1868/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001869 * Work out whether we can put this event group on the CPU now.
1870 */
1871static int group_can_go_on(struct perf_event *event,
1872 struct perf_cpu_context *cpuctx,
1873 int can_add_hw)
1874{
1875 /*
1876 * Groups consisting entirely of software events can always go on.
1877 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001878 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001879 return 1;
1880 /*
1881 * If an exclusive group is already on, no other hardware
1882 * events can go on.
1883 */
1884 if (cpuctx->exclusive)
1885 return 0;
1886 /*
1887 * If this group is exclusive and there are already
1888 * events on the CPU, it can't go on.
1889 */
1890 if (event->attr.exclusive && cpuctx->active_oncpu)
1891 return 0;
1892 /*
1893 * Otherwise, try to add it if all previous groups were able
1894 * to go on.
1895 */
1896 return can_add_hw;
1897}
1898
1899static void add_event_to_ctx(struct perf_event *event,
1900 struct perf_event_context *ctx)
1901{
Stephane Eranian41587552011-01-03 18:20:01 +02001902 u64 tstamp = perf_event_time(event);
1903
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001904 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001905 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001906 event->tstamp_enabled = tstamp;
1907 event->tstamp_running = tstamp;
1908 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001909}
1910
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001911static void task_ctx_sched_out(struct perf_event_context *ctx);
1912static void
1913ctx_sched_in(struct perf_event_context *ctx,
1914 struct perf_cpu_context *cpuctx,
1915 enum event_type_t event_type,
1916 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001917
Peter Zijlstradce58552011-04-09 21:17:46 +02001918static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1919 struct perf_event_context *ctx,
1920 struct task_struct *task)
1921{
1922 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1923 if (ctx)
1924 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1925 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1926 if (ctx)
1927 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1928}
1929
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001930/*
1931 * Cross CPU call to install and enable a performance event
1932 *
1933 * Must be called with ctx->mutex held
1934 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001935static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001936{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001937 struct perf_event *event = info;
1938 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001939 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001940 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1941 struct task_struct *task = current;
1942
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001943 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001944 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001945
1946 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001947 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001948 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001949 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001950 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001951
1952 /*
1953 * If the context we're installing events in is not the
1954 * active task_ctx, flip them.
1955 */
1956 if (ctx->task && task_ctx != ctx) {
1957 if (task_ctx)
1958 raw_spin_unlock(&task_ctx->lock);
1959 raw_spin_lock(&ctx->lock);
1960 task_ctx = ctx;
1961 }
1962
1963 if (task_ctx) {
1964 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001965 task = task_ctx->task;
1966 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001967
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001968 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001969
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001970 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001971 /*
1972 * update cgrp time only if current cgrp
1973 * matches event->cgrp. Must be done before
1974 * calling add_event_to_ctx()
1975 */
1976 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001977
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001978 add_event_to_ctx(event, ctx);
1979
1980 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001981 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001982 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001983 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001984
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001985 perf_pmu_enable(cpuctx->ctx.pmu);
1986 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001987
1988 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001989}
1990
1991/*
1992 * Attach a performance event to a context
1993 *
1994 * First we add the event to the list with the hardware enable bit
1995 * in event->hw_config cleared.
1996 *
1997 * If the event is attached to a task which is on a CPU we use a smp
1998 * call to enable it in the task context. The task might have been
1999 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002000 */
2001static void
2002perf_install_in_context(struct perf_event_context *ctx,
2003 struct perf_event *event,
2004 int cpu)
2005{
2006 struct task_struct *task = ctx->task;
2007
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002008 lockdep_assert_held(&ctx->mutex);
2009
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002010 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002011 if (event->cpu != -1)
2012 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002013
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002014 if (!task) {
2015 /*
2016 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002017 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002018 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002019 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002020 return;
2021 }
2022
2023retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002024 if (!task_function_call(task, __perf_install_in_context, event))
2025 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002026
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002027 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002028 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002029 * If we failed to find a running task, but find the context active now
2030 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002031 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002032 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002033 raw_spin_unlock_irq(&ctx->lock);
Cong Wang3577af72014-09-02 15:27:20 -07002034 /*
2035 * Reload the task pointer, it might have been changed by
2036 * a concurrent perf_event_context_sched_out().
2037 */
2038 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002039 goto retry;
2040 }
2041
2042 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002043 * Since the task isn't running, its safe to add the event, us holding
2044 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002045 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002046 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002047 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002048}
2049
2050/*
2051 * Put a event into inactive state and update time fields.
2052 * Enabling the leader of a group effectively enables all
2053 * the group members that aren't explicitly disabled, so we
2054 * have to update their ->tstamp_enabled also.
2055 * Note: this works for group members as well as group leaders
2056 * since the non-leader members' sibling_lists will be empty.
2057 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002058static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002059{
2060 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002061 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002062
2063 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002064 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002065 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002066 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2067 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002068 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002069}
2070
2071/*
2072 * Cross CPU call to enable a performance event
2073 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002074static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002075{
2076 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002077 struct perf_event_context *ctx = event->ctx;
2078 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002079 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002080 int err;
2081
Jiri Olsa06f41792013-07-09 17:44:11 +02002082 /*
2083 * There's a time window between 'ctx->is_active' check
2084 * in perf_event_enable function and this place having:
2085 * - IRQs on
2086 * - ctx->lock unlocked
2087 *
2088 * where the task could be killed and 'ctx' deactivated
2089 * by perf_event_exit_task.
2090 */
2091 if (!ctx->is_active)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002092 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002093
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002094 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002095 update_context_time(ctx);
2096
2097 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2098 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002099
2100 /*
2101 * set current task's cgroup time reference point
2102 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002103 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002104
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002105 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002106
Stephane Eraniane5d13672011-02-14 11:20:01 +02002107 if (!event_filter_match(event)) {
2108 if (is_cgroup_event(event))
2109 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002110 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002111 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002112
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002113 /*
2114 * If the event is in a group and isn't the group leader,
2115 * then don't put it on unless the group is on.
2116 */
2117 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2118 goto unlock;
2119
2120 if (!group_can_go_on(event, cpuctx, 1)) {
2121 err = -EEXIST;
2122 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002123 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002124 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002125 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01002126 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002127 }
2128
2129 if (err) {
2130 /*
2131 * If this event can't go on and it's part of a
2132 * group, then the whole group has to come off.
2133 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002134 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002135 group_sched_out(leader, cpuctx, ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002136 perf_cpu_hrtimer_restart(cpuctx);
2137 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002138 if (leader->attr.pinned) {
2139 update_group_times(leader);
2140 leader->state = PERF_EVENT_STATE_ERROR;
2141 }
2142 }
2143
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002144unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002145 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002146
2147 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002148}
2149
2150/*
2151 * Enable a event.
2152 *
2153 * If event->ctx is a cloned context, callers must make sure that
2154 * every task struct that event->ctx->task could possibly point to
2155 * remains valid. This condition is satisfied when called through
2156 * perf_event_for_each_child or perf_event_for_each as described
2157 * for perf_event_disable.
2158 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01002159void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002160{
2161 struct perf_event_context *ctx = event->ctx;
2162 struct task_struct *task = ctx->task;
2163
2164 if (!task) {
2165 /*
2166 * Enable the event on the cpu that it's on
2167 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002168 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002169 return;
2170 }
2171
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002172 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002173 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2174 goto out;
2175
2176 /*
2177 * If the event is in error state, clear that first.
2178 * That way, if we see the event in error state below, we
2179 * know that it has gone back into error state, as distinct
2180 * from the task having been scheduled away before the
2181 * cross-call arrived.
2182 */
2183 if (event->state == PERF_EVENT_STATE_ERROR)
2184 event->state = PERF_EVENT_STATE_OFF;
2185
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002186retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002187 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002188 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002189 goto out;
2190 }
2191
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002192 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002193
2194 if (!task_function_call(task, __perf_event_enable, event))
2195 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002196
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002197 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002198
2199 /*
2200 * If the context is active and the event is still off,
2201 * we need to retry the cross-call.
2202 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002203 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2204 /*
2205 * task could have been flipped by a concurrent
2206 * perf_event_context_sched_out()
2207 */
2208 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002209 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002210 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002211
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002212out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002213 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002214}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002215EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002216
Avi Kivity26ca5c12011-06-29 18:42:37 +03002217int perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002218{
2219 /*
2220 * not supported on inherited events
2221 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002222 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002223 return -EINVAL;
2224
2225 atomic_add(refresh, &event->event_limit);
2226 perf_event_enable(event);
2227
2228 return 0;
2229}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002230EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002231
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002232static void ctx_sched_out(struct perf_event_context *ctx,
2233 struct perf_cpu_context *cpuctx,
2234 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002235{
2236 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002237 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002238
Peter Zijlstradb24d332011-04-09 21:17:45 +02002239 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002240 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002241 return;
2242
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002243 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002244 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002245 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002246 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002247
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002248 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002249 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002250 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2251 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002252 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002253
Peter Zijlstradb24d332011-04-09 21:17:45 +02002254 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002255 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002256 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002257 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002258 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002259}
2260
2261/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002262 * Test whether two contexts are equivalent, i.e. whether they have both been
2263 * cloned from the same version of the same context.
2264 *
2265 * Equivalence is measured using a generation number in the context that is
2266 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2267 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002268 */
2269static int context_equiv(struct perf_event_context *ctx1,
2270 struct perf_event_context *ctx2)
2271{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02002272 lockdep_assert_held(&ctx1->lock);
2273 lockdep_assert_held(&ctx2->lock);
2274
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002275 /* Pinning disables the swap optimization */
2276 if (ctx1->pin_count || ctx2->pin_count)
2277 return 0;
2278
2279 /* If ctx1 is the parent of ctx2 */
2280 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2281 return 1;
2282
2283 /* If ctx2 is the parent of ctx1 */
2284 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2285 return 1;
2286
2287 /*
2288 * If ctx1 and ctx2 have the same parent; we flatten the parent
2289 * hierarchy, see perf_event_init_context().
2290 */
2291 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2292 ctx1->parent_gen == ctx2->parent_gen)
2293 return 1;
2294
2295 /* Unmatched */
2296 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002297}
2298
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002299static void __perf_event_sync_stat(struct perf_event *event,
2300 struct perf_event *next_event)
2301{
2302 u64 value;
2303
2304 if (!event->attr.inherit_stat)
2305 return;
2306
2307 /*
2308 * Update the event value, we cannot use perf_event_read()
2309 * because we're in the middle of a context switch and have IRQs
2310 * disabled, which upsets smp_call_function_single(), however
2311 * we know the event must be on the current CPU, therefore we
2312 * don't need to use it.
2313 */
2314 switch (event->state) {
2315 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002316 event->pmu->read(event);
2317 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002318
2319 case PERF_EVENT_STATE_INACTIVE:
2320 update_event_times(event);
2321 break;
2322
2323 default:
2324 break;
2325 }
2326
2327 /*
2328 * In order to keep per-task stats reliable we need to flip the event
2329 * values when we flip the contexts.
2330 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002331 value = local64_read(&next_event->count);
2332 value = local64_xchg(&event->count, value);
2333 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002334
2335 swap(event->total_time_enabled, next_event->total_time_enabled);
2336 swap(event->total_time_running, next_event->total_time_running);
2337
2338 /*
2339 * Since we swizzled the values, update the user visible data too.
2340 */
2341 perf_event_update_userpage(event);
2342 perf_event_update_userpage(next_event);
2343}
2344
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002345static void perf_event_sync_stat(struct perf_event_context *ctx,
2346 struct perf_event_context *next_ctx)
2347{
2348 struct perf_event *event, *next_event;
2349
2350 if (!ctx->nr_stat)
2351 return;
2352
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002353 update_context_time(ctx);
2354
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002355 event = list_first_entry(&ctx->event_list,
2356 struct perf_event, event_entry);
2357
2358 next_event = list_first_entry(&next_ctx->event_list,
2359 struct perf_event, event_entry);
2360
2361 while (&event->event_entry != &ctx->event_list &&
2362 &next_event->event_entry != &next_ctx->event_list) {
2363
2364 __perf_event_sync_stat(event, next_event);
2365
2366 event = list_next_entry(event, event_entry);
2367 next_event = list_next_entry(next_event, event_entry);
2368 }
2369}
2370
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002371static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2372 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002373{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002374 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002375 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002376 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002377 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002378 int do_switch = 1;
2379
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002380 if (likely(!ctx))
2381 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002382
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002383 cpuctx = __get_cpu_context(ctx);
2384 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002385 return;
2386
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002387 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002388 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002389 if (!next_ctx)
2390 goto unlock;
2391
2392 parent = rcu_dereference(ctx->parent_ctx);
2393 next_parent = rcu_dereference(next_ctx->parent_ctx);
2394
2395 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002396 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002397 goto unlock;
2398
2399 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002400 /*
2401 * Looks like the two contexts are clones, so we might be
2402 * able to optimize the context switch. We lock both
2403 * contexts and check that they are clones under the
2404 * lock (including re-checking that neither has been
2405 * uncloned in the meantime). It doesn't matter which
2406 * order we take the locks because no other cpu could
2407 * be trying to lock both of these tasks.
2408 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002409 raw_spin_lock(&ctx->lock);
2410 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002411 if (context_equiv(ctx, next_ctx)) {
2412 /*
2413 * XXX do we need a memory barrier of sorts
2414 * wrt to rcu_dereference() of perf_event_ctxp
2415 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002416 task->perf_event_ctxp[ctxn] = next_ctx;
2417 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002418 ctx->task = next;
2419 next_ctx->task = task;
2420 do_switch = 0;
2421
2422 perf_event_sync_stat(ctx, next_ctx);
2423 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002424 raw_spin_unlock(&next_ctx->lock);
2425 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002426 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002427unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002428 rcu_read_unlock();
2429
2430 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002431 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002432 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002433 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002434 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002435 }
2436}
2437
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002438#define for_each_task_context_nr(ctxn) \
2439 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2440
2441/*
2442 * Called from scheduler to remove the events of the current task,
2443 * with interrupts disabled.
2444 *
2445 * We stop each event and update the event value in event->count.
2446 *
2447 * This does not protect us against NMI, but disable()
2448 * sets the disabled bit in the control field of event _before_
2449 * accessing the event control register. If a NMI hits, then it will
2450 * not restart the event.
2451 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002452void __perf_event_task_sched_out(struct task_struct *task,
2453 struct task_struct *next)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002454{
2455 int ctxn;
2456
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002457 for_each_task_context_nr(ctxn)
2458 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002459
2460 /*
2461 * if cgroup events exist on this CPU, then we need
2462 * to check if we have to switch out PMU state.
2463 * cgroup event are system-wide mode only
2464 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002465 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002466 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002467}
2468
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002469static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002470{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002471 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002472
2473 if (!cpuctx->task_ctx)
2474 return;
2475
2476 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2477 return;
2478
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002479 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002480 cpuctx->task_ctx = NULL;
2481}
2482
2483/*
2484 * Called with IRQs disabled
2485 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002486static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2487 enum event_type_t event_type)
2488{
2489 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002490}
2491
2492static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002493ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002494 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002495{
2496 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002497
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002498 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2499 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002500 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002501 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002502 continue;
2503
Stephane Eraniane5d13672011-02-14 11:20:01 +02002504 /* may need to reset tstamp_enabled */
2505 if (is_cgroup_event(event))
2506 perf_cgroup_mark_enabled(event, ctx);
2507
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002508 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002509 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002510
2511 /*
2512 * If this pinned group hasn't been scheduled,
2513 * put it in error state.
2514 */
2515 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2516 update_group_times(event);
2517 event->state = PERF_EVENT_STATE_ERROR;
2518 }
2519 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002520}
2521
2522static void
2523ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002524 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002525{
2526 struct perf_event *event;
2527 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002528
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002529 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2530 /* Ignore events in OFF or ERROR state */
2531 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002532 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002533 /*
2534 * Listen to the 'cpu' scheduling filter constraint
2535 * of events:
2536 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002537 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002538 continue;
2539
Stephane Eraniane5d13672011-02-14 11:20:01 +02002540 /* may need to reset tstamp_enabled */
2541 if (is_cgroup_event(event))
2542 perf_cgroup_mark_enabled(event, ctx);
2543
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002544 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002545 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002546 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002547 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002548 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002549}
2550
2551static void
2552ctx_sched_in(struct perf_event_context *ctx,
2553 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002554 enum event_type_t event_type,
2555 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002556{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002557 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002558 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002559
Peter Zijlstradb24d332011-04-09 21:17:45 +02002560 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002561 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002562 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002563
Stephane Eraniane5d13672011-02-14 11:20:01 +02002564 now = perf_clock();
2565 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002566 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002567 /*
2568 * First go through the list and put on any pinned groups
2569 * in order to give them the best chance of going on.
2570 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002571 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002572 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002573
2574 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002575 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002576 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002577}
2578
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002579static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002580 enum event_type_t event_type,
2581 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002582{
2583 struct perf_event_context *ctx = &cpuctx->ctx;
2584
Stephane Eraniane5d13672011-02-14 11:20:01 +02002585 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002586}
2587
Stephane Eraniane5d13672011-02-14 11:20:01 +02002588static void perf_event_context_sched_in(struct perf_event_context *ctx,
2589 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002590{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002591 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002592
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002593 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002594 if (cpuctx->task_ctx == ctx)
2595 return;
2596
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002597 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002598 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002599 /*
2600 * We want to keep the following priority order:
2601 * cpu pinned (that don't need to move), task pinned,
2602 * cpu flexible, task flexible.
2603 */
2604 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2605
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002606 if (ctx->nr_events)
2607 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002608
Gleb Natapov86b47c22011-11-22 16:08:21 +02002609 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2610
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002611 perf_pmu_enable(ctx->pmu);
2612 perf_ctx_unlock(cpuctx, ctx);
2613
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002614 /*
2615 * Since these rotations are per-cpu, we need to ensure the
2616 * cpu-context we got scheduled on is actually rotating.
2617 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002618 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002619}
2620
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002621/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002622 * When sampling the branck stack in system-wide, it may be necessary
2623 * to flush the stack on context switch. This happens when the branch
2624 * stack does not tag its entries with the pid of the current task.
2625 * Otherwise it becomes impossible to associate a branch entry with a
2626 * task. This ambiguity is more likely to appear when the branch stack
2627 * supports priv level filtering and the user sets it to monitor only
2628 * at the user level (which could be a useful measurement in system-wide
2629 * mode). In that case, the risk is high of having a branch stack with
2630 * branch from multiple tasks. Flushing may mean dropping the existing
2631 * entries or stashing them somewhere in the PMU specific code layer.
2632 *
2633 * This function provides the context switch callback to the lower code
2634 * layer. It is invoked ONLY when there is at least one system-wide context
2635 * with at least one active event using taken branch sampling.
2636 */
2637static void perf_branch_stack_sched_in(struct task_struct *prev,
2638 struct task_struct *task)
2639{
2640 struct perf_cpu_context *cpuctx;
2641 struct pmu *pmu;
2642 unsigned long flags;
2643
2644 /* no need to flush branch stack if not changing task */
2645 if (prev == task)
2646 return;
2647
2648 local_irq_save(flags);
2649
2650 rcu_read_lock();
2651
2652 list_for_each_entry_rcu(pmu, &pmus, entry) {
2653 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2654
2655 /*
2656 * check if the context has at least one
2657 * event using PERF_SAMPLE_BRANCH_STACK
2658 */
2659 if (cpuctx->ctx.nr_branch_stack > 0
2660 && pmu->flush_branch_stack) {
2661
Stephane Eraniand010b332012-02-09 23:21:00 +01002662 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2663
2664 perf_pmu_disable(pmu);
2665
2666 pmu->flush_branch_stack();
2667
2668 perf_pmu_enable(pmu);
2669
2670 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2671 }
2672 }
2673
2674 rcu_read_unlock();
2675
2676 local_irq_restore(flags);
2677}
2678
2679/*
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002680 * Called from scheduler to add the events of the current task
2681 * with interrupts disabled.
2682 *
2683 * We restore the event value and then enable it.
2684 *
2685 * This does not protect us against NMI, but enable()
2686 * sets the enabled bit in the control field of event _before_
2687 * accessing the event control register. If a NMI hits, then it will
2688 * keep the event running.
2689 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002690void __perf_event_task_sched_in(struct task_struct *prev,
2691 struct task_struct *task)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002692{
2693 struct perf_event_context *ctx;
2694 int ctxn;
2695
2696 for_each_task_context_nr(ctxn) {
2697 ctx = task->perf_event_ctxp[ctxn];
2698 if (likely(!ctx))
2699 continue;
2700
Stephane Eraniane5d13672011-02-14 11:20:01 +02002701 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002702 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002703 /*
2704 * if cgroup events exist on this CPU, then we need
2705 * to check if we have to switch in PMU state.
2706 * cgroup event are system-wide mode only
2707 */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002708 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002709 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002710
2711 /* check for system-wide branch_stack events */
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002712 if (atomic_read(this_cpu_ptr(&perf_branch_stack_events)))
Stephane Eraniand010b332012-02-09 23:21:00 +01002713 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002714}
2715
Peter Zijlstraabd50712010-01-26 18:50:16 +01002716static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2717{
2718 u64 frequency = event->attr.sample_freq;
2719 u64 sec = NSEC_PER_SEC;
2720 u64 divisor, dividend;
2721
2722 int count_fls, nsec_fls, frequency_fls, sec_fls;
2723
2724 count_fls = fls64(count);
2725 nsec_fls = fls64(nsec);
2726 frequency_fls = fls64(frequency);
2727 sec_fls = 30;
2728
2729 /*
2730 * We got @count in @nsec, with a target of sample_freq HZ
2731 * the target period becomes:
2732 *
2733 * @count * 10^9
2734 * period = -------------------
2735 * @nsec * sample_freq
2736 *
2737 */
2738
2739 /*
2740 * Reduce accuracy by one bit such that @a and @b converge
2741 * to a similar magnitude.
2742 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002743#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002744do { \
2745 if (a##_fls > b##_fls) { \
2746 a >>= 1; \
2747 a##_fls--; \
2748 } else { \
2749 b >>= 1; \
2750 b##_fls--; \
2751 } \
2752} while (0)
2753
2754 /*
2755 * Reduce accuracy until either term fits in a u64, then proceed with
2756 * the other, so that finally we can do a u64/u64 division.
2757 */
2758 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2759 REDUCE_FLS(nsec, frequency);
2760 REDUCE_FLS(sec, count);
2761 }
2762
2763 if (count_fls + sec_fls > 64) {
2764 divisor = nsec * frequency;
2765
2766 while (count_fls + sec_fls > 64) {
2767 REDUCE_FLS(count, sec);
2768 divisor >>= 1;
2769 }
2770
2771 dividend = count * sec;
2772 } else {
2773 dividend = count * sec;
2774
2775 while (nsec_fls + frequency_fls > 64) {
2776 REDUCE_FLS(nsec, frequency);
2777 dividend >>= 1;
2778 }
2779
2780 divisor = nsec * frequency;
2781 }
2782
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02002783 if (!divisor)
2784 return dividend;
2785
Peter Zijlstraabd50712010-01-26 18:50:16 +01002786 return div64_u64(dividend, divisor);
2787}
2788
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002789static DEFINE_PER_CPU(int, perf_throttled_count);
2790static DEFINE_PER_CPU(u64, perf_throttled_seq);
2791
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002792static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002793{
2794 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02002795 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002796 s64 delta;
2797
Peter Zijlstraabd50712010-01-26 18:50:16 +01002798 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002799
2800 delta = (s64)(period - hwc->sample_period);
2801 delta = (delta + 7) / 8; /* low pass filter */
2802
2803 sample_period = hwc->sample_period + delta;
2804
2805 if (!sample_period)
2806 sample_period = 1;
2807
2808 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002809
Peter Zijlstrae7850592010-05-21 14:43:08 +02002810 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002811 if (disable)
2812 event->pmu->stop(event, PERF_EF_UPDATE);
2813
Peter Zijlstrae7850592010-05-21 14:43:08 +02002814 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002815
2816 if (disable)
2817 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002818 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002819}
2820
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002821/*
2822 * combine freq adjustment with unthrottling to avoid two passes over the
2823 * events. At the same time, make sure, having freq events does not change
2824 * the rate of unthrottling as that would introduce bias.
2825 */
2826static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2827 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002828{
2829 struct perf_event *event;
2830 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002831 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002832 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002833
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002834 /*
2835 * only need to iterate over all events iff:
2836 * - context have events in frequency mode (needs freq adjust)
2837 * - there are events to unthrottle on this cpu
2838 */
2839 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002840 return;
2841
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002842 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002843 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002844
Paul Mackerras03541f82009-10-14 16:58:03 +11002845 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002846 if (event->state != PERF_EVENT_STATE_ACTIVE)
2847 continue;
2848
Stephane Eranian5632ab12011-01-03 18:20:01 +02002849 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002850 continue;
2851
Alexander Shishkin44377272013-12-16 14:17:36 +02002852 perf_pmu_disable(event->pmu);
2853
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002854 hwc = &event->hw;
2855
Jiri Olsaae23bff2013-08-24 16:45:54 +02002856 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002857 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002858 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002859 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002860 }
2861
2862 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02002863 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002864
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002865 /*
2866 * stop the event and update event->count
2867 */
2868 event->pmu->stop(event, PERF_EF_UPDATE);
2869
Peter Zijlstrae7850592010-05-21 14:43:08 +02002870 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002871 delta = now - hwc->freq_count_stamp;
2872 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002873
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002874 /*
2875 * restart the event
2876 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002877 * we have stopped the event so tell that
2878 * to perf_adjust_period() to avoid stopping it
2879 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002880 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002881 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002882 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002883
2884 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02002885 next:
2886 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002887 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002888
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002889 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002890 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002891}
2892
2893/*
2894 * Round-robin a context's events:
2895 */
2896static void rotate_ctx(struct perf_event_context *ctx)
2897{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002898 /*
2899 * Rotate the first entry last of non-pinned groups. Rotation might be
2900 * disabled by the inheritance code.
2901 */
2902 if (!ctx->rotate_disable)
2903 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002904}
2905
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002906/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002907 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2908 * because they're strictly cpu affine and rotate_start is called with IRQs
2909 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002910 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002911static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002912{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002913 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002914 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002915
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002916 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002917 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002918 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2919 rotate = 1;
2920 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002921
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002922 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002923 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002924 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002925 if (ctx->nr_events != ctx->nr_active)
2926 rotate = 1;
2927 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002928
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002929 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002930 goto done;
2931
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002932 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002933 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002934
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002935 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2936 if (ctx)
2937 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002938
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002939 rotate_ctx(&cpuctx->ctx);
2940 if (ctx)
2941 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002942
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002943 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002944
2945 perf_pmu_enable(cpuctx->ctx.pmu);
2946 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002947done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002948 if (remove)
2949 list_del_init(&cpuctx->rotation_list);
Stephane Eranian9e630202013-04-03 14:21:33 +02002950
2951 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002952}
2953
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002954#ifdef CONFIG_NO_HZ_FULL
2955bool perf_event_can_stop_tick(void)
2956{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02002957 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002958 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002959 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002960 else
2961 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002962}
2963#endif
2964
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002965void perf_event_task_tick(void)
2966{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05002967 struct list_head *head = this_cpu_ptr(&rotation_list);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002968 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002969 struct perf_event_context *ctx;
2970 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002971
2972 WARN_ON(!irqs_disabled());
2973
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002974 __this_cpu_inc(perf_throttled_seq);
2975 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2976
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002977 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002978 ctx = &cpuctx->ctx;
2979 perf_adjust_freq_unthr_context(ctx, throttled);
2980
2981 ctx = cpuctx->task_ctx;
2982 if (ctx)
2983 perf_adjust_freq_unthr_context(ctx, throttled);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002984 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002985}
2986
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002987static int event_enable_on_exec(struct perf_event *event,
2988 struct perf_event_context *ctx)
2989{
2990 if (!event->attr.enable_on_exec)
2991 return 0;
2992
2993 event->attr.enable_on_exec = 0;
2994 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2995 return 0;
2996
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002997 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002998
2999 return 1;
3000}
3001
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003002/*
3003 * Enable all of a task's events that have been marked enable-on-exec.
3004 * This expects task == current.
3005 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003006static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003007{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003008 struct perf_event_context *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003009 struct perf_event *event;
3010 unsigned long flags;
3011 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003012 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003013
3014 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003015 if (!ctx || !ctx->nr_events)
3016 goto out;
3017
Stephane Eraniane566b762011-04-06 02:54:54 +02003018 /*
3019 * We must ctxsw out cgroup events to avoid conflict
3020 * when invoking perf_task_event_sched_in() later on
3021 * in this function. Otherwise we end up trying to
3022 * ctxswin cgroup events which are already scheduled
3023 * in.
3024 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003025 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003026
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003027 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02003028 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003029
Peter Zijlstrab79387e2011-11-22 11:25:43 +01003030 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003031 ret = event_enable_on_exec(event, ctx);
3032 if (ret)
3033 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003034 }
3035
3036 /*
3037 * Unclone this context if we enabled any event.
3038 */
3039 if (enabled)
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003040 clone_ctx = unclone_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003041
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003042 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003043
Stephane Eraniane566b762011-04-06 02:54:54 +02003044 /*
3045 * Also calls ctxswin for cgroup events, if any:
3046 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003047 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003048out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003049 local_irq_restore(flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003050
3051 if (clone_ctx)
3052 put_ctx(clone_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003053}
3054
Peter Zijlstrae041e322014-05-21 17:32:19 +02003055void perf_event_exec(void)
3056{
3057 struct perf_event_context *ctx;
3058 int ctxn;
3059
3060 rcu_read_lock();
3061 for_each_task_context_nr(ctxn) {
3062 ctx = current->perf_event_ctxp[ctxn];
3063 if (!ctx)
3064 continue;
3065
3066 perf_event_enable_on_exec(ctx);
3067 }
3068 rcu_read_unlock();
3069}
3070
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003071/*
3072 * Cross CPU call to read the hardware event
3073 */
3074static void __perf_event_read(void *info)
3075{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003076 struct perf_event *event = info;
3077 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003078 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003079
3080 /*
3081 * If this is a task context, we need to check whether it is
3082 * the current task context of this cpu. If not it has been
3083 * scheduled out before the smp call arrived. In that case
3084 * event->count would have been updated to a recent sample
3085 * when the event was scheduled out.
3086 */
3087 if (ctx->task && cpuctx->task_ctx != ctx)
3088 return;
3089
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003090 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003091 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003092 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003093 update_cgrp_time_from_event(event);
3094 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003095 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003096 if (event->state == PERF_EVENT_STATE_ACTIVE)
3097 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003098 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003099}
3100
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003101static inline u64 perf_event_count(struct perf_event *event)
3102{
Peter Zijlstrae7850592010-05-21 14:43:08 +02003103 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003104}
3105
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003106static u64 perf_event_read(struct perf_event *event)
3107{
3108 /*
3109 * If event is enabled and currently active on a CPU, update the
3110 * value in the event structure:
3111 */
3112 if (event->state == PERF_EVENT_STATE_ACTIVE) {
3113 smp_call_function_single(event->oncpu,
3114 __perf_event_read, event, 1);
3115 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003116 struct perf_event_context *ctx = event->ctx;
3117 unsigned long flags;
3118
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003119 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003120 /*
3121 * may read while context is not active
3122 * (e.g., thread is blocked), in that case
3123 * we cannot update context time
3124 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003125 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003126 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003127 update_cgrp_time_from_event(event);
3128 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003129 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003130 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003131 }
3132
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003133 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003134}
3135
3136/*
3137 * Initialize the perf_event context in a task_struct:
3138 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003139static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003140{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003141 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003142 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003143 INIT_LIST_HEAD(&ctx->pinned_groups);
3144 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003145 INIT_LIST_HEAD(&ctx->event_list);
3146 atomic_set(&ctx->refcount, 1);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003147 INIT_DELAYED_WORK(&ctx->orphans_remove, orphans_remove_work);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003148}
3149
Peter Zijlstraeb184472010-09-07 15:55:13 +02003150static struct perf_event_context *
3151alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003152{
3153 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003154
3155 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3156 if (!ctx)
3157 return NULL;
3158
3159 __perf_event_init_context(ctx);
3160 if (task) {
3161 ctx->task = task;
3162 get_task_struct(task);
3163 }
3164 ctx->pmu = pmu;
3165
3166 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003167}
3168
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003169static struct task_struct *
3170find_lively_task_by_vpid(pid_t vpid)
3171{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003172 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003173 int err;
3174
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003175 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003176 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003177 task = current;
3178 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003179 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003180 if (task)
3181 get_task_struct(task);
3182 rcu_read_unlock();
3183
3184 if (!task)
3185 return ERR_PTR(-ESRCH);
3186
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003187 /* Reuse ptrace permission checks for now. */
3188 err = -EACCES;
3189 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3190 goto errout;
3191
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003192 return task;
3193errout:
3194 put_task_struct(task);
3195 return ERR_PTR(err);
3196
3197}
3198
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003199/*
3200 * Returns a matching context with refcount and pincount.
3201 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003202static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07003203find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003204{
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003205 struct perf_event_context *ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003206 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003207 unsigned long flags;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003208 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003209
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003210 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003211 /* Must be root to operate on a CPU event: */
3212 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3213 return ERR_PTR(-EACCES);
3214
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003215 /*
3216 * We could be clever and allow to attach a event to an
3217 * offline CPU and activate it when the CPU comes up, but
3218 * that's for later.
3219 */
3220 if (!cpu_online(cpu))
3221 return ERR_PTR(-ENODEV);
3222
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003223 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003224 ctx = &cpuctx->ctx;
3225 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003226 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003227
3228 return ctx;
3229 }
3230
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003231 err = -EINVAL;
3232 ctxn = pmu->task_ctx_nr;
3233 if (ctxn < 0)
3234 goto errout;
3235
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003236retry:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003237 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003238 if (ctx) {
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003239 clone_ctx = unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003240 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003241 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra211de6e2014-09-30 19:23:08 +02003242
3243 if (clone_ctx)
3244 put_ctx(clone_ctx);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003245 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003246 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003247 err = -ENOMEM;
3248 if (!ctx)
3249 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003250
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003251 err = 0;
3252 mutex_lock(&task->perf_event_mutex);
3253 /*
3254 * If it has already passed perf_event_exit_task().
3255 * we must see PF_EXITING, it takes this mutex too.
3256 */
3257 if (task->flags & PF_EXITING)
3258 err = -ESRCH;
3259 else if (task->perf_event_ctxp[ctxn])
3260 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003261 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003262 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003263 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003264 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003265 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003266 mutex_unlock(&task->perf_event_mutex);
3267
3268 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003269 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003270
3271 if (err == -EAGAIN)
3272 goto retry;
3273 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003274 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003275 }
3276
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003277 return ctx;
3278
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003279errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003280 return ERR_PTR(err);
3281}
3282
Li Zefan6fb29152009-10-15 11:21:42 +08003283static void perf_event_free_filter(struct perf_event *event);
3284
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003285static void free_event_rcu(struct rcu_head *head)
3286{
3287 struct perf_event *event;
3288
3289 event = container_of(head, struct perf_event, rcu_head);
3290 if (event->ns)
3291 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003292 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003293 kfree(event);
3294}
3295
Frederic Weisbecker76369132011-05-19 19:55:04 +02003296static void ring_buffer_put(struct ring_buffer *rb);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003297static void ring_buffer_attach(struct perf_event *event,
3298 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003299
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003300static void unaccount_event_cpu(struct perf_event *event, int cpu)
3301{
3302 if (event->parent)
3303 return;
3304
3305 if (has_branch_stack(event)) {
3306 if (!(event->attach_state & PERF_ATTACH_TASK))
3307 atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
3308 }
3309 if (is_cgroup_event(event))
3310 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3311}
3312
3313static void unaccount_event(struct perf_event *event)
3314{
3315 if (event->parent)
3316 return;
3317
3318 if (event->attach_state & PERF_ATTACH_TASK)
3319 static_key_slow_dec_deferred(&perf_sched_events);
3320 if (event->attr.mmap || event->attr.mmap_data)
3321 atomic_dec(&nr_mmap_events);
3322 if (event->attr.comm)
3323 atomic_dec(&nr_comm_events);
3324 if (event->attr.task)
3325 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003326 if (event->attr.freq)
3327 atomic_dec(&nr_freq_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003328 if (is_cgroup_event(event))
3329 static_key_slow_dec_deferred(&perf_sched_events);
3330 if (has_branch_stack(event))
3331 static_key_slow_dec_deferred(&perf_sched_events);
3332
3333 unaccount_event_cpu(event, event->cpu);
3334}
3335
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003336static void __free_event(struct perf_event *event)
3337{
3338 if (!event->parent) {
3339 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3340 put_callchain_buffers();
3341 }
3342
3343 if (event->destroy)
3344 event->destroy(event);
3345
3346 if (event->ctx)
3347 put_ctx(event->ctx);
3348
Yan, Zhengc464c762014-03-18 16:56:41 +08003349 if (event->pmu)
3350 module_put(event->pmu->module);
3351
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003352 call_rcu(&event->rcu_head, free_event_rcu);
3353}
Peter Zijlstra683ede42014-05-05 12:11:24 +02003354
3355static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003356{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003357 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003358
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003359 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003360
Frederic Weisbecker76369132011-05-19 19:55:04 +02003361 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003362 /*
3363 * Can happen when we close an event with re-directed output.
3364 *
3365 * Since we have a 0 refcount, perf_mmap_close() will skip
3366 * over us; possibly making our ring_buffer_put() the last.
3367 */
3368 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003369 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003370 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003371 }
3372
Stephane Eraniane5d13672011-02-14 11:20:01 +02003373 if (is_cgroup_event(event))
3374 perf_detach_cgroup(event);
3375
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003376 __free_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003377}
3378
Peter Zijlstra683ede42014-05-05 12:11:24 +02003379/*
3380 * Used to free events which have a known refcount of 1, such as in error paths
3381 * where the event isn't exposed yet and inherited events.
3382 */
3383static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003384{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003385 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3386 "unexpected event refcount: %ld; ptr=%p\n",
3387 atomic_long_read(&event->refcount), event)) {
3388 /* leak to avoid use-after-free */
3389 return;
3390 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003391
Peter Zijlstra683ede42014-05-05 12:11:24 +02003392 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003393}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003394
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003395/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003396 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003397 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003398static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003399{
Peter Zijlstra88821352010-11-09 19:01:43 +01003400 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003401
Peter Zijlstra88821352010-11-09 19:01:43 +01003402 rcu_read_lock();
3403 owner = ACCESS_ONCE(event->owner);
3404 /*
3405 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3406 * !owner it means the list deletion is complete and we can indeed
3407 * free this event, otherwise we need to serialize on
3408 * owner->perf_event_mutex.
3409 */
3410 smp_read_barrier_depends();
3411 if (owner) {
3412 /*
3413 * Since delayed_put_task_struct() also drops the last
3414 * task reference we can safely take a new reference
3415 * while holding the rcu_read_lock().
3416 */
3417 get_task_struct(owner);
3418 }
3419 rcu_read_unlock();
3420
3421 if (owner) {
3422 mutex_lock(&owner->perf_event_mutex);
3423 /*
3424 * We have to re-check the event->owner field, if it is cleared
3425 * we raced with perf_event_exit_task(), acquiring the mutex
3426 * ensured they're done, and we can proceed with freeing the
3427 * event.
3428 */
3429 if (event->owner)
3430 list_del_init(&event->owner_entry);
3431 mutex_unlock(&owner->perf_event_mutex);
3432 put_task_struct(owner);
3433 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003434}
3435
3436/*
3437 * Called when the last reference to the file is gone.
3438 */
3439static void put_event(struct perf_event *event)
3440{
3441 struct perf_event_context *ctx = event->ctx;
3442
3443 if (!atomic_long_dec_and_test(&event->refcount))
3444 return;
3445
3446 if (!is_kernel_event(event))
3447 perf_remove_from_owner(event);
Peter Zijlstra88821352010-11-09 19:01:43 +01003448
Peter Zijlstra683ede42014-05-05 12:11:24 +02003449 WARN_ON_ONCE(ctx->parent_ctx);
3450 /*
3451 * There are two ways this annotation is useful:
3452 *
3453 * 1) there is a lock recursion from perf_event_exit_task
3454 * see the comment there.
3455 *
3456 * 2) there is a lock-inversion with mmap_sem through
3457 * perf_event_read_group(), which takes faults while
3458 * holding ctx->mutex, however this is called after
3459 * the last filedesc died, so there is no possibility
3460 * to trigger the AB-BA case.
3461 */
3462 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3463 perf_remove_from_context(event, true);
3464 mutex_unlock(&ctx->mutex);
3465
3466 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003467}
3468
Peter Zijlstra683ede42014-05-05 12:11:24 +02003469int perf_event_release_kernel(struct perf_event *event)
3470{
3471 put_event(event);
3472 return 0;
3473}
3474EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3475
Al Viroa6fa9412012-08-20 14:59:25 +01003476static int perf_release(struct inode *inode, struct file *file)
3477{
3478 put_event(file->private_data);
3479 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003480}
3481
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003482/*
3483 * Remove all orphanes events from the context.
3484 */
3485static void orphans_remove_work(struct work_struct *work)
3486{
3487 struct perf_event_context *ctx;
3488 struct perf_event *event, *tmp;
3489
3490 ctx = container_of(work, struct perf_event_context,
3491 orphans_remove.work);
3492
3493 mutex_lock(&ctx->mutex);
3494 list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) {
3495 struct perf_event *parent_event = event->parent;
3496
3497 if (!is_orphaned_child(event))
3498 continue;
3499
3500 perf_remove_from_context(event, true);
3501
3502 mutex_lock(&parent_event->child_mutex);
3503 list_del_init(&event->child_list);
3504 mutex_unlock(&parent_event->child_mutex);
3505
3506 free_event(event);
3507 put_event(parent_event);
3508 }
3509
3510 raw_spin_lock_irq(&ctx->lock);
3511 ctx->orphans_remove_sched = false;
3512 raw_spin_unlock_irq(&ctx->lock);
3513 mutex_unlock(&ctx->mutex);
3514
3515 put_ctx(ctx);
3516}
3517
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003518u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003519{
3520 struct perf_event *child;
3521 u64 total = 0;
3522
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003523 *enabled = 0;
3524 *running = 0;
3525
Peter Zijlstra6f105812009-11-20 22:19:56 +01003526 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003527 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003528 *enabled += event->total_time_enabled +
3529 atomic64_read(&event->child_total_time_enabled);
3530 *running += event->total_time_running +
3531 atomic64_read(&event->child_total_time_running);
3532
3533 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003534 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003535 *enabled += child->total_time_enabled;
3536 *running += child->total_time_running;
3537 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003538 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003539
3540 return total;
3541}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003542EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003543
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003544static int perf_event_read_group(struct perf_event *event,
3545 u64 read_format, char __user *buf)
3546{
3547 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003548 int n = 0, size = 0, ret = -EFAULT;
3549 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003550 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003551 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003552
Peter Zijlstra6f105812009-11-20 22:19:56 +01003553 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003554 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003555
3556 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003557 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3558 values[n++] = enabled;
3559 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3560 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003561 values[n++] = count;
3562 if (read_format & PERF_FORMAT_ID)
3563 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003564
3565 size = n * sizeof(u64);
3566
3567 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003568 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003569
Peter Zijlstra6f105812009-11-20 22:19:56 +01003570 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003571
3572 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003573 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003574
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003575 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003576 if (read_format & PERF_FORMAT_ID)
3577 values[n++] = primary_event_id(sub);
3578
3579 size = n * sizeof(u64);
3580
Stephane Eranian184d3da2009-11-23 21:40:49 -08003581 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003582 ret = -EFAULT;
3583 goto unlock;
3584 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003585
3586 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003587 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003588unlock:
3589 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003590
Peter Zijlstraabf48682009-11-20 22:19:49 +01003591 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003592}
3593
3594static int perf_event_read_one(struct perf_event *event,
3595 u64 read_format, char __user *buf)
3596{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003597 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003598 u64 values[4];
3599 int n = 0;
3600
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003601 values[n++] = perf_event_read_value(event, &enabled, &running);
3602 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3603 values[n++] = enabled;
3604 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3605 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003606 if (read_format & PERF_FORMAT_ID)
3607 values[n++] = primary_event_id(event);
3608
3609 if (copy_to_user(buf, values, n * sizeof(u64)))
3610 return -EFAULT;
3611
3612 return n * sizeof(u64);
3613}
3614
Jiri Olsadc633982014-09-12 13:18:26 +02003615static bool is_event_hup(struct perf_event *event)
3616{
3617 bool no_children;
3618
3619 if (event->state != PERF_EVENT_STATE_EXIT)
3620 return false;
3621
3622 mutex_lock(&event->child_mutex);
3623 no_children = list_empty(&event->child_list);
3624 mutex_unlock(&event->child_mutex);
3625 return no_children;
3626}
3627
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003628/*
3629 * Read the performance event - simple non blocking version for now
3630 */
3631static ssize_t
3632perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3633{
3634 u64 read_format = event->attr.read_format;
3635 int ret;
3636
3637 /*
3638 * Return end-of-file for a read on a event that is in
3639 * error state (i.e. because it was pinned but it couldn't be
3640 * scheduled on to the CPU at some point).
3641 */
3642 if (event->state == PERF_EVENT_STATE_ERROR)
3643 return 0;
3644
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003645 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003646 return -ENOSPC;
3647
3648 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003649 if (read_format & PERF_FORMAT_GROUP)
3650 ret = perf_event_read_group(event, read_format, buf);
3651 else
3652 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003653
3654 return ret;
3655}
3656
3657static ssize_t
3658perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3659{
3660 struct perf_event *event = file->private_data;
3661
3662 return perf_read_hw(event, buf, count);
3663}
3664
3665static unsigned int perf_poll(struct file *file, poll_table *wait)
3666{
3667 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003668 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02003669 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003670
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02003671 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04003672
Jiri Olsadc633982014-09-12 13:18:26 +02003673 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04003674 return events;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003675
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003676 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003677 * Pin the event->rb by taking event->mmap_mutex; otherwise
3678 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003679 */
3680 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003681 rb = event->rb;
3682 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003683 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003684 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003685 return events;
3686}
3687
3688static void perf_event_reset(struct perf_event *event)
3689{
3690 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003691 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003692 perf_event_update_userpage(event);
3693}
3694
3695/*
3696 * Holding the top-level event's child_mutex means that any
3697 * descendant process that has inherited this event will block
3698 * in sync_child_event if it goes to exit, thus satisfying the
3699 * task existence requirements of perf_event_enable/disable.
3700 */
3701static void perf_event_for_each_child(struct perf_event *event,
3702 void (*func)(struct perf_event *))
3703{
3704 struct perf_event *child;
3705
3706 WARN_ON_ONCE(event->ctx->parent_ctx);
3707 mutex_lock(&event->child_mutex);
3708 func(event);
3709 list_for_each_entry(child, &event->child_list, child_list)
3710 func(child);
3711 mutex_unlock(&event->child_mutex);
3712}
3713
3714static void perf_event_for_each(struct perf_event *event,
3715 void (*func)(struct perf_event *))
3716{
3717 struct perf_event_context *ctx = event->ctx;
3718 struct perf_event *sibling;
3719
3720 WARN_ON_ONCE(ctx->parent_ctx);
3721 mutex_lock(&ctx->mutex);
3722 event = event->group_leader;
3723
3724 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003725 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003726 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003727 mutex_unlock(&ctx->mutex);
3728}
3729
3730static int perf_event_period(struct perf_event *event, u64 __user *arg)
3731{
3732 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrabad71922013-11-27 13:54:38 +00003733 int ret = 0, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003734 u64 value;
3735
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003736 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003737 return -EINVAL;
3738
John Blackwoodad0cf342010-09-28 18:03:11 -04003739 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003740 return -EFAULT;
3741
3742 if (!value)
3743 return -EINVAL;
3744
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003745 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003746 if (event->attr.freq) {
3747 if (value > sysctl_perf_event_sample_rate) {
3748 ret = -EINVAL;
3749 goto unlock;
3750 }
3751
3752 event->attr.sample_freq = value;
3753 } else {
3754 event->attr.sample_period = value;
3755 event->hw.sample_period = value;
3756 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00003757
3758 active = (event->state == PERF_EVENT_STATE_ACTIVE);
3759 if (active) {
3760 perf_pmu_disable(ctx->pmu);
3761 event->pmu->stop(event, PERF_EF_UPDATE);
3762 }
3763
3764 local64_set(&event->hw.period_left, 0);
3765
3766 if (active) {
3767 event->pmu->start(event, PERF_EF_RELOAD);
3768 perf_pmu_enable(ctx->pmu);
3769 }
3770
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003771unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003772 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003773
3774 return ret;
3775}
3776
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003777static const struct file_operations perf_fops;
3778
Al Viro2903ff02012-08-28 12:52:22 -04003779static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003780{
Al Viro2903ff02012-08-28 12:52:22 -04003781 struct fd f = fdget(fd);
3782 if (!f.file)
3783 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003784
Al Viro2903ff02012-08-28 12:52:22 -04003785 if (f.file->f_op != &perf_fops) {
3786 fdput(f);
3787 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003788 }
Al Viro2903ff02012-08-28 12:52:22 -04003789 *p = f;
3790 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003791}
3792
3793static int perf_event_set_output(struct perf_event *event,
3794 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003795static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003796
3797static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3798{
3799 struct perf_event *event = file->private_data;
3800 void (*func)(struct perf_event *);
3801 u32 flags = arg;
3802
3803 switch (cmd) {
3804 case PERF_EVENT_IOC_ENABLE:
3805 func = perf_event_enable;
3806 break;
3807 case PERF_EVENT_IOC_DISABLE:
3808 func = perf_event_disable;
3809 break;
3810 case PERF_EVENT_IOC_RESET:
3811 func = perf_event_reset;
3812 break;
3813
3814 case PERF_EVENT_IOC_REFRESH:
3815 return perf_event_refresh(event, arg);
3816
3817 case PERF_EVENT_IOC_PERIOD:
3818 return perf_event_period(event, (u64 __user *)arg);
3819
Jiri Olsacf4957f2012-10-24 13:37:58 +02003820 case PERF_EVENT_IOC_ID:
3821 {
3822 u64 id = primary_event_id(event);
3823
3824 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
3825 return -EFAULT;
3826 return 0;
3827 }
3828
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003829 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003830 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003831 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003832 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003833 struct perf_event *output_event;
3834 struct fd output;
3835 ret = perf_fget_light(arg, &output);
3836 if (ret)
3837 return ret;
3838 output_event = output.file->private_data;
3839 ret = perf_event_set_output(event, output_event);
3840 fdput(output);
3841 } else {
3842 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003843 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003844 return ret;
3845 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003846
Li Zefan6fb29152009-10-15 11:21:42 +08003847 case PERF_EVENT_IOC_SET_FILTER:
3848 return perf_event_set_filter(event, (void __user *)arg);
3849
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003850 default:
3851 return -ENOTTY;
3852 }
3853
3854 if (flags & PERF_IOC_FLAG_GROUP)
3855 perf_event_for_each(event, func);
3856 else
3857 perf_event_for_each_child(event, func);
3858
3859 return 0;
3860}
3861
Pawel Mollb3f20782014-06-13 16:03:32 +01003862#ifdef CONFIG_COMPAT
3863static long perf_compat_ioctl(struct file *file, unsigned int cmd,
3864 unsigned long arg)
3865{
3866 switch (_IOC_NR(cmd)) {
3867 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
3868 case _IOC_NR(PERF_EVENT_IOC_ID):
3869 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
3870 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
3871 cmd &= ~IOCSIZE_MASK;
3872 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
3873 }
3874 break;
3875 }
3876 return perf_ioctl(file, cmd, arg);
3877}
3878#else
3879# define perf_compat_ioctl NULL
3880#endif
3881
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003882int perf_event_task_enable(void)
3883{
3884 struct perf_event *event;
3885
3886 mutex_lock(&current->perf_event_mutex);
3887 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3888 perf_event_for_each_child(event, perf_event_enable);
3889 mutex_unlock(&current->perf_event_mutex);
3890
3891 return 0;
3892}
3893
3894int perf_event_task_disable(void)
3895{
3896 struct perf_event *event;
3897
3898 mutex_lock(&current->perf_event_mutex);
3899 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3900 perf_event_for_each_child(event, perf_event_disable);
3901 mutex_unlock(&current->perf_event_mutex);
3902
3903 return 0;
3904}
3905
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003906static int perf_event_index(struct perf_event *event)
3907{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003908 if (event->hw.state & PERF_HES_STOPPED)
3909 return 0;
3910
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003911 if (event->state != PERF_EVENT_STATE_ACTIVE)
3912 return 0;
3913
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01003914 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003915}
3916
Eric B Munsonc4794292011-06-23 16:34:38 -04003917static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003918 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04003919 u64 *enabled,
3920 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04003921{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003922 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04003923
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003924 *now = perf_clock();
3925 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04003926 *enabled = ctx_time - event->tstamp_enabled;
3927 *running = ctx_time - event->tstamp_running;
3928}
3929
Peter Zijlstrafa7315872013-09-19 10:16:42 +02003930static void perf_event_init_userpage(struct perf_event *event)
3931{
3932 struct perf_event_mmap_page *userpg;
3933 struct ring_buffer *rb;
3934
3935 rcu_read_lock();
3936 rb = rcu_dereference(event->rb);
3937 if (!rb)
3938 goto unlock;
3939
3940 userpg = rb->user_page;
3941
3942 /* Allow new userspace to detect that bit 0 is deprecated */
3943 userpg->cap_bit0_is_deprecated = 1;
3944 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
3945
3946unlock:
3947 rcu_read_unlock();
3948}
3949
Peter Zijlstrac7206202012-03-22 17:26:36 +01003950void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003951{
3952}
3953
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003954/*
3955 * Callers need to ensure there can be no nesting of this function, otherwise
3956 * the seqlock logic goes bad. We can not serialize this because the arch
3957 * code calls this from NMI context.
3958 */
3959void perf_event_update_userpage(struct perf_event *event)
3960{
3961 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003962 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003963 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003964
3965 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02003966 rb = rcu_dereference(event->rb);
3967 if (!rb)
3968 goto unlock;
3969
Eric B Munson0d641202011-06-24 12:26:26 -04003970 /*
3971 * compute total_time_enabled, total_time_running
3972 * based on snapshot values taken when the event
3973 * was last scheduled in.
3974 *
3975 * we cannot simply called update_context_time()
3976 * because of locking issue as we can be called in
3977 * NMI context
3978 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003979 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003980
Frederic Weisbecker76369132011-05-19 19:55:04 +02003981 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003982 /*
3983 * Disable preemption so as to not let the corresponding user-space
3984 * spin too long if we get preempted.
3985 */
3986 preempt_disable();
3987 ++userpg->lock;
3988 barrier();
3989 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003990 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01003991 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003992 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003993
Eric B Munson0d641202011-06-24 12:26:26 -04003994 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003995 atomic64_read(&event->child_total_time_enabled);
3996
Eric B Munson0d641202011-06-24 12:26:26 -04003997 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003998 atomic64_read(&event->child_total_time_running);
3999
Peter Zijlstrac7206202012-03-22 17:26:36 +01004000 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004001
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004002 barrier();
4003 ++userpg->lock;
4004 preempt_enable();
4005unlock:
4006 rcu_read_unlock();
4007}
4008
Peter Zijlstra906010b2009-09-21 16:08:49 +02004009static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4010{
4011 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004012 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004013 int ret = VM_FAULT_SIGBUS;
4014
4015 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4016 if (vmf->pgoff == 0)
4017 ret = 0;
4018 return ret;
4019 }
4020
4021 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004022 rb = rcu_dereference(event->rb);
4023 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004024 goto unlock;
4025
4026 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4027 goto unlock;
4028
Frederic Weisbecker76369132011-05-19 19:55:04 +02004029 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004030 if (!vmf->page)
4031 goto unlock;
4032
4033 get_page(vmf->page);
4034 vmf->page->mapping = vma->vm_file->f_mapping;
4035 vmf->page->index = vmf->pgoff;
4036
4037 ret = 0;
4038unlock:
4039 rcu_read_unlock();
4040
4041 return ret;
4042}
4043
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004044static void ring_buffer_attach(struct perf_event *event,
4045 struct ring_buffer *rb)
4046{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004047 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004048 unsigned long flags;
4049
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004050 if (event->rb) {
4051 /*
4052 * Should be impossible, we set this when removing
4053 * event->rb_entry and wait/clear when adding event->rb_entry.
4054 */
4055 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004056
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004057 old_rb = event->rb;
4058 event->rcu_batches = get_state_synchronize_rcu();
4059 event->rcu_pending = 1;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004060
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004061 spin_lock_irqsave(&old_rb->event_lock, flags);
4062 list_del_rcu(&event->rb_entry);
4063 spin_unlock_irqrestore(&old_rb->event_lock, flags);
4064 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004065
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004066 if (event->rcu_pending && rb) {
4067 cond_synchronize_rcu(event->rcu_batches);
4068 event->rcu_pending = 0;
4069 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004070
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004071 if (rb) {
4072 spin_lock_irqsave(&rb->event_lock, flags);
4073 list_add_rcu(&event->rb_entry, &rb->event_list);
4074 spin_unlock_irqrestore(&rb->event_lock, flags);
4075 }
4076
4077 rcu_assign_pointer(event->rb, rb);
4078
4079 if (old_rb) {
4080 ring_buffer_put(old_rb);
4081 /*
4082 * Since we detached before setting the new rb, so that we
4083 * could attach the new rb, we could have missed a wakeup.
4084 * Provide it now.
4085 */
4086 wake_up_all(&event->waitq);
4087 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004088}
4089
4090static void ring_buffer_wakeup(struct perf_event *event)
4091{
4092 struct ring_buffer *rb;
4093
4094 rcu_read_lock();
4095 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004096 if (rb) {
4097 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4098 wake_up_all(&event->waitq);
4099 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004100 rcu_read_unlock();
4101}
4102
Frederic Weisbecker76369132011-05-19 19:55:04 +02004103static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004104{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004105 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004106
Frederic Weisbecker76369132011-05-19 19:55:04 +02004107 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
4108 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004109}
4110
Frederic Weisbecker76369132011-05-19 19:55:04 +02004111static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004112{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004113 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004114
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004115 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004116 rb = rcu_dereference(event->rb);
4117 if (rb) {
4118 if (!atomic_inc_not_zero(&rb->refcount))
4119 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004120 }
4121 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004122
Frederic Weisbecker76369132011-05-19 19:55:04 +02004123 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004124}
4125
Frederic Weisbecker76369132011-05-19 19:55:04 +02004126static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004127{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004128 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004129 return;
4130
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004131 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004132
Frederic Weisbecker76369132011-05-19 19:55:04 +02004133 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004134}
4135
4136static void perf_mmap_open(struct vm_area_struct *vma)
4137{
4138 struct perf_event *event = vma->vm_file->private_data;
4139
4140 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004141 atomic_inc(&event->rb->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004142}
4143
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004144/*
4145 * A buffer can be mmap()ed multiple times; either directly through the same
4146 * event, or through other events by use of perf_event_set_output().
4147 *
4148 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4149 * the buffer here, where we still have a VM context. This means we need
4150 * to detach all events redirecting to us.
4151 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004152static void perf_mmap_close(struct vm_area_struct *vma)
4153{
4154 struct perf_event *event = vma->vm_file->private_data;
4155
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004156 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004157 struct user_struct *mmap_user = rb->mmap_user;
4158 int mmap_locked = rb->mmap_locked;
4159 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004160
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004161 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004162
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004163 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004164 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004165
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004166 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004167 mutex_unlock(&event->mmap_mutex);
4168
4169 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004170 if (atomic_read(&rb->mmap_count))
4171 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004172
4173 /*
4174 * No other mmap()s, detach from all other events that might redirect
4175 * into the now unreachable buffer. Somewhat complicated by the
4176 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4177 */
4178again:
4179 rcu_read_lock();
4180 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4181 if (!atomic_long_inc_not_zero(&event->refcount)) {
4182 /*
4183 * This event is en-route to free_event() which will
4184 * detach it and remove it from the list.
4185 */
4186 continue;
4187 }
4188 rcu_read_unlock();
4189
4190 mutex_lock(&event->mmap_mutex);
4191 /*
4192 * Check we didn't race with perf_event_set_output() which can
4193 * swizzle the rb from under us while we were waiting to
4194 * acquire mmap_mutex.
4195 *
4196 * If we find a different rb; ignore this event, a next
4197 * iteration will no longer find it on the list. We have to
4198 * still restart the iteration to make sure we're not now
4199 * iterating the wrong list.
4200 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004201 if (event->rb == rb)
4202 ring_buffer_attach(event, NULL);
4203
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004204 mutex_unlock(&event->mmap_mutex);
4205 put_event(event);
4206
4207 /*
4208 * Restart the iteration; either we're on the wrong list or
4209 * destroyed its integrity by doing a deletion.
4210 */
4211 goto again;
4212 }
4213 rcu_read_unlock();
4214
4215 /*
4216 * It could be there's still a few 0-ref events on the list; they'll
4217 * get cleaned up by free_event() -- they'll also still have their
4218 * ref on the rb and will free it whenever they are done with it.
4219 *
4220 * Aside from that, this buffer is 'fully' detached and unmapped,
4221 * undo the VM accounting.
4222 */
4223
4224 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4225 vma->vm_mm->pinned_vm -= mmap_locked;
4226 free_uid(mmap_user);
4227
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004228out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004229 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004230}
4231
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04004232static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004233 .open = perf_mmap_open,
4234 .close = perf_mmap_close,
4235 .fault = perf_mmap_fault,
4236 .page_mkwrite = perf_mmap_fault,
4237};
4238
4239static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4240{
4241 struct perf_event *event = file->private_data;
4242 unsigned long user_locked, user_lock_limit;
4243 struct user_struct *user = current_user();
4244 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004245 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004246 unsigned long vma_size;
4247 unsigned long nr_pages;
4248 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004249 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004250
Peter Zijlstrac7920612010-05-18 10:33:24 +02004251 /*
4252 * Don't allow mmap() of inherited per-task counters. This would
4253 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004254 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004255 */
4256 if (event->cpu == -1 && event->attr.inherit)
4257 return -EINVAL;
4258
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004259 if (!(vma->vm_flags & VM_SHARED))
4260 return -EINVAL;
4261
4262 vma_size = vma->vm_end - vma->vm_start;
4263 nr_pages = (vma_size / PAGE_SIZE) - 1;
4264
4265 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004266 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004267 * can do bitmasks instead of modulo.
4268 */
4269 if (nr_pages != 0 && !is_power_of_2(nr_pages))
4270 return -EINVAL;
4271
4272 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4273 return -EINVAL;
4274
4275 if (vma->vm_pgoff != 0)
4276 return -EINVAL;
4277
4278 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004279again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004280 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004281 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004282 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004283 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004284 goto unlock;
4285 }
4286
4287 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4288 /*
4289 * Raced against perf_mmap_close() through
4290 * perf_event_set_output(). Try again, hope for better
4291 * luck.
4292 */
4293 mutex_unlock(&event->mmap_mutex);
4294 goto again;
4295 }
4296
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004297 goto unlock;
4298 }
4299
4300 user_extra = nr_pages + 1;
4301 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4302
4303 /*
4304 * Increase the limit linearly with more CPUs:
4305 */
4306 user_lock_limit *= num_online_cpus();
4307
4308 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4309
4310 extra = 0;
4311 if (user_locked > user_lock_limit)
4312 extra = user_locked - user_lock_limit;
4313
Jiri Slaby78d7d402010-03-05 13:42:54 -08004314 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004315 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004316 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004317
4318 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4319 !capable(CAP_IPC_LOCK)) {
4320 ret = -EPERM;
4321 goto unlock;
4322 }
4323
Frederic Weisbecker76369132011-05-19 19:55:04 +02004324 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004325
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004326 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004327 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004328
Vince Weaver4ec83632011-06-01 15:15:36 -04004329 rb = rb_alloc(nr_pages,
4330 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4331 event->cpu, flags);
4332
Frederic Weisbecker76369132011-05-19 19:55:04 +02004333 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004334 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004335 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004336 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004337
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004338 atomic_set(&rb->mmap_count, 1);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004339 rb->mmap_locked = extra;
4340 rb->mmap_user = get_current_user();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004341
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004342 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004343 vma->vm_mm->pinned_vm += extra;
4344
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004345 ring_buffer_attach(event, rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004346
Peter Zijlstrafa7315872013-09-19 10:16:42 +02004347 perf_event_init_userpage(event);
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01004348 perf_event_update_userpage(event);
4349
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004350unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004351 if (!ret)
4352 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004353 mutex_unlock(&event->mmap_mutex);
4354
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004355 /*
4356 * Since pinned accounting is per vm we cannot allow fork() to copy our
4357 * vma.
4358 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004359 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004360 vma->vm_ops = &perf_mmap_vmops;
4361
4362 return ret;
4363}
4364
4365static int perf_fasync(int fd, struct file *filp, int on)
4366{
Al Viro496ad9a2013-01-23 17:07:38 -05004367 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004368 struct perf_event *event = filp->private_data;
4369 int retval;
4370
4371 mutex_lock(&inode->i_mutex);
4372 retval = fasync_helper(fd, filp, on, &event->fasync);
4373 mutex_unlock(&inode->i_mutex);
4374
4375 if (retval < 0)
4376 return retval;
4377
4378 return 0;
4379}
4380
4381static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004382 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004383 .release = perf_release,
4384 .read = perf_read,
4385 .poll = perf_poll,
4386 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01004387 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004388 .mmap = perf_mmap,
4389 .fasync = perf_fasync,
4390};
4391
4392/*
4393 * Perf event wakeup
4394 *
4395 * If there's data, ensure we set the poll() state and publish everything
4396 * to user-space before waking everybody up.
4397 */
4398
4399void perf_event_wakeup(struct perf_event *event)
4400{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004401 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004402
4403 if (event->pending_kill) {
4404 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4405 event->pending_kill = 0;
4406 }
4407}
4408
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004409static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004410{
4411 struct perf_event *event = container_of(entry,
4412 struct perf_event, pending);
4413
4414 if (event->pending_disable) {
4415 event->pending_disable = 0;
4416 __perf_event_disable(event);
4417 }
4418
4419 if (event->pending_wakeup) {
4420 event->pending_wakeup = 0;
4421 perf_event_wakeup(event);
4422 }
4423}
4424
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004425/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004426 * We assume there is only KVM supporting the callbacks.
4427 * Later on, we might change it to a list if there is
4428 * another virtualization implementation supporting the callbacks.
4429 */
4430struct perf_guest_info_callbacks *perf_guest_cbs;
4431
4432int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4433{
4434 perf_guest_cbs = cbs;
4435 return 0;
4436}
4437EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4438
4439int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4440{
4441 perf_guest_cbs = NULL;
4442 return 0;
4443}
4444EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4445
Jiri Olsa40189942012-08-07 15:20:37 +02004446static void
4447perf_output_sample_regs(struct perf_output_handle *handle,
4448 struct pt_regs *regs, u64 mask)
4449{
4450 int bit;
4451
4452 for_each_set_bit(bit, (const unsigned long *) &mask,
4453 sizeof(mask) * BITS_PER_BYTE) {
4454 u64 val;
4455
4456 val = perf_reg_value(regs, bit);
4457 perf_output_put(handle, val);
4458 }
4459}
4460
4461static void perf_sample_regs_user(struct perf_regs_user *regs_user,
4462 struct pt_regs *regs)
4463{
4464 if (!user_mode(regs)) {
4465 if (current->mm)
4466 regs = task_pt_regs(current);
4467 else
4468 regs = NULL;
4469 }
4470
4471 if (regs) {
4472 regs_user->regs = regs;
4473 regs_user->abi = perf_reg_abi(current);
4474 }
4475}
4476
Jiri Olsac5ebced2012-08-07 15:20:40 +02004477/*
4478 * Get remaining task size from user stack pointer.
4479 *
4480 * It'd be better to take stack vma map and limit this more
4481 * precisly, but there's no way to get it safely under interrupt,
4482 * so using TASK_SIZE as limit.
4483 */
4484static u64 perf_ustack_task_size(struct pt_regs *regs)
4485{
4486 unsigned long addr = perf_user_stack_pointer(regs);
4487
4488 if (!addr || addr >= TASK_SIZE)
4489 return 0;
4490
4491 return TASK_SIZE - addr;
4492}
4493
4494static u16
4495perf_sample_ustack_size(u16 stack_size, u16 header_size,
4496 struct pt_regs *regs)
4497{
4498 u64 task_size;
4499
4500 /* No regs, no stack pointer, no dump. */
4501 if (!regs)
4502 return 0;
4503
4504 /*
4505 * Check if we fit in with the requested stack size into the:
4506 * - TASK_SIZE
4507 * If we don't, we limit the size to the TASK_SIZE.
4508 *
4509 * - remaining sample size
4510 * If we don't, we customize the stack size to
4511 * fit in to the remaining sample size.
4512 */
4513
4514 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4515 stack_size = min(stack_size, (u16) task_size);
4516
4517 /* Current header size plus static size and dynamic size. */
4518 header_size += 2 * sizeof(u64);
4519
4520 /* Do we fit in with the current stack dump size? */
4521 if ((u16) (header_size + stack_size) < header_size) {
4522 /*
4523 * If we overflow the maximum size for the sample,
4524 * we customize the stack dump size to fit in.
4525 */
4526 stack_size = USHRT_MAX - header_size - sizeof(u64);
4527 stack_size = round_up(stack_size, sizeof(u64));
4528 }
4529
4530 return stack_size;
4531}
4532
4533static void
4534perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4535 struct pt_regs *regs)
4536{
4537 /* Case of a kernel thread, nothing to dump */
4538 if (!regs) {
4539 u64 size = 0;
4540 perf_output_put(handle, size);
4541 } else {
4542 unsigned long sp;
4543 unsigned int rem;
4544 u64 dyn_size;
4545
4546 /*
4547 * We dump:
4548 * static size
4549 * - the size requested by user or the best one we can fit
4550 * in to the sample max size
4551 * data
4552 * - user stack dump data
4553 * dynamic size
4554 * - the actual dumped size
4555 */
4556
4557 /* Static size. */
4558 perf_output_put(handle, dump_size);
4559
4560 /* Data. */
4561 sp = perf_user_stack_pointer(regs);
4562 rem = __output_copy_user(handle, (void *) sp, dump_size);
4563 dyn_size = dump_size - rem;
4564
4565 perf_output_skip(handle, rem);
4566
4567 /* Dynamic size. */
4568 perf_output_put(handle, dyn_size);
4569 }
4570}
4571
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004572static void __perf_event_header__init_id(struct perf_event_header *header,
4573 struct perf_sample_data *data,
4574 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004575{
4576 u64 sample_type = event->attr.sample_type;
4577
4578 data->type = sample_type;
4579 header->size += event->id_header_size;
4580
4581 if (sample_type & PERF_SAMPLE_TID) {
4582 /* namespace issues */
4583 data->tid_entry.pid = perf_event_pid(event, current);
4584 data->tid_entry.tid = perf_event_tid(event, current);
4585 }
4586
4587 if (sample_type & PERF_SAMPLE_TIME)
4588 data->time = perf_clock();
4589
Adrian Hunterff3d5272013-08-27 11:23:07 +03004590 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004591 data->id = primary_event_id(event);
4592
4593 if (sample_type & PERF_SAMPLE_STREAM_ID)
4594 data->stream_id = event->id;
4595
4596 if (sample_type & PERF_SAMPLE_CPU) {
4597 data->cpu_entry.cpu = raw_smp_processor_id();
4598 data->cpu_entry.reserved = 0;
4599 }
4600}
4601
Frederic Weisbecker76369132011-05-19 19:55:04 +02004602void perf_event_header__init_id(struct perf_event_header *header,
4603 struct perf_sample_data *data,
4604 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004605{
4606 if (event->attr.sample_id_all)
4607 __perf_event_header__init_id(header, data, event);
4608}
4609
4610static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4611 struct perf_sample_data *data)
4612{
4613 u64 sample_type = data->type;
4614
4615 if (sample_type & PERF_SAMPLE_TID)
4616 perf_output_put(handle, data->tid_entry);
4617
4618 if (sample_type & PERF_SAMPLE_TIME)
4619 perf_output_put(handle, data->time);
4620
4621 if (sample_type & PERF_SAMPLE_ID)
4622 perf_output_put(handle, data->id);
4623
4624 if (sample_type & PERF_SAMPLE_STREAM_ID)
4625 perf_output_put(handle, data->stream_id);
4626
4627 if (sample_type & PERF_SAMPLE_CPU)
4628 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03004629
4630 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4631 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004632}
4633
Frederic Weisbecker76369132011-05-19 19:55:04 +02004634void perf_event__output_id_sample(struct perf_event *event,
4635 struct perf_output_handle *handle,
4636 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004637{
4638 if (event->attr.sample_id_all)
4639 __perf_event__output_id_sample(handle, sample);
4640}
4641
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004642static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004643 struct perf_event *event,
4644 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004645{
4646 u64 read_format = event->attr.read_format;
4647 u64 values[4];
4648 int n = 0;
4649
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004650 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004651 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004652 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004653 atomic64_read(&event->child_total_time_enabled);
4654 }
4655 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004656 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004657 atomic64_read(&event->child_total_time_running);
4658 }
4659 if (read_format & PERF_FORMAT_ID)
4660 values[n++] = primary_event_id(event);
4661
Frederic Weisbecker76369132011-05-19 19:55:04 +02004662 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004663}
4664
4665/*
4666 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4667 */
4668static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004669 struct perf_event *event,
4670 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004671{
4672 struct perf_event *leader = event->group_leader, *sub;
4673 u64 read_format = event->attr.read_format;
4674 u64 values[5];
4675 int n = 0;
4676
4677 values[n++] = 1 + leader->nr_siblings;
4678
4679 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004680 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004681
4682 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004683 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004684
4685 if (leader != event)
4686 leader->pmu->read(leader);
4687
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004688 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004689 if (read_format & PERF_FORMAT_ID)
4690 values[n++] = primary_event_id(leader);
4691
Frederic Weisbecker76369132011-05-19 19:55:04 +02004692 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004693
4694 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4695 n = 0;
4696
Jiri Olsa6f5ab002012-10-15 20:13:45 +02004697 if ((sub != event) &&
4698 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004699 sub->pmu->read(sub);
4700
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004701 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004702 if (read_format & PERF_FORMAT_ID)
4703 values[n++] = primary_event_id(sub);
4704
Frederic Weisbecker76369132011-05-19 19:55:04 +02004705 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004706 }
4707}
4708
Stephane Eranianeed01522010-10-26 16:08:01 +02004709#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4710 PERF_FORMAT_TOTAL_TIME_RUNNING)
4711
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004712static void perf_output_read(struct perf_output_handle *handle,
4713 struct perf_event *event)
4714{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004715 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004716 u64 read_format = event->attr.read_format;
4717
4718 /*
4719 * compute total_time_enabled, total_time_running
4720 * based on snapshot values taken when the event
4721 * was last scheduled in.
4722 *
4723 * we cannot simply called update_context_time()
4724 * because of locking issue as we are called in
4725 * NMI context
4726 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004727 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004728 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004729
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004730 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004731 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004732 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004733 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004734}
4735
4736void perf_output_sample(struct perf_output_handle *handle,
4737 struct perf_event_header *header,
4738 struct perf_sample_data *data,
4739 struct perf_event *event)
4740{
4741 u64 sample_type = data->type;
4742
4743 perf_output_put(handle, *header);
4744
Adrian Hunterff3d5272013-08-27 11:23:07 +03004745 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4746 perf_output_put(handle, data->id);
4747
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004748 if (sample_type & PERF_SAMPLE_IP)
4749 perf_output_put(handle, data->ip);
4750
4751 if (sample_type & PERF_SAMPLE_TID)
4752 perf_output_put(handle, data->tid_entry);
4753
4754 if (sample_type & PERF_SAMPLE_TIME)
4755 perf_output_put(handle, data->time);
4756
4757 if (sample_type & PERF_SAMPLE_ADDR)
4758 perf_output_put(handle, data->addr);
4759
4760 if (sample_type & PERF_SAMPLE_ID)
4761 perf_output_put(handle, data->id);
4762
4763 if (sample_type & PERF_SAMPLE_STREAM_ID)
4764 perf_output_put(handle, data->stream_id);
4765
4766 if (sample_type & PERF_SAMPLE_CPU)
4767 perf_output_put(handle, data->cpu_entry);
4768
4769 if (sample_type & PERF_SAMPLE_PERIOD)
4770 perf_output_put(handle, data->period);
4771
4772 if (sample_type & PERF_SAMPLE_READ)
4773 perf_output_read(handle, event);
4774
4775 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4776 if (data->callchain) {
4777 int size = 1;
4778
4779 if (data->callchain)
4780 size += data->callchain->nr;
4781
4782 size *= sizeof(u64);
4783
Frederic Weisbecker76369132011-05-19 19:55:04 +02004784 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004785 } else {
4786 u64 nr = 0;
4787 perf_output_put(handle, nr);
4788 }
4789 }
4790
4791 if (sample_type & PERF_SAMPLE_RAW) {
4792 if (data->raw) {
4793 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004794 __output_copy(handle, data->raw->data,
4795 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004796 } else {
4797 struct {
4798 u32 size;
4799 u32 data;
4800 } raw = {
4801 .size = sizeof(u32),
4802 .data = 0,
4803 };
4804 perf_output_put(handle, raw);
4805 }
4806 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004807
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004808 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4809 if (data->br_stack) {
4810 size_t size;
4811
4812 size = data->br_stack->nr
4813 * sizeof(struct perf_branch_entry);
4814
4815 perf_output_put(handle, data->br_stack->nr);
4816 perf_output_copy(handle, data->br_stack->entries, size);
4817 } else {
4818 /*
4819 * we always store at least the value of nr
4820 */
4821 u64 nr = 0;
4822 perf_output_put(handle, nr);
4823 }
4824 }
Jiri Olsa40189942012-08-07 15:20:37 +02004825
4826 if (sample_type & PERF_SAMPLE_REGS_USER) {
4827 u64 abi = data->regs_user.abi;
4828
4829 /*
4830 * If there are no regs to dump, notice it through
4831 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4832 */
4833 perf_output_put(handle, abi);
4834
4835 if (abi) {
4836 u64 mask = event->attr.sample_regs_user;
4837 perf_output_sample_regs(handle,
4838 data->regs_user.regs,
4839 mask);
4840 }
4841 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004842
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004843 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02004844 perf_output_sample_ustack(handle,
4845 data->stack_user_size,
4846 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004847 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01004848
4849 if (sample_type & PERF_SAMPLE_WEIGHT)
4850 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01004851
4852 if (sample_type & PERF_SAMPLE_DATA_SRC)
4853 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004854
Andi Kleenfdfbbd02013-09-20 07:40:39 -07004855 if (sample_type & PERF_SAMPLE_TRANSACTION)
4856 perf_output_put(handle, data->txn);
4857
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004858 if (!event->attr.watermark) {
4859 int wakeup_events = event->attr.wakeup_events;
4860
4861 if (wakeup_events) {
4862 struct ring_buffer *rb = handle->rb;
4863 int events = local_inc_return(&rb->events);
4864
4865 if (events >= wakeup_events) {
4866 local_sub(wakeup_events, &rb->events);
4867 local_inc(&rb->wakeup);
4868 }
4869 }
4870 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004871}
4872
4873void perf_prepare_sample(struct perf_event_header *header,
4874 struct perf_sample_data *data,
4875 struct perf_event *event,
4876 struct pt_regs *regs)
4877{
4878 u64 sample_type = event->attr.sample_type;
4879
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004880 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004881 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004882
4883 header->misc = 0;
4884 header->misc |= perf_misc_flags(regs);
4885
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004886 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004887
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004888 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004889 data->ip = perf_instruction_pointer(regs);
4890
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004891 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4892 int size = 1;
4893
Andrew Vagine6dab5f2012-07-11 18:14:58 +04004894 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004895
4896 if (data->callchain)
4897 size += data->callchain->nr;
4898
4899 header->size += size * sizeof(u64);
4900 }
4901
4902 if (sample_type & PERF_SAMPLE_RAW) {
4903 int size = sizeof(u32);
4904
4905 if (data->raw)
4906 size += data->raw->size;
4907 else
4908 size += sizeof(u32);
4909
4910 WARN_ON_ONCE(size & (sizeof(u64)-1));
4911 header->size += size;
4912 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004913
4914 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4915 int size = sizeof(u64); /* nr */
4916 if (data->br_stack) {
4917 size += data->br_stack->nr
4918 * sizeof(struct perf_branch_entry);
4919 }
4920 header->size += size;
4921 }
Jiri Olsa40189942012-08-07 15:20:37 +02004922
4923 if (sample_type & PERF_SAMPLE_REGS_USER) {
4924 /* regs dump ABI info */
4925 int size = sizeof(u64);
4926
4927 perf_sample_regs_user(&data->regs_user, regs);
4928
4929 if (data->regs_user.regs) {
4930 u64 mask = event->attr.sample_regs_user;
4931 size += hweight64(mask) * sizeof(u64);
4932 }
4933
4934 header->size += size;
4935 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004936
4937 if (sample_type & PERF_SAMPLE_STACK_USER) {
4938 /*
4939 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4940 * processed as the last one or have additional check added
4941 * in case new sample type is added, because we could eat
4942 * up the rest of the sample size.
4943 */
4944 struct perf_regs_user *uregs = &data->regs_user;
4945 u16 stack_size = event->attr.sample_stack_user;
4946 u16 size = sizeof(u64);
4947
4948 if (!uregs->abi)
4949 perf_sample_regs_user(uregs, regs);
4950
4951 stack_size = perf_sample_ustack_size(stack_size, header->size,
4952 uregs->regs);
4953
4954 /*
4955 * If there is something to dump, add space for the dump
4956 * itself and for the field that tells the dynamic size,
4957 * which is how many have been actually dumped.
4958 */
4959 if (stack_size)
4960 size += sizeof(u64) + stack_size;
4961
4962 data->stack_user_size = stack_size;
4963 header->size += size;
4964 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004965}
4966
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004967static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004968 struct perf_sample_data *data,
4969 struct pt_regs *regs)
4970{
4971 struct perf_output_handle handle;
4972 struct perf_event_header header;
4973
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004974 /* protect the callchain buffers */
4975 rcu_read_lock();
4976
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004977 perf_prepare_sample(&header, data, event, regs);
4978
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004979 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004980 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004981
4982 perf_output_sample(&handle, &header, data, event);
4983
4984 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004985
4986exit:
4987 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004988}
4989
4990/*
4991 * read event_id
4992 */
4993
4994struct perf_read_event {
4995 struct perf_event_header header;
4996
4997 u32 pid;
4998 u32 tid;
4999};
5000
5001static void
5002perf_event_read_event(struct perf_event *event,
5003 struct task_struct *task)
5004{
5005 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005006 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005007 struct perf_read_event read_event = {
5008 .header = {
5009 .type = PERF_RECORD_READ,
5010 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005011 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005012 },
5013 .pid = perf_event_pid(event, task),
5014 .tid = perf_event_tid(event, task),
5015 };
5016 int ret;
5017
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005018 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005019 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005020 if (ret)
5021 return;
5022
5023 perf_output_put(&handle, read_event);
5024 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005025 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005026
5027 perf_output_end(&handle);
5028}
5029
Jiri Olsa52d857a2013-05-06 18:27:18 +02005030typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5031
5032static void
5033perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005034 perf_event_aux_output_cb output,
5035 void *data)
5036{
5037 struct perf_event *event;
5038
5039 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5040 if (event->state < PERF_EVENT_STATE_INACTIVE)
5041 continue;
5042 if (!event_filter_match(event))
5043 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02005044 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005045 }
5046}
5047
5048static void
Jiri Olsa67516842013-07-09 18:56:31 +02005049perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005050 struct perf_event_context *task_ctx)
5051{
5052 struct perf_cpu_context *cpuctx;
5053 struct perf_event_context *ctx;
5054 struct pmu *pmu;
5055 int ctxn;
5056
5057 rcu_read_lock();
5058 list_for_each_entry_rcu(pmu, &pmus, entry) {
5059 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5060 if (cpuctx->unique_pmu != pmu)
5061 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02005062 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005063 if (task_ctx)
5064 goto next;
5065 ctxn = pmu->task_ctx_nr;
5066 if (ctxn < 0)
5067 goto next;
5068 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5069 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02005070 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005071next:
5072 put_cpu_ptr(pmu->pmu_cpu_context);
5073 }
5074
5075 if (task_ctx) {
5076 preempt_disable();
Jiri Olsa67516842013-07-09 18:56:31 +02005077 perf_event_aux_ctx(task_ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005078 preempt_enable();
5079 }
5080 rcu_read_unlock();
5081}
5082
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005083/*
5084 * task tracking -- fork/exit
5085 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02005086 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005087 */
5088
5089struct perf_task_event {
5090 struct task_struct *task;
5091 struct perf_event_context *task_ctx;
5092
5093 struct {
5094 struct perf_event_header header;
5095
5096 u32 pid;
5097 u32 ppid;
5098 u32 tid;
5099 u32 ptid;
5100 u64 time;
5101 } event_id;
5102};
5103
Jiri Olsa67516842013-07-09 18:56:31 +02005104static int perf_event_task_match(struct perf_event *event)
5105{
Stephane Eranian13d7a242013-08-21 12:10:24 +02005106 return event->attr.comm || event->attr.mmap ||
5107 event->attr.mmap2 || event->attr.mmap_data ||
5108 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02005109}
5110
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005111static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005112 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005113{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005114 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005115 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005116 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005117 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005118 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01005119
Jiri Olsa67516842013-07-09 18:56:31 +02005120 if (!perf_event_task_match(event))
5121 return;
5122
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005123 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005124
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005125 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005126 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02005127 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005128 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005129
5130 task_event->event_id.pid = perf_event_pid(event, task);
5131 task_event->event_id.ppid = perf_event_pid(event, current);
5132
5133 task_event->event_id.tid = perf_event_tid(event, task);
5134 task_event->event_id.ptid = perf_event_tid(event, current);
5135
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005136 perf_output_put(&handle, task_event->event_id);
5137
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005138 perf_event__output_id_sample(event, &handle, &sample);
5139
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005140 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005141out:
5142 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005143}
5144
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005145static void perf_event_task(struct task_struct *task,
5146 struct perf_event_context *task_ctx,
5147 int new)
5148{
5149 struct perf_task_event task_event;
5150
5151 if (!atomic_read(&nr_comm_events) &&
5152 !atomic_read(&nr_mmap_events) &&
5153 !atomic_read(&nr_task_events))
5154 return;
5155
5156 task_event = (struct perf_task_event){
5157 .task = task,
5158 .task_ctx = task_ctx,
5159 .event_id = {
5160 .header = {
5161 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5162 .misc = 0,
5163 .size = sizeof(task_event.event_id),
5164 },
5165 /* .pid */
5166 /* .ppid */
5167 /* .tid */
5168 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01005169 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005170 },
5171 };
5172
Jiri Olsa67516842013-07-09 18:56:31 +02005173 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005174 &task_event,
5175 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005176}
5177
5178void perf_event_fork(struct task_struct *task)
5179{
5180 perf_event_task(task, NULL, 1);
5181}
5182
5183/*
5184 * comm tracking
5185 */
5186
5187struct perf_comm_event {
5188 struct task_struct *task;
5189 char *comm;
5190 int comm_size;
5191
5192 struct {
5193 struct perf_event_header header;
5194
5195 u32 pid;
5196 u32 tid;
5197 } event_id;
5198};
5199
Jiri Olsa67516842013-07-09 18:56:31 +02005200static int perf_event_comm_match(struct perf_event *event)
5201{
5202 return event->attr.comm;
5203}
5204
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005205static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005206 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005207{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005208 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005209 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005210 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005211 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005212 int ret;
5213
Jiri Olsa67516842013-07-09 18:56:31 +02005214 if (!perf_event_comm_match(event))
5215 return;
5216
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005217 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5218 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005219 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005220
5221 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005222 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005223
5224 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5225 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5226
5227 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005228 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005229 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005230
5231 perf_event__output_id_sample(event, &handle, &sample);
5232
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005233 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005234out:
5235 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005236}
5237
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005238static void perf_event_comm_event(struct perf_comm_event *comm_event)
5239{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005240 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005241 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005242
5243 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005244 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005245 size = ALIGN(strlen(comm)+1, sizeof(u64));
5246
5247 comm_event->comm = comm;
5248 comm_event->comm_size = size;
5249
5250 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005251
Jiri Olsa67516842013-07-09 18:56:31 +02005252 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005253 comm_event,
5254 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005255}
5256
Adrian Hunter82b89772014-05-28 11:45:04 +03005257void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005258{
5259 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005260
5261 if (!atomic_read(&nr_comm_events))
5262 return;
5263
5264 comm_event = (struct perf_comm_event){
5265 .task = task,
5266 /* .comm */
5267 /* .comm_size */
5268 .event_id = {
5269 .header = {
5270 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005271 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005272 /* .size */
5273 },
5274 /* .pid */
5275 /* .tid */
5276 },
5277 };
5278
5279 perf_event_comm_event(&comm_event);
5280}
5281
5282/*
5283 * mmap tracking
5284 */
5285
5286struct perf_mmap_event {
5287 struct vm_area_struct *vma;
5288
5289 const char *file_name;
5290 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005291 int maj, min;
5292 u64 ino;
5293 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005294 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005295
5296 struct {
5297 struct perf_event_header header;
5298
5299 u32 pid;
5300 u32 tid;
5301 u64 start;
5302 u64 len;
5303 u64 pgoff;
5304 } event_id;
5305};
5306
Jiri Olsa67516842013-07-09 18:56:31 +02005307static int perf_event_mmap_match(struct perf_event *event,
5308 void *data)
5309{
5310 struct perf_mmap_event *mmap_event = data;
5311 struct vm_area_struct *vma = mmap_event->vma;
5312 int executable = vma->vm_flags & VM_EXEC;
5313
5314 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005315 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005316}
5317
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005318static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005319 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005320{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005321 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005322 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005323 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005324 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005325 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005326
Jiri Olsa67516842013-07-09 18:56:31 +02005327 if (!perf_event_mmap_match(event, data))
5328 return;
5329
Stephane Eranian13d7a242013-08-21 12:10:24 +02005330 if (event->attr.mmap2) {
5331 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5332 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5333 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5334 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005335 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005336 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
5337 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005338 }
5339
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005340 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5341 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005342 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005343 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005344 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005345
5346 mmap_event->event_id.pid = perf_event_pid(event, current);
5347 mmap_event->event_id.tid = perf_event_tid(event, current);
5348
5349 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005350
5351 if (event->attr.mmap2) {
5352 perf_output_put(&handle, mmap_event->maj);
5353 perf_output_put(&handle, mmap_event->min);
5354 perf_output_put(&handle, mmap_event->ino);
5355 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005356 perf_output_put(&handle, mmap_event->prot);
5357 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005358 }
5359
Frederic Weisbecker76369132011-05-19 19:55:04 +02005360 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005361 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005362
5363 perf_event__output_id_sample(event, &handle, &sample);
5364
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005365 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005366out:
5367 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005368}
5369
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005370static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5371{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005372 struct vm_area_struct *vma = mmap_event->vma;
5373 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005374 int maj = 0, min = 0;
5375 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005376 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005377 unsigned int size;
5378 char tmp[16];
5379 char *buf = NULL;
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02005380 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005381
5382 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005383 struct inode *inode;
5384 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005385
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02005386 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005387 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005388 name = "//enomem";
5389 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005390 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005391 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005392 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005393 * need to add enough zero bytes after the string to handle
5394 * the 64bit alignment we do later.
5395 */
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005396 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005397 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005398 name = "//toolong";
5399 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005400 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005401 inode = file_inode(vma->vm_file);
5402 dev = inode->i_sb->s_dev;
5403 ino = inode->i_ino;
5404 gen = inode->i_generation;
5405 maj = MAJOR(dev);
5406 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005407
5408 if (vma->vm_flags & VM_READ)
5409 prot |= PROT_READ;
5410 if (vma->vm_flags & VM_WRITE)
5411 prot |= PROT_WRITE;
5412 if (vma->vm_flags & VM_EXEC)
5413 prot |= PROT_EXEC;
5414
5415 if (vma->vm_flags & VM_MAYSHARE)
5416 flags = MAP_SHARED;
5417 else
5418 flags = MAP_PRIVATE;
5419
5420 if (vma->vm_flags & VM_DENYWRITE)
5421 flags |= MAP_DENYWRITE;
5422 if (vma->vm_flags & VM_MAYEXEC)
5423 flags |= MAP_EXECUTABLE;
5424 if (vma->vm_flags & VM_LOCKED)
5425 flags |= MAP_LOCKED;
5426 if (vma->vm_flags & VM_HUGETLB)
5427 flags |= MAP_HUGETLB;
5428
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005429 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005430 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02005431 if (vma->vm_ops && vma->vm_ops->name) {
5432 name = (char *) vma->vm_ops->name(vma);
5433 if (name)
5434 goto cpy_name;
5435 }
5436
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02005437 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005438 if (name)
5439 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005440
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005441 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005442 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005443 name = "[heap]";
5444 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005445 }
5446 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005447 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005448 name = "[stack]";
5449 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005450 }
5451
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005452 name = "//anon";
5453 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005454 }
5455
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005456cpy_name:
5457 strlcpy(tmp, name, sizeof(tmp));
5458 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005459got_name:
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02005460 /*
5461 * Since our buffer works in 8 byte units we need to align our string
5462 * size to a multiple of 8. However, we must guarantee the tail end is
5463 * zero'd out to avoid leaking random bits to userspace.
5464 */
5465 size = strlen(name)+1;
5466 while (!IS_ALIGNED(size, sizeof(u64)))
5467 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005468
5469 mmap_event->file_name = name;
5470 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005471 mmap_event->maj = maj;
5472 mmap_event->min = min;
5473 mmap_event->ino = ino;
5474 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005475 mmap_event->prot = prot;
5476 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005477
Stephane Eranian2fe85422013-01-24 16:10:39 +01005478 if (!(vma->vm_flags & VM_EXEC))
5479 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5480
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005481 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5482
Jiri Olsa67516842013-07-09 18:56:31 +02005483 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005484 mmap_event,
5485 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005486
5487 kfree(buf);
5488}
5489
Eric B Munson3af9e852010-05-18 15:30:49 +01005490void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005491{
5492 struct perf_mmap_event mmap_event;
5493
5494 if (!atomic_read(&nr_mmap_events))
5495 return;
5496
5497 mmap_event = (struct perf_mmap_event){
5498 .vma = vma,
5499 /* .file_name */
5500 /* .file_size */
5501 .event_id = {
5502 .header = {
5503 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005504 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005505 /* .size */
5506 },
5507 /* .pid */
5508 /* .tid */
5509 .start = vma->vm_start,
5510 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01005511 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005512 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02005513 /* .maj (attr_mmap2 only) */
5514 /* .min (attr_mmap2 only) */
5515 /* .ino (attr_mmap2 only) */
5516 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005517 /* .prot (attr_mmap2 only) */
5518 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005519 };
5520
5521 perf_event_mmap_event(&mmap_event);
5522}
5523
5524/*
5525 * IRQ throttle logging
5526 */
5527
5528static void perf_log_throttle(struct perf_event *event, int enable)
5529{
5530 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005531 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005532 int ret;
5533
5534 struct {
5535 struct perf_event_header header;
5536 u64 time;
5537 u64 id;
5538 u64 stream_id;
5539 } throttle_event = {
5540 .header = {
5541 .type = PERF_RECORD_THROTTLE,
5542 .misc = 0,
5543 .size = sizeof(throttle_event),
5544 },
5545 .time = perf_clock(),
5546 .id = primary_event_id(event),
5547 .stream_id = event->id,
5548 };
5549
5550 if (enable)
5551 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5552
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005553 perf_event_header__init_id(&throttle_event.header, &sample, event);
5554
5555 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005556 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005557 if (ret)
5558 return;
5559
5560 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005561 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005562 perf_output_end(&handle);
5563}
5564
5565/*
5566 * Generic event overflow handling, sampling.
5567 */
5568
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005569static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005570 int throttle, struct perf_sample_data *data,
5571 struct pt_regs *regs)
5572{
5573 int events = atomic_read(&event->event_limit);
5574 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005575 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005576 int ret = 0;
5577
Peter Zijlstra96398822010-11-24 18:55:29 +01005578 /*
5579 * Non-sampling counters might still use the PMI to fold short
5580 * hardware counters, ignore those.
5581 */
5582 if (unlikely(!is_sampling_event(event)))
5583 return 0;
5584
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005585 seq = __this_cpu_read(perf_throttled_seq);
5586 if (seq != hwc->interrupts_seq) {
5587 hwc->interrupts_seq = seq;
5588 hwc->interrupts = 1;
5589 } else {
5590 hwc->interrupts++;
5591 if (unlikely(throttle
5592 && hwc->interrupts >= max_samples_per_tick)) {
5593 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01005594 hwc->interrupts = MAX_INTERRUPTS;
5595 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02005596 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005597 ret = 1;
5598 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005599 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005600
5601 if (event->attr.freq) {
5602 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01005603 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005604
Peter Zijlstraabd50712010-01-26 18:50:16 +01005605 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005606
Peter Zijlstraabd50712010-01-26 18:50:16 +01005607 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01005608 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005609 }
5610
5611 /*
5612 * XXX event_limit might not quite work as expected on inherited
5613 * events
5614 */
5615
5616 event->pending_kill = POLL_IN;
5617 if (events && atomic_dec_and_test(&event->event_limit)) {
5618 ret = 1;
5619 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005620 event->pending_disable = 1;
5621 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005622 }
5623
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005624 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005625 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005626 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005627 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005628
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005629 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005630 event->pending_wakeup = 1;
5631 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005632 }
5633
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005634 return ret;
5635}
5636
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005637int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005638 struct perf_sample_data *data,
5639 struct pt_regs *regs)
5640{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005641 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005642}
5643
5644/*
5645 * Generic software event infrastructure
5646 */
5647
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005648struct swevent_htable {
5649 struct swevent_hlist *swevent_hlist;
5650 struct mutex hlist_mutex;
5651 int hlist_refcount;
5652
5653 /* Recursion avoidance in each contexts */
5654 int recursion[PERF_NR_CONTEXTS];
Jiri Olsa39af6b12014-04-07 11:04:08 +02005655
5656 /* Keeps track of cpu being initialized/exited */
5657 bool online;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005658};
5659
5660static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5661
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005662/*
5663 * We directly increment event->count and keep a second value in
5664 * event->hw.period_left to count intervals. This period event
5665 * is kept in the range [-sample_period, 0] so that we can use the
5666 * sign as trigger.
5667 */
5668
Jiri Olsaab573842013-05-01 17:25:44 +02005669u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005670{
5671 struct hw_perf_event *hwc = &event->hw;
5672 u64 period = hwc->last_period;
5673 u64 nr, offset;
5674 s64 old, val;
5675
5676 hwc->last_period = hwc->sample_period;
5677
5678again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005679 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005680 if (val < 0)
5681 return 0;
5682
5683 nr = div64_u64(period + val, period);
5684 offset = nr * period;
5685 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005686 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005687 goto again;
5688
5689 return nr;
5690}
5691
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005692static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005693 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005694 struct pt_regs *regs)
5695{
5696 struct hw_perf_event *hwc = &event->hw;
5697 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005698
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005699 if (!overflow)
5700 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005701
5702 if (hwc->interrupts == MAX_INTERRUPTS)
5703 return;
5704
5705 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005706 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005707 data, regs)) {
5708 /*
5709 * We inhibit the overflow from happening when
5710 * hwc->interrupts == MAX_INTERRUPTS.
5711 */
5712 break;
5713 }
5714 throttle = 1;
5715 }
5716}
5717
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005718static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005719 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005720 struct pt_regs *regs)
5721{
5722 struct hw_perf_event *hwc = &event->hw;
5723
Peter Zijlstrae7850592010-05-21 14:43:08 +02005724 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005725
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005726 if (!regs)
5727 return;
5728
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005729 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005730 return;
5731
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005732 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5733 data->period = nr;
5734 return perf_swevent_overflow(event, 1, data, regs);
5735 } else
5736 data->period = event->hw.last_period;
5737
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005738 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005739 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005740
Peter Zijlstrae7850592010-05-21 14:43:08 +02005741 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005742 return;
5743
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005744 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005745}
5746
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005747static int perf_exclude_event(struct perf_event *event,
5748 struct pt_regs *regs)
5749{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005750 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005751 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005752
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005753 if (regs) {
5754 if (event->attr.exclude_user && user_mode(regs))
5755 return 1;
5756
5757 if (event->attr.exclude_kernel && !user_mode(regs))
5758 return 1;
5759 }
5760
5761 return 0;
5762}
5763
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005764static int perf_swevent_match(struct perf_event *event,
5765 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005766 u32 event_id,
5767 struct perf_sample_data *data,
5768 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005769{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005770 if (event->attr.type != type)
5771 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005772
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005773 if (event->attr.config != event_id)
5774 return 0;
5775
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005776 if (perf_exclude_event(event, regs))
5777 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005778
5779 return 1;
5780}
5781
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005782static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005783{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005784 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005785
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005786 return hash_64(val, SWEVENT_HLIST_BITS);
5787}
5788
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005789static inline struct hlist_head *
5790__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005791{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005792 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005793
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005794 return &hlist->heads[hash];
5795}
5796
5797/* For the read side: events when they trigger */
5798static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005799find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005800{
5801 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005802
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005803 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005804 if (!hlist)
5805 return NULL;
5806
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005807 return __find_swevent_head(hlist, type, event_id);
5808}
5809
5810/* For the event head insertion and removal in the hlist */
5811static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005812find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005813{
5814 struct swevent_hlist *hlist;
5815 u32 event_id = event->attr.config;
5816 u64 type = event->attr.type;
5817
5818 /*
5819 * Event scheduling is always serialized against hlist allocation
5820 * and release. Which makes the protected version suitable here.
5821 * The context lock guarantees that.
5822 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005823 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005824 lockdep_is_held(&event->ctx->lock));
5825 if (!hlist)
5826 return NULL;
5827
5828 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005829}
5830
5831static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005832 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005833 struct perf_sample_data *data,
5834 struct pt_regs *regs)
5835{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05005836 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005837 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005838 struct hlist_head *head;
5839
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005840 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005841 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005842 if (!head)
5843 goto end;
5844
Sasha Levinb67bfe02013-02-27 17:06:00 -08005845 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005846 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005847 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005848 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005849end:
5850 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005851}
5852
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005853int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005854{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05005855 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005856
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005857 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005858}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005859EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005860
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005861inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005862{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05005863 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005864
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005865 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005866}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005867
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005868void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005869{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005870 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005871 int rctx;
5872
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005873 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005874 rctx = perf_swevent_get_recursion_context();
5875 if (rctx < 0)
5876 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005877
Robert Richterfd0d0002012-04-02 20:19:08 +02005878 perf_sample_data_init(&data, addr, 0);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005879
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005880 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005881
5882 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005883 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005884}
5885
5886static void perf_swevent_read(struct perf_event *event)
5887{
5888}
5889
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005890static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005891{
Christoph Lameter4a32fea2014-08-17 12:30:27 -05005892 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005893 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005894 struct hlist_head *head;
5895
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005896 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005897 hwc->last_period = hwc->sample_period;
5898 perf_swevent_set_period(event);
5899 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005900
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005901 hwc->state = !(flags & PERF_EF_START);
5902
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005903 head = find_swevent_head(swhash, event);
Jiri Olsa39af6b12014-04-07 11:04:08 +02005904 if (!head) {
5905 /*
5906 * We can race with cpu hotplug code. Do not
5907 * WARN if the cpu just got unplugged.
5908 */
5909 WARN_ON_ONCE(swhash->online);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005910 return -EINVAL;
Jiri Olsa39af6b12014-04-07 11:04:08 +02005911 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005912
5913 hlist_add_head_rcu(&event->hlist_entry, head);
5914
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005915 return 0;
5916}
5917
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005918static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005919{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005920 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005921}
5922
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005923static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005924{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005925 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005926}
5927
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005928static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005929{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005930 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005931}
5932
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005933/* Deref the hlist from the update side */
5934static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005935swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005936{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005937 return rcu_dereference_protected(swhash->swevent_hlist,
5938 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005939}
5940
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005941static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005942{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005943 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005944
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005945 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005946 return;
5947
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03005948 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08005949 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005950}
5951
5952static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5953{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005954 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005955
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005956 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005957
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005958 if (!--swhash->hlist_refcount)
5959 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005960
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005961 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005962}
5963
5964static void swevent_hlist_put(struct perf_event *event)
5965{
5966 int cpu;
5967
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005968 for_each_possible_cpu(cpu)
5969 swevent_hlist_put_cpu(event, cpu);
5970}
5971
5972static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5973{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005974 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005975 int err = 0;
5976
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005977 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005978
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005979 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005980 struct swevent_hlist *hlist;
5981
5982 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5983 if (!hlist) {
5984 err = -ENOMEM;
5985 goto exit;
5986 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005987 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005988 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005989 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005990exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005991 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005992
5993 return err;
5994}
5995
5996static int swevent_hlist_get(struct perf_event *event)
5997{
5998 int err;
5999 int cpu, failed_cpu;
6000
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006001 get_online_cpus();
6002 for_each_possible_cpu(cpu) {
6003 err = swevent_hlist_get_cpu(event, cpu);
6004 if (err) {
6005 failed_cpu = cpu;
6006 goto fail;
6007 }
6008 }
6009 put_online_cpus();
6010
6011 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02006012fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006013 for_each_possible_cpu(cpu) {
6014 if (cpu == failed_cpu)
6015 break;
6016 swevent_hlist_put_cpu(event, cpu);
6017 }
6018
6019 put_online_cpus();
6020 return err;
6021}
6022
Ingo Molnarc5905af2012-02-24 08:31:31 +01006023struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006024
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006025static void sw_perf_event_destroy(struct perf_event *event)
6026{
6027 u64 event_id = event->attr.config;
6028
6029 WARN_ON(event->parent);
6030
Ingo Molnarc5905af2012-02-24 08:31:31 +01006031 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006032 swevent_hlist_put(event);
6033}
6034
6035static int perf_swevent_init(struct perf_event *event)
6036{
Tommi Rantala8176cce2013-04-13 22:49:14 +03006037 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006038
6039 if (event->attr.type != PERF_TYPE_SOFTWARE)
6040 return -ENOENT;
6041
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006042 /*
6043 * no branch sampling for software events
6044 */
6045 if (has_branch_stack(event))
6046 return -EOPNOTSUPP;
6047
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006048 switch (event_id) {
6049 case PERF_COUNT_SW_CPU_CLOCK:
6050 case PERF_COUNT_SW_TASK_CLOCK:
6051 return -ENOENT;
6052
6053 default:
6054 break;
6055 }
6056
Dan Carpenterce677832010-10-24 21:50:42 +02006057 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006058 return -ENOENT;
6059
6060 if (!event->parent) {
6061 int err;
6062
6063 err = swevent_hlist_get(event);
6064 if (err)
6065 return err;
6066
Ingo Molnarc5905af2012-02-24 08:31:31 +01006067 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006068 event->destroy = sw_perf_event_destroy;
6069 }
6070
6071 return 0;
6072}
6073
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006074static int perf_swevent_event_idx(struct perf_event *event)
6075{
6076 return 0;
6077}
6078
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006079static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006080 .task_ctx_nr = perf_sw_context,
6081
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006082 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006083 .add = perf_swevent_add,
6084 .del = perf_swevent_del,
6085 .start = perf_swevent_start,
6086 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006087 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006088
6089 .event_idx = perf_swevent_event_idx,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006090};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006091
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006092#ifdef CONFIG_EVENT_TRACING
6093
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006094static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006095 struct perf_sample_data *data)
6096{
6097 void *record = data->raw->data;
6098
6099 if (likely(!event->filter) || filter_match_preds(event->filter, record))
6100 return 1;
6101 return 0;
6102}
6103
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006104static int perf_tp_event_match(struct perf_event *event,
6105 struct perf_sample_data *data,
6106 struct pt_regs *regs)
6107{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01006108 if (event->hw.state & PERF_HES_STOPPED)
6109 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02006110 /*
6111 * All tracepoints are from kernel-space.
6112 */
6113 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006114 return 0;
6115
6116 if (!perf_tp_filter_match(event, data))
6117 return 0;
6118
6119 return 1;
6120}
6121
6122void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006123 struct pt_regs *regs, struct hlist_head *head, int rctx,
6124 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006125{
6126 struct perf_sample_data data;
6127 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006128
6129 struct perf_raw_record raw = {
6130 .size = entry_size,
6131 .data = record,
6132 };
6133
Robert Richterfd0d0002012-04-02 20:19:08 +02006134 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006135 data.raw = &raw;
6136
Sasha Levinb67bfe02013-02-27 17:06:00 -08006137 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006138 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006139 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006140 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006141
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006142 /*
6143 * If we got specified a target task, also iterate its context and
6144 * deliver this event there too.
6145 */
6146 if (task && task != current) {
6147 struct perf_event_context *ctx;
6148 struct trace_entry *entry = record;
6149
6150 rcu_read_lock();
6151 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
6152 if (!ctx)
6153 goto unlock;
6154
6155 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
6156 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6157 continue;
6158 if (event->attr.config != entry->type)
6159 continue;
6160 if (perf_tp_event_match(event, &data, regs))
6161 perf_swevent_event(event, count, &data, regs);
6162 }
6163unlock:
6164 rcu_read_unlock();
6165 }
6166
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006167 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006168}
6169EXPORT_SYMBOL_GPL(perf_tp_event);
6170
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006171static void tp_perf_event_destroy(struct perf_event *event)
6172{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006173 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006174}
6175
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006176static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006177{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006178 int err;
6179
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006180 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6181 return -ENOENT;
6182
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006183 /*
6184 * no branch sampling for tracepoint events
6185 */
6186 if (has_branch_stack(event))
6187 return -EOPNOTSUPP;
6188
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006189 err = perf_trace_init(event);
6190 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006191 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006192
6193 event->destroy = tp_perf_event_destroy;
6194
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006195 return 0;
6196}
6197
6198static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006199 .task_ctx_nr = perf_sw_context,
6200
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006201 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006202 .add = perf_trace_add,
6203 .del = perf_trace_del,
6204 .start = perf_swevent_start,
6205 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006206 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006207
6208 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006209};
6210
6211static inline void perf_tp_register(void)
6212{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006213 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006214}
Li Zefan6fb29152009-10-15 11:21:42 +08006215
6216static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6217{
6218 char *filter_str;
6219 int ret;
6220
6221 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6222 return -EINVAL;
6223
6224 filter_str = strndup_user(arg, PAGE_SIZE);
6225 if (IS_ERR(filter_str))
6226 return PTR_ERR(filter_str);
6227
6228 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
6229
6230 kfree(filter_str);
6231 return ret;
6232}
6233
6234static void perf_event_free_filter(struct perf_event *event)
6235{
6236 ftrace_profile_free_filter(event);
6237}
6238
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006239#else
Li Zefan6fb29152009-10-15 11:21:42 +08006240
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006241static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006242{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006243}
Li Zefan6fb29152009-10-15 11:21:42 +08006244
6245static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6246{
6247 return -ENOENT;
6248}
6249
6250static void perf_event_free_filter(struct perf_event *event)
6251{
6252}
6253
Li Zefan07b139c2009-12-21 14:27:35 +08006254#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006255
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006256#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006257void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006258{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006259 struct perf_sample_data sample;
6260 struct pt_regs *regs = data;
6261
Robert Richterfd0d0002012-04-02 20:19:08 +02006262 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006263
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006264 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006265 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006266}
6267#endif
6268
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006269/*
6270 * hrtimer based swevent callback
6271 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006272
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006273static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006274{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006275 enum hrtimer_restart ret = HRTIMER_RESTART;
6276 struct perf_sample_data data;
6277 struct pt_regs *regs;
6278 struct perf_event *event;
6279 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006280
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006281 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006282
6283 if (event->state != PERF_EVENT_STATE_ACTIVE)
6284 return HRTIMER_NORESTART;
6285
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006286 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006287
Robert Richterfd0d0002012-04-02 20:19:08 +02006288 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006289 regs = get_irq_regs();
6290
6291 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08006292 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02006293 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006294 ret = HRTIMER_NORESTART;
6295 }
6296
6297 period = max_t(u64, 10000, event->hw.sample_period);
6298 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
6299
6300 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006301}
6302
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006303static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006304{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006305 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006306 s64 period;
6307
6308 if (!is_sampling_event(event))
6309 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006310
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006311 period = local64_read(&hwc->period_left);
6312 if (period) {
6313 if (period < 0)
6314 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006315
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006316 local64_set(&hwc->period_left, 0);
6317 } else {
6318 period = max_t(u64, 10000, hwc->sample_period);
6319 }
6320 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006321 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006322 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006323}
6324
6325static void perf_swevent_cancel_hrtimer(struct perf_event *event)
6326{
6327 struct hw_perf_event *hwc = &event->hw;
6328
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006329 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006330 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006331 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006332
6333 hrtimer_cancel(&hwc->hrtimer);
6334 }
6335}
6336
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006337static void perf_swevent_init_hrtimer(struct perf_event *event)
6338{
6339 struct hw_perf_event *hwc = &event->hw;
6340
6341 if (!is_sampling_event(event))
6342 return;
6343
6344 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6345 hwc->hrtimer.function = perf_swevent_hrtimer;
6346
6347 /*
6348 * Since hrtimers have a fixed rate, we can do a static freq->period
6349 * mapping and avoid the whole period adjust feedback stuff.
6350 */
6351 if (event->attr.freq) {
6352 long freq = event->attr.sample_freq;
6353
6354 event->attr.sample_period = NSEC_PER_SEC / freq;
6355 hwc->sample_period = event->attr.sample_period;
6356 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09006357 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006358 event->attr.freq = 0;
6359 }
6360}
6361
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006362/*
6363 * Software event: cpu wall time clock
6364 */
6365
6366static void cpu_clock_event_update(struct perf_event *event)
6367{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006368 s64 prev;
6369 u64 now;
6370
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006371 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006372 prev = local64_xchg(&event->hw.prev_count, now);
6373 local64_add(now - prev, &event->count);
6374}
6375
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006376static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006377{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006378 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006379 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006380}
6381
6382static void cpu_clock_event_stop(struct perf_event *event, int flags)
6383{
6384 perf_swevent_cancel_hrtimer(event);
6385 cpu_clock_event_update(event);
6386}
6387
6388static int cpu_clock_event_add(struct perf_event *event, int flags)
6389{
6390 if (flags & PERF_EF_START)
6391 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006392
6393 return 0;
6394}
6395
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006396static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006397{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006398 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006399}
6400
6401static void cpu_clock_event_read(struct perf_event *event)
6402{
6403 cpu_clock_event_update(event);
6404}
6405
6406static int cpu_clock_event_init(struct perf_event *event)
6407{
6408 if (event->attr.type != PERF_TYPE_SOFTWARE)
6409 return -ENOENT;
6410
6411 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
6412 return -ENOENT;
6413
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006414 /*
6415 * no branch sampling for software events
6416 */
6417 if (has_branch_stack(event))
6418 return -EOPNOTSUPP;
6419
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006420 perf_swevent_init_hrtimer(event);
6421
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006422 return 0;
6423}
6424
6425static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006426 .task_ctx_nr = perf_sw_context,
6427
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006428 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006429 .add = cpu_clock_event_add,
6430 .del = cpu_clock_event_del,
6431 .start = cpu_clock_event_start,
6432 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006433 .read = cpu_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006434
6435 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006436};
6437
6438/*
6439 * Software event: task time clock
6440 */
6441
6442static void task_clock_event_update(struct perf_event *event, u64 now)
6443{
6444 u64 prev;
6445 s64 delta;
6446
6447 prev = local64_xchg(&event->hw.prev_count, now);
6448 delta = now - prev;
6449 local64_add(delta, &event->count);
6450}
6451
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006452static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006453{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006454 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006455 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006456}
6457
6458static void task_clock_event_stop(struct perf_event *event, int flags)
6459{
6460 perf_swevent_cancel_hrtimer(event);
6461 task_clock_event_update(event, event->ctx->time);
6462}
6463
6464static int task_clock_event_add(struct perf_event *event, int flags)
6465{
6466 if (flags & PERF_EF_START)
6467 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006468
6469 return 0;
6470}
6471
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006472static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006473{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006474 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006475}
6476
6477static void task_clock_event_read(struct perf_event *event)
6478{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01006479 u64 now = perf_clock();
6480 u64 delta = now - event->ctx->timestamp;
6481 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006482
6483 task_clock_event_update(event, time);
6484}
6485
6486static int task_clock_event_init(struct perf_event *event)
6487{
6488 if (event->attr.type != PERF_TYPE_SOFTWARE)
6489 return -ENOENT;
6490
6491 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6492 return -ENOENT;
6493
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006494 /*
6495 * no branch sampling for software events
6496 */
6497 if (has_branch_stack(event))
6498 return -EOPNOTSUPP;
6499
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006500 perf_swevent_init_hrtimer(event);
6501
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006502 return 0;
6503}
6504
6505static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006506 .task_ctx_nr = perf_sw_context,
6507
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006508 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006509 .add = task_clock_event_add,
6510 .del = task_clock_event_del,
6511 .start = task_clock_event_start,
6512 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006513 .read = task_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006514
6515 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006516};
6517
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006518static void perf_pmu_nop_void(struct pmu *pmu)
6519{
6520}
6521
6522static int perf_pmu_nop_int(struct pmu *pmu)
6523{
6524 return 0;
6525}
6526
6527static void perf_pmu_start_txn(struct pmu *pmu)
6528{
6529 perf_pmu_disable(pmu);
6530}
6531
6532static int perf_pmu_commit_txn(struct pmu *pmu)
6533{
6534 perf_pmu_enable(pmu);
6535 return 0;
6536}
6537
6538static void perf_pmu_cancel_txn(struct pmu *pmu)
6539{
6540 perf_pmu_enable(pmu);
6541}
6542
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006543static int perf_event_idx_default(struct perf_event *event)
6544{
6545 return event->hw.idx + 1;
6546}
6547
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006548/*
6549 * Ensures all contexts with the same task_ctx_nr have the same
6550 * pmu_cpu_context too.
6551 */
Mark Rutland9e317042014-02-10 17:44:18 +00006552static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006553{
6554 struct pmu *pmu;
6555
6556 if (ctxn < 0)
6557 return NULL;
6558
6559 list_for_each_entry(pmu, &pmus, entry) {
6560 if (pmu->task_ctx_nr == ctxn)
6561 return pmu->pmu_cpu_context;
6562 }
6563
6564 return NULL;
6565}
6566
Peter Zijlstra51676952010-12-07 14:18:20 +01006567static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006568{
Peter Zijlstra51676952010-12-07 14:18:20 +01006569 int cpu;
6570
6571 for_each_possible_cpu(cpu) {
6572 struct perf_cpu_context *cpuctx;
6573
6574 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6575
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006576 if (cpuctx->unique_pmu == old_pmu)
6577 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01006578 }
6579}
6580
6581static void free_pmu_context(struct pmu *pmu)
6582{
6583 struct pmu *i;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006584
6585 mutex_lock(&pmus_lock);
6586 /*
6587 * Like a real lame refcount.
6588 */
Peter Zijlstra51676952010-12-07 14:18:20 +01006589 list_for_each_entry(i, &pmus, entry) {
6590 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6591 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006592 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01006593 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006594 }
6595
Peter Zijlstra51676952010-12-07 14:18:20 +01006596 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006597out:
6598 mutex_unlock(&pmus_lock);
6599}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006600static struct idr pmu_idr;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006601
Peter Zijlstraabe43402010-11-17 23:17:37 +01006602static ssize_t
6603type_show(struct device *dev, struct device_attribute *attr, char *page)
6604{
6605 struct pmu *pmu = dev_get_drvdata(dev);
6606
6607 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6608}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006609static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006610
Stephane Eranian62b85632013-04-03 14:21:34 +02006611static ssize_t
6612perf_event_mux_interval_ms_show(struct device *dev,
6613 struct device_attribute *attr,
6614 char *page)
6615{
6616 struct pmu *pmu = dev_get_drvdata(dev);
6617
6618 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6619}
6620
6621static ssize_t
6622perf_event_mux_interval_ms_store(struct device *dev,
6623 struct device_attribute *attr,
6624 const char *buf, size_t count)
6625{
6626 struct pmu *pmu = dev_get_drvdata(dev);
6627 int timer, cpu, ret;
6628
6629 ret = kstrtoint(buf, 0, &timer);
6630 if (ret)
6631 return ret;
6632
6633 if (timer < 1)
6634 return -EINVAL;
6635
6636 /* same value, noting to do */
6637 if (timer == pmu->hrtimer_interval_ms)
6638 return count;
6639
6640 pmu->hrtimer_interval_ms = timer;
6641
6642 /* update all cpuctx for this PMU */
6643 for_each_possible_cpu(cpu) {
6644 struct perf_cpu_context *cpuctx;
6645 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6646 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6647
6648 if (hrtimer_active(&cpuctx->hrtimer))
6649 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6650 }
6651
6652 return count;
6653}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006654static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02006655
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006656static struct attribute *pmu_dev_attrs[] = {
6657 &dev_attr_type.attr,
6658 &dev_attr_perf_event_mux_interval_ms.attr,
6659 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006660};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006661ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006662
6663static int pmu_bus_running;
6664static struct bus_type pmu_bus = {
6665 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006666 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006667};
6668
6669static void pmu_dev_release(struct device *dev)
6670{
6671 kfree(dev);
6672}
6673
6674static int pmu_dev_alloc(struct pmu *pmu)
6675{
6676 int ret = -ENOMEM;
6677
6678 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6679 if (!pmu->dev)
6680 goto out;
6681
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01006682 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01006683 device_initialize(pmu->dev);
6684 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6685 if (ret)
6686 goto free_dev;
6687
6688 dev_set_drvdata(pmu->dev, pmu);
6689 pmu->dev->bus = &pmu_bus;
6690 pmu->dev->release = pmu_dev_release;
6691 ret = device_add(pmu->dev);
6692 if (ret)
6693 goto free_dev;
6694
6695out:
6696 return ret;
6697
6698free_dev:
6699 put_device(pmu->dev);
6700 goto out;
6701}
6702
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006703static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006704static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006705
Mischa Jonker03d8e802013-06-04 11:45:48 +02006706int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006707{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006708 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006709
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006710 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006711 ret = -ENOMEM;
6712 pmu->pmu_disable_count = alloc_percpu(int);
6713 if (!pmu->pmu_disable_count)
6714 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006715
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006716 pmu->type = -1;
6717 if (!name)
6718 goto skip_type;
6719 pmu->name = name;
6720
6721 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08006722 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6723 if (type < 0) {
6724 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006725 goto free_pdc;
6726 }
6727 }
6728 pmu->type = type;
6729
Peter Zijlstraabe43402010-11-17 23:17:37 +01006730 if (pmu_bus_running) {
6731 ret = pmu_dev_alloc(pmu);
6732 if (ret)
6733 goto free_idr;
6734 }
6735
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006736skip_type:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006737 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6738 if (pmu->pmu_cpu_context)
6739 goto got_cpu_context;
6740
Wei Yongjunc4814202013-04-12 11:05:54 +08006741 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006742 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6743 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006744 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006745
6746 for_each_possible_cpu(cpu) {
6747 struct perf_cpu_context *cpuctx;
6748
6749 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006750 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006751 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006752 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006753 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006754 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02006755
6756 __perf_cpu_hrtimer_init(cpuctx, cpu);
6757
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006758 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006759 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006760 }
6761
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006762got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006763 if (!pmu->start_txn) {
6764 if (pmu->pmu_enable) {
6765 /*
6766 * If we have pmu_enable/pmu_disable calls, install
6767 * transaction stubs that use that to try and batch
6768 * hardware accesses.
6769 */
6770 pmu->start_txn = perf_pmu_start_txn;
6771 pmu->commit_txn = perf_pmu_commit_txn;
6772 pmu->cancel_txn = perf_pmu_cancel_txn;
6773 } else {
6774 pmu->start_txn = perf_pmu_nop_void;
6775 pmu->commit_txn = perf_pmu_nop_int;
6776 pmu->cancel_txn = perf_pmu_nop_void;
6777 }
6778 }
6779
6780 if (!pmu->pmu_enable) {
6781 pmu->pmu_enable = perf_pmu_nop_void;
6782 pmu->pmu_disable = perf_pmu_nop_void;
6783 }
6784
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006785 if (!pmu->event_idx)
6786 pmu->event_idx = perf_event_idx_default;
6787
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006788 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006789 ret = 0;
6790unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006791 mutex_unlock(&pmus_lock);
6792
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006793 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006794
Peter Zijlstraabe43402010-11-17 23:17:37 +01006795free_dev:
6796 device_del(pmu->dev);
6797 put_device(pmu->dev);
6798
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006799free_idr:
6800 if (pmu->type >= PERF_TYPE_MAX)
6801 idr_remove(&pmu_idr, pmu->type);
6802
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006803free_pdc:
6804 free_percpu(pmu->pmu_disable_count);
6805 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006806}
Yan, Zhengc464c762014-03-18 16:56:41 +08006807EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006808
6809void perf_pmu_unregister(struct pmu *pmu)
6810{
6811 mutex_lock(&pmus_lock);
6812 list_del_rcu(&pmu->entry);
6813 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006814
6815 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006816 * We dereference the pmu list under both SRCU and regular RCU, so
6817 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006818 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006819 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006820 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006821
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006822 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006823 if (pmu->type >= PERF_TYPE_MAX)
6824 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006825 device_del(pmu->dev);
6826 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006827 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006828}
Yan, Zhengc464c762014-03-18 16:56:41 +08006829EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006830
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006831struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006832{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006833 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006834 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08006835 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006836
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006837 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006838
6839 rcu_read_lock();
6840 pmu = idr_find(&pmu_idr, event->attr.type);
6841 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08006842 if (pmu) {
Yan, Zhengc464c762014-03-18 16:56:41 +08006843 if (!try_module_get(pmu->module)) {
6844 pmu = ERR_PTR(-ENODEV);
6845 goto unlock;
6846 }
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006847 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006848 ret = pmu->event_init(event);
6849 if (ret)
6850 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006851 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08006852 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006853
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006854 list_for_each_entry_rcu(pmu, &pmus, entry) {
Yan, Zhengc464c762014-03-18 16:56:41 +08006855 if (!try_module_get(pmu->module)) {
6856 pmu = ERR_PTR(-ENODEV);
6857 goto unlock;
6858 }
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006859 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006860 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006861 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006862 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006863
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006864 if (ret != -ENOENT) {
6865 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006866 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006867 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006868 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006869 pmu = ERR_PTR(-ENOENT);
6870unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006871 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006872
6873 return pmu;
6874}
6875
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006876static void account_event_cpu(struct perf_event *event, int cpu)
6877{
6878 if (event->parent)
6879 return;
6880
6881 if (has_branch_stack(event)) {
6882 if (!(event->attach_state & PERF_ATTACH_TASK))
6883 atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
6884 }
6885 if (is_cgroup_event(event))
6886 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
6887}
6888
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006889static void account_event(struct perf_event *event)
6890{
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006891 if (event->parent)
6892 return;
6893
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006894 if (event->attach_state & PERF_ATTACH_TASK)
6895 static_key_slow_inc(&perf_sched_events.key);
6896 if (event->attr.mmap || event->attr.mmap_data)
6897 atomic_inc(&nr_mmap_events);
6898 if (event->attr.comm)
6899 atomic_inc(&nr_comm_events);
6900 if (event->attr.task)
6901 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02006902 if (event->attr.freq) {
6903 if (atomic_inc_return(&nr_freq_events) == 1)
6904 tick_nohz_full_kick_all();
6905 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006906 if (has_branch_stack(event))
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006907 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006908 if (is_cgroup_event(event))
6909 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006910
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006911 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006912}
6913
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006914/*
6915 * Allocate and initialize a event structure
6916 */
6917static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006918perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006919 struct task_struct *task,
6920 struct perf_event *group_leader,
6921 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006922 perf_overflow_handler_t overflow_handler,
6923 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006924{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006925 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006926 struct perf_event *event;
6927 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006928 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006929
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006930 if ((unsigned)cpu >= nr_cpu_ids) {
6931 if (!task || cpu != -1)
6932 return ERR_PTR(-EINVAL);
6933 }
6934
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006935 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006936 if (!event)
6937 return ERR_PTR(-ENOMEM);
6938
6939 /*
6940 * Single events are their own group leaders, with an
6941 * empty sibling list:
6942 */
6943 if (!group_leader)
6944 group_leader = event;
6945
6946 mutex_init(&event->child_mutex);
6947 INIT_LIST_HEAD(&event->child_list);
6948
6949 INIT_LIST_HEAD(&event->group_entry);
6950 INIT_LIST_HEAD(&event->event_entry);
6951 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006952 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01006953 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01006954 INIT_HLIST_NODE(&event->hlist_entry);
6955
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006956
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006957 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006958 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006959
6960 mutex_init(&event->mmap_mutex);
6961
Al Viroa6fa9412012-08-20 14:59:25 +01006962 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006963 event->cpu = cpu;
6964 event->attr = *attr;
6965 event->group_leader = group_leader;
6966 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006967 event->oncpu = -1;
6968
6969 event->parent = parent_event;
6970
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08006971 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006972 event->id = atomic64_inc_return(&perf_event_id);
6973
6974 event->state = PERF_EVENT_STATE_INACTIVE;
6975
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006976 if (task) {
6977 event->attach_state = PERF_ATTACH_TASK;
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006978
6979 if (attr->type == PERF_TYPE_TRACEPOINT)
6980 event->hw.tp_target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006981#ifdef CONFIG_HAVE_HW_BREAKPOINT
6982 /*
6983 * hw_breakpoint is a bit difficult here..
6984 */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006985 else if (attr->type == PERF_TYPE_BREAKPOINT)
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006986 event->hw.bp_target = task;
6987#endif
6988 }
6989
Avi Kivity4dc0da82011-06-29 18:42:35 +03006990 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006991 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006992 context = parent_event->overflow_handler_context;
6993 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006994
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006995 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006996 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006997
Jiri Olsa0231bb52013-02-01 11:23:45 +01006998 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006999
7000 pmu = NULL;
7001
7002 hwc = &event->hw;
7003 hwc->sample_period = attr->sample_period;
7004 if (attr->freq && attr->sample_freq)
7005 hwc->sample_period = 1;
7006 hwc->last_period = hwc->sample_period;
7007
Peter Zijlstrae7850592010-05-21 14:43:08 +02007008 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007009
7010 /*
7011 * we currently do not support PERF_FORMAT_GROUP on inherited events
7012 */
7013 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007014 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007015
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007016 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007017 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007018 goto err_ns;
7019 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007020 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007021 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007022 }
7023
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007024 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007025 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
7026 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007027 if (err)
7028 goto err_pmu;
Stephane Eraniand010b332012-02-09 23:21:00 +01007029 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007030 }
7031
7032 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007033
7034err_pmu:
7035 if (event->destroy)
7036 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08007037 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007038err_ns:
7039 if (event->ns)
7040 put_pid_ns(event->ns);
7041 kfree(event);
7042
7043 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007044}
7045
7046static int perf_copy_attr(struct perf_event_attr __user *uattr,
7047 struct perf_event_attr *attr)
7048{
7049 u32 size;
7050 int ret;
7051
7052 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
7053 return -EFAULT;
7054
7055 /*
7056 * zero the full structure, so that a short copy will be nice.
7057 */
7058 memset(attr, 0, sizeof(*attr));
7059
7060 ret = get_user(size, &uattr->size);
7061 if (ret)
7062 return ret;
7063
7064 if (size > PAGE_SIZE) /* silly large */
7065 goto err_size;
7066
7067 if (!size) /* abi compat */
7068 size = PERF_ATTR_SIZE_VER0;
7069
7070 if (size < PERF_ATTR_SIZE_VER0)
7071 goto err_size;
7072
7073 /*
7074 * If we're handed a bigger struct than we know of,
7075 * ensure all the unknown bits are 0 - i.e. new
7076 * user-space does not rely on any kernel feature
7077 * extensions we dont know about yet.
7078 */
7079 if (size > sizeof(*attr)) {
7080 unsigned char __user *addr;
7081 unsigned char __user *end;
7082 unsigned char val;
7083
7084 addr = (void __user *)uattr + sizeof(*attr);
7085 end = (void __user *)uattr + size;
7086
7087 for (; addr < end; addr++) {
7088 ret = get_user(val, addr);
7089 if (ret)
7090 return ret;
7091 if (val)
7092 goto err_size;
7093 }
7094 size = sizeof(*attr);
7095 }
7096
7097 ret = copy_from_user(attr, uattr, size);
7098 if (ret)
7099 return -EFAULT;
7100
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05307101 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007102 return -EINVAL;
7103
7104 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
7105 return -EINVAL;
7106
7107 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
7108 return -EINVAL;
7109
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007110 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
7111 u64 mask = attr->branch_sample_type;
7112
7113 /* only using defined bits */
7114 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
7115 return -EINVAL;
7116
7117 /* at least one branch bit must be set */
7118 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
7119 return -EINVAL;
7120
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007121 /* propagate priv level, when not set for branch */
7122 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
7123
7124 /* exclude_kernel checked on syscall entry */
7125 if (!attr->exclude_kernel)
7126 mask |= PERF_SAMPLE_BRANCH_KERNEL;
7127
7128 if (!attr->exclude_user)
7129 mask |= PERF_SAMPLE_BRANCH_USER;
7130
7131 if (!attr->exclude_hv)
7132 mask |= PERF_SAMPLE_BRANCH_HV;
7133 /*
7134 * adjust user setting (for HW filter setup)
7135 */
7136 attr->branch_sample_type = mask;
7137 }
Stephane Eraniane7122092013-06-06 11:02:04 +02007138 /* privileged levels capture (kernel, hv): check permissions */
7139 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02007140 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7141 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007142 }
Jiri Olsa40189942012-08-07 15:20:37 +02007143
Jiri Olsac5ebced2012-08-07 15:20:40 +02007144 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02007145 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02007146 if (ret)
7147 return ret;
7148 }
7149
7150 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
7151 if (!arch_perf_have_user_stack_dump())
7152 return -ENOSYS;
7153
7154 /*
7155 * We have __u32 type for the size, but so far
7156 * we can only use __u16 as maximum due to the
7157 * __u16 sample size limit.
7158 */
7159 if (attr->sample_stack_user >= USHRT_MAX)
7160 ret = -EINVAL;
7161 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
7162 ret = -EINVAL;
7163 }
Jiri Olsa40189942012-08-07 15:20:37 +02007164
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007165out:
7166 return ret;
7167
7168err_size:
7169 put_user(sizeof(*attr), &uattr->size);
7170 ret = -E2BIG;
7171 goto out;
7172}
7173
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007174static int
7175perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007176{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007177 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007178 int ret = -EINVAL;
7179
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007180 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007181 goto set;
7182
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007183 /* don't allow circular references */
7184 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007185 goto out;
7186
Peter Zijlstra0f139302010-05-20 14:35:15 +02007187 /*
7188 * Don't allow cross-cpu buffers
7189 */
7190 if (output_event->cpu != event->cpu)
7191 goto out;
7192
7193 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02007194 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02007195 */
7196 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
7197 goto out;
7198
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007199set:
7200 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007201 /* Can't redirect output if we've got an active mmap() */
7202 if (atomic_read(&event->mmap_count))
7203 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007204
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007205 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02007206 /* get the rb we want to redirect to */
7207 rb = ring_buffer_get(output_event);
7208 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007209 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007210 }
7211
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007212 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02007213
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007214 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007215unlock:
7216 mutex_unlock(&event->mmap_mutex);
7217
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007218out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007219 return ret;
7220}
7221
7222/**
7223 * sys_perf_event_open - open a performance event, associate it to a task/cpu
7224 *
7225 * @attr_uptr: event_id type attributes for monitoring/sampling
7226 * @pid: target pid
7227 * @cpu: target cpu
7228 * @group_fd: group leader event fd
7229 */
7230SYSCALL_DEFINE5(perf_event_open,
7231 struct perf_event_attr __user *, attr_uptr,
7232 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
7233{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007234 struct perf_event *group_leader = NULL, *output_event = NULL;
7235 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007236 struct perf_event_attr attr;
7237 struct perf_event_context *ctx;
7238 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04007239 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07007240 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007241 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04007242 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007243 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007244 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01007245 int f_flags = O_RDWR;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007246
7247 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02007248 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007249 return -EINVAL;
7250
7251 err = perf_copy_attr(attr_uptr, &attr);
7252 if (err)
7253 return err;
7254
7255 if (!attr.exclude_kernel) {
7256 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7257 return -EACCES;
7258 }
7259
7260 if (attr.freq) {
7261 if (attr.sample_freq > sysctl_perf_event_sample_rate)
7262 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02007263 } else {
7264 if (attr.sample_period & (1ULL << 63))
7265 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007266 }
7267
Stephane Eraniane5d13672011-02-14 11:20:01 +02007268 /*
7269 * In cgroup mode, the pid argument is used to pass the fd
7270 * opened to the cgroup directory in cgroupfs. The cpu argument
7271 * designates the cpu on which to monitor threads from that
7272 * cgroup.
7273 */
7274 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
7275 return -EINVAL;
7276
Yann Droneauda21b0b32014-01-05 21:36:33 +01007277 if (flags & PERF_FLAG_FD_CLOEXEC)
7278 f_flags |= O_CLOEXEC;
7279
7280 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007281 if (event_fd < 0)
7282 return event_fd;
7283
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007284 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04007285 err = perf_fget_light(group_fd, &group);
7286 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007287 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04007288 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007289 if (flags & PERF_FLAG_FD_OUTPUT)
7290 output_event = group_leader;
7291 if (flags & PERF_FLAG_FD_NO_GROUP)
7292 group_leader = NULL;
7293 }
7294
Stephane Eraniane5d13672011-02-14 11:20:01 +02007295 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007296 task = find_lively_task_by_vpid(pid);
7297 if (IS_ERR(task)) {
7298 err = PTR_ERR(task);
7299 goto err_group_fd;
7300 }
7301 }
7302
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007303 if (task && group_leader &&
7304 group_leader->attr.inherit != attr.inherit) {
7305 err = -EINVAL;
7306 goto err_task;
7307 }
7308
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007309 get_online_cpus();
7310
Avi Kivity4dc0da82011-06-29 18:42:35 +03007311 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
7312 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007313 if (IS_ERR(event)) {
7314 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007315 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007316 }
7317
Stephane Eraniane5d13672011-02-14 11:20:01 +02007318 if (flags & PERF_FLAG_PID_CGROUP) {
7319 err = perf_cgroup_connect(pid, event, &attr, group_leader);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007320 if (err) {
7321 __free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007322 goto err_cpus;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007323 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02007324 }
7325
Vince Weaver53b25332014-05-16 17:12:12 -04007326 if (is_sampling_event(event)) {
7327 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
7328 err = -ENOTSUPP;
7329 goto err_alloc;
7330 }
7331 }
7332
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007333 account_event(event);
7334
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007335 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007336 * Special case software events and allow them to be part of
7337 * any hardware group.
7338 */
7339 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007340
7341 if (group_leader &&
7342 (is_software_event(event) != is_software_event(group_leader))) {
7343 if (is_software_event(event)) {
7344 /*
7345 * If event and group_leader are not both a software
7346 * event, and event is, then group leader is not.
7347 *
7348 * Allow the addition of software events to !software
7349 * groups, this is safe because software events never
7350 * fail to schedule.
7351 */
7352 pmu = group_leader->pmu;
7353 } else if (is_software_event(group_leader) &&
7354 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
7355 /*
7356 * In case the group is a pure software group, and we
7357 * try to add a hardware event, move the whole group to
7358 * the hardware context.
7359 */
7360 move_group = 1;
7361 }
7362 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007363
7364 /*
7365 * Get the target context (task or percpu):
7366 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007367 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007368 if (IS_ERR(ctx)) {
7369 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007370 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007371 }
7372
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02007373 if (task) {
7374 put_task_struct(task);
7375 task = NULL;
7376 }
7377
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007378 /*
7379 * Look up the group leader (we will attach this event to it):
7380 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007381 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007382 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007383
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007384 /*
7385 * Do not allow a recursive hierarchy (this new sibling
7386 * becoming part of another group-sibling):
7387 */
7388 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007389 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007390 /*
7391 * Do not allow to attach to a group in a different
7392 * task or CPU context:
7393 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007394 if (move_group) {
7395 if (group_leader->ctx->type != ctx->type)
7396 goto err_context;
7397 } else {
7398 if (group_leader->ctx != ctx)
7399 goto err_context;
7400 }
7401
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007402 /*
7403 * Only a group leader can be exclusive or pinned
7404 */
7405 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007406 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007407 }
7408
7409 if (output_event) {
7410 err = perf_event_set_output(event, output_event);
7411 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007412 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007413 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007414
Yann Droneauda21b0b32014-01-05 21:36:33 +01007415 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
7416 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007417 if (IS_ERR(event_file)) {
7418 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007419 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04007420 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007421
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007422 if (move_group) {
7423 struct perf_event_context *gctx = group_leader->ctx;
7424
7425 mutex_lock(&gctx->mutex);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007426 perf_remove_from_context(group_leader, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007427
7428 /*
7429 * Removing from the context ends up with disabled
7430 * event. What we want here is event in the initial
7431 * startup state, ready to be add into new context.
7432 */
7433 perf_event__state_init(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007434 list_for_each_entry(sibling, &group_leader->sibling_list,
7435 group_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007436 perf_remove_from_context(sibling, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007437 perf_event__state_init(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007438 put_ctx(gctx);
7439 }
7440 mutex_unlock(&gctx->mutex);
7441 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007442 }
7443
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007444 WARN_ON_ONCE(ctx->parent_ctx);
7445 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007446
7447 if (move_group) {
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007448 synchronize_rcu();
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007449 perf_install_in_context(ctx, group_leader, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007450 get_ctx(ctx);
7451 list_for_each_entry(sibling, &group_leader->sibling_list,
7452 group_entry) {
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007453 perf_install_in_context(ctx, sibling, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007454 get_ctx(ctx);
7455 }
7456 }
7457
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007458 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007459 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007460 mutex_unlock(&ctx->mutex);
7461
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007462 put_online_cpus();
7463
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007464 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01007465
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007466 mutex_lock(&current->perf_event_mutex);
7467 list_add_tail(&event->owner_entry, &current->perf_event_list);
7468 mutex_unlock(&current->perf_event_mutex);
7469
Peter Zijlstra8a495422010-05-27 15:47:49 +02007470 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007471 * Precalculate sample_data sizes
7472 */
7473 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007474 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007475
7476 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02007477 * Drop the reference on the group_event after placing the
7478 * new event on the sibling_list. This ensures destruction
7479 * of the group leader will find the pointer to itself in
7480 * perf_group_detach().
7481 */
Al Viro2903ff02012-08-28 12:52:22 -04007482 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007483 fd_install(event_fd, event_file);
7484 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007485
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007486err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007487 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04007488 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007489err_alloc:
7490 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007491err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007492 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007493err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007494 if (task)
7495 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007496err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04007497 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007498err_fd:
7499 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007500 return err;
7501}
7502
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007503/**
7504 * perf_event_create_kernel_counter
7505 *
7506 * @attr: attributes of the counter to create
7507 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07007508 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007509 */
7510struct perf_event *
7511perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07007512 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007513 perf_overflow_handler_t overflow_handler,
7514 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007515{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007516 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007517 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007518 int err;
7519
7520 /*
7521 * Get the target context (task or percpu):
7522 */
7523
Avi Kivity4dc0da82011-06-29 18:42:35 +03007524 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7525 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007526 if (IS_ERR(event)) {
7527 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007528 goto err;
7529 }
7530
Jiri Olsaf8697762014-08-01 14:33:01 +02007531 /* Mark owner so we could distinguish it from user events. */
7532 event->owner = EVENT_OWNER_KERNEL;
7533
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007534 account_event(event);
7535
Matt Helsley38a81da2010-09-13 13:01:20 -07007536 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007537 if (IS_ERR(ctx)) {
7538 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007539 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007540 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007541
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007542 WARN_ON_ONCE(ctx->parent_ctx);
7543 mutex_lock(&ctx->mutex);
7544 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007545 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007546 mutex_unlock(&ctx->mutex);
7547
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007548 return event;
7549
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007550err_free:
7551 free_event(event);
7552err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007553 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007554}
7555EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7556
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007557void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7558{
7559 struct perf_event_context *src_ctx;
7560 struct perf_event_context *dst_ctx;
7561 struct perf_event *event, *tmp;
7562 LIST_HEAD(events);
7563
7564 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7565 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7566
7567 mutex_lock(&src_ctx->mutex);
7568 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7569 event_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007570 perf_remove_from_context(event, false);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007571 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007572 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02007573 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007574 }
7575 mutex_unlock(&src_ctx->mutex);
7576
7577 synchronize_rcu();
7578
7579 mutex_lock(&dst_ctx->mutex);
Peter Zijlstra98861672013-10-03 16:02:23 +02007580 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7581 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007582 if (event->state >= PERF_EVENT_STATE_OFF)
7583 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007584 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007585 perf_install_in_context(dst_ctx, event, dst_cpu);
7586 get_ctx(dst_ctx);
7587 }
7588 mutex_unlock(&dst_ctx->mutex);
7589}
7590EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7591
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007592static void sync_child_event(struct perf_event *child_event,
7593 struct task_struct *child)
7594{
7595 struct perf_event *parent_event = child_event->parent;
7596 u64 child_val;
7597
7598 if (child_event->attr.inherit_stat)
7599 perf_event_read_event(child_event, child);
7600
Peter Zijlstrab5e58792010-05-21 14:43:12 +02007601 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007602
7603 /*
7604 * Add back the child's count to the parent's count:
7605 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02007606 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007607 atomic64_add(child_event->total_time_enabled,
7608 &parent_event->child_total_time_enabled);
7609 atomic64_add(child_event->total_time_running,
7610 &parent_event->child_total_time_running);
7611
7612 /*
7613 * Remove this event from the parent's list
7614 */
7615 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7616 mutex_lock(&parent_event->child_mutex);
7617 list_del_init(&child_event->child_list);
7618 mutex_unlock(&parent_event->child_mutex);
7619
7620 /*
Jiri Olsadc633982014-09-12 13:18:26 +02007621 * Make sure user/parent get notified, that we just
7622 * lost one event.
7623 */
7624 perf_event_wakeup(parent_event);
7625
7626 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007627 * Release the parent event, if this was the last
7628 * reference to it.
7629 */
Al Viroa6fa9412012-08-20 14:59:25 +01007630 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007631}
7632
7633static void
7634__perf_event_exit_task(struct perf_event *child_event,
7635 struct perf_event_context *child_ctx,
7636 struct task_struct *child)
7637{
Peter Zijlstra1903d502014-07-15 17:27:27 +02007638 /*
7639 * Do not destroy the 'original' grouping; because of the context
7640 * switch optimization the original events could've ended up in a
7641 * random child task.
7642 *
7643 * If we were to destroy the original group, all group related
7644 * operations would cease to function properly after this random
7645 * child dies.
7646 *
7647 * Do destroy all inherited groups, we don't care about those
7648 * and being thorough is better.
7649 */
7650 perf_remove_from_context(child_event, !!child_event->parent);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007651
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007652 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007653 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007654 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007655 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007656 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007657 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007658 sync_child_event(child_event, child);
7659 free_event(child_event);
Jiri Olsa179033b2014-08-07 11:48:26 -04007660 } else {
7661 child_event->state = PERF_EVENT_STATE_EXIT;
7662 perf_event_wakeup(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007663 }
7664}
7665
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007666static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007667{
Peter Zijlstraebf905f2014-05-29 19:00:24 +02007668 struct perf_event *child_event, *next;
Peter Zijlstra211de6e2014-09-30 19:23:08 +02007669 struct perf_event_context *child_ctx, *clone_ctx = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007670 unsigned long flags;
7671
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007672 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007673 perf_event_task(child, NULL, 0);
7674 return;
7675 }
7676
7677 local_irq_save(flags);
7678 /*
7679 * We can't reschedule here because interrupts are disabled,
7680 * and either child is current or it is a task that can't be
7681 * scheduled, so we are now safe from rescheduling changing
7682 * our context.
7683 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01007684 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007685
7686 /*
7687 * Take the context lock here so that if find_get_context is
7688 * reading child->perf_event_ctxp, we wait until it has
7689 * incremented the context's refcount before we do put_ctx below.
7690 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007691 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02007692 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007693 child->perf_event_ctxp[ctxn] = NULL;
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007694
7695 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007696 * If this context is a clone; unclone it so it can't get
7697 * swapped to another process while we're removing all
7698 * the events from it.
7699 */
Peter Zijlstra211de6e2014-09-30 19:23:08 +02007700 clone_ctx = unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01007701 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007702 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007703
Peter Zijlstra211de6e2014-09-30 19:23:08 +02007704 if (clone_ctx)
7705 put_ctx(clone_ctx);
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007706
7707 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007708 * Report the task dead after unscheduling the events so that we
7709 * won't get any samples after PERF_RECORD_EXIT. We can however still
7710 * get a few PERF_RECORD_READ events.
7711 */
7712 perf_event_task(child, child_ctx, 0);
7713
7714 /*
7715 * We can recurse on the same lock type through:
7716 *
7717 * __perf_event_exit_task()
7718 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01007719 * put_event()
7720 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007721 *
7722 * But since its the parent context it won't be the same instance.
7723 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02007724 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007725
Peter Zijlstraebf905f2014-05-29 19:00:24 +02007726 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007727 __perf_event_exit_task(child_event, child_ctx, child);
7728
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007729 mutex_unlock(&child_ctx->mutex);
7730
7731 put_ctx(child_ctx);
7732}
7733
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007734/*
7735 * When a child task exits, feed back event values to parent events.
7736 */
7737void perf_event_exit_task(struct task_struct *child)
7738{
Peter Zijlstra88821352010-11-09 19:01:43 +01007739 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007740 int ctxn;
7741
Peter Zijlstra88821352010-11-09 19:01:43 +01007742 mutex_lock(&child->perf_event_mutex);
7743 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
7744 owner_entry) {
7745 list_del_init(&event->owner_entry);
7746
7747 /*
7748 * Ensure the list deletion is visible before we clear
7749 * the owner, closes a race against perf_release() where
7750 * we need to serialize on the owner->perf_event_mutex.
7751 */
7752 smp_wmb();
7753 event->owner = NULL;
7754 }
7755 mutex_unlock(&child->perf_event_mutex);
7756
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007757 for_each_task_context_nr(ctxn)
7758 perf_event_exit_task_context(child, ctxn);
7759}
7760
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007761static void perf_free_event(struct perf_event *event,
7762 struct perf_event_context *ctx)
7763{
7764 struct perf_event *parent = event->parent;
7765
7766 if (WARN_ON_ONCE(!parent))
7767 return;
7768
7769 mutex_lock(&parent->child_mutex);
7770 list_del_init(&event->child_list);
7771 mutex_unlock(&parent->child_mutex);
7772
Al Viroa6fa9412012-08-20 14:59:25 +01007773 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007774
Peter Zijlstra8a495422010-05-27 15:47:49 +02007775 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007776 list_del_event(event, ctx);
7777 free_event(event);
7778}
7779
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007780/*
7781 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007782 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007783 */
7784void perf_event_free_task(struct task_struct *task)
7785{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007786 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007787 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007788 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007789
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007790 for_each_task_context_nr(ctxn) {
7791 ctx = task->perf_event_ctxp[ctxn];
7792 if (!ctx)
7793 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007794
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007795 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007796again:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007797 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7798 group_entry)
7799 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007800
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007801 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7802 group_entry)
7803 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007804
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007805 if (!list_empty(&ctx->pinned_groups) ||
7806 !list_empty(&ctx->flexible_groups))
7807 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007808
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007809 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007810
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007811 put_ctx(ctx);
7812 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007813}
7814
Peter Zijlstra4e231c72010-09-09 21:01:59 +02007815void perf_event_delayed_put(struct task_struct *task)
7816{
7817 int ctxn;
7818
7819 for_each_task_context_nr(ctxn)
7820 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7821}
7822
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007823/*
7824 * inherit a event from parent task to child task:
7825 */
7826static struct perf_event *
7827inherit_event(struct perf_event *parent_event,
7828 struct task_struct *parent,
7829 struct perf_event_context *parent_ctx,
7830 struct task_struct *child,
7831 struct perf_event *group_leader,
7832 struct perf_event_context *child_ctx)
7833{
Jiri Olsa1929def2014-09-12 13:18:27 +02007834 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007835 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02007836 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007837
7838 /*
7839 * Instead of creating recursive hierarchies of events,
7840 * we link inherited events back to the original parent,
7841 * which has a filp for sure, which we use as the reference
7842 * count:
7843 */
7844 if (parent_event->parent)
7845 parent_event = parent_event->parent;
7846
7847 child_event = perf_event_alloc(&parent_event->attr,
7848 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007849 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007850 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007851 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007852 if (IS_ERR(child_event))
7853 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01007854
Jiri Olsafadfe7b2014-08-01 14:33:02 +02007855 if (is_orphaned_event(parent_event) ||
7856 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Al Viroa6fa9412012-08-20 14:59:25 +01007857 free_event(child_event);
7858 return NULL;
7859 }
7860
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007861 get_ctx(child_ctx);
7862
7863 /*
7864 * Make the child state follow the state of the parent event,
7865 * not its attr.disabled bit. We hold the parent's mutex,
7866 * so we won't race with perf_event_{en, dis}able_family.
7867 */
Jiri Olsa1929def2014-09-12 13:18:27 +02007868 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007869 child_event->state = PERF_EVENT_STATE_INACTIVE;
7870 else
7871 child_event->state = PERF_EVENT_STATE_OFF;
7872
7873 if (parent_event->attr.freq) {
7874 u64 sample_period = parent_event->hw.sample_period;
7875 struct hw_perf_event *hwc = &child_event->hw;
7876
7877 hwc->sample_period = sample_period;
7878 hwc->last_period = sample_period;
7879
7880 local64_set(&hwc->period_left, sample_period);
7881 }
7882
7883 child_event->ctx = child_ctx;
7884 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007885 child_event->overflow_handler_context
7886 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007887
7888 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02007889 * Precalculate sample_data sizes
7890 */
7891 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007892 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02007893
7894 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007895 * Link it up in the child's context:
7896 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02007897 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007898 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02007899 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007900
7901 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007902 * Link this into the parent event's child list
7903 */
7904 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7905 mutex_lock(&parent_event->child_mutex);
7906 list_add_tail(&child_event->child_list, &parent_event->child_list);
7907 mutex_unlock(&parent_event->child_mutex);
7908
7909 return child_event;
7910}
7911
7912static int inherit_group(struct perf_event *parent_event,
7913 struct task_struct *parent,
7914 struct perf_event_context *parent_ctx,
7915 struct task_struct *child,
7916 struct perf_event_context *child_ctx)
7917{
7918 struct perf_event *leader;
7919 struct perf_event *sub;
7920 struct perf_event *child_ctr;
7921
7922 leader = inherit_event(parent_event, parent, parent_ctx,
7923 child, NULL, child_ctx);
7924 if (IS_ERR(leader))
7925 return PTR_ERR(leader);
7926 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7927 child_ctr = inherit_event(sub, parent, parent_ctx,
7928 child, leader, child_ctx);
7929 if (IS_ERR(child_ctr))
7930 return PTR_ERR(child_ctr);
7931 }
7932 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007933}
7934
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007935static int
7936inherit_task_group(struct perf_event *event, struct task_struct *parent,
7937 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007938 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007939 int *inherited_all)
7940{
7941 int ret;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007942 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007943
7944 if (!event->attr.inherit) {
7945 *inherited_all = 0;
7946 return 0;
7947 }
7948
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007949 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007950 if (!child_ctx) {
7951 /*
7952 * This is executed from the parent task context, so
7953 * inherit events that have been marked for cloning.
7954 * First allocate and initialize a context for the
7955 * child.
7956 */
7957
Jiri Olsa734df5a2013-07-09 17:44:10 +02007958 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007959 if (!child_ctx)
7960 return -ENOMEM;
7961
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007962 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007963 }
7964
7965 ret = inherit_group(event, parent, parent_ctx,
7966 child, child_ctx);
7967
7968 if (ret)
7969 *inherited_all = 0;
7970
7971 return ret;
7972}
7973
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007974/*
7975 * Initialize the perf_event context in task_struct
7976 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02007977static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007978{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007979 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007980 struct perf_event_context *cloned_ctx;
7981 struct perf_event *event;
7982 struct task_struct *parent = current;
7983 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007984 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007985 int ret = 0;
7986
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007987 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007988 return 0;
7989
7990 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007991 * If the parent's context is a clone, pin it so it won't get
7992 * swapped under us.
7993 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007994 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02007995 if (!parent_ctx)
7996 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007997
7998 /*
7999 * No need to check if parent_ctx != NULL here; since we saw
8000 * it non-NULL earlier, the only reason for it to become NULL
8001 * is if we exit, and since we're currently in the middle of
8002 * a fork we can't be exiting at the same time.
8003 */
8004
8005 /*
8006 * Lock the parent list. No need to lock the child - not PID
8007 * hashed yet and not running, so nobody can access it.
8008 */
8009 mutex_lock(&parent_ctx->mutex);
8010
8011 /*
8012 * We dont have to disable NMIs - we are only looking at
8013 * the list, not manipulating it:
8014 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008015 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008016 ret = inherit_task_group(event, parent, parent_ctx,
8017 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008018 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008019 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008020 }
8021
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008022 /*
8023 * We can't hold ctx->lock when iterating the ->flexible_group list due
8024 * to allocations, but we need to prevent rotation because
8025 * rotate_ctx() will change the list from interrupt context.
8026 */
8027 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
8028 parent_ctx->rotate_disable = 1;
8029 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
8030
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008031 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008032 ret = inherit_task_group(event, parent, parent_ctx,
8033 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008034 if (ret)
8035 break;
8036 }
8037
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008038 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
8039 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008040
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008041 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008042
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01008043 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008044 /*
8045 * Mark the child context as a clone of the parent
8046 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008047 *
8048 * Note that if the parent is a clone, the holding of
8049 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008050 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008051 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008052 if (cloned_ctx) {
8053 child_ctx->parent_ctx = cloned_ctx;
8054 child_ctx->parent_gen = parent_ctx->parent_gen;
8055 } else {
8056 child_ctx->parent_ctx = parent_ctx;
8057 child_ctx->parent_gen = parent_ctx->generation;
8058 }
8059 get_ctx(child_ctx->parent_ctx);
8060 }
8061
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008062 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008063 mutex_unlock(&parent_ctx->mutex);
8064
8065 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008066 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008067
8068 return ret;
8069}
8070
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008071/*
8072 * Initialize the perf_event context in task_struct
8073 */
8074int perf_event_init_task(struct task_struct *child)
8075{
8076 int ctxn, ret;
8077
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01008078 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
8079 mutex_init(&child->perf_event_mutex);
8080 INIT_LIST_HEAD(&child->perf_event_list);
8081
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008082 for_each_task_context_nr(ctxn) {
8083 ret = perf_event_init_context(child, ctxn);
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07008084 if (ret) {
8085 perf_event_free_task(child);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008086 return ret;
Peter Zijlstra6c72e3502014-10-02 16:17:02 -07008087 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02008088 }
8089
8090 return 0;
8091}
8092
Paul Mackerras220b1402010-03-10 20:45:52 +11008093static void __init perf_event_init_all_cpus(void)
8094{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008095 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11008096 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11008097
8098 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008099 swhash = &per_cpu(swevent_htable, cpu);
8100 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02008101 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11008102 }
8103}
8104
Paul Gortmaker0db06282013-06-19 14:53:51 -04008105static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008106{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008107 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008108
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008109 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02008110 swhash->online = true;
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008111 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008112 struct swevent_hlist *hlist;
8113
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008114 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
8115 WARN_ON(!hlist);
8116 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008117 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008118 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008119}
8120
Peter Zijlstrac2774432010-12-08 15:29:02 +01008121#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02008122static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008123{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02008124 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
8125
8126 WARN_ON(!irqs_disabled());
8127
8128 list_del_init(&cpuctx->rotation_list);
8129}
8130
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008131static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008132{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02008133 struct remove_event re = { .detach_group = false };
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008134 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008135
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008136 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02008137
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008138 rcu_read_lock();
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02008139 list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
8140 __perf_remove_from_context(&re);
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008141 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008142}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008143
8144static void perf_event_exit_cpu_context(int cpu)
8145{
8146 struct perf_event_context *ctx;
8147 struct pmu *pmu;
8148 int idx;
8149
8150 idx = srcu_read_lock(&pmus_srcu);
8151 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02008152 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008153
8154 mutex_lock(&ctx->mutex);
8155 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
8156 mutex_unlock(&ctx->mutex);
8157 }
8158 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008159}
8160
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008161static void perf_event_exit_cpu(int cpu)
8162{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008163 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008164
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008165 perf_event_exit_cpu_context(cpu);
8166
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008167 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02008168 swhash->online = false;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008169 swevent_hlist_release(swhash);
8170 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008171}
8172#else
8173static inline void perf_event_exit_cpu(int cpu) { }
8174#endif
8175
Peter Zijlstrac2774432010-12-08 15:29:02 +01008176static int
8177perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
8178{
8179 int cpu;
8180
8181 for_each_online_cpu(cpu)
8182 perf_event_exit_cpu(cpu);
8183
8184 return NOTIFY_OK;
8185}
8186
8187/*
8188 * Run the perf reboot notifier at the very last possible moment so that
8189 * the generic watchdog code runs as long as possible.
8190 */
8191static struct notifier_block perf_reboot_notifier = {
8192 .notifier_call = perf_reboot,
8193 .priority = INT_MIN,
8194};
8195
Paul Gortmaker0db06282013-06-19 14:53:51 -04008196static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008197perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
8198{
8199 unsigned int cpu = (long)hcpu;
8200
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008201 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008202
8203 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02008204 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008205 perf_event_init_cpu(cpu);
8206 break;
8207
Peter Zijlstra5e116372010-06-11 13:35:08 +02008208 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008209 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008210 perf_event_exit_cpu(cpu);
8211 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008212 default:
8213 break;
8214 }
8215
8216 return NOTIFY_OK;
8217}
8218
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008219void __init perf_event_init(void)
8220{
Jason Wessel3c502e72010-11-04 17:33:01 -05008221 int ret;
8222
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008223 idr_init(&pmu_idr);
8224
Paul Mackerras220b1402010-03-10 20:45:52 +11008225 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008226 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008227 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
8228 perf_pmu_register(&perf_cpu_clock, NULL, -1);
8229 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008230 perf_tp_register();
8231 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01008232 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05008233
8234 ret = init_hw_breakpoint();
8235 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02008236
8237 /* do not patch jump label more than once per second */
8238 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01008239
8240 /*
8241 * Build time assertion that we keep the data_head at the intended
8242 * location. IOW, validation we got the __reserved[] size right.
8243 */
8244 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
8245 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008246}
Peter Zijlstraabe43402010-11-17 23:17:37 +01008247
8248static int __init perf_event_sysfs_init(void)
8249{
8250 struct pmu *pmu;
8251 int ret;
8252
8253 mutex_lock(&pmus_lock);
8254
8255 ret = bus_register(&pmu_bus);
8256 if (ret)
8257 goto unlock;
8258
8259 list_for_each_entry(pmu, &pmus, entry) {
8260 if (!pmu->name || pmu->type < 0)
8261 continue;
8262
8263 ret = pmu_dev_alloc(pmu);
8264 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
8265 }
8266 pmu_bus_running = 1;
8267 ret = 0;
8268
8269unlock:
8270 mutex_unlock(&pmus_lock);
8271
8272 return ret;
8273}
8274device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008275
8276#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04008277static struct cgroup_subsys_state *
8278perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008279{
8280 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02008281
Li Zefan1b15d052011-03-03 14:26:06 +08008282 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008283 if (!jc)
8284 return ERR_PTR(-ENOMEM);
8285
Stephane Eraniane5d13672011-02-14 11:20:01 +02008286 jc->info = alloc_percpu(struct perf_cgroup_info);
8287 if (!jc->info) {
8288 kfree(jc);
8289 return ERR_PTR(-ENOMEM);
8290 }
8291
Stephane Eraniane5d13672011-02-14 11:20:01 +02008292 return &jc->css;
8293}
8294
Tejun Heoeb954192013-08-08 20:11:23 -04008295static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008296{
Tejun Heoeb954192013-08-08 20:11:23 -04008297 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
8298
Stephane Eraniane5d13672011-02-14 11:20:01 +02008299 free_percpu(jc->info);
8300 kfree(jc);
8301}
8302
8303static int __perf_cgroup_move(void *info)
8304{
8305 struct task_struct *task = info;
8306 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
8307 return 0;
8308}
8309
Tejun Heoeb954192013-08-08 20:11:23 -04008310static void perf_cgroup_attach(struct cgroup_subsys_state *css,
8311 struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008312{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008313 struct task_struct *task;
8314
Tejun Heo924f0d9a2014-02-13 06:58:41 -05008315 cgroup_taskset_for_each(task, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008316 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008317}
8318
Tejun Heoeb954192013-08-08 20:11:23 -04008319static void perf_cgroup_exit(struct cgroup_subsys_state *css,
8320 struct cgroup_subsys_state *old_css,
Li Zefan761b3ef52012-01-31 13:47:36 +08008321 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008322{
8323 /*
8324 * cgroup_exit() is called in the copy_process() failure path.
8325 * Ignore this case since the task hasn't ran yet, this avoids
8326 * trying to poke a half freed task state from generic code.
8327 */
8328 if (!(task->flags & PF_EXITING))
8329 return;
8330
Tejun Heobb9d97b2011-12-12 18:12:21 -08008331 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008332}
8333
Tejun Heo073219e2014-02-08 10:36:58 -05008334struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008335 .css_alloc = perf_cgroup_css_alloc,
8336 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008337 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008338 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02008339};
8340#endif /* CONFIG_CGROUP_PERF */