blob: 17b3c6cf16066b47b55af7ee8e49bab41d94da7c [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
Ingo Molnare7e7ee22011-05-04 08:42:29 +02005 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
Al Virod36b6912011-12-29 17:09:01 -05007 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02008 *
Ingo Molnar57c0c152009-09-21 12:20:38 +02009 * For licensing details see kernel-base/COPYING
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010 */
11
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include <linux/cpu.h>
15#include <linux/smp.h>
Peter Zijlstra2e80a822010-11-17 23:17:36 +010016#include <linux/idr.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/file.h>
18#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020020#include <linux/hash.h>
Frederic Weisbecker12351ef2013-04-20 15:48:22 +020021#include <linux/tick.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020022#include <linux/sysfs.h>
23#include <linux/dcache.h>
24#include <linux/percpu.h>
25#include <linux/ptrace.h>
Peter Zijlstrac2774432010-12-08 15:29:02 +010026#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020027#include <linux/vmstat.h>
Peter Zijlstraabe43402010-11-17 23:17:37 +010028#include <linux/device.h>
Paul Gortmaker6e5fdee2011-05-26 16:00:52 -040029#include <linux/export.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020030#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020031#include <linux/hardirq.h>
32#include <linux/rculist.h>
33#include <linux/uaccess.h>
34#include <linux/syscalls.h>
35#include <linux/anon_inodes.h>
36#include <linux/kernel_stat.h>
37#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080038#include <linux/ftrace_event.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050039#include <linux/hw_breakpoint.h>
Jiri Olsac5ebced2012-08-07 15:20:40 +020040#include <linux/mm_types.h>
Li Zefan877c6852013-03-05 11:38:08 +080041#include <linux/cgroup.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020042
Frederic Weisbecker76369132011-05-19 19:55:04 +020043#include "internal.h"
44
Ingo Molnarcdd6c482009-09-21 12:02:48 +020045#include <asm/irq_regs.h>
46
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010047struct remote_function_call {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020048 struct task_struct *p;
49 int (*func)(void *info);
50 void *info;
51 int ret;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010052};
53
54static void remote_function(void *data)
55{
56 struct remote_function_call *tfc = data;
57 struct task_struct *p = tfc->p;
58
59 if (p) {
60 tfc->ret = -EAGAIN;
61 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
62 return;
63 }
64
65 tfc->ret = tfc->func(tfc->info);
66}
67
68/**
69 * task_function_call - call a function on the cpu on which a task runs
70 * @p: the task to evaluate
71 * @func: the function to be called
72 * @info: the function call argument
73 *
74 * Calls the function @func when the task is currently running. This might
75 * be on the current CPU, which just calls the function directly
76 *
77 * returns: @func return value, or
78 * -ESRCH - when the process isn't running
79 * -EAGAIN - when the process moved away
80 */
81static int
82task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
83{
84 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +020085 .p = p,
86 .func = func,
87 .info = info,
88 .ret = -ESRCH, /* No such (running) process */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010089 };
90
91 if (task_curr(p))
92 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
93
94 return data.ret;
95}
96
97/**
98 * cpu_function_call - call a function on the cpu
99 * @func: the function to be called
100 * @info: the function call argument
101 *
102 * Calls the function @func on the remote cpu.
103 *
104 * returns: @func return value or -ENXIO when the cpu is offline
105 */
106static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
107{
108 struct remote_function_call data = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +0200109 .p = NULL,
110 .func = func,
111 .info = info,
112 .ret = -ENXIO, /* No such CPU */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +0100113 };
114
115 smp_call_function_single(cpu, remote_function, &data, 1);
116
117 return data.ret;
118}
119
Stephane Eraniane5d13672011-02-14 11:20:01 +0200120#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
121 PERF_FLAG_FD_OUTPUT |\
122 PERF_FLAG_PID_CGROUP)
123
Stephane Eranianbce38cd2012-02-09 23:20:51 +0100124/*
125 * branch priv levels that need permission checks
126 */
127#define PERF_SAMPLE_BRANCH_PERM_PLM \
128 (PERF_SAMPLE_BRANCH_KERNEL |\
129 PERF_SAMPLE_BRANCH_HV)
130
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200131enum event_type_t {
132 EVENT_FLEXIBLE = 0x1,
133 EVENT_PINNED = 0x2,
134 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
135};
136
Stephane Eraniane5d13672011-02-14 11:20:01 +0200137/*
138 * perf_sched_events : >0 events exist
139 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
140 */
Ingo Molnarc5905af2012-02-24 08:31:31 +0100141struct static_key_deferred perf_sched_events __read_mostly;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200142static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
Stephane Eraniand010b332012-02-09 23:21:00 +0100143static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200144
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200145static atomic_t nr_mmap_events __read_mostly;
146static atomic_t nr_comm_events __read_mostly;
147static atomic_t nr_task_events __read_mostly;
Frederic Weisbecker948b26b2013-08-02 18:29:55 +0200148static atomic_t nr_freq_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200149
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200150static LIST_HEAD(pmus);
151static DEFINE_MUTEX(pmus_lock);
152static struct srcu_struct pmus_srcu;
153
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200154/*
155 * perf event paranoia level:
156 * -1 - not paranoid at all
157 * 0 - disallow raw tracepoint access for unpriv
158 * 1 - disallow cpu events for unpriv
159 * 2 - disallow kernel profiling for unpriv
160 */
161int sysctl_perf_event_paranoid __read_mostly = 1;
162
Frederic Weisbecker20443382011-03-31 03:33:29 +0200163/* Minimum for 512 kiB + 1 user control page */
164int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200165
166/*
167 * max perf event sample rate
168 */
Dave Hansen14c63f12013-06-21 08:51:36 -0700169#define DEFAULT_MAX_SAMPLE_RATE 100000
170#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
171#define DEFAULT_CPU_TIME_MAX_PERCENT 25
172
173int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
174
175static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
176static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
177
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200178static int perf_sample_allowed_ns __read_mostly =
179 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
Dave Hansen14c63f12013-06-21 08:51:36 -0700180
181void update_perf_cpu_limits(void)
182{
183 u64 tmp = perf_sample_period_ns;
184
185 tmp *= sysctl_perf_cpu_time_max_percent;
Stephane Eraniane5302922013-07-05 00:30:11 +0200186 do_div(tmp, 100);
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200187 ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
Dave Hansen14c63f12013-06-21 08:51:36 -0700188}
Peter Zijlstra163ec432011-02-16 11:22:34 +0100189
Stephane Eranian9e630202013-04-03 14:21:33 +0200190static int perf_rotate_context(struct perf_cpu_context *cpuctx);
191
Peter Zijlstra163ec432011-02-16 11:22:34 +0100192int perf_proc_update_handler(struct ctl_table *table, int write,
193 void __user *buffer, size_t *lenp,
194 loff_t *ppos)
195{
Knut Petersen723478c2013-09-25 14:29:37 +0200196 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
Peter Zijlstra163ec432011-02-16 11:22:34 +0100197
198 if (ret || !write)
199 return ret;
200
201 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
Dave Hansen14c63f12013-06-21 08:51:36 -0700202 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
203 update_perf_cpu_limits();
Peter Zijlstra163ec432011-02-16 11:22:34 +0100204
205 return 0;
206}
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200207
Dave Hansen14c63f12013-06-21 08:51:36 -0700208int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
209
210int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
211 void __user *buffer, size_t *lenp,
212 loff_t *ppos)
213{
214 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
215
216 if (ret || !write)
217 return ret;
218
219 update_perf_cpu_limits();
220
221 return 0;
222}
223
224/*
225 * perf samples are done in some very critical code paths (NMIs).
226 * If they take too much CPU time, the system can lock up and not
227 * get any real work done. This will drop the sample rate when
228 * we detect that events are taking too long.
229 */
230#define NR_ACCUMULATED_SAMPLES 128
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200231static DEFINE_PER_CPU(u64, running_sample_length);
Dave Hansen14c63f12013-06-21 08:51:36 -0700232
233void perf_sample_event_took(u64 sample_len_ns)
234{
235 u64 avg_local_sample_len;
Stephane Eraniane5302922013-07-05 00:30:11 +0200236 u64 local_samples_len;
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200237 u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
Dave Hansen14c63f12013-06-21 08:51:36 -0700238
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200239 if (allowed_ns == 0)
Dave Hansen14c63f12013-06-21 08:51:36 -0700240 return;
241
242 /* decay the counter by 1 average sample */
243 local_samples_len = __get_cpu_var(running_sample_length);
244 local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
245 local_samples_len += sample_len_ns;
246 __get_cpu_var(running_sample_length) = local_samples_len;
247
248 /*
249 * note: this will be biased artifically low until we have
250 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
251 * from having to maintain a count.
252 */
253 avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
254
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200255 if (avg_local_sample_len <= allowed_ns)
Dave Hansen14c63f12013-06-21 08:51:36 -0700256 return;
257
258 if (max_samples_per_tick <= 1)
259 return;
260
261 max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
262 sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
263 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
264
265 printk_ratelimited(KERN_WARNING
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200266 "perf samples too long (%lld > %lld), lowering "
Dave Hansen14c63f12013-06-21 08:51:36 -0700267 "kernel.perf_event_max_sample_rate to %d\n",
Peter Zijlstrad9494cb2013-10-17 15:36:19 +0200268 avg_local_sample_len, allowed_ns,
Dave Hansen14c63f12013-06-21 08:51:36 -0700269 sysctl_perf_event_sample_rate);
270
271 update_perf_cpu_limits();
272}
273
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200274static atomic64_t perf_event_id;
275
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200276static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
277 enum event_type_t event_type);
278
279static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200280 enum event_type_t event_type,
281 struct task_struct *task);
282
283static void update_context_time(struct perf_event_context *ctx);
284static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200285
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200286void __weak perf_event_print_debug(void) { }
287
Matt Fleming84c79912010-10-03 21:41:13 +0100288extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200289{
Matt Fleming84c79912010-10-03 21:41:13 +0100290 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200291}
292
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200293static inline u64 perf_clock(void)
294{
295 return local_clock();
296}
297
Stephane Eraniane5d13672011-02-14 11:20:01 +0200298static inline struct perf_cpu_context *
299__get_cpu_context(struct perf_event_context *ctx)
300{
301 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
302}
303
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200304static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
305 struct perf_event_context *ctx)
306{
307 raw_spin_lock(&cpuctx->ctx.lock);
308 if (ctx)
309 raw_spin_lock(&ctx->lock);
310}
311
312static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
313 struct perf_event_context *ctx)
314{
315 if (ctx)
316 raw_spin_unlock(&ctx->lock);
317 raw_spin_unlock(&cpuctx->ctx.lock);
318}
319
Stephane Eraniane5d13672011-02-14 11:20:01 +0200320#ifdef CONFIG_CGROUP_PERF
321
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200322/*
Li Zefan877c6852013-03-05 11:38:08 +0800323 * perf_cgroup_info keeps track of time_enabled for a cgroup.
324 * This is a per-cpu dynamically allocated data structure.
325 */
326struct perf_cgroup_info {
327 u64 time;
328 u64 timestamp;
329};
330
331struct perf_cgroup {
332 struct cgroup_subsys_state css;
Namhyung Kim86e213e2013-03-18 18:56:34 +0900333 struct perf_cgroup_info __percpu *info;
Li Zefan877c6852013-03-05 11:38:08 +0800334};
335
336/*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200337 * Must ensure cgroup is pinned (css_get) before calling
338 * this function. In other words, we cannot call this function
339 * if there is no cgroup event for the current CPU context.
340 */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200341static inline struct perf_cgroup *
342perf_cgroup_from_task(struct task_struct *task)
343{
Tejun Heo8af01f52013-08-08 20:11:22 -0400344 return container_of(task_css(task, perf_subsys_id),
345 struct perf_cgroup, css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200346}
347
348static inline bool
349perf_cgroup_match(struct perf_event *event)
350{
351 struct perf_event_context *ctx = event->ctx;
352 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
353
Tejun Heoef824fa2013-04-08 19:00:38 -0700354 /* @event doesn't care about cgroup */
355 if (!event->cgrp)
356 return true;
357
358 /* wants specific cgroup scope but @cpuctx isn't associated with any */
359 if (!cpuctx->cgrp)
360 return false;
361
362 /*
363 * Cgroup scoping is recursive. An event enabled for a cgroup is
364 * also enabled for all its descendant cgroups. If @cpuctx's
365 * cgroup is a descendant of @event's (the test covers identity
366 * case), it's a match.
367 */
368 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
369 event->cgrp->css.cgroup);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200370}
371
Salman Qazi9c5da092012-06-14 15:31:09 -0700372static inline bool perf_tryget_cgroup(struct perf_event *event)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200373{
Salman Qazi9c5da092012-06-14 15:31:09 -0700374 return css_tryget(&event->cgrp->css);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200375}
376
377static inline void perf_put_cgroup(struct perf_event *event)
378{
379 css_put(&event->cgrp->css);
380}
381
382static inline void perf_detach_cgroup(struct perf_event *event)
383{
384 perf_put_cgroup(event);
385 event->cgrp = NULL;
386}
387
388static inline int is_cgroup_event(struct perf_event *event)
389{
390 return event->cgrp != NULL;
391}
392
393static inline u64 perf_cgroup_event_time(struct perf_event *event)
394{
395 struct perf_cgroup_info *t;
396
397 t = per_cpu_ptr(event->cgrp->info, event->cpu);
398 return t->time;
399}
400
401static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
402{
403 struct perf_cgroup_info *info;
404 u64 now;
405
406 now = perf_clock();
407
408 info = this_cpu_ptr(cgrp->info);
409
410 info->time += now - info->timestamp;
411 info->timestamp = now;
412}
413
414static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
415{
416 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
417 if (cgrp_out)
418 __update_cgrp_time(cgrp_out);
419}
420
421static inline void update_cgrp_time_from_event(struct perf_event *event)
422{
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200423 struct perf_cgroup *cgrp;
424
Stephane Eraniane5d13672011-02-14 11:20:01 +0200425 /*
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200426 * ensure we access cgroup data only when needed and
427 * when we know the cgroup is pinned (css_get)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200428 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200429 if (!is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +0200430 return;
431
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200432 cgrp = perf_cgroup_from_task(current);
433 /*
434 * Do not update time when cgroup is not active
435 */
436 if (cgrp == event->cgrp)
437 __update_cgrp_time(event->cgrp);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200438}
439
440static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200441perf_cgroup_set_timestamp(struct task_struct *task,
442 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200443{
444 struct perf_cgroup *cgrp;
445 struct perf_cgroup_info *info;
446
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200447 /*
448 * ctx->lock held by caller
449 * ensure we do not access cgroup data
450 * unless we have the cgroup pinned (css_get)
451 */
452 if (!task || !ctx->nr_cgroups)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200453 return;
454
455 cgrp = perf_cgroup_from_task(task);
456 info = this_cpu_ptr(cgrp->info);
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200457 info->timestamp = ctx->timestamp;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200458}
459
460#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
461#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
462
463/*
464 * reschedule events based on the cgroup constraint of task.
465 *
466 * mode SWOUT : schedule out everything
467 * mode SWIN : schedule in based on cgroup for next
468 */
469void perf_cgroup_switch(struct task_struct *task, int mode)
470{
471 struct perf_cpu_context *cpuctx;
472 struct pmu *pmu;
473 unsigned long flags;
474
475 /*
476 * disable interrupts to avoid geting nr_cgroup
477 * changes via __perf_event_disable(). Also
478 * avoids preemption.
479 */
480 local_irq_save(flags);
481
482 /*
483 * we reschedule only in the presence of cgroup
484 * constrained events.
485 */
486 rcu_read_lock();
487
488 list_for_each_entry_rcu(pmu, &pmus, entry) {
Stephane Eraniane5d13672011-02-14 11:20:01 +0200489 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200490 if (cpuctx->unique_pmu != pmu)
491 continue; /* ensure we process each cpuctx once */
Stephane Eraniane5d13672011-02-14 11:20:01 +0200492
Stephane Eraniane5d13672011-02-14 11:20:01 +0200493 /*
494 * perf_cgroup_events says at least one
495 * context on this CPU has cgroup events.
496 *
497 * ctx->nr_cgroups reports the number of cgroup
498 * events for a context.
499 */
500 if (cpuctx->ctx.nr_cgroups > 0) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200501 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
502 perf_pmu_disable(cpuctx->ctx.pmu);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200503
504 if (mode & PERF_CGROUP_SWOUT) {
505 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
506 /*
507 * must not be done before ctxswout due
508 * to event_filter_match() in event_sched_out()
509 */
510 cpuctx->cgrp = NULL;
511 }
512
513 if (mode & PERF_CGROUP_SWIN) {
Stephane Eraniane566b762011-04-06 02:54:54 +0200514 WARN_ON_ONCE(cpuctx->cgrp);
Peter Zijlstra95cf59e2012-10-02 15:41:23 +0200515 /*
516 * set cgrp before ctxsw in to allow
517 * event_filter_match() to not have to pass
518 * task around
Stephane Eraniane5d13672011-02-14 11:20:01 +0200519 */
520 cpuctx->cgrp = perf_cgroup_from_task(task);
521 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
522 }
Peter Zijlstrafacc4302011-04-09 21:17:42 +0200523 perf_pmu_enable(cpuctx->ctx.pmu);
524 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200525 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200526 }
527
528 rcu_read_unlock();
529
530 local_irq_restore(flags);
531}
532
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200533static inline void perf_cgroup_sched_out(struct task_struct *task,
534 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200535{
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200536 struct perf_cgroup *cgrp1;
537 struct perf_cgroup *cgrp2 = NULL;
538
539 /*
540 * we come here when we know perf_cgroup_events > 0
541 */
542 cgrp1 = perf_cgroup_from_task(task);
543
544 /*
545 * next is NULL when called from perf_event_enable_on_exec()
546 * that will systematically cause a cgroup_switch()
547 */
548 if (next)
549 cgrp2 = perf_cgroup_from_task(next);
550
551 /*
552 * only schedule out current cgroup events if we know
553 * that we are switching to a different cgroup. Otherwise,
554 * do no touch the cgroup events.
555 */
556 if (cgrp1 != cgrp2)
557 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200558}
559
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200560static inline void perf_cgroup_sched_in(struct task_struct *prev,
561 struct task_struct *task)
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 /* prev can never be NULL */
572 cgrp2 = perf_cgroup_from_task(prev);
573
574 /*
575 * only need to schedule in cgroup events if we are changing
576 * cgroup during ctxsw. Cgroup events were not scheduled
577 * out of ctxsw out if that was not the case.
578 */
579 if (cgrp1 != cgrp2)
580 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200581}
582
583static inline int perf_cgroup_connect(int fd, struct perf_event *event,
584 struct perf_event_attr *attr,
585 struct perf_event *group_leader)
586{
587 struct perf_cgroup *cgrp;
588 struct cgroup_subsys_state *css;
Al Viro2903ff02012-08-28 12:52:22 -0400589 struct fd f = fdget(fd);
590 int ret = 0;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200591
Al Viro2903ff02012-08-28 12:52:22 -0400592 if (!f.file)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200593 return -EBADF;
594
Tejun Heob77d7b62013-08-13 11:01:54 -0400595 rcu_read_lock();
596
Tejun Heo35cf0832013-08-26 18:40:56 -0400597 css = css_from_dir(f.file->f_dentry, &perf_subsys);
Li Zefan3db272c2011-03-03 14:25:37 +0800598 if (IS_ERR(css)) {
599 ret = PTR_ERR(css);
600 goto out;
601 }
Stephane Eraniane5d13672011-02-14 11:20:01 +0200602
603 cgrp = container_of(css, struct perf_cgroup, css);
604 event->cgrp = cgrp;
605
Li Zefanf75e18c2011-03-03 14:25:50 +0800606 /* must be done before we fput() the file */
Salman Qazi9c5da092012-06-14 15:31:09 -0700607 if (!perf_tryget_cgroup(event)) {
608 event->cgrp = NULL;
609 ret = -ENOENT;
610 goto out;
611 }
Li Zefanf75e18c2011-03-03 14:25:50 +0800612
Stephane Eraniane5d13672011-02-14 11:20:01 +0200613 /*
614 * all events in a group must monitor
615 * the same cgroup because a task belongs
616 * to only one perf cgroup at a time
617 */
618 if (group_leader && group_leader->cgrp != cgrp) {
619 perf_detach_cgroup(event);
620 ret = -EINVAL;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200621 }
Li Zefan3db272c2011-03-03 14:25:37 +0800622out:
Tejun Heob77d7b62013-08-13 11:01:54 -0400623 rcu_read_unlock();
Al Viro2903ff02012-08-28 12:52:22 -0400624 fdput(f);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200625 return ret;
626}
627
628static inline void
629perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
630{
631 struct perf_cgroup_info *t;
632 t = per_cpu_ptr(event->cgrp->info, event->cpu);
633 event->shadow_ctx_time = now - t->timestamp;
634}
635
636static inline void
637perf_cgroup_defer_enabled(struct perf_event *event)
638{
639 /*
640 * when the current task's perf cgroup does not match
641 * the event's, we need to remember to call the
642 * perf_mark_enable() function the first time a task with
643 * a matching perf cgroup is scheduled in.
644 */
645 if (is_cgroup_event(event) && !perf_cgroup_match(event))
646 event->cgrp_defer_enabled = 1;
647}
648
649static inline void
650perf_cgroup_mark_enabled(struct perf_event *event,
651 struct perf_event_context *ctx)
652{
653 struct perf_event *sub;
654 u64 tstamp = perf_event_time(event);
655
656 if (!event->cgrp_defer_enabled)
657 return;
658
659 event->cgrp_defer_enabled = 0;
660
661 event->tstamp_enabled = tstamp - event->total_time_enabled;
662 list_for_each_entry(sub, &event->sibling_list, group_entry) {
663 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
664 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
665 sub->cgrp_defer_enabled = 0;
666 }
667 }
668}
669#else /* !CONFIG_CGROUP_PERF */
670
671static inline bool
672perf_cgroup_match(struct perf_event *event)
673{
674 return true;
675}
676
677static inline void perf_detach_cgroup(struct perf_event *event)
678{}
679
680static inline int is_cgroup_event(struct perf_event *event)
681{
682 return 0;
683}
684
685static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
686{
687 return 0;
688}
689
690static inline void update_cgrp_time_from_event(struct perf_event *event)
691{
692}
693
694static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
695{
696}
697
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200698static inline void perf_cgroup_sched_out(struct task_struct *task,
699 struct task_struct *next)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200700{
701}
702
Stephane Eraniana8d757e2011-08-25 15:58:03 +0200703static inline void perf_cgroup_sched_in(struct task_struct *prev,
704 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200705{
706}
707
708static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
709 struct perf_event_attr *attr,
710 struct perf_event *group_leader)
711{
712 return -EINVAL;
713}
714
715static inline void
Stephane Eranian3f7cce32011-02-18 14:40:01 +0200716perf_cgroup_set_timestamp(struct task_struct *task,
717 struct perf_event_context *ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +0200718{
719}
720
721void
722perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
723{
724}
725
726static inline void
727perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
728{
729}
730
731static inline u64 perf_cgroup_event_time(struct perf_event *event)
732{
733 return 0;
734}
735
736static inline void
737perf_cgroup_defer_enabled(struct perf_event *event)
738{
739}
740
741static inline void
742perf_cgroup_mark_enabled(struct perf_event *event,
743 struct perf_event_context *ctx)
744{
745}
746#endif
747
Stephane Eranian9e630202013-04-03 14:21:33 +0200748/*
749 * set default to be dependent on timer tick just
750 * like original code
751 */
752#define PERF_CPU_HRTIMER (1000 / HZ)
753/*
754 * function must be called with interrupts disbled
755 */
756static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
757{
758 struct perf_cpu_context *cpuctx;
759 enum hrtimer_restart ret = HRTIMER_NORESTART;
760 int rotations = 0;
761
762 WARN_ON(!irqs_disabled());
763
764 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
765
766 rotations = perf_rotate_context(cpuctx);
767
768 /*
769 * arm timer if needed
770 */
771 if (rotations) {
772 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
773 ret = HRTIMER_RESTART;
774 }
775
776 return ret;
777}
778
779/* CPU is going down */
780void perf_cpu_hrtimer_cancel(int cpu)
781{
782 struct perf_cpu_context *cpuctx;
783 struct pmu *pmu;
784 unsigned long flags;
785
786 if (WARN_ON(cpu != smp_processor_id()))
787 return;
788
789 local_irq_save(flags);
790
791 rcu_read_lock();
792
793 list_for_each_entry_rcu(pmu, &pmus, entry) {
794 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
795
796 if (pmu->task_ctx_nr == perf_sw_context)
797 continue;
798
799 hrtimer_cancel(&cpuctx->hrtimer);
800 }
801
802 rcu_read_unlock();
803
804 local_irq_restore(flags);
805}
806
807static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
808{
809 struct hrtimer *hr = &cpuctx->hrtimer;
810 struct pmu *pmu = cpuctx->ctx.pmu;
Stephane Eranian62b85632013-04-03 14:21:34 +0200811 int timer;
Stephane Eranian9e630202013-04-03 14:21:33 +0200812
813 /* no multiplexing needed for SW PMU */
814 if (pmu->task_ctx_nr == perf_sw_context)
815 return;
816
Stephane Eranian62b85632013-04-03 14:21:34 +0200817 /*
818 * check default is sane, if not set then force to
819 * default interval (1/tick)
820 */
821 timer = pmu->hrtimer_interval_ms;
822 if (timer < 1)
823 timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
824
825 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
Stephane Eranian9e630202013-04-03 14:21:33 +0200826
827 hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
828 hr->function = perf_cpu_hrtimer_handler;
829}
830
831static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
832{
833 struct hrtimer *hr = &cpuctx->hrtimer;
834 struct pmu *pmu = cpuctx->ctx.pmu;
835
836 /* not for SW PMU */
837 if (pmu->task_ctx_nr == perf_sw_context)
838 return;
839
840 if (hrtimer_active(hr))
841 return;
842
843 if (!hrtimer_callback_running(hr))
844 __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
845 0, HRTIMER_MODE_REL_PINNED, 0);
846}
847
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200848void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200849{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200850 int *count = this_cpu_ptr(pmu->pmu_disable_count);
851 if (!(*count)++)
852 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200853}
854
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200855void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200856{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200857 int *count = this_cpu_ptr(pmu->pmu_disable_count);
858 if (!--(*count))
859 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200860}
861
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200862static DEFINE_PER_CPU(struct list_head, rotation_list);
863
864/*
865 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
866 * because they're strictly cpu affine and rotate_start is called with IRQs
867 * disabled, while rotate_context is called from IRQ context.
868 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200869static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200870{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200871 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200872 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200873
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200874 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200875
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +0200876 if (list_empty(&cpuctx->rotation_list))
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200877 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200878}
879
880static void get_ctx(struct perf_event_context *ctx)
881{
882 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
883}
884
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200885static void put_ctx(struct perf_event_context *ctx)
886{
887 if (atomic_dec_and_test(&ctx->refcount)) {
888 if (ctx->parent_ctx)
889 put_ctx(ctx->parent_ctx);
890 if (ctx->task)
891 put_task_struct(ctx->task);
Lai Jiangshancb796ff2011-03-18 12:07:41 +0800892 kfree_rcu(ctx, rcu_head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200893 }
894}
895
896static void unclone_ctx(struct perf_event_context *ctx)
897{
898 if (ctx->parent_ctx) {
899 put_ctx(ctx->parent_ctx);
900 ctx->parent_ctx = NULL;
901 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +0200902 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200903}
904
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200905static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
906{
907 /*
908 * only top level events have the pid namespace they were created in
909 */
910 if (event->parent)
911 event = event->parent;
912
913 return task_tgid_nr_ns(p, event->ns);
914}
915
916static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
917{
918 /*
919 * only top level events have the pid namespace they were created in
920 */
921 if (event->parent)
922 event = event->parent;
923
924 return task_pid_nr_ns(p, event->ns);
925}
926
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200927/*
928 * If we inherit events we want to return the parent event id
929 * to userspace.
930 */
931static u64 primary_event_id(struct perf_event *event)
932{
933 u64 id = event->id;
934
935 if (event->parent)
936 id = event->parent->id;
937
938 return id;
939}
940
941/*
942 * Get the perf_event_context for a task and lock it.
943 * This has to cope with with the fact that until it is locked,
944 * the context could get moved to another task.
945 */
946static struct perf_event_context *
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200947perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200948{
949 struct perf_event_context *ctx;
950
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200951retry:
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200952 /*
953 * One of the few rules of preemptible RCU is that one cannot do
954 * rcu_read_unlock() while holding a scheduler (or nested) lock when
955 * part of the read side critical section was preemptible -- see
956 * rcu_read_unlock_special().
957 *
958 * Since ctx->lock nests under rq->lock we must ensure the entire read
959 * side critical section is non-preemptible.
960 */
961 preempt_disable();
962 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200963 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200964 if (ctx) {
965 /*
966 * If this context is a clone of another, it might
967 * get swapped for another underneath us by
968 * perf_event_task_sched_out, though the
969 * rcu_read_lock() protects us from any context
970 * getting freed. Lock the context and check if it
971 * got swapped before we could get the lock, and retry
972 * if so. If we locked the right context, then it
973 * can't get swapped on us any more.
974 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100975 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200976 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100977 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200978 rcu_read_unlock();
979 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200980 goto retry;
981 }
982
983 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100984 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200985 ctx = NULL;
986 }
987 }
988 rcu_read_unlock();
Peter Zijlstra058ebd02013-07-12 11:08:33 +0200989 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200990 return ctx;
991}
992
993/*
994 * Get the context for a task and increment its pin_count so it
995 * can't get swapped to another task. This also increments its
996 * reference count so that the context can't get freed.
997 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200998static struct perf_event_context *
999perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001000{
1001 struct perf_event_context *ctx;
1002 unsigned long flags;
1003
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001004 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001005 if (ctx) {
1006 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001007 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001008 }
1009 return ctx;
1010}
1011
1012static void perf_unpin_context(struct perf_event_context *ctx)
1013{
1014 unsigned long flags;
1015
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001016 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001017 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001018 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001019}
1020
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001021/*
1022 * Update the record of the current time in a context.
1023 */
1024static void update_context_time(struct perf_event_context *ctx)
1025{
1026 u64 now = perf_clock();
1027
1028 ctx->time += now - ctx->timestamp;
1029 ctx->timestamp = now;
1030}
1031
Stephane Eranian41587552011-01-03 18:20:01 +02001032static u64 perf_event_time(struct perf_event *event)
1033{
1034 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001035
1036 if (is_cgroup_event(event))
1037 return perf_cgroup_event_time(event);
1038
Stephane Eranian41587552011-01-03 18:20:01 +02001039 return ctx ? ctx->time : 0;
1040}
1041
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001042/*
1043 * Update the total_time_enabled and total_time_running fields for a event.
Eric B Munsonb7526f02011-06-23 16:34:37 -04001044 * The caller of this function needs to hold the ctx->lock.
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001045 */
1046static void update_event_times(struct perf_event *event)
1047{
1048 struct perf_event_context *ctx = event->ctx;
1049 u64 run_end;
1050
1051 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1052 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1053 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001054 /*
1055 * in cgroup mode, time_enabled represents
1056 * the time the event was enabled AND active
1057 * tasks were in the monitored cgroup. This is
1058 * independent of the activity of the context as
1059 * there may be a mix of cgroup and non-cgroup events.
1060 *
1061 * That is why we treat cgroup events differently
1062 * here.
1063 */
1064 if (is_cgroup_event(event))
Namhyung Kim46cd6a7f2012-01-20 10:12:46 +09001065 run_end = perf_cgroup_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001066 else if (ctx->is_active)
1067 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +01001068 else
1069 run_end = event->tstamp_stopped;
1070
1071 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001072
1073 if (event->state == PERF_EVENT_STATE_INACTIVE)
1074 run_end = event->tstamp_stopped;
1075 else
Stephane Eranian41587552011-01-03 18:20:01 +02001076 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001077
1078 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001079
Peter Zijlstraf67218c2009-11-23 11:37:27 +01001080}
1081
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001082/*
1083 * Update total_time_enabled and total_time_running for all events in a group.
1084 */
1085static void update_group_times(struct perf_event *leader)
1086{
1087 struct perf_event *event;
1088
1089 update_event_times(leader);
1090 list_for_each_entry(event, &leader->sibling_list, group_entry)
1091 update_event_times(event);
1092}
1093
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001094static struct list_head *
1095ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1096{
1097 if (event->attr.pinned)
1098 return &ctx->pinned_groups;
1099 else
1100 return &ctx->flexible_groups;
1101}
1102
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001103/*
1104 * Add a event from the lists for its context.
1105 * Must be called with ctx->mutex and ctx->lock held.
1106 */
1107static void
1108list_add_event(struct perf_event *event, struct perf_event_context *ctx)
1109{
Peter Zijlstra8a495422010-05-27 15:47:49 +02001110 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1111 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001112
1113 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02001114 * If we're a stand alone event or group leader, we go to the context
1115 * list, group events are kept attached to the group so that
1116 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001117 */
Peter Zijlstra8a495422010-05-27 15:47:49 +02001118 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001119 struct list_head *list;
1120
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001121 if (is_software_event(event))
1122 event->group_flags |= PERF_GROUP_SOFTWARE;
1123
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001124 list = ctx_group_list(event, ctx);
1125 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001126 }
1127
Peter Zijlstra08309372011-03-03 11:31:20 +01001128 if (is_cgroup_event(event))
Stephane Eraniane5d13672011-02-14 11:20:01 +02001129 ctx->nr_cgroups++;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001130
Stephane Eraniand010b332012-02-09 23:21:00 +01001131 if (has_branch_stack(event))
1132 ctx->nr_branch_stack++;
1133
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001134 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001135 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001136 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001137 ctx->nr_events++;
1138 if (event->attr.inherit_stat)
1139 ctx->nr_stat++;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001140
1141 ctx->generation++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001142}
1143
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001144/*
Jiri Olsa0231bb52013-02-01 11:23:45 +01001145 * Initialize event state based on the perf_event_attr::disabled.
1146 */
1147static inline void perf_event__state_init(struct perf_event *event)
1148{
1149 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1150 PERF_EVENT_STATE_INACTIVE;
1151}
1152
1153/*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001154 * Called at perf_event creation and when events are attached/detached from a
1155 * group.
1156 */
1157static void perf_event__read_size(struct perf_event *event)
1158{
1159 int entry = sizeof(u64); /* value */
1160 int size = 0;
1161 int nr = 1;
1162
1163 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1164 size += sizeof(u64);
1165
1166 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1167 size += sizeof(u64);
1168
1169 if (event->attr.read_format & PERF_FORMAT_ID)
1170 entry += sizeof(u64);
1171
1172 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1173 nr += event->group_leader->nr_siblings;
1174 size += sizeof(u64);
1175 }
1176
1177 size += entry * nr;
1178 event->read_size = size;
1179}
1180
1181static void perf_event__header_size(struct perf_event *event)
1182{
1183 struct perf_sample_data *data;
1184 u64 sample_type = event->attr.sample_type;
1185 u16 size = 0;
1186
1187 perf_event__read_size(event);
1188
1189 if (sample_type & PERF_SAMPLE_IP)
1190 size += sizeof(data->ip);
1191
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001192 if (sample_type & PERF_SAMPLE_ADDR)
1193 size += sizeof(data->addr);
1194
1195 if (sample_type & PERF_SAMPLE_PERIOD)
1196 size += sizeof(data->period);
1197
Andi Kleenc3feedf2013-01-24 16:10:28 +01001198 if (sample_type & PERF_SAMPLE_WEIGHT)
1199 size += sizeof(data->weight);
1200
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001201 if (sample_type & PERF_SAMPLE_READ)
1202 size += event->read_size;
1203
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01001204 if (sample_type & PERF_SAMPLE_DATA_SRC)
1205 size += sizeof(data->data_src.val);
1206
Andi Kleenfdfbbd02013-09-20 07:40:39 -07001207 if (sample_type & PERF_SAMPLE_TRANSACTION)
1208 size += sizeof(data->txn);
1209
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001210 event->header_size = size;
1211}
1212
1213static void perf_event__id_header_size(struct perf_event *event)
1214{
1215 struct perf_sample_data *data;
1216 u64 sample_type = event->attr.sample_type;
1217 u16 size = 0;
1218
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001219 if (sample_type & PERF_SAMPLE_TID)
1220 size += sizeof(data->tid_entry);
1221
1222 if (sample_type & PERF_SAMPLE_TIME)
1223 size += sizeof(data->time);
1224
Adrian Hunterff3d5272013-08-27 11:23:07 +03001225 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1226 size += sizeof(data->id);
1227
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001228 if (sample_type & PERF_SAMPLE_ID)
1229 size += sizeof(data->id);
1230
1231 if (sample_type & PERF_SAMPLE_STREAM_ID)
1232 size += sizeof(data->stream_id);
1233
1234 if (sample_type & PERF_SAMPLE_CPU)
1235 size += sizeof(data->cpu_entry);
1236
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02001237 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001238}
1239
Peter Zijlstra8a495422010-05-27 15:47:49 +02001240static void perf_group_attach(struct perf_event *event)
1241{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001242 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001243
Peter Zijlstra74c33372010-10-15 11:40:29 +02001244 /*
1245 * We can have double attach due to group movement in perf_event_open.
1246 */
1247 if (event->attach_state & PERF_ATTACH_GROUP)
1248 return;
1249
Peter Zijlstra8a495422010-05-27 15:47:49 +02001250 event->attach_state |= PERF_ATTACH_GROUP;
1251
1252 if (group_leader == event)
1253 return;
1254
1255 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1256 !is_software_event(event))
1257 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1258
1259 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1260 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001261
1262 perf_event__header_size(group_leader);
1263
1264 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1265 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001266}
1267
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001268/*
1269 * Remove a event from the lists for its context.
1270 * Must be called with ctx->mutex and ctx->lock held.
1271 */
1272static void
1273list_del_event(struct perf_event *event, struct perf_event_context *ctx)
1274{
Stephane Eranian68cacd22011-03-23 16:03:06 +01001275 struct perf_cpu_context *cpuctx;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001276 /*
1277 * We can have double detach due to exit/hot-unplug + close.
1278 */
1279 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001280 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001281
1282 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1283
Stephane Eranian68cacd22011-03-23 16:03:06 +01001284 if (is_cgroup_event(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001285 ctx->nr_cgroups--;
Stephane Eranian68cacd22011-03-23 16:03:06 +01001286 cpuctx = __get_cpu_context(ctx);
1287 /*
1288 * if there are no more cgroup events
1289 * then cler cgrp to avoid stale pointer
1290 * in update_cgrp_time_from_cpuctx()
1291 */
1292 if (!ctx->nr_cgroups)
1293 cpuctx->cgrp = NULL;
1294 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02001295
Stephane Eraniand010b332012-02-09 23:21:00 +01001296 if (has_branch_stack(event))
1297 ctx->nr_branch_stack--;
1298
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001299 ctx->nr_events--;
1300 if (event->attr.inherit_stat)
1301 ctx->nr_stat--;
1302
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001303 list_del_rcu(&event->event_entry);
1304
Peter Zijlstra8a495422010-05-27 15:47:49 +02001305 if (event->group_leader == event)
1306 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001307
Peter Zijlstra96c21a42010-05-11 16:19:10 +02001308 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -08001309
1310 /*
1311 * If event was in error state, then keep it
1312 * that way, otherwise bogus counts will be
1313 * returned on read(). The only way to get out
1314 * of error state is by explicit re-enabling
1315 * of the event
1316 */
1317 if (event->state > PERF_EVENT_STATE_OFF)
1318 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02001319
1320 ctx->generation++;
Peter Zijlstra050735b2010-05-11 11:51:53 +02001321}
1322
Peter Zijlstra8a495422010-05-27 15:47:49 +02001323static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +02001324{
1325 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001326 struct list_head *list = NULL;
1327
1328 /*
1329 * We can have double detach due to exit/hot-unplug + close.
1330 */
1331 if (!(event->attach_state & PERF_ATTACH_GROUP))
1332 return;
1333
1334 event->attach_state &= ~PERF_ATTACH_GROUP;
1335
1336 /*
1337 * If this is a sibling, remove it from its group.
1338 */
1339 if (event->group_leader != event) {
1340 list_del_init(&event->group_entry);
1341 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001342 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +02001343 }
1344
1345 if (!list_empty(&event->group_entry))
1346 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +01001347
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001348 /*
1349 * If this was a group event with sibling events then
1350 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +02001351 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001352 */
1353 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +02001354 if (list)
1355 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001356 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001357
1358 /* Inherit group flags from the previous leader */
1359 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001360 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02001361
1362out:
1363 perf_event__header_size(event->group_leader);
1364
1365 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1366 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001367}
1368
Stephane Eranianfa66f072010-08-26 16:40:01 +02001369static inline int
1370event_filter_match(struct perf_event *event)
1371{
Stephane Eraniane5d13672011-02-14 11:20:01 +02001372 return (event->cpu == -1 || event->cpu == smp_processor_id())
1373 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001374}
1375
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001376static void
1377event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001378 struct perf_cpu_context *cpuctx,
1379 struct perf_event_context *ctx)
1380{
Stephane Eranian41587552011-01-03 18:20:01 +02001381 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001382 u64 delta;
1383 /*
1384 * An event which could not be activated because of
1385 * filter mismatch still needs to have its timings
1386 * maintained, otherwise bogus information is return
1387 * via read() for time_enabled, time_running:
1388 */
1389 if (event->state == PERF_EVENT_STATE_INACTIVE
1390 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001391 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001392 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001393 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001394 }
1395
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001396 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001397 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001398
1399 event->state = PERF_EVENT_STATE_INACTIVE;
1400 if (event->pending_disable) {
1401 event->pending_disable = 0;
1402 event->state = PERF_EVENT_STATE_OFF;
1403 }
Stephane Eranian41587552011-01-03 18:20:01 +02001404 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001405 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001406 event->oncpu = -1;
1407
1408 if (!is_software_event(event))
1409 cpuctx->active_oncpu--;
1410 ctx->nr_active--;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001411 if (event->attr.freq && event->attr.sample_freq)
1412 ctx->nr_freq--;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001413 if (event->attr.exclusive || !cpuctx->active_oncpu)
1414 cpuctx->exclusive = 0;
1415}
1416
1417static void
1418group_sched_out(struct perf_event *group_event,
1419 struct perf_cpu_context *cpuctx,
1420 struct perf_event_context *ctx)
1421{
1422 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001423 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001424
1425 event_sched_out(group_event, cpuctx, ctx);
1426
1427 /*
1428 * Schedule out siblings (if any):
1429 */
1430 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1431 event_sched_out(event, cpuctx, ctx);
1432
Stephane Eranianfa66f072010-08-26 16:40:01 +02001433 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001434 cpuctx->exclusive = 0;
1435}
1436
1437/*
1438 * Cross CPU call to remove a performance event
1439 *
1440 * We disable the event on the hardware level first. After that we
1441 * remove it from the context list.
1442 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001443static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001444{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001445 struct perf_event *event = info;
1446 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001447 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001448
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001449 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001450 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001451 list_del_event(event, ctx);
Peter Zijlstra64ce3122011-04-09 21:17:48 +02001452 if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
1453 ctx->is_active = 0;
1454 cpuctx->task_ctx = NULL;
1455 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001456 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001457
1458 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001459}
1460
1461
1462/*
1463 * Remove the event from a task's (or a CPU's) list of events.
1464 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001465 * CPU events are removed with a smp call. For task events we only
1466 * call when the task is on a CPU.
1467 *
1468 * If event->ctx is a cloned context, callers must make sure that
1469 * every task struct that event->ctx->task could possibly point to
1470 * remains valid. This is OK when called from perf_release since
1471 * that only calls us on the top-level context, which can't be a clone.
1472 * When called from perf_event_exit_task, it's OK because the
1473 * context has been detached from its task.
1474 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001475static void perf_remove_from_context(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001476{
1477 struct perf_event_context *ctx = event->ctx;
1478 struct task_struct *task = ctx->task;
1479
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001480 lockdep_assert_held(&ctx->mutex);
1481
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001482 if (!task) {
1483 /*
1484 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001485 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001486 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001487 cpu_function_call(event->cpu, __perf_remove_from_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001488 return;
1489 }
1490
1491retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001492 if (!task_function_call(task, __perf_remove_from_context, event))
1493 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001494
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001495 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001496 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001497 * If we failed to find a running task, but find the context active now
1498 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001499 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001500 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001501 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001502 goto retry;
1503 }
1504
1505 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001506 * Since the task isn't running, its safe to remove the event, us
1507 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001508 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001509 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001510 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001511}
1512
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001513/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001514 * Cross CPU call to disable a performance event
1515 */
K.Prasad500ad2d2012-08-02 13:46:35 +05301516int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001517{
1518 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001519 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001520 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001521
1522 /*
1523 * If this is a per-task event, need to check whether this
1524 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001525 *
1526 * Can trigger due to concurrent perf_event_context_sched_out()
1527 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001528 */
1529 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001530 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001531
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001532 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001533
1534 /*
1535 * If the event is on, turn it off.
1536 * If it is in error state, leave it in error state.
1537 */
1538 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1539 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001540 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001541 update_group_times(event);
1542 if (event == event->group_leader)
1543 group_sched_out(event, cpuctx, ctx);
1544 else
1545 event_sched_out(event, cpuctx, ctx);
1546 event->state = PERF_EVENT_STATE_OFF;
1547 }
1548
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001549 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001550
1551 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001552}
1553
1554/*
1555 * Disable a event.
1556 *
1557 * If event->ctx is a cloned context, callers must make sure that
1558 * every task struct that event->ctx->task could possibly point to
1559 * remains valid. This condition is satisifed when called through
1560 * perf_event_for_each_child or perf_event_for_each because they
1561 * hold the top-level event's child_mutex, so any descendant that
1562 * goes to exit will block in sync_child_event.
1563 * When called from perf_pending_event it's OK because event->ctx
1564 * is the current context on this CPU and preemption is disabled,
1565 * hence we can't get into perf_event_task_sched_out for this context.
1566 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001567void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001568{
1569 struct perf_event_context *ctx = event->ctx;
1570 struct task_struct *task = ctx->task;
1571
1572 if (!task) {
1573 /*
1574 * Disable the event on the cpu that it's on
1575 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001576 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001577 return;
1578 }
1579
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001580retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001581 if (!task_function_call(task, __perf_event_disable, event))
1582 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001583
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001584 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001585 /*
1586 * If the event is still active, we need to retry the cross-call.
1587 */
1588 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001589 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001590 /*
1591 * Reload the task pointer, it might have been changed by
1592 * a concurrent perf_event_context_sched_out().
1593 */
1594 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001595 goto retry;
1596 }
1597
1598 /*
1599 * Since we have the lock this context can't be scheduled
1600 * in, so we can change the state safely.
1601 */
1602 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1603 update_group_times(event);
1604 event->state = PERF_EVENT_STATE_OFF;
1605 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001606 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001607}
Robert Richterdcfce4a2011-10-11 17:11:08 +02001608EXPORT_SYMBOL_GPL(perf_event_disable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001609
Stephane Eraniane5d13672011-02-14 11:20:01 +02001610static void perf_set_shadow_time(struct perf_event *event,
1611 struct perf_event_context *ctx,
1612 u64 tstamp)
1613{
1614 /*
1615 * use the correct time source for the time snapshot
1616 *
1617 * We could get by without this by leveraging the
1618 * fact that to get to this function, the caller
1619 * has most likely already called update_context_time()
1620 * and update_cgrp_time_xx() and thus both timestamp
1621 * are identical (or very close). Given that tstamp is,
1622 * already adjusted for cgroup, we could say that:
1623 * tstamp - ctx->timestamp
1624 * is equivalent to
1625 * tstamp - cgrp->timestamp.
1626 *
1627 * Then, in perf_output_read(), the calculation would
1628 * work with no changes because:
1629 * - event is guaranteed scheduled in
1630 * - no scheduled out in between
1631 * - thus the timestamp would be the same
1632 *
1633 * But this is a bit hairy.
1634 *
1635 * So instead, we have an explicit cgroup call to remain
1636 * within the time time source all along. We believe it
1637 * is cleaner and simpler to understand.
1638 */
1639 if (is_cgroup_event(event))
1640 perf_cgroup_set_shadow_time(event, tstamp);
1641 else
1642 event->shadow_ctx_time = tstamp - ctx->timestamp;
1643}
1644
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001645#define MAX_INTERRUPTS (~0ULL)
1646
1647static void perf_log_throttle(struct perf_event *event, int enable);
1648
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001649static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001650event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001651 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001652 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001653{
Stephane Eranian41587552011-01-03 18:20:01 +02001654 u64 tstamp = perf_event_time(event);
1655
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001656 if (event->state <= PERF_EVENT_STATE_OFF)
1657 return 0;
1658
1659 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001660 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001661
1662 /*
1663 * Unthrottle events, since we scheduled we might have missed several
1664 * ticks already, also for a heavily scheduling task there is little
1665 * guarantee it'll get a tick in a timely manner.
1666 */
1667 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1668 perf_log_throttle(event, 1);
1669 event->hw.interrupts = 0;
1670 }
1671
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001672 /*
1673 * The new state must be visible before we turn it on in the hardware:
1674 */
1675 smp_wmb();
1676
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001677 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001678 event->state = PERF_EVENT_STATE_INACTIVE;
1679 event->oncpu = -1;
1680 return -EAGAIN;
1681 }
1682
Stephane Eranian41587552011-01-03 18:20:01 +02001683 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001684
Stephane Eraniane5d13672011-02-14 11:20:01 +02001685 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001686
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001687 if (!is_software_event(event))
1688 cpuctx->active_oncpu++;
1689 ctx->nr_active++;
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01001690 if (event->attr.freq && event->attr.sample_freq)
1691 ctx->nr_freq++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001692
1693 if (event->attr.exclusive)
1694 cpuctx->exclusive = 1;
1695
1696 return 0;
1697}
1698
1699static int
1700group_sched_in(struct perf_event *group_event,
1701 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001702 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001703{
Lin Ming6bde9b62010-04-23 13:56:00 +08001704 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001705 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001706 u64 now = ctx->time;
1707 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001708
1709 if (group_event->state == PERF_EVENT_STATE_OFF)
1710 return 0;
1711
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001712 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001713
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001714 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001715 pmu->cancel_txn(pmu);
Stephane Eranian9e630202013-04-03 14:21:33 +02001716 perf_cpu_hrtimer_restart(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001717 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +02001718 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001719
1720 /*
1721 * Schedule in siblings as one group (if any):
1722 */
1723 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001724 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001725 partial_group = event;
1726 goto group_error;
1727 }
1728 }
1729
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001730 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001731 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001732
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001733group_error:
1734 /*
1735 * Groups can be scheduled in as one unit only, so undo any
1736 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001737 * The events up to the failed event are scheduled out normally,
1738 * tstamp_stopped will be updated.
1739 *
1740 * The failed events and the remaining siblings need to have
1741 * their timings updated as if they had gone thru event_sched_in()
1742 * and event_sched_out(). This is required to get consistent timings
1743 * across the group. This also takes care of the case where the group
1744 * could never be scheduled by ensuring tstamp_stopped is set to mark
1745 * the time the event was actually stopped, such that time delta
1746 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001747 */
1748 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1749 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001750 simulate = true;
1751
1752 if (simulate) {
1753 event->tstamp_running += now - event->tstamp_stopped;
1754 event->tstamp_stopped = now;
1755 } else {
1756 event_sched_out(event, cpuctx, ctx);
1757 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001758 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001759 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001760
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001761 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +02001762
Stephane Eranian9e630202013-04-03 14:21:33 +02001763 perf_cpu_hrtimer_restart(cpuctx);
1764
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001765 return -EAGAIN;
1766}
1767
1768/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001769 * Work out whether we can put this event group on the CPU now.
1770 */
1771static int group_can_go_on(struct perf_event *event,
1772 struct perf_cpu_context *cpuctx,
1773 int can_add_hw)
1774{
1775 /*
1776 * Groups consisting entirely of software events can always go on.
1777 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001778 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001779 return 1;
1780 /*
1781 * If an exclusive group is already on, no other hardware
1782 * events can go on.
1783 */
1784 if (cpuctx->exclusive)
1785 return 0;
1786 /*
1787 * If this group is exclusive and there are already
1788 * events on the CPU, it can't go on.
1789 */
1790 if (event->attr.exclusive && cpuctx->active_oncpu)
1791 return 0;
1792 /*
1793 * Otherwise, try to add it if all previous groups were able
1794 * to go on.
1795 */
1796 return can_add_hw;
1797}
1798
1799static void add_event_to_ctx(struct perf_event *event,
1800 struct perf_event_context *ctx)
1801{
Stephane Eranian41587552011-01-03 18:20:01 +02001802 u64 tstamp = perf_event_time(event);
1803
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001804 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001805 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001806 event->tstamp_enabled = tstamp;
1807 event->tstamp_running = tstamp;
1808 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001809}
1810
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001811static void task_ctx_sched_out(struct perf_event_context *ctx);
1812static void
1813ctx_sched_in(struct perf_event_context *ctx,
1814 struct perf_cpu_context *cpuctx,
1815 enum event_type_t event_type,
1816 struct task_struct *task);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001817
Peter Zijlstradce58552011-04-09 21:17:46 +02001818static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
1819 struct perf_event_context *ctx,
1820 struct task_struct *task)
1821{
1822 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
1823 if (ctx)
1824 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
1825 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
1826 if (ctx)
1827 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
1828}
1829
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001830/*
1831 * Cross CPU call to install and enable a performance event
1832 *
1833 * Must be called with ctx->mutex held
1834 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001835static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001836{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001837 struct perf_event *event = info;
1838 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001839 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001840 struct perf_event_context *task_ctx = cpuctx->task_ctx;
1841 struct task_struct *task = current;
1842
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001843 perf_ctx_lock(cpuctx, task_ctx);
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001844 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001845
1846 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001847 * If there was an active task_ctx schedule it out.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001848 */
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001849 if (task_ctx)
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001850 task_ctx_sched_out(task_ctx);
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001851
1852 /*
1853 * If the context we're installing events in is not the
1854 * active task_ctx, flip them.
1855 */
1856 if (ctx->task && task_ctx != ctx) {
1857 if (task_ctx)
1858 raw_spin_unlock(&task_ctx->lock);
1859 raw_spin_lock(&ctx->lock);
1860 task_ctx = ctx;
1861 }
1862
1863 if (task_ctx) {
1864 cpuctx->task_ctx = task_ctx;
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001865 task = task_ctx->task;
1866 }
Peter Zijlstrab58f6b02011-06-07 00:23:28 +02001867
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001868 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001869
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001870 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001871 /*
1872 * update cgrp time only if current cgrp
1873 * matches event->cgrp. Must be done before
1874 * calling add_event_to_ctx()
1875 */
1876 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001877
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001878 add_event_to_ctx(event, ctx);
1879
1880 /*
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001881 * Schedule everything back in
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001882 */
Peter Zijlstradce58552011-04-09 21:17:46 +02001883 perf_event_sched_in(cpuctx, task_ctx, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001884
Peter Zijlstra2c29ef02011-04-09 21:17:44 +02001885 perf_pmu_enable(cpuctx->ctx.pmu);
1886 perf_ctx_unlock(cpuctx, task_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001887
1888 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001889}
1890
1891/*
1892 * Attach a performance event to a context
1893 *
1894 * First we add the event to the list with the hardware enable bit
1895 * in event->hw_config cleared.
1896 *
1897 * If the event is attached to a task which is on a CPU we use a smp
1898 * call to enable it in the task context. The task might have been
1899 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001900 */
1901static void
1902perf_install_in_context(struct perf_event_context *ctx,
1903 struct perf_event *event,
1904 int cpu)
1905{
1906 struct task_struct *task = ctx->task;
1907
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001908 lockdep_assert_held(&ctx->mutex);
1909
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001910 event->ctx = ctx;
Yan, Zheng0cda4c02012-06-15 14:31:33 +08001911 if (event->cpu != -1)
1912 event->cpu = cpu;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001913
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001914 if (!task) {
1915 /*
1916 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001917 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001918 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001919 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001920 return;
1921 }
1922
1923retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001924 if (!task_function_call(task, __perf_install_in_context, event))
1925 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001926
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001927 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001928 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001929 * If we failed to find a running task, but find the context active now
1930 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001931 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001932 if (ctx->is_active) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001933 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001934 goto retry;
1935 }
1936
1937 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001938 * Since the task isn't running, its safe to add the event, us holding
1939 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001940 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001941 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001942 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001943}
1944
1945/*
1946 * Put a event into inactive state and update time fields.
1947 * Enabling the leader of a group effectively enables all
1948 * the group members that aren't explicitly disabled, so we
1949 * have to update their ->tstamp_enabled also.
1950 * Note: this works for group members as well as group leaders
1951 * since the non-leader members' sibling_lists will be empty.
1952 */
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01001953static void __perf_event_mark_enabled(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001954{
1955 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001956 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001957
1958 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001959 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001960 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001961 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1962 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001963 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001964}
1965
1966/*
1967 * Cross CPU call to enable a performance event
1968 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001969static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001970{
1971 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001972 struct perf_event_context *ctx = event->ctx;
1973 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001974 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001975 int err;
1976
Jiri Olsa06f41792013-07-09 17:44:11 +02001977 /*
1978 * There's a time window between 'ctx->is_active' check
1979 * in perf_event_enable function and this place having:
1980 * - IRQs on
1981 * - ctx->lock unlocked
1982 *
1983 * where the task could be killed and 'ctx' deactivated
1984 * by perf_event_exit_task.
1985 */
1986 if (!ctx->is_active)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001987 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001988
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001989 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001990 update_context_time(ctx);
1991
1992 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1993 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001994
1995 /*
1996 * set current task's cgroup time reference point
1997 */
Stephane Eranian3f7cce32011-02-18 14:40:01 +02001998 perf_cgroup_set_timestamp(current, ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001999
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002000 __perf_event_mark_enabled(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002001
Stephane Eraniane5d13672011-02-14 11:20:01 +02002002 if (!event_filter_match(event)) {
2003 if (is_cgroup_event(event))
2004 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002005 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002006 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002007
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002008 /*
2009 * If the event is in a group and isn't the group leader,
2010 * then don't put it on unless the group is on.
2011 */
2012 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
2013 goto unlock;
2014
2015 if (!group_can_go_on(event, cpuctx, 1)) {
2016 err = -EEXIST;
2017 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002018 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002019 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002020 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01002021 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002022 }
2023
2024 if (err) {
2025 /*
2026 * If this event can't go on and it's part of a
2027 * group, then the whole group has to come off.
2028 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002029 if (leader != event) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002030 group_sched_out(leader, cpuctx, ctx);
Stephane Eranian9e630202013-04-03 14:21:33 +02002031 perf_cpu_hrtimer_restart(cpuctx);
2032 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002033 if (leader->attr.pinned) {
2034 update_group_times(leader);
2035 leader->state = PERF_EVENT_STATE_ERROR;
2036 }
2037 }
2038
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002039unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002040 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002041
2042 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002043}
2044
2045/*
2046 * Enable a event.
2047 *
2048 * If event->ctx is a cloned context, callers must make sure that
2049 * every task struct that event->ctx->task could possibly point to
2050 * remains valid. This condition is satisfied when called through
2051 * perf_event_for_each_child or perf_event_for_each as described
2052 * for perf_event_disable.
2053 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01002054void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002055{
2056 struct perf_event_context *ctx = event->ctx;
2057 struct task_struct *task = ctx->task;
2058
2059 if (!task) {
2060 /*
2061 * Enable the event on the cpu that it's on
2062 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002063 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002064 return;
2065 }
2066
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002067 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002068 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2069 goto out;
2070
2071 /*
2072 * If the event is in error state, clear that first.
2073 * That way, if we see the event in error state below, we
2074 * know that it has gone back into error state, as distinct
2075 * from the task having been scheduled away before the
2076 * cross-call arrived.
2077 */
2078 if (event->state == PERF_EVENT_STATE_ERROR)
2079 event->state = PERF_EVENT_STATE_OFF;
2080
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002081retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002082 if (!ctx->is_active) {
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002083 __perf_event_mark_enabled(event);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002084 goto out;
2085 }
2086
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002087 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002088
2089 if (!task_function_call(task, __perf_event_enable, event))
2090 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002091
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002092 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002093
2094 /*
2095 * If the context is active and the event is still off,
2096 * we need to retry the cross-call.
2097 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002098 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
2099 /*
2100 * task could have been flipped by a concurrent
2101 * perf_event_context_sched_out()
2102 */
2103 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002104 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002105 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002106
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002107out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002108 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002109}
Robert Richterdcfce4a2011-10-11 17:11:08 +02002110EXPORT_SYMBOL_GPL(perf_event_enable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002111
Avi Kivity26ca5c12011-06-29 18:42:37 +03002112int perf_event_refresh(struct perf_event *event, int refresh)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002113{
2114 /*
2115 * not supported on inherited events
2116 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01002117 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002118 return -EINVAL;
2119
2120 atomic_add(refresh, &event->event_limit);
2121 perf_event_enable(event);
2122
2123 return 0;
2124}
Avi Kivity26ca5c12011-06-29 18:42:37 +03002125EXPORT_SYMBOL_GPL(perf_event_refresh);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002126
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002127static void ctx_sched_out(struct perf_event_context *ctx,
2128 struct perf_cpu_context *cpuctx,
2129 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002130{
2131 struct perf_event *event;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002132 int is_active = ctx->is_active;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002133
Peter Zijlstradb24d332011-04-09 21:17:45 +02002134 ctx->is_active &= ~event_type;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002135 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002136 return;
2137
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002138 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002139 update_cgrp_time_from_cpuctx(cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002140 if (!ctx->nr_active)
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002141 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002142
Peter Zijlstra075e0b02011-04-09 21:17:40 +02002143 perf_pmu_disable(ctx->pmu);
Peter Zijlstradb24d332011-04-09 21:17:45 +02002144 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002145 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2146 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002147 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002148
Peter Zijlstradb24d332011-04-09 21:17:45 +02002149 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002150 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002151 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002152 }
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002153 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002154}
2155
2156/*
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002157 * Test whether two contexts are equivalent, i.e. whether they have both been
2158 * cloned from the same version of the same context.
2159 *
2160 * Equivalence is measured using a generation number in the context that is
2161 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2162 * and list_del_event().
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002163 */
2164static int context_equiv(struct perf_event_context *ctx1,
2165 struct perf_event_context *ctx2)
2166{
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002167 /* Pinning disables the swap optimization */
2168 if (ctx1->pin_count || ctx2->pin_count)
2169 return 0;
2170
2171 /* If ctx1 is the parent of ctx2 */
2172 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2173 return 1;
2174
2175 /* If ctx2 is the parent of ctx1 */
2176 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2177 return 1;
2178
2179 /*
2180 * If ctx1 and ctx2 have the same parent; we flatten the parent
2181 * hierarchy, see perf_event_init_context().
2182 */
2183 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2184 ctx1->parent_gen == ctx2->parent_gen)
2185 return 1;
2186
2187 /* Unmatched */
2188 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002189}
2190
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002191static void __perf_event_sync_stat(struct perf_event *event,
2192 struct perf_event *next_event)
2193{
2194 u64 value;
2195
2196 if (!event->attr.inherit_stat)
2197 return;
2198
2199 /*
2200 * Update the event value, we cannot use perf_event_read()
2201 * because we're in the middle of a context switch and have IRQs
2202 * disabled, which upsets smp_call_function_single(), however
2203 * we know the event must be on the current CPU, therefore we
2204 * don't need to use it.
2205 */
2206 switch (event->state) {
2207 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01002208 event->pmu->read(event);
2209 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002210
2211 case PERF_EVENT_STATE_INACTIVE:
2212 update_event_times(event);
2213 break;
2214
2215 default:
2216 break;
2217 }
2218
2219 /*
2220 * In order to keep per-task stats reliable we need to flip the event
2221 * values when we flip the contexts.
2222 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02002223 value = local64_read(&next_event->count);
2224 value = local64_xchg(&event->count, value);
2225 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002226
2227 swap(event->total_time_enabled, next_event->total_time_enabled);
2228 swap(event->total_time_running, next_event->total_time_running);
2229
2230 /*
2231 * Since we swizzled the values, update the user visible data too.
2232 */
2233 perf_event_update_userpage(event);
2234 perf_event_update_userpage(next_event);
2235}
2236
2237#define list_next_entry(pos, member) \
2238 list_entry(pos->member.next, typeof(*pos), member)
2239
2240static void perf_event_sync_stat(struct perf_event_context *ctx,
2241 struct perf_event_context *next_ctx)
2242{
2243 struct perf_event *event, *next_event;
2244
2245 if (!ctx->nr_stat)
2246 return;
2247
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01002248 update_context_time(ctx);
2249
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002250 event = list_first_entry(&ctx->event_list,
2251 struct perf_event, event_entry);
2252
2253 next_event = list_first_entry(&next_ctx->event_list,
2254 struct perf_event, event_entry);
2255
2256 while (&event->event_entry != &ctx->event_list &&
2257 &next_event->event_entry != &next_ctx->event_list) {
2258
2259 __perf_event_sync_stat(event, next_event);
2260
2261 event = list_next_entry(event, event_entry);
2262 next_event = list_next_entry(next_event, event_entry);
2263 }
2264}
2265
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002266static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2267 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002268{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002269 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002270 struct perf_event_context *next_ctx;
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002271 struct perf_event_context *parent, *next_parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002272 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002273 int do_switch = 1;
2274
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002275 if (likely(!ctx))
2276 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002277
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002278 cpuctx = __get_cpu_context(ctx);
2279 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002280 return;
2281
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002282 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002283 next_ctx = next->perf_event_ctxp[ctxn];
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002284 if (!next_ctx)
2285 goto unlock;
2286
2287 parent = rcu_dereference(ctx->parent_ctx);
2288 next_parent = rcu_dereference(next_ctx->parent_ctx);
2289
2290 /* If neither context have a parent context; they cannot be clones. */
2291 if (!parent && !next_parent)
2292 goto unlock;
2293
2294 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002295 /*
2296 * Looks like the two contexts are clones, so we might be
2297 * able to optimize the context switch. We lock both
2298 * contexts and check that they are clones under the
2299 * lock (including re-checking that neither has been
2300 * uncloned in the meantime). It doesn't matter which
2301 * order we take the locks because no other cpu could
2302 * be trying to lock both of these tasks.
2303 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002304 raw_spin_lock(&ctx->lock);
2305 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002306 if (context_equiv(ctx, next_ctx)) {
2307 /*
2308 * XXX do we need a memory barrier of sorts
2309 * wrt to rcu_dereference() of perf_event_ctxp
2310 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002311 task->perf_event_ctxp[ctxn] = next_ctx;
2312 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002313 ctx->task = next;
2314 next_ctx->task = task;
2315 do_switch = 0;
2316
2317 perf_event_sync_stat(ctx, next_ctx);
2318 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002319 raw_spin_unlock(&next_ctx->lock);
2320 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002321 }
Peter Zijlstra5a3126d2013-10-07 17:12:48 +02002322unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002323 rcu_read_unlock();
2324
2325 if (do_switch) {
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002326 raw_spin_lock(&ctx->lock);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002327 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002328 cpuctx->task_ctx = NULL;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002329 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002330 }
2331}
2332
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002333#define for_each_task_context_nr(ctxn) \
2334 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2335
2336/*
2337 * Called from scheduler to remove the events of the current task,
2338 * with interrupts disabled.
2339 *
2340 * We stop each event and update the event value in event->count.
2341 *
2342 * This does not protect us against NMI, but disable()
2343 * sets the disabled bit in the control field of event _before_
2344 * accessing the event control register. If a NMI hits, then it will
2345 * not restart the event.
2346 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002347void __perf_event_task_sched_out(struct task_struct *task,
2348 struct task_struct *next)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002349{
2350 int ctxn;
2351
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002352 for_each_task_context_nr(ctxn)
2353 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002354
2355 /*
2356 * if cgroup events exist on this CPU, then we need
2357 * to check if we have to switch out PMU state.
2358 * cgroup event are system-wide mode only
2359 */
2360 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002361 perf_cgroup_sched_out(task, next);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002362}
2363
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002364static void task_ctx_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002365{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002366 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002367
2368 if (!cpuctx->task_ctx)
2369 return;
2370
2371 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2372 return;
2373
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002374 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002375 cpuctx->task_ctx = NULL;
2376}
2377
2378/*
2379 * Called with IRQs disabled
2380 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002381static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2382 enum event_type_t event_type)
2383{
2384 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002385}
2386
2387static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002388ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002389 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002390{
2391 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002392
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002393 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2394 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002395 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02002396 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002397 continue;
2398
Stephane Eraniane5d13672011-02-14 11:20:01 +02002399 /* may need to reset tstamp_enabled */
2400 if (is_cgroup_event(event))
2401 perf_cgroup_mark_enabled(event, ctx);
2402
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08002403 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002404 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002405
2406 /*
2407 * If this pinned group hasn't been scheduled,
2408 * put it in error state.
2409 */
2410 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2411 update_group_times(event);
2412 event->state = PERF_EVENT_STATE_ERROR;
2413 }
2414 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002415}
2416
2417static void
2418ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01002419 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002420{
2421 struct perf_event *event;
2422 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002423
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002424 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2425 /* Ignore events in OFF or ERROR state */
2426 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002427 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002428 /*
2429 * Listen to the 'cpu' scheduling filter constraint
2430 * of events:
2431 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002432 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002433 continue;
2434
Stephane Eraniane5d13672011-02-14 11:20:01 +02002435 /* may need to reset tstamp_enabled */
2436 if (is_cgroup_event(event))
2437 perf_cgroup_mark_enabled(event, ctx);
2438
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002439 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002440 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002441 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002442 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002443 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002444}
2445
2446static void
2447ctx_sched_in(struct perf_event_context *ctx,
2448 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002449 enum event_type_t event_type,
2450 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002451{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002452 u64 now;
Peter Zijlstradb24d332011-04-09 21:17:45 +02002453 int is_active = ctx->is_active;
Stephane Eraniane5d13672011-02-14 11:20:01 +02002454
Peter Zijlstradb24d332011-04-09 21:17:45 +02002455 ctx->is_active |= event_type;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002456 if (likely(!ctx->nr_events))
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002457 return;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002458
Stephane Eraniane5d13672011-02-14 11:20:01 +02002459 now = perf_clock();
2460 ctx->timestamp = now;
Stephane Eranian3f7cce32011-02-18 14:40:01 +02002461 perf_cgroup_set_timestamp(task, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002462 /*
2463 * First go through the list and put on any pinned groups
2464 * in order to give them the best chance of going on.
2465 */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002466 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002467 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002468
2469 /* Then walk through the lower prio flexible groups */
Peter Zijlstradb24d332011-04-09 21:17:45 +02002470 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
Peter Zijlstra6e377382010-02-11 13:21:58 +01002471 ctx_flexible_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002472}
2473
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002474static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002475 enum event_type_t event_type,
2476 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002477{
2478 struct perf_event_context *ctx = &cpuctx->ctx;
2479
Stephane Eraniane5d13672011-02-14 11:20:01 +02002480 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002481}
2482
Stephane Eraniane5d13672011-02-14 11:20:01 +02002483static void perf_event_context_sched_in(struct perf_event_context *ctx,
2484 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002485{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002486 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002487
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002488 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002489 if (cpuctx->task_ctx == ctx)
2490 return;
2491
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002492 perf_ctx_lock(cpuctx, ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002493 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002494 /*
2495 * We want to keep the following priority order:
2496 * cpu pinned (that don't need to move), task pinned,
2497 * cpu flexible, task flexible.
2498 */
2499 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2500
Gleb Natapov1d5f0032011-10-23 19:10:33 +02002501 if (ctx->nr_events)
2502 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002503
Gleb Natapov86b47c22011-11-22 16:08:21 +02002504 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
2505
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002506 perf_pmu_enable(ctx->pmu);
2507 perf_ctx_unlock(cpuctx, ctx);
2508
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002509 /*
2510 * Since these rotations are per-cpu, we need to ensure the
2511 * cpu-context we got scheduled on is actually rotating.
2512 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002513 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002514}
2515
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002516/*
Stephane Eraniand010b332012-02-09 23:21:00 +01002517 * When sampling the branck stack in system-wide, it may be necessary
2518 * to flush the stack on context switch. This happens when the branch
2519 * stack does not tag its entries with the pid of the current task.
2520 * Otherwise it becomes impossible to associate a branch entry with a
2521 * task. This ambiguity is more likely to appear when the branch stack
2522 * supports priv level filtering and the user sets it to monitor only
2523 * at the user level (which could be a useful measurement in system-wide
2524 * mode). In that case, the risk is high of having a branch stack with
2525 * branch from multiple tasks. Flushing may mean dropping the existing
2526 * entries or stashing them somewhere in the PMU specific code layer.
2527 *
2528 * This function provides the context switch callback to the lower code
2529 * layer. It is invoked ONLY when there is at least one system-wide context
2530 * with at least one active event using taken branch sampling.
2531 */
2532static void perf_branch_stack_sched_in(struct task_struct *prev,
2533 struct task_struct *task)
2534{
2535 struct perf_cpu_context *cpuctx;
2536 struct pmu *pmu;
2537 unsigned long flags;
2538
2539 /* no need to flush branch stack if not changing task */
2540 if (prev == task)
2541 return;
2542
2543 local_irq_save(flags);
2544
2545 rcu_read_lock();
2546
2547 list_for_each_entry_rcu(pmu, &pmus, entry) {
2548 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2549
2550 /*
2551 * check if the context has at least one
2552 * event using PERF_SAMPLE_BRANCH_STACK
2553 */
2554 if (cpuctx->ctx.nr_branch_stack > 0
2555 && pmu->flush_branch_stack) {
2556
2557 pmu = cpuctx->ctx.pmu;
2558
2559 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2560
2561 perf_pmu_disable(pmu);
2562
2563 pmu->flush_branch_stack();
2564
2565 perf_pmu_enable(pmu);
2566
2567 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2568 }
2569 }
2570
2571 rcu_read_unlock();
2572
2573 local_irq_restore(flags);
2574}
2575
2576/*
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002577 * Called from scheduler to add the events of the current task
2578 * with interrupts disabled.
2579 *
2580 * We restore the event value and then enable it.
2581 *
2582 * This does not protect us against NMI, but enable()
2583 * sets the enabled bit in the control field of event _before_
2584 * accessing the event control register. If a NMI hits, then it will
2585 * keep the event running.
2586 */
Jiri Olsaab0cce52012-05-23 13:13:02 +02002587void __perf_event_task_sched_in(struct task_struct *prev,
2588 struct task_struct *task)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002589{
2590 struct perf_event_context *ctx;
2591 int ctxn;
2592
2593 for_each_task_context_nr(ctxn) {
2594 ctx = task->perf_event_ctxp[ctxn];
2595 if (likely(!ctx))
2596 continue;
2597
Stephane Eraniane5d13672011-02-14 11:20:01 +02002598 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002599 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002600 /*
2601 * if cgroup events exist on this CPU, then we need
2602 * to check if we have to switch in PMU state.
2603 * cgroup event are system-wide mode only
2604 */
2605 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002606 perf_cgroup_sched_in(prev, task);
Stephane Eraniand010b332012-02-09 23:21:00 +01002607
2608 /* check for system-wide branch_stack events */
2609 if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
2610 perf_branch_stack_sched_in(prev, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002611}
2612
Peter Zijlstraabd50712010-01-26 18:50:16 +01002613static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2614{
2615 u64 frequency = event->attr.sample_freq;
2616 u64 sec = NSEC_PER_SEC;
2617 u64 divisor, dividend;
2618
2619 int count_fls, nsec_fls, frequency_fls, sec_fls;
2620
2621 count_fls = fls64(count);
2622 nsec_fls = fls64(nsec);
2623 frequency_fls = fls64(frequency);
2624 sec_fls = 30;
2625
2626 /*
2627 * We got @count in @nsec, with a target of sample_freq HZ
2628 * the target period becomes:
2629 *
2630 * @count * 10^9
2631 * period = -------------------
2632 * @nsec * sample_freq
2633 *
2634 */
2635
2636 /*
2637 * Reduce accuracy by one bit such that @a and @b converge
2638 * to a similar magnitude.
2639 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002640#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002641do { \
2642 if (a##_fls > b##_fls) { \
2643 a >>= 1; \
2644 a##_fls--; \
2645 } else { \
2646 b >>= 1; \
2647 b##_fls--; \
2648 } \
2649} while (0)
2650
2651 /*
2652 * Reduce accuracy until either term fits in a u64, then proceed with
2653 * the other, so that finally we can do a u64/u64 division.
2654 */
2655 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2656 REDUCE_FLS(nsec, frequency);
2657 REDUCE_FLS(sec, count);
2658 }
2659
2660 if (count_fls + sec_fls > 64) {
2661 divisor = nsec * frequency;
2662
2663 while (count_fls + sec_fls > 64) {
2664 REDUCE_FLS(count, sec);
2665 divisor >>= 1;
2666 }
2667
2668 dividend = count * sec;
2669 } else {
2670 dividend = count * sec;
2671
2672 while (nsec_fls + frequency_fls > 64) {
2673 REDUCE_FLS(nsec, frequency);
2674 dividend >>= 1;
2675 }
2676
2677 divisor = nsec * frequency;
2678 }
2679
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02002680 if (!divisor)
2681 return dividend;
2682
Peter Zijlstraabd50712010-01-26 18:50:16 +01002683 return div64_u64(dividend, divisor);
2684}
2685
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002686static DEFINE_PER_CPU(int, perf_throttled_count);
2687static DEFINE_PER_CPU(u64, perf_throttled_seq);
2688
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002689static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002690{
2691 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02002692 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002693 s64 delta;
2694
Peter Zijlstraabd50712010-01-26 18:50:16 +01002695 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002696
2697 delta = (s64)(period - hwc->sample_period);
2698 delta = (delta + 7) / 8; /* low pass filter */
2699
2700 sample_period = hwc->sample_period + delta;
2701
2702 if (!sample_period)
2703 sample_period = 1;
2704
2705 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002706
Peter Zijlstrae7850592010-05-21 14:43:08 +02002707 if (local64_read(&hwc->period_left) > 8*sample_period) {
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002708 if (disable)
2709 event->pmu->stop(event, PERF_EF_UPDATE);
2710
Peter Zijlstrae7850592010-05-21 14:43:08 +02002711 local64_set(&hwc->period_left, 0);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002712
2713 if (disable)
2714 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002715 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002716}
2717
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002718/*
2719 * combine freq adjustment with unthrottling to avoid two passes over the
2720 * events. At the same time, make sure, having freq events does not change
2721 * the rate of unthrottling as that would introduce bias.
2722 */
2723static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
2724 int needs_unthr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002725{
2726 struct perf_event *event;
2727 struct hw_perf_event *hwc;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002728 u64 now, period = TICK_NSEC;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002729 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002730
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002731 /*
2732 * only need to iterate over all events iff:
2733 * - context have events in frequency mode (needs freq adjust)
2734 * - there are events to unthrottle on this cpu
2735 */
2736 if (!(ctx->nr_freq || needs_unthr))
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002737 return;
2738
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002739 raw_spin_lock(&ctx->lock);
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002740 perf_pmu_disable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002741
Paul Mackerras03541f82009-10-14 16:58:03 +11002742 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002743 if (event->state != PERF_EVENT_STATE_ACTIVE)
2744 continue;
2745
Stephane Eranian5632ab12011-01-03 18:20:01 +02002746 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002747 continue;
2748
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002749 hwc = &event->hw;
2750
Jiri Olsaae23bff2013-08-24 16:45:54 +02002751 if (hwc->interrupts == MAX_INTERRUPTS) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002752 hwc->interrupts = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002753 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002754 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002755 }
2756
2757 if (!event->attr.freq || !event->attr.sample_freq)
2758 continue;
2759
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002760 /*
2761 * stop the event and update event->count
2762 */
2763 event->pmu->stop(event, PERF_EF_UPDATE);
2764
Peter Zijlstrae7850592010-05-21 14:43:08 +02002765 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002766 delta = now - hwc->freq_count_stamp;
2767 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002768
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002769 /*
2770 * restart the event
2771 * reload only if value has changed
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002772 * we have stopped the event so tell that
2773 * to perf_adjust_period() to avoid stopping it
2774 * twice.
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002775 */
Peter Zijlstraabd50712010-01-26 18:50:16 +01002776 if (delta > 0)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002777 perf_adjust_period(event, period, delta, false);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002778
2779 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002780 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002781
Stephane Eranianf39d47f2012-02-07 14:39:57 +01002782 perf_pmu_enable(ctx->pmu);
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002783 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002784}
2785
2786/*
2787 * Round-robin a context's events:
2788 */
2789static void rotate_ctx(struct perf_event_context *ctx)
2790{
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002791 /*
2792 * Rotate the first entry last of non-pinned groups. Rotation might be
2793 * disabled by the inheritance code.
2794 */
2795 if (!ctx->rotate_disable)
2796 list_rotate_left(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002797}
2798
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002799/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002800 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2801 * because they're strictly cpu affine and rotate_start is called with IRQs
2802 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002803 */
Stephane Eranian9e630202013-04-03 14:21:33 +02002804static int perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002805{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002806 struct perf_event_context *ctx = NULL;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002807 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002808
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002809 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002810 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002811 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2812 rotate = 1;
2813 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002814
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002815 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002816 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002817 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002818 if (ctx->nr_events != ctx->nr_active)
2819 rotate = 1;
2820 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002821
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002822 if (!rotate)
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002823 goto done;
2824
Peter Zijlstrafacc4302011-04-09 21:17:42 +02002825 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002826 perf_pmu_disable(cpuctx->ctx.pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002827
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002828 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2829 if (ctx)
2830 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002831
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002832 rotate_ctx(&cpuctx->ctx);
2833 if (ctx)
2834 rotate_ctx(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002835
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002836 perf_event_sched_in(cpuctx, ctx, current);
Peter Zijlstra0f5a2602011-11-16 14:38:16 +01002837
2838 perf_pmu_enable(cpuctx->ctx.pmu);
2839 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002840done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002841 if (remove)
2842 list_del_init(&cpuctx->rotation_list);
Stephane Eranian9e630202013-04-03 14:21:33 +02002843
2844 return rotate;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002845}
2846
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002847#ifdef CONFIG_NO_HZ_FULL
2848bool perf_event_can_stop_tick(void)
2849{
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02002850 if (atomic_read(&nr_freq_events) ||
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002851 __this_cpu_read(perf_throttled_count))
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002852 return false;
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02002853 else
2854 return true;
Frederic Weisbecker026249e2013-04-20 15:58:34 +02002855}
2856#endif
2857
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002858void perf_event_task_tick(void)
2859{
2860 struct list_head *head = &__get_cpu_var(rotation_list);
2861 struct perf_cpu_context *cpuctx, *tmp;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002862 struct perf_event_context *ctx;
2863 int throttled;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002864
2865 WARN_ON(!irqs_disabled());
2866
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002867 __this_cpu_inc(perf_throttled_seq);
2868 throttled = __this_cpu_xchg(perf_throttled_count, 0);
2869
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002870 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
Stephane Eraniane050e3f2012-01-26 17:03:19 +01002871 ctx = &cpuctx->ctx;
2872 perf_adjust_freq_unthr_context(ctx, throttled);
2873
2874 ctx = cpuctx->task_ctx;
2875 if (ctx)
2876 perf_adjust_freq_unthr_context(ctx, throttled);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002877 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002878}
2879
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002880static int event_enable_on_exec(struct perf_event *event,
2881 struct perf_event_context *ctx)
2882{
2883 if (!event->attr.enable_on_exec)
2884 return 0;
2885
2886 event->attr.enable_on_exec = 0;
2887 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2888 return 0;
2889
Peter Zijlstra1d9b4822011-11-23 12:34:20 +01002890 __perf_event_mark_enabled(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002891
2892 return 1;
2893}
2894
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002895/*
2896 * Enable all of a task's events that have been marked enable-on-exec.
2897 * This expects task == current.
2898 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002899static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002900{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002901 struct perf_event *event;
2902 unsigned long flags;
2903 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002904 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002905
2906 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002907 if (!ctx || !ctx->nr_events)
2908 goto out;
2909
Stephane Eraniane566b762011-04-06 02:54:54 +02002910 /*
2911 * We must ctxsw out cgroup events to avoid conflict
2912 * when invoking perf_task_event_sched_in() later on
2913 * in this function. Otherwise we end up trying to
2914 * ctxswin cgroup events which are already scheduled
2915 * in.
2916 */
Stephane Eraniana8d757e2011-08-25 15:58:03 +02002917 perf_cgroup_sched_out(current, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002918
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002919 raw_spin_lock(&ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02002920 task_ctx_sched_out(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002921
Peter Zijlstrab79387e2011-11-22 11:25:43 +01002922 list_for_each_entry(event, &ctx->event_list, event_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002923 ret = event_enable_on_exec(event, ctx);
2924 if (ret)
2925 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002926 }
2927
2928 /*
2929 * Unclone this context if we enabled any event.
2930 */
2931 if (enabled)
2932 unclone_ctx(ctx);
2933
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002934 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002935
Stephane Eraniane566b762011-04-06 02:54:54 +02002936 /*
2937 * Also calls ctxswin for cgroup events, if any:
2938 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002939 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002940out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002941 local_irq_restore(flags);
2942}
2943
2944/*
2945 * Cross CPU call to read the hardware event
2946 */
2947static void __perf_event_read(void *info)
2948{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002949 struct perf_event *event = info;
2950 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002951 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002952
2953 /*
2954 * If this is a task context, we need to check whether it is
2955 * the current task context of this cpu. If not it has been
2956 * scheduled out before the smp call arrived. In that case
2957 * event->count would have been updated to a recent sample
2958 * when the event was scheduled out.
2959 */
2960 if (ctx->task && cpuctx->task_ctx != ctx)
2961 return;
2962
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002963 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002964 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002965 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002966 update_cgrp_time_from_event(event);
2967 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002968 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002969 if (event->state == PERF_EVENT_STATE_ACTIVE)
2970 event->pmu->read(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002971 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002972}
2973
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002974static inline u64 perf_event_count(struct perf_event *event)
2975{
Peter Zijlstrae7850592010-05-21 14:43:08 +02002976 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002977}
2978
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002979static u64 perf_event_read(struct perf_event *event)
2980{
2981 /*
2982 * If event is enabled and currently active on a CPU, update the
2983 * value in the event structure:
2984 */
2985 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2986 smp_call_function_single(event->oncpu,
2987 __perf_event_read, event, 1);
2988 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01002989 struct perf_event_context *ctx = event->ctx;
2990 unsigned long flags;
2991
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002992 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002993 /*
2994 * may read while context is not active
2995 * (e.g., thread is blocked), in that case
2996 * we cannot update context time
2997 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002998 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002999 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02003000 update_cgrp_time_from_event(event);
3001 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003002 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003003 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003004 }
3005
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003006 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003007}
3008
3009/*
3010 * Initialize the perf_event context in a task_struct:
3011 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02003012static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003013{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003014 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003015 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01003016 INIT_LIST_HEAD(&ctx->pinned_groups);
3017 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003018 INIT_LIST_HEAD(&ctx->event_list);
3019 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003020}
3021
Peter Zijlstraeb184472010-09-07 15:55:13 +02003022static struct perf_event_context *
3023alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024{
3025 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003026
3027 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3028 if (!ctx)
3029 return NULL;
3030
3031 __perf_event_init_context(ctx);
3032 if (task) {
3033 ctx->task = task;
3034 get_task_struct(task);
3035 }
3036 ctx->pmu = pmu;
3037
3038 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003039}
3040
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003041static struct task_struct *
3042find_lively_task_by_vpid(pid_t vpid)
3043{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003044 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003045 int err;
3046
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003047 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003048 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003049 task = current;
3050 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003051 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003052 if (task)
3053 get_task_struct(task);
3054 rcu_read_unlock();
3055
3056 if (!task)
3057 return ERR_PTR(-ESRCH);
3058
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003059 /* Reuse ptrace permission checks for now. */
3060 err = -EACCES;
3061 if (!ptrace_may_access(task, PTRACE_MODE_READ))
3062 goto errout;
3063
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07003064 return task;
3065errout:
3066 put_task_struct(task);
3067 return ERR_PTR(err);
3068
3069}
3070
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003071/*
3072 * Returns a matching context with refcount and pincount.
3073 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003074static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07003075find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003076{
3077 struct perf_event_context *ctx;
3078 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003079 unsigned long flags;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003080 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003081
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01003082 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003083 /* Must be root to operate on a CPU event: */
3084 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3085 return ERR_PTR(-EACCES);
3086
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003087 /*
3088 * We could be clever and allow to attach a event to an
3089 * offline CPU and activate it when the CPU comes up, but
3090 * that's for later.
3091 */
3092 if (!cpu_online(cpu))
3093 return ERR_PTR(-ENODEV);
3094
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003095 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003096 ctx = &cpuctx->ctx;
3097 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003098 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003099
3100 return ctx;
3101 }
3102
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003103 err = -EINVAL;
3104 ctxn = pmu->task_ctx_nr;
3105 if (ctxn < 0)
3106 goto errout;
3107
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003108retry:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003109 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003110 if (ctx) {
3111 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003112 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003113 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003114 } else {
Peter Zijlstraeb184472010-09-07 15:55:13 +02003115 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003116 err = -ENOMEM;
3117 if (!ctx)
3118 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02003119
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003120 err = 0;
3121 mutex_lock(&task->perf_event_mutex);
3122 /*
3123 * If it has already passed perf_event_exit_task().
3124 * we must see PF_EXITING, it takes this mutex too.
3125 */
3126 if (task->flags & PF_EXITING)
3127 err = -ESRCH;
3128 else if (task->perf_event_ctxp[ctxn])
3129 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003130 else {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003131 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003132 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003133 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01003134 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003135 mutex_unlock(&task->perf_event_mutex);
3136
3137 if (unlikely(err)) {
Peter Zijlstra9137fb22011-04-09 21:17:41 +02003138 put_ctx(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01003139
3140 if (err == -EAGAIN)
3141 goto retry;
3142 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003143 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003144 }
3145
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003146 return ctx;
3147
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003148errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003149 return ERR_PTR(err);
3150}
3151
Li Zefan6fb29152009-10-15 11:21:42 +08003152static void perf_event_free_filter(struct perf_event *event);
3153
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003154static void free_event_rcu(struct rcu_head *head)
3155{
3156 struct perf_event *event;
3157
3158 event = container_of(head, struct perf_event, rcu_head);
3159 if (event->ns)
3160 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08003161 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003162 kfree(event);
3163}
3164
Frederic Weisbecker76369132011-05-19 19:55:04 +02003165static void ring_buffer_put(struct ring_buffer *rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003166static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003167
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003168static void unaccount_event_cpu(struct perf_event *event, int cpu)
3169{
3170 if (event->parent)
3171 return;
3172
3173 if (has_branch_stack(event)) {
3174 if (!(event->attach_state & PERF_ATTACH_TASK))
3175 atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
3176 }
3177 if (is_cgroup_event(event))
3178 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3179}
3180
3181static void unaccount_event(struct perf_event *event)
3182{
3183 if (event->parent)
3184 return;
3185
3186 if (event->attach_state & PERF_ATTACH_TASK)
3187 static_key_slow_dec_deferred(&perf_sched_events);
3188 if (event->attr.mmap || event->attr.mmap_data)
3189 atomic_dec(&nr_mmap_events);
3190 if (event->attr.comm)
3191 atomic_dec(&nr_comm_events);
3192 if (event->attr.task)
3193 atomic_dec(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02003194 if (event->attr.freq)
3195 atomic_dec(&nr_freq_events);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003196 if (is_cgroup_event(event))
3197 static_key_slow_dec_deferred(&perf_sched_events);
3198 if (has_branch_stack(event))
3199 static_key_slow_dec_deferred(&perf_sched_events);
3200
3201 unaccount_event_cpu(event, event->cpu);
3202}
3203
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003204static void __free_event(struct perf_event *event)
3205{
3206 if (!event->parent) {
3207 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3208 put_callchain_buffers();
3209 }
3210
3211 if (event->destroy)
3212 event->destroy(event);
3213
3214 if (event->ctx)
3215 put_ctx(event->ctx);
3216
3217 call_rcu(&event->rcu_head, free_event_rcu);
3218}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003219static void free_event(struct perf_event *event)
3220{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003221 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003222
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02003223 unaccount_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003224
Frederic Weisbecker76369132011-05-19 19:55:04 +02003225 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003226 struct ring_buffer *rb;
3227
3228 /*
3229 * Can happen when we close an event with re-directed output.
3230 *
3231 * Since we have a 0 refcount, perf_mmap_close() will skip
3232 * over us; possibly making our ring_buffer_put() the last.
3233 */
3234 mutex_lock(&event->mmap_mutex);
3235 rb = event->rb;
3236 if (rb) {
3237 rcu_assign_pointer(event->rb, NULL);
3238 ring_buffer_detach(event, rb);
3239 ring_buffer_put(rb); /* could be last */
3240 }
3241 mutex_unlock(&event->mmap_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003242 }
3243
Stephane Eraniane5d13672011-02-14 11:20:01 +02003244 if (is_cgroup_event(event))
3245 perf_detach_cgroup(event);
3246
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003247
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02003248 __free_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003249}
3250
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003251int perf_event_release_kernel(struct perf_event *event)
3252{
3253 struct perf_event_context *ctx = event->ctx;
3254
3255 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02003256 /*
3257 * There are two ways this annotation is useful:
3258 *
3259 * 1) there is a lock recursion from perf_event_exit_task
3260 * see the comment there.
3261 *
3262 * 2) there is a lock-inversion with mmap_sem through
3263 * perf_event_read_group(), which takes faults while
3264 * holding ctx->mutex, however this is called after
3265 * the last filedesc died, so there is no possibility
3266 * to trigger the AB-BA case.
3267 */
3268 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02003269 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02003270 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02003271 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrae03a9a52011-04-09 21:17:47 +02003272 perf_remove_from_context(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003273 mutex_unlock(&ctx->mutex);
3274
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003275 free_event(event);
3276
3277 return 0;
3278}
3279EXPORT_SYMBOL_GPL(perf_event_release_kernel);
3280
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003281/*
3282 * Called when the last reference to the file is gone.
3283 */
Al Viroa6fa9412012-08-20 14:59:25 +01003284static void put_event(struct perf_event *event)
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003285{
Peter Zijlstra88821352010-11-09 19:01:43 +01003286 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003287
Al Viroa6fa9412012-08-20 14:59:25 +01003288 if (!atomic_long_dec_and_test(&event->refcount))
3289 return;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003290
Peter Zijlstra88821352010-11-09 19:01:43 +01003291 rcu_read_lock();
3292 owner = ACCESS_ONCE(event->owner);
3293 /*
3294 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
3295 * !owner it means the list deletion is complete and we can indeed
3296 * free this event, otherwise we need to serialize on
3297 * owner->perf_event_mutex.
3298 */
3299 smp_read_barrier_depends();
3300 if (owner) {
3301 /*
3302 * Since delayed_put_task_struct() also drops the last
3303 * task reference we can safely take a new reference
3304 * while holding the rcu_read_lock().
3305 */
3306 get_task_struct(owner);
3307 }
3308 rcu_read_unlock();
3309
3310 if (owner) {
3311 mutex_lock(&owner->perf_event_mutex);
3312 /*
3313 * We have to re-check the event->owner field, if it is cleared
3314 * we raced with perf_event_exit_task(), acquiring the mutex
3315 * ensured they're done, and we can proceed with freeing the
3316 * event.
3317 */
3318 if (event->owner)
3319 list_del_init(&event->owner_entry);
3320 mutex_unlock(&owner->perf_event_mutex);
3321 put_task_struct(owner);
3322 }
3323
Al Viroa6fa9412012-08-20 14:59:25 +01003324 perf_event_release_kernel(event);
3325}
3326
3327static int perf_release(struct inode *inode, struct file *file)
3328{
3329 put_event(file->private_data);
3330 return 0;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01003331}
3332
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003333u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003334{
3335 struct perf_event *child;
3336 u64 total = 0;
3337
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003338 *enabled = 0;
3339 *running = 0;
3340
Peter Zijlstra6f105812009-11-20 22:19:56 +01003341 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003342 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003343 *enabled += event->total_time_enabled +
3344 atomic64_read(&event->child_total_time_enabled);
3345 *running += event->total_time_running +
3346 atomic64_read(&event->child_total_time_running);
3347
3348 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003349 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003350 *enabled += child->total_time_enabled;
3351 *running += child->total_time_running;
3352 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003353 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003354
3355 return total;
3356}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02003357EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003358
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003359static int perf_event_read_group(struct perf_event *event,
3360 u64 read_format, char __user *buf)
3361{
3362 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01003363 int n = 0, size = 0, ret = -EFAULT;
3364 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003365 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003366 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003367
Peter Zijlstra6f105812009-11-20 22:19:56 +01003368 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003369 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003370
3371 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003372 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3373 values[n++] = enabled;
3374 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3375 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003376 values[n++] = count;
3377 if (read_format & PERF_FORMAT_ID)
3378 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003379
3380 size = n * sizeof(u64);
3381
3382 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003383 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003384
Peter Zijlstra6f105812009-11-20 22:19:56 +01003385 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003386
3387 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003388 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003389
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003390 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003391 if (read_format & PERF_FORMAT_ID)
3392 values[n++] = primary_event_id(sub);
3393
3394 size = n * sizeof(u64);
3395
Stephane Eranian184d3da2009-11-23 21:40:49 -08003396 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003397 ret = -EFAULT;
3398 goto unlock;
3399 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003400
3401 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003403unlock:
3404 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003405
Peter Zijlstraabf48682009-11-20 22:19:49 +01003406 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003407}
3408
3409static int perf_event_read_one(struct perf_event *event,
3410 u64 read_format, char __user *buf)
3411{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003412 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003413 u64 values[4];
3414 int n = 0;
3415
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003416 values[n++] = perf_event_read_value(event, &enabled, &running);
3417 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3418 values[n++] = enabled;
3419 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3420 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003421 if (read_format & PERF_FORMAT_ID)
3422 values[n++] = primary_event_id(event);
3423
3424 if (copy_to_user(buf, values, n * sizeof(u64)))
3425 return -EFAULT;
3426
3427 return n * sizeof(u64);
3428}
3429
3430/*
3431 * Read the performance event - simple non blocking version for now
3432 */
3433static ssize_t
3434perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3435{
3436 u64 read_format = event->attr.read_format;
3437 int ret;
3438
3439 /*
3440 * Return end-of-file for a read on a event that is in
3441 * error state (i.e. because it was pinned but it couldn't be
3442 * scheduled on to the CPU at some point).
3443 */
3444 if (event->state == PERF_EVENT_STATE_ERROR)
3445 return 0;
3446
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003447 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003448 return -ENOSPC;
3449
3450 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003451 if (read_format & PERF_FORMAT_GROUP)
3452 ret = perf_event_read_group(event, read_format, buf);
3453 else
3454 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003455
3456 return ret;
3457}
3458
3459static ssize_t
3460perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3461{
3462 struct perf_event *event = file->private_data;
3463
3464 return perf_read_hw(event, buf, count);
3465}
3466
3467static unsigned int perf_poll(struct file *file, poll_table *wait)
3468{
3469 struct perf_event *event = file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003470 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003471 unsigned int events = POLL_HUP;
3472
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003473 /*
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003474 * Pin the event->rb by taking event->mmap_mutex; otherwise
3475 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003476 */
3477 mutex_lock(&event->mmap_mutex);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003478 rb = event->rb;
3479 if (rb)
Frederic Weisbecker76369132011-05-19 19:55:04 +02003480 events = atomic_xchg(&rb->poll, 0);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003481 mutex_unlock(&event->mmap_mutex);
3482
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003483 poll_wait(file, &event->waitq, wait);
3484
3485 return events;
3486}
3487
3488static void perf_event_reset(struct perf_event *event)
3489{
3490 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003491 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003492 perf_event_update_userpage(event);
3493}
3494
3495/*
3496 * Holding the top-level event's child_mutex means that any
3497 * descendant process that has inherited this event will block
3498 * in sync_child_event if it goes to exit, thus satisfying the
3499 * task existence requirements of perf_event_enable/disable.
3500 */
3501static void perf_event_for_each_child(struct perf_event *event,
3502 void (*func)(struct perf_event *))
3503{
3504 struct perf_event *child;
3505
3506 WARN_ON_ONCE(event->ctx->parent_ctx);
3507 mutex_lock(&event->child_mutex);
3508 func(event);
3509 list_for_each_entry(child, &event->child_list, child_list)
3510 func(child);
3511 mutex_unlock(&event->child_mutex);
3512}
3513
3514static void perf_event_for_each(struct perf_event *event,
3515 void (*func)(struct perf_event *))
3516{
3517 struct perf_event_context *ctx = event->ctx;
3518 struct perf_event *sibling;
3519
3520 WARN_ON_ONCE(ctx->parent_ctx);
3521 mutex_lock(&ctx->mutex);
3522 event = event->group_leader;
3523
3524 perf_event_for_each_child(event, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003525 list_for_each_entry(sibling, &event->sibling_list, group_entry)
Michael Ellerman724b6da2012-04-11 11:54:13 +10003526 perf_event_for_each_child(sibling, func);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003527 mutex_unlock(&ctx->mutex);
3528}
3529
3530static int perf_event_period(struct perf_event *event, u64 __user *arg)
3531{
3532 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003533 int ret = 0;
3534 u64 value;
3535
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003536 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003537 return -EINVAL;
3538
John Blackwoodad0cf342010-09-28 18:03:11 -04003539 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003540 return -EFAULT;
3541
3542 if (!value)
3543 return -EINVAL;
3544
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003545 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003546 if (event->attr.freq) {
3547 if (value > sysctl_perf_event_sample_rate) {
3548 ret = -EINVAL;
3549 goto unlock;
3550 }
3551
3552 event->attr.sample_freq = value;
3553 } else {
3554 event->attr.sample_period = value;
3555 event->hw.sample_period = value;
3556 }
3557unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01003558 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003559
3560 return ret;
3561}
3562
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003563static const struct file_operations perf_fops;
3564
Al Viro2903ff02012-08-28 12:52:22 -04003565static inline int perf_fget_light(int fd, struct fd *p)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003566{
Al Viro2903ff02012-08-28 12:52:22 -04003567 struct fd f = fdget(fd);
3568 if (!f.file)
3569 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003570
Al Viro2903ff02012-08-28 12:52:22 -04003571 if (f.file->f_op != &perf_fops) {
3572 fdput(f);
3573 return -EBADF;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003574 }
Al Viro2903ff02012-08-28 12:52:22 -04003575 *p = f;
3576 return 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003577}
3578
3579static int perf_event_set_output(struct perf_event *event,
3580 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003581static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003582
3583static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3584{
3585 struct perf_event *event = file->private_data;
3586 void (*func)(struct perf_event *);
3587 u32 flags = arg;
3588
3589 switch (cmd) {
3590 case PERF_EVENT_IOC_ENABLE:
3591 func = perf_event_enable;
3592 break;
3593 case PERF_EVENT_IOC_DISABLE:
3594 func = perf_event_disable;
3595 break;
3596 case PERF_EVENT_IOC_RESET:
3597 func = perf_event_reset;
3598 break;
3599
3600 case PERF_EVENT_IOC_REFRESH:
3601 return perf_event_refresh(event, arg);
3602
3603 case PERF_EVENT_IOC_PERIOD:
3604 return perf_event_period(event, (u64 __user *)arg);
3605
Jiri Olsacf4957f2012-10-24 13:37:58 +02003606 case PERF_EVENT_IOC_ID:
3607 {
3608 u64 id = primary_event_id(event);
3609
3610 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
3611 return -EFAULT;
3612 return 0;
3613 }
3614
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003615 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003616 {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003617 int ret;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003618 if (arg != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04003619 struct perf_event *output_event;
3620 struct fd output;
3621 ret = perf_fget_light(arg, &output);
3622 if (ret)
3623 return ret;
3624 output_event = output.file->private_data;
3625 ret = perf_event_set_output(event, output_event);
3626 fdput(output);
3627 } else {
3628 ret = perf_event_set_output(event, NULL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003629 }
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003630 return ret;
3631 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003632
Li Zefan6fb29152009-10-15 11:21:42 +08003633 case PERF_EVENT_IOC_SET_FILTER:
3634 return perf_event_set_filter(event, (void __user *)arg);
3635
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003636 default:
3637 return -ENOTTY;
3638 }
3639
3640 if (flags & PERF_IOC_FLAG_GROUP)
3641 perf_event_for_each(event, func);
3642 else
3643 perf_event_for_each_child(event, func);
3644
3645 return 0;
3646}
3647
3648int perf_event_task_enable(void)
3649{
3650 struct perf_event *event;
3651
3652 mutex_lock(&current->perf_event_mutex);
3653 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3654 perf_event_for_each_child(event, perf_event_enable);
3655 mutex_unlock(&current->perf_event_mutex);
3656
3657 return 0;
3658}
3659
3660int perf_event_task_disable(void)
3661{
3662 struct perf_event *event;
3663
3664 mutex_lock(&current->perf_event_mutex);
3665 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3666 perf_event_for_each_child(event, perf_event_disable);
3667 mutex_unlock(&current->perf_event_mutex);
3668
3669 return 0;
3670}
3671
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003672static int perf_event_index(struct perf_event *event)
3673{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003674 if (event->hw.state & PERF_HES_STOPPED)
3675 return 0;
3676
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003677 if (event->state != PERF_EVENT_STATE_ACTIVE)
3678 return 0;
3679
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01003680 return event->pmu->event_idx(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003681}
3682
Eric B Munsonc4794292011-06-23 16:34:38 -04003683static void calc_timer_values(struct perf_event *event,
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003684 u64 *now,
Eric B Munson7f310a52011-06-23 16:34:38 -04003685 u64 *enabled,
3686 u64 *running)
Eric B Munsonc4794292011-06-23 16:34:38 -04003687{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003688 u64 ctx_time;
Eric B Munsonc4794292011-06-23 16:34:38 -04003689
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003690 *now = perf_clock();
3691 ctx_time = event->shadow_ctx_time + *now;
Eric B Munsonc4794292011-06-23 16:34:38 -04003692 *enabled = ctx_time - event->tstamp_enabled;
3693 *running = ctx_time - event->tstamp_running;
3694}
3695
Peter Zijlstrafa7315872013-09-19 10:16:42 +02003696static void perf_event_init_userpage(struct perf_event *event)
3697{
3698 struct perf_event_mmap_page *userpg;
3699 struct ring_buffer *rb;
3700
3701 rcu_read_lock();
3702 rb = rcu_dereference(event->rb);
3703 if (!rb)
3704 goto unlock;
3705
3706 userpg = rb->user_page;
3707
3708 /* Allow new userspace to detect that bit 0 is deprecated */
3709 userpg->cap_bit0_is_deprecated = 1;
3710 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
3711
3712unlock:
3713 rcu_read_unlock();
3714}
3715
Peter Zijlstrac7206202012-03-22 17:26:36 +01003716void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003717{
3718}
3719
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003720/*
3721 * Callers need to ensure there can be no nesting of this function, otherwise
3722 * the seqlock logic goes bad. We can not serialize this because the arch
3723 * code calls this from NMI context.
3724 */
3725void perf_event_update_userpage(struct perf_event *event)
3726{
3727 struct perf_event_mmap_page *userpg;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003728 struct ring_buffer *rb;
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003729 u64 enabled, running, now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003730
3731 rcu_read_lock();
Peter Zijlstra5ec4c592013-08-02 21:16:30 +02003732 rb = rcu_dereference(event->rb);
3733 if (!rb)
3734 goto unlock;
3735
Eric B Munson0d641202011-06-24 12:26:26 -04003736 /*
3737 * compute total_time_enabled, total_time_running
3738 * based on snapshot values taken when the event
3739 * was last scheduled in.
3740 *
3741 * we cannot simply called update_context_time()
3742 * because of locking issue as we can be called in
3743 * NMI context
3744 */
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003745 calc_timer_values(event, &now, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003746
Frederic Weisbecker76369132011-05-19 19:55:04 +02003747 userpg = rb->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003748 /*
3749 * Disable preemption so as to not let the corresponding user-space
3750 * spin too long if we get preempted.
3751 */
3752 preempt_disable();
3753 ++userpg->lock;
3754 barrier();
3755 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003756 userpg->offset = perf_event_count(event);
Peter Zijlstra365a4032011-11-21 20:58:59 +01003757 if (userpg->index)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003758 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003759
Eric B Munson0d641202011-06-24 12:26:26 -04003760 userpg->time_enabled = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003761 atomic64_read(&event->child_total_time_enabled);
3762
Eric B Munson0d641202011-06-24 12:26:26 -04003763 userpg->time_running = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003764 atomic64_read(&event->child_total_time_running);
3765
Peter Zijlstrac7206202012-03-22 17:26:36 +01003766 arch_perf_update_userpage(userpg, now);
Peter Zijlstrae3f35412011-11-21 11:43:53 +01003767
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003768 barrier();
3769 ++userpg->lock;
3770 preempt_enable();
3771unlock:
3772 rcu_read_unlock();
3773}
3774
Peter Zijlstra906010b2009-09-21 16:08:49 +02003775static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3776{
3777 struct perf_event *event = vma->vm_file->private_data;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003778 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003779 int ret = VM_FAULT_SIGBUS;
3780
3781 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3782 if (vmf->pgoff == 0)
3783 ret = 0;
3784 return ret;
3785 }
3786
3787 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003788 rb = rcu_dereference(event->rb);
3789 if (!rb)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003790 goto unlock;
3791
3792 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3793 goto unlock;
3794
Frederic Weisbecker76369132011-05-19 19:55:04 +02003795 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003796 if (!vmf->page)
3797 goto unlock;
3798
3799 get_page(vmf->page);
3800 vmf->page->mapping = vma->vm_file->f_mapping;
3801 vmf->page->index = vmf->pgoff;
3802
3803 ret = 0;
3804unlock:
3805 rcu_read_unlock();
3806
3807 return ret;
3808}
3809
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003810static void ring_buffer_attach(struct perf_event *event,
3811 struct ring_buffer *rb)
3812{
3813 unsigned long flags;
3814
3815 if (!list_empty(&event->rb_entry))
3816 return;
3817
3818 spin_lock_irqsave(&rb->event_lock, flags);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003819 if (list_empty(&event->rb_entry))
3820 list_add(&event->rb_entry, &rb->event_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003821 spin_unlock_irqrestore(&rb->event_lock, flags);
3822}
3823
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003824static void ring_buffer_detach(struct perf_event *event, struct ring_buffer *rb)
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003825{
3826 unsigned long flags;
3827
3828 if (list_empty(&event->rb_entry))
3829 return;
3830
3831 spin_lock_irqsave(&rb->event_lock, flags);
3832 list_del_init(&event->rb_entry);
3833 wake_up_all(&event->waitq);
3834 spin_unlock_irqrestore(&rb->event_lock, flags);
3835}
3836
3837static void ring_buffer_wakeup(struct perf_event *event)
3838{
3839 struct ring_buffer *rb;
3840
3841 rcu_read_lock();
3842 rb = rcu_dereference(event->rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003843 if (rb) {
3844 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
3845 wake_up_all(&event->waitq);
3846 }
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003847 rcu_read_unlock();
3848}
3849
Frederic Weisbecker76369132011-05-19 19:55:04 +02003850static void rb_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003851{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003852 struct ring_buffer *rb;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003853
Frederic Weisbecker76369132011-05-19 19:55:04 +02003854 rb = container_of(rcu_head, struct ring_buffer, rcu_head);
3855 rb_free(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003856}
3857
Frederic Weisbecker76369132011-05-19 19:55:04 +02003858static struct ring_buffer *ring_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003859{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003860 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003861
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003862 rcu_read_lock();
Frederic Weisbecker76369132011-05-19 19:55:04 +02003863 rb = rcu_dereference(event->rb);
3864 if (rb) {
3865 if (!atomic_inc_not_zero(&rb->refcount))
3866 rb = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003867 }
3868 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003869
Frederic Weisbecker76369132011-05-19 19:55:04 +02003870 return rb;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003871}
3872
Frederic Weisbecker76369132011-05-19 19:55:04 +02003873static void ring_buffer_put(struct ring_buffer *rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003874{
Frederic Weisbecker76369132011-05-19 19:55:04 +02003875 if (!atomic_dec_and_test(&rb->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003876 return;
3877
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003878 WARN_ON_ONCE(!list_empty(&rb->event_list));
Peter Zijlstra10c6db12011-11-26 02:47:31 +01003879
Frederic Weisbecker76369132011-05-19 19:55:04 +02003880 call_rcu(&rb->rcu_head, rb_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003881}
3882
3883static void perf_mmap_open(struct vm_area_struct *vma)
3884{
3885 struct perf_event *event = vma->vm_file->private_data;
3886
3887 atomic_inc(&event->mmap_count);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003888 atomic_inc(&event->rb->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003889}
3890
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003891/*
3892 * A buffer can be mmap()ed multiple times; either directly through the same
3893 * event, or through other events by use of perf_event_set_output().
3894 *
3895 * In order to undo the VM accounting done by perf_mmap() we need to destroy
3896 * the buffer here, where we still have a VM context. This means we need
3897 * to detach all events redirecting to us.
3898 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003899static void perf_mmap_close(struct vm_area_struct *vma)
3900{
3901 struct perf_event *event = vma->vm_file->private_data;
3902
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003903 struct ring_buffer *rb = event->rb;
3904 struct user_struct *mmap_user = rb->mmap_user;
3905 int mmap_locked = rb->mmap_locked;
3906 unsigned long size = perf_data_size(rb);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003907
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003908 atomic_dec(&rb->mmap_count);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003909
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003910 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
3911 return;
3912
3913 /* Detach current event from the buffer. */
3914 rcu_assign_pointer(event->rb, NULL);
3915 ring_buffer_detach(event, rb);
3916 mutex_unlock(&event->mmap_mutex);
3917
3918 /* If there's still other mmap()s of this buffer, we're done. */
3919 if (atomic_read(&rb->mmap_count)) {
3920 ring_buffer_put(rb); /* can't be last */
3921 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003922 }
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02003923
3924 /*
3925 * No other mmap()s, detach from all other events that might redirect
3926 * into the now unreachable buffer. Somewhat complicated by the
3927 * fact that rb::event_lock otherwise nests inside mmap_mutex.
3928 */
3929again:
3930 rcu_read_lock();
3931 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
3932 if (!atomic_long_inc_not_zero(&event->refcount)) {
3933 /*
3934 * This event is en-route to free_event() which will
3935 * detach it and remove it from the list.
3936 */
3937 continue;
3938 }
3939 rcu_read_unlock();
3940
3941 mutex_lock(&event->mmap_mutex);
3942 /*
3943 * Check we didn't race with perf_event_set_output() which can
3944 * swizzle the rb from under us while we were waiting to
3945 * acquire mmap_mutex.
3946 *
3947 * If we find a different rb; ignore this event, a next
3948 * iteration will no longer find it on the list. We have to
3949 * still restart the iteration to make sure we're not now
3950 * iterating the wrong list.
3951 */
3952 if (event->rb == rb) {
3953 rcu_assign_pointer(event->rb, NULL);
3954 ring_buffer_detach(event, rb);
3955 ring_buffer_put(rb); /* can't be last, we still have one */
3956 }
3957 mutex_unlock(&event->mmap_mutex);
3958 put_event(event);
3959
3960 /*
3961 * Restart the iteration; either we're on the wrong list or
3962 * destroyed its integrity by doing a deletion.
3963 */
3964 goto again;
3965 }
3966 rcu_read_unlock();
3967
3968 /*
3969 * It could be there's still a few 0-ref events on the list; they'll
3970 * get cleaned up by free_event() -- they'll also still have their
3971 * ref on the rb and will free it whenever they are done with it.
3972 *
3973 * Aside from that, this buffer is 'fully' detached and unmapped,
3974 * undo the VM accounting.
3975 */
3976
3977 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
3978 vma->vm_mm->pinned_vm -= mmap_locked;
3979 free_uid(mmap_user);
3980
3981 ring_buffer_put(rb); /* could be last */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003982}
3983
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003984static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003985 .open = perf_mmap_open,
3986 .close = perf_mmap_close,
3987 .fault = perf_mmap_fault,
3988 .page_mkwrite = perf_mmap_fault,
3989};
3990
3991static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3992{
3993 struct perf_event *event = file->private_data;
3994 unsigned long user_locked, user_lock_limit;
3995 struct user_struct *user = current_user();
3996 unsigned long locked, lock_limit;
Frederic Weisbecker76369132011-05-19 19:55:04 +02003997 struct ring_buffer *rb;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003998 unsigned long vma_size;
3999 unsigned long nr_pages;
4000 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004001 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004002
Peter Zijlstrac7920612010-05-18 10:33:24 +02004003 /*
4004 * Don't allow mmap() of inherited per-task counters. This would
4005 * create a performance issue due to all children writing to the
Frederic Weisbecker76369132011-05-19 19:55:04 +02004006 * same rb.
Peter Zijlstrac7920612010-05-18 10:33:24 +02004007 */
4008 if (event->cpu == -1 && event->attr.inherit)
4009 return -EINVAL;
4010
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004011 if (!(vma->vm_flags & VM_SHARED))
4012 return -EINVAL;
4013
4014 vma_size = vma->vm_end - vma->vm_start;
4015 nr_pages = (vma_size / PAGE_SIZE) - 1;
4016
4017 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02004018 * If we have rb pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004019 * can do bitmasks instead of modulo.
4020 */
4021 if (nr_pages != 0 && !is_power_of_2(nr_pages))
4022 return -EINVAL;
4023
4024 if (vma_size != PAGE_SIZE * (1 + nr_pages))
4025 return -EINVAL;
4026
4027 if (vma->vm_pgoff != 0)
4028 return -EINVAL;
4029
4030 WARN_ON_ONCE(event->ctx->parent_ctx);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004031again:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004032 mutex_lock(&event->mmap_mutex);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004033 if (event->rb) {
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004034 if (event->rb->nr_pages != nr_pages) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004035 ret = -EINVAL;
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004036 goto unlock;
4037 }
4038
4039 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
4040 /*
4041 * Raced against perf_mmap_close() through
4042 * perf_event_set_output(). Try again, hope for better
4043 * luck.
4044 */
4045 mutex_unlock(&event->mmap_mutex);
4046 goto again;
4047 }
4048
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004049 goto unlock;
4050 }
4051
4052 user_extra = nr_pages + 1;
4053 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
4054
4055 /*
4056 * Increase the limit linearly with more CPUs:
4057 */
4058 user_lock_limit *= num_online_cpus();
4059
4060 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
4061
4062 extra = 0;
4063 if (user_locked > user_lock_limit)
4064 extra = user_locked - user_lock_limit;
4065
Jiri Slaby78d7d402010-03-05 13:42:54 -08004066 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004067 lock_limit >>= PAGE_SHIFT;
Christoph Lameterbc3e53f2011-10-31 17:07:30 -07004068 locked = vma->vm_mm->pinned_vm + extra;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004069
4070 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
4071 !capable(CAP_IPC_LOCK)) {
4072 ret = -EPERM;
4073 goto unlock;
4074 }
4075
Frederic Weisbecker76369132011-05-19 19:55:04 +02004076 WARN_ON(event->rb);
Peter Zijlstra906010b2009-09-21 16:08:49 +02004077
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004078 if (vma->vm_flags & VM_WRITE)
Frederic Weisbecker76369132011-05-19 19:55:04 +02004079 flags |= RING_BUFFER_WRITABLE;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02004080
Vince Weaver4ec83632011-06-01 15:15:36 -04004081 rb = rb_alloc(nr_pages,
4082 event->attr.watermark ? event->attr.wakeup_watermark : 0,
4083 event->cpu, flags);
4084
Frederic Weisbecker76369132011-05-19 19:55:04 +02004085 if (!rb) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004086 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004087 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004088 }
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004089
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004090 atomic_set(&rb->mmap_count, 1);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004091 rb->mmap_locked = extra;
4092 rb->mmap_user = get_current_user();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004093
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004094 atomic_long_add(user_extra, &user->locked_vm);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004095 vma->vm_mm->pinned_vm += extra;
4096
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004097 ring_buffer_attach(event, rb);
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004098 rcu_assign_pointer(event->rb, rb);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004099
Peter Zijlstrafa7315872013-09-19 10:16:42 +02004100 perf_event_init_userpage(event);
Peter Zijlstra9a0f05c2011-11-21 15:13:29 +01004101 perf_event_update_userpage(event);
4102
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004103unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02004104 if (!ret)
4105 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004106 mutex_unlock(&event->mmap_mutex);
4107
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02004108 /*
4109 * Since pinned accounting is per vm we cannot allow fork() to copy our
4110 * vma.
4111 */
Peter Zijlstra26cb63a2013-05-28 10:55:48 +02004112 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004113 vma->vm_ops = &perf_mmap_vmops;
4114
4115 return ret;
4116}
4117
4118static int perf_fasync(int fd, struct file *filp, int on)
4119{
Al Viro496ad9a2013-01-23 17:07:38 -05004120 struct inode *inode = file_inode(filp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004121 struct perf_event *event = filp->private_data;
4122 int retval;
4123
4124 mutex_lock(&inode->i_mutex);
4125 retval = fasync_helper(fd, filp, on, &event->fasync);
4126 mutex_unlock(&inode->i_mutex);
4127
4128 if (retval < 0)
4129 return retval;
4130
4131 return 0;
4132}
4133
4134static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01004135 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004136 .release = perf_release,
4137 .read = perf_read,
4138 .poll = perf_poll,
4139 .unlocked_ioctl = perf_ioctl,
4140 .compat_ioctl = perf_ioctl,
4141 .mmap = perf_mmap,
4142 .fasync = perf_fasync,
4143};
4144
4145/*
4146 * Perf event wakeup
4147 *
4148 * If there's data, ensure we set the poll() state and publish everything
4149 * to user-space before waking everybody up.
4150 */
4151
4152void perf_event_wakeup(struct perf_event *event)
4153{
Peter Zijlstra10c6db12011-11-26 02:47:31 +01004154 ring_buffer_wakeup(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004155
4156 if (event->pending_kill) {
4157 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
4158 event->pending_kill = 0;
4159 }
4160}
4161
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004162static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004163{
4164 struct perf_event *event = container_of(entry,
4165 struct perf_event, pending);
4166
4167 if (event->pending_disable) {
4168 event->pending_disable = 0;
4169 __perf_event_disable(event);
4170 }
4171
4172 if (event->pending_wakeup) {
4173 event->pending_wakeup = 0;
4174 perf_event_wakeup(event);
4175 }
4176}
4177
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004178/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004179 * We assume there is only KVM supporting the callbacks.
4180 * Later on, we might change it to a list if there is
4181 * another virtualization implementation supporting the callbacks.
4182 */
4183struct perf_guest_info_callbacks *perf_guest_cbs;
4184
4185int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4186{
4187 perf_guest_cbs = cbs;
4188 return 0;
4189}
4190EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
4191
4192int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
4193{
4194 perf_guest_cbs = NULL;
4195 return 0;
4196}
4197EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
4198
Jiri Olsa40189942012-08-07 15:20:37 +02004199static void
4200perf_output_sample_regs(struct perf_output_handle *handle,
4201 struct pt_regs *regs, u64 mask)
4202{
4203 int bit;
4204
4205 for_each_set_bit(bit, (const unsigned long *) &mask,
4206 sizeof(mask) * BITS_PER_BYTE) {
4207 u64 val;
4208
4209 val = perf_reg_value(regs, bit);
4210 perf_output_put(handle, val);
4211 }
4212}
4213
4214static void perf_sample_regs_user(struct perf_regs_user *regs_user,
4215 struct pt_regs *regs)
4216{
4217 if (!user_mode(regs)) {
4218 if (current->mm)
4219 regs = task_pt_regs(current);
4220 else
4221 regs = NULL;
4222 }
4223
4224 if (regs) {
4225 regs_user->regs = regs;
4226 regs_user->abi = perf_reg_abi(current);
4227 }
4228}
4229
Jiri Olsac5ebced2012-08-07 15:20:40 +02004230/*
4231 * Get remaining task size from user stack pointer.
4232 *
4233 * It'd be better to take stack vma map and limit this more
4234 * precisly, but there's no way to get it safely under interrupt,
4235 * so using TASK_SIZE as limit.
4236 */
4237static u64 perf_ustack_task_size(struct pt_regs *regs)
4238{
4239 unsigned long addr = perf_user_stack_pointer(regs);
4240
4241 if (!addr || addr >= TASK_SIZE)
4242 return 0;
4243
4244 return TASK_SIZE - addr;
4245}
4246
4247static u16
4248perf_sample_ustack_size(u16 stack_size, u16 header_size,
4249 struct pt_regs *regs)
4250{
4251 u64 task_size;
4252
4253 /* No regs, no stack pointer, no dump. */
4254 if (!regs)
4255 return 0;
4256
4257 /*
4258 * Check if we fit in with the requested stack size into the:
4259 * - TASK_SIZE
4260 * If we don't, we limit the size to the TASK_SIZE.
4261 *
4262 * - remaining sample size
4263 * If we don't, we customize the stack size to
4264 * fit in to the remaining sample size.
4265 */
4266
4267 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
4268 stack_size = min(stack_size, (u16) task_size);
4269
4270 /* Current header size plus static size and dynamic size. */
4271 header_size += 2 * sizeof(u64);
4272
4273 /* Do we fit in with the current stack dump size? */
4274 if ((u16) (header_size + stack_size) < header_size) {
4275 /*
4276 * If we overflow the maximum size for the sample,
4277 * we customize the stack dump size to fit in.
4278 */
4279 stack_size = USHRT_MAX - header_size - sizeof(u64);
4280 stack_size = round_up(stack_size, sizeof(u64));
4281 }
4282
4283 return stack_size;
4284}
4285
4286static void
4287perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
4288 struct pt_regs *regs)
4289{
4290 /* Case of a kernel thread, nothing to dump */
4291 if (!regs) {
4292 u64 size = 0;
4293 perf_output_put(handle, size);
4294 } else {
4295 unsigned long sp;
4296 unsigned int rem;
4297 u64 dyn_size;
4298
4299 /*
4300 * We dump:
4301 * static size
4302 * - the size requested by user or the best one we can fit
4303 * in to the sample max size
4304 * data
4305 * - user stack dump data
4306 * dynamic size
4307 * - the actual dumped size
4308 */
4309
4310 /* Static size. */
4311 perf_output_put(handle, dump_size);
4312
4313 /* Data. */
4314 sp = perf_user_stack_pointer(regs);
4315 rem = __output_copy_user(handle, (void *) sp, dump_size);
4316 dyn_size = dump_size - rem;
4317
4318 perf_output_skip(handle, rem);
4319
4320 /* Dynamic size. */
4321 perf_output_put(handle, dyn_size);
4322 }
4323}
4324
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004325static void __perf_event_header__init_id(struct perf_event_header *header,
4326 struct perf_sample_data *data,
4327 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004328{
4329 u64 sample_type = event->attr.sample_type;
4330
4331 data->type = sample_type;
4332 header->size += event->id_header_size;
4333
4334 if (sample_type & PERF_SAMPLE_TID) {
4335 /* namespace issues */
4336 data->tid_entry.pid = perf_event_pid(event, current);
4337 data->tid_entry.tid = perf_event_tid(event, current);
4338 }
4339
4340 if (sample_type & PERF_SAMPLE_TIME)
4341 data->time = perf_clock();
4342
Adrian Hunterff3d5272013-08-27 11:23:07 +03004343 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004344 data->id = primary_event_id(event);
4345
4346 if (sample_type & PERF_SAMPLE_STREAM_ID)
4347 data->stream_id = event->id;
4348
4349 if (sample_type & PERF_SAMPLE_CPU) {
4350 data->cpu_entry.cpu = raw_smp_processor_id();
4351 data->cpu_entry.reserved = 0;
4352 }
4353}
4354
Frederic Weisbecker76369132011-05-19 19:55:04 +02004355void perf_event_header__init_id(struct perf_event_header *header,
4356 struct perf_sample_data *data,
4357 struct perf_event *event)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004358{
4359 if (event->attr.sample_id_all)
4360 __perf_event_header__init_id(header, data, event);
4361}
4362
4363static void __perf_event__output_id_sample(struct perf_output_handle *handle,
4364 struct perf_sample_data *data)
4365{
4366 u64 sample_type = data->type;
4367
4368 if (sample_type & PERF_SAMPLE_TID)
4369 perf_output_put(handle, data->tid_entry);
4370
4371 if (sample_type & PERF_SAMPLE_TIME)
4372 perf_output_put(handle, data->time);
4373
4374 if (sample_type & PERF_SAMPLE_ID)
4375 perf_output_put(handle, data->id);
4376
4377 if (sample_type & PERF_SAMPLE_STREAM_ID)
4378 perf_output_put(handle, data->stream_id);
4379
4380 if (sample_type & PERF_SAMPLE_CPU)
4381 perf_output_put(handle, data->cpu_entry);
Adrian Hunterff3d5272013-08-27 11:23:07 +03004382
4383 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4384 perf_output_put(handle, data->id);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004385}
4386
Frederic Weisbecker76369132011-05-19 19:55:04 +02004387void perf_event__output_id_sample(struct perf_event *event,
4388 struct perf_output_handle *handle,
4389 struct perf_sample_data *sample)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004390{
4391 if (event->attr.sample_id_all)
4392 __perf_event__output_id_sample(handle, sample);
4393}
4394
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004395static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004396 struct perf_event *event,
4397 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004398{
4399 u64 read_format = event->attr.read_format;
4400 u64 values[4];
4401 int n = 0;
4402
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004403 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004404 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004405 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004406 atomic64_read(&event->child_total_time_enabled);
4407 }
4408 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004409 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004410 atomic64_read(&event->child_total_time_running);
4411 }
4412 if (read_format & PERF_FORMAT_ID)
4413 values[n++] = primary_event_id(event);
4414
Frederic Weisbecker76369132011-05-19 19:55:04 +02004415 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004416}
4417
4418/*
4419 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4420 */
4421static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004422 struct perf_event *event,
4423 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004424{
4425 struct perf_event *leader = event->group_leader, *sub;
4426 u64 read_format = event->attr.read_format;
4427 u64 values[5];
4428 int n = 0;
4429
4430 values[n++] = 1 + leader->nr_siblings;
4431
4432 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004433 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004434
4435 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004436 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004437
4438 if (leader != event)
4439 leader->pmu->read(leader);
4440
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004441 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004442 if (read_format & PERF_FORMAT_ID)
4443 values[n++] = primary_event_id(leader);
4444
Frederic Weisbecker76369132011-05-19 19:55:04 +02004445 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004446
4447 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4448 n = 0;
4449
Jiri Olsa6f5ab002012-10-15 20:13:45 +02004450 if ((sub != event) &&
4451 (sub->state == PERF_EVENT_STATE_ACTIVE))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004452 sub->pmu->read(sub);
4453
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004454 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004455 if (read_format & PERF_FORMAT_ID)
4456 values[n++] = primary_event_id(sub);
4457
Frederic Weisbecker76369132011-05-19 19:55:04 +02004458 __output_copy(handle, values, n * sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004459 }
4460}
4461
Stephane Eranianeed01522010-10-26 16:08:01 +02004462#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4463 PERF_FORMAT_TOTAL_TIME_RUNNING)
4464
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004465static void perf_output_read(struct perf_output_handle *handle,
4466 struct perf_event *event)
4467{
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004468 u64 enabled = 0, running = 0, now;
Stephane Eranianeed01522010-10-26 16:08:01 +02004469 u64 read_format = event->attr.read_format;
4470
4471 /*
4472 * compute total_time_enabled, total_time_running
4473 * based on snapshot values taken when the event
4474 * was last scheduled in.
4475 *
4476 * we cannot simply called update_context_time()
4477 * because of locking issue as we are called in
4478 * NMI context
4479 */
Eric B Munsonc4794292011-06-23 16:34:38 -04004480 if (read_format & PERF_FORMAT_TOTAL_TIMES)
Peter Zijlstrae3f35412011-11-21 11:43:53 +01004481 calc_timer_values(event, &now, &enabled, &running);
Stephane Eranianeed01522010-10-26 16:08:01 +02004482
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004483 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004484 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004485 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004486 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004487}
4488
4489void perf_output_sample(struct perf_output_handle *handle,
4490 struct perf_event_header *header,
4491 struct perf_sample_data *data,
4492 struct perf_event *event)
4493{
4494 u64 sample_type = data->type;
4495
4496 perf_output_put(handle, *header);
4497
Adrian Hunterff3d5272013-08-27 11:23:07 +03004498 if (sample_type & PERF_SAMPLE_IDENTIFIER)
4499 perf_output_put(handle, data->id);
4500
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004501 if (sample_type & PERF_SAMPLE_IP)
4502 perf_output_put(handle, data->ip);
4503
4504 if (sample_type & PERF_SAMPLE_TID)
4505 perf_output_put(handle, data->tid_entry);
4506
4507 if (sample_type & PERF_SAMPLE_TIME)
4508 perf_output_put(handle, data->time);
4509
4510 if (sample_type & PERF_SAMPLE_ADDR)
4511 perf_output_put(handle, data->addr);
4512
4513 if (sample_type & PERF_SAMPLE_ID)
4514 perf_output_put(handle, data->id);
4515
4516 if (sample_type & PERF_SAMPLE_STREAM_ID)
4517 perf_output_put(handle, data->stream_id);
4518
4519 if (sample_type & PERF_SAMPLE_CPU)
4520 perf_output_put(handle, data->cpu_entry);
4521
4522 if (sample_type & PERF_SAMPLE_PERIOD)
4523 perf_output_put(handle, data->period);
4524
4525 if (sample_type & PERF_SAMPLE_READ)
4526 perf_output_read(handle, event);
4527
4528 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4529 if (data->callchain) {
4530 int size = 1;
4531
4532 if (data->callchain)
4533 size += data->callchain->nr;
4534
4535 size *= sizeof(u64);
4536
Frederic Weisbecker76369132011-05-19 19:55:04 +02004537 __output_copy(handle, data->callchain, size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004538 } else {
4539 u64 nr = 0;
4540 perf_output_put(handle, nr);
4541 }
4542 }
4543
4544 if (sample_type & PERF_SAMPLE_RAW) {
4545 if (data->raw) {
4546 perf_output_put(handle, data->raw->size);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004547 __output_copy(handle, data->raw->data,
4548 data->raw->size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004549 } else {
4550 struct {
4551 u32 size;
4552 u32 data;
4553 } raw = {
4554 .size = sizeof(u32),
4555 .data = 0,
4556 };
4557 perf_output_put(handle, raw);
4558 }
4559 }
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004560
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004561 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4562 if (data->br_stack) {
4563 size_t size;
4564
4565 size = data->br_stack->nr
4566 * sizeof(struct perf_branch_entry);
4567
4568 perf_output_put(handle, data->br_stack->nr);
4569 perf_output_copy(handle, data->br_stack->entries, size);
4570 } else {
4571 /*
4572 * we always store at least the value of nr
4573 */
4574 u64 nr = 0;
4575 perf_output_put(handle, nr);
4576 }
4577 }
Jiri Olsa40189942012-08-07 15:20:37 +02004578
4579 if (sample_type & PERF_SAMPLE_REGS_USER) {
4580 u64 abi = data->regs_user.abi;
4581
4582 /*
4583 * If there are no regs to dump, notice it through
4584 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
4585 */
4586 perf_output_put(handle, abi);
4587
4588 if (abi) {
4589 u64 mask = event->attr.sample_regs_user;
4590 perf_output_sample_regs(handle,
4591 data->regs_user.regs,
4592 mask);
4593 }
4594 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004595
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004596 if (sample_type & PERF_SAMPLE_STACK_USER) {
Jiri Olsac5ebced2012-08-07 15:20:40 +02004597 perf_output_sample_ustack(handle,
4598 data->stack_user_size,
4599 data->regs_user.regs);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004600 }
Andi Kleenc3feedf2013-01-24 16:10:28 +01004601
4602 if (sample_type & PERF_SAMPLE_WEIGHT)
4603 perf_output_put(handle, data->weight);
Stephane Eraniand6be9ad2013-01-24 16:10:31 +01004604
4605 if (sample_type & PERF_SAMPLE_DATA_SRC)
4606 perf_output_put(handle, data->data_src.val);
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004607
Andi Kleenfdfbbd02013-09-20 07:40:39 -07004608 if (sample_type & PERF_SAMPLE_TRANSACTION)
4609 perf_output_put(handle, data->txn);
4610
Peter Zijlstraa5cdd402013-07-16 17:09:07 +02004611 if (!event->attr.watermark) {
4612 int wakeup_events = event->attr.wakeup_events;
4613
4614 if (wakeup_events) {
4615 struct ring_buffer *rb = handle->rb;
4616 int events = local_inc_return(&rb->events);
4617
4618 if (events >= wakeup_events) {
4619 local_sub(wakeup_events, &rb->events);
4620 local_inc(&rb->wakeup);
4621 }
4622 }
4623 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004624}
4625
4626void perf_prepare_sample(struct perf_event_header *header,
4627 struct perf_sample_data *data,
4628 struct perf_event *event,
4629 struct pt_regs *regs)
4630{
4631 u64 sample_type = event->attr.sample_type;
4632
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004633 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004634 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004635
4636 header->misc = 0;
4637 header->misc |= perf_misc_flags(regs);
4638
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004639 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004640
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004641 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004642 data->ip = perf_instruction_pointer(regs);
4643
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004644 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4645 int size = 1;
4646
Andrew Vagine6dab5f2012-07-11 18:14:58 +04004647 data->callchain = perf_callchain(event, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004648
4649 if (data->callchain)
4650 size += data->callchain->nr;
4651
4652 header->size += size * sizeof(u64);
4653 }
4654
4655 if (sample_type & PERF_SAMPLE_RAW) {
4656 int size = sizeof(u32);
4657
4658 if (data->raw)
4659 size += data->raw->size;
4660 else
4661 size += sizeof(u32);
4662
4663 WARN_ON_ONCE(size & (sizeof(u64)-1));
4664 header->size += size;
4665 }
Stephane Eranianbce38cd2012-02-09 23:20:51 +01004666
4667 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
4668 int size = sizeof(u64); /* nr */
4669 if (data->br_stack) {
4670 size += data->br_stack->nr
4671 * sizeof(struct perf_branch_entry);
4672 }
4673 header->size += size;
4674 }
Jiri Olsa40189942012-08-07 15:20:37 +02004675
4676 if (sample_type & PERF_SAMPLE_REGS_USER) {
4677 /* regs dump ABI info */
4678 int size = sizeof(u64);
4679
4680 perf_sample_regs_user(&data->regs_user, regs);
4681
4682 if (data->regs_user.regs) {
4683 u64 mask = event->attr.sample_regs_user;
4684 size += hweight64(mask) * sizeof(u64);
4685 }
4686
4687 header->size += size;
4688 }
Jiri Olsac5ebced2012-08-07 15:20:40 +02004689
4690 if (sample_type & PERF_SAMPLE_STACK_USER) {
4691 /*
4692 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
4693 * processed as the last one or have additional check added
4694 * in case new sample type is added, because we could eat
4695 * up the rest of the sample size.
4696 */
4697 struct perf_regs_user *uregs = &data->regs_user;
4698 u16 stack_size = event->attr.sample_stack_user;
4699 u16 size = sizeof(u64);
4700
4701 if (!uregs->abi)
4702 perf_sample_regs_user(uregs, regs);
4703
4704 stack_size = perf_sample_ustack_size(stack_size, header->size,
4705 uregs->regs);
4706
4707 /*
4708 * If there is something to dump, add space for the dump
4709 * itself and for the field that tells the dynamic size,
4710 * which is how many have been actually dumped.
4711 */
4712 if (stack_size)
4713 size += sizeof(u64) + stack_size;
4714
4715 data->stack_user_size = stack_size;
4716 header->size += size;
4717 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004718}
4719
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02004720static void perf_event_output(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004721 struct perf_sample_data *data,
4722 struct pt_regs *regs)
4723{
4724 struct perf_output_handle handle;
4725 struct perf_event_header header;
4726
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004727 /* protect the callchain buffers */
4728 rcu_read_lock();
4729
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004730 perf_prepare_sample(&header, data, event, regs);
4731
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004732 if (perf_output_begin(&handle, event, header.size))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004733 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004734
4735 perf_output_sample(&handle, &header, data, event);
4736
4737 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004738
4739exit:
4740 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004741}
4742
4743/*
4744 * read event_id
4745 */
4746
4747struct perf_read_event {
4748 struct perf_event_header header;
4749
4750 u32 pid;
4751 u32 tid;
4752};
4753
4754static void
4755perf_event_read_event(struct perf_event *event,
4756 struct task_struct *task)
4757{
4758 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004759 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004760 struct perf_read_event read_event = {
4761 .header = {
4762 .type = PERF_RECORD_READ,
4763 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004764 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004765 },
4766 .pid = perf_event_pid(event, task),
4767 .tid = perf_event_tid(event, task),
4768 };
4769 int ret;
4770
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004771 perf_event_header__init_id(&read_event.header, &sample, event);
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004772 ret = perf_output_begin(&handle, event, read_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004773 if (ret)
4774 return;
4775
4776 perf_output_put(&handle, read_event);
4777 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004778 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004779
4780 perf_output_end(&handle);
4781}
4782
Jiri Olsa52d857a2013-05-06 18:27:18 +02004783typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
4784
4785static void
4786perf_event_aux_ctx(struct perf_event_context *ctx,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004787 perf_event_aux_output_cb output,
4788 void *data)
4789{
4790 struct perf_event *event;
4791
4792 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4793 if (event->state < PERF_EVENT_STATE_INACTIVE)
4794 continue;
4795 if (!event_filter_match(event))
4796 continue;
Jiri Olsa67516842013-07-09 18:56:31 +02004797 output(event, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004798 }
4799}
4800
4801static void
Jiri Olsa67516842013-07-09 18:56:31 +02004802perf_event_aux(perf_event_aux_output_cb output, void *data,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004803 struct perf_event_context *task_ctx)
4804{
4805 struct perf_cpu_context *cpuctx;
4806 struct perf_event_context *ctx;
4807 struct pmu *pmu;
4808 int ctxn;
4809
4810 rcu_read_lock();
4811 list_for_each_entry_rcu(pmu, &pmus, entry) {
4812 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
4813 if (cpuctx->unique_pmu != pmu)
4814 goto next;
Jiri Olsa67516842013-07-09 18:56:31 +02004815 perf_event_aux_ctx(&cpuctx->ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004816 if (task_ctx)
4817 goto next;
4818 ctxn = pmu->task_ctx_nr;
4819 if (ctxn < 0)
4820 goto next;
4821 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4822 if (ctx)
Jiri Olsa67516842013-07-09 18:56:31 +02004823 perf_event_aux_ctx(ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004824next:
4825 put_cpu_ptr(pmu->pmu_cpu_context);
4826 }
4827
4828 if (task_ctx) {
4829 preempt_disable();
Jiri Olsa67516842013-07-09 18:56:31 +02004830 perf_event_aux_ctx(task_ctx, output, data);
Jiri Olsa52d857a2013-05-06 18:27:18 +02004831 preempt_enable();
4832 }
4833 rcu_read_unlock();
4834}
4835
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004836/*
4837 * task tracking -- fork/exit
4838 *
Stephane Eranian13d7a242013-08-21 12:10:24 +02004839 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004840 */
4841
4842struct perf_task_event {
4843 struct task_struct *task;
4844 struct perf_event_context *task_ctx;
4845
4846 struct {
4847 struct perf_event_header header;
4848
4849 u32 pid;
4850 u32 ppid;
4851 u32 tid;
4852 u32 ptid;
4853 u64 time;
4854 } event_id;
4855};
4856
Jiri Olsa67516842013-07-09 18:56:31 +02004857static int perf_event_task_match(struct perf_event *event)
4858{
Stephane Eranian13d7a242013-08-21 12:10:24 +02004859 return event->attr.comm || event->attr.mmap ||
4860 event->attr.mmap2 || event->attr.mmap_data ||
4861 event->attr.task;
Jiri Olsa67516842013-07-09 18:56:31 +02004862}
4863
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004864static void perf_event_task_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004865 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004866{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004867 struct perf_task_event *task_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004868 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004869 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004870 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004871 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004872
Jiri Olsa67516842013-07-09 18:56:31 +02004873 if (!perf_event_task_match(event))
4874 return;
4875
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004876 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004877
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004878 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004879 task_event->event_id.header.size);
Peter Zijlstraef607772010-05-18 10:50:41 +02004880 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004881 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004882
4883 task_event->event_id.pid = perf_event_pid(event, task);
4884 task_event->event_id.ppid = perf_event_pid(event, current);
4885
4886 task_event->event_id.tid = perf_event_tid(event, task);
4887 task_event->event_id.ptid = perf_event_tid(event, current);
4888
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004889 perf_output_put(&handle, task_event->event_id);
4890
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004891 perf_event__output_id_sample(event, &handle, &sample);
4892
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004893 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004894out:
4895 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004896}
4897
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004898static void perf_event_task(struct task_struct *task,
4899 struct perf_event_context *task_ctx,
4900 int new)
4901{
4902 struct perf_task_event task_event;
4903
4904 if (!atomic_read(&nr_comm_events) &&
4905 !atomic_read(&nr_mmap_events) &&
4906 !atomic_read(&nr_task_events))
4907 return;
4908
4909 task_event = (struct perf_task_event){
4910 .task = task,
4911 .task_ctx = task_ctx,
4912 .event_id = {
4913 .header = {
4914 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4915 .misc = 0,
4916 .size = sizeof(task_event.event_id),
4917 },
4918 /* .pid */
4919 /* .ppid */
4920 /* .tid */
4921 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004922 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004923 },
4924 };
4925
Jiri Olsa67516842013-07-09 18:56:31 +02004926 perf_event_aux(perf_event_task_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004927 &task_event,
4928 task_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004929}
4930
4931void perf_event_fork(struct task_struct *task)
4932{
4933 perf_event_task(task, NULL, 1);
4934}
4935
4936/*
4937 * comm tracking
4938 */
4939
4940struct perf_comm_event {
4941 struct task_struct *task;
4942 char *comm;
4943 int comm_size;
4944
4945 struct {
4946 struct perf_event_header header;
4947
4948 u32 pid;
4949 u32 tid;
4950 } event_id;
4951};
4952
Jiri Olsa67516842013-07-09 18:56:31 +02004953static int perf_event_comm_match(struct perf_event *event)
4954{
4955 return event->attr.comm;
4956}
4957
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004958static void perf_event_comm_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02004959 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004960{
Jiri Olsa52d857a2013-05-06 18:27:18 +02004961 struct perf_comm_event *comm_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004962 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004963 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004964 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004965 int ret;
4966
Jiri Olsa67516842013-07-09 18:56:31 +02004967 if (!perf_event_comm_match(event))
4968 return;
4969
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004970 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4971 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02004972 comm_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004973
4974 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004975 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004976
4977 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4978 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4979
4980 perf_output_put(&handle, comm_event->event_id);
Frederic Weisbecker76369132011-05-19 19:55:04 +02004981 __output_copy(&handle, comm_event->comm,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004982 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004983
4984 perf_event__output_id_sample(event, &handle, &sample);
4985
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004986 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004987out:
4988 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004989}
4990
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004991static void perf_event_comm_event(struct perf_comm_event *comm_event)
4992{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004993 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004994 unsigned int size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004995
4996 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01004997 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004998 size = ALIGN(strlen(comm)+1, sizeof(u64));
4999
5000 comm_event->comm = comm;
5001 comm_event->comm_size = size;
5002
5003 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005004
Jiri Olsa67516842013-07-09 18:56:31 +02005005 perf_event_aux(perf_event_comm_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005006 comm_event,
5007 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005008}
5009
5010void perf_event_comm(struct task_struct *task)
5011{
5012 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005013 struct perf_event_context *ctx;
5014 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005015
Paul E. McKenneyc79aa0d92013-04-19 12:01:24 -07005016 rcu_read_lock();
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005017 for_each_task_context_nr(ctxn) {
5018 ctx = task->perf_event_ctxp[ctxn];
5019 if (!ctx)
5020 continue;
5021
5022 perf_event_enable_on_exec(ctx);
5023 }
Paul E. McKenneyc79aa0d92013-04-19 12:01:24 -07005024 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005025
5026 if (!atomic_read(&nr_comm_events))
5027 return;
5028
5029 comm_event = (struct perf_comm_event){
5030 .task = task,
5031 /* .comm */
5032 /* .comm_size */
5033 .event_id = {
5034 .header = {
5035 .type = PERF_RECORD_COMM,
5036 .misc = 0,
5037 /* .size */
5038 },
5039 /* .pid */
5040 /* .tid */
5041 },
5042 };
5043
5044 perf_event_comm_event(&comm_event);
5045}
5046
5047/*
5048 * mmap tracking
5049 */
5050
5051struct perf_mmap_event {
5052 struct vm_area_struct *vma;
5053
5054 const char *file_name;
5055 int file_size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005056 int maj, min;
5057 u64 ino;
5058 u64 ino_generation;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005059
5060 struct {
5061 struct perf_event_header header;
5062
5063 u32 pid;
5064 u32 tid;
5065 u64 start;
5066 u64 len;
5067 u64 pgoff;
5068 } event_id;
5069};
5070
Jiri Olsa67516842013-07-09 18:56:31 +02005071static int perf_event_mmap_match(struct perf_event *event,
5072 void *data)
5073{
5074 struct perf_mmap_event *mmap_event = data;
5075 struct vm_area_struct *vma = mmap_event->vma;
5076 int executable = vma->vm_flags & VM_EXEC;
5077
5078 return (!executable && event->attr.mmap_data) ||
Stephane Eranian13d7a242013-08-21 12:10:24 +02005079 (executable && (event->attr.mmap || event->attr.mmap2));
Jiri Olsa67516842013-07-09 18:56:31 +02005080}
5081
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005082static void perf_event_mmap_output(struct perf_event *event,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005083 void *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005084{
Jiri Olsa52d857a2013-05-06 18:27:18 +02005085 struct perf_mmap_event *mmap_event = data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005086 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005087 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005088 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005089 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005090
Jiri Olsa67516842013-07-09 18:56:31 +02005091 if (!perf_event_mmap_match(event, data))
5092 return;
5093
Stephane Eranian13d7a242013-08-21 12:10:24 +02005094 if (event->attr.mmap2) {
5095 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
5096 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
5097 mmap_event->event_id.header.size += sizeof(mmap_event->min);
5098 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
Arnaldo Carvalho de Melod008d522013-09-10 10:24:05 -03005099 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005100 }
5101
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005102 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
5103 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005104 mmap_event->event_id.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005105 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005106 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005107
5108 mmap_event->event_id.pid = perf_event_pid(event, current);
5109 mmap_event->event_id.tid = perf_event_tid(event, current);
5110
5111 perf_output_put(&handle, mmap_event->event_id);
Stephane Eranian13d7a242013-08-21 12:10:24 +02005112
5113 if (event->attr.mmap2) {
5114 perf_output_put(&handle, mmap_event->maj);
5115 perf_output_put(&handle, mmap_event->min);
5116 perf_output_put(&handle, mmap_event->ino);
5117 perf_output_put(&handle, mmap_event->ino_generation);
5118 }
5119
Frederic Weisbecker76369132011-05-19 19:55:04 +02005120 __output_copy(&handle, mmap_event->file_name,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005121 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005122
5123 perf_event__output_id_sample(event, &handle, &sample);
5124
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005125 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005126out:
5127 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005128}
5129
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005130static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
5131{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005132 struct vm_area_struct *vma = mmap_event->vma;
5133 struct file *file = vma->vm_file;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005134 int maj = 0, min = 0;
5135 u64 ino = 0, gen = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005136 unsigned int size;
5137 char tmp[16];
5138 char *buf = NULL;
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02005139 char *name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005140
5141 if (file) {
Stephane Eranian13d7a242013-08-21 12:10:24 +02005142 struct inode *inode;
5143 dev_t dev;
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005144
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02005145 buf = kmalloc(PATH_MAX, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005146 if (!buf) {
5147 name = strncpy(tmp, "//enomem", sizeof(tmp));
5148 goto got_name;
5149 }
Oleg Nesterov3ea2f2b2013-10-16 22:10:04 +02005150 /*
5151 * d_path() works from the end of the rb backwards, so we
5152 * need to add enough zero bytes after the string to handle
5153 * the 64bit alignment we do later.
5154 */
5155 name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005156 if (IS_ERR(name)) {
5157 name = strncpy(tmp, "//toolong", sizeof(tmp));
5158 goto got_name;
5159 }
Stephane Eranian13d7a242013-08-21 12:10:24 +02005160 inode = file_inode(vma->vm_file);
5161 dev = inode->i_sb->s_dev;
5162 ino = inode->i_ino;
5163 gen = inode->i_generation;
5164 maj = MAJOR(dev);
5165 min = MINOR(dev);
5166
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005167 } else {
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02005168 name = (char *)arch_vma_name(vma);
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005169 if (name) {
5170 name = strncpy(tmp, name, sizeof(tmp) - 1);
Chen Gangc97847d2013-04-08 11:48:27 +08005171 tmp[sizeof(tmp) - 1] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005172 goto got_name;
5173 }
5174
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005175 if (vma->vm_start <= vma->vm_mm->start_brk &&
Eric B Munson3af9e852010-05-18 15:30:49 +01005176 vma->vm_end >= vma->vm_mm->brk) {
5177 name = strncpy(tmp, "[heap]", sizeof(tmp));
5178 goto got_name;
Oleg Nesterov32c5fb72013-10-16 22:09:45 +02005179 }
5180 if (vma->vm_start <= vma->vm_mm->start_stack &&
Eric B Munson3af9e852010-05-18 15:30:49 +01005181 vma->vm_end >= vma->vm_mm->start_stack) {
5182 name = strncpy(tmp, "[stack]", sizeof(tmp));
5183 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005184 }
5185
5186 name = strncpy(tmp, "//anon", sizeof(tmp));
5187 goto got_name;
5188 }
5189
5190got_name:
Peter Zijlstra2c42cfbf2013-10-17 00:06:46 +02005191 /*
5192 * Since our buffer works in 8 byte units we need to align our string
5193 * size to a multiple of 8. However, we must guarantee the tail end is
5194 * zero'd out to avoid leaking random bits to userspace.
5195 */
5196 size = strlen(name)+1;
5197 while (!IS_ALIGNED(size, sizeof(u64)))
5198 name[size++] = '\0';
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005199
5200 mmap_event->file_name = name;
5201 mmap_event->file_size = size;
Stephane Eranian13d7a242013-08-21 12:10:24 +02005202 mmap_event->maj = maj;
5203 mmap_event->min = min;
5204 mmap_event->ino = ino;
5205 mmap_event->ino_generation = gen;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005206
Stephane Eranian2fe85422013-01-24 16:10:39 +01005207 if (!(vma->vm_flags & VM_EXEC))
5208 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
5209
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005210 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
5211
Jiri Olsa67516842013-07-09 18:56:31 +02005212 perf_event_aux(perf_event_mmap_output,
Jiri Olsa52d857a2013-05-06 18:27:18 +02005213 mmap_event,
5214 NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005215
5216 kfree(buf);
5217}
5218
Eric B Munson3af9e852010-05-18 15:30:49 +01005219void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005220{
5221 struct perf_mmap_event mmap_event;
5222
5223 if (!atomic_read(&nr_mmap_events))
5224 return;
5225
5226 mmap_event = (struct perf_mmap_event){
5227 .vma = vma,
5228 /* .file_name */
5229 /* .file_size */
5230 .event_id = {
5231 .header = {
5232 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08005233 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005234 /* .size */
5235 },
5236 /* .pid */
5237 /* .tid */
5238 .start = vma->vm_start,
5239 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01005240 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005241 },
Stephane Eranian13d7a242013-08-21 12:10:24 +02005242 /* .maj (attr_mmap2 only) */
5243 /* .min (attr_mmap2 only) */
5244 /* .ino (attr_mmap2 only) */
5245 /* .ino_generation (attr_mmap2 only) */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005246 };
5247
5248 perf_event_mmap_event(&mmap_event);
5249}
5250
5251/*
5252 * IRQ throttle logging
5253 */
5254
5255static void perf_log_throttle(struct perf_event *event, int enable)
5256{
5257 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005258 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005259 int ret;
5260
5261 struct {
5262 struct perf_event_header header;
5263 u64 time;
5264 u64 id;
5265 u64 stream_id;
5266 } throttle_event = {
5267 .header = {
5268 .type = PERF_RECORD_THROTTLE,
5269 .misc = 0,
5270 .size = sizeof(throttle_event),
5271 },
5272 .time = perf_clock(),
5273 .id = primary_event_id(event),
5274 .stream_id = event->id,
5275 };
5276
5277 if (enable)
5278 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
5279
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005280 perf_event_header__init_id(&throttle_event.header, &sample, event);
5281
5282 ret = perf_output_begin(&handle, event,
Peter Zijlstraa7ac67e2011-06-27 16:47:16 +02005283 throttle_event.header.size);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005284 if (ret)
5285 return;
5286
5287 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02005288 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005289 perf_output_end(&handle);
5290}
5291
5292/*
5293 * Generic event overflow handling, sampling.
5294 */
5295
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005296static int __perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005297 int throttle, struct perf_sample_data *data,
5298 struct pt_regs *regs)
5299{
5300 int events = atomic_read(&event->event_limit);
5301 struct hw_perf_event *hwc = &event->hw;
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005302 u64 seq;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005303 int ret = 0;
5304
Peter Zijlstra96398822010-11-24 18:55:29 +01005305 /*
5306 * Non-sampling counters might still use the PMI to fold short
5307 * hardware counters, ignore those.
5308 */
5309 if (unlikely(!is_sampling_event(event)))
5310 return 0;
5311
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005312 seq = __this_cpu_read(perf_throttled_seq);
5313 if (seq != hwc->interrupts_seq) {
5314 hwc->interrupts_seq = seq;
5315 hwc->interrupts = 1;
5316 } else {
5317 hwc->interrupts++;
5318 if (unlikely(throttle
5319 && hwc->interrupts >= max_samples_per_tick)) {
5320 __this_cpu_inc(perf_throttled_count);
Peter Zijlstra163ec432011-02-16 11:22:34 +01005321 hwc->interrupts = MAX_INTERRUPTS;
5322 perf_log_throttle(event, 0);
Frederic Weisbeckerd84153d2013-07-23 02:31:05 +02005323 tick_nohz_full_kick();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005324 ret = 1;
5325 }
Stephane Eraniane050e3f2012-01-26 17:03:19 +01005326 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005327
5328 if (event->attr.freq) {
5329 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01005330 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005331
Peter Zijlstraabd50712010-01-26 18:50:16 +01005332 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005333
Peter Zijlstraabd50712010-01-26 18:50:16 +01005334 if (delta > 0 && delta < 2*TICK_NSEC)
Stephane Eranianf39d47f2012-02-07 14:39:57 +01005335 perf_adjust_period(event, delta, hwc->last_period, true);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005336 }
5337
5338 /*
5339 * XXX event_limit might not quite work as expected on inherited
5340 * events
5341 */
5342
5343 event->pending_kill = POLL_IN;
5344 if (events && atomic_dec_and_test(&event->event_limit)) {
5345 ret = 1;
5346 event->pending_kill = POLL_HUP;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005347 event->pending_disable = 1;
5348 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005349 }
5350
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005351 if (event->overflow_handler)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005352 event->overflow_handler(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005353 else
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005354 perf_event_output(event, data, regs);
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005355
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005356 if (event->fasync && event->pending_kill) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005357 event->pending_wakeup = 1;
5358 irq_work_queue(&event->pending);
Peter Zijlstraf506b3d2011-05-26 17:02:53 +02005359 }
5360
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005361 return ret;
5362}
5363
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005364int perf_event_overflow(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005365 struct perf_sample_data *data,
5366 struct pt_regs *regs)
5367{
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005368 return __perf_event_overflow(event, 1, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005369}
5370
5371/*
5372 * Generic software event infrastructure
5373 */
5374
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005375struct swevent_htable {
5376 struct swevent_hlist *swevent_hlist;
5377 struct mutex hlist_mutex;
5378 int hlist_refcount;
5379
5380 /* Recursion avoidance in each contexts */
5381 int recursion[PERF_NR_CONTEXTS];
5382};
5383
5384static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5385
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005386/*
5387 * We directly increment event->count and keep a second value in
5388 * event->hw.period_left to count intervals. This period event
5389 * is kept in the range [-sample_period, 0] so that we can use the
5390 * sign as trigger.
5391 */
5392
Jiri Olsaab573842013-05-01 17:25:44 +02005393u64 perf_swevent_set_period(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005394{
5395 struct hw_perf_event *hwc = &event->hw;
5396 u64 period = hwc->last_period;
5397 u64 nr, offset;
5398 s64 old, val;
5399
5400 hwc->last_period = hwc->sample_period;
5401
5402again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005403 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005404 if (val < 0)
5405 return 0;
5406
5407 nr = div64_u64(period + val, period);
5408 offset = nr * period;
5409 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005410 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005411 goto again;
5412
5413 return nr;
5414}
5415
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005416static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005417 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005418 struct pt_regs *regs)
5419{
5420 struct hw_perf_event *hwc = &event->hw;
5421 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005422
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005423 if (!overflow)
5424 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005425
5426 if (hwc->interrupts == MAX_INTERRUPTS)
5427 return;
5428
5429 for (; overflow; overflow--) {
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005430 if (__perf_event_overflow(event, throttle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005431 data, regs)) {
5432 /*
5433 * We inhibit the overflow from happening when
5434 * hwc->interrupts == MAX_INTERRUPTS.
5435 */
5436 break;
5437 }
5438 throttle = 1;
5439 }
5440}
5441
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005442static void perf_swevent_event(struct perf_event *event, u64 nr,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005443 struct perf_sample_data *data,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005444 struct pt_regs *regs)
5445{
5446 struct hw_perf_event *hwc = &event->hw;
5447
Peter Zijlstrae7850592010-05-21 14:43:08 +02005448 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005449
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005450 if (!regs)
5451 return;
5452
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005453 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005454 return;
5455
Andrew Vagin5d81e5c2011-11-07 15:54:12 +03005456 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
5457 data->period = nr;
5458 return perf_swevent_overflow(event, 1, data, regs);
5459 } else
5460 data->period = event->hw.last_period;
5461
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005462 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005463 return perf_swevent_overflow(event, 1, data, regs);
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005464
Peter Zijlstrae7850592010-05-21 14:43:08 +02005465 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005466 return;
5467
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005468 perf_swevent_overflow(event, 0, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005469}
5470
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005471static int perf_exclude_event(struct perf_event *event,
5472 struct pt_regs *regs)
5473{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005474 if (event->hw.state & PERF_HES_STOPPED)
Frederic Weisbecker91b2f482011-03-07 21:27:08 +01005475 return 1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005476
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005477 if (regs) {
5478 if (event->attr.exclude_user && user_mode(regs))
5479 return 1;
5480
5481 if (event->attr.exclude_kernel && !user_mode(regs))
5482 return 1;
5483 }
5484
5485 return 0;
5486}
5487
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005488static int perf_swevent_match(struct perf_event *event,
5489 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005490 u32 event_id,
5491 struct perf_sample_data *data,
5492 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005493{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005494 if (event->attr.type != type)
5495 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005496
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005497 if (event->attr.config != event_id)
5498 return 0;
5499
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005500 if (perf_exclude_event(event, regs))
5501 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005502
5503 return 1;
5504}
5505
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005506static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005507{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005508 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005509
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005510 return hash_64(val, SWEVENT_HLIST_BITS);
5511}
5512
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005513static inline struct hlist_head *
5514__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005515{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005516 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005517
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005518 return &hlist->heads[hash];
5519}
5520
5521/* For the read side: events when they trigger */
5522static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005523find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005524{
5525 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005526
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005527 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005528 if (!hlist)
5529 return NULL;
5530
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005531 return __find_swevent_head(hlist, type, event_id);
5532}
5533
5534/* For the event head insertion and removal in the hlist */
5535static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005536find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005537{
5538 struct swevent_hlist *hlist;
5539 u32 event_id = event->attr.config;
5540 u64 type = event->attr.type;
5541
5542 /*
5543 * Event scheduling is always serialized against hlist allocation
5544 * and release. Which makes the protected version suitable here.
5545 * The context lock guarantees that.
5546 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005547 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005548 lockdep_is_held(&event->ctx->lock));
5549 if (!hlist)
5550 return NULL;
5551
5552 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005553}
5554
5555static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005556 u64 nr,
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005557 struct perf_sample_data *data,
5558 struct pt_regs *regs)
5559{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005560 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005561 struct perf_event *event;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005562 struct hlist_head *head;
5563
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005564 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005565 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005566 if (!head)
5567 goto end;
5568
Sasha Levinb67bfe02013-02-27 17:06:00 -08005569 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005570 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005571 perf_swevent_event(event, nr, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005572 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005573end:
5574 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005575}
5576
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005577int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005578{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005579 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005580
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005581 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005582}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005583EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005584
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005585inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005586{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005587 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005588
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005589 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005590}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005591
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005592void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005593{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005594 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005595 int rctx;
5596
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005597 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005598 rctx = perf_swevent_get_recursion_context();
5599 if (rctx < 0)
5600 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005601
Robert Richterfd0d0002012-04-02 20:19:08 +02005602 perf_sample_data_init(&data, addr, 0);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005603
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005604 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005605
5606 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005607 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005608}
5609
5610static void perf_swevent_read(struct perf_event *event)
5611{
5612}
5613
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005614static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005615{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005616 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005617 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005618 struct hlist_head *head;
5619
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005620 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005621 hwc->last_period = hwc->sample_period;
5622 perf_swevent_set_period(event);
5623 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005624
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005625 hwc->state = !(flags & PERF_EF_START);
5626
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005627 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005628 if (WARN_ON_ONCE(!head))
5629 return -EINVAL;
5630
5631 hlist_add_head_rcu(&event->hlist_entry, head);
5632
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005633 return 0;
5634}
5635
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005636static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005637{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005638 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005639}
5640
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005641static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005642{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005643 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005644}
5645
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005646static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005647{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005648 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005649}
5650
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005651/* Deref the hlist from the update side */
5652static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005653swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005654{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005655 return rcu_dereference_protected(swhash->swevent_hlist,
5656 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005657}
5658
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005659static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005660{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005661 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005662
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005663 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005664 return;
5665
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005666 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Lai Jiangshanfa4bbc42011-03-18 12:08:29 +08005667 kfree_rcu(hlist, rcu_head);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005668}
5669
5670static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5671{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005672 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005673
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005674 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005675
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005676 if (!--swhash->hlist_refcount)
5677 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005678
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005679 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005680}
5681
5682static void swevent_hlist_put(struct perf_event *event)
5683{
5684 int cpu;
5685
5686 if (event->cpu != -1) {
5687 swevent_hlist_put_cpu(event, event->cpu);
5688 return;
5689 }
5690
5691 for_each_possible_cpu(cpu)
5692 swevent_hlist_put_cpu(event, cpu);
5693}
5694
5695static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5696{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005697 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005698 int err = 0;
5699
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005700 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005701
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005702 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005703 struct swevent_hlist *hlist;
5704
5705 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5706 if (!hlist) {
5707 err = -ENOMEM;
5708 goto exit;
5709 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005710 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005711 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005712 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005713exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005714 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005715
5716 return err;
5717}
5718
5719static int swevent_hlist_get(struct perf_event *event)
5720{
5721 int err;
5722 int cpu, failed_cpu;
5723
5724 if (event->cpu != -1)
5725 return swevent_hlist_get_cpu(event, event->cpu);
5726
5727 get_online_cpus();
5728 for_each_possible_cpu(cpu) {
5729 err = swevent_hlist_get_cpu(event, cpu);
5730 if (err) {
5731 failed_cpu = cpu;
5732 goto fail;
5733 }
5734 }
5735 put_online_cpus();
5736
5737 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005738fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005739 for_each_possible_cpu(cpu) {
5740 if (cpu == failed_cpu)
5741 break;
5742 swevent_hlist_put_cpu(event, cpu);
5743 }
5744
5745 put_online_cpus();
5746 return err;
5747}
5748
Ingo Molnarc5905af2012-02-24 08:31:31 +01005749struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005750
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005751static void sw_perf_event_destroy(struct perf_event *event)
5752{
5753 u64 event_id = event->attr.config;
5754
5755 WARN_ON(event->parent);
5756
Ingo Molnarc5905af2012-02-24 08:31:31 +01005757 static_key_slow_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005758 swevent_hlist_put(event);
5759}
5760
5761static int perf_swevent_init(struct perf_event *event)
5762{
Tommi Rantala8176cce2013-04-13 22:49:14 +03005763 u64 event_id = event->attr.config;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005764
5765 if (event->attr.type != PERF_TYPE_SOFTWARE)
5766 return -ENOENT;
5767
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005768 /*
5769 * no branch sampling for software events
5770 */
5771 if (has_branch_stack(event))
5772 return -EOPNOTSUPP;
5773
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005774 switch (event_id) {
5775 case PERF_COUNT_SW_CPU_CLOCK:
5776 case PERF_COUNT_SW_TASK_CLOCK:
5777 return -ENOENT;
5778
5779 default:
5780 break;
5781 }
5782
Dan Carpenterce677832010-10-24 21:50:42 +02005783 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005784 return -ENOENT;
5785
5786 if (!event->parent) {
5787 int err;
5788
5789 err = swevent_hlist_get(event);
5790 if (err)
5791 return err;
5792
Ingo Molnarc5905af2012-02-24 08:31:31 +01005793 static_key_slow_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005794 event->destroy = sw_perf_event_destroy;
5795 }
5796
5797 return 0;
5798}
5799
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005800static int perf_swevent_event_idx(struct perf_event *event)
5801{
5802 return 0;
5803}
5804
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005805static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005806 .task_ctx_nr = perf_sw_context,
5807
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005808 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005809 .add = perf_swevent_add,
5810 .del = perf_swevent_del,
5811 .start = perf_swevent_start,
5812 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005813 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005814
5815 .event_idx = perf_swevent_event_idx,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005816};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005817
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005818#ifdef CONFIG_EVENT_TRACING
5819
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005820static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005821 struct perf_sample_data *data)
5822{
5823 void *record = data->raw->data;
5824
5825 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5826 return 1;
5827 return 0;
5828}
5829
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005830static int perf_tp_event_match(struct perf_event *event,
5831 struct perf_sample_data *data,
5832 struct pt_regs *regs)
5833{
Frederic Weisbeckera0f7d0f2011-03-07 21:27:09 +01005834 if (event->hw.state & PERF_HES_STOPPED)
5835 return 0;
Peter Zijlstra580d6072010-05-20 20:54:31 +02005836 /*
5837 * All tracepoints are from kernel-space.
5838 */
5839 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005840 return 0;
5841
5842 if (!perf_tp_filter_match(event, data))
5843 return 0;
5844
5845 return 1;
5846}
5847
5848void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005849 struct pt_regs *regs, struct hlist_head *head, int rctx,
5850 struct task_struct *task)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005851{
5852 struct perf_sample_data data;
5853 struct perf_event *event;
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005854
5855 struct perf_raw_record raw = {
5856 .size = entry_size,
5857 .data = record,
5858 };
5859
Robert Richterfd0d0002012-04-02 20:19:08 +02005860 perf_sample_data_init(&data, addr, 0);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005861 data.raw = &raw;
5862
Sasha Levinb67bfe02013-02-27 17:06:00 -08005863 hlist_for_each_entry_rcu(event, head, hlist_entry) {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005864 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005865 perf_swevent_event(event, count, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005866 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005867
Andrew Vagine6dab5f2012-07-11 18:14:58 +04005868 /*
5869 * If we got specified a target task, also iterate its context and
5870 * deliver this event there too.
5871 */
5872 if (task && task != current) {
5873 struct perf_event_context *ctx;
5874 struct trace_entry *entry = record;
5875
5876 rcu_read_lock();
5877 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
5878 if (!ctx)
5879 goto unlock;
5880
5881 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
5882 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5883 continue;
5884 if (event->attr.config != entry->type)
5885 continue;
5886 if (perf_tp_event_match(event, &data, regs))
5887 perf_swevent_event(event, count, &data, regs);
5888 }
5889unlock:
5890 rcu_read_unlock();
5891 }
5892
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005893 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005894}
5895EXPORT_SYMBOL_GPL(perf_tp_event);
5896
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005897static void tp_perf_event_destroy(struct perf_event *event)
5898{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005899 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005900}
5901
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005902static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005903{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005904 int err;
5905
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005906 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5907 return -ENOENT;
5908
Stephane Eranian2481c5f2012-02-09 23:20:59 +01005909 /*
5910 * no branch sampling for tracepoint events
5911 */
5912 if (has_branch_stack(event))
5913 return -EOPNOTSUPP;
5914
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005915 err = perf_trace_init(event);
5916 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005917 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005918
5919 event->destroy = tp_perf_event_destroy;
5920
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005921 return 0;
5922}
5923
5924static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005925 .task_ctx_nr = perf_sw_context,
5926
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005927 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005928 .add = perf_trace_add,
5929 .del = perf_trace_del,
5930 .start = perf_swevent_start,
5931 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005932 .read = perf_swevent_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01005933
5934 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005935};
5936
5937static inline void perf_tp_register(void)
5938{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005939 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005940}
Li Zefan6fb29152009-10-15 11:21:42 +08005941
5942static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5943{
5944 char *filter_str;
5945 int ret;
5946
5947 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5948 return -EINVAL;
5949
5950 filter_str = strndup_user(arg, PAGE_SIZE);
5951 if (IS_ERR(filter_str))
5952 return PTR_ERR(filter_str);
5953
5954 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5955
5956 kfree(filter_str);
5957 return ret;
5958}
5959
5960static void perf_event_free_filter(struct perf_event *event)
5961{
5962 ftrace_profile_free_filter(event);
5963}
5964
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005965#else
Li Zefan6fb29152009-10-15 11:21:42 +08005966
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005967static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005968{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005969}
Li Zefan6fb29152009-10-15 11:21:42 +08005970
5971static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5972{
5973 return -ENOENT;
5974}
5975
5976static void perf_event_free_filter(struct perf_event *event)
5977{
5978}
5979
Li Zefan07b139c2009-12-21 14:27:35 +08005980#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005981
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005982#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005983void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005984{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005985 struct perf_sample_data sample;
5986 struct pt_regs *regs = data;
5987
Robert Richterfd0d0002012-04-02 20:19:08 +02005988 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005989
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005990 if (!bp->hw.state && !perf_exclude_event(bp, regs))
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02005991 perf_swevent_event(bp, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005992}
5993#endif
5994
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005995/*
5996 * hrtimer based swevent callback
5997 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005998
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005999static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006000{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006001 enum hrtimer_restart ret = HRTIMER_RESTART;
6002 struct perf_sample_data data;
6003 struct pt_regs *regs;
6004 struct perf_event *event;
6005 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006006
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006007 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006008
6009 if (event->state != PERF_EVENT_STATE_ACTIVE)
6010 return HRTIMER_NORESTART;
6011
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006012 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006013
Robert Richterfd0d0002012-04-02 20:19:08 +02006014 perf_sample_data_init(&data, 0, event->hw.last_period);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006015 regs = get_irq_regs();
6016
6017 if (regs && !perf_exclude_event(event, regs)) {
Paul E. McKenney77aeeeb2011-11-10 16:02:52 -08006018 if (!(event->attr.exclude_idle && is_idle_task(current)))
Robert Richter33b07b82012-04-05 18:24:43 +02006019 if (__perf_event_overflow(event, 1, &data, regs))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006020 ret = HRTIMER_NORESTART;
6021 }
6022
6023 period = max_t(u64, 10000, event->hw.sample_period);
6024 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
6025
6026 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006027}
6028
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006029static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006030{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006031 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006032 s64 period;
6033
6034 if (!is_sampling_event(event))
6035 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006036
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006037 period = local64_read(&hwc->period_left);
6038 if (period) {
6039 if (period < 0)
6040 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006041
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01006042 local64_set(&hwc->period_left, 0);
6043 } else {
6044 period = max_t(u64, 10000, hwc->sample_period);
6045 }
6046 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006047 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006048 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006049}
6050
6051static void perf_swevent_cancel_hrtimer(struct perf_event *event)
6052{
6053 struct hw_perf_event *hwc = &event->hw;
6054
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01006055 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006056 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02006057 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006058
6059 hrtimer_cancel(&hwc->hrtimer);
6060 }
6061}
6062
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006063static void perf_swevent_init_hrtimer(struct perf_event *event)
6064{
6065 struct hw_perf_event *hwc = &event->hw;
6066
6067 if (!is_sampling_event(event))
6068 return;
6069
6070 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
6071 hwc->hrtimer.function = perf_swevent_hrtimer;
6072
6073 /*
6074 * Since hrtimers have a fixed rate, we can do a static freq->period
6075 * mapping and avoid the whole period adjust feedback stuff.
6076 */
6077 if (event->attr.freq) {
6078 long freq = event->attr.sample_freq;
6079
6080 event->attr.sample_period = NSEC_PER_SEC / freq;
6081 hwc->sample_period = event->attr.sample_period;
6082 local64_set(&hwc->period_left, hwc->sample_period);
Namhyung Kim778141e2013-03-18 11:41:46 +09006083 hwc->last_period = hwc->sample_period;
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006084 event->attr.freq = 0;
6085 }
6086}
6087
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006088/*
6089 * Software event: cpu wall time clock
6090 */
6091
6092static void cpu_clock_event_update(struct perf_event *event)
6093{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006094 s64 prev;
6095 u64 now;
6096
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006097 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006098 prev = local64_xchg(&event->hw.prev_count, now);
6099 local64_add(now - prev, &event->count);
6100}
6101
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006102static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006103{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006104 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006105 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006106}
6107
6108static void cpu_clock_event_stop(struct perf_event *event, int flags)
6109{
6110 perf_swevent_cancel_hrtimer(event);
6111 cpu_clock_event_update(event);
6112}
6113
6114static int cpu_clock_event_add(struct perf_event *event, int flags)
6115{
6116 if (flags & PERF_EF_START)
6117 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006118
6119 return 0;
6120}
6121
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006122static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006123{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006124 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006125}
6126
6127static void cpu_clock_event_read(struct perf_event *event)
6128{
6129 cpu_clock_event_update(event);
6130}
6131
6132static int cpu_clock_event_init(struct perf_event *event)
6133{
6134 if (event->attr.type != PERF_TYPE_SOFTWARE)
6135 return -ENOENT;
6136
6137 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
6138 return -ENOENT;
6139
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006140 /*
6141 * no branch sampling for software events
6142 */
6143 if (has_branch_stack(event))
6144 return -EOPNOTSUPP;
6145
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006146 perf_swevent_init_hrtimer(event);
6147
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006148 return 0;
6149}
6150
6151static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006152 .task_ctx_nr = perf_sw_context,
6153
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006154 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006155 .add = cpu_clock_event_add,
6156 .del = cpu_clock_event_del,
6157 .start = cpu_clock_event_start,
6158 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006159 .read = cpu_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006160
6161 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006162};
6163
6164/*
6165 * Software event: task time clock
6166 */
6167
6168static void task_clock_event_update(struct perf_event *event, u64 now)
6169{
6170 u64 prev;
6171 s64 delta;
6172
6173 prev = local64_xchg(&event->hw.prev_count, now);
6174 delta = now - prev;
6175 local64_add(delta, &event->count);
6176}
6177
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006178static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006179{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006180 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006181 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006182}
6183
6184static void task_clock_event_stop(struct perf_event *event, int flags)
6185{
6186 perf_swevent_cancel_hrtimer(event);
6187 task_clock_event_update(event, event->ctx->time);
6188}
6189
6190static int task_clock_event_add(struct perf_event *event, int flags)
6191{
6192 if (flags & PERF_EF_START)
6193 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006194
6195 return 0;
6196}
6197
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006198static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006199{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006200 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006201}
6202
6203static void task_clock_event_read(struct perf_event *event)
6204{
Peter Zijlstra768a06e2011-02-22 16:52:24 +01006205 u64 now = perf_clock();
6206 u64 delta = now - event->ctx->timestamp;
6207 u64 time = event->ctx->time + delta;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006208
6209 task_clock_event_update(event, time);
6210}
6211
6212static int task_clock_event_init(struct perf_event *event)
6213{
6214 if (event->attr.type != PERF_TYPE_SOFTWARE)
6215 return -ENOENT;
6216
6217 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
6218 return -ENOENT;
6219
Stephane Eranian2481c5f2012-02-09 23:20:59 +01006220 /*
6221 * no branch sampling for software events
6222 */
6223 if (has_branch_stack(event))
6224 return -EOPNOTSUPP;
6225
Peter Zijlstraba3dd362011-02-15 12:41:46 +01006226 perf_swevent_init_hrtimer(event);
6227
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006228 return 0;
6229}
6230
6231static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006232 .task_ctx_nr = perf_sw_context,
6233
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006234 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02006235 .add = task_clock_event_add,
6236 .del = task_clock_event_del,
6237 .start = task_clock_event_start,
6238 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006239 .read = task_clock_event_read,
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006240
6241 .event_idx = perf_swevent_event_idx,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006242};
6243
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006244static void perf_pmu_nop_void(struct pmu *pmu)
6245{
6246}
6247
6248static int perf_pmu_nop_int(struct pmu *pmu)
6249{
6250 return 0;
6251}
6252
6253static void perf_pmu_start_txn(struct pmu *pmu)
6254{
6255 perf_pmu_disable(pmu);
6256}
6257
6258static int perf_pmu_commit_txn(struct pmu *pmu)
6259{
6260 perf_pmu_enable(pmu);
6261 return 0;
6262}
6263
6264static void perf_pmu_cancel_txn(struct pmu *pmu)
6265{
6266 perf_pmu_enable(pmu);
6267}
6268
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006269static int perf_event_idx_default(struct perf_event *event)
6270{
6271 return event->hw.idx + 1;
6272}
6273
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006274/*
6275 * Ensures all contexts with the same task_ctx_nr have the same
6276 * pmu_cpu_context too.
6277 */
6278static void *find_pmu_context(int ctxn)
6279{
6280 struct pmu *pmu;
6281
6282 if (ctxn < 0)
6283 return NULL;
6284
6285 list_for_each_entry(pmu, &pmus, entry) {
6286 if (pmu->task_ctx_nr == ctxn)
6287 return pmu->pmu_cpu_context;
6288 }
6289
6290 return NULL;
6291}
6292
Peter Zijlstra51676952010-12-07 14:18:20 +01006293static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006294{
Peter Zijlstra51676952010-12-07 14:18:20 +01006295 int cpu;
6296
6297 for_each_possible_cpu(cpu) {
6298 struct perf_cpu_context *cpuctx;
6299
6300 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6301
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006302 if (cpuctx->unique_pmu == old_pmu)
6303 cpuctx->unique_pmu = pmu;
Peter Zijlstra51676952010-12-07 14:18:20 +01006304 }
6305}
6306
6307static void free_pmu_context(struct pmu *pmu)
6308{
6309 struct pmu *i;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006310
6311 mutex_lock(&pmus_lock);
6312 /*
6313 * Like a real lame refcount.
6314 */
Peter Zijlstra51676952010-12-07 14:18:20 +01006315 list_for_each_entry(i, &pmus, entry) {
6316 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
6317 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006318 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01006319 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006320 }
6321
Peter Zijlstra51676952010-12-07 14:18:20 +01006322 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006323out:
6324 mutex_unlock(&pmus_lock);
6325}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006326static struct idr pmu_idr;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006327
Peter Zijlstraabe43402010-11-17 23:17:37 +01006328static ssize_t
6329type_show(struct device *dev, struct device_attribute *attr, char *page)
6330{
6331 struct pmu *pmu = dev_get_drvdata(dev);
6332
6333 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
6334}
6335
Stephane Eranian62b85632013-04-03 14:21:34 +02006336static ssize_t
6337perf_event_mux_interval_ms_show(struct device *dev,
6338 struct device_attribute *attr,
6339 char *page)
6340{
6341 struct pmu *pmu = dev_get_drvdata(dev);
6342
6343 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
6344}
6345
6346static ssize_t
6347perf_event_mux_interval_ms_store(struct device *dev,
6348 struct device_attribute *attr,
6349 const char *buf, size_t count)
6350{
6351 struct pmu *pmu = dev_get_drvdata(dev);
6352 int timer, cpu, ret;
6353
6354 ret = kstrtoint(buf, 0, &timer);
6355 if (ret)
6356 return ret;
6357
6358 if (timer < 1)
6359 return -EINVAL;
6360
6361 /* same value, noting to do */
6362 if (timer == pmu->hrtimer_interval_ms)
6363 return count;
6364
6365 pmu->hrtimer_interval_ms = timer;
6366
6367 /* update all cpuctx for this PMU */
6368 for_each_possible_cpu(cpu) {
6369 struct perf_cpu_context *cpuctx;
6370 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
6371 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
6372
6373 if (hrtimer_active(&cpuctx->hrtimer))
6374 hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
6375 }
6376
6377 return count;
6378}
6379
Peter Zijlstraabe43402010-11-17 23:17:37 +01006380static struct device_attribute pmu_dev_attrs[] = {
Stephane Eranian62b85632013-04-03 14:21:34 +02006381 __ATTR_RO(type),
6382 __ATTR_RW(perf_event_mux_interval_ms),
6383 __ATTR_NULL,
Peter Zijlstraabe43402010-11-17 23:17:37 +01006384};
6385
6386static int pmu_bus_running;
6387static struct bus_type pmu_bus = {
6388 .name = "event_source",
6389 .dev_attrs = pmu_dev_attrs,
6390};
6391
6392static void pmu_dev_release(struct device *dev)
6393{
6394 kfree(dev);
6395}
6396
6397static int pmu_dev_alloc(struct pmu *pmu)
6398{
6399 int ret = -ENOMEM;
6400
6401 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
6402 if (!pmu->dev)
6403 goto out;
6404
Peter Zijlstra0c9d42e2011-11-20 23:30:47 +01006405 pmu->dev->groups = pmu->attr_groups;
Peter Zijlstraabe43402010-11-17 23:17:37 +01006406 device_initialize(pmu->dev);
6407 ret = dev_set_name(pmu->dev, "%s", pmu->name);
6408 if (ret)
6409 goto free_dev;
6410
6411 dev_set_drvdata(pmu->dev, pmu);
6412 pmu->dev->bus = &pmu_bus;
6413 pmu->dev->release = pmu_dev_release;
6414 ret = device_add(pmu->dev);
6415 if (ret)
6416 goto free_dev;
6417
6418out:
6419 return ret;
6420
6421free_dev:
6422 put_device(pmu->dev);
6423 goto out;
6424}
6425
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006426static struct lock_class_key cpuctx_mutex;
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006427static struct lock_class_key cpuctx_lock;
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006428
Mischa Jonker03d8e802013-06-04 11:45:48 +02006429int perf_pmu_register(struct pmu *pmu, const char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006430{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006431 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006432
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006433 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006434 ret = -ENOMEM;
6435 pmu->pmu_disable_count = alloc_percpu(int);
6436 if (!pmu->pmu_disable_count)
6437 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006438
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006439 pmu->type = -1;
6440 if (!name)
6441 goto skip_type;
6442 pmu->name = name;
6443
6444 if (type < 0) {
Tejun Heo0e9c3be2013-02-27 17:04:55 -08006445 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
6446 if (type < 0) {
6447 ret = type;
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006448 goto free_pdc;
6449 }
6450 }
6451 pmu->type = type;
6452
Peter Zijlstraabe43402010-11-17 23:17:37 +01006453 if (pmu_bus_running) {
6454 ret = pmu_dev_alloc(pmu);
6455 if (ret)
6456 goto free_idr;
6457 }
6458
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006459skip_type:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006460 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
6461 if (pmu->pmu_cpu_context)
6462 goto got_cpu_context;
6463
Wei Yongjunc4814202013-04-12 11:05:54 +08006464 ret = -ENOMEM;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006465 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
6466 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01006467 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006468
6469 for_each_possible_cpu(cpu) {
6470 struct perf_cpu_context *cpuctx;
6471
6472 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02006473 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01006474 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrafacc4302011-04-09 21:17:42 +02006475 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006476 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006477 cpuctx->ctx.pmu = pmu;
Stephane Eranian9e630202013-04-03 14:21:33 +02006478
6479 __perf_cpu_hrtimer_init(cpuctx, cpu);
6480
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006481 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra3f1f3322012-10-02 15:38:52 +02006482 cpuctx->unique_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006483 }
6484
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006485got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02006486 if (!pmu->start_txn) {
6487 if (pmu->pmu_enable) {
6488 /*
6489 * If we have pmu_enable/pmu_disable calls, install
6490 * transaction stubs that use that to try and batch
6491 * hardware accesses.
6492 */
6493 pmu->start_txn = perf_pmu_start_txn;
6494 pmu->commit_txn = perf_pmu_commit_txn;
6495 pmu->cancel_txn = perf_pmu_cancel_txn;
6496 } else {
6497 pmu->start_txn = perf_pmu_nop_void;
6498 pmu->commit_txn = perf_pmu_nop_int;
6499 pmu->cancel_txn = perf_pmu_nop_void;
6500 }
6501 }
6502
6503 if (!pmu->pmu_enable) {
6504 pmu->pmu_enable = perf_pmu_nop_void;
6505 pmu->pmu_disable = perf_pmu_nop_void;
6506 }
6507
Peter Zijlstra35edc2a2011-11-20 20:36:02 +01006508 if (!pmu->event_idx)
6509 pmu->event_idx = perf_event_idx_default;
6510
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006511 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006512 ret = 0;
6513unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006514 mutex_unlock(&pmus_lock);
6515
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006516 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006517
Peter Zijlstraabe43402010-11-17 23:17:37 +01006518free_dev:
6519 device_del(pmu->dev);
6520 put_device(pmu->dev);
6521
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006522free_idr:
6523 if (pmu->type >= PERF_TYPE_MAX)
6524 idr_remove(&pmu_idr, pmu->type);
6525
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006526free_pdc:
6527 free_percpu(pmu->pmu_disable_count);
6528 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006529}
6530
6531void perf_pmu_unregister(struct pmu *pmu)
6532{
6533 mutex_lock(&pmus_lock);
6534 list_del_rcu(&pmu->entry);
6535 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006536
6537 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006538 * We dereference the pmu list under both SRCU and regular RCU, so
6539 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006540 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006541 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006542 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006543
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006544 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006545 if (pmu->type >= PERF_TYPE_MAX)
6546 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006547 device_del(pmu->dev);
6548 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006549 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006550}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006551
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006552struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006553{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006554 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006555 int idx;
Lin Ming940c5b22011-02-27 21:13:31 +08006556 int ret;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006557
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006558 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006559
6560 rcu_read_lock();
6561 pmu = idr_find(&pmu_idr, event->attr.type);
6562 rcu_read_unlock();
Lin Ming940c5b22011-02-27 21:13:31 +08006563 if (pmu) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006564 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006565 ret = pmu->event_init(event);
6566 if (ret)
6567 pmu = ERR_PTR(ret);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006568 goto unlock;
Lin Ming940c5b22011-02-27 21:13:31 +08006569 }
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006570
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006571 list_for_each_entry_rcu(pmu, &pmus, entry) {
Mark Rutland7e5b2a02011-08-11 12:31:20 +01006572 event->pmu = pmu;
Lin Ming940c5b22011-02-27 21:13:31 +08006573 ret = pmu->event_init(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006574 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006575 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006576
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006577 if (ret != -ENOENT) {
6578 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006579 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006580 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006581 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006582 pmu = ERR_PTR(-ENOENT);
6583unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006584 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006585
6586 return pmu;
6587}
6588
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006589static void account_event_cpu(struct perf_event *event, int cpu)
6590{
6591 if (event->parent)
6592 return;
6593
6594 if (has_branch_stack(event)) {
6595 if (!(event->attach_state & PERF_ATTACH_TASK))
6596 atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
6597 }
6598 if (is_cgroup_event(event))
6599 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
6600}
6601
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006602static void account_event(struct perf_event *event)
6603{
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006604 if (event->parent)
6605 return;
6606
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006607 if (event->attach_state & PERF_ATTACH_TASK)
6608 static_key_slow_inc(&perf_sched_events.key);
6609 if (event->attr.mmap || event->attr.mmap_data)
6610 atomic_inc(&nr_mmap_events);
6611 if (event->attr.comm)
6612 atomic_inc(&nr_comm_events);
6613 if (event->attr.task)
6614 atomic_inc(&nr_task_events);
Frederic Weisbecker948b26b2013-08-02 18:29:55 +02006615 if (event->attr.freq) {
6616 if (atomic_inc_return(&nr_freq_events) == 1)
6617 tick_nohz_full_kick_all();
6618 }
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006619 if (has_branch_stack(event))
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006620 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006621 if (is_cgroup_event(event))
6622 static_key_slow_inc(&perf_sched_events.key);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006623
Frederic Weisbecker4beb31f2013-07-23 02:31:02 +02006624 account_event_cpu(event, event->cpu);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02006625}
6626
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006627/*
6628 * Allocate and initialize a event structure
6629 */
6630static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006631perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006632 struct task_struct *task,
6633 struct perf_event *group_leader,
6634 struct perf_event *parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03006635 perf_overflow_handler_t overflow_handler,
6636 void *context)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006637{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006638 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006639 struct perf_event *event;
6640 struct hw_perf_event *hwc;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006641 long err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006642
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006643 if ((unsigned)cpu >= nr_cpu_ids) {
6644 if (!task || cpu != -1)
6645 return ERR_PTR(-EINVAL);
6646 }
6647
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006648 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006649 if (!event)
6650 return ERR_PTR(-ENOMEM);
6651
6652 /*
6653 * Single events are their own group leaders, with an
6654 * empty sibling list:
6655 */
6656 if (!group_leader)
6657 group_leader = event;
6658
6659 mutex_init(&event->child_mutex);
6660 INIT_LIST_HEAD(&event->child_list);
6661
6662 INIT_LIST_HEAD(&event->group_entry);
6663 INIT_LIST_HEAD(&event->event_entry);
6664 INIT_LIST_HEAD(&event->sibling_list);
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006665 INIT_LIST_HEAD(&event->rb_entry);
6666
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006667 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006668 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006669
6670 mutex_init(&event->mmap_mutex);
6671
Al Viroa6fa9412012-08-20 14:59:25 +01006672 atomic_long_set(&event->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006673 event->cpu = cpu;
6674 event->attr = *attr;
6675 event->group_leader = group_leader;
6676 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006677 event->oncpu = -1;
6678
6679 event->parent = parent_event;
6680
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08006681 event->ns = get_pid_ns(task_active_pid_ns(current));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006682 event->id = atomic64_inc_return(&perf_event_id);
6683
6684 event->state = PERF_EVENT_STATE_INACTIVE;
6685
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006686 if (task) {
6687 event->attach_state = PERF_ATTACH_TASK;
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006688
6689 if (attr->type == PERF_TYPE_TRACEPOINT)
6690 event->hw.tp_target = task;
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006691#ifdef CONFIG_HAVE_HW_BREAKPOINT
6692 /*
6693 * hw_breakpoint is a bit difficult here..
6694 */
Oleg Nesterovf22c1bb2013-02-02 16:27:52 +01006695 else if (attr->type == PERF_TYPE_BREAKPOINT)
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006696 event->hw.bp_target = task;
6697#endif
6698 }
6699
Avi Kivity4dc0da82011-06-29 18:42:35 +03006700 if (!overflow_handler && parent_event) {
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006701 overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006702 context = parent_event->overflow_handler_context;
6703 }
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006704
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006705 event->overflow_handler = overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03006706 event->overflow_handler_context = context;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006707
Jiri Olsa0231bb52013-02-01 11:23:45 +01006708 perf_event__state_init(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006709
6710 pmu = NULL;
6711
6712 hwc = &event->hw;
6713 hwc->sample_period = attr->sample_period;
6714 if (attr->freq && attr->sample_freq)
6715 hwc->sample_period = 1;
6716 hwc->last_period = hwc->sample_period;
6717
Peter Zijlstrae7850592010-05-21 14:43:08 +02006718 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006719
6720 /*
6721 * we currently do not support PERF_FORMAT_GROUP on inherited events
6722 */
6723 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006724 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006725
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006726 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006727 if (!pmu)
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006728 goto err_ns;
6729 else if (IS_ERR(pmu)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006730 err = PTR_ERR(pmu);
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006731 goto err_ns;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006732 }
6733
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006734 if (!event->parent) {
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006735 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6736 err = get_callchain_buffers();
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006737 if (err)
6738 goto err_pmu;
Stephane Eraniand010b332012-02-09 23:21:00 +01006739 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006740 }
6741
6742 return event;
Frederic Weisbecker90983b12013-07-23 02:31:00 +02006743
6744err_pmu:
6745 if (event->destroy)
6746 event->destroy(event);
6747err_ns:
6748 if (event->ns)
6749 put_pid_ns(event->ns);
6750 kfree(event);
6751
6752 return ERR_PTR(err);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006753}
6754
6755static int perf_copy_attr(struct perf_event_attr __user *uattr,
6756 struct perf_event_attr *attr)
6757{
6758 u32 size;
6759 int ret;
6760
6761 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6762 return -EFAULT;
6763
6764 /*
6765 * zero the full structure, so that a short copy will be nice.
6766 */
6767 memset(attr, 0, sizeof(*attr));
6768
6769 ret = get_user(size, &uattr->size);
6770 if (ret)
6771 return ret;
6772
6773 if (size > PAGE_SIZE) /* silly large */
6774 goto err_size;
6775
6776 if (!size) /* abi compat */
6777 size = PERF_ATTR_SIZE_VER0;
6778
6779 if (size < PERF_ATTR_SIZE_VER0)
6780 goto err_size;
6781
6782 /*
6783 * If we're handed a bigger struct than we know of,
6784 * ensure all the unknown bits are 0 - i.e. new
6785 * user-space does not rely on any kernel feature
6786 * extensions we dont know about yet.
6787 */
6788 if (size > sizeof(*attr)) {
6789 unsigned char __user *addr;
6790 unsigned char __user *end;
6791 unsigned char val;
6792
6793 addr = (void __user *)uattr + sizeof(*attr);
6794 end = (void __user *)uattr + size;
6795
6796 for (; addr < end; addr++) {
6797 ret = get_user(val, addr);
6798 if (ret)
6799 return ret;
6800 if (val)
6801 goto err_size;
6802 }
6803 size = sizeof(*attr);
6804 }
6805
6806 ret = copy_from_user(attr, uattr, size);
6807 if (ret)
6808 return -EFAULT;
6809
Stephane Eranian3090ffb2013-10-17 19:32:15 +02006810 /* disabled for now */
6811 if (attr->mmap2)
6812 return -EINVAL;
6813
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306814 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006815 return -EINVAL;
6816
6817 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6818 return -EINVAL;
6819
6820 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6821 return -EINVAL;
6822
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006823 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
6824 u64 mask = attr->branch_sample_type;
6825
6826 /* only using defined bits */
6827 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
6828 return -EINVAL;
6829
6830 /* at least one branch bit must be set */
6831 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
6832 return -EINVAL;
6833
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006834 /* propagate priv level, when not set for branch */
6835 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
6836
6837 /* exclude_kernel checked on syscall entry */
6838 if (!attr->exclude_kernel)
6839 mask |= PERF_SAMPLE_BRANCH_KERNEL;
6840
6841 if (!attr->exclude_user)
6842 mask |= PERF_SAMPLE_BRANCH_USER;
6843
6844 if (!attr->exclude_hv)
6845 mask |= PERF_SAMPLE_BRANCH_HV;
6846 /*
6847 * adjust user setting (for HW filter setup)
6848 */
6849 attr->branch_sample_type = mask;
6850 }
Stephane Eraniane7122092013-06-06 11:02:04 +02006851 /* privileged levels capture (kernel, hv): check permissions */
6852 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
Stephane Eranian2b923c82013-05-21 12:53:37 +02006853 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6854 return -EACCES;
Stephane Eranianbce38cd2012-02-09 23:20:51 +01006855 }
Jiri Olsa40189942012-08-07 15:20:37 +02006856
Jiri Olsac5ebced2012-08-07 15:20:40 +02006857 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
Jiri Olsa40189942012-08-07 15:20:37 +02006858 ret = perf_reg_validate(attr->sample_regs_user);
Jiri Olsac5ebced2012-08-07 15:20:40 +02006859 if (ret)
6860 return ret;
6861 }
6862
6863 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
6864 if (!arch_perf_have_user_stack_dump())
6865 return -ENOSYS;
6866
6867 /*
6868 * We have __u32 type for the size, but so far
6869 * we can only use __u16 as maximum due to the
6870 * __u16 sample size limit.
6871 */
6872 if (attr->sample_stack_user >= USHRT_MAX)
6873 ret = -EINVAL;
6874 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
6875 ret = -EINVAL;
6876 }
Jiri Olsa40189942012-08-07 15:20:37 +02006877
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006878out:
6879 return ret;
6880
6881err_size:
6882 put_user(sizeof(*attr), &uattr->size);
6883 ret = -E2BIG;
6884 goto out;
6885}
6886
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006887static int
6888perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006889{
Frederic Weisbecker76369132011-05-19 19:55:04 +02006890 struct ring_buffer *rb = NULL, *old_rb = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006891 int ret = -EINVAL;
6892
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006893 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006894 goto set;
6895
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006896 /* don't allow circular references */
6897 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006898 goto out;
6899
Peter Zijlstra0f139302010-05-20 14:35:15 +02006900 /*
6901 * Don't allow cross-cpu buffers
6902 */
6903 if (output_event->cpu != event->cpu)
6904 goto out;
6905
6906 /*
Frederic Weisbecker76369132011-05-19 19:55:04 +02006907 * If its not a per-cpu rb, it must be the same task.
Peter Zijlstra0f139302010-05-20 14:35:15 +02006908 */
6909 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6910 goto out;
6911
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006912set:
6913 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006914 /* Can't redirect output if we've got an active mmap() */
6915 if (atomic_read(&event->mmap_count))
6916 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006917
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006918 old_rb = event->rb;
6919
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006920 if (output_event) {
Frederic Weisbecker76369132011-05-19 19:55:04 +02006921 /* get the rb we want to redirect to */
6922 rb = ring_buffer_get(output_event);
6923 if (!rb)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006924 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006925 }
6926
Peter Zijlstra10c6db12011-11-26 02:47:31 +01006927 if (old_rb)
6928 ring_buffer_detach(event, old_rb);
Peter Zijlstra9bb5d402013-06-04 10:44:21 +02006929
6930 if (rb)
6931 ring_buffer_attach(event, rb);
6932
6933 rcu_assign_pointer(event->rb, rb);
6934
6935 if (old_rb) {
6936 ring_buffer_put(old_rb);
6937 /*
6938 * Since we detached before setting the new rb, so that we
6939 * could attach the new rb, we could have missed a wakeup.
6940 * Provide it now.
6941 */
6942 wake_up_all(&event->waitq);
6943 }
6944
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006945 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006946unlock:
6947 mutex_unlock(&event->mmap_mutex);
6948
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006949out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006950 return ret;
6951}
6952
6953/**
6954 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6955 *
6956 * @attr_uptr: event_id type attributes for monitoring/sampling
6957 * @pid: target pid
6958 * @cpu: target cpu
6959 * @group_fd: group leader event fd
6960 */
6961SYSCALL_DEFINE5(perf_event_open,
6962 struct perf_event_attr __user *, attr_uptr,
6963 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6964{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006965 struct perf_event *group_leader = NULL, *output_event = NULL;
6966 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006967 struct perf_event_attr attr;
6968 struct perf_event_context *ctx;
6969 struct file *event_file = NULL;
Al Viro2903ff02012-08-28 12:52:22 -04006970 struct fd group = {NULL, 0};
Matt Helsley38a81da2010-09-13 13:01:20 -07006971 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006972 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04006973 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006974 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006975 int err;
6976
6977 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02006978 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006979 return -EINVAL;
6980
6981 err = perf_copy_attr(attr_uptr, &attr);
6982 if (err)
6983 return err;
6984
6985 if (!attr.exclude_kernel) {
6986 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6987 return -EACCES;
6988 }
6989
6990 if (attr.freq) {
6991 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6992 return -EINVAL;
6993 }
6994
Stephane Eraniane5d13672011-02-14 11:20:01 +02006995 /*
6996 * In cgroup mode, the pid argument is used to pass the fd
6997 * opened to the cgroup directory in cgroupfs. The cpu argument
6998 * designates the cpu on which to monitor threads from that
6999 * cgroup.
7000 */
7001 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
7002 return -EINVAL;
7003
Al Viroab72a702012-08-21 09:40:46 -04007004 event_fd = get_unused_fd();
Al Viroea635c62010-05-26 17:40:29 -04007005 if (event_fd < 0)
7006 return event_fd;
7007
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007008 if (group_fd != -1) {
Al Viro2903ff02012-08-28 12:52:22 -04007009 err = perf_fget_light(group_fd, &group);
7010 if (err)
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007011 goto err_fd;
Al Viro2903ff02012-08-28 12:52:22 -04007012 group_leader = group.file->private_data;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007013 if (flags & PERF_FLAG_FD_OUTPUT)
7014 output_event = group_leader;
7015 if (flags & PERF_FLAG_FD_NO_GROUP)
7016 group_leader = NULL;
7017 }
7018
Stephane Eraniane5d13672011-02-14 11:20:01 +02007019 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007020 task = find_lively_task_by_vpid(pid);
7021 if (IS_ERR(task)) {
7022 err = PTR_ERR(task);
7023 goto err_group_fd;
7024 }
7025 }
7026
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007027 get_online_cpus();
7028
Avi Kivity4dc0da82011-06-29 18:42:35 +03007029 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
7030 NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007031 if (IS_ERR(event)) {
7032 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007033 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02007034 }
7035
Stephane Eraniane5d13672011-02-14 11:20:01 +02007036 if (flags & PERF_FLAG_PID_CGROUP) {
7037 err = perf_cgroup_connect(pid, event, &attr, group_leader);
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007038 if (err) {
7039 __free_event(event);
7040 goto err_task;
7041 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02007042 }
7043
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007044 account_event(event);
7045
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007046 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007047 * Special case software events and allow them to be part of
7048 * any hardware group.
7049 */
7050 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007051
7052 if (group_leader &&
7053 (is_software_event(event) != is_software_event(group_leader))) {
7054 if (is_software_event(event)) {
7055 /*
7056 * If event and group_leader are not both a software
7057 * event, and event is, then group leader is not.
7058 *
7059 * Allow the addition of software events to !software
7060 * groups, this is safe because software events never
7061 * fail to schedule.
7062 */
7063 pmu = group_leader->pmu;
7064 } else if (is_software_event(group_leader) &&
7065 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
7066 /*
7067 * In case the group is a pure software group, and we
7068 * try to add a hardware event, move the whole group to
7069 * the hardware context.
7070 */
7071 move_group = 1;
7072 }
7073 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007074
7075 /*
7076 * Get the target context (task or percpu):
7077 */
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007078 ctx = find_get_context(pmu, task, event->cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007079 if (IS_ERR(ctx)) {
7080 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007081 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007082 }
7083
Peter Zijlstrafd1edb32011-03-28 13:13:56 +02007084 if (task) {
7085 put_task_struct(task);
7086 task = NULL;
7087 }
7088
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007089 /*
7090 * Look up the group leader (we will attach this event to it):
7091 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007092 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007093 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007094
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007095 /*
7096 * Do not allow a recursive hierarchy (this new sibling
7097 * becoming part of another group-sibling):
7098 */
7099 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007100 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007101 /*
7102 * Do not allow to attach to a group in a different
7103 * task or CPU context:
7104 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007105 if (move_group) {
7106 if (group_leader->ctx->type != ctx->type)
7107 goto err_context;
7108 } else {
7109 if (group_leader->ctx != ctx)
7110 goto err_context;
7111 }
7112
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007113 /*
7114 * Only a group leader can be exclusive or pinned
7115 */
7116 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007117 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007118 }
7119
7120 if (output_event) {
7121 err = perf_event_set_output(event, output_event);
7122 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007123 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02007124 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007125
Al Viroea635c62010-05-26 17:40:29 -04007126 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
7127 if (IS_ERR(event_file)) {
7128 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007129 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04007130 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007131
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007132 if (move_group) {
7133 struct perf_event_context *gctx = group_leader->ctx;
7134
7135 mutex_lock(&gctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007136 perf_remove_from_context(group_leader);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007137
7138 /*
7139 * Removing from the context ends up with disabled
7140 * event. What we want here is event in the initial
7141 * startup state, ready to be add into new context.
7142 */
7143 perf_event__state_init(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007144 list_for_each_entry(sibling, &group_leader->sibling_list,
7145 group_entry) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007146 perf_remove_from_context(sibling);
Jiri Olsa0231bb52013-02-01 11:23:45 +01007147 perf_event__state_init(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007148 put_ctx(gctx);
7149 }
7150 mutex_unlock(&gctx->mutex);
7151 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007152 }
7153
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007154 WARN_ON_ONCE(ctx->parent_ctx);
7155 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007156
7157 if (move_group) {
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007158 synchronize_rcu();
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007159 perf_install_in_context(ctx, group_leader, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007160 get_ctx(ctx);
7161 list_for_each_entry(sibling, &group_leader->sibling_list,
7162 group_entry) {
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007163 perf_install_in_context(ctx, sibling, event->cpu);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02007164 get_ctx(ctx);
7165 }
7166 }
7167
Yan, Zhenge2d37cd2012-06-15 14:31:32 +08007168 perf_install_in_context(ctx, event, event->cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007169 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007170 mutex_unlock(&ctx->mutex);
7171
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007172 put_online_cpus();
7173
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007174 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01007175
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007176 mutex_lock(&current->perf_event_mutex);
7177 list_add_tail(&event->owner_entry, &current->perf_event_list);
7178 mutex_unlock(&current->perf_event_mutex);
7179
Peter Zijlstra8a495422010-05-27 15:47:49 +02007180 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007181 * Precalculate sample_data sizes
7182 */
7183 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007184 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02007185
7186 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02007187 * Drop the reference on the group_event after placing the
7188 * new event on the sibling_list. This ensures destruction
7189 * of the group leader will find the pointer to itself in
7190 * perf_group_detach().
7191 */
Al Viro2903ff02012-08-28 12:52:22 -04007192 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007193 fd_install(event_fd, event_file);
7194 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007195
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007196err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007197 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04007198 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02007199err_alloc:
7200 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007201err_task:
Yan, Zhengfbfc6232012-06-15 14:31:31 +08007202 put_online_cpus();
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02007203 if (task)
7204 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02007205err_group_fd:
Al Viro2903ff02012-08-28 12:52:22 -04007206 fdput(group);
Al Viroea635c62010-05-26 17:40:29 -04007207err_fd:
7208 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007209 return err;
7210}
7211
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007212/**
7213 * perf_event_create_kernel_counter
7214 *
7215 * @attr: attributes of the counter to create
7216 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07007217 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007218 */
7219struct perf_event *
7220perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07007221 struct task_struct *task,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007222 perf_overflow_handler_t overflow_handler,
7223 void *context)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007224{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007225 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007226 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007227 int err;
7228
7229 /*
7230 * Get the target context (task or percpu):
7231 */
7232
Avi Kivity4dc0da82011-06-29 18:42:35 +03007233 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
7234 overflow_handler, context);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007235 if (IS_ERR(event)) {
7236 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007237 goto err;
7238 }
7239
Frederic Weisbecker766d6c02013-07-23 02:31:01 +02007240 account_event(event);
7241
Matt Helsley38a81da2010-09-13 13:01:20 -07007242 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007243 if (IS_ERR(ctx)) {
7244 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007245 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007246 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007247
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007248 WARN_ON_ONCE(ctx->parent_ctx);
7249 mutex_lock(&ctx->mutex);
7250 perf_install_in_context(ctx, event, cpu);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007251 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007252 mutex_unlock(&ctx->mutex);
7253
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007254 return event;
7255
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02007256err_free:
7257 free_event(event);
7258err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01007259 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02007260}
7261EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
7262
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007263void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
7264{
7265 struct perf_event_context *src_ctx;
7266 struct perf_event_context *dst_ctx;
7267 struct perf_event *event, *tmp;
7268 LIST_HEAD(events);
7269
7270 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
7271 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
7272
7273 mutex_lock(&src_ctx->mutex);
7274 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
7275 event_entry) {
7276 perf_remove_from_context(event);
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007277 unaccount_event_cpu(event, src_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007278 put_ctx(src_ctx);
Peter Zijlstra98861672013-10-03 16:02:23 +02007279 list_add(&event->migrate_entry, &events);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007280 }
7281 mutex_unlock(&src_ctx->mutex);
7282
7283 synchronize_rcu();
7284
7285 mutex_lock(&dst_ctx->mutex);
Peter Zijlstra98861672013-10-03 16:02:23 +02007286 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
7287 list_del(&event->migrate_entry);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007288 if (event->state >= PERF_EVENT_STATE_OFF)
7289 event->state = PERF_EVENT_STATE_INACTIVE;
Frederic Weisbecker9a545de2013-07-23 02:31:03 +02007290 account_event_cpu(event, dst_cpu);
Yan, Zheng0cda4c02012-06-15 14:31:33 +08007291 perf_install_in_context(dst_ctx, event, dst_cpu);
7292 get_ctx(dst_ctx);
7293 }
7294 mutex_unlock(&dst_ctx->mutex);
7295}
7296EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
7297
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007298static void sync_child_event(struct perf_event *child_event,
7299 struct task_struct *child)
7300{
7301 struct perf_event *parent_event = child_event->parent;
7302 u64 child_val;
7303
7304 if (child_event->attr.inherit_stat)
7305 perf_event_read_event(child_event, child);
7306
Peter Zijlstrab5e58792010-05-21 14:43:12 +02007307 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007308
7309 /*
7310 * Add back the child's count to the parent's count:
7311 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02007312 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007313 atomic64_add(child_event->total_time_enabled,
7314 &parent_event->child_total_time_enabled);
7315 atomic64_add(child_event->total_time_running,
7316 &parent_event->child_total_time_running);
7317
7318 /*
7319 * Remove this event from the parent's list
7320 */
7321 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7322 mutex_lock(&parent_event->child_mutex);
7323 list_del_init(&child_event->child_list);
7324 mutex_unlock(&parent_event->child_mutex);
7325
7326 /*
7327 * Release the parent event, if this was the last
7328 * reference to it.
7329 */
Al Viroa6fa9412012-08-20 14:59:25 +01007330 put_event(parent_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007331}
7332
7333static void
7334__perf_event_exit_task(struct perf_event *child_event,
7335 struct perf_event_context *child_ctx,
7336 struct task_struct *child)
7337{
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007338 if (child_event->parent) {
7339 raw_spin_lock_irq(&child_ctx->lock);
7340 perf_group_detach(child_event);
7341 raw_spin_unlock_irq(&child_ctx->lock);
7342 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007343
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007344 perf_remove_from_context(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007345
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007346 /*
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007347 * It can happen that the parent exits first, and has events
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007348 * that are still around due to the child reference. These
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007349 * events need to be zapped.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007350 */
Peter Zijlstra38b435b2011-03-15 14:37:10 +01007351 if (child_event->parent) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007352 sync_child_event(child_event, child);
7353 free_event(child_event);
7354 }
7355}
7356
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007357static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007358{
7359 struct perf_event *child_event, *tmp;
7360 struct perf_event_context *child_ctx;
7361 unsigned long flags;
7362
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007363 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007364 perf_event_task(child, NULL, 0);
7365 return;
7366 }
7367
7368 local_irq_save(flags);
7369 /*
7370 * We can't reschedule here because interrupts are disabled,
7371 * and either child is current or it is a task that can't be
7372 * scheduled, so we are now safe from rescheduling changing
7373 * our context.
7374 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01007375 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007376
7377 /*
7378 * Take the context lock here so that if find_get_context is
7379 * reading child->perf_event_ctxp, we wait until it has
7380 * incremented the context's refcount before we do put_ctx below.
7381 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007382 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra04dc2db2011-04-09 21:17:43 +02007383 task_ctx_sched_out(child_ctx);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007384 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007385 /*
7386 * If this context is a clone; unclone it so it can't get
7387 * swapped to another process while we're removing all
7388 * the events from it.
7389 */
7390 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01007391 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01007392 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007393
7394 /*
7395 * Report the task dead after unscheduling the events so that we
7396 * won't get any samples after PERF_RECORD_EXIT. We can however still
7397 * get a few PERF_RECORD_READ events.
7398 */
7399 perf_event_task(child, child_ctx, 0);
7400
7401 /*
7402 * We can recurse on the same lock type through:
7403 *
7404 * __perf_event_exit_task()
7405 * sync_child_event()
Al Viroa6fa9412012-08-20 14:59:25 +01007406 * put_event()
7407 * mutex_lock(&ctx->mutex)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007408 *
7409 * But since its the parent context it won't be the same instance.
7410 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02007411 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007412
7413again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007414 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
7415 group_entry)
7416 __perf_event_exit_task(child_event, child_ctx, child);
7417
7418 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007419 group_entry)
7420 __perf_event_exit_task(child_event, child_ctx, child);
7421
7422 /*
7423 * If the last event was a group event, it will have appended all
7424 * its siblings to the list, but we obtained 'tmp' before that which
7425 * will still point to the list head terminating the iteration.
7426 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007427 if (!list_empty(&child_ctx->pinned_groups) ||
7428 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007429 goto again;
7430
7431 mutex_unlock(&child_ctx->mutex);
7432
7433 put_ctx(child_ctx);
7434}
7435
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007436/*
7437 * When a child task exits, feed back event values to parent events.
7438 */
7439void perf_event_exit_task(struct task_struct *child)
7440{
Peter Zijlstra88821352010-11-09 19:01:43 +01007441 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007442 int ctxn;
7443
Peter Zijlstra88821352010-11-09 19:01:43 +01007444 mutex_lock(&child->perf_event_mutex);
7445 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
7446 owner_entry) {
7447 list_del_init(&event->owner_entry);
7448
7449 /*
7450 * Ensure the list deletion is visible before we clear
7451 * the owner, closes a race against perf_release() where
7452 * we need to serialize on the owner->perf_event_mutex.
7453 */
7454 smp_wmb();
7455 event->owner = NULL;
7456 }
7457 mutex_unlock(&child->perf_event_mutex);
7458
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007459 for_each_task_context_nr(ctxn)
7460 perf_event_exit_task_context(child, ctxn);
7461}
7462
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007463static void perf_free_event(struct perf_event *event,
7464 struct perf_event_context *ctx)
7465{
7466 struct perf_event *parent = event->parent;
7467
7468 if (WARN_ON_ONCE(!parent))
7469 return;
7470
7471 mutex_lock(&parent->child_mutex);
7472 list_del_init(&event->child_list);
7473 mutex_unlock(&parent->child_mutex);
7474
Al Viroa6fa9412012-08-20 14:59:25 +01007475 put_event(parent);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007476
Peter Zijlstra8a495422010-05-27 15:47:49 +02007477 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007478 list_del_event(event, ctx);
7479 free_event(event);
7480}
7481
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007482/*
7483 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007484 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007485 */
7486void perf_event_free_task(struct task_struct *task)
7487{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007488 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007489 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007490 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007491
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007492 for_each_task_context_nr(ctxn) {
7493 ctx = task->perf_event_ctxp[ctxn];
7494 if (!ctx)
7495 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007496
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007497 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007498again:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007499 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
7500 group_entry)
7501 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007502
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007503 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
7504 group_entry)
7505 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007506
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007507 if (!list_empty(&ctx->pinned_groups) ||
7508 !list_empty(&ctx->flexible_groups))
7509 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007510
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007511 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007512
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007513 put_ctx(ctx);
7514 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007515}
7516
Peter Zijlstra4e231c72010-09-09 21:01:59 +02007517void perf_event_delayed_put(struct task_struct *task)
7518{
7519 int ctxn;
7520
7521 for_each_task_context_nr(ctxn)
7522 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
7523}
7524
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007525/*
7526 * inherit a event from parent task to child task:
7527 */
7528static struct perf_event *
7529inherit_event(struct perf_event *parent_event,
7530 struct task_struct *parent,
7531 struct perf_event_context *parent_ctx,
7532 struct task_struct *child,
7533 struct perf_event *group_leader,
7534 struct perf_event_context *child_ctx)
7535{
7536 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02007537 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007538
7539 /*
7540 * Instead of creating recursive hierarchies of events,
7541 * we link inherited events back to the original parent,
7542 * which has a filp for sure, which we use as the reference
7543 * count:
7544 */
7545 if (parent_event->parent)
7546 parent_event = parent_event->parent;
7547
7548 child_event = perf_event_alloc(&parent_event->attr,
7549 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02007550 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007551 group_leader, parent_event,
Avi Kivity4dc0da82011-06-29 18:42:35 +03007552 NULL, NULL);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007553 if (IS_ERR(child_event))
7554 return child_event;
Al Viroa6fa9412012-08-20 14:59:25 +01007555
7556 if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
7557 free_event(child_event);
7558 return NULL;
7559 }
7560
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007561 get_ctx(child_ctx);
7562
7563 /*
7564 * Make the child state follow the state of the parent event,
7565 * not its attr.disabled bit. We hold the parent's mutex,
7566 * so we won't race with perf_event_{en, dis}able_family.
7567 */
7568 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
7569 child_event->state = PERF_EVENT_STATE_INACTIVE;
7570 else
7571 child_event->state = PERF_EVENT_STATE_OFF;
7572
7573 if (parent_event->attr.freq) {
7574 u64 sample_period = parent_event->hw.sample_period;
7575 struct hw_perf_event *hwc = &child_event->hw;
7576
7577 hwc->sample_period = sample_period;
7578 hwc->last_period = sample_period;
7579
7580 local64_set(&hwc->period_left, sample_period);
7581 }
7582
7583 child_event->ctx = child_ctx;
7584 child_event->overflow_handler = parent_event->overflow_handler;
Avi Kivity4dc0da82011-06-29 18:42:35 +03007585 child_event->overflow_handler_context
7586 = parent_event->overflow_handler_context;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007587
7588 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02007589 * Precalculate sample_data sizes
7590 */
7591 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02007592 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02007593
7594 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007595 * Link it up in the child's context:
7596 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02007597 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007598 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02007599 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007600
7601 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02007602 * Link this into the parent event's child list
7603 */
7604 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
7605 mutex_lock(&parent_event->child_mutex);
7606 list_add_tail(&child_event->child_list, &parent_event->child_list);
7607 mutex_unlock(&parent_event->child_mutex);
7608
7609 return child_event;
7610}
7611
7612static int inherit_group(struct perf_event *parent_event,
7613 struct task_struct *parent,
7614 struct perf_event_context *parent_ctx,
7615 struct task_struct *child,
7616 struct perf_event_context *child_ctx)
7617{
7618 struct perf_event *leader;
7619 struct perf_event *sub;
7620 struct perf_event *child_ctr;
7621
7622 leader = inherit_event(parent_event, parent, parent_ctx,
7623 child, NULL, child_ctx);
7624 if (IS_ERR(leader))
7625 return PTR_ERR(leader);
7626 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
7627 child_ctr = inherit_event(sub, parent, parent_ctx,
7628 child, leader, child_ctx);
7629 if (IS_ERR(child_ctr))
7630 return PTR_ERR(child_ctr);
7631 }
7632 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007633}
7634
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007635static int
7636inherit_task_group(struct perf_event *event, struct task_struct *parent,
7637 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007638 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007639 int *inherited_all)
7640{
7641 int ret;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007642 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007643
7644 if (!event->attr.inherit) {
7645 *inherited_all = 0;
7646 return 0;
7647 }
7648
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007649 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007650 if (!child_ctx) {
7651 /*
7652 * This is executed from the parent task context, so
7653 * inherit events that have been marked for cloning.
7654 * First allocate and initialize a context for the
7655 * child.
7656 */
7657
Jiri Olsa734df5a2013-07-09 17:44:10 +02007658 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007659 if (!child_ctx)
7660 return -ENOMEM;
7661
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007662 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007663 }
7664
7665 ret = inherit_group(event, parent, parent_ctx,
7666 child, child_ctx);
7667
7668 if (ret)
7669 *inherited_all = 0;
7670
7671 return ret;
7672}
7673
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007674/*
7675 * Initialize the perf_event context in task_struct
7676 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007677int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007678{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007679 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007680 struct perf_event_context *cloned_ctx;
7681 struct perf_event *event;
7682 struct task_struct *parent = current;
7683 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007684 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007685 int ret = 0;
7686
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007687 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007688 return 0;
7689
7690 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007691 * If the parent's context is a clone, pin it so it won't get
7692 * swapped under us.
7693 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007694 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007695
7696 /*
7697 * No need to check if parent_ctx != NULL here; since we saw
7698 * it non-NULL earlier, the only reason for it to become NULL
7699 * is if we exit, and since we're currently in the middle of
7700 * a fork we can't be exiting at the same time.
7701 */
7702
7703 /*
7704 * Lock the parent list. No need to lock the child - not PID
7705 * hashed yet and not running, so nobody can access it.
7706 */
7707 mutex_lock(&parent_ctx->mutex);
7708
7709 /*
7710 * We dont have to disable NMIs - we are only looking at
7711 * the list, not manipulating it:
7712 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007713 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007714 ret = inherit_task_group(event, parent, parent_ctx,
7715 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007716 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007717 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007718 }
7719
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007720 /*
7721 * We can't hold ctx->lock when iterating the ->flexible_group list due
7722 * to allocations, but we need to prevent rotation because
7723 * rotate_ctx() will change the list from interrupt context.
7724 */
7725 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7726 parent_ctx->rotate_disable = 1;
7727 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7728
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007729 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007730 ret = inherit_task_group(event, parent, parent_ctx,
7731 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007732 if (ret)
7733 break;
7734 }
7735
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007736 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7737 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007738
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007739 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007740
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01007741 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007742 /*
7743 * Mark the child context as a clone of the parent
7744 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007745 *
7746 * Note that if the parent is a clone, the holding of
7747 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007748 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007749 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007750 if (cloned_ctx) {
7751 child_ctx->parent_ctx = cloned_ctx;
7752 child_ctx->parent_gen = parent_ctx->parent_gen;
7753 } else {
7754 child_ctx->parent_ctx = parent_ctx;
7755 child_ctx->parent_gen = parent_ctx->generation;
7756 }
7757 get_ctx(child_ctx->parent_ctx);
7758 }
7759
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007760 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007761 mutex_unlock(&parent_ctx->mutex);
7762
7763 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007764 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007765
7766 return ret;
7767}
7768
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007769/*
7770 * Initialize the perf_event context in task_struct
7771 */
7772int perf_event_init_task(struct task_struct *child)
7773{
7774 int ctxn, ret;
7775
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01007776 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7777 mutex_init(&child->perf_event_mutex);
7778 INIT_LIST_HEAD(&child->perf_event_list);
7779
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02007780 for_each_task_context_nr(ctxn) {
7781 ret = perf_event_init_context(child, ctxn);
7782 if (ret)
7783 return ret;
7784 }
7785
7786 return 0;
7787}
7788
Paul Mackerras220b1402010-03-10 20:45:52 +11007789static void __init perf_event_init_all_cpus(void)
7790{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007791 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11007792 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11007793
7794 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007795 swhash = &per_cpu(swevent_htable, cpu);
7796 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007797 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11007798 }
7799}
7800
Paul Gortmaker0db06282013-06-19 14:53:51 -04007801static void perf_event_init_cpu(int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007802{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007803 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007804
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007805 mutex_lock(&swhash->hlist_mutex);
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007806 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007807 struct swevent_hlist *hlist;
7808
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007809 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7810 WARN_ON(!hlist);
7811 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007812 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007813 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007814}
7815
Peter Zijlstrac2774432010-12-08 15:29:02 +01007816#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007817static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007818{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007819 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7820
7821 WARN_ON(!irqs_disabled());
7822
7823 list_del_init(&cpuctx->rotation_list);
7824}
7825
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007826static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007827{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007828 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007829 struct perf_event *event, *tmp;
7830
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007831 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02007832
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007833 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007834 __perf_remove_from_context(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007835 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007836 __perf_remove_from_context(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007837}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007838
7839static void perf_event_exit_cpu_context(int cpu)
7840{
7841 struct perf_event_context *ctx;
7842 struct pmu *pmu;
7843 int idx;
7844
7845 idx = srcu_read_lock(&pmus_srcu);
7846 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02007847 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007848
7849 mutex_lock(&ctx->mutex);
7850 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7851 mutex_unlock(&ctx->mutex);
7852 }
7853 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007854}
7855
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007856static void perf_event_exit_cpu(int cpu)
7857{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007858 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007859
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007860 mutex_lock(&swhash->hlist_mutex);
7861 swevent_hlist_release(swhash);
7862 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007863
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007864 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007865}
7866#else
7867static inline void perf_event_exit_cpu(int cpu) { }
7868#endif
7869
Peter Zijlstrac2774432010-12-08 15:29:02 +01007870static int
7871perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7872{
7873 int cpu;
7874
7875 for_each_online_cpu(cpu)
7876 perf_event_exit_cpu(cpu);
7877
7878 return NOTIFY_OK;
7879}
7880
7881/*
7882 * Run the perf reboot notifier at the very last possible moment so that
7883 * the generic watchdog code runs as long as possible.
7884 */
7885static struct notifier_block perf_reboot_notifier = {
7886 .notifier_call = perf_reboot,
7887 .priority = INT_MIN,
7888};
7889
Paul Gortmaker0db06282013-06-19 14:53:51 -04007890static int
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007891perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7892{
7893 unsigned int cpu = (long)hcpu;
7894
Linus Torvalds4536e4d2011-11-03 07:44:04 -07007895 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007896
7897 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02007898 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007899 perf_event_init_cpu(cpu);
7900 break;
7901
Peter Zijlstra5e116372010-06-11 13:35:08 +02007902 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007903 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007904 perf_event_exit_cpu(cpu);
7905 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007906 default:
7907 break;
7908 }
7909
7910 return NOTIFY_OK;
7911}
7912
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007913void __init perf_event_init(void)
7914{
Jason Wessel3c502e72010-11-04 17:33:01 -05007915 int ret;
7916
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007917 idr_init(&pmu_idr);
7918
Paul Mackerras220b1402010-03-10 20:45:52 +11007919 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007920 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007921 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7922 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7923 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007924 perf_tp_register();
7925 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01007926 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05007927
7928 ret = init_hw_breakpoint();
7929 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Gleb Natapovb2029522011-11-27 17:59:09 +02007930
7931 /* do not patch jump label more than once per second */
7932 jump_label_rate_limit(&perf_sched_events, HZ);
Jiri Olsab01c3a02012-03-23 15:41:20 +01007933
7934 /*
7935 * Build time assertion that we keep the data_head at the intended
7936 * location. IOW, validation we got the __reserved[] size right.
7937 */
7938 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
7939 != 1024);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007940}
Peter Zijlstraabe43402010-11-17 23:17:37 +01007941
7942static int __init perf_event_sysfs_init(void)
7943{
7944 struct pmu *pmu;
7945 int ret;
7946
7947 mutex_lock(&pmus_lock);
7948
7949 ret = bus_register(&pmu_bus);
7950 if (ret)
7951 goto unlock;
7952
7953 list_for_each_entry(pmu, &pmus, entry) {
7954 if (!pmu->name || pmu->type < 0)
7955 continue;
7956
7957 ret = pmu_dev_alloc(pmu);
7958 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7959 }
7960 pmu_bus_running = 1;
7961 ret = 0;
7962
7963unlock:
7964 mutex_unlock(&pmus_lock);
7965
7966 return ret;
7967}
7968device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007969
7970#ifdef CONFIG_CGROUP_PERF
Tejun Heoeb954192013-08-08 20:11:23 -04007971static struct cgroup_subsys_state *
7972perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007973{
7974 struct perf_cgroup *jc;
Stephane Eraniane5d13672011-02-14 11:20:01 +02007975
Li Zefan1b15d052011-03-03 14:26:06 +08007976 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007977 if (!jc)
7978 return ERR_PTR(-ENOMEM);
7979
Stephane Eraniane5d13672011-02-14 11:20:01 +02007980 jc->info = alloc_percpu(struct perf_cgroup_info);
7981 if (!jc->info) {
7982 kfree(jc);
7983 return ERR_PTR(-ENOMEM);
7984 }
7985
Stephane Eraniane5d13672011-02-14 11:20:01 +02007986 return &jc->css;
7987}
7988
Tejun Heoeb954192013-08-08 20:11:23 -04007989static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
Stephane Eraniane5d13672011-02-14 11:20:01 +02007990{
Tejun Heoeb954192013-08-08 20:11:23 -04007991 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
7992
Stephane Eraniane5d13672011-02-14 11:20:01 +02007993 free_percpu(jc->info);
7994 kfree(jc);
7995}
7996
7997static int __perf_cgroup_move(void *info)
7998{
7999 struct task_struct *task = info;
8000 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
8001 return 0;
8002}
8003
Tejun Heoeb954192013-08-08 20:11:23 -04008004static void perf_cgroup_attach(struct cgroup_subsys_state *css,
8005 struct cgroup_taskset *tset)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008006{
Tejun Heobb9d97b2011-12-12 18:12:21 -08008007 struct task_struct *task;
8008
Tejun Heod99c8722013-08-08 20:11:27 -04008009 cgroup_taskset_for_each(task, css, tset)
Tejun Heobb9d97b2011-12-12 18:12:21 -08008010 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008011}
8012
Tejun Heoeb954192013-08-08 20:11:23 -04008013static void perf_cgroup_exit(struct cgroup_subsys_state *css,
8014 struct cgroup_subsys_state *old_css,
Li Zefan761b3ef52012-01-31 13:47:36 +08008015 struct task_struct *task)
Stephane Eraniane5d13672011-02-14 11:20:01 +02008016{
8017 /*
8018 * cgroup_exit() is called in the copy_process() failure path.
8019 * Ignore this case since the task hasn't ran yet, this avoids
8020 * trying to poke a half freed task state from generic code.
8021 */
8022 if (!(task->flags & PF_EXITING))
8023 return;
8024
Tejun Heobb9d97b2011-12-12 18:12:21 -08008025 task_function_call(task, __perf_cgroup_move, task);
Stephane Eraniane5d13672011-02-14 11:20:01 +02008026}
8027
8028struct cgroup_subsys perf_subsys = {
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008029 .name = "perf_event",
8030 .subsys_id = perf_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08008031 .css_alloc = perf_cgroup_css_alloc,
8032 .css_free = perf_cgroup_css_free,
Ingo Molnare7e7ee22011-05-04 08:42:29 +02008033 .exit = perf_cgroup_exit,
Tejun Heobb9d97b2011-12-12 18:12:21 -08008034 .attach = perf_cgroup_attach,
Stephane Eraniane5d13672011-02-14 11:20:01 +02008035};
8036#endif /* CONFIG_CGROUP_PERF */