blob: b164cb07b30df175cb10398794a2cea0fbf01781 [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
252 local_samples_len = __get_cpu_var(running_sample_length);
253 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 */
274 local_samples_len = __get_cpu_var(running_sample_length);
275 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
276 local_samples_len += sample_len_ns;
277 __get_cpu_var(running_sample_length) = local_samples_len;
278
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_put_cgroup(struct perf_event *event)
405{
406 css_put(&event->cgrp->css);
407}
408
409static inline void perf_detach_cgroup(struct perf_event *event)
410{
411 perf_put_cgroup(event);
412 event->cgrp = NULL;
413}
414
415static inline int is_cgroup_event(struct perf_event *event)
416{
417 return event->cgrp != NULL;
418}
419
420static inline u64 perf_cgroup_event_time(struct perf_event *event)
421{
422 struct perf_cgroup_info *t;
423
424 t = per_cpu_ptr(event->cgrp->info, event->cpu);
425 return t->time;
426}
427
428static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
429{
430 struct perf_cgroup_info *info;
431 u64 now;
432
433 now = perf_clock();
434
435 info = this_cpu_ptr(cgrp->info);
436
437 info->time += now - info->timestamp;
438 info->timestamp = now;
439}
440
441static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
442{
443 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
444 if (cgrp_out)
445 __update_cgrp_time(cgrp_out);
446}
447
448static inline void update_cgrp_time_from_event(struct perf_event *event)
449{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200450 struct perf_cgroup *cgrp;
451
Stephane Eraniane5d13672011-02-14 11:20:01 +0200452 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200453 * ensure we access cgroup data only when needed and
454 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200455 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200456 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200457 return;
458
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200459 cgrp = perf_cgroup_from_task(current);
460 /*
461 * Do not update time when cgroup is not active
462 */
463 if (cgrp == event->cgrp)
464 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200465}
466
467static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200468perf_cgroup_set_timestamp(struct task_struct *task,
469 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200470{
471 struct perf_cgroup *cgrp;
472 struct perf_cgroup_info *info;
473
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200474 /*
475 * ctx->lock held by caller
476 * ensure we do not access cgroup data
477 * unless we have the cgroup pinned (css_get)
478 */
479 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200480 return;
481
482 cgrp = perf_cgroup_from_task(task);
483 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200484 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200485}
486
487#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
488#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
489
490/*
491 * reschedule events based on the cgroup constraint of task.
492 *
493 * mode SWOUT : schedule out everything
494 * mode SWIN : schedule in based on cgroup for next
495 */
496void perf_cgroup_switch(struct task_struct *task, int mode)
497{
498 struct perf_cpu_context *cpuctx;
499 struct pmu *pmu;
500 unsigned long flags;
501
502 /*
503 * disable interrupts to avoid geting nr_cgroup
504 * changes via __perf_event_disable(). Also
505 * avoids preemption.
506 */
507 local_irq_save(flags);
508
509 /*
510 * we reschedule only in the presence of cgroup
511 * constrained events.
512 */
513 rcu_read_lock();
514
515 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200516 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200517 if (cpuctx->unique_pmu != pmu)
518 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200519
Stephane Eraniane5d13672011-02-14 11:20:01 +0200520 /*
521 * perf_cgroup_events says at least one
522 * context on this CPU has cgroup events.
523 *
524 * ctx->nr_cgroups reports the number of cgroup
525 * events for a context.
526 */
527 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200528 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
529 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200530
531 if (mode & PERF_CGROUP_SWOUT) {
532 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
533 /*
534 * must not be done before ctxswout due
535 * to event_filter_match() in event_sched_out()
536 */
537 cpuctx->cgrp = NULL;
538 }
539
540 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200541 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200542 /*
543 * set cgrp before ctxsw in to allow
544 * event_filter_match() to not have to pass
545 * task around
Stephane Eraniane5d13672011-02-14 11:20:01 +0200546 */
547 cpuctx->cgrp = perf_cgroup_from_task(task);
548 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
549 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200550 perf_pmu_enable(cpuctx->ctx.pmu);
551 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200552 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200553 }
554
555 rcu_read_unlock();
556
557 local_irq_restore(flags);
558}
559
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200560static inline void perf_cgroup_sched_out(struct task_struct *task,
561 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200562{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200563 struct perf_cgroup *cgrp1;
564 struct perf_cgroup *cgrp2 = NULL;
565
566 /*
567 * we come here when we know perf_cgroup_events > 0
568 */
569 cgrp1 = perf_cgroup_from_task(task);
570
571 /*
572 * next is NULL when called from perf_event_enable_on_exec()
573 * that will systematically cause a cgroup_switch()
574 */
575 if (next)
576 cgrp2 = perf_cgroup_from_task(next);
577
578 /*
579 * only schedule out current cgroup events if we know
580 * that we are switching to a different cgroup. Otherwise,
581 * do no touch the cgroup events.
582 */
583 if (cgrp1 != cgrp2)
584 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200585}
586
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200587static inline void perf_cgroup_sched_in(struct task_struct *prev,
588 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200589{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200590 struct perf_cgroup *cgrp1;
591 struct perf_cgroup *cgrp2 = NULL;
592
593 /*
594 * we come here when we know perf_cgroup_events > 0
595 */
596 cgrp1 = perf_cgroup_from_task(task);
597
598 /* prev can never be NULL */
599 cgrp2 = perf_cgroup_from_task(prev);
600
601 /*
602 * only need to schedule in cgroup events if we are changing
603 * cgroup during ctxsw. Cgroup events were not scheduled
604 * out of ctxsw out if that was not the case.
605 */
606 if (cgrp1 != cgrp2)
607 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200608}
609
610static inline int perf_cgroup_connect(int fd, struct perf_event *event,
611 struct perf_event_attr *attr,
612 struct perf_event *group_leader)
613{
614 struct perf_cgroup *cgrp;
615 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400616 struct fd f = fdget(fd);
617 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200618
Al Viro2903ff02012-08-28 12:52:22 -0400619 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200620 return -EBADF;
621
Tejun Heoec903c02014-05-13 12:11:01 -0400622 css = css_tryget_online_from_dir(f.file->f_dentry,
623 &perf_event_cgrp_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800624 if (IS_ERR(css)) {
625 ret = PTR_ERR(css);
626 goto out;
627 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200628
629 cgrp = container_of(css, struct perf_cgroup, css);
630 event->cgrp = cgrp;
631
632 /*
633 * all events in a group must monitor
634 * the same cgroup because a task belongs
635 * to only one perf cgroup at a time
636 */
637 if (group_leader && group_leader->cgrp != cgrp) {
638 perf_detach_cgroup(event);
639 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200640 }
Li Zefan3db272c2011-03-03 14:25:37 +0800641out:
Al Viro2903ff02012-08-28 12:52:22 -0400642 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200643 return ret;
644}
645
646static inline void
647perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
648{
649 struct perf_cgroup_info *t;
650 t = per_cpu_ptr(event->cgrp->info, event->cpu);
651 event->shadow_ctx_time = now - t->timestamp;
652}
653
654static inline void
655perf_cgroup_defer_enabled(struct perf_event *event)
656{
657 /*
658 * when the current task's perf cgroup does not match
659 * the event's, we need to remember to call the
660 * perf_mark_enable() function the first time a task with
661 * a matching perf cgroup is scheduled in.
662 */
663 if (is_cgroup_event(event) && !perf_cgroup_match(event))
664 event->cgrp_defer_enabled = 1;
665}
666
667static inline void
668perf_cgroup_mark_enabled(struct perf_event *event,
669 struct perf_event_context *ctx)
670{
671 struct perf_event *sub;
672 u64 tstamp = perf_event_time(event);
673
674 if (!event->cgrp_defer_enabled)
675 return;
676
677 event->cgrp_defer_enabled = 0;
678
679 event->tstamp_enabled = tstamp - event->total_time_enabled;
680 list_for_each_entry(sub, &event->sibling_list, group_entry) {
681 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
682 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
683 sub->cgrp_defer_enabled = 0;
684 }
685 }
686}
687#else /* !CONFIG_CGROUP_PERF */
688
689static inline bool
690perf_cgroup_match(struct perf_event *event)
691{
692 return true;
693}
694
695static inline void perf_detach_cgroup(struct perf_event *event)
696{}
697
698static inline int is_cgroup_event(struct perf_event *event)
699{
700 return 0;
701}
702
703static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
704{
705 return 0;
706}
707
708static inline void update_cgrp_time_from_event(struct perf_event *event)
709{
710}
711
712static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
713{
714}
715
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200716static inline void perf_cgroup_sched_out(struct task_struct *task,
717 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200718{
719}
720
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200721static inline void perf_cgroup_sched_in(struct task_struct *prev,
722 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200723{
724}
725
726static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
727 struct perf_event_attr *attr,
728 struct perf_event *group_leader)
729{
730 return -EINVAL;
731}
732
733static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200734perf_cgroup_set_timestamp(struct task_struct *task,
735 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200736{
737}
738
739void
740perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
741{
742}
743
744static inline void
745perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
746{
747}
748
749static inline u64 perf_cgroup_event_time(struct perf_event *event)
750{
751 return 0;
752}
753
754static inline void
755perf_cgroup_defer_enabled(struct perf_event *event)
756{
757}
758
759static inline void
760perf_cgroup_mark_enabled(struct perf_event *event,
761 struct perf_event_context *ctx)
762{
763}
764#endif
765
Stephane Eranian9e630202013-04-03 14:21:33 +0200766/*
767 * set default to be dependent on timer tick just
768 * like original code
769 */
770#define PERF_CPU_HRTIMER (1000 / HZ)
771/*
772 * function must be called with interrupts disbled
773 */
774static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
775{
776 struct perf_cpu_context *cpuctx;
777 enum hrtimer_restart ret = HRTIMER_NORESTART;
778 int rotations = 0;
779
780 WARN_ON(!irqs_disabled());
781
782 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
783
784 rotations = perf_rotate_context(cpuctx);
785
786 /*
787 * arm timer if needed
788 */
789 if (rotations) {
790 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
791 ret = HRTIMER_RESTART;
792 }
793
794 return ret;
795}
796
797/* CPU is going down */
798void perf_cpu_hrtimer_cancel(int cpu)
799{
800 struct perf_cpu_context *cpuctx;
801 struct pmu *pmu;
802 unsigned long flags;
803
804 if (WARN_ON(cpu != smp_processor_id()))
805 return;
806
807 local_irq_save(flags);
808
809 rcu_read_lock();
810
811 list_for_each_entry_rcu(pmu, &pmus, entry) {
812 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
813
814 if (pmu->task_ctx_nr == perf_sw_context)
815 continue;
816
817 hrtimer_cancel(&cpuctx->hrtimer);
818 }
819
820 rcu_read_unlock();
821
822 local_irq_restore(flags);
823}
824
825static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
826{
827 struct hrtimer *hr = &cpuctx->hrtimer;
828 struct pmu *pmu = cpuctx->ctx.pmu;
Stephane Eranian62b85632013-04-03 14:21:34 +0200829 int timer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200830
831 /* no multiplexing needed for SW PMU */
832 if (pmu->task_ctx_nr == perf_sw_context)
833 return;
834
Stephane Eranian62b85632013-04-03 14:21:34 +0200835 /*
836 * check default is sane, if not set then force to
837 * default interval (1/tick)
838 */
839 timer = pmu->hrtimer_interval_ms;
840 if (timer < 1)
841 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
842
843 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200844
845 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
846 hr->function = perf_cpu_hrtimer_handler;
847}
848
849static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
850{
851 struct hrtimer *hr = &cpuctx->hrtimer;
852 struct pmu *pmu = cpuctx->ctx.pmu;
853
854 /* not for SW PMU */
855 if (pmu->task_ctx_nr == perf_sw_context)
856 return;
857
858 if (hrtimer_active(hr))
859 return;
860
861 if (!hrtimer_callback_running(hr))
862 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
863 0, HRTIMER_MODE_REL_PINNED, 0);
864}
865
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200866void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200867{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200868 int *count = this_cpu_ptr(pmu->pmu_disable_count);
869 if (!(*count)++)
870 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200871}
872
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200873void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200874{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200875 int *count = this_cpu_ptr(pmu->pmu_disable_count);
876 if (!--(*count))
877 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200878}
879
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200880static DEFINE_PER_CPU(struct list_head, rotation_list);
881
882/*
883 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
884 * because they're strictly cpu affine and rotate_start is called with IRQs
885 * disabled, while rotate_context is called from IRQ context.
886 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200887static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200888{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200889 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200890 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200891
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200892 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200893
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +0200894 if (list_empty(&cpuctx->rotation_list))
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200895 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200896}
897
898static void get_ctx(struct perf_event_context *ctx)
899{
900 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
901}
902
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200903static void put_ctx(struct perf_event_context *ctx)
904{
905 if (atomic_dec_and_test(&ctx->refcount)) {
906 if (ctx->parent_ctx)
907 put_ctx(ctx->parent_ctx);
908 if (ctx->task)
909 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800910 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200911 }
912}
913
914static void unclone_ctx(struct perf_event_context *ctx)
915{
916 if (ctx->parent_ctx) {
917 put_ctx(ctx->parent_ctx);
918 ctx->parent_ctx = NULL;
919 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +0200920 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200921}
922
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200923static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
924{
925 /*
926 * only top level events have the pid namespace they were created in
927 */
928 if (event->parent)
929 event = event->parent;
930
931 return task_tgid_nr_ns(p, event->ns);
932}
933
934static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
935{
936 /*
937 * only top level events have the pid namespace they were created in
938 */
939 if (event->parent)
940 event = event->parent;
941
942 return task_pid_nr_ns(p, event->ns);
943}
944
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200945/*
946 * If we inherit events we want to return the parent event id
947 * to userspace.
948 */
949static u64 primary_event_id(struct perf_event *event)
950{
951 u64 id = event->id;
952
953 if (event->parent)
954 id = event->parent->id;
955
956 return id;
957}
958
959/*
960 * Get the perf_event_context for a task and lock it.
961 * This has to cope with with the fact that until it is locked,
962 * the context could get moved to another task.
963 */
964static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200965perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200966{
967 struct perf_event_context *ctx;
968
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200969retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200970 /*
971 * One of the few rules of preemptible RCU is that one cannot do
972 * rcu_read_unlock() while holding a scheduler (or nested) lock when
973 * part of the read side critical section was preemptible -- see
974 * rcu_read_unlock_special().
975 *
976 * Since ctx->lock nests under rq->lock we must ensure the entire read
977 * side critical section is non-preemptible.
978 */
979 preempt_disable();
980 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200981 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200982 if (ctx) {
983 /*
984 * If this context is a clone of another, it might
985 * get swapped for another underneath us by
986 * perf_event_task_sched_out, though the
987 * rcu_read_lock() protects us from any context
988 * getting freed. Lock the context and check if it
989 * got swapped before we could get the lock, and retry
990 * if so. If we locked the right context, then it
991 * can't get swapped on us any more.
992 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100993 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200994 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100995 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200996 rcu_read_unlock();
997 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200998 goto retry;
999 }
1000
1001 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001002 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001003 ctx = NULL;
1004 }
1005 }
1006 rcu_read_unlock();
Peter Zijlstra058ebd02013-07-12 11:08:33 +02001007 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001008 return ctx;
1009}
1010
1011/*
1012 * Get the context for a task and increment its pin_count so it
1013 * can't get swapped to another task. This also increments its
1014 * reference count so that the context can't get freed.
1015 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001016static struct perf_event_context *
1017perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001018{
1019 struct perf_event_context *ctx;
1020 unsigned long flags;
1021
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001022 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001023 if (ctx) {
1024 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001025 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001026 }
1027 return ctx;
1028}
1029
1030static void perf_unpin_context(struct perf_event_context *ctx)
1031{
1032 unsigned long flags;
1033
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001034 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001035 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001036 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001037}
1038
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001039/*
1040 * Update the record of the current time in a context.
1041 */
1042static void update_context_time(struct perf_event_context *ctx)
1043{
1044 u64 now = perf_clock();
1045
1046 ctx->time += now - ctx->timestamp;
1047 ctx->timestamp = now;
1048}
1049
Stephane Eranian41587552011-01-03 18:20:01 +02001050static u64 perf_event_time(struct perf_event *event)
1051{
1052 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001053
1054 if (is_cgroup_event(event))
1055 return perf_cgroup_event_time(event);
1056
Stephane Eranian41587552011-01-03 18:20:01 +02001057 return ctx ? ctx->time : 0;
1058}
1059
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001060/*
1061 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001062 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001063 */
1064static void update_event_times(struct perf_event *event)
1065{
1066 struct perf_event_context *ctx = event->ctx;
1067 u64 run_end;
1068
1069 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1070 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1071 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001072 /*
1073 * in cgroup mode, time_enabled represents
1074 * the time the event was enabled AND active
1075 * tasks were in the monitored cgroup. This is
1076 * independent of the activity of the context as
1077 * there may be a mix of cgroup and non-cgroup events.
1078 *
1079 * That is why we treat cgroup events differently
1080 * here.
1081 */
1082 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001083 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001084 else if (ctx->is_active)
1085 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001086 else
1087 run_end = event->tstamp_stopped;
1088
1089 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001090
1091 if (event->state == PERF_EVENT_STATE_INACTIVE)
1092 run_end = event->tstamp_stopped;
1093 else
Stephane Eranian41587552011-01-03 18:20:01 +02001094 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001095
1096 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001097
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001098}
1099
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001100/*
1101 * Update total_time_enabled and total_time_running for all events in a group.
1102 */
1103static void update_group_times(struct perf_event *leader)
1104{
1105 struct perf_event *event;
1106
1107 update_event_times(leader);
1108 list_for_each_entry(event, &leader->sibling_list, group_entry)
1109 update_event_times(event);
1110}
1111
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001112static struct list_head *
1113ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1114{
1115 if (event->attr.pinned)
1116 return &ctx->pinned_groups;
1117 else
1118 return &ctx->flexible_groups;
1119}
1120
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001121/*
1122 * Add a event from the lists for its context.
1123 * Must be called with ctx->mutex and ctx->lock held.
1124 */
1125static void
1126list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1127{
Peter Zijlstra8a495422010-05-27 15:47:49 +02001128 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1129 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001130
1131 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001132 * If we're a stand alone event or group leader, we go to the context
1133 * list, group events are kept attached to the group so that
1134 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001135 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001136 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001137 struct list_head *list;
1138
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001139 if (is_software_event(event))
1140 event->group_flags |= PERF_GROUP_SOFTWARE;
1141
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001142 list = ctx_group_list(event, ctx);
1143 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001144 }
1145
Peter Zijlstra08309372011-03-03 11:31:20 +01001146 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001147 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001148
Stephane Eraniand010b332012-02-09 23:21:00 +01001149 if (has_branch_stack(event))
1150 ctx->nr_branch_stack++;
1151
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001152 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001153 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001154 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001155 ctx->nr_events++;
1156 if (event->attr.inherit_stat)
1157 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001158
1159 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001160}
1161
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001162/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001163 * Initialize event state based on the perf_event_attr::disabled.
1164 */
1165static inline void perf_event__state_init(struct perf_event *event)
1166{
1167 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1168 PERF_EVENT_STATE_INACTIVE;
1169}
1170
1171/*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001172 * Called at perf_event creation and when events are attached/detached from a
1173 * group.
1174 */
1175static void perf_event__read_size(struct perf_event *event)
1176{
1177 int entry = sizeof(u64); /* value */
1178 int size = 0;
1179 int nr = 1;
1180
1181 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1182 size += sizeof(u64);
1183
1184 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1185 size += sizeof(u64);
1186
1187 if (event->attr.read_format & PERF_FORMAT_ID)
1188 entry += sizeof(u64);
1189
1190 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1191 nr += event->group_leader->nr_siblings;
1192 size += sizeof(u64);
1193 }
1194
1195 size += entry * nr;
1196 event->read_size = size;
1197}
1198
1199static void perf_event__header_size(struct perf_event *event)
1200{
1201 struct perf_sample_data *data;
1202 u64 sample_type = event->attr.sample_type;
1203 u16 size = 0;
1204
1205 perf_event__read_size(event);
1206
1207 if (sample_type & PERF_SAMPLE_IP)
1208 size += sizeof(data->ip);
1209
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001210 if (sample_type & PERF_SAMPLE_ADDR)
1211 size += sizeof(data->addr);
1212
1213 if (sample_type & PERF_SAMPLE_PERIOD)
1214 size += sizeof(data->period);
1215
Andi Kleenc3feedf2013-01-24 16:10:28 +01001216 if (sample_type & PERF_SAMPLE_WEIGHT)
1217 size += sizeof(data->weight);
1218
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001219 if (sample_type & PERF_SAMPLE_READ)
1220 size += event->read_size;
1221
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001222 if (sample_type & PERF_SAMPLE_DATA_SRC)
1223 size += sizeof(data->data_src.val);
1224
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001225 if (sample_type & PERF_SAMPLE_TRANSACTION)
1226 size += sizeof(data->txn);
1227
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001228 event->header_size = size;
1229}
1230
1231static void perf_event__id_header_size(struct perf_event *event)
1232{
1233 struct perf_sample_data *data;
1234 u64 sample_type = event->attr.sample_type;
1235 u16 size = 0;
1236
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001237 if (sample_type & PERF_SAMPLE_TID)
1238 size += sizeof(data->tid_entry);
1239
1240 if (sample_type & PERF_SAMPLE_TIME)
1241 size += sizeof(data->time);
1242
Adrian Hunterff3d5272013-08-27 11:23:07 +03001243 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1244 size += sizeof(data->id);
1245
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001246 if (sample_type & PERF_SAMPLE_ID)
1247 size += sizeof(data->id);
1248
1249 if (sample_type & PERF_SAMPLE_STREAM_ID)
1250 size += sizeof(data->stream_id);
1251
1252 if (sample_type & PERF_SAMPLE_CPU)
1253 size += sizeof(data->cpu_entry);
1254
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001255 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001256}
1257
Peter Zijlstra8a495422010-05-27 15:47:49 +02001258static void perf_group_attach(struct perf_event *event)
1259{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001260 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001261
Peter Zijlstra74c33372010-10-15 11:40:29 +02001262 /*
1263 * We can have double attach due to group movement in perf_event_open.
1264 */
1265 if (event->attach_state & PERF_ATTACH_GROUP)
1266 return;
1267
Peter Zijlstra8a495422010-05-27 15:47:49 +02001268 event->attach_state |= PERF_ATTACH_GROUP;
1269
1270 if (group_leader == event)
1271 return;
1272
1273 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1274 !is_software_event(event))
1275 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1276
1277 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1278 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001279
1280 perf_event__header_size(group_leader);
1281
1282 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1283 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001284}
1285
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001286/*
1287 * Remove a event from the lists for its context.
1288 * Must be called with ctx->mutex and ctx->lock held.
1289 */
1290static void
1291list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1292{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001293 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001294 /*
1295 * We can have double detach due to exit/hot-unplug + close.
1296 */
1297 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001298 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001299
1300 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1301
Stephane Eranian68cacd22011-03-23 16:03:06 +01001302 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001303 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001304 cpuctx = __get_cpu_context(ctx);
1305 /*
1306 * if there are no more cgroup events
1307 * then cler cgrp to avoid stale pointer
1308 * in update_cgrp_time_from_cpuctx()
1309 */
1310 if (!ctx->nr_cgroups)
1311 cpuctx->cgrp = NULL;
1312 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001313
Stephane Eraniand010b332012-02-09 23:21:00 +01001314 if (has_branch_stack(event))
1315 ctx->nr_branch_stack--;
1316
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001317 ctx->nr_events--;
1318 if (event->attr.inherit_stat)
1319 ctx->nr_stat--;
1320
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001321 list_del_rcu(&event->event_entry);
1322
Peter Zijlstra8a495422010-05-27 15:47:49 +02001323 if (event->group_leader == event)
1324 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001325
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001326 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001327
1328 /*
1329 * If event was in error state, then keep it
1330 * that way, otherwise bogus counts will be
1331 * returned on read(). The only way to get out
1332 * of error state is by explicit re-enabling
1333 * of the event
1334 */
1335 if (event->state > PERF_EVENT_STATE_OFF)
1336 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001337
1338 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001339}
1340
Peter Zijlstra8a495422010-05-27 15:47:49 +02001341static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001342{
1343 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001344 struct list_head *list = NULL;
1345
1346 /*
1347 * We can have double detach due to exit/hot-unplug + close.
1348 */
1349 if (!(event->attach_state & PERF_ATTACH_GROUP))
1350 return;
1351
1352 event->attach_state &= ~PERF_ATTACH_GROUP;
1353
1354 /*
1355 * If this is a sibling, remove it from its group.
1356 */
1357 if (event->group_leader != event) {
1358 list_del_init(&event->group_entry);
1359 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001360 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001361 }
1362
1363 if (!list_empty(&event->group_entry))
1364 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001365
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001366 /*
1367 * If this was a group event with sibling events then
1368 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001369 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001370 */
1371 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001372 if (list)
1373 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001374 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001375
1376 /* Inherit group flags from the previous leader */
1377 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001378 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001379
1380out:
1381 perf_event__header_size(event->group_leader);
1382
1383 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1384 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001385}
1386
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001387/*
1388 * User event without the task.
1389 */
1390static bool is_orphaned_event(struct perf_event *event)
1391{
1392 return event && !is_kernel_event(event) && !event->owner;
1393}
1394
1395/*
1396 * Event has a parent but parent's task finished and it's
1397 * alive only because of children holding refference.
1398 */
1399static bool is_orphaned_child(struct perf_event *event)
1400{
1401 return is_orphaned_event(event->parent);
1402}
1403
1404static void orphans_remove_work(struct work_struct *work);
1405
1406static void schedule_orphans_remove(struct perf_event_context *ctx)
1407{
1408 if (!ctx->task || ctx->orphans_remove_sched || !perf_wq)
1409 return;
1410
1411 if (queue_delayed_work(perf_wq, &ctx->orphans_remove, 1)) {
1412 get_ctx(ctx);
1413 ctx->orphans_remove_sched = true;
1414 }
1415}
1416
1417static int __init perf_workqueue_init(void)
1418{
1419 perf_wq = create_singlethread_workqueue("perf");
1420 WARN(!perf_wq, "failed to create perf workqueue\n");
1421 return perf_wq ? 0 : -1;
1422}
1423
1424core_initcall(perf_workqueue_init);
1425
Stephane Eranianfa66f072010-08-26 16:40:01 +02001426static inline int
1427event_filter_match(struct perf_event *event)
1428{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001429 return (event->cpu == -1 || event->cpu == smp_processor_id())
1430 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001431}
1432
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001433static void
1434event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001435 struct perf_cpu_context *cpuctx,
1436 struct perf_event_context *ctx)
1437{
Stephane Eranian41587552011-01-03 18:20:01 +02001438 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001439 u64 delta;
1440 /*
1441 * An event which could not be activated because of
1442 * filter mismatch still needs to have its timings
1443 * maintained, otherwise bogus information is return
1444 * via read() for time_enabled, time_running:
1445 */
1446 if (event->state == PERF_EVENT_STATE_INACTIVE
1447 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001448 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001449 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001450 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001451 }
1452
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001453 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001454 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001455
Alexander Shishkin44377272013-12-16 14:17:36 +02001456 perf_pmu_disable(event->pmu);
1457
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001458 event->state = PERF_EVENT_STATE_INACTIVE;
1459 if (event->pending_disable) {
1460 event->pending_disable = 0;
1461 event->state = PERF_EVENT_STATE_OFF;
1462 }
Stephane Eranian41587552011-01-03 18:20:01 +02001463 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001464 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001465 event->oncpu = -1;
1466
1467 if (!is_software_event(event))
1468 cpuctx->active_oncpu--;
1469 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001470 if (event->attr.freq && event->attr.sample_freq)
1471 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001472 if (event->attr.exclusive || !cpuctx->active_oncpu)
1473 cpuctx->exclusive = 0;
Alexander Shishkin44377272013-12-16 14:17:36 +02001474
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001475 if (is_orphaned_child(event))
1476 schedule_orphans_remove(ctx);
1477
Alexander Shishkin44377272013-12-16 14:17:36 +02001478 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001479}
1480
1481static void
1482group_sched_out(struct perf_event *group_event,
1483 struct perf_cpu_context *cpuctx,
1484 struct perf_event_context *ctx)
1485{
1486 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001487 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001488
1489 event_sched_out(group_event, cpuctx, ctx);
1490
1491 /*
1492 * Schedule out siblings (if any):
1493 */
1494 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1495 event_sched_out(event, cpuctx, ctx);
1496
Stephane Eranianfa66f072010-08-26 16:40:01 +02001497 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001498 cpuctx->exclusive = 0;
1499}
1500
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001501struct remove_event {
1502 struct perf_event *event;
1503 bool detach_group;
1504};
1505
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001506/*
1507 * Cross CPU call to remove a performance event
1508 *
1509 * We disable the event on the hardware level first. After that we
1510 * remove it from the context list.
1511 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001512static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001513{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001514 struct remove_event *re = info;
1515 struct perf_event *event = re->event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001516 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001517 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001518
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001519 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001520 event_sched_out(event, cpuctx, ctx);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001521 if (re->detach_group)
1522 perf_group_detach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001523 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001524 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1525 ctx->is_active = 0;
1526 cpuctx->task_ctx = NULL;
1527 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001528 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001529
1530 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001531}
1532
1533
1534/*
1535 * Remove the event from a task's (or a CPU's) list of events.
1536 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001537 * CPU events are removed with a smp call. For task events we only
1538 * call when the task is on a CPU.
1539 *
1540 * If event->ctx is a cloned context, callers must make sure that
1541 * every task struct that event->ctx->task could possibly point to
1542 * remains valid. This is OK when called from perf_release since
1543 * that only calls us on the top-level context, which can't be a clone.
1544 * When called from perf_event_exit_task, it's OK because the
1545 * context has been detached from its task.
1546 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001547static void perf_remove_from_context(struct perf_event *event, bool detach_group)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001548{
1549 struct perf_event_context *ctx = event->ctx;
1550 struct task_struct *task = ctx->task;
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001551 struct remove_event re = {
1552 .event = event,
1553 .detach_group = detach_group,
1554 };
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001555
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001556 lockdep_assert_held(&ctx->mutex);
1557
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001558 if (!task) {
1559 /*
1560 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001561 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001562 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001563 cpu_function_call(event->cpu, __perf_remove_from_context, &re);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001564 return;
1565 }
1566
1567retry:
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001568 if (!task_function_call(task, __perf_remove_from_context, &re))
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001569 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001570
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001571 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001572 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001573 * If we failed to find a running task, but find the context active now
1574 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001575 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001576 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001577 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001578 goto retry;
1579 }
1580
1581 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001582 * Since the task isn't running, its safe to remove the event, us
1583 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001584 */
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02001585 if (detach_group)
1586 perf_group_detach(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001587 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001588 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001589}
1590
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001591/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001592 * Cross CPU call to disable a performance event
1593 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301594int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001595{
1596 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001597 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001598 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001599
1600 /*
1601 * If this is a per-task event, need to check whether this
1602 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001603 *
1604 * Can trigger due to concurrent perf_event_context_sched_out()
1605 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001606 */
1607 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001608 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001609
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001610 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001611
1612 /*
1613 * If the event is on, turn it off.
1614 * If it is in error state, leave it in error state.
1615 */
1616 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1617 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001618 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001619 update_group_times(event);
1620 if (event == event->group_leader)
1621 group_sched_out(event, cpuctx, ctx);
1622 else
1623 event_sched_out(event, cpuctx, ctx);
1624 event->state = PERF_EVENT_STATE_OFF;
1625 }
1626
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001627 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001628
1629 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001630}
1631
1632/*
1633 * Disable a event.
1634 *
1635 * If event->ctx is a cloned context, callers must make sure that
1636 * every task struct that event->ctx->task could possibly point to
1637 * remains valid. This condition is satisifed when called through
1638 * perf_event_for_each_child or perf_event_for_each because they
1639 * hold the top-level event's child_mutex, so any descendant that
1640 * goes to exit will block in sync_child_event.
1641 * When called from perf_pending_event it's OK because event->ctx
1642 * is the current context on this CPU and preemption is disabled,
1643 * hence we can't get into perf_event_task_sched_out for this context.
1644 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001645void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001646{
1647 struct perf_event_context *ctx = event->ctx;
1648 struct task_struct *task = ctx->task;
1649
1650 if (!task) {
1651 /*
1652 * Disable the event on the cpu that it's on
1653 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001654 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001655 return;
1656 }
1657
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001658retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001659 if (!task_function_call(task, __perf_event_disable, event))
1660 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001661
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001662 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001663 /*
1664 * If the event is still active, we need to retry the cross-call.
1665 */
1666 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001667 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001668 /*
1669 * Reload the task pointer, it might have been changed by
1670 * a concurrent perf_event_context_sched_out().
1671 */
1672 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001673 goto retry;
1674 }
1675
1676 /*
1677 * Since we have the lock this context can't be scheduled
1678 * in, so we can change the state safely.
1679 */
1680 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1681 update_group_times(event);
1682 event->state = PERF_EVENT_STATE_OFF;
1683 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001684 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001685}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001686EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001687
Stephane Eraniane5d13672011-02-14 11:20:01 +02001688static void perf_set_shadow_time(struct perf_event *event,
1689 struct perf_event_context *ctx,
1690 u64 tstamp)
1691{
1692 /*
1693 * use the correct time source for the time snapshot
1694 *
1695 * We could get by without this by leveraging the
1696 * fact that to get to this function, the caller
1697 * has most likely already called update_context_time()
1698 * and update_cgrp_time_xx() and thus both timestamp
1699 * are identical (or very close). Given that tstamp is,
1700 * already adjusted for cgroup, we could say that:
1701 * tstamp - ctx->timestamp
1702 * is equivalent to
1703 * tstamp - cgrp->timestamp.
1704 *
1705 * Then, in perf_output_read(), the calculation would
1706 * work with no changes because:
1707 * - event is guaranteed scheduled in
1708 * - no scheduled out in between
1709 * - thus the timestamp would be the same
1710 *
1711 * But this is a bit hairy.
1712 *
1713 * So instead, we have an explicit cgroup call to remain
1714 * within the time time source all along. We believe it
1715 * is cleaner and simpler to understand.
1716 */
1717 if (is_cgroup_event(event))
1718 perf_cgroup_set_shadow_time(event, tstamp);
1719 else
1720 event->shadow_ctx_time = tstamp - ctx->timestamp;
1721}
1722
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001723#define MAX_INTERRUPTS (~0ULL)
1724
1725static void perf_log_throttle(struct perf_event *event, int enable);
1726
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001727static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001728event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001729 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001730 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001731{
Stephane Eranian41587552011-01-03 18:20:01 +02001732 u64 tstamp = perf_event_time(event);
Alexander Shishkin44377272013-12-16 14:17:36 +02001733 int ret = 0;
Stephane Eranian41587552011-01-03 18:20:01 +02001734
Peter Zijlstra63342412014-05-05 11:49:16 +02001735 lockdep_assert_held(&ctx->lock);
1736
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001737 if (event->state <= PERF_EVENT_STATE_OFF)
1738 return 0;
1739
1740 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001741 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001742
1743 /*
1744 * Unthrottle events, since we scheduled we might have missed several
1745 * ticks already, also for a heavily scheduling task there is little
1746 * guarantee it'll get a tick in a timely manner.
1747 */
1748 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1749 perf_log_throttle(event, 1);
1750 event->hw.interrupts = 0;
1751 }
1752
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001753 /*
1754 * The new state must be visible before we turn it on in the hardware:
1755 */
1756 smp_wmb();
1757
Alexander Shishkin44377272013-12-16 14:17:36 +02001758 perf_pmu_disable(event->pmu);
1759
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001760 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001761 event->state = PERF_EVENT_STATE_INACTIVE;
1762 event->oncpu = -1;
Alexander Shishkin44377272013-12-16 14:17:36 +02001763 ret = -EAGAIN;
1764 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001765 }
1766
Stephane Eranian41587552011-01-03 18:20:01 +02001767 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001768
Stephane Eraniane5d13672011-02-14 11:20:01 +02001769 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001770
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001771 if (!is_software_event(event))
1772 cpuctx->active_oncpu++;
1773 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001774 if (event->attr.freq && event->attr.sample_freq)
1775 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001776
1777 if (event->attr.exclusive)
1778 cpuctx->exclusive = 1;
1779
Jiri Olsafadfe7b2014-08-01 14:33:02 +02001780 if (is_orphaned_child(event))
1781 schedule_orphans_remove(ctx);
1782
Alexander Shishkin44377272013-12-16 14:17:36 +02001783out:
1784 perf_pmu_enable(event->pmu);
1785
1786 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001787}
1788
1789static int
1790group_sched_in(struct perf_event *group_event,
1791 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001792 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001793{
Lin Ming6bde9b62010-04-23 13:56:00 +08001794 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra4a234592014-02-24 12:43:31 +01001795 struct pmu *pmu = ctx->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001796 u64 now = ctx->time;
1797 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001798
1799 if (group_event->state == PERF_EVENT_STATE_OFF)
1800 return 0;
1801
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001802 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001803
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001804 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001805 pmu->cancel_txn(pmu);
Stephane Eranian9e630202013-04-03 14:21:33 +02001806 perf_cpu_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001807 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001808 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001809
1810 /*
1811 * Schedule in siblings as one group (if any):
1812 */
1813 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001814 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001815 partial_group = event;
1816 goto group_error;
1817 }
1818 }
1819
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001820 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001821 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001822
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001823group_error:
1824 /*
1825 * Groups can be scheduled in as one unit only, so undo any
1826 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001827 * The events up to the failed event are scheduled out normally,
1828 * tstamp_stopped will be updated.
1829 *
1830 * The failed events and the remaining siblings need to have
1831 * their timings updated as if they had gone thru event_sched_in()
1832 * and event_sched_out(). This is required to get consistent timings
1833 * across the group. This also takes care of the case where the group
1834 * could never be scheduled by ensuring tstamp_stopped is set to mark
1835 * the time the event was actually stopped, such that time delta
1836 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001837 */
1838 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1839 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001840 simulate = true;
1841
1842 if (simulate) {
1843 event->tstamp_running += now - event->tstamp_stopped;
1844 event->tstamp_stopped = now;
1845 } else {
1846 event_sched_out(event, cpuctx, ctx);
1847 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001848 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001849 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001850
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001851 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001852
Stephane Eranian9e630202013-04-03 14:21:33 +02001853 perf_cpu_hrtimer_restart(cpuctx);
1854
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001855 return -EAGAIN;
1856}
1857
1858/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001859 * Work out whether we can put this event group on the CPU now.
1860 */
1861static int group_can_go_on(struct perf_event *event,
1862 struct perf_cpu_context *cpuctx,
1863 int can_add_hw)
1864{
1865 /*
1866 * Groups consisting entirely of software events can always go on.
1867 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001868 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001869 return 1;
1870 /*
1871 * If an exclusive group is already on, no other hardware
1872 * events can go on.
1873 */
1874 if (cpuctx->exclusive)
1875 return 0;
1876 /*
1877 * If this group is exclusive and there are already
1878 * events on the CPU, it can't go on.
1879 */
1880 if (event->attr.exclusive && cpuctx->active_oncpu)
1881 return 0;
1882 /*
1883 * Otherwise, try to add it if all previous groups were able
1884 * to go on.
1885 */
1886 return can_add_hw;
1887}
1888
1889static void add_event_to_ctx(struct perf_event *event,
1890 struct perf_event_context *ctx)
1891{
Stephane Eranian41587552011-01-03 18:20:01 +02001892 u64 tstamp = perf_event_time(event);
1893
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001894 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001895 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001896 event->tstamp_enabled = tstamp;
1897 event->tstamp_running = tstamp;
1898 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001899}
1900
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001901static void task_ctx_sched_out(struct perf_event_context *ctx);
1902static void
1903ctx_sched_in(struct perf_event_context *ctx,
1904 struct perf_cpu_context *cpuctx,
1905 enum event_type_t event_type,
1906 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001907
Peter Zijlstradce58552011-04-09 21:17:46 +02001908static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1909 struct perf_event_context *ctx,
1910 struct task_struct *task)
1911{
1912 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1913 if (ctx)
1914 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1915 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1916 if (ctx)
1917 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1918}
1919
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001920/*
1921 * Cross CPU call to install and enable a performance event
1922 *
1923 * Must be called with ctx->mutex held
1924 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001925static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001926{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001927 struct perf_event *event = info;
1928 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001929 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001930 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1931 struct task_struct *task = current;
1932
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001933 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001934 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001935
1936 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001937 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001938 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001939 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001940 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001941
1942 /*
1943 * If the context we're installing events in is not the
1944 * active task_ctx, flip them.
1945 */
1946 if (ctx->task && task_ctx != ctx) {
1947 if (task_ctx)
1948 raw_spin_unlock(&task_ctx->lock);
1949 raw_spin_lock(&ctx->lock);
1950 task_ctx = ctx;
1951 }
1952
1953 if (task_ctx) {
1954 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001955 task = task_ctx->task;
1956 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001957
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001958 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001959
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001960 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001961 /*
1962 * update cgrp time only if current cgrp
1963 * matches event->cgrp. Must be done before
1964 * calling add_event_to_ctx()
1965 */
1966 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001967
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001968 add_event_to_ctx(event, ctx);
1969
1970 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001971 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001972 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001973 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001974
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001975 perf_pmu_enable(cpuctx->ctx.pmu);
1976 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001977
1978 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001979}
1980
1981/*
1982 * Attach a performance event to a context
1983 *
1984 * First we add the event to the list with the hardware enable bit
1985 * in event->hw_config cleared.
1986 *
1987 * If the event is attached to a task which is on a CPU we use a smp
1988 * call to enable it in the task context. The task might have been
1989 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001990 */
1991static void
1992perf_install_in_context(struct perf_event_context *ctx,
1993 struct perf_event *event,
1994 int cpu)
1995{
1996 struct task_struct *task = ctx->task;
1997
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001998 lockdep_assert_held(&ctx->mutex);
1999
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002000 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08002001 if (event->cpu != -1)
2002 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02002003
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002004 if (!task) {
2005 /*
2006 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002007 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002008 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002009 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002010 return;
2011 }
2012
2013retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002014 if (!task_function_call(task, __perf_install_in_context, event))
2015 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002016
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002017 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002018 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002019 * If we failed to find a running task, but find the context active now
2020 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002021 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002022 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002023 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002024 goto retry;
2025 }
2026
2027 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002028 * Since the task isn't running, its safe to add the event, us holding
2029 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002030 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002031 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002032 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002033}
2034
2035/*
2036 * Put a event into inactive state and update time fields.
2037 * Enabling the leader of a group effectively enables all
2038 * the group members that aren't explicitly disabled, so we
2039 * have to update their ->tstamp_enabled also.
2040 * Note: this works for group members as well as group leaders
2041 * since the non-leader members' sibling_lists will be empty.
2042 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002043static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002044{
2045 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02002046 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002047
2048 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02002049 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002050 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02002051 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2052 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002053 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002054}
2055
2056/*
2057 * Cross CPU call to enable a performance event
2058 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002059static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002060{
2061 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002062 struct perf_event_context *ctx = event->ctx;
2063 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002064 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002065 int err;
2066
Jiri Olsa06f41792013-07-09 17:44:11 +02002067 /*
2068 * There's a time window between 'ctx->is_active' check
2069 * in perf_event_enable function and this place having:
2070 * - IRQs on
2071 * - ctx->lock unlocked
2072 *
2073 * where the task could be killed and 'ctx' deactivated
2074 * by perf_event_exit_task.
2075 */
2076 if (!ctx->is_active)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002077 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002078
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002079 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002080 update_context_time(ctx);
2081
2082 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2083 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002084
2085 /*
2086 * set current task's cgroup time reference point
2087 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002088 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002089
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002090 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002091
Stephane Eraniane5d13672011-02-14 11:20:01 +02002092 if (!event_filter_match(event)) {
2093 if (is_cgroup_event(event))
2094 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002095 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002096 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002097
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002098 /*
2099 * If the event is in a group and isn't the group leader,
2100 * then don't put it on unless the group is on.
2101 */
2102 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2103 goto unlock;
2104
2105 if (!group_can_go_on(event, cpuctx, 1)) {
2106 err = -EEXIST;
2107 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002108 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002109 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002110 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01002111 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002112 }
2113
2114 if (err) {
2115 /*
2116 * If this event can't go on and it's part of a
2117 * group, then the whole group has to come off.
2118 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002119 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002120 group_sched_out(leader, cpuctx, ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002121 perf_cpu_hrtimer_restart(cpuctx);
2122 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002123 if (leader->attr.pinned) {
2124 update_group_times(leader);
2125 leader->state = PERF_EVENT_STATE_ERROR;
2126 }
2127 }
2128
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002129unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002130 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002131
2132 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002133}
2134
2135/*
2136 * Enable a event.
2137 *
2138 * If event->ctx is a cloned context, callers must make sure that
2139 * every task struct that event->ctx->task could possibly point to
2140 * remains valid. This condition is satisfied when called through
2141 * perf_event_for_each_child or perf_event_for_each as described
2142 * for perf_event_disable.
2143 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01002144void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002145{
2146 struct perf_event_context *ctx = event->ctx;
2147 struct task_struct *task = ctx->task;
2148
2149 if (!task) {
2150 /*
2151 * Enable the event on the cpu that it's on
2152 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002153 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002154 return;
2155 }
2156
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002157 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002158 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2159 goto out;
2160
2161 /*
2162 * If the event is in error state, clear that first.
2163 * That way, if we see the event in error state below, we
2164 * know that it has gone back into error state, as distinct
2165 * from the task having been scheduled away before the
2166 * cross-call arrived.
2167 */
2168 if (event->state == PERF_EVENT_STATE_ERROR)
2169 event->state = PERF_EVENT_STATE_OFF;
2170
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002171retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002172 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002173 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002174 goto out;
2175 }
2176
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002177 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002178
2179 if (!task_function_call(task, __perf_event_enable, event))
2180 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002181
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002182 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002183
2184 /*
2185 * If the context is active and the event is still off,
2186 * we need to retry the cross-call.
2187 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002188 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2189 /*
2190 * task could have been flipped by a concurrent
2191 * perf_event_context_sched_out()
2192 */
2193 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002194 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002195 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002196
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002197out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002198 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002199}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002200EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002201
Avi Kivity26ca5c12011-06-29 18:42:37 +03002202int perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002203{
2204 /*
2205 * not supported on inherited events
2206 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002207 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002208 return -EINVAL;
2209
2210 atomic_add(refresh, &event->event_limit);
2211 perf_event_enable(event);
2212
2213 return 0;
2214}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002215EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002216
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002217static void ctx_sched_out(struct perf_event_context *ctx,
2218 struct perf_cpu_context *cpuctx,
2219 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002220{
2221 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002222 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002223
Peter Zijlstradb24d332011-04-09 21:17:45 +02002224 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002225 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002226 return;
2227
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002228 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002229 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002230 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002231 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002232
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002233 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002234 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002235 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2236 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002237 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002238
Peter Zijlstradb24d332011-04-09 21:17:45 +02002239 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002240 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002241 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002242 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002243 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002244}
2245
2246/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002247 * Test whether two contexts are equivalent, i.e. whether they have both been
2248 * cloned from the same version of the same context.
2249 *
2250 * Equivalence is measured using a generation number in the context that is
2251 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2252 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002253 */
2254static int context_equiv(struct perf_event_context *ctx1,
2255 struct perf_event_context *ctx2)
2256{
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002257 /* Pinning disables the swap optimization */
2258 if (ctx1->pin_count || ctx2->pin_count)
2259 return 0;
2260
2261 /* If ctx1 is the parent of ctx2 */
2262 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2263 return 1;
2264
2265 /* If ctx2 is the parent of ctx1 */
2266 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2267 return 1;
2268
2269 /*
2270 * If ctx1 and ctx2 have the same parent; we flatten the parent
2271 * hierarchy, see perf_event_init_context().
2272 */
2273 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2274 ctx1->parent_gen == ctx2->parent_gen)
2275 return 1;
2276
2277 /* Unmatched */
2278 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002279}
2280
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002281static void __perf_event_sync_stat(struct perf_event *event,
2282 struct perf_event *next_event)
2283{
2284 u64 value;
2285
2286 if (!event->attr.inherit_stat)
2287 return;
2288
2289 /*
2290 * Update the event value, we cannot use perf_event_read()
2291 * because we're in the middle of a context switch and have IRQs
2292 * disabled, which upsets smp_call_function_single(), however
2293 * we know the event must be on the current CPU, therefore we
2294 * don't need to use it.
2295 */
2296 switch (event->state) {
2297 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002298 event->pmu->read(event);
2299 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002300
2301 case PERF_EVENT_STATE_INACTIVE:
2302 update_event_times(event);
2303 break;
2304
2305 default:
2306 break;
2307 }
2308
2309 /*
2310 * In order to keep per-task stats reliable we need to flip the event
2311 * values when we flip the contexts.
2312 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002313 value = local64_read(&next_event->count);
2314 value = local64_xchg(&event->count, value);
2315 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002316
2317 swap(event->total_time_enabled, next_event->total_time_enabled);
2318 swap(event->total_time_running, next_event->total_time_running);
2319
2320 /*
2321 * Since we swizzled the values, update the user visible data too.
2322 */
2323 perf_event_update_userpage(event);
2324 perf_event_update_userpage(next_event);
2325}
2326
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002327static void perf_event_sync_stat(struct perf_event_context *ctx,
2328 struct perf_event_context *next_ctx)
2329{
2330 struct perf_event *event, *next_event;
2331
2332 if (!ctx->nr_stat)
2333 return;
2334
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002335 update_context_time(ctx);
2336
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002337 event = list_first_entry(&ctx->event_list,
2338 struct perf_event, event_entry);
2339
2340 next_event = list_first_entry(&next_ctx->event_list,
2341 struct perf_event, event_entry);
2342
2343 while (&event->event_entry != &ctx->event_list &&
2344 &next_event->event_entry != &next_ctx->event_list) {
2345
2346 __perf_event_sync_stat(event, next_event);
2347
2348 event = list_next_entry(event, event_entry);
2349 next_event = list_next_entry(next_event, event_entry);
2350 }
2351}
2352
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002353static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2354 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002355{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002356 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002357 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002358 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002359 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002360 int do_switch = 1;
2361
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002362 if (likely(!ctx))
2363 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002364
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002365 cpuctx = __get_cpu_context(ctx);
2366 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002367 return;
2368
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002369 rcu_read_lock();
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002370 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002371 if (!next_ctx)
2372 goto unlock;
2373
2374 parent = rcu_dereference(ctx->parent_ctx);
2375 next_parent = rcu_dereference(next_ctx->parent_ctx);
2376
2377 /* If neither context have a parent context; they cannot be clones. */
Jiri Olsa802c8a62014-09-12 13:18:28 +02002378 if (!parent && !next_parent)
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002379 goto unlock;
2380
2381 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002382 /*
2383 * Looks like the two contexts are clones, so we might be
2384 * able to optimize the context switch. We lock both
2385 * contexts and check that they are clones under the
2386 * lock (including re-checking that neither has been
2387 * uncloned in the meantime). It doesn't matter which
2388 * order we take the locks because no other cpu could
2389 * be trying to lock both of these tasks.
2390 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002391 raw_spin_lock(&ctx->lock);
2392 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002393 if (context_equiv(ctx, next_ctx)) {
2394 /*
2395 * XXX do we need a memory barrier of sorts
2396 * wrt to rcu_dereference() of perf_event_ctxp
2397 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002398 task->perf_event_ctxp[ctxn] = next_ctx;
2399 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002400 ctx->task = next;
2401 next_ctx->task = task;
2402 do_switch = 0;
2403
2404 perf_event_sync_stat(ctx, next_ctx);
2405 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002406 raw_spin_unlock(&next_ctx->lock);
2407 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002408 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002409unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002410 rcu_read_unlock();
2411
2412 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002413 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002414 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002415 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002416 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002417 }
2418}
2419
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002420#define for_each_task_context_nr(ctxn) \
2421 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2422
2423/*
2424 * Called from scheduler to remove the events of the current task,
2425 * with interrupts disabled.
2426 *
2427 * We stop each event and update the event value in event->count.
2428 *
2429 * This does not protect us against NMI, but disable()
2430 * sets the disabled bit in the control field of event _before_
2431 * accessing the event control register. If a NMI hits, then it will
2432 * not restart the event.
2433 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002434void __perf_event_task_sched_out(struct task_struct *task,
2435 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002436{
2437 int ctxn;
2438
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002439 for_each_task_context_nr(ctxn)
2440 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002441
2442 /*
2443 * if cgroup events exist on this CPU, then we need
2444 * to check if we have to switch out PMU state.
2445 * cgroup event are system-wide mode only
2446 */
2447 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002448 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002449}
2450
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002451static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002452{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002453 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002454
2455 if (!cpuctx->task_ctx)
2456 return;
2457
2458 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2459 return;
2460
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002461 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002462 cpuctx->task_ctx = NULL;
2463}
2464
2465/*
2466 * Called with IRQs disabled
2467 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002468static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2469 enum event_type_t event_type)
2470{
2471 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002472}
2473
2474static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002475ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002476 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002477{
2478 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002479
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002480 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2481 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002482 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002483 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002484 continue;
2485
Stephane Eraniane5d13672011-02-14 11:20:01 +02002486 /* may need to reset tstamp_enabled */
2487 if (is_cgroup_event(event))
2488 perf_cgroup_mark_enabled(event, ctx);
2489
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002490 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002491 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002492
2493 /*
2494 * If this pinned group hasn't been scheduled,
2495 * put it in error state.
2496 */
2497 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2498 update_group_times(event);
2499 event->state = PERF_EVENT_STATE_ERROR;
2500 }
2501 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002502}
2503
2504static void
2505ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002506 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002507{
2508 struct perf_event *event;
2509 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002510
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002511 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2512 /* Ignore events in OFF or ERROR state */
2513 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002514 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002515 /*
2516 * Listen to the 'cpu' scheduling filter constraint
2517 * of events:
2518 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002519 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002520 continue;
2521
Stephane Eraniane5d13672011-02-14 11:20:01 +02002522 /* may need to reset tstamp_enabled */
2523 if (is_cgroup_event(event))
2524 perf_cgroup_mark_enabled(event, ctx);
2525
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002526 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002527 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002528 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002529 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002530 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002531}
2532
2533static void
2534ctx_sched_in(struct perf_event_context *ctx,
2535 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002536 enum event_type_t event_type,
2537 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002538{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002539 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002540 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002541
Peter Zijlstradb24d332011-04-09 21:17:45 +02002542 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002543 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002544 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002545
Stephane Eraniane5d13672011-02-14 11:20:01 +02002546 now = perf_clock();
2547 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002548 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002549 /*
2550 * First go through the list and put on any pinned groups
2551 * in order to give them the best chance of going on.
2552 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002553 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002554 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002555
2556 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002557 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002558 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002559}
2560
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002561static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002562 enum event_type_t event_type,
2563 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002564{
2565 struct perf_event_context *ctx = &cpuctx->ctx;
2566
Stephane Eraniane5d13672011-02-14 11:20:01 +02002567 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002568}
2569
Stephane Eraniane5d13672011-02-14 11:20:01 +02002570static void perf_event_context_sched_in(struct perf_event_context *ctx,
2571 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002572{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002573 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002574
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002575 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002576 if (cpuctx->task_ctx == ctx)
2577 return;
2578
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002579 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002580 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002581 /*
2582 * We want to keep the following priority order:
2583 * cpu pinned (that don't need to move), task pinned,
2584 * cpu flexible, task flexible.
2585 */
2586 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2587
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002588 if (ctx->nr_events)
2589 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002590
Gleb Natapov86b47c22011-11-22 16:08:21 +02002591 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2592
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002593 perf_pmu_enable(ctx->pmu);
2594 perf_ctx_unlock(cpuctx, ctx);
2595
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002596 /*
2597 * Since these rotations are per-cpu, we need to ensure the
2598 * cpu-context we got scheduled on is actually rotating.
2599 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002600 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002601}
2602
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002603/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002604 * When sampling the branck stack in system-wide, it may be necessary
2605 * to flush the stack on context switch. This happens when the branch
2606 * stack does not tag its entries with the pid of the current task.
2607 * Otherwise it becomes impossible to associate a branch entry with a
2608 * task. This ambiguity is more likely to appear when the branch stack
2609 * supports priv level filtering and the user sets it to monitor only
2610 * at the user level (which could be a useful measurement in system-wide
2611 * mode). In that case, the risk is high of having a branch stack with
2612 * branch from multiple tasks. Flushing may mean dropping the existing
2613 * entries or stashing them somewhere in the PMU specific code layer.
2614 *
2615 * This function provides the context switch callback to the lower code
2616 * layer. It is invoked ONLY when there is at least one system-wide context
2617 * with at least one active event using taken branch sampling.
2618 */
2619static void perf_branch_stack_sched_in(struct task_struct *prev,
2620 struct task_struct *task)
2621{
2622 struct perf_cpu_context *cpuctx;
2623 struct pmu *pmu;
2624 unsigned long flags;
2625
2626 /* no need to flush branch stack if not changing task */
2627 if (prev == task)
2628 return;
2629
2630 local_irq_save(flags);
2631
2632 rcu_read_lock();
2633
2634 list_for_each_entry_rcu(pmu, &pmus, entry) {
2635 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2636
2637 /*
2638 * check if the context has at least one
2639 * event using PERF_SAMPLE_BRANCH_STACK
2640 */
2641 if (cpuctx->ctx.nr_branch_stack > 0
2642 && pmu->flush_branch_stack) {
2643
Stephane Eraniand010b332012-02-09 23:21:00 +01002644 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2645
2646 perf_pmu_disable(pmu);
2647
2648 pmu->flush_branch_stack();
2649
2650 perf_pmu_enable(pmu);
2651
2652 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2653 }
2654 }
2655
2656 rcu_read_unlock();
2657
2658 local_irq_restore(flags);
2659}
2660
2661/*
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002662 * Called from scheduler to add the events of the current task
2663 * with interrupts disabled.
2664 *
2665 * We restore the event value and then enable it.
2666 *
2667 * This does not protect us against NMI, but enable()
2668 * sets the enabled bit in the control field of event _before_
2669 * accessing the event control register. If a NMI hits, then it will
2670 * keep the event running.
2671 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002672void __perf_event_task_sched_in(struct task_struct *prev,
2673 struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002674{
2675 struct perf_event_context *ctx;
2676 int ctxn;
2677
2678 for_each_task_context_nr(ctxn) {
2679 ctx = task->perf_event_ctxp[ctxn];
2680 if (likely(!ctx))
2681 continue;
2682
Stephane Eraniane5d13672011-02-14 11:20:01 +02002683 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002684 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002685 /*
2686 * if cgroup events exist on this CPU, then we need
2687 * to check if we have to switch in PMU state.
2688 * cgroup event are system-wide mode only
2689 */
2690 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002691 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002692
2693 /* check for system-wide branch_stack events */
2694 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2695 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002696}
2697
Peter Zijlstraabd50712010-01-26 18:50:16 +01002698static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2699{
2700 u64 frequency = event->attr.sample_freq;
2701 u64 sec = NSEC_PER_SEC;
2702 u64 divisor, dividend;
2703
2704 int count_fls, nsec_fls, frequency_fls, sec_fls;
2705
2706 count_fls = fls64(count);
2707 nsec_fls = fls64(nsec);
2708 frequency_fls = fls64(frequency);
2709 sec_fls = 30;
2710
2711 /*
2712 * We got @count in @nsec, with a target of sample_freq HZ
2713 * the target period becomes:
2714 *
2715 * @count * 10^9
2716 * period = -------------------
2717 * @nsec * sample_freq
2718 *
2719 */
2720
2721 /*
2722 * Reduce accuracy by one bit such that @a and @b converge
2723 * to a similar magnitude.
2724 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002725#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002726do { \
2727 if (a##_fls > b##_fls) { \
2728 a >>= 1; \
2729 a##_fls--; \
2730 } else { \
2731 b >>= 1; \
2732 b##_fls--; \
2733 } \
2734} while (0)
2735
2736 /*
2737 * Reduce accuracy until either term fits in a u64, then proceed with
2738 * the other, so that finally we can do a u64/u64 division.
2739 */
2740 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2741 REDUCE_FLS(nsec, frequency);
2742 REDUCE_FLS(sec, count);
2743 }
2744
2745 if (count_fls + sec_fls > 64) {
2746 divisor = nsec * frequency;
2747
2748 while (count_fls + sec_fls > 64) {
2749 REDUCE_FLS(count, sec);
2750 divisor >>= 1;
2751 }
2752
2753 dividend = count * sec;
2754 } else {
2755 dividend = count * sec;
2756
2757 while (nsec_fls + frequency_fls > 64) {
2758 REDUCE_FLS(nsec, frequency);
2759 dividend >>= 1;
2760 }
2761
2762 divisor = nsec * frequency;
2763 }
2764
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002765 if (!divisor)
2766 return dividend;
2767
Peter Zijlstraabd50712010-01-26 18:50:16 +01002768 return div64_u64(dividend, divisor);
2769}
2770
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002771static DEFINE_PER_CPU(int, perf_throttled_count);
2772static DEFINE_PER_CPU(u64, perf_throttled_seq);
2773
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002774static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002775{
2776 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002777 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002778 s64 delta;
2779
Peter Zijlstraabd50712010-01-26 18:50:16 +01002780 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002781
2782 delta = (s64)(period - hwc->sample_period);
2783 delta = (delta + 7) / 8; /* low pass filter */
2784
2785 sample_period = hwc->sample_period + delta;
2786
2787 if (!sample_period)
2788 sample_period = 1;
2789
2790 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002791
Peter Zijlstrae7850592010-05-21 14:43:08 +02002792 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002793 if (disable)
2794 event->pmu->stop(event, PERF_EF_UPDATE);
2795
Peter Zijlstrae7850592010-05-21 14:43:08 +02002796 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002797
2798 if (disable)
2799 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002800 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002801}
2802
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002803/*
2804 * combine freq adjustment with unthrottling to avoid two passes over the
2805 * events. At the same time, make sure, having freq events does not change
2806 * the rate of unthrottling as that would introduce bias.
2807 */
2808static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2809 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002810{
2811 struct perf_event *event;
2812 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002813 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002814 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002815
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002816 /*
2817 * only need to iterate over all events iff:
2818 * - context have events in frequency mode (needs freq adjust)
2819 * - there are events to unthrottle on this cpu
2820 */
2821 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002822 return;
2823
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002824 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002825 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002826
Paul Mackerras03541f82009-10-14 16:58:03 +11002827 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002828 if (event->state != PERF_EVENT_STATE_ACTIVE)
2829 continue;
2830
Stephane Eranian5632ab12011-01-03 18:20:01 +02002831 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002832 continue;
2833
Alexander Shishkin44377272013-12-16 14:17:36 +02002834 perf_pmu_disable(event->pmu);
2835
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002836 hwc = &event->hw;
2837
Jiri Olsaae23bff2013-08-24 16:45:54 +02002838 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002839 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002840 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002841 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002842 }
2843
2844 if (!event->attr.freq || !event->attr.sample_freq)
Alexander Shishkin44377272013-12-16 14:17:36 +02002845 goto next;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002846
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002847 /*
2848 * stop the event and update event->count
2849 */
2850 event->pmu->stop(event, PERF_EF_UPDATE);
2851
Peter Zijlstrae7850592010-05-21 14:43:08 +02002852 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002853 delta = now - hwc->freq_count_stamp;
2854 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002855
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002856 /*
2857 * restart the event
2858 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002859 * we have stopped the event so tell that
2860 * to perf_adjust_period() to avoid stopping it
2861 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002862 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002863 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002864 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002865
2866 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Alexander Shishkin44377272013-12-16 14:17:36 +02002867 next:
2868 perf_pmu_enable(event->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002869 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002870
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002871 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002872 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002873}
2874
2875/*
2876 * Round-robin a context's events:
2877 */
2878static void rotate_ctx(struct perf_event_context *ctx)
2879{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002880 /*
2881 * Rotate the first entry last of non-pinned groups. Rotation might be
2882 * disabled by the inheritance code.
2883 */
2884 if (!ctx->rotate_disable)
2885 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002886}
2887
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002888/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002889 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2890 * because they're strictly cpu affine and rotate_start is called with IRQs
2891 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002892 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002893static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002894{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002895 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002896 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002897
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002898 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002899 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002900 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2901 rotate = 1;
2902 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002903
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002904 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002905 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002906 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002907 if (ctx->nr_events != ctx->nr_active)
2908 rotate = 1;
2909 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002910
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002911 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002912 goto done;
2913
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002914 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002915 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002916
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002917 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2918 if (ctx)
2919 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002920
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002921 rotate_ctx(&cpuctx->ctx);
2922 if (ctx)
2923 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002924
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002925 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002926
2927 perf_pmu_enable(cpuctx->ctx.pmu);
2928 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002929done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002930 if (remove)
2931 list_del_init(&cpuctx->rotation_list);
Stephane Eranian9e630202013-04-03 14:21:33 +02002932
2933 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002934}
2935
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002936#ifdef CONFIG_NO_HZ_FULL
2937bool perf_event_can_stop_tick(void)
2938{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02002939 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002940 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002941 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002942 else
2943 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002944}
2945#endif
2946
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002947void perf_event_task_tick(void)
2948{
2949 struct list_head *head = &__get_cpu_var(rotation_list);
2950 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002951 struct perf_event_context *ctx;
2952 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002953
2954 WARN_ON(!irqs_disabled());
2955
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002956 __this_cpu_inc(perf_throttled_seq);
2957 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2958
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002959 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002960 ctx = &cpuctx->ctx;
2961 perf_adjust_freq_unthr_context(ctx, throttled);
2962
2963 ctx = cpuctx->task_ctx;
2964 if (ctx)
2965 perf_adjust_freq_unthr_context(ctx, throttled);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002966 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002967}
2968
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002969static int event_enable_on_exec(struct perf_event *event,
2970 struct perf_event_context *ctx)
2971{
2972 if (!event->attr.enable_on_exec)
2973 return 0;
2974
2975 event->attr.enable_on_exec = 0;
2976 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2977 return 0;
2978
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002979 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002980
2981 return 1;
2982}
2983
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002984/*
2985 * Enable all of a task's events that have been marked enable-on-exec.
2986 * This expects task == current.
2987 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002988static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002989{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002990 struct perf_event *event;
2991 unsigned long flags;
2992 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002993 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002994
2995 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002996 if (!ctx || !ctx->nr_events)
2997 goto out;
2998
Stephane Eraniane566b762011-04-06 02:54:54 +02002999 /*
3000 * We must ctxsw out cgroup events to avoid conflict
3001 * when invoking perf_task_event_sched_in() later on
3002 * in this function. Otherwise we end up trying to
3003 * ctxswin cgroup events which are already scheduled
3004 * in.
3005 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02003006 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003007
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003008 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02003009 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003010
Peter Zijlstrab79387e2011-11-22 11:25:43 +01003011 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003012 ret = event_enable_on_exec(event, ctx);
3013 if (ret)
3014 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003015 }
3016
3017 /*
3018 * Unclone this context if we enabled any event.
3019 */
3020 if (enabled)
3021 unclone_ctx(ctx);
3022
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003023 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024
Stephane Eraniane566b762011-04-06 02:54:54 +02003025 /*
3026 * Also calls ctxswin for cgroup events, if any:
3027 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003028 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003029out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003030 local_irq_restore(flags);
3031}
3032
Peter Zijlstrae041e322014-05-21 17:32:19 +02003033void perf_event_exec(void)
3034{
3035 struct perf_event_context *ctx;
3036 int ctxn;
3037
3038 rcu_read_lock();
3039 for_each_task_context_nr(ctxn) {
3040 ctx = current->perf_event_ctxp[ctxn];
3041 if (!ctx)
3042 continue;
3043
3044 perf_event_enable_on_exec(ctx);
3045 }
3046 rcu_read_unlock();
3047}
3048
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003049/*
3050 * Cross CPU call to read the hardware event
3051 */
3052static void __perf_event_read(void *info)
3053{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003054 struct perf_event *event = info;
3055 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003056 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003057
3058 /*
3059 * If this is a task context, we need to check whether it is
3060 * the current task context of this cpu. If not it has been
3061 * scheduled out before the smp call arrived. In that case
3062 * event->count would have been updated to a recent sample
3063 * when the event was scheduled out.
3064 */
3065 if (ctx->task && cpuctx->task_ctx != ctx)
3066 return;
3067
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003068 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003069 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003070 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003071 update_cgrp_time_from_event(event);
3072 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003073 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01003074 if (event->state == PERF_EVENT_STATE_ACTIVE)
3075 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003076 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003077}
3078
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003079static inline u64 perf_event_count(struct perf_event *event)
3080{
Peter Zijlstrae7850592010-05-21 14:43:08 +02003081 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003082}
3083
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003084static u64 perf_event_read(struct perf_event *event)
3085{
3086 /*
3087 * If event is enabled and currently active on a CPU, update the
3088 * value in the event structure:
3089 */
3090 if (event->state == PERF_EVENT_STATE_ACTIVE) {
3091 smp_call_function_single(event->oncpu,
3092 __perf_event_read, event, 1);
3093 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01003094 struct perf_event_context *ctx = event->ctx;
3095 unsigned long flags;
3096
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003097 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003098 /*
3099 * may read while context is not active
3100 * (e.g., thread is blocked), in that case
3101 * we cannot update context time
3102 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02003103 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02003104 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003105 update_cgrp_time_from_event(event);
3106 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003107 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003108 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003109 }
3110
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003111 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003112}
3113
3114/*
3115 * Initialize the perf_event context in a task_struct:
3116 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003117static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003118{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003119 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003120 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003121 INIT_LIST_HEAD(&ctx->pinned_groups);
3122 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003123 INIT_LIST_HEAD(&ctx->event_list);
3124 atomic_set(&ctx->refcount, 1);
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003125 INIT_DELAYED_WORK(&ctx->orphans_remove, orphans_remove_work);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003126}
3127
Peter Zijlstraeb184472010-09-07 15:55:13 +02003128static struct perf_event_context *
3129alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003130{
3131 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003132
3133 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3134 if (!ctx)
3135 return NULL;
3136
3137 __perf_event_init_context(ctx);
3138 if (task) {
3139 ctx->task = task;
3140 get_task_struct(task);
3141 }
3142 ctx->pmu = pmu;
3143
3144 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003145}
3146
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003147static struct task_struct *
3148find_lively_task_by_vpid(pid_t vpid)
3149{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003150 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003151 int err;
3152
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003153 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003154 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003155 task = current;
3156 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003157 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003158 if (task)
3159 get_task_struct(task);
3160 rcu_read_unlock();
3161
3162 if (!task)
3163 return ERR_PTR(-ESRCH);
3164
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003165 /* Reuse ptrace permission checks for now. */
3166 err = -EACCES;
3167 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3168 goto errout;
3169
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003170 return task;
3171errout:
3172 put_task_struct(task);
3173 return ERR_PTR(err);
3174
3175}
3176
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003177/*
3178 * Returns a matching context with refcount and pincount.
3179 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003180static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07003181find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003182{
3183 struct perf_event_context *ctx;
3184 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003185 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003186 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003187
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003188 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003189 /* Must be root to operate on a CPU event: */
3190 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3191 return ERR_PTR(-EACCES);
3192
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003193 /*
3194 * We could be clever and allow to attach a event to an
3195 * offline CPU and activate it when the CPU comes up, but
3196 * that's for later.
3197 */
3198 if (!cpu_online(cpu))
3199 return ERR_PTR(-ENODEV);
3200
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003201 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003202 ctx = &cpuctx->ctx;
3203 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003204 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003205
3206 return ctx;
3207 }
3208
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003209 err = -EINVAL;
3210 ctxn = pmu->task_ctx_nr;
3211 if (ctxn < 0)
3212 goto errout;
3213
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003214retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003215 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003216 if (ctx) {
3217 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003218 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003219 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003220 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003221 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003222 err = -ENOMEM;
3223 if (!ctx)
3224 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003225
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003226 err = 0;
3227 mutex_lock(&task->perf_event_mutex);
3228 /*
3229 * If it has already passed perf_event_exit_task().
3230 * we must see PF_EXITING, it takes this mutex too.
3231 */
3232 if (task->flags & PF_EXITING)
3233 err = -ESRCH;
3234 else if (task->perf_event_ctxp[ctxn])
3235 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003236 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003237 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003238 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003239 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003240 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003241 mutex_unlock(&task->perf_event_mutex);
3242
3243 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003244 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003245
3246 if (err == -EAGAIN)
3247 goto retry;
3248 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003249 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003250 }
3251
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003252 return ctx;
3253
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003254errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003255 return ERR_PTR(err);
3256}
3257
Li Zefan6fb29152009-10-15 11:21:42 +08003258static void perf_event_free_filter(struct perf_event *event);
3259
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003260static void free_event_rcu(struct rcu_head *head)
3261{
3262 struct perf_event *event;
3263
3264 event = container_of(head, struct perf_event, rcu_head);
3265 if (event->ns)
3266 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003267 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003268 kfree(event);
3269}
3270
Frederic Weisbecker76369132011-05-19 19:55:04 +02003271static void ring_buffer_put(struct ring_buffer *rb);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003272static void ring_buffer_attach(struct perf_event *event,
3273 struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003274
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003275static void unaccount_event_cpu(struct perf_event *event, int cpu)
3276{
3277 if (event->parent)
3278 return;
3279
3280 if (has_branch_stack(event)) {
3281 if (!(event->attach_state & PERF_ATTACH_TASK))
3282 atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
3283 }
3284 if (is_cgroup_event(event))
3285 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3286}
3287
3288static void unaccount_event(struct perf_event *event)
3289{
3290 if (event->parent)
3291 return;
3292
3293 if (event->attach_state & PERF_ATTACH_TASK)
3294 static_key_slow_dec_deferred(&perf_sched_events);
3295 if (event->attr.mmap || event->attr.mmap_data)
3296 atomic_dec(&nr_mmap_events);
3297 if (event->attr.comm)
3298 atomic_dec(&nr_comm_events);
3299 if (event->attr.task)
3300 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003301 if (event->attr.freq)
3302 atomic_dec(&nr_freq_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003303 if (is_cgroup_event(event))
3304 static_key_slow_dec_deferred(&perf_sched_events);
3305 if (has_branch_stack(event))
3306 static_key_slow_dec_deferred(&perf_sched_events);
3307
3308 unaccount_event_cpu(event, event->cpu);
3309}
3310
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003311static void __free_event(struct perf_event *event)
3312{
3313 if (!event->parent) {
3314 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3315 put_callchain_buffers();
3316 }
3317
3318 if (event->destroy)
3319 event->destroy(event);
3320
3321 if (event->ctx)
3322 put_ctx(event->ctx);
3323
Yan, Zhengc464c762014-03-18 16:56:41 +08003324 if (event->pmu)
3325 module_put(event->pmu->module);
3326
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003327 call_rcu(&event->rcu_head, free_event_rcu);
3328}
Peter Zijlstra683ede42014-05-05 12:11:24 +02003329
3330static void _free_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003331{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003332 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003333
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003334 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003335
Frederic Weisbecker76369132011-05-19 19:55:04 +02003336 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003337 /*
3338 * Can happen when we close an event with re-directed output.
3339 *
3340 * Since we have a 0 refcount, perf_mmap_close() will skip
3341 * over us; possibly making our ring_buffer_put() the last.
3342 */
3343 mutex_lock(&event->mmap_mutex);
Peter Zijlstrab69cf532014-03-14 10:50:33 +01003344 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003345 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003346 }
3347
Stephane Eraniane5d13672011-02-14 11:20:01 +02003348 if (is_cgroup_event(event))
3349 perf_detach_cgroup(event);
3350
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003351 __free_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003352}
3353
Peter Zijlstra683ede42014-05-05 12:11:24 +02003354/*
3355 * Used to free events which have a known refcount of 1, such as in error paths
3356 * where the event isn't exposed yet and inherited events.
3357 */
3358static void free_event(struct perf_event *event)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003359{
Peter Zijlstra683ede42014-05-05 12:11:24 +02003360 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3361 "unexpected event refcount: %ld; ptr=%p\n",
3362 atomic_long_read(&event->refcount), event)) {
3363 /* leak to avoid use-after-free */
3364 return;
3365 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003366
Peter Zijlstra683ede42014-05-05 12:11:24 +02003367 _free_event(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003368}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003369
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003370/*
Jiri Olsaf8697762014-08-01 14:33:01 +02003371 * Remove user event from the owner task.
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003372 */
Jiri Olsaf8697762014-08-01 14:33:01 +02003373static void perf_remove_from_owner(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003374{
Peter Zijlstra88821352010-11-09 19:01:43 +01003375 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003376
Peter Zijlstra88821352010-11-09 19:01:43 +01003377 rcu_read_lock();
3378 owner = ACCESS_ONCE(event->owner);
3379 /*
3380 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3381 * !owner it means the list deletion is complete and we can indeed
3382 * free this event, otherwise we need to serialize on
3383 * owner->perf_event_mutex.
3384 */
3385 smp_read_barrier_depends();
3386 if (owner) {
3387 /*
3388 * Since delayed_put_task_struct() also drops the last
3389 * task reference we can safely take a new reference
3390 * while holding the rcu_read_lock().
3391 */
3392 get_task_struct(owner);
3393 }
3394 rcu_read_unlock();
3395
3396 if (owner) {
3397 mutex_lock(&owner->perf_event_mutex);
3398 /*
3399 * We have to re-check the event->owner field, if it is cleared
3400 * we raced with perf_event_exit_task(), acquiring the mutex
3401 * ensured they're done, and we can proceed with freeing the
3402 * event.
3403 */
3404 if (event->owner)
3405 list_del_init(&event->owner_entry);
3406 mutex_unlock(&owner->perf_event_mutex);
3407 put_task_struct(owner);
3408 }
Jiri Olsaf8697762014-08-01 14:33:01 +02003409}
3410
3411/*
3412 * Called when the last reference to the file is gone.
3413 */
3414static void put_event(struct perf_event *event)
3415{
3416 struct perf_event_context *ctx = event->ctx;
3417
3418 if (!atomic_long_dec_and_test(&event->refcount))
3419 return;
3420
3421 if (!is_kernel_event(event))
3422 perf_remove_from_owner(event);
Peter Zijlstra88821352010-11-09 19:01:43 +01003423
Peter Zijlstra683ede42014-05-05 12:11:24 +02003424 WARN_ON_ONCE(ctx->parent_ctx);
3425 /*
3426 * There are two ways this annotation is useful:
3427 *
3428 * 1) there is a lock recursion from perf_event_exit_task
3429 * see the comment there.
3430 *
3431 * 2) there is a lock-inversion with mmap_sem through
3432 * perf_event_read_group(), which takes faults while
3433 * holding ctx->mutex, however this is called after
3434 * the last filedesc died, so there is no possibility
3435 * to trigger the AB-BA case.
3436 */
3437 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
3438 perf_remove_from_context(event, true);
3439 mutex_unlock(&ctx->mutex);
3440
3441 _free_event(event);
Al Viroa6fa9412012-08-20 14:59:25 +01003442}
3443
Peter Zijlstra683ede42014-05-05 12:11:24 +02003444int perf_event_release_kernel(struct perf_event *event)
3445{
3446 put_event(event);
3447 return 0;
3448}
3449EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3450
Al Viroa6fa9412012-08-20 14:59:25 +01003451static int perf_release(struct inode *inode, struct file *file)
3452{
3453 put_event(file->private_data);
3454 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003455}
3456
Jiri Olsafadfe7b2014-08-01 14:33:02 +02003457/*
3458 * Remove all orphanes events from the context.
3459 */
3460static void orphans_remove_work(struct work_struct *work)
3461{
3462 struct perf_event_context *ctx;
3463 struct perf_event *event, *tmp;
3464
3465 ctx = container_of(work, struct perf_event_context,
3466 orphans_remove.work);
3467
3468 mutex_lock(&ctx->mutex);
3469 list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) {
3470 struct perf_event *parent_event = event->parent;
3471
3472 if (!is_orphaned_child(event))
3473 continue;
3474
3475 perf_remove_from_context(event, true);
3476
3477 mutex_lock(&parent_event->child_mutex);
3478 list_del_init(&event->child_list);
3479 mutex_unlock(&parent_event->child_mutex);
3480
3481 free_event(event);
3482 put_event(parent_event);
3483 }
3484
3485 raw_spin_lock_irq(&ctx->lock);
3486 ctx->orphans_remove_sched = false;
3487 raw_spin_unlock_irq(&ctx->lock);
3488 mutex_unlock(&ctx->mutex);
3489
3490 put_ctx(ctx);
3491}
3492
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003493u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003494{
3495 struct perf_event *child;
3496 u64 total = 0;
3497
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003498 *enabled = 0;
3499 *running = 0;
3500
Peter Zijlstra6f105812009-11-20 22:19:56 +01003501 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003502 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003503 *enabled += event->total_time_enabled +
3504 atomic64_read(&event->child_total_time_enabled);
3505 *running += event->total_time_running +
3506 atomic64_read(&event->child_total_time_running);
3507
3508 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003509 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003510 *enabled += child->total_time_enabled;
3511 *running += child->total_time_running;
3512 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003513 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003514
3515 return total;
3516}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003517EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003518
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003519static int perf_event_read_group(struct perf_event *event,
3520 u64 read_format, char __user *buf)
3521{
3522 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003523 int n = 0, size = 0, ret = -EFAULT;
3524 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003525 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003526 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003527
Peter Zijlstra6f105812009-11-20 22:19:56 +01003528 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003529 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003530
3531 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003532 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3533 values[n++] = enabled;
3534 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3535 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003536 values[n++] = count;
3537 if (read_format & PERF_FORMAT_ID)
3538 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003539
3540 size = n * sizeof(u64);
3541
3542 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003543 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003544
Peter Zijlstra6f105812009-11-20 22:19:56 +01003545 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003546
3547 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003548 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003549
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003550 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003551 if (read_format & PERF_FORMAT_ID)
3552 values[n++] = primary_event_id(sub);
3553
3554 size = n * sizeof(u64);
3555
Stephane Eranian184d3da2009-11-23 21:40:49 -08003556 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003557 ret = -EFAULT;
3558 goto unlock;
3559 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003560
3561 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003562 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003563unlock:
3564 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003565
Peter Zijlstraabf48682009-11-20 22:19:49 +01003566 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003567}
3568
3569static int perf_event_read_one(struct perf_event *event,
3570 u64 read_format, char __user *buf)
3571{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003572 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003573 u64 values[4];
3574 int n = 0;
3575
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003576 values[n++] = perf_event_read_value(event, &enabled, &running);
3577 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3578 values[n++] = enabled;
3579 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3580 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003581 if (read_format & PERF_FORMAT_ID)
3582 values[n++] = primary_event_id(event);
3583
3584 if (copy_to_user(buf, values, n * sizeof(u64)))
3585 return -EFAULT;
3586
3587 return n * sizeof(u64);
3588}
3589
Jiri Olsadc633982014-09-12 13:18:26 +02003590static bool is_event_hup(struct perf_event *event)
3591{
3592 bool no_children;
3593
3594 if (event->state != PERF_EVENT_STATE_EXIT)
3595 return false;
3596
3597 mutex_lock(&event->child_mutex);
3598 no_children = list_empty(&event->child_list);
3599 mutex_unlock(&event->child_mutex);
3600 return no_children;
3601}
3602
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003603/*
3604 * Read the performance event - simple non blocking version for now
3605 */
3606static ssize_t
3607perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3608{
3609 u64 read_format = event->attr.read_format;
3610 int ret;
3611
3612 /*
3613 * Return end-of-file for a read on a event that is in
3614 * error state (i.e. because it was pinned but it couldn't be
3615 * scheduled on to the CPU at some point).
3616 */
Jiri Olsac88f2092014-09-08 16:31:07 +02003617 if (event->state == PERF_EVENT_STATE_ERROR)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003618 return 0;
3619
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003620 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003621 return -ENOSPC;
3622
3623 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003624 if (read_format & PERF_FORMAT_GROUP)
3625 ret = perf_event_read_group(event, read_format, buf);
3626 else
3627 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003628
3629 return ret;
3630}
3631
3632static ssize_t
3633perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3634{
3635 struct perf_event *event = file->private_data;
3636
3637 return perf_read_hw(event, buf, count);
3638}
3639
3640static unsigned int perf_poll(struct file *file, poll_table *wait)
3641{
3642 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003643 struct ring_buffer *rb;
Jiri Olsa61b67682014-08-13 19:39:56 +02003644 unsigned int events = POLLHUP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003645
Sebastian Andrzej Siewiore708d7a2014-08-04 15:31:08 +02003646 poll_wait(file, &event->waitq, wait);
Jiri Olsa179033b2014-08-07 11:48:26 -04003647
Jiri Olsadc633982014-09-12 13:18:26 +02003648 if (is_event_hup(event))
Jiri Olsa179033b2014-08-07 11:48:26 -04003649 return events;
3650
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003651 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003652 * Pin the event->rb by taking event->mmap_mutex; otherwise
3653 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003654 */
3655 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003656 rb = event->rb;
3657 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003658 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003659 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003660 return events;
3661}
3662
3663static void perf_event_reset(struct perf_event *event)
3664{
3665 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003666 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003667 perf_event_update_userpage(event);
3668}
3669
3670/*
3671 * Holding the top-level event's child_mutex means that any
3672 * descendant process that has inherited this event will block
3673 * in sync_child_event if it goes to exit, thus satisfying the
3674 * task existence requirements of perf_event_enable/disable.
3675 */
3676static void perf_event_for_each_child(struct perf_event *event,
3677 void (*func)(struct perf_event *))
3678{
3679 struct perf_event *child;
3680
3681 WARN_ON_ONCE(event->ctx->parent_ctx);
3682 mutex_lock(&event->child_mutex);
3683 func(event);
3684 list_for_each_entry(child, &event->child_list, child_list)
3685 func(child);
3686 mutex_unlock(&event->child_mutex);
3687}
3688
3689static void perf_event_for_each(struct perf_event *event,
3690 void (*func)(struct perf_event *))
3691{
3692 struct perf_event_context *ctx = event->ctx;
3693 struct perf_event *sibling;
3694
3695 WARN_ON_ONCE(ctx->parent_ctx);
3696 mutex_lock(&ctx->mutex);
3697 event = event->group_leader;
3698
3699 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003700 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003701 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003702 mutex_unlock(&ctx->mutex);
3703}
3704
3705static int perf_event_period(struct perf_event *event, u64 __user *arg)
3706{
3707 struct perf_event_context *ctx = event->ctx;
Peter Zijlstrabad71922013-11-27 13:54:38 +00003708 int ret = 0, active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003709 u64 value;
3710
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003711 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003712 return -EINVAL;
3713
John Blackwoodad0cf342010-09-28 18:03:11 -04003714 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003715 return -EFAULT;
3716
3717 if (!value)
3718 return -EINVAL;
3719
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003720 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003721 if (event->attr.freq) {
3722 if (value > sysctl_perf_event_sample_rate) {
3723 ret = -EINVAL;
3724 goto unlock;
3725 }
3726
3727 event->attr.sample_freq = value;
3728 } else {
3729 event->attr.sample_period = value;
3730 event->hw.sample_period = value;
3731 }
Peter Zijlstrabad71922013-11-27 13:54:38 +00003732
3733 active = (event->state == PERF_EVENT_STATE_ACTIVE);
3734 if (active) {
3735 perf_pmu_disable(ctx->pmu);
3736 event->pmu->stop(event, PERF_EF_UPDATE);
3737 }
3738
3739 local64_set(&event->hw.period_left, 0);
3740
3741 if (active) {
3742 event->pmu->start(event, PERF_EF_RELOAD);
3743 perf_pmu_enable(ctx->pmu);
3744 }
3745
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003746unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003747 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003748
3749 return ret;
3750}
3751
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003752static const struct file_operations perf_fops;
3753
Al Viro2903ff02012-08-28 12:52:22 -04003754static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003755{
Al Viro2903ff02012-08-28 12:52:22 -04003756 struct fd f = fdget(fd);
3757 if (!f.file)
3758 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003759
Al Viro2903ff02012-08-28 12:52:22 -04003760 if (f.file->f_op != &perf_fops) {
3761 fdput(f);
3762 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003763 }
Al Viro2903ff02012-08-28 12:52:22 -04003764 *p = f;
3765 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003766}
3767
3768static int perf_event_set_output(struct perf_event *event,
3769 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003770static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003771
3772static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3773{
3774 struct perf_event *event = file->private_data;
3775 void (*func)(struct perf_event *);
3776 u32 flags = arg;
3777
3778 switch (cmd) {
3779 case PERF_EVENT_IOC_ENABLE:
3780 func = perf_event_enable;
3781 break;
3782 case PERF_EVENT_IOC_DISABLE:
3783 func = perf_event_disable;
3784 break;
3785 case PERF_EVENT_IOC_RESET:
3786 func = perf_event_reset;
3787 break;
3788
3789 case PERF_EVENT_IOC_REFRESH:
3790 return perf_event_refresh(event, arg);
3791
3792 case PERF_EVENT_IOC_PERIOD:
3793 return perf_event_period(event, (u64 __user *)arg);
3794
Jiri Olsacf4957f2012-10-24 13:37:58 +02003795 case PERF_EVENT_IOC_ID:
3796 {
3797 u64 id = primary_event_id(event);
3798
3799 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
3800 return -EFAULT;
3801 return 0;
3802 }
3803
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003804 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003805 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003806 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003807 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003808 struct perf_event *output_event;
3809 struct fd output;
3810 ret = perf_fget_light(arg, &output);
3811 if (ret)
3812 return ret;
3813 output_event = output.file->private_data;
3814 ret = perf_event_set_output(event, output_event);
3815 fdput(output);
3816 } else {
3817 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003818 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003819 return ret;
3820 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003821
Li Zefan6fb29152009-10-15 11:21:42 +08003822 case PERF_EVENT_IOC_SET_FILTER:
3823 return perf_event_set_filter(event, (void __user *)arg);
3824
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003825 default:
3826 return -ENOTTY;
3827 }
3828
3829 if (flags & PERF_IOC_FLAG_GROUP)
3830 perf_event_for_each(event, func);
3831 else
3832 perf_event_for_each_child(event, func);
3833
3834 return 0;
3835}
3836
Pawel Mollb3f20782014-06-13 16:03:32 +01003837#ifdef CONFIG_COMPAT
3838static long perf_compat_ioctl(struct file *file, unsigned int cmd,
3839 unsigned long arg)
3840{
3841 switch (_IOC_NR(cmd)) {
3842 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
3843 case _IOC_NR(PERF_EVENT_IOC_ID):
3844 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
3845 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
3846 cmd &= ~IOCSIZE_MASK;
3847 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
3848 }
3849 break;
3850 }
3851 return perf_ioctl(file, cmd, arg);
3852}
3853#else
3854# define perf_compat_ioctl NULL
3855#endif
3856
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003857int perf_event_task_enable(void)
3858{
3859 struct perf_event *event;
3860
3861 mutex_lock(&current->perf_event_mutex);
3862 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3863 perf_event_for_each_child(event, perf_event_enable);
3864 mutex_unlock(&current->perf_event_mutex);
3865
3866 return 0;
3867}
3868
3869int perf_event_task_disable(void)
3870{
3871 struct perf_event *event;
3872
3873 mutex_lock(&current->perf_event_mutex);
3874 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3875 perf_event_for_each_child(event, perf_event_disable);
3876 mutex_unlock(&current->perf_event_mutex);
3877
3878 return 0;
3879}
3880
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003881static int perf_event_index(struct perf_event *event)
3882{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003883 if (event->hw.state & PERF_HES_STOPPED)
3884 return 0;
3885
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003886 if (event->state != PERF_EVENT_STATE_ACTIVE)
3887 return 0;
3888
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01003889 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003890}
3891
Eric B Munsonc4794292011-06-23 16:34:38 -04003892static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003893 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04003894 u64 *enabled,
3895 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04003896{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003897 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04003898
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003899 *now = perf_clock();
3900 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04003901 *enabled = ctx_time - event->tstamp_enabled;
3902 *running = ctx_time - event->tstamp_running;
3903}
3904
Peter Zijlstrafa731582013-09-19 10:16:42 +02003905static void perf_event_init_userpage(struct perf_event *event)
3906{
3907 struct perf_event_mmap_page *userpg;
3908 struct ring_buffer *rb;
3909
3910 rcu_read_lock();
3911 rb = rcu_dereference(event->rb);
3912 if (!rb)
3913 goto unlock;
3914
3915 userpg = rb->user_page;
3916
3917 /* Allow new userspace to detect that bit 0 is deprecated */
3918 userpg->cap_bit0_is_deprecated = 1;
3919 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
3920
3921unlock:
3922 rcu_read_unlock();
3923}
3924
Peter Zijlstrac7206202012-03-22 17:26:36 +01003925void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003926{
3927}
3928
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003929/*
3930 * Callers need to ensure there can be no nesting of this function, otherwise
3931 * the seqlock logic goes bad. We can not serialize this because the arch
3932 * code calls this from NMI context.
3933 */
3934void perf_event_update_userpage(struct perf_event *event)
3935{
3936 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003937 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003938 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003939
3940 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02003941 rb = rcu_dereference(event->rb);
3942 if (!rb)
3943 goto unlock;
3944
Eric B Munson0d641202011-06-24 12:26:26 -04003945 /*
3946 * compute total_time_enabled, total_time_running
3947 * based on snapshot values taken when the event
3948 * was last scheduled in.
3949 *
3950 * we cannot simply called update_context_time()
3951 * because of locking issue as we can be called in
3952 * NMI context
3953 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003954 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003955
Frederic Weisbecker76369132011-05-19 19:55:04 +02003956 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003957 /*
3958 * Disable preemption so as to not let the corresponding user-space
3959 * spin too long if we get preempted.
3960 */
3961 preempt_disable();
3962 ++userpg->lock;
3963 barrier();
3964 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003965 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01003966 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003967 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003968
Eric B Munson0d641202011-06-24 12:26:26 -04003969 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003970 atomic64_read(&event->child_total_time_enabled);
3971
Eric B Munson0d641202011-06-24 12:26:26 -04003972 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003973 atomic64_read(&event->child_total_time_running);
3974
Peter Zijlstrac7206202012-03-22 17:26:36 +01003975 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003976
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003977 barrier();
3978 ++userpg->lock;
3979 preempt_enable();
3980unlock:
3981 rcu_read_unlock();
3982}
3983
Peter Zijlstra906010b2009-09-21 16:08:49 +02003984static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3985{
3986 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003987 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003988 int ret = VM_FAULT_SIGBUS;
3989
3990 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3991 if (vmf->pgoff == 0)
3992 ret = 0;
3993 return ret;
3994 }
3995
3996 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003997 rb = rcu_dereference(event->rb);
3998 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003999 goto unlock;
4000
4001 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4002 goto unlock;
4003
Frederic Weisbecker76369132011-05-19 19:55:04 +02004004 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004005 if (!vmf->page)
4006 goto unlock;
4007
4008 get_page(vmf->page);
4009 vmf->page->mapping = vma->vm_file->f_mapping;
4010 vmf->page->index = vmf->pgoff;
4011
4012 ret = 0;
4013unlock:
4014 rcu_read_unlock();
4015
4016 return ret;
4017}
4018
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004019static void ring_buffer_attach(struct perf_event *event,
4020 struct ring_buffer *rb)
4021{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004022 struct ring_buffer *old_rb = NULL;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004023 unsigned long flags;
4024
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004025 if (event->rb) {
4026 /*
4027 * Should be impossible, we set this when removing
4028 * event->rb_entry and wait/clear when adding event->rb_entry.
4029 */
4030 WARN_ON_ONCE(event->rcu_pending);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004031
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004032 old_rb = event->rb;
4033 event->rcu_batches = get_state_synchronize_rcu();
4034 event->rcu_pending = 1;
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004035
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004036 spin_lock_irqsave(&old_rb->event_lock, flags);
4037 list_del_rcu(&event->rb_entry);
4038 spin_unlock_irqrestore(&old_rb->event_lock, flags);
4039 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004040
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004041 if (event->rcu_pending && rb) {
4042 cond_synchronize_rcu(event->rcu_batches);
4043 event->rcu_pending = 0;
4044 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004045
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004046 if (rb) {
4047 spin_lock_irqsave(&rb->event_lock, flags);
4048 list_add_rcu(&event->rb_entry, &rb->event_list);
4049 spin_unlock_irqrestore(&rb->event_lock, flags);
4050 }
4051
4052 rcu_assign_pointer(event->rb, rb);
4053
4054 if (old_rb) {
4055 ring_buffer_put(old_rb);
4056 /*
4057 * Since we detached before setting the new rb, so that we
4058 * could attach the new rb, we could have missed a wakeup.
4059 * Provide it now.
4060 */
4061 wake_up_all(&event->waitq);
4062 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004063}
4064
4065static void ring_buffer_wakeup(struct perf_event *event)
4066{
4067 struct ring_buffer *rb;
4068
4069 rcu_read_lock();
4070 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004071 if (rb) {
4072 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4073 wake_up_all(&event->waitq);
4074 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004075 rcu_read_unlock();
4076}
4077
Frederic Weisbecker76369132011-05-19 19:55:04 +02004078static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02004079{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004080 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02004081
Frederic Weisbecker76369132011-05-19 19:55:04 +02004082 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
4083 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004084}
4085
Frederic Weisbecker76369132011-05-19 19:55:04 +02004086static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004087{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004088 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004089
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004090 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02004091 rb = rcu_dereference(event->rb);
4092 if (rb) {
4093 if (!atomic_inc_not_zero(&rb->refcount))
4094 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004095 }
4096 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004097
Frederic Weisbecker76369132011-05-19 19:55:04 +02004098 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004099}
4100
Frederic Weisbecker76369132011-05-19 19:55:04 +02004101static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004102{
Frederic Weisbecker76369132011-05-19 19:55:04 +02004103 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004104 return;
4105
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004106 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004107
Frederic Weisbecker76369132011-05-19 19:55:04 +02004108 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004109}
4110
4111static void perf_mmap_open(struct vm_area_struct *vma)
4112{
4113 struct perf_event *event = vma->vm_file->private_data;
4114
4115 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004116 atomic_inc(&event->rb->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004117}
4118
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004119/*
4120 * A buffer can be mmap()ed multiple times; either directly through the same
4121 * event, or through other events by use of perf_event_set_output().
4122 *
4123 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4124 * the buffer here, where we still have a VM context. This means we need
4125 * to detach all events redirecting to us.
4126 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004127static void perf_mmap_close(struct vm_area_struct *vma)
4128{
4129 struct perf_event *event = vma->vm_file->private_data;
4130
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004131 struct ring_buffer *rb = ring_buffer_get(event);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004132 struct user_struct *mmap_user = rb->mmap_user;
4133 int mmap_locked = rb->mmap_locked;
4134 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004135
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004136 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004137
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004138 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004139 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004140
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004141 ring_buffer_attach(event, NULL);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004142 mutex_unlock(&event->mmap_mutex);
4143
4144 /* If there's still other mmap()s of this buffer, we're done. */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004145 if (atomic_read(&rb->mmap_count))
4146 goto out_put;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004147
4148 /*
4149 * No other mmap()s, detach from all other events that might redirect
4150 * into the now unreachable buffer. Somewhat complicated by the
4151 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4152 */
4153again:
4154 rcu_read_lock();
4155 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4156 if (!atomic_long_inc_not_zero(&event->refcount)) {
4157 /*
4158 * This event is en-route to free_event() which will
4159 * detach it and remove it from the list.
4160 */
4161 continue;
4162 }
4163 rcu_read_unlock();
4164
4165 mutex_lock(&event->mmap_mutex);
4166 /*
4167 * Check we didn't race with perf_event_set_output() which can
4168 * swizzle the rb from under us while we were waiting to
4169 * acquire mmap_mutex.
4170 *
4171 * If we find a different rb; ignore this event, a next
4172 * iteration will no longer find it on the list. We have to
4173 * still restart the iteration to make sure we're not now
4174 * iterating the wrong list.
4175 */
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004176 if (event->rb == rb)
4177 ring_buffer_attach(event, NULL);
4178
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004179 mutex_unlock(&event->mmap_mutex);
4180 put_event(event);
4181
4182 /*
4183 * Restart the iteration; either we're on the wrong list or
4184 * destroyed its integrity by doing a deletion.
4185 */
4186 goto again;
4187 }
4188 rcu_read_unlock();
4189
4190 /*
4191 * It could be there's still a few 0-ref events on the list; they'll
4192 * get cleaned up by free_event() -- they'll also still have their
4193 * ref on the rb and will free it whenever they are done with it.
4194 *
4195 * Aside from that, this buffer is 'fully' detached and unmapped,
4196 * undo the VM accounting.
4197 */
4198
4199 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4200 vma->vm_mm->pinned_vm -= mmap_locked;
4201 free_uid(mmap_user);
4202
Peter Zijlstrab69cf532014-03-14 10:50:33 +01004203out_put:
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004204 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004205}
4206
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004207static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004208 .open = perf_mmap_open,
4209 .close = perf_mmap_close,
4210 .fault = perf_mmap_fault,
4211 .page_mkwrite = perf_mmap_fault,
4212};
4213
4214static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4215{
4216 struct perf_event *event = file->private_data;
4217 unsigned long user_locked, user_lock_limit;
4218 struct user_struct *user = current_user();
4219 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02004220 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004221 unsigned long vma_size;
4222 unsigned long nr_pages;
4223 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004224 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004225
Peter Zijlstrac7920612010-05-18 10:33:24 +02004226 /*
4227 * Don't allow mmap() of inherited per-task counters. This would
4228 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004229 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004230 */
4231 if (event->cpu == -1 && event->attr.inherit)
4232 return -EINVAL;
4233
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004234 if (!(vma->vm_flags & VM_SHARED))
4235 return -EINVAL;
4236
4237 vma_size = vma->vm_end - vma->vm_start;
4238 nr_pages = (vma_size / PAGE_SIZE) - 1;
4239
4240 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004241 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004242 * can do bitmasks instead of modulo.
4243 */
4244 if (nr_pages != 0 && !is_power_of_2(nr_pages))
4245 return -EINVAL;
4246
4247 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4248 return -EINVAL;
4249
4250 if (vma->vm_pgoff != 0)
4251 return -EINVAL;
4252
4253 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004254again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004255 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004256 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004257 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004258 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004259 goto unlock;
4260 }
4261
4262 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4263 /*
4264 * Raced against perf_mmap_close() through
4265 * perf_event_set_output(). Try again, hope for better
4266 * luck.
4267 */
4268 mutex_unlock(&event->mmap_mutex);
4269 goto again;
4270 }
4271
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004272 goto unlock;
4273 }
4274
4275 user_extra = nr_pages + 1;
4276 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4277
4278 /*
4279 * Increase the limit linearly with more CPUs:
4280 */
4281 user_lock_limit *= num_online_cpus();
4282
4283 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4284
4285 extra = 0;
4286 if (user_locked > user_lock_limit)
4287 extra = user_locked - user_lock_limit;
4288
Jiri Slaby78d7d402010-03-05 13:42:54 -08004289 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004290 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004291 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004292
4293 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4294 !capable(CAP_IPC_LOCK)) {
4295 ret = -EPERM;
4296 goto unlock;
4297 }
4298
Frederic Weisbecker76369132011-05-19 19:55:04 +02004299 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004300
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004301 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004302 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004303
Vince Weaver4ec83632011-06-01 15:15:36 -04004304 rb = rb_alloc(nr_pages,
4305 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4306 event->cpu, flags);
4307
Frederic Weisbecker76369132011-05-19 19:55:04 +02004308 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004309 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004310 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004311 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004312
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004313 atomic_set(&rb->mmap_count, 1);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004314 rb->mmap_locked = extra;
4315 rb->mmap_user = get_current_user();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004316
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004317 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004318 vma->vm_mm->pinned_vm += extra;
4319
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004320 ring_buffer_attach(event, rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004321
Peter Zijlstrafa731582013-09-19 10:16:42 +02004322 perf_event_init_userpage(event);
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01004323 perf_event_update_userpage(event);
4324
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004325unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004326 if (!ret)
4327 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004328 mutex_unlock(&event->mmap_mutex);
4329
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004330 /*
4331 * Since pinned accounting is per vm we cannot allow fork() to copy our
4332 * vma.
4333 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004334 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004335 vma->vm_ops = &perf_mmap_vmops;
4336
4337 return ret;
4338}
4339
4340static int perf_fasync(int fd, struct file *filp, int on)
4341{
Al Viro496ad9a2013-01-23 17:07:38 -05004342 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004343 struct perf_event *event = filp->private_data;
4344 int retval;
4345
4346 mutex_lock(&inode->i_mutex);
4347 retval = fasync_helper(fd, filp, on, &event->fasync);
4348 mutex_unlock(&inode->i_mutex);
4349
4350 if (retval < 0)
4351 return retval;
4352
4353 return 0;
4354}
4355
4356static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004357 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004358 .release = perf_release,
4359 .read = perf_read,
4360 .poll = perf_poll,
4361 .unlocked_ioctl = perf_ioctl,
Pawel Mollb3f20782014-06-13 16:03:32 +01004362 .compat_ioctl = perf_compat_ioctl,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004363 .mmap = perf_mmap,
4364 .fasync = perf_fasync,
4365};
4366
4367/*
4368 * Perf event wakeup
4369 *
4370 * If there's data, ensure we set the poll() state and publish everything
4371 * to user-space before waking everybody up.
4372 */
4373
4374void perf_event_wakeup(struct perf_event *event)
4375{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004376 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004377
4378 if (event->pending_kill) {
4379 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4380 event->pending_kill = 0;
4381 }
4382}
4383
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004384static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004385{
4386 struct perf_event *event = container_of(entry,
4387 struct perf_event, pending);
4388
4389 if (event->pending_disable) {
4390 event->pending_disable = 0;
4391 __perf_event_disable(event);
4392 }
4393
4394 if (event->pending_wakeup) {
4395 event->pending_wakeup = 0;
4396 perf_event_wakeup(event);
4397 }
4398}
4399
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004400/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004401 * We assume there is only KVM supporting the callbacks.
4402 * Later on, we might change it to a list if there is
4403 * another virtualization implementation supporting the callbacks.
4404 */
4405struct perf_guest_info_callbacks *perf_guest_cbs;
4406
4407int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4408{
4409 perf_guest_cbs = cbs;
4410 return 0;
4411}
4412EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4413
4414int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4415{
4416 perf_guest_cbs = NULL;
4417 return 0;
4418}
4419EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4420
Jiri Olsa40189942012-08-07 15:20:37 +02004421static void
4422perf_output_sample_regs(struct perf_output_handle *handle,
4423 struct pt_regs *regs, u64 mask)
4424{
4425 int bit;
4426
4427 for_each_set_bit(bit, (const unsigned long *) &mask,
4428 sizeof(mask) * BITS_PER_BYTE) {
4429 u64 val;
4430
4431 val = perf_reg_value(regs, bit);
4432 perf_output_put(handle, val);
4433 }
4434}
4435
4436static void perf_sample_regs_user(struct perf_regs_user *regs_user,
4437 struct pt_regs *regs)
4438{
4439 if (!user_mode(regs)) {
4440 if (current->mm)
4441 regs = task_pt_regs(current);
4442 else
4443 regs = NULL;
4444 }
4445
4446 if (regs) {
4447 regs_user->regs = regs;
4448 regs_user->abi = perf_reg_abi(current);
4449 }
4450}
4451
Jiri Olsac5ebced2012-08-07 15:20:40 +02004452/*
4453 * Get remaining task size from user stack pointer.
4454 *
4455 * It'd be better to take stack vma map and limit this more
4456 * precisly, but there's no way to get it safely under interrupt,
4457 * so using TASK_SIZE as limit.
4458 */
4459static u64 perf_ustack_task_size(struct pt_regs *regs)
4460{
4461 unsigned long addr = perf_user_stack_pointer(regs);
4462
4463 if (!addr || addr >= TASK_SIZE)
4464 return 0;
4465
4466 return TASK_SIZE - addr;
4467}
4468
4469static u16
4470perf_sample_ustack_size(u16 stack_size, u16 header_size,
4471 struct pt_regs *regs)
4472{
4473 u64 task_size;
4474
4475 /* No regs, no stack pointer, no dump. */
4476 if (!regs)
4477 return 0;
4478
4479 /*
4480 * Check if we fit in with the requested stack size into the:
4481 * - TASK_SIZE
4482 * If we don't, we limit the size to the TASK_SIZE.
4483 *
4484 * - remaining sample size
4485 * If we don't, we customize the stack size to
4486 * fit in to the remaining sample size.
4487 */
4488
4489 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4490 stack_size = min(stack_size, (u16) task_size);
4491
4492 /* Current header size plus static size and dynamic size. */
4493 header_size += 2 * sizeof(u64);
4494
4495 /* Do we fit in with the current stack dump size? */
4496 if ((u16) (header_size + stack_size) < header_size) {
4497 /*
4498 * If we overflow the maximum size for the sample,
4499 * we customize the stack dump size to fit in.
4500 */
4501 stack_size = USHRT_MAX - header_size - sizeof(u64);
4502 stack_size = round_up(stack_size, sizeof(u64));
4503 }
4504
4505 return stack_size;
4506}
4507
4508static void
4509perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4510 struct pt_regs *regs)
4511{
4512 /* Case of a kernel thread, nothing to dump */
4513 if (!regs) {
4514 u64 size = 0;
4515 perf_output_put(handle, size);
4516 } else {
4517 unsigned long sp;
4518 unsigned int rem;
4519 u64 dyn_size;
4520
4521 /*
4522 * We dump:
4523 * static size
4524 * - the size requested by user or the best one we can fit
4525 * in to the sample max size
4526 * data
4527 * - user stack dump data
4528 * dynamic size
4529 * - the actual dumped size
4530 */
4531
4532 /* Static size. */
4533 perf_output_put(handle, dump_size);
4534
4535 /* Data. */
4536 sp = perf_user_stack_pointer(regs);
4537 rem = __output_copy_user(handle, (void *) sp, dump_size);
4538 dyn_size = dump_size - rem;
4539
4540 perf_output_skip(handle, rem);
4541
4542 /* Dynamic size. */
4543 perf_output_put(handle, dyn_size);
4544 }
4545}
4546
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004547static void __perf_event_header__init_id(struct perf_event_header *header,
4548 struct perf_sample_data *data,
4549 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004550{
4551 u64 sample_type = event->attr.sample_type;
4552
4553 data->type = sample_type;
4554 header->size += event->id_header_size;
4555
4556 if (sample_type & PERF_SAMPLE_TID) {
4557 /* namespace issues */
4558 data->tid_entry.pid = perf_event_pid(event, current);
4559 data->tid_entry.tid = perf_event_tid(event, current);
4560 }
4561
4562 if (sample_type & PERF_SAMPLE_TIME)
4563 data->time = perf_clock();
4564
Adrian Hunterff3d5272013-08-27 11:23:07 +03004565 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004566 data->id = primary_event_id(event);
4567
4568 if (sample_type & PERF_SAMPLE_STREAM_ID)
4569 data->stream_id = event->id;
4570
4571 if (sample_type & PERF_SAMPLE_CPU) {
4572 data->cpu_entry.cpu = raw_smp_processor_id();
4573 data->cpu_entry.reserved = 0;
4574 }
4575}
4576
Frederic Weisbecker76369132011-05-19 19:55:04 +02004577void perf_event_header__init_id(struct perf_event_header *header,
4578 struct perf_sample_data *data,
4579 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004580{
4581 if (event->attr.sample_id_all)
4582 __perf_event_header__init_id(header, data, event);
4583}
4584
4585static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4586 struct perf_sample_data *data)
4587{
4588 u64 sample_type = data->type;
4589
4590 if (sample_type & PERF_SAMPLE_TID)
4591 perf_output_put(handle, data->tid_entry);
4592
4593 if (sample_type & PERF_SAMPLE_TIME)
4594 perf_output_put(handle, data->time);
4595
4596 if (sample_type & PERF_SAMPLE_ID)
4597 perf_output_put(handle, data->id);
4598
4599 if (sample_type & PERF_SAMPLE_STREAM_ID)
4600 perf_output_put(handle, data->stream_id);
4601
4602 if (sample_type & PERF_SAMPLE_CPU)
4603 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03004604
4605 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4606 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004607}
4608
Frederic Weisbecker76369132011-05-19 19:55:04 +02004609void perf_event__output_id_sample(struct perf_event *event,
4610 struct perf_output_handle *handle,
4611 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004612{
4613 if (event->attr.sample_id_all)
4614 __perf_event__output_id_sample(handle, sample);
4615}
4616
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004617static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004618 struct perf_event *event,
4619 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004620{
4621 u64 read_format = event->attr.read_format;
4622 u64 values[4];
4623 int n = 0;
4624
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004625 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004626 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004627 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004628 atomic64_read(&event->child_total_time_enabled);
4629 }
4630 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004631 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004632 atomic64_read(&event->child_total_time_running);
4633 }
4634 if (read_format & PERF_FORMAT_ID)
4635 values[n++] = primary_event_id(event);
4636
Frederic Weisbecker76369132011-05-19 19:55:04 +02004637 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004638}
4639
4640/*
4641 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4642 */
4643static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004644 struct perf_event *event,
4645 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004646{
4647 struct perf_event *leader = event->group_leader, *sub;
4648 u64 read_format = event->attr.read_format;
4649 u64 values[5];
4650 int n = 0;
4651
4652 values[n++] = 1 + leader->nr_siblings;
4653
4654 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004655 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004656
4657 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004658 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004659
4660 if (leader != event)
4661 leader->pmu->read(leader);
4662
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004663 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004664 if (read_format & PERF_FORMAT_ID)
4665 values[n++] = primary_event_id(leader);
4666
Frederic Weisbecker76369132011-05-19 19:55:04 +02004667 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004668
4669 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4670 n = 0;
4671
Jiri Olsa6f5ab002012-10-15 20:13:45 +02004672 if ((sub != event) &&
4673 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004674 sub->pmu->read(sub);
4675
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004676 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004677 if (read_format & PERF_FORMAT_ID)
4678 values[n++] = primary_event_id(sub);
4679
Frederic Weisbecker76369132011-05-19 19:55:04 +02004680 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004681 }
4682}
4683
Stephane Eranianeed01522010-10-26 16:08:01 +02004684#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4685 PERF_FORMAT_TOTAL_TIME_RUNNING)
4686
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004687static void perf_output_read(struct perf_output_handle *handle,
4688 struct perf_event *event)
4689{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004690 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004691 u64 read_format = event->attr.read_format;
4692
4693 /*
4694 * compute total_time_enabled, total_time_running
4695 * based on snapshot values taken when the event
4696 * was last scheduled in.
4697 *
4698 * we cannot simply called update_context_time()
4699 * because of locking issue as we are called in
4700 * NMI context
4701 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004702 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004703 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004704
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004705 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004706 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004707 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004708 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004709}
4710
4711void perf_output_sample(struct perf_output_handle *handle,
4712 struct perf_event_header *header,
4713 struct perf_sample_data *data,
4714 struct perf_event *event)
4715{
4716 u64 sample_type = data->type;
4717
4718 perf_output_put(handle, *header);
4719
Adrian Hunterff3d5272013-08-27 11:23:07 +03004720 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4721 perf_output_put(handle, data->id);
4722
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004723 if (sample_type & PERF_SAMPLE_IP)
4724 perf_output_put(handle, data->ip);
4725
4726 if (sample_type & PERF_SAMPLE_TID)
4727 perf_output_put(handle, data->tid_entry);
4728
4729 if (sample_type & PERF_SAMPLE_TIME)
4730 perf_output_put(handle, data->time);
4731
4732 if (sample_type & PERF_SAMPLE_ADDR)
4733 perf_output_put(handle, data->addr);
4734
4735 if (sample_type & PERF_SAMPLE_ID)
4736 perf_output_put(handle, data->id);
4737
4738 if (sample_type & PERF_SAMPLE_STREAM_ID)
4739 perf_output_put(handle, data->stream_id);
4740
4741 if (sample_type & PERF_SAMPLE_CPU)
4742 perf_output_put(handle, data->cpu_entry);
4743
4744 if (sample_type & PERF_SAMPLE_PERIOD)
4745 perf_output_put(handle, data->period);
4746
4747 if (sample_type & PERF_SAMPLE_READ)
4748 perf_output_read(handle, event);
4749
4750 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4751 if (data->callchain) {
4752 int size = 1;
4753
4754 if (data->callchain)
4755 size += data->callchain->nr;
4756
4757 size *= sizeof(u64);
4758
Frederic Weisbecker76369132011-05-19 19:55:04 +02004759 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004760 } else {
4761 u64 nr = 0;
4762 perf_output_put(handle, nr);
4763 }
4764 }
4765
4766 if (sample_type & PERF_SAMPLE_RAW) {
4767 if (data->raw) {
4768 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004769 __output_copy(handle, data->raw->data,
4770 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004771 } else {
4772 struct {
4773 u32 size;
4774 u32 data;
4775 } raw = {
4776 .size = sizeof(u32),
4777 .data = 0,
4778 };
4779 perf_output_put(handle, raw);
4780 }
4781 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004782
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004783 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4784 if (data->br_stack) {
4785 size_t size;
4786
4787 size = data->br_stack->nr
4788 * sizeof(struct perf_branch_entry);
4789
4790 perf_output_put(handle, data->br_stack->nr);
4791 perf_output_copy(handle, data->br_stack->entries, size);
4792 } else {
4793 /*
4794 * we always store at least the value of nr
4795 */
4796 u64 nr = 0;
4797 perf_output_put(handle, nr);
4798 }
4799 }
Jiri Olsa40189942012-08-07 15:20:37 +02004800
4801 if (sample_type & PERF_SAMPLE_REGS_USER) {
4802 u64 abi = data->regs_user.abi;
4803
4804 /*
4805 * If there are no regs to dump, notice it through
4806 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4807 */
4808 perf_output_put(handle, abi);
4809
4810 if (abi) {
4811 u64 mask = event->attr.sample_regs_user;
4812 perf_output_sample_regs(handle,
4813 data->regs_user.regs,
4814 mask);
4815 }
4816 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004817
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004818 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02004819 perf_output_sample_ustack(handle,
4820 data->stack_user_size,
4821 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004822 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01004823
4824 if (sample_type & PERF_SAMPLE_WEIGHT)
4825 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01004826
4827 if (sample_type & PERF_SAMPLE_DATA_SRC)
4828 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004829
Andi Kleenfdfbbd02013-09-20 07:40:39 -07004830 if (sample_type & PERF_SAMPLE_TRANSACTION)
4831 perf_output_put(handle, data->txn);
4832
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004833 if (!event->attr.watermark) {
4834 int wakeup_events = event->attr.wakeup_events;
4835
4836 if (wakeup_events) {
4837 struct ring_buffer *rb = handle->rb;
4838 int events = local_inc_return(&rb->events);
4839
4840 if (events >= wakeup_events) {
4841 local_sub(wakeup_events, &rb->events);
4842 local_inc(&rb->wakeup);
4843 }
4844 }
4845 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004846}
4847
4848void perf_prepare_sample(struct perf_event_header *header,
4849 struct perf_sample_data *data,
4850 struct perf_event *event,
4851 struct pt_regs *regs)
4852{
4853 u64 sample_type = event->attr.sample_type;
4854
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004855 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004856 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004857
4858 header->misc = 0;
4859 header->misc |= perf_misc_flags(regs);
4860
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004861 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004862
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004863 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004864 data->ip = perf_instruction_pointer(regs);
4865
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004866 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4867 int size = 1;
4868
Andrew Vagine6dab5f2012-07-11 18:14:58 +04004869 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004870
4871 if (data->callchain)
4872 size += data->callchain->nr;
4873
4874 header->size += size * sizeof(u64);
4875 }
4876
4877 if (sample_type & PERF_SAMPLE_RAW) {
4878 int size = sizeof(u32);
4879
4880 if (data->raw)
4881 size += data->raw->size;
4882 else
4883 size += sizeof(u32);
4884
4885 WARN_ON_ONCE(size & (sizeof(u64)-1));
4886 header->size += size;
4887 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004888
4889 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4890 int size = sizeof(u64); /* nr */
4891 if (data->br_stack) {
4892 size += data->br_stack->nr
4893 * sizeof(struct perf_branch_entry);
4894 }
4895 header->size += size;
4896 }
Jiri Olsa40189942012-08-07 15:20:37 +02004897
4898 if (sample_type & PERF_SAMPLE_REGS_USER) {
4899 /* regs dump ABI info */
4900 int size = sizeof(u64);
4901
4902 perf_sample_regs_user(&data->regs_user, regs);
4903
4904 if (data->regs_user.regs) {
4905 u64 mask = event->attr.sample_regs_user;
4906 size += hweight64(mask) * sizeof(u64);
4907 }
4908
4909 header->size += size;
4910 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004911
4912 if (sample_type & PERF_SAMPLE_STACK_USER) {
4913 /*
4914 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4915 * processed as the last one or have additional check added
4916 * in case new sample type is added, because we could eat
4917 * up the rest of the sample size.
4918 */
4919 struct perf_regs_user *uregs = &data->regs_user;
4920 u16 stack_size = event->attr.sample_stack_user;
4921 u16 size = sizeof(u64);
4922
4923 if (!uregs->abi)
4924 perf_sample_regs_user(uregs, regs);
4925
4926 stack_size = perf_sample_ustack_size(stack_size, header->size,
4927 uregs->regs);
4928
4929 /*
4930 * If there is something to dump, add space for the dump
4931 * itself and for the field that tells the dynamic size,
4932 * which is how many have been actually dumped.
4933 */
4934 if (stack_size)
4935 size += sizeof(u64) + stack_size;
4936
4937 data->stack_user_size = stack_size;
4938 header->size += size;
4939 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004940}
4941
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004942static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004943 struct perf_sample_data *data,
4944 struct pt_regs *regs)
4945{
4946 struct perf_output_handle handle;
4947 struct perf_event_header header;
4948
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004949 /* protect the callchain buffers */
4950 rcu_read_lock();
4951
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004952 perf_prepare_sample(&header, data, event, regs);
4953
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004954 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004955 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004956
4957 perf_output_sample(&handle, &header, data, event);
4958
4959 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004960
4961exit:
4962 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004963}
4964
4965/*
4966 * read event_id
4967 */
4968
4969struct perf_read_event {
4970 struct perf_event_header header;
4971
4972 u32 pid;
4973 u32 tid;
4974};
4975
4976static void
4977perf_event_read_event(struct perf_event *event,
4978 struct task_struct *task)
4979{
4980 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004981 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004982 struct perf_read_event read_event = {
4983 .header = {
4984 .type = PERF_RECORD_READ,
4985 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004986 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004987 },
4988 .pid = perf_event_pid(event, task),
4989 .tid = perf_event_tid(event, task),
4990 };
4991 int ret;
4992
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004993 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004994 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004995 if (ret)
4996 return;
4997
4998 perf_output_put(&handle, read_event);
4999 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005000 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005001
5002 perf_output_end(&handle);
5003}
5004
Jiri Olsa52d857a2013-05-06 18:27:18 +02005005typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5006
5007static void
5008perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005009 perf_event_aux_output_cb output,
5010 void *data)
5011{
5012 struct perf_event *event;
5013
5014 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5015 if (event->state < PERF_EVENT_STATE_INACTIVE)
5016 continue;
5017 if (!event_filter_match(event))
5018 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02005019 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005020 }
5021}
5022
5023static void
Jiri Olsa67516842013-07-09 18:56:31 +02005024perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005025 struct perf_event_context *task_ctx)
5026{
5027 struct perf_cpu_context *cpuctx;
5028 struct perf_event_context *ctx;
5029 struct pmu *pmu;
5030 int ctxn;
5031
5032 rcu_read_lock();
5033 list_for_each_entry_rcu(pmu, &pmus, entry) {
5034 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5035 if (cpuctx->unique_pmu != pmu)
5036 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02005037 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005038 if (task_ctx)
5039 goto next;
5040 ctxn = pmu->task_ctx_nr;
5041 if (ctxn < 0)
5042 goto next;
5043 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5044 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02005045 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005046next:
5047 put_cpu_ptr(pmu->pmu_cpu_context);
5048 }
5049
5050 if (task_ctx) {
5051 preempt_disable();
Jiri Olsa67516842013-07-09 18:56:31 +02005052 perf_event_aux_ctx(task_ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02005053 preempt_enable();
5054 }
5055 rcu_read_unlock();
5056}
5057
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005058/*
5059 * task tracking -- fork/exit
5060 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02005061 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005062 */
5063
5064struct perf_task_event {
5065 struct task_struct *task;
5066 struct perf_event_context *task_ctx;
5067
5068 struct {
5069 struct perf_event_header header;
5070
5071 u32 pid;
5072 u32 ppid;
5073 u32 tid;
5074 u32 ptid;
5075 u64 time;
5076 } event_id;
5077};
5078
Jiri Olsa67516842013-07-09 18:56:31 +02005079static int perf_event_task_match(struct perf_event *event)
5080{
Stephane Eranian13d7a242013-08-21 12:10:24 +02005081 return event->attr.comm || event->attr.mmap ||
5082 event->attr.mmap2 || event->attr.mmap_data ||
5083 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02005084}
5085
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005086static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005087 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005088{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005089 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005090 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005091 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005092 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005093 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01005094
Jiri Olsa67516842013-07-09 18:56:31 +02005095 if (!perf_event_task_match(event))
5096 return;
5097
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005098 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005099
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005100 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005101 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02005102 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005103 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005104
5105 task_event->event_id.pid = perf_event_pid(event, task);
5106 task_event->event_id.ppid = perf_event_pid(event, current);
5107
5108 task_event->event_id.tid = perf_event_tid(event, task);
5109 task_event->event_id.ptid = perf_event_tid(event, current);
5110
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005111 perf_output_put(&handle, task_event->event_id);
5112
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005113 perf_event__output_id_sample(event, &handle, &sample);
5114
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005115 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005116out:
5117 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005118}
5119
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005120static void perf_event_task(struct task_struct *task,
5121 struct perf_event_context *task_ctx,
5122 int new)
5123{
5124 struct perf_task_event task_event;
5125
5126 if (!atomic_read(&nr_comm_events) &&
5127 !atomic_read(&nr_mmap_events) &&
5128 !atomic_read(&nr_task_events))
5129 return;
5130
5131 task_event = (struct perf_task_event){
5132 .task = task,
5133 .task_ctx = task_ctx,
5134 .event_id = {
5135 .header = {
5136 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
5137 .misc = 0,
5138 .size = sizeof(task_event.event_id),
5139 },
5140 /* .pid */
5141 /* .ppid */
5142 /* .tid */
5143 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01005144 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005145 },
5146 };
5147
Jiri Olsa67516842013-07-09 18:56:31 +02005148 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005149 &task_event,
5150 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005151}
5152
5153void perf_event_fork(struct task_struct *task)
5154{
5155 perf_event_task(task, NULL, 1);
5156}
5157
5158/*
5159 * comm tracking
5160 */
5161
5162struct perf_comm_event {
5163 struct task_struct *task;
5164 char *comm;
5165 int comm_size;
5166
5167 struct {
5168 struct perf_event_header header;
5169
5170 u32 pid;
5171 u32 tid;
5172 } event_id;
5173};
5174
Jiri Olsa67516842013-07-09 18:56:31 +02005175static int perf_event_comm_match(struct perf_event *event)
5176{
5177 return event->attr.comm;
5178}
5179
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005180static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005181 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005182{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005183 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005184 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005185 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005186 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005187 int ret;
5188
Jiri Olsa67516842013-07-09 18:56:31 +02005189 if (!perf_event_comm_match(event))
5190 return;
5191
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005192 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
5193 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005194 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005195
5196 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005197 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005198
5199 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
5200 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
5201
5202 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02005203 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005204 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005205
5206 perf_event__output_id_sample(event, &handle, &sample);
5207
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005208 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005209out:
5210 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005211}
5212
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005213static void perf_event_comm_event(struct perf_comm_event *comm_event)
5214{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005215 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005216 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005217
5218 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01005219 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005220 size = ALIGN(strlen(comm)+1, sizeof(u64));
5221
5222 comm_event->comm = comm;
5223 comm_event->comm_size = size;
5224
5225 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005226
Jiri Olsa67516842013-07-09 18:56:31 +02005227 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005228 comm_event,
5229 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005230}
5231
Adrian Hunter82b89772014-05-28 11:45:04 +03005232void perf_event_comm(struct task_struct *task, bool exec)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005233{
5234 struct perf_comm_event comm_event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005235
5236 if (!atomic_read(&nr_comm_events))
5237 return;
5238
5239 comm_event = (struct perf_comm_event){
5240 .task = task,
5241 /* .comm */
5242 /* .comm_size */
5243 .event_id = {
5244 .header = {
5245 .type = PERF_RECORD_COMM,
Adrian Hunter82b89772014-05-28 11:45:04 +03005246 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005247 /* .size */
5248 },
5249 /* .pid */
5250 /* .tid */
5251 },
5252 };
5253
5254 perf_event_comm_event(&comm_event);
5255}
5256
5257/*
5258 * mmap tracking
5259 */
5260
5261struct perf_mmap_event {
5262 struct vm_area_struct *vma;
5263
5264 const char *file_name;
5265 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005266 int maj, min;
5267 u64 ino;
5268 u64 ino_generation;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005269 u32 prot, flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005270
5271 struct {
5272 struct perf_event_header header;
5273
5274 u32 pid;
5275 u32 tid;
5276 u64 start;
5277 u64 len;
5278 u64 pgoff;
5279 } event_id;
5280};
5281
Jiri Olsa67516842013-07-09 18:56:31 +02005282static int perf_event_mmap_match(struct perf_event *event,
5283 void *data)
5284{
5285 struct perf_mmap_event *mmap_event = data;
5286 struct vm_area_struct *vma = mmap_event->vma;
5287 int executable = vma->vm_flags & VM_EXEC;
5288
5289 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005290 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005291}
5292
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005293static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005294 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005295{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005296 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005297 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005298 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005299 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005300 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005301
Jiri Olsa67516842013-07-09 18:56:31 +02005302 if (!perf_event_mmap_match(event, data))
5303 return;
5304
Stephane Eranian13d7a242013-08-21 12:10:24 +02005305 if (event->attr.mmap2) {
5306 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5307 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5308 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5309 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005310 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005311 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
5312 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005313 }
5314
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005315 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5316 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005317 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005318 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005319 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005320
5321 mmap_event->event_id.pid = perf_event_pid(event, current);
5322 mmap_event->event_id.tid = perf_event_tid(event, current);
5323
5324 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005325
5326 if (event->attr.mmap2) {
5327 perf_output_put(&handle, mmap_event->maj);
5328 perf_output_put(&handle, mmap_event->min);
5329 perf_output_put(&handle, mmap_event->ino);
5330 perf_output_put(&handle, mmap_event->ino_generation);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005331 perf_output_put(&handle, mmap_event->prot);
5332 perf_output_put(&handle, mmap_event->flags);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005333 }
5334
Frederic Weisbecker76369132011-05-19 19:55:04 +02005335 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005336 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005337
5338 perf_event__output_id_sample(event, &handle, &sample);
5339
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005340 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005341out:
5342 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005343}
5344
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005345static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5346{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005347 struct vm_area_struct *vma = mmap_event->vma;
5348 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005349 int maj = 0, min = 0;
5350 u64 ino = 0, gen = 0;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005351 u32 prot = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005352 unsigned int size;
5353 char tmp[16];
5354 char *buf = NULL;
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005355 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005356
5357 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005358 struct inode *inode;
5359 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005360
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005361 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005362 if (!buf) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005363 name = "//enomem";
5364 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005365 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005366 /*
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005367 * d_path() works from the end of the rb backwards, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005368 * need to add enough zero bytes after the string to handle
5369 * the 64bit alignment we do later.
5370 */
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005371 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005372 if (IS_ERR(name)) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005373 name = "//toolong";
5374 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005375 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005376 inode = file_inode(vma->vm_file);
5377 dev = inode->i_sb->s_dev;
5378 ino = inode->i_ino;
5379 gen = inode->i_generation;
5380 maj = MAJOR(dev);
5381 min = MINOR(dev);
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005382
5383 if (vma->vm_flags & VM_READ)
5384 prot |= PROT_READ;
5385 if (vma->vm_flags & VM_WRITE)
5386 prot |= PROT_WRITE;
5387 if (vma->vm_flags & VM_EXEC)
5388 prot |= PROT_EXEC;
5389
5390 if (vma->vm_flags & VM_MAYSHARE)
5391 flags = MAP_SHARED;
5392 else
5393 flags = MAP_PRIVATE;
5394
5395 if (vma->vm_flags & VM_DENYWRITE)
5396 flags |= MAP_DENYWRITE;
5397 if (vma->vm_flags & VM_MAYEXEC)
5398 flags |= MAP_EXECUTABLE;
5399 if (vma->vm_flags & VM_LOCKED)
5400 flags |= MAP_LOCKED;
5401 if (vma->vm_flags & VM_HUGETLB)
5402 flags |= MAP_HUGETLB;
5403
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005404 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005405 } else {
Jiri Olsafbe26ab2014-07-14 17:57:19 +02005406 if (vma->vm_ops && vma->vm_ops->name) {
5407 name = (char *) vma->vm_ops->name(vma);
5408 if (name)
5409 goto cpy_name;
5410 }
5411
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005412 name = (char *)arch_vma_name(vma);
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005413 if (name)
5414 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005415
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005416 if (vma->vm_start <= vma->vm_mm->start_brk &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005417 vma->vm_end >= vma->vm_mm->brk) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005418 name = "[heap]";
5419 goto cpy_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005420 }
5421 if (vma->vm_start <= vma->vm_mm->start_stack &&
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005422 vma->vm_end >= vma->vm_mm->start_stack) {
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005423 name = "[stack]";
5424 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005425 }
5426
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005427 name = "//anon";
5428 goto cpy_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005429 }
5430
Oleg Nesterovc7e548b2013-10-17 20:24:17 +02005431cpy_name:
5432 strlcpy(tmp, name, sizeof(tmp));
5433 name = tmp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005434got_name:
Peter Zijlstra2c42cfb2013-10-17 00:06:46 +02005435 /*
5436 * Since our buffer works in 8 byte units we need to align our string
5437 * size to a multiple of 8. However, we must guarantee the tail end is
5438 * zero'd out to avoid leaking random bits to userspace.
5439 */
5440 size = strlen(name)+1;
5441 while (!IS_ALIGNED(size, sizeof(u64)))
5442 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005443
5444 mmap_event->file_name = name;
5445 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005446 mmap_event->maj = maj;
5447 mmap_event->min = min;
5448 mmap_event->ino = ino;
5449 mmap_event->ino_generation = gen;
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005450 mmap_event->prot = prot;
5451 mmap_event->flags = flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005452
Stephane Eranian2fe85422013-01-24 16:10:39 +01005453 if (!(vma->vm_flags & VM_EXEC))
5454 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5455
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005456 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5457
Jiri Olsa67516842013-07-09 18:56:31 +02005458 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005459 mmap_event,
5460 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005461
5462 kfree(buf);
5463}
5464
Eric B Munson3af9e852010-05-18 15:30:49 +01005465void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005466{
5467 struct perf_mmap_event mmap_event;
5468
5469 if (!atomic_read(&nr_mmap_events))
5470 return;
5471
5472 mmap_event = (struct perf_mmap_event){
5473 .vma = vma,
5474 /* .file_name */
5475 /* .file_size */
5476 .event_id = {
5477 .header = {
5478 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005479 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005480 /* .size */
5481 },
5482 /* .pid */
5483 /* .tid */
5484 .start = vma->vm_start,
5485 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01005486 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005487 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02005488 /* .maj (attr_mmap2 only) */
5489 /* .min (attr_mmap2 only) */
5490 /* .ino (attr_mmap2 only) */
5491 /* .ino_generation (attr_mmap2 only) */
Peter Zijlstraf972eb62014-05-19 15:13:47 -04005492 /* .prot (attr_mmap2 only) */
5493 /* .flags (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005494 };
5495
5496 perf_event_mmap_event(&mmap_event);
5497}
5498
5499/*
5500 * IRQ throttle logging
5501 */
5502
5503static void perf_log_throttle(struct perf_event *event, int enable)
5504{
5505 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005506 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005507 int ret;
5508
5509 struct {
5510 struct perf_event_header header;
5511 u64 time;
5512 u64 id;
5513 u64 stream_id;
5514 } throttle_event = {
5515 .header = {
5516 .type = PERF_RECORD_THROTTLE,
5517 .misc = 0,
5518 .size = sizeof(throttle_event),
5519 },
5520 .time = perf_clock(),
5521 .id = primary_event_id(event),
5522 .stream_id = event->id,
5523 };
5524
5525 if (enable)
5526 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5527
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005528 perf_event_header__init_id(&throttle_event.header, &sample, event);
5529
5530 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005531 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005532 if (ret)
5533 return;
5534
5535 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005536 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005537 perf_output_end(&handle);
5538}
5539
5540/*
5541 * Generic event overflow handling, sampling.
5542 */
5543
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005544static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005545 int throttle, struct perf_sample_data *data,
5546 struct pt_regs *regs)
5547{
5548 int events = atomic_read(&event->event_limit);
5549 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005550 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005551 int ret = 0;
5552
Peter Zijlstra96398822010-11-24 18:55:29 +01005553 /*
5554 * Non-sampling counters might still use the PMI to fold short
5555 * hardware counters, ignore those.
5556 */
5557 if (unlikely(!is_sampling_event(event)))
5558 return 0;
5559
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005560 seq = __this_cpu_read(perf_throttled_seq);
5561 if (seq != hwc->interrupts_seq) {
5562 hwc->interrupts_seq = seq;
5563 hwc->interrupts = 1;
5564 } else {
5565 hwc->interrupts++;
5566 if (unlikely(throttle
5567 && hwc->interrupts >= max_samples_per_tick)) {
5568 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01005569 hwc->interrupts = MAX_INTERRUPTS;
5570 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02005571 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005572 ret = 1;
5573 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005574 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005575
5576 if (event->attr.freq) {
5577 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01005578 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005579
Peter Zijlstraabd50712010-01-26 18:50:16 +01005580 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005581
Peter Zijlstraabd50712010-01-26 18:50:16 +01005582 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01005583 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005584 }
5585
5586 /*
5587 * XXX event_limit might not quite work as expected on inherited
5588 * events
5589 */
5590
5591 event->pending_kill = POLL_IN;
5592 if (events && atomic_dec_and_test(&event->event_limit)) {
5593 ret = 1;
5594 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005595 event->pending_disable = 1;
5596 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005597 }
5598
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005599 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005600 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005601 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005602 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005603
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005604 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005605 event->pending_wakeup = 1;
5606 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005607 }
5608
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005609 return ret;
5610}
5611
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005612int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005613 struct perf_sample_data *data,
5614 struct pt_regs *regs)
5615{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005616 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005617}
5618
5619/*
5620 * Generic software event infrastructure
5621 */
5622
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005623struct swevent_htable {
5624 struct swevent_hlist *swevent_hlist;
5625 struct mutex hlist_mutex;
5626 int hlist_refcount;
5627
5628 /* Recursion avoidance in each contexts */
5629 int recursion[PERF_NR_CONTEXTS];
Jiri Olsa39af6b12014-04-07 11:04:08 +02005630
5631 /* Keeps track of cpu being initialized/exited */
5632 bool online;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005633};
5634
5635static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5636
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005637/*
5638 * We directly increment event->count and keep a second value in
5639 * event->hw.period_left to count intervals. This period event
5640 * is kept in the range [-sample_period, 0] so that we can use the
5641 * sign as trigger.
5642 */
5643
Jiri Olsaab573842013-05-01 17:25:44 +02005644u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005645{
5646 struct hw_perf_event *hwc = &event->hw;
5647 u64 period = hwc->last_period;
5648 u64 nr, offset;
5649 s64 old, val;
5650
5651 hwc->last_period = hwc->sample_period;
5652
5653again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005654 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005655 if (val < 0)
5656 return 0;
5657
5658 nr = div64_u64(period + val, period);
5659 offset = nr * period;
5660 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005661 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005662 goto again;
5663
5664 return nr;
5665}
5666
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005667static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005668 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005669 struct pt_regs *regs)
5670{
5671 struct hw_perf_event *hwc = &event->hw;
5672 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005673
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005674 if (!overflow)
5675 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005676
5677 if (hwc->interrupts == MAX_INTERRUPTS)
5678 return;
5679
5680 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005681 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005682 data, regs)) {
5683 /*
5684 * We inhibit the overflow from happening when
5685 * hwc->interrupts == MAX_INTERRUPTS.
5686 */
5687 break;
5688 }
5689 throttle = 1;
5690 }
5691}
5692
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005693static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005694 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005695 struct pt_regs *regs)
5696{
5697 struct hw_perf_event *hwc = &event->hw;
5698
Peter Zijlstrae7850592010-05-21 14:43:08 +02005699 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005700
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005701 if (!regs)
5702 return;
5703
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005704 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005705 return;
5706
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005707 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5708 data->period = nr;
5709 return perf_swevent_overflow(event, 1, data, regs);
5710 } else
5711 data->period = event->hw.last_period;
5712
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005713 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005714 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005715
Peter Zijlstrae7850592010-05-21 14:43:08 +02005716 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005717 return;
5718
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005719 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005720}
5721
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005722static int perf_exclude_event(struct perf_event *event,
5723 struct pt_regs *regs)
5724{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005725 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005726 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005727
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005728 if (regs) {
5729 if (event->attr.exclude_user && user_mode(regs))
5730 return 1;
5731
5732 if (event->attr.exclude_kernel && !user_mode(regs))
5733 return 1;
5734 }
5735
5736 return 0;
5737}
5738
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005739static int perf_swevent_match(struct perf_event *event,
5740 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005741 u32 event_id,
5742 struct perf_sample_data *data,
5743 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005744{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005745 if (event->attr.type != type)
5746 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005747
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005748 if (event->attr.config != event_id)
5749 return 0;
5750
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005751 if (perf_exclude_event(event, regs))
5752 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005753
5754 return 1;
5755}
5756
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005757static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005758{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005759 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005760
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005761 return hash_64(val, SWEVENT_HLIST_BITS);
5762}
5763
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005764static inline struct hlist_head *
5765__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005766{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005767 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005768
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005769 return &hlist->heads[hash];
5770}
5771
5772/* For the read side: events when they trigger */
5773static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005774find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005775{
5776 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005777
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005778 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005779 if (!hlist)
5780 return NULL;
5781
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005782 return __find_swevent_head(hlist, type, event_id);
5783}
5784
5785/* For the event head insertion and removal in the hlist */
5786static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005787find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005788{
5789 struct swevent_hlist *hlist;
5790 u32 event_id = event->attr.config;
5791 u64 type = event->attr.type;
5792
5793 /*
5794 * Event scheduling is always serialized against hlist allocation
5795 * and release. Which makes the protected version suitable here.
5796 * The context lock guarantees that.
5797 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005798 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005799 lockdep_is_held(&event->ctx->lock));
5800 if (!hlist)
5801 return NULL;
5802
5803 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005804}
5805
5806static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005807 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005808 struct perf_sample_data *data,
5809 struct pt_regs *regs)
5810{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005811 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005812 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005813 struct hlist_head *head;
5814
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005815 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005816 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005817 if (!head)
5818 goto end;
5819
Sasha Levinb67bfe02013-02-27 17:06:00 -08005820 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005821 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005822 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005823 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005824end:
5825 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005826}
5827
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005828int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005829{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005830 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005831
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005832 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005833}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005834EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005835
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005836inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005837{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005838 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005839
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005840 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005841}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005842
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005843void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005844{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005845 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005846 int rctx;
5847
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005848 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005849 rctx = perf_swevent_get_recursion_context();
5850 if (rctx < 0)
5851 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005852
Robert Richterfd0d0002012-04-02 20:19:08 +02005853 perf_sample_data_init(&data, addr, 0);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005854
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005855 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005856
5857 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005858 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005859}
5860
5861static void perf_swevent_read(struct perf_event *event)
5862{
5863}
5864
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005865static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005866{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005867 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005868 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005869 struct hlist_head *head;
5870
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005871 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005872 hwc->last_period = hwc->sample_period;
5873 perf_swevent_set_period(event);
5874 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005875
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005876 hwc->state = !(flags & PERF_EF_START);
5877
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005878 head = find_swevent_head(swhash, event);
Jiri Olsa39af6b12014-04-07 11:04:08 +02005879 if (!head) {
5880 /*
5881 * We can race with cpu hotplug code. Do not
5882 * WARN if the cpu just got unplugged.
5883 */
5884 WARN_ON_ONCE(swhash->online);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005885 return -EINVAL;
Jiri Olsa39af6b12014-04-07 11:04:08 +02005886 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005887
5888 hlist_add_head_rcu(&event->hlist_entry, head);
5889
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005890 return 0;
5891}
5892
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005893static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005894{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005895 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005896}
5897
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005898static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005899{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005900 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005901}
5902
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005903static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005904{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005905 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005906}
5907
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005908/* Deref the hlist from the update side */
5909static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005910swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005911{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005912 return rcu_dereference_protected(swhash->swevent_hlist,
5913 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005914}
5915
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005916static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005917{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005918 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005919
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005920 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005921 return;
5922
Andreea-Cristina Bernat70691d42014-08-22 16:26:05 +03005923 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08005924 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005925}
5926
5927static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5928{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005929 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005930
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005931 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005932
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005933 if (!--swhash->hlist_refcount)
5934 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005935
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005936 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005937}
5938
5939static void swevent_hlist_put(struct perf_event *event)
5940{
5941 int cpu;
5942
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005943 for_each_possible_cpu(cpu)
5944 swevent_hlist_put_cpu(event, cpu);
5945}
5946
5947static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5948{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005949 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005950 int err = 0;
5951
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005952 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005953
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005954 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005955 struct swevent_hlist *hlist;
5956
5957 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5958 if (!hlist) {
5959 err = -ENOMEM;
5960 goto exit;
5961 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005962 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005963 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005964 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005965exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005966 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005967
5968 return err;
5969}
5970
5971static int swevent_hlist_get(struct perf_event *event)
5972{
5973 int err;
5974 int cpu, failed_cpu;
5975
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005976 get_online_cpus();
5977 for_each_possible_cpu(cpu) {
5978 err = swevent_hlist_get_cpu(event, cpu);
5979 if (err) {
5980 failed_cpu = cpu;
5981 goto fail;
5982 }
5983 }
5984 put_online_cpus();
5985
5986 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005987fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005988 for_each_possible_cpu(cpu) {
5989 if (cpu == failed_cpu)
5990 break;
5991 swevent_hlist_put_cpu(event, cpu);
5992 }
5993
5994 put_online_cpus();
5995 return err;
5996}
5997
Ingo Molnarc5905af2012-02-24 08:31:31 +01005998struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005999
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006000static void sw_perf_event_destroy(struct perf_event *event)
6001{
6002 u64 event_id = event->attr.config;
6003
6004 WARN_ON(event->parent);
6005
Ingo Molnarc5905af2012-02-24 08:31:31 +01006006 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006007 swevent_hlist_put(event);
6008}
6009
6010static int perf_swevent_init(struct perf_event *event)
6011{
Tommi Rantala8176cce2013-04-13 22:49:14 +03006012 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006013
6014 if (event->attr.type != PERF_TYPE_SOFTWARE)
6015 return -ENOENT;
6016
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006017 /*
6018 * no branch sampling for software events
6019 */
6020 if (has_branch_stack(event))
6021 return -EOPNOTSUPP;
6022
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006023 switch (event_id) {
6024 case PERF_COUNT_SW_CPU_CLOCK:
6025 case PERF_COUNT_SW_TASK_CLOCK:
6026 return -ENOENT;
6027
6028 default:
6029 break;
6030 }
6031
Dan Carpenterce677832010-10-24 21:50:42 +02006032 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006033 return -ENOENT;
6034
6035 if (!event->parent) {
6036 int err;
6037
6038 err = swevent_hlist_get(event);
6039 if (err)
6040 return err;
6041
Ingo Molnarc5905af2012-02-24 08:31:31 +01006042 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006043 event->destroy = sw_perf_event_destroy;
6044 }
6045
6046 return 0;
6047}
6048
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006049static int perf_swevent_event_idx(struct perf_event *event)
6050{
6051 return 0;
6052}
6053
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006054static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006055 .task_ctx_nr = perf_sw_context,
6056
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006057 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006058 .add = perf_swevent_add,
6059 .del = perf_swevent_del,
6060 .start = perf_swevent_start,
6061 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006062 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006063
6064 .event_idx = perf_swevent_event_idx,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006065};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006066
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006067#ifdef CONFIG_EVENT_TRACING
6068
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006069static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02006070 struct perf_sample_data *data)
6071{
6072 void *record = data->raw->data;
6073
6074 if (likely(!event->filter) || filter_match_preds(event->filter, record))
6075 return 1;
6076 return 0;
6077}
6078
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006079static int perf_tp_event_match(struct perf_event *event,
6080 struct perf_sample_data *data,
6081 struct pt_regs *regs)
6082{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01006083 if (event->hw.state & PERF_HES_STOPPED)
6084 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02006085 /*
6086 * All tracepoints are from kernel-space.
6087 */
6088 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006089 return 0;
6090
6091 if (!perf_tp_filter_match(event, data))
6092 return 0;
6093
6094 return 1;
6095}
6096
6097void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006098 struct pt_regs *regs, struct hlist_head *head, int rctx,
6099 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006100{
6101 struct perf_sample_data data;
6102 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006103
6104 struct perf_raw_record raw = {
6105 .size = entry_size,
6106 .data = record,
6107 };
6108
Robert Richterfd0d0002012-04-02 20:19:08 +02006109 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006110 data.raw = &raw;
6111
Sasha Levinb67bfe02013-02-27 17:06:00 -08006112 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006113 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006114 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006115 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006116
Andrew Vagine6dab5f2012-07-11 18:14:58 +04006117 /*
6118 * If we got specified a target task, also iterate its context and
6119 * deliver this event there too.
6120 */
6121 if (task && task != current) {
6122 struct perf_event_context *ctx;
6123 struct trace_entry *entry = record;
6124
6125 rcu_read_lock();
6126 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
6127 if (!ctx)
6128 goto unlock;
6129
6130 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
6131 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6132 continue;
6133 if (event->attr.config != entry->type)
6134 continue;
6135 if (perf_tp_event_match(event, &data, regs))
6136 perf_swevent_event(event, count, &data, regs);
6137 }
6138unlock:
6139 rcu_read_unlock();
6140 }
6141
Peter Zijlstraecc55f82010-05-21 15:11:34 +02006142 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006143}
6144EXPORT_SYMBOL_GPL(perf_tp_event);
6145
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006146static void tp_perf_event_destroy(struct perf_event *event)
6147{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006148 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006149}
6150
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006151static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006152{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006153 int err;
6154
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006155 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6156 return -ENOENT;
6157
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006158 /*
6159 * no branch sampling for tracepoint events
6160 */
6161 if (has_branch_stack(event))
6162 return -EOPNOTSUPP;
6163
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02006164 err = perf_trace_init(event);
6165 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006166 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006167
6168 event->destroy = tp_perf_event_destroy;
6169
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006170 return 0;
6171}
6172
6173static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006174 .task_ctx_nr = perf_sw_context,
6175
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006176 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006177 .add = perf_trace_add,
6178 .del = perf_trace_del,
6179 .start = perf_swevent_start,
6180 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006181 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006182
6183 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006184};
6185
6186static inline void perf_tp_register(void)
6187{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006188 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006189}
Li Zefan6fb29152009-10-15 11:21:42 +08006190
6191static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6192{
6193 char *filter_str;
6194 int ret;
6195
6196 if (event->attr.type != PERF_TYPE_TRACEPOINT)
6197 return -EINVAL;
6198
6199 filter_str = strndup_user(arg, PAGE_SIZE);
6200 if (IS_ERR(filter_str))
6201 return PTR_ERR(filter_str);
6202
6203 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
6204
6205 kfree(filter_str);
6206 return ret;
6207}
6208
6209static void perf_event_free_filter(struct perf_event *event)
6210{
6211 ftrace_profile_free_filter(event);
6212}
6213
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006214#else
Li Zefan6fb29152009-10-15 11:21:42 +08006215
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006216static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006217{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006218}
Li Zefan6fb29152009-10-15 11:21:42 +08006219
6220static int perf_event_set_filter(struct perf_event *event, void __user *arg)
6221{
6222 return -ENOENT;
6223}
6224
6225static void perf_event_free_filter(struct perf_event *event)
6226{
6227}
6228
Li Zefan07b139c2009-12-21 14:27:35 +08006229#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006230
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006231#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006232void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006233{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006234 struct perf_sample_data sample;
6235 struct pt_regs *regs = data;
6236
Robert Richterfd0d0002012-04-02 20:19:08 +02006237 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01006238
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006239 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02006240 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02006241}
6242#endif
6243
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006244/*
6245 * hrtimer based swevent callback
6246 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006247
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006248static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006249{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006250 enum hrtimer_restart ret = HRTIMER_RESTART;
6251 struct perf_sample_data data;
6252 struct pt_regs *regs;
6253 struct perf_event *event;
6254 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006255
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006256 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006257
6258 if (event->state != PERF_EVENT_STATE_ACTIVE)
6259 return HRTIMER_NORESTART;
6260
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006261 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006262
Robert Richterfd0d0002012-04-02 20:19:08 +02006263 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006264 regs = get_irq_regs();
6265
6266 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08006267 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02006268 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006269 ret = HRTIMER_NORESTART;
6270 }
6271
6272 period = max_t(u64, 10000, event->hw.sample_period);
6273 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
6274
6275 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006276}
6277
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006278static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006279{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006280 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006281 s64 period;
6282
6283 if (!is_sampling_event(event))
6284 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006285
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006286 period = local64_read(&hwc->period_left);
6287 if (period) {
6288 if (period < 0)
6289 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006290
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006291 local64_set(&hwc->period_left, 0);
6292 } else {
6293 period = max_t(u64, 10000, hwc->sample_period);
6294 }
6295 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006296 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006297 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006298}
6299
6300static void perf_swevent_cancel_hrtimer(struct perf_event *event)
6301{
6302 struct hw_perf_event *hwc = &event->hw;
6303
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006304 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006305 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006306 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006307
6308 hrtimer_cancel(&hwc->hrtimer);
6309 }
6310}
6311
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006312static void perf_swevent_init_hrtimer(struct perf_event *event)
6313{
6314 struct hw_perf_event *hwc = &event->hw;
6315
6316 if (!is_sampling_event(event))
6317 return;
6318
6319 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6320 hwc->hrtimer.function = perf_swevent_hrtimer;
6321
6322 /*
6323 * Since hrtimers have a fixed rate, we can do a static freq->period
6324 * mapping and avoid the whole period adjust feedback stuff.
6325 */
6326 if (event->attr.freq) {
6327 long freq = event->attr.sample_freq;
6328
6329 event->attr.sample_period = NSEC_PER_SEC / freq;
6330 hwc->sample_period = event->attr.sample_period;
6331 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09006332 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006333 event->attr.freq = 0;
6334 }
6335}
6336
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006337/*
6338 * Software event: cpu wall time clock
6339 */
6340
6341static void cpu_clock_event_update(struct perf_event *event)
6342{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006343 s64 prev;
6344 u64 now;
6345
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006346 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006347 prev = local64_xchg(&event->hw.prev_count, now);
6348 local64_add(now - prev, &event->count);
6349}
6350
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006351static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006352{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006353 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006354 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006355}
6356
6357static void cpu_clock_event_stop(struct perf_event *event, int flags)
6358{
6359 perf_swevent_cancel_hrtimer(event);
6360 cpu_clock_event_update(event);
6361}
6362
6363static int cpu_clock_event_add(struct perf_event *event, int flags)
6364{
6365 if (flags & PERF_EF_START)
6366 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006367
6368 return 0;
6369}
6370
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006371static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006372{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006373 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006374}
6375
6376static void cpu_clock_event_read(struct perf_event *event)
6377{
6378 cpu_clock_event_update(event);
6379}
6380
6381static int cpu_clock_event_init(struct perf_event *event)
6382{
6383 if (event->attr.type != PERF_TYPE_SOFTWARE)
6384 return -ENOENT;
6385
6386 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
6387 return -ENOENT;
6388
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006389 /*
6390 * no branch sampling for software events
6391 */
6392 if (has_branch_stack(event))
6393 return -EOPNOTSUPP;
6394
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006395 perf_swevent_init_hrtimer(event);
6396
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006397 return 0;
6398}
6399
6400static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006401 .task_ctx_nr = perf_sw_context,
6402
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006403 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006404 .add = cpu_clock_event_add,
6405 .del = cpu_clock_event_del,
6406 .start = cpu_clock_event_start,
6407 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006408 .read = cpu_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006409
6410 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006411};
6412
6413/*
6414 * Software event: task time clock
6415 */
6416
6417static void task_clock_event_update(struct perf_event *event, u64 now)
6418{
6419 u64 prev;
6420 s64 delta;
6421
6422 prev = local64_xchg(&event->hw.prev_count, now);
6423 delta = now - prev;
6424 local64_add(delta, &event->count);
6425}
6426
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006427static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006428{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006429 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006430 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006431}
6432
6433static void task_clock_event_stop(struct perf_event *event, int flags)
6434{
6435 perf_swevent_cancel_hrtimer(event);
6436 task_clock_event_update(event, event->ctx->time);
6437}
6438
6439static int task_clock_event_add(struct perf_event *event, int flags)
6440{
6441 if (flags & PERF_EF_START)
6442 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006443
6444 return 0;
6445}
6446
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006447static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006448{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006449 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006450}
6451
6452static void task_clock_event_read(struct perf_event *event)
6453{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01006454 u64 now = perf_clock();
6455 u64 delta = now - event->ctx->timestamp;
6456 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006457
6458 task_clock_event_update(event, time);
6459}
6460
6461static int task_clock_event_init(struct perf_event *event)
6462{
6463 if (event->attr.type != PERF_TYPE_SOFTWARE)
6464 return -ENOENT;
6465
6466 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6467 return -ENOENT;
6468
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006469 /*
6470 * no branch sampling for software events
6471 */
6472 if (has_branch_stack(event))
6473 return -EOPNOTSUPP;
6474
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006475 perf_swevent_init_hrtimer(event);
6476
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006477 return 0;
6478}
6479
6480static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006481 .task_ctx_nr = perf_sw_context,
6482
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006483 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006484 .add = task_clock_event_add,
6485 .del = task_clock_event_del,
6486 .start = task_clock_event_start,
6487 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006488 .read = task_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006489
6490 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006491};
6492
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006493static void perf_pmu_nop_void(struct pmu *pmu)
6494{
6495}
6496
6497static int perf_pmu_nop_int(struct pmu *pmu)
6498{
6499 return 0;
6500}
6501
6502static void perf_pmu_start_txn(struct pmu *pmu)
6503{
6504 perf_pmu_disable(pmu);
6505}
6506
6507static int perf_pmu_commit_txn(struct pmu *pmu)
6508{
6509 perf_pmu_enable(pmu);
6510 return 0;
6511}
6512
6513static void perf_pmu_cancel_txn(struct pmu *pmu)
6514{
6515 perf_pmu_enable(pmu);
6516}
6517
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006518static int perf_event_idx_default(struct perf_event *event)
6519{
6520 return event->hw.idx + 1;
6521}
6522
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006523/*
6524 * Ensures all contexts with the same task_ctx_nr have the same
6525 * pmu_cpu_context too.
6526 */
Mark Rutland9e317042014-02-10 17:44:18 +00006527static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006528{
6529 struct pmu *pmu;
6530
6531 if (ctxn < 0)
6532 return NULL;
6533
6534 list_for_each_entry(pmu, &pmus, entry) {
6535 if (pmu->task_ctx_nr == ctxn)
6536 return pmu->pmu_cpu_context;
6537 }
6538
6539 return NULL;
6540}
6541
Peter Zijlstra51676952010-12-07 14:18:20 +01006542static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006543{
Peter Zijlstra51676952010-12-07 14:18:20 +01006544 int cpu;
6545
6546 for_each_possible_cpu(cpu) {
6547 struct perf_cpu_context *cpuctx;
6548
6549 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6550
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006551 if (cpuctx->unique_pmu == old_pmu)
6552 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01006553 }
6554}
6555
6556static void free_pmu_context(struct pmu *pmu)
6557{
6558 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006559
6560 mutex_lock(&pmus_lock);
6561 /*
6562 * Like a real lame refcount.
6563 */
Peter Zijlstra51676952010-12-07 14:18:20 +01006564 list_for_each_entry(i, &pmus, entry) {
6565 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6566 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006567 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01006568 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006569 }
6570
Peter Zijlstra51676952010-12-07 14:18:20 +01006571 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006572out:
6573 mutex_unlock(&pmus_lock);
6574}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006575static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006576
Peter Zijlstraabe43402010-11-17 23:17:37 +01006577static ssize_t
6578type_show(struct device *dev, struct device_attribute *attr, char *page)
6579{
6580 struct pmu *pmu = dev_get_drvdata(dev);
6581
6582 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6583}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006584static DEVICE_ATTR_RO(type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006585
Stephane Eranian62b85632013-04-03 14:21:34 +02006586static ssize_t
6587perf_event_mux_interval_ms_show(struct device *dev,
6588 struct device_attribute *attr,
6589 char *page)
6590{
6591 struct pmu *pmu = dev_get_drvdata(dev);
6592
6593 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6594}
6595
6596static ssize_t
6597perf_event_mux_interval_ms_store(struct device *dev,
6598 struct device_attribute *attr,
6599 const char *buf, size_t count)
6600{
6601 struct pmu *pmu = dev_get_drvdata(dev);
6602 int timer, cpu, ret;
6603
6604 ret = kstrtoint(buf, 0, &timer);
6605 if (ret)
6606 return ret;
6607
6608 if (timer < 1)
6609 return -EINVAL;
6610
6611 /* same value, noting to do */
6612 if (timer == pmu->hrtimer_interval_ms)
6613 return count;
6614
6615 pmu->hrtimer_interval_ms = timer;
6616
6617 /* update all cpuctx for this PMU */
6618 for_each_possible_cpu(cpu) {
6619 struct perf_cpu_context *cpuctx;
6620 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6621 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6622
6623 if (hrtimer_active(&cpuctx->hrtimer))
6624 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6625 }
6626
6627 return count;
6628}
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006629static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
Stephane Eranian62b85632013-04-03 14:21:34 +02006630
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006631static struct attribute *pmu_dev_attrs[] = {
6632 &dev_attr_type.attr,
6633 &dev_attr_perf_event_mux_interval_ms.attr,
6634 NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006635};
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006636ATTRIBUTE_GROUPS(pmu_dev);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006637
6638static int pmu_bus_running;
6639static struct bus_type pmu_bus = {
6640 .name = "event_source",
Greg Kroah-Hartman90826ca2013-08-23 14:24:40 -07006641 .dev_groups = pmu_dev_groups,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006642};
6643
6644static void pmu_dev_release(struct device *dev)
6645{
6646 kfree(dev);
6647}
6648
6649static int pmu_dev_alloc(struct pmu *pmu)
6650{
6651 int ret = -ENOMEM;
6652
6653 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6654 if (!pmu->dev)
6655 goto out;
6656
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01006657 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01006658 device_initialize(pmu->dev);
6659 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6660 if (ret)
6661 goto free_dev;
6662
6663 dev_set_drvdata(pmu->dev, pmu);
6664 pmu->dev->bus = &pmu_bus;
6665 pmu->dev->release = pmu_dev_release;
6666 ret = device_add(pmu->dev);
6667 if (ret)
6668 goto free_dev;
6669
6670out:
6671 return ret;
6672
6673free_dev:
6674 put_device(pmu->dev);
6675 goto out;
6676}
6677
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006678static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006679static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006680
Mischa Jonker03d8e802013-06-04 11:45:48 +02006681int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006682{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006683 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006684
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006685 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006686 ret = -ENOMEM;
6687 pmu->pmu_disable_count = alloc_percpu(int);
6688 if (!pmu->pmu_disable_count)
6689 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006690
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006691 pmu->type = -1;
6692 if (!name)
6693 goto skip_type;
6694 pmu->name = name;
6695
6696 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08006697 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6698 if (type < 0) {
6699 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006700 goto free_pdc;
6701 }
6702 }
6703 pmu->type = type;
6704
Peter Zijlstraabe43402010-11-17 23:17:37 +01006705 if (pmu_bus_running) {
6706 ret = pmu_dev_alloc(pmu);
6707 if (ret)
6708 goto free_idr;
6709 }
6710
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006711skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006712 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6713 if (pmu->pmu_cpu_context)
6714 goto got_cpu_context;
6715
Wei Yongjunc4814202013-04-12 11:05:54 +08006716 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006717 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6718 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006719 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006720
6721 for_each_possible_cpu(cpu) {
6722 struct perf_cpu_context *cpuctx;
6723
6724 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006725 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006726 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006727 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006728 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006729 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02006730
6731 __perf_cpu_hrtimer_init(cpuctx, cpu);
6732
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006733 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006734 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006735 }
6736
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006737got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006738 if (!pmu->start_txn) {
6739 if (pmu->pmu_enable) {
6740 /*
6741 * If we have pmu_enable/pmu_disable calls, install
6742 * transaction stubs that use that to try and batch
6743 * hardware accesses.
6744 */
6745 pmu->start_txn = perf_pmu_start_txn;
6746 pmu->commit_txn = perf_pmu_commit_txn;
6747 pmu->cancel_txn = perf_pmu_cancel_txn;
6748 } else {
6749 pmu->start_txn = perf_pmu_nop_void;
6750 pmu->commit_txn = perf_pmu_nop_int;
6751 pmu->cancel_txn = perf_pmu_nop_void;
6752 }
6753 }
6754
6755 if (!pmu->pmu_enable) {
6756 pmu->pmu_enable = perf_pmu_nop_void;
6757 pmu->pmu_disable = perf_pmu_nop_void;
6758 }
6759
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006760 if (!pmu->event_idx)
6761 pmu->event_idx = perf_event_idx_default;
6762
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006763 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006764 ret = 0;
6765unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006766 mutex_unlock(&pmus_lock);
6767
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006768 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006769
Peter Zijlstraabe43402010-11-17 23:17:37 +01006770free_dev:
6771 device_del(pmu->dev);
6772 put_device(pmu->dev);
6773
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006774free_idr:
6775 if (pmu->type >= PERF_TYPE_MAX)
6776 idr_remove(&pmu_idr, pmu->type);
6777
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006778free_pdc:
6779 free_percpu(pmu->pmu_disable_count);
6780 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006781}
Yan, Zhengc464c762014-03-18 16:56:41 +08006782EXPORT_SYMBOL_GPL(perf_pmu_register);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006783
6784void perf_pmu_unregister(struct pmu *pmu)
6785{
6786 mutex_lock(&pmus_lock);
6787 list_del_rcu(&pmu->entry);
6788 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006789
6790 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006791 * We dereference the pmu list under both SRCU and regular RCU, so
6792 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006793 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006794 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006795 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006796
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006797 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006798 if (pmu->type >= PERF_TYPE_MAX)
6799 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006800 device_del(pmu->dev);
6801 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006802 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006803}
Yan, Zhengc464c762014-03-18 16:56:41 +08006804EXPORT_SYMBOL_GPL(perf_pmu_unregister);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006805
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006806struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006807{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006808 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006809 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08006810 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006811
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006812 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006813
6814 rcu_read_lock();
6815 pmu = idr_find(&pmu_idr, event->attr.type);
6816 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08006817 if (pmu) {
Yan, Zhengc464c762014-03-18 16:56:41 +08006818 if (!try_module_get(pmu->module)) {
6819 pmu = ERR_PTR(-ENODEV);
6820 goto unlock;
6821 }
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006822 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006823 ret = pmu->event_init(event);
6824 if (ret)
6825 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006826 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08006827 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006828
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006829 list_for_each_entry_rcu(pmu, &pmus, entry) {
Yan, Zhengc464c762014-03-18 16:56:41 +08006830 if (!try_module_get(pmu->module)) {
6831 pmu = ERR_PTR(-ENODEV);
6832 goto unlock;
6833 }
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006834 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006835 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006836 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006837 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006838
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006839 if (ret != -ENOENT) {
6840 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006841 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006842 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006843 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006844 pmu = ERR_PTR(-ENOENT);
6845unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006846 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006847
6848 return pmu;
6849}
6850
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006851static void account_event_cpu(struct perf_event *event, int cpu)
6852{
6853 if (event->parent)
6854 return;
6855
6856 if (has_branch_stack(event)) {
6857 if (!(event->attach_state & PERF_ATTACH_TASK))
6858 atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
6859 }
6860 if (is_cgroup_event(event))
6861 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
6862}
6863
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006864static void account_event(struct perf_event *event)
6865{
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006866 if (event->parent)
6867 return;
6868
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006869 if (event->attach_state & PERF_ATTACH_TASK)
6870 static_key_slow_inc(&perf_sched_events.key);
6871 if (event->attr.mmap || event->attr.mmap_data)
6872 atomic_inc(&nr_mmap_events);
6873 if (event->attr.comm)
6874 atomic_inc(&nr_comm_events);
6875 if (event->attr.task)
6876 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02006877 if (event->attr.freq) {
6878 if (atomic_inc_return(&nr_freq_events) == 1)
6879 tick_nohz_full_kick_all();
6880 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006881 if (has_branch_stack(event))
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006882 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006883 if (is_cgroup_event(event))
6884 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006885
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006886 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006887}
6888
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006889/*
6890 * Allocate and initialize a event structure
6891 */
6892static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006893perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006894 struct task_struct *task,
6895 struct perf_event *group_leader,
6896 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006897 perf_overflow_handler_t overflow_handler,
6898 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006899{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006900 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006901 struct perf_event *event;
6902 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006903 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006904
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006905 if ((unsigned)cpu >= nr_cpu_ids) {
6906 if (!task || cpu != -1)
6907 return ERR_PTR(-EINVAL);
6908 }
6909
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006910 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006911 if (!event)
6912 return ERR_PTR(-ENOMEM);
6913
6914 /*
6915 * Single events are their own group leaders, with an
6916 * empty sibling list:
6917 */
6918 if (!group_leader)
6919 group_leader = event;
6920
6921 mutex_init(&event->child_mutex);
6922 INIT_LIST_HEAD(&event->child_list);
6923
6924 INIT_LIST_HEAD(&event->group_entry);
6925 INIT_LIST_HEAD(&event->event_entry);
6926 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006927 INIT_LIST_HEAD(&event->rb_entry);
Stephane Eranian71ad88e2013-11-12 17:58:48 +01006928 INIT_LIST_HEAD(&event->active_entry);
Stephane Eranianf3ae75d2014-01-08 11:15:52 +01006929 INIT_HLIST_NODE(&event->hlist_entry);
6930
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006931
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006932 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006933 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006934
6935 mutex_init(&event->mmap_mutex);
6936
Al Viroa6fa9412012-08-20 14:59:25 +01006937 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006938 event->cpu = cpu;
6939 event->attr = *attr;
6940 event->group_leader = group_leader;
6941 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006942 event->oncpu = -1;
6943
6944 event->parent = parent_event;
6945
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08006946 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006947 event->id = atomic64_inc_return(&perf_event_id);
6948
6949 event->state = PERF_EVENT_STATE_INACTIVE;
6950
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006951 if (task) {
6952 event->attach_state = PERF_ATTACH_TASK;
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006953
6954 if (attr->type == PERF_TYPE_TRACEPOINT)
6955 event->hw.tp_target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006956#ifdef CONFIG_HAVE_HW_BREAKPOINT
6957 /*
6958 * hw_breakpoint is a bit difficult here..
6959 */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006960 else if (attr->type == PERF_TYPE_BREAKPOINT)
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006961 event->hw.bp_target = task;
6962#endif
6963 }
6964
Avi Kivity4dc0da82011-06-29 18:42:35 +03006965 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006966 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006967 context = parent_event->overflow_handler_context;
6968 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006969
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006970 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006971 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006972
Jiri Olsa0231bb52013-02-01 11:23:45 +01006973 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006974
6975 pmu = NULL;
6976
6977 hwc = &event->hw;
6978 hwc->sample_period = attr->sample_period;
6979 if (attr->freq && attr->sample_freq)
6980 hwc->sample_period = 1;
6981 hwc->last_period = hwc->sample_period;
6982
Peter Zijlstrae7850592010-05-21 14:43:08 +02006983 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006984
6985 /*
6986 * we currently do not support PERF_FORMAT_GROUP on inherited events
6987 */
6988 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006989 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006990
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006991 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006992 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006993 goto err_ns;
6994 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006995 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006996 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006997 }
6998
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006999 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02007000 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
7001 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007002 if (err)
7003 goto err_pmu;
Stephane Eraniand010b332012-02-09 23:21:00 +01007004 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007005 }
7006
7007 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007008
7009err_pmu:
7010 if (event->destroy)
7011 event->destroy(event);
Yan, Zhengc464c762014-03-18 16:56:41 +08007012 module_put(pmu->module);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02007013err_ns:
7014 if (event->ns)
7015 put_pid_ns(event->ns);
7016 kfree(event);
7017
7018 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007019}
7020
7021static int perf_copy_attr(struct perf_event_attr __user *uattr,
7022 struct perf_event_attr *attr)
7023{
7024 u32 size;
7025 int ret;
7026
7027 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
7028 return -EFAULT;
7029
7030 /*
7031 * zero the full structure, so that a short copy will be nice.
7032 */
7033 memset(attr, 0, sizeof(*attr));
7034
7035 ret = get_user(size, &uattr->size);
7036 if (ret)
7037 return ret;
7038
7039 if (size > PAGE_SIZE) /* silly large */
7040 goto err_size;
7041
7042 if (!size) /* abi compat */
7043 size = PERF_ATTR_SIZE_VER0;
7044
7045 if (size < PERF_ATTR_SIZE_VER0)
7046 goto err_size;
7047
7048 /*
7049 * If we're handed a bigger struct than we know of,
7050 * ensure all the unknown bits are 0 - i.e. new
7051 * user-space does not rely on any kernel feature
7052 * extensions we dont know about yet.
7053 */
7054 if (size > sizeof(*attr)) {
7055 unsigned char __user *addr;
7056 unsigned char __user *end;
7057 unsigned char val;
7058
7059 addr = (void __user *)uattr + sizeof(*attr);
7060 end = (void __user *)uattr + size;
7061
7062 for (; addr < end; addr++) {
7063 ret = get_user(val, addr);
7064 if (ret)
7065 return ret;
7066 if (val)
7067 goto err_size;
7068 }
7069 size = sizeof(*attr);
7070 }
7071
7072 ret = copy_from_user(attr, uattr, size);
7073 if (ret)
7074 return -EFAULT;
7075
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05307076 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007077 return -EINVAL;
7078
7079 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
7080 return -EINVAL;
7081
7082 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
7083 return -EINVAL;
7084
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007085 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
7086 u64 mask = attr->branch_sample_type;
7087
7088 /* only using defined bits */
7089 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
7090 return -EINVAL;
7091
7092 /* at least one branch bit must be set */
7093 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
7094 return -EINVAL;
7095
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007096 /* propagate priv level, when not set for branch */
7097 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
7098
7099 /* exclude_kernel checked on syscall entry */
7100 if (!attr->exclude_kernel)
7101 mask |= PERF_SAMPLE_BRANCH_KERNEL;
7102
7103 if (!attr->exclude_user)
7104 mask |= PERF_SAMPLE_BRANCH_USER;
7105
7106 if (!attr->exclude_hv)
7107 mask |= PERF_SAMPLE_BRANCH_HV;
7108 /*
7109 * adjust user setting (for HW filter setup)
7110 */
7111 attr->branch_sample_type = mask;
7112 }
Stephane Eraniane7122092013-06-06 11:02:04 +02007113 /* privileged levels capture (kernel, hv): check permissions */
7114 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02007115 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7116 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01007117 }
Jiri Olsa40189942012-08-07 15:20:37 +02007118
Jiri Olsac5ebced2012-08-07 15:20:40 +02007119 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02007120 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02007121 if (ret)
7122 return ret;
7123 }
7124
7125 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
7126 if (!arch_perf_have_user_stack_dump())
7127 return -ENOSYS;
7128
7129 /*
7130 * We have __u32 type for the size, but so far
7131 * we can only use __u16 as maximum due to the
7132 * __u16 sample size limit.
7133 */
7134 if (attr->sample_stack_user >= USHRT_MAX)
7135 ret = -EINVAL;
7136 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
7137 ret = -EINVAL;
7138 }
Jiri Olsa40189942012-08-07 15:20:37 +02007139
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007140out:
7141 return ret;
7142
7143err_size:
7144 put_user(sizeof(*attr), &uattr->size);
7145 ret = -E2BIG;
7146 goto out;
7147}
7148
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007149static int
7150perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007151{
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007152 struct ring_buffer *rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007153 int ret = -EINVAL;
7154
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007155 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007156 goto set;
7157
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007158 /* don't allow circular references */
7159 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007160 goto out;
7161
Peter Zijlstra0f139302010-05-20 14:35:15 +02007162 /*
7163 * Don't allow cross-cpu buffers
7164 */
7165 if (output_event->cpu != event->cpu)
7166 goto out;
7167
7168 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02007169 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02007170 */
7171 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
7172 goto out;
7173
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007174set:
7175 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007176 /* Can't redirect output if we've got an active mmap() */
7177 if (atomic_read(&event->mmap_count))
7178 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007179
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007180 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02007181 /* get the rb we want to redirect to */
7182 rb = ring_buffer_get(output_event);
7183 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007184 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007185 }
7186
Peter Zijlstrab69cf532014-03-14 10:50:33 +01007187 ring_buffer_attach(event, rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02007188
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007189 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007190unlock:
7191 mutex_unlock(&event->mmap_mutex);
7192
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007193out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007194 return ret;
7195}
7196
7197/**
7198 * sys_perf_event_open - open a performance event, associate it to a task/cpu
7199 *
7200 * @attr_uptr: event_id type attributes for monitoring/sampling
7201 * @pid: target pid
7202 * @cpu: target cpu
7203 * @group_fd: group leader event fd
7204 */
7205SYSCALL_DEFINE5(perf_event_open,
7206 struct perf_event_attr __user *, attr_uptr,
7207 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
7208{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007209 struct perf_event *group_leader = NULL, *output_event = NULL;
7210 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007211 struct perf_event_attr attr;
7212 struct perf_event_context *ctx;
7213 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04007214 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07007215 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007216 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04007217 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007218 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007219 int err;
Yann Droneauda21b0b32014-01-05 21:36:33 +01007220 int f_flags = O_RDWR;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007221
7222 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02007223 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007224 return -EINVAL;
7225
7226 err = perf_copy_attr(attr_uptr, &attr);
7227 if (err)
7228 return err;
7229
7230 if (!attr.exclude_kernel) {
7231 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
7232 return -EACCES;
7233 }
7234
7235 if (attr.freq) {
7236 if (attr.sample_freq > sysctl_perf_event_sample_rate)
7237 return -EINVAL;
Peter Zijlstra0819b2e2014-05-15 20:23:48 +02007238 } else {
7239 if (attr.sample_period & (1ULL << 63))
7240 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007241 }
7242
Stephane Eraniane5d13672011-02-14 11:20:01 +02007243 /*
7244 * In cgroup mode, the pid argument is used to pass the fd
7245 * opened to the cgroup directory in cgroupfs. The cpu argument
7246 * designates the cpu on which to monitor threads from that
7247 * cgroup.
7248 */
7249 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
7250 return -EINVAL;
7251
Yann Droneauda21b0b32014-01-05 21:36:33 +01007252 if (flags & PERF_FLAG_FD_CLOEXEC)
7253 f_flags |= O_CLOEXEC;
7254
7255 event_fd = get_unused_fd_flags(f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007256 if (event_fd < 0)
7257 return event_fd;
7258
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007259 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04007260 err = perf_fget_light(group_fd, &group);
7261 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007262 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04007263 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007264 if (flags & PERF_FLAG_FD_OUTPUT)
7265 output_event = group_leader;
7266 if (flags & PERF_FLAG_FD_NO_GROUP)
7267 group_leader = NULL;
7268 }
7269
Stephane Eraniane5d13672011-02-14 11:20:01 +02007270 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007271 task = find_lively_task_by_vpid(pid);
7272 if (IS_ERR(task)) {
7273 err = PTR_ERR(task);
7274 goto err_group_fd;
7275 }
7276 }
7277
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007278 if (task && group_leader &&
7279 group_leader->attr.inherit != attr.inherit) {
7280 err = -EINVAL;
7281 goto err_task;
7282 }
7283
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007284 get_online_cpus();
7285
Avi Kivity4dc0da82011-06-29 18:42:35 +03007286 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
7287 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007288 if (IS_ERR(event)) {
7289 err = PTR_ERR(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007290 goto err_cpus;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007291 }
7292
Stephane Eraniane5d13672011-02-14 11:20:01 +02007293 if (flags & PERF_FLAG_PID_CGROUP) {
7294 err = perf_cgroup_connect(pid, event, &attr, group_leader);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007295 if (err) {
7296 __free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007297 goto err_cpus;
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007298 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02007299 }
7300
Vince Weaver53b25332014-05-16 17:12:12 -04007301 if (is_sampling_event(event)) {
7302 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
7303 err = -ENOTSUPP;
7304 goto err_alloc;
7305 }
7306 }
7307
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007308 account_event(event);
7309
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007310 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007311 * Special case software events and allow them to be part of
7312 * any hardware group.
7313 */
7314 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007315
7316 if (group_leader &&
7317 (is_software_event(event) != is_software_event(group_leader))) {
7318 if (is_software_event(event)) {
7319 /*
7320 * If event and group_leader are not both a software
7321 * event, and event is, then group leader is not.
7322 *
7323 * Allow the addition of software events to !software
7324 * groups, this is safe because software events never
7325 * fail to schedule.
7326 */
7327 pmu = group_leader->pmu;
7328 } else if (is_software_event(group_leader) &&
7329 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
7330 /*
7331 * In case the group is a pure software group, and we
7332 * try to add a hardware event, move the whole group to
7333 * the hardware context.
7334 */
7335 move_group = 1;
7336 }
7337 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007338
7339 /*
7340 * Get the target context (task or percpu):
7341 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007342 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007343 if (IS_ERR(ctx)) {
7344 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007345 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007346 }
7347
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02007348 if (task) {
7349 put_task_struct(task);
7350 task = NULL;
7351 }
7352
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007353 /*
7354 * Look up the group leader (we will attach this event to it):
7355 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007356 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007357 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007358
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007359 /*
7360 * Do not allow a recursive hierarchy (this new sibling
7361 * becoming part of another group-sibling):
7362 */
7363 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007364 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007365 /*
7366 * Do not allow to attach to a group in a different
7367 * task or CPU context:
7368 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007369 if (move_group) {
7370 if (group_leader->ctx->type != ctx->type)
7371 goto err_context;
7372 } else {
7373 if (group_leader->ctx != ctx)
7374 goto err_context;
7375 }
7376
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007377 /*
7378 * Only a group leader can be exclusive or pinned
7379 */
7380 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007381 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007382 }
7383
7384 if (output_event) {
7385 err = perf_event_set_output(event, output_event);
7386 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007387 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007388 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007389
Yann Droneauda21b0b32014-01-05 21:36:33 +01007390 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
7391 f_flags);
Al Viroea635c62010-05-26 17:40:29 -04007392 if (IS_ERR(event_file)) {
7393 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007394 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04007395 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007396
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007397 if (move_group) {
7398 struct perf_event_context *gctx = group_leader->ctx;
7399
7400 mutex_lock(&gctx->mutex);
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007401 perf_remove_from_context(group_leader, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007402
7403 /*
7404 * Removing from the context ends up with disabled
7405 * event. What we want here is event in the initial
7406 * startup state, ready to be add into new context.
7407 */
7408 perf_event__state_init(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007409 list_for_each_entry(sibling, &group_leader->sibling_list,
7410 group_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007411 perf_remove_from_context(sibling, false);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007412 perf_event__state_init(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007413 put_ctx(gctx);
7414 }
7415 mutex_unlock(&gctx->mutex);
7416 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007417 }
7418
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007419 WARN_ON_ONCE(ctx->parent_ctx);
7420 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007421
7422 if (move_group) {
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007423 synchronize_rcu();
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007424 perf_install_in_context(ctx, group_leader, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007425 get_ctx(ctx);
7426 list_for_each_entry(sibling, &group_leader->sibling_list,
7427 group_entry) {
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007428 perf_install_in_context(ctx, sibling, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007429 get_ctx(ctx);
7430 }
7431 }
7432
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007433 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007434 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007435 mutex_unlock(&ctx->mutex);
7436
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007437 put_online_cpus();
7438
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007439 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01007440
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007441 mutex_lock(&current->perf_event_mutex);
7442 list_add_tail(&event->owner_entry, &current->perf_event_list);
7443 mutex_unlock(&current->perf_event_mutex);
7444
Peter Zijlstra8a495422010-05-27 15:47:49 +02007445 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007446 * Precalculate sample_data sizes
7447 */
7448 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007449 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007450
7451 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02007452 * Drop the reference on the group_event after placing the
7453 * new event on the sibling_list. This ensures destruction
7454 * of the group leader will find the pointer to itself in
7455 * perf_group_detach().
7456 */
Al Viro2903ff02012-08-28 12:52:22 -04007457 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007458 fd_install(event_fd, event_file);
7459 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007460
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007461err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007462 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04007463 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007464err_alloc:
7465 free_event(event);
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007466err_cpus:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007467 put_online_cpus();
Peter Zijlstra1f4ee502014-05-06 09:59:34 +02007468err_task:
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007469 if (task)
7470 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007471err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04007472 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007473err_fd:
7474 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007475 return err;
7476}
7477
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007478/**
7479 * perf_event_create_kernel_counter
7480 *
7481 * @attr: attributes of the counter to create
7482 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07007483 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007484 */
7485struct perf_event *
7486perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07007487 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007488 perf_overflow_handler_t overflow_handler,
7489 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007490{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007491 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007492 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007493 int err;
7494
7495 /*
7496 * Get the target context (task or percpu):
7497 */
7498
Avi Kivity4dc0da82011-06-29 18:42:35 +03007499 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7500 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007501 if (IS_ERR(event)) {
7502 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007503 goto err;
7504 }
7505
Jiri Olsaf8697762014-08-01 14:33:01 +02007506 /* Mark owner so we could distinguish it from user events. */
7507 event->owner = EVENT_OWNER_KERNEL;
7508
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007509 account_event(event);
7510
Matt Helsley38a81da2010-09-13 13:01:20 -07007511 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007512 if (IS_ERR(ctx)) {
7513 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007514 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007515 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007516
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007517 WARN_ON_ONCE(ctx->parent_ctx);
7518 mutex_lock(&ctx->mutex);
7519 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007520 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007521 mutex_unlock(&ctx->mutex);
7522
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007523 return event;
7524
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007525err_free:
7526 free_event(event);
7527err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007528 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007529}
7530EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7531
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007532void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7533{
7534 struct perf_event_context *src_ctx;
7535 struct perf_event_context *dst_ctx;
7536 struct perf_event *event, *tmp;
7537 LIST_HEAD(events);
7538
7539 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7540 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7541
7542 mutex_lock(&src_ctx->mutex);
7543 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7544 event_entry) {
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02007545 perf_remove_from_context(event, false);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007546 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007547 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02007548 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007549 }
7550 mutex_unlock(&src_ctx->mutex);
7551
7552 synchronize_rcu();
7553
7554 mutex_lock(&dst_ctx->mutex);
Peter Zijlstra98861672013-10-03 16:02:23 +02007555 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7556 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007557 if (event->state >= PERF_EVENT_STATE_OFF)
7558 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007559 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007560 perf_install_in_context(dst_ctx, event, dst_cpu);
7561 get_ctx(dst_ctx);
7562 }
7563 mutex_unlock(&dst_ctx->mutex);
7564}
7565EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7566
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007567static void sync_child_event(struct perf_event *child_event,
7568 struct task_struct *child)
7569{
7570 struct perf_event *parent_event = child_event->parent;
7571 u64 child_val;
7572
7573 if (child_event->attr.inherit_stat)
7574 perf_event_read_event(child_event, child);
7575
Peter Zijlstrab5e58792010-05-21 14:43:12 +02007576 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007577
7578 /*
7579 * Add back the child's count to the parent's count:
7580 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02007581 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007582 atomic64_add(child_event->total_time_enabled,
7583 &parent_event->child_total_time_enabled);
7584 atomic64_add(child_event->total_time_running,
7585 &parent_event->child_total_time_running);
7586
7587 /*
7588 * Remove this event from the parent's list
7589 */
7590 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7591 mutex_lock(&parent_event->child_mutex);
7592 list_del_init(&child_event->child_list);
7593 mutex_unlock(&parent_event->child_mutex);
7594
7595 /*
Jiri Olsadc633982014-09-12 13:18:26 +02007596 * Make sure user/parent get notified, that we just
7597 * lost one event.
7598 */
7599 perf_event_wakeup(parent_event);
7600
7601 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007602 * Release the parent event, if this was the last
7603 * reference to it.
7604 */
Al Viroa6fa9412012-08-20 14:59:25 +01007605 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007606}
7607
7608static void
7609__perf_event_exit_task(struct perf_event *child_event,
7610 struct perf_event_context *child_ctx,
7611 struct task_struct *child)
7612{
Peter Zijlstra1903d502014-07-15 17:27:27 +02007613 /*
7614 * Do not destroy the 'original' grouping; because of the context
7615 * switch optimization the original events could've ended up in a
7616 * random child task.
7617 *
7618 * If we were to destroy the original group, all group related
7619 * operations would cease to function properly after this random
7620 * child dies.
7621 *
7622 * Do destroy all inherited groups, we don't care about those
7623 * and being thorough is better.
7624 */
7625 perf_remove_from_context(child_event, !!child_event->parent);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007626
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007627 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007628 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007629 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007630 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007631 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007632 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007633 sync_child_event(child_event, child);
7634 free_event(child_event);
Jiri Olsa179033b2014-08-07 11:48:26 -04007635 } else {
7636 child_event->state = PERF_EVENT_STATE_EXIT;
7637 perf_event_wakeup(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007638 }
7639}
7640
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007641static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007642{
Peter Zijlstraebf905f2014-05-29 19:00:24 +02007643 struct perf_event *child_event, *next;
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007644 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007645 unsigned long flags;
7646
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007647 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007648 perf_event_task(child, NULL, 0);
7649 return;
7650 }
7651
7652 local_irq_save(flags);
7653 /*
7654 * We can't reschedule here because interrupts are disabled,
7655 * and either child is current or it is a task that can't be
7656 * scheduled, so we are now safe from rescheduling changing
7657 * our context.
7658 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01007659 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007660
7661 /*
7662 * Take the context lock here so that if find_get_context is
7663 * reading child->perf_event_ctxp, we wait until it has
7664 * incremented the context's refcount before we do put_ctx below.
7665 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007666 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02007667 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007668 child->perf_event_ctxp[ctxn] = NULL;
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007669
7670 /*
7671 * In order to avoid freeing: child_ctx->parent_ctx->task
7672 * under perf_event_context::lock, grab another reference.
7673 */
7674 parent_ctx = child_ctx->parent_ctx;
7675 if (parent_ctx)
7676 get_ctx(parent_ctx);
7677
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007678 /*
7679 * If this context is a clone; unclone it so it can't get
7680 * swapped to another process while we're removing all
7681 * the events from it.
7682 */
7683 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01007684 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007685 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007686
7687 /*
Peter Zijlstra4a1c0f22014-06-23 16:12:42 +02007688 * Now that we no longer hold perf_event_context::lock, drop
7689 * our extra child_ctx->parent_ctx reference.
7690 */
7691 if (parent_ctx)
7692 put_ctx(parent_ctx);
7693
7694 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007695 * Report the task dead after unscheduling the events so that we
7696 * won't get any samples after PERF_RECORD_EXIT. We can however still
7697 * get a few PERF_RECORD_READ events.
7698 */
7699 perf_event_task(child, child_ctx, 0);
7700
7701 /*
7702 * We can recurse on the same lock type through:
7703 *
7704 * __perf_event_exit_task()
7705 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01007706 * put_event()
7707 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007708 *
7709 * But since its the parent context it won't be the same instance.
7710 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02007711 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007712
Peter Zijlstraebf905f2014-05-29 19:00:24 +02007713 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007714 __perf_event_exit_task(child_event, child_ctx, child);
7715
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007716 mutex_unlock(&child_ctx->mutex);
7717
7718 put_ctx(child_ctx);
7719}
7720
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007721/*
7722 * When a child task exits, feed back event values to parent events.
7723 */
7724void perf_event_exit_task(struct task_struct *child)
7725{
Peter Zijlstra88821352010-11-09 19:01:43 +01007726 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007727 int ctxn;
7728
Peter Zijlstra88821352010-11-09 19:01:43 +01007729 mutex_lock(&child->perf_event_mutex);
7730 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
7731 owner_entry) {
7732 list_del_init(&event->owner_entry);
7733
7734 /*
7735 * Ensure the list deletion is visible before we clear
7736 * the owner, closes a race against perf_release() where
7737 * we need to serialize on the owner->perf_event_mutex.
7738 */
7739 smp_wmb();
7740 event->owner = NULL;
7741 }
7742 mutex_unlock(&child->perf_event_mutex);
7743
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007744 for_each_task_context_nr(ctxn)
7745 perf_event_exit_task_context(child, ctxn);
7746}
7747
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007748static void perf_free_event(struct perf_event *event,
7749 struct perf_event_context *ctx)
7750{
7751 struct perf_event *parent = event->parent;
7752
7753 if (WARN_ON_ONCE(!parent))
7754 return;
7755
7756 mutex_lock(&parent->child_mutex);
7757 list_del_init(&event->child_list);
7758 mutex_unlock(&parent->child_mutex);
7759
Al Viroa6fa9412012-08-20 14:59:25 +01007760 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007761
Peter Zijlstra8a495422010-05-27 15:47:49 +02007762 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007763 list_del_event(event, ctx);
7764 free_event(event);
7765}
7766
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007767/*
7768 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007769 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007770 */
7771void perf_event_free_task(struct task_struct *task)
7772{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007773 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007774 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007775 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007776
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007777 for_each_task_context_nr(ctxn) {
7778 ctx = task->perf_event_ctxp[ctxn];
7779 if (!ctx)
7780 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007781
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007782 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007783again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007784 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7785 group_entry)
7786 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007787
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007788 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7789 group_entry)
7790 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007791
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007792 if (!list_empty(&ctx->pinned_groups) ||
7793 !list_empty(&ctx->flexible_groups))
7794 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007795
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007796 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007797
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007798 put_ctx(ctx);
7799 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007800}
7801
Peter Zijlstra4e231c72010-09-09 21:01:59 +02007802void perf_event_delayed_put(struct task_struct *task)
7803{
7804 int ctxn;
7805
7806 for_each_task_context_nr(ctxn)
7807 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7808}
7809
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007810/*
7811 * inherit a event from parent task to child task:
7812 */
7813static struct perf_event *
7814inherit_event(struct perf_event *parent_event,
7815 struct task_struct *parent,
7816 struct perf_event_context *parent_ctx,
7817 struct task_struct *child,
7818 struct perf_event *group_leader,
7819 struct perf_event_context *child_ctx)
7820{
Jiri Olsa1929def2014-09-12 13:18:27 +02007821 enum perf_event_active_state parent_state = parent_event->state;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007822 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02007823 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007824
7825 /*
7826 * Instead of creating recursive hierarchies of events,
7827 * we link inherited events back to the original parent,
7828 * which has a filp for sure, which we use as the reference
7829 * count:
7830 */
7831 if (parent_event->parent)
7832 parent_event = parent_event->parent;
7833
7834 child_event = perf_event_alloc(&parent_event->attr,
7835 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007836 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007837 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007838 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007839 if (IS_ERR(child_event))
7840 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01007841
Jiri Olsafadfe7b2014-08-01 14:33:02 +02007842 if (is_orphaned_event(parent_event) ||
7843 !atomic_long_inc_not_zero(&parent_event->refcount)) {
Al Viroa6fa9412012-08-20 14:59:25 +01007844 free_event(child_event);
7845 return NULL;
7846 }
7847
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007848 get_ctx(child_ctx);
7849
7850 /*
7851 * Make the child state follow the state of the parent event,
7852 * not its attr.disabled bit. We hold the parent's mutex,
7853 * so we won't race with perf_event_{en, dis}able_family.
7854 */
Jiri Olsa1929def2014-09-12 13:18:27 +02007855 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007856 child_event->state = PERF_EVENT_STATE_INACTIVE;
7857 else
7858 child_event->state = PERF_EVENT_STATE_OFF;
7859
7860 if (parent_event->attr.freq) {
7861 u64 sample_period = parent_event->hw.sample_period;
7862 struct hw_perf_event *hwc = &child_event->hw;
7863
7864 hwc->sample_period = sample_period;
7865 hwc->last_period = sample_period;
7866
7867 local64_set(&hwc->period_left, sample_period);
7868 }
7869
7870 child_event->ctx = child_ctx;
7871 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007872 child_event->overflow_handler_context
7873 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007874
7875 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02007876 * Precalculate sample_data sizes
7877 */
7878 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007879 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02007880
7881 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007882 * Link it up in the child's context:
7883 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02007884 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007885 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02007886 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007887
7888 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007889 * Link this into the parent event's child list
7890 */
7891 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7892 mutex_lock(&parent_event->child_mutex);
7893 list_add_tail(&child_event->child_list, &parent_event->child_list);
7894 mutex_unlock(&parent_event->child_mutex);
7895
7896 return child_event;
7897}
7898
7899static int inherit_group(struct perf_event *parent_event,
7900 struct task_struct *parent,
7901 struct perf_event_context *parent_ctx,
7902 struct task_struct *child,
7903 struct perf_event_context *child_ctx)
7904{
7905 struct perf_event *leader;
7906 struct perf_event *sub;
7907 struct perf_event *child_ctr;
7908
7909 leader = inherit_event(parent_event, parent, parent_ctx,
7910 child, NULL, child_ctx);
7911 if (IS_ERR(leader))
7912 return PTR_ERR(leader);
7913 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7914 child_ctr = inherit_event(sub, parent, parent_ctx,
7915 child, leader, child_ctx);
7916 if (IS_ERR(child_ctr))
7917 return PTR_ERR(child_ctr);
7918 }
7919 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007920}
7921
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007922static int
7923inherit_task_group(struct perf_event *event, struct task_struct *parent,
7924 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007925 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007926 int *inherited_all)
7927{
7928 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007929 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007930
7931 if (!event->attr.inherit) {
7932 *inherited_all = 0;
7933 return 0;
7934 }
7935
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007936 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007937 if (!child_ctx) {
7938 /*
7939 * This is executed from the parent task context, so
7940 * inherit events that have been marked for cloning.
7941 * First allocate and initialize a context for the
7942 * child.
7943 */
7944
Jiri Olsa734df5a2013-07-09 17:44:10 +02007945 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007946 if (!child_ctx)
7947 return -ENOMEM;
7948
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007949 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007950 }
7951
7952 ret = inherit_group(event, parent, parent_ctx,
7953 child, child_ctx);
7954
7955 if (ret)
7956 *inherited_all = 0;
7957
7958 return ret;
7959}
7960
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007961/*
7962 * Initialize the perf_event context in task_struct
7963 */
Jiri Olsa985c8dc2014-06-24 10:20:24 +02007964static int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007965{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007966 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007967 struct perf_event_context *cloned_ctx;
7968 struct perf_event *event;
7969 struct task_struct *parent = current;
7970 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007971 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007972 int ret = 0;
7973
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007974 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007975 return 0;
7976
7977 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007978 * If the parent's context is a clone, pin it so it won't get
7979 * swapped under us.
7980 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007981 parent_ctx = perf_pin_task_context(parent, ctxn);
Peter Zijlstraffb4ef22014-05-05 19:12:20 +02007982 if (!parent_ctx)
7983 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007984
7985 /*
7986 * No need to check if parent_ctx != NULL here; since we saw
7987 * it non-NULL earlier, the only reason for it to become NULL
7988 * is if we exit, and since we're currently in the middle of
7989 * a fork we can't be exiting at the same time.
7990 */
7991
7992 /*
7993 * Lock the parent list. No need to lock the child - not PID
7994 * hashed yet and not running, so nobody can access it.
7995 */
7996 mutex_lock(&parent_ctx->mutex);
7997
7998 /*
7999 * We dont have to disable NMIs - we are only looking at
8000 * the list, not manipulating it:
8001 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008002 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008003 ret = inherit_task_group(event, parent, parent_ctx,
8004 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008005 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008006 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008007 }
8008
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008009 /*
8010 * We can't hold ctx->lock when iterating the ->flexible_group list due
8011 * to allocations, but we need to prevent rotation because
8012 * rotate_ctx() will change the list from interrupt context.
8013 */
8014 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
8015 parent_ctx->rotate_disable = 1;
8016 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
8017
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008018 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008019 ret = inherit_task_group(event, parent, parent_ctx,
8020 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008021 if (ret)
8022 break;
8023 }
8024
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008025 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
8026 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01008027
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008028 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01008029
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01008030 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008031 /*
8032 * Mark the child context as a clone of the parent
8033 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008034 *
8035 * Note that if the parent is a clone, the holding of
8036 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008037 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008038 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008039 if (cloned_ctx) {
8040 child_ctx->parent_ctx = cloned_ctx;
8041 child_ctx->parent_gen = parent_ctx->parent_gen;
8042 } else {
8043 child_ctx->parent_ctx = parent_ctx;
8044 child_ctx->parent_gen = parent_ctx->generation;
8045 }
8046 get_ctx(child_ctx->parent_ctx);
8047 }
8048
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01008049 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008050 mutex_unlock(&parent_ctx->mutex);
8051
8052 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01008053 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008054
8055 return ret;
8056}
8057
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008058/*
8059 * Initialize the perf_event context in task_struct
8060 */
8061int perf_event_init_task(struct task_struct *child)
8062{
8063 int ctxn, ret;
8064
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01008065 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
8066 mutex_init(&child->perf_event_mutex);
8067 INIT_LIST_HEAD(&child->perf_event_list);
8068
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02008069 for_each_task_context_nr(ctxn) {
8070 ret = perf_event_init_context(child, ctxn);
8071 if (ret)
8072 return ret;
8073 }
8074
8075 return 0;
8076}
8077
Paul Mackerras220b1402010-03-10 20:45:52 +11008078static void __init perf_event_init_all_cpus(void)
8079{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008080 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11008081 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11008082
8083 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008084 swhash = &per_cpu(swevent_htable, cpu);
8085 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02008086 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11008087 }
8088}
8089
Paul Gortmaker0db06282013-06-19 14:53:51 -04008090static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008091{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008092 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008093
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008094 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02008095 swhash->online = true;
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008096 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008097 struct swevent_hlist *hlist;
8098
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008099 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
8100 WARN_ON(!hlist);
8101 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02008102 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008103 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008104}
8105
Peter Zijlstrac2774432010-12-08 15:29:02 +01008106#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02008107static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008108{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02008109 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
8110
8111 WARN_ON(!irqs_disabled());
8112
8113 list_del_init(&cpuctx->rotation_list);
8114}
8115
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008116static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008117{
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02008118 struct remove_event re = { .detach_group = false };
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008119 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008120
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008121 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02008122
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008123 rcu_read_lock();
Peter Zijlstra46ce0fe2014-05-02 16:56:01 +02008124 list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
8125 __perf_remove_from_context(&re);
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008126 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008127}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008128
8129static void perf_event_exit_cpu_context(int cpu)
8130{
8131 struct perf_event_context *ctx;
8132 struct pmu *pmu;
8133 int idx;
8134
8135 idx = srcu_read_lock(&pmus_srcu);
8136 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02008137 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008138
8139 mutex_lock(&ctx->mutex);
8140 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
8141 mutex_unlock(&ctx->mutex);
8142 }
8143 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02008144}
8145
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008146static void perf_event_exit_cpu(int cpu)
8147{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008148 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008149
Peter Zijlstrae3703f82014-02-24 12:06:12 +01008150 perf_event_exit_cpu_context(cpu);
8151
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008152 mutex_lock(&swhash->hlist_mutex);
Jiri Olsa39af6b12014-04-07 11:04:08 +02008153 swhash->online = false;
Peter Zijlstrab28ab832010-09-06 14:48:15 +02008154 swevent_hlist_release(swhash);
8155 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008156}
8157#else
8158static inline void perf_event_exit_cpu(int cpu) { }
8159#endif
8160
Peter Zijlstrac2774432010-12-08 15:29:02 +01008161static int
8162perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
8163{
8164 int cpu;
8165
8166 for_each_online_cpu(cpu)
8167 perf_event_exit_cpu(cpu);
8168
8169 return NOTIFY_OK;
8170}
8171
8172/*
8173 * Run the perf reboot notifier at the very last possible moment so that
8174 * the generic watchdog code runs as long as possible.
8175 */
8176static struct notifier_block perf_reboot_notifier = {
8177 .notifier_call = perf_reboot,
8178 .priority = INT_MIN,
8179};
8180
Paul Gortmaker0db06282013-06-19 14:53:51 -04008181static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008182perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
8183{
8184 unsigned int cpu = (long)hcpu;
8185
Linus Torvalds4536e4d2011-11-03 07:44:04 -07008186 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008187
8188 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02008189 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008190 perf_event_init_cpu(cpu);
8191 break;
8192
Peter Zijlstra5e116372010-06-11 13:35:08 +02008193 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008194 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008195 perf_event_exit_cpu(cpu);
8196 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008197 default:
8198 break;
8199 }
8200
8201 return NOTIFY_OK;
8202}
8203
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008204void __init perf_event_init(void)
8205{
Jason Wessel3c502e72010-11-04 17:33:01 -05008206 int ret;
8207
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008208 idr_init(&pmu_idr);
8209
Paul Mackerras220b1402010-03-10 20:45:52 +11008210 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008211 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01008212 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
8213 perf_pmu_register(&perf_cpu_clock, NULL, -1);
8214 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02008215 perf_tp_register();
8216 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01008217 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05008218
8219 ret = init_hw_breakpoint();
8220 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02008221
8222 /* do not patch jump label more than once per second */
8223 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01008224
8225 /*
8226 * Build time assertion that we keep the data_head at the intended
8227 * location. IOW, validation we got the __reserved[] size right.
8228 */
8229 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
8230 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008231}
Peter Zijlstraabe43402010-11-17 23:17:37 +01008232
8233static int __init perf_event_sysfs_init(void)
8234{
8235 struct pmu *pmu;
8236 int ret;
8237
8238 mutex_lock(&pmus_lock);
8239
8240 ret = bus_register(&pmu_bus);
8241 if (ret)
8242 goto unlock;
8243
8244 list_for_each_entry(pmu, &pmus, entry) {
8245 if (!pmu->name || pmu->type < 0)
8246 continue;
8247
8248 ret = pmu_dev_alloc(pmu);
8249 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
8250 }
8251 pmu_bus_running = 1;
8252 ret = 0;
8253
8254unlock:
8255 mutex_unlock(&pmus_lock);
8256
8257 return ret;
8258}
8259device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008260
8261#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04008262static struct cgroup_subsys_state *
8263perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008264{
8265 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02008266
Li Zefan1b15d052011-03-03 14:26:06 +08008267 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008268 if (!jc)
8269 return ERR_PTR(-ENOMEM);
8270
Stephane Eraniane5d13672011-02-14 11:20:01 +02008271 jc->info = alloc_percpu(struct perf_cgroup_info);
8272 if (!jc->info) {
8273 kfree(jc);
8274 return ERR_PTR(-ENOMEM);
8275 }
8276
Stephane Eraniane5d13672011-02-14 11:20:01 +02008277 return &jc->css;
8278}
8279
Tejun Heoeb954192013-08-08 20:11:23 -04008280static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008281{
Tejun Heoeb954192013-08-08 20:11:23 -04008282 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
8283
Stephane Eraniane5d13672011-02-14 11:20:01 +02008284 free_percpu(jc->info);
8285 kfree(jc);
8286}
8287
8288static int __perf_cgroup_move(void *info)
8289{
8290 struct task_struct *task = info;
8291 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
8292 return 0;
8293}
8294
Tejun Heoeb954192013-08-08 20:11:23 -04008295static void perf_cgroup_attach(struct cgroup_subsys_state *css,
8296 struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008297{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008298 struct task_struct *task;
8299
Tejun Heo924f0d92014-02-13 06:58:41 -05008300 cgroup_taskset_for_each(task, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008301 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008302}
8303
Tejun Heoeb954192013-08-08 20:11:23 -04008304static void perf_cgroup_exit(struct cgroup_subsys_state *css,
8305 struct cgroup_subsys_state *old_css,
Li Zefan761b3ef2012-01-31 13:47:36 +08008306 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008307{
8308 /*
8309 * cgroup_exit() is called in the copy_process() failure path.
8310 * Ignore this case since the task hasn't ran yet, this avoids
8311 * trying to poke a half freed task state from generic code.
8312 */
8313 if (!(task->flags & PF_EXITING))
8314 return;
8315
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 Heo073219e2014-02-08 10:36:58 -05008319struct cgroup_subsys perf_event_cgrp_subsys = {
Tejun Heo92fb9742012-11-19 08:13:38 -08008320 .css_alloc = perf_cgroup_css_alloc,
8321 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008322 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008323 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02008324};
8325#endif /* CONFIG_CGROUP_PERF */