blob: 65dcdc76d709efff7f5596e149a24e2c6fd551aa [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>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
8 *
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>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020021#include <linux/sysfs.h>
22#include <linux/dcache.h>
23#include <linux/percpu.h>
24#include <linux/ptrace.h>
Peter Zijlstrac2774432010-12-08 15:29:02 +010025#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020026#include <linux/vmstat.h>
Peter Zijlstraabe43402010-11-17 23:17:37 +010027#include <linux/device.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020028#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020029#include <linux/hardirq.h>
30#include <linux/rculist.h>
31#include <linux/uaccess.h>
32#include <linux/syscalls.h>
33#include <linux/anon_inodes.h>
34#include <linux/kernel_stat.h>
35#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080036#include <linux/ftrace_event.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050037#include <linux/hw_breakpoint.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020038
39#include <asm/irq_regs.h>
40
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +010041struct remote_function_call {
42 struct task_struct *p;
43 int (*func)(void *info);
44 void *info;
45 int ret;
46};
47
48static void remote_function(void *data)
49{
50 struct remote_function_call *tfc = data;
51 struct task_struct *p = tfc->p;
52
53 if (p) {
54 tfc->ret = -EAGAIN;
55 if (task_cpu(p) != smp_processor_id() || !task_curr(p))
56 return;
57 }
58
59 tfc->ret = tfc->func(tfc->info);
60}
61
62/**
63 * task_function_call - call a function on the cpu on which a task runs
64 * @p: the task to evaluate
65 * @func: the function to be called
66 * @info: the function call argument
67 *
68 * Calls the function @func when the task is currently running. This might
69 * be on the current CPU, which just calls the function directly
70 *
71 * returns: @func return value, or
72 * -ESRCH - when the process isn't running
73 * -EAGAIN - when the process moved away
74 */
75static int
76task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
77{
78 struct remote_function_call data = {
79 .p = p,
80 .func = func,
81 .info = info,
82 .ret = -ESRCH, /* No such (running) process */
83 };
84
85 if (task_curr(p))
86 smp_call_function_single(task_cpu(p), remote_function, &data, 1);
87
88 return data.ret;
89}
90
91/**
92 * cpu_function_call - call a function on the cpu
93 * @func: the function to be called
94 * @info: the function call argument
95 *
96 * Calls the function @func on the remote cpu.
97 *
98 * returns: @func return value or -ENXIO when the cpu is offline
99 */
100static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
101{
102 struct remote_function_call data = {
103 .p = NULL,
104 .func = func,
105 .info = info,
106 .ret = -ENXIO, /* No such CPU */
107 };
108
109 smp_call_function_single(cpu, remote_function, &data, 1);
110
111 return data.ret;
112}
113
Stephane Eraniane5d13672011-02-14 11:20:01 +0200114#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
115 PERF_FLAG_FD_OUTPUT |\
116 PERF_FLAG_PID_CGROUP)
117
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200118enum event_type_t {
119 EVENT_FLEXIBLE = 0x1,
120 EVENT_PINNED = 0x2,
121 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
122};
123
Stephane Eraniane5d13672011-02-14 11:20:01 +0200124/*
125 * perf_sched_events : >0 events exist
126 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
127 */
128atomic_t perf_sched_events __read_mostly;
129static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
130
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200131static atomic_t nr_mmap_events __read_mostly;
132static atomic_t nr_comm_events __read_mostly;
133static atomic_t nr_task_events __read_mostly;
134
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200135static LIST_HEAD(pmus);
136static DEFINE_MUTEX(pmus_lock);
137static struct srcu_struct pmus_srcu;
138
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200139/*
140 * perf event paranoia level:
141 * -1 - not paranoid at all
142 * 0 - disallow raw tracepoint access for unpriv
143 * 1 - disallow cpu events for unpriv
144 * 2 - disallow kernel profiling for unpriv
145 */
146int sysctl_perf_event_paranoid __read_mostly = 1;
147
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200148int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
149
150/*
151 * max perf event sample rate
152 */
153int sysctl_perf_event_sample_rate __read_mostly = 100000;
154
155static atomic64_t perf_event_id;
156
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200157static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
158 enum event_type_t event_type);
159
160static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +0200161 enum event_type_t event_type,
162 struct task_struct *task);
163
164static void update_context_time(struct perf_event_context *ctx);
165static u64 perf_event_time(struct perf_event *event);
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200166
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200167void __weak perf_event_print_debug(void) { }
168
Matt Fleming84c79912010-10-03 21:41:13 +0100169extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200170{
Matt Fleming84c79912010-10-03 21:41:13 +0100171 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200172}
173
Stephane Eranian0b3fcf12011-01-03 18:20:01 +0200174static inline u64 perf_clock(void)
175{
176 return local_clock();
177}
178
Stephane Eraniane5d13672011-02-14 11:20:01 +0200179static inline struct perf_cpu_context *
180__get_cpu_context(struct perf_event_context *ctx)
181{
182 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
183}
184
185#ifdef CONFIG_CGROUP_PERF
186
187static inline struct perf_cgroup *
188perf_cgroup_from_task(struct task_struct *task)
189{
190 return container_of(task_subsys_state(task, perf_subsys_id),
191 struct perf_cgroup, css);
192}
193
194static inline bool
195perf_cgroup_match(struct perf_event *event)
196{
197 struct perf_event_context *ctx = event->ctx;
198 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
199
200 return !event->cgrp || event->cgrp == cpuctx->cgrp;
201}
202
203static inline void perf_get_cgroup(struct perf_event *event)
204{
205 css_get(&event->cgrp->css);
206}
207
208static inline void perf_put_cgroup(struct perf_event *event)
209{
210 css_put(&event->cgrp->css);
211}
212
213static inline void perf_detach_cgroup(struct perf_event *event)
214{
215 perf_put_cgroup(event);
216 event->cgrp = NULL;
217}
218
219static inline int is_cgroup_event(struct perf_event *event)
220{
221 return event->cgrp != NULL;
222}
223
224static inline u64 perf_cgroup_event_time(struct perf_event *event)
225{
226 struct perf_cgroup_info *t;
227
228 t = per_cpu_ptr(event->cgrp->info, event->cpu);
229 return t->time;
230}
231
232static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
233{
234 struct perf_cgroup_info *info;
235 u64 now;
236
237 now = perf_clock();
238
239 info = this_cpu_ptr(cgrp->info);
240
241 info->time += now - info->timestamp;
242 info->timestamp = now;
243}
244
245static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
246{
247 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
248 if (cgrp_out)
249 __update_cgrp_time(cgrp_out);
250}
251
252static inline void update_cgrp_time_from_event(struct perf_event *event)
253{
254 struct perf_cgroup *cgrp = perf_cgroup_from_task(current);
255 /*
256 * do not update time when cgroup is not active
257 */
258 if (!event->cgrp || cgrp != event->cgrp)
259 return;
260
261 __update_cgrp_time(event->cgrp);
262}
263
264static inline void
265perf_cgroup_set_timestamp(struct task_struct *task, u64 now)
266{
267 struct perf_cgroup *cgrp;
268 struct perf_cgroup_info *info;
269
270 if (!task)
271 return;
272
273 cgrp = perf_cgroup_from_task(task);
274 info = this_cpu_ptr(cgrp->info);
275 info->timestamp = now;
276}
277
278#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
279#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
280
281/*
282 * reschedule events based on the cgroup constraint of task.
283 *
284 * mode SWOUT : schedule out everything
285 * mode SWIN : schedule in based on cgroup for next
286 */
287void perf_cgroup_switch(struct task_struct *task, int mode)
288{
289 struct perf_cpu_context *cpuctx;
290 struct pmu *pmu;
291 unsigned long flags;
292
293 /*
294 * disable interrupts to avoid geting nr_cgroup
295 * changes via __perf_event_disable(). Also
296 * avoids preemption.
297 */
298 local_irq_save(flags);
299
300 /*
301 * we reschedule only in the presence of cgroup
302 * constrained events.
303 */
304 rcu_read_lock();
305
306 list_for_each_entry_rcu(pmu, &pmus, entry) {
307
308 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
309
310 perf_pmu_disable(cpuctx->ctx.pmu);
311
312 /*
313 * perf_cgroup_events says at least one
314 * context on this CPU has cgroup events.
315 *
316 * ctx->nr_cgroups reports the number of cgroup
317 * events for a context.
318 */
319 if (cpuctx->ctx.nr_cgroups > 0) {
320
321 if (mode & PERF_CGROUP_SWOUT) {
322 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
323 /*
324 * must not be done before ctxswout due
325 * to event_filter_match() in event_sched_out()
326 */
327 cpuctx->cgrp = NULL;
328 }
329
330 if (mode & PERF_CGROUP_SWIN) {
331 /* set cgrp before ctxsw in to
332 * allow event_filter_match() to not
333 * have to pass task around
334 */
335 cpuctx->cgrp = perf_cgroup_from_task(task);
336 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
337 }
338 }
339
340 perf_pmu_enable(cpuctx->ctx.pmu);
341 }
342
343 rcu_read_unlock();
344
345 local_irq_restore(flags);
346}
347
348static inline void perf_cgroup_sched_out(struct task_struct *task)
349{
350 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
351}
352
353static inline void perf_cgroup_sched_in(struct task_struct *task)
354{
355 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
356}
357
358static inline int perf_cgroup_connect(int fd, struct perf_event *event,
359 struct perf_event_attr *attr,
360 struct perf_event *group_leader)
361{
362 struct perf_cgroup *cgrp;
363 struct cgroup_subsys_state *css;
364 struct file *file;
365 int ret = 0, fput_needed;
366
367 file = fget_light(fd, &fput_needed);
368 if (!file)
369 return -EBADF;
370
371 css = cgroup_css_from_dir(file, perf_subsys_id);
372 if (IS_ERR(css))
373 return PTR_ERR(css);
374
375 cgrp = container_of(css, struct perf_cgroup, css);
376 event->cgrp = cgrp;
377
378 /*
379 * all events in a group must monitor
380 * the same cgroup because a task belongs
381 * to only one perf cgroup at a time
382 */
383 if (group_leader && group_leader->cgrp != cgrp) {
384 perf_detach_cgroup(event);
385 ret = -EINVAL;
386 } else {
387 /* must be done before we fput() the file */
388 perf_get_cgroup(event);
389 }
390 fput_light(file, fput_needed);
391 return ret;
392}
393
394static inline void
395perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
396{
397 struct perf_cgroup_info *t;
398 t = per_cpu_ptr(event->cgrp->info, event->cpu);
399 event->shadow_ctx_time = now - t->timestamp;
400}
401
402static inline void
403perf_cgroup_defer_enabled(struct perf_event *event)
404{
405 /*
406 * when the current task's perf cgroup does not match
407 * the event's, we need to remember to call the
408 * perf_mark_enable() function the first time a task with
409 * a matching perf cgroup is scheduled in.
410 */
411 if (is_cgroup_event(event) && !perf_cgroup_match(event))
412 event->cgrp_defer_enabled = 1;
413}
414
415static inline void
416perf_cgroup_mark_enabled(struct perf_event *event,
417 struct perf_event_context *ctx)
418{
419 struct perf_event *sub;
420 u64 tstamp = perf_event_time(event);
421
422 if (!event->cgrp_defer_enabled)
423 return;
424
425 event->cgrp_defer_enabled = 0;
426
427 event->tstamp_enabled = tstamp - event->total_time_enabled;
428 list_for_each_entry(sub, &event->sibling_list, group_entry) {
429 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
430 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
431 sub->cgrp_defer_enabled = 0;
432 }
433 }
434}
435#else /* !CONFIG_CGROUP_PERF */
436
437static inline bool
438perf_cgroup_match(struct perf_event *event)
439{
440 return true;
441}
442
443static inline void perf_detach_cgroup(struct perf_event *event)
444{}
445
446static inline int is_cgroup_event(struct perf_event *event)
447{
448 return 0;
449}
450
451static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
452{
453 return 0;
454}
455
456static inline void update_cgrp_time_from_event(struct perf_event *event)
457{
458}
459
460static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
461{
462}
463
464static inline void perf_cgroup_sched_out(struct task_struct *task)
465{
466}
467
468static inline void perf_cgroup_sched_in(struct task_struct *task)
469{
470}
471
472static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
473 struct perf_event_attr *attr,
474 struct perf_event *group_leader)
475{
476 return -EINVAL;
477}
478
479static inline void
480perf_cgroup_set_timestamp(struct task_struct *task, u64 now)
481{
482}
483
484void
485perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
486{
487}
488
489static inline void
490perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
491{
492}
493
494static inline u64 perf_cgroup_event_time(struct perf_event *event)
495{
496 return 0;
497}
498
499static inline void
500perf_cgroup_defer_enabled(struct perf_event *event)
501{
502}
503
504static inline void
505perf_cgroup_mark_enabled(struct perf_event *event,
506 struct perf_event_context *ctx)
507{
508}
509#endif
510
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200511void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200512{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200513 int *count = this_cpu_ptr(pmu->pmu_disable_count);
514 if (!(*count)++)
515 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200516}
517
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200518void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200519{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200520 int *count = this_cpu_ptr(pmu->pmu_disable_count);
521 if (!--(*count))
522 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200523}
524
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200525static DEFINE_PER_CPU(struct list_head, rotation_list);
526
527/*
528 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
529 * because they're strictly cpu affine and rotate_start is called with IRQs
530 * disabled, while rotate_context is called from IRQ context.
531 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200532static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200533{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200534 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200535 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200536
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200537 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200538
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200539 if (list_empty(&cpuctx->rotation_list))
540 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200541}
542
543static void get_ctx(struct perf_event_context *ctx)
544{
545 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
546}
547
548static void free_ctx(struct rcu_head *head)
549{
550 struct perf_event_context *ctx;
551
552 ctx = container_of(head, struct perf_event_context, rcu_head);
553 kfree(ctx);
554}
555
556static void put_ctx(struct perf_event_context *ctx)
557{
558 if (atomic_dec_and_test(&ctx->refcount)) {
559 if (ctx->parent_ctx)
560 put_ctx(ctx->parent_ctx);
561 if (ctx->task)
562 put_task_struct(ctx->task);
563 call_rcu(&ctx->rcu_head, free_ctx);
564 }
565}
566
567static void unclone_ctx(struct perf_event_context *ctx)
568{
569 if (ctx->parent_ctx) {
570 put_ctx(ctx->parent_ctx);
571 ctx->parent_ctx = NULL;
572 }
573}
574
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200575static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
576{
577 /*
578 * only top level events have the pid namespace they were created in
579 */
580 if (event->parent)
581 event = event->parent;
582
583 return task_tgid_nr_ns(p, event->ns);
584}
585
586static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
587{
588 /*
589 * only top level events have the pid namespace they were created in
590 */
591 if (event->parent)
592 event = event->parent;
593
594 return task_pid_nr_ns(p, event->ns);
595}
596
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200597/*
598 * If we inherit events we want to return the parent event id
599 * to userspace.
600 */
601static u64 primary_event_id(struct perf_event *event)
602{
603 u64 id = event->id;
604
605 if (event->parent)
606 id = event->parent->id;
607
608 return id;
609}
610
611/*
612 * Get the perf_event_context for a task and lock it.
613 * This has to cope with with the fact that until it is locked,
614 * the context could get moved to another task.
615 */
616static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200617perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200618{
619 struct perf_event_context *ctx;
620
621 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200622retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200623 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200624 if (ctx) {
625 /*
626 * If this context is a clone of another, it might
627 * get swapped for another underneath us by
628 * perf_event_task_sched_out, though the
629 * rcu_read_lock() protects us from any context
630 * getting freed. Lock the context and check if it
631 * got swapped before we could get the lock, and retry
632 * if so. If we locked the right context, then it
633 * can't get swapped on us any more.
634 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100635 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200636 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100637 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200638 goto retry;
639 }
640
641 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100642 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200643 ctx = NULL;
644 }
645 }
646 rcu_read_unlock();
647 return ctx;
648}
649
650/*
651 * Get the context for a task and increment its pin_count so it
652 * can't get swapped to another task. This also increments its
653 * reference count so that the context can't get freed.
654 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200655static struct perf_event_context *
656perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200657{
658 struct perf_event_context *ctx;
659 unsigned long flags;
660
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200661 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200662 if (ctx) {
663 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100664 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200665 }
666 return ctx;
667}
668
669static void perf_unpin_context(struct perf_event_context *ctx)
670{
671 unsigned long flags;
672
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100673 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200674 --ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100675 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200676}
677
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100678/*
679 * Update the record of the current time in a context.
680 */
681static void update_context_time(struct perf_event_context *ctx)
682{
683 u64 now = perf_clock();
684
685 ctx->time += now - ctx->timestamp;
686 ctx->timestamp = now;
687}
688
Stephane Eranian41587552011-01-03 18:20:01 +0200689static u64 perf_event_time(struct perf_event *event)
690{
691 struct perf_event_context *ctx = event->ctx;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200692
693 if (is_cgroup_event(event))
694 return perf_cgroup_event_time(event);
695
Stephane Eranian41587552011-01-03 18:20:01 +0200696 return ctx ? ctx->time : 0;
697}
698
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100699/*
700 * Update the total_time_enabled and total_time_running fields for a event.
701 */
702static void update_event_times(struct perf_event *event)
703{
704 struct perf_event_context *ctx = event->ctx;
705 u64 run_end;
706
707 if (event->state < PERF_EVENT_STATE_INACTIVE ||
708 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
709 return;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200710 /*
711 * in cgroup mode, time_enabled represents
712 * the time the event was enabled AND active
713 * tasks were in the monitored cgroup. This is
714 * independent of the activity of the context as
715 * there may be a mix of cgroup and non-cgroup events.
716 *
717 * That is why we treat cgroup events differently
718 * here.
719 */
720 if (is_cgroup_event(event))
Stephane Eranian41587552011-01-03 18:20:01 +0200721 run_end = perf_event_time(event);
Stephane Eraniane5d13672011-02-14 11:20:01 +0200722 else if (ctx->is_active)
723 run_end = ctx->time;
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100724 else
725 run_end = event->tstamp_stopped;
726
727 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100728
729 if (event->state == PERF_EVENT_STATE_INACTIVE)
730 run_end = event->tstamp_stopped;
731 else
Stephane Eranian41587552011-01-03 18:20:01 +0200732 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100733
734 event->total_time_running = run_end - event->tstamp_running;
Stephane Eraniane5d13672011-02-14 11:20:01 +0200735
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100736}
737
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200738/*
739 * Update total_time_enabled and total_time_running for all events in a group.
740 */
741static void update_group_times(struct perf_event *leader)
742{
743 struct perf_event *event;
744
745 update_event_times(leader);
746 list_for_each_entry(event, &leader->sibling_list, group_entry)
747 update_event_times(event);
748}
749
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100750static struct list_head *
751ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
752{
753 if (event->attr.pinned)
754 return &ctx->pinned_groups;
755 else
756 return &ctx->flexible_groups;
757}
758
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200759/*
760 * Add a event from the lists for its context.
761 * Must be called with ctx->mutex and ctx->lock held.
762 */
763static void
764list_add_event(struct perf_event *event, struct perf_event_context *ctx)
765{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200766 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
767 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200768
769 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200770 * If we're a stand alone event or group leader, we go to the context
771 * list, group events are kept attached to the group so that
772 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200773 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200774 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100775 struct list_head *list;
776
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100777 if (is_software_event(event))
778 event->group_flags |= PERF_GROUP_SOFTWARE;
779
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100780 list = ctx_group_list(event, ctx);
781 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200782 }
783
Stephane Eraniane5d13672011-02-14 11:20:01 +0200784 if (is_cgroup_event(event)) {
785 ctx->nr_cgroups++;
786 /*
787 * one more event:
788 * - that has cgroup constraint on event->cpu
789 * - that may need work on context switch
790 */
791 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
792 jump_label_inc(&perf_sched_events);
793 }
794
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200795 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200796 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200797 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200798 ctx->nr_events++;
799 if (event->attr.inherit_stat)
800 ctx->nr_stat++;
801}
802
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200803/*
804 * Called at perf_event creation and when events are attached/detached from a
805 * group.
806 */
807static void perf_event__read_size(struct perf_event *event)
808{
809 int entry = sizeof(u64); /* value */
810 int size = 0;
811 int nr = 1;
812
813 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
814 size += sizeof(u64);
815
816 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
817 size += sizeof(u64);
818
819 if (event->attr.read_format & PERF_FORMAT_ID)
820 entry += sizeof(u64);
821
822 if (event->attr.read_format & PERF_FORMAT_GROUP) {
823 nr += event->group_leader->nr_siblings;
824 size += sizeof(u64);
825 }
826
827 size += entry * nr;
828 event->read_size = size;
829}
830
831static void perf_event__header_size(struct perf_event *event)
832{
833 struct perf_sample_data *data;
834 u64 sample_type = event->attr.sample_type;
835 u16 size = 0;
836
837 perf_event__read_size(event);
838
839 if (sample_type & PERF_SAMPLE_IP)
840 size += sizeof(data->ip);
841
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200842 if (sample_type & PERF_SAMPLE_ADDR)
843 size += sizeof(data->addr);
844
845 if (sample_type & PERF_SAMPLE_PERIOD)
846 size += sizeof(data->period);
847
848 if (sample_type & PERF_SAMPLE_READ)
849 size += event->read_size;
850
851 event->header_size = size;
852}
853
854static void perf_event__id_header_size(struct perf_event *event)
855{
856 struct perf_sample_data *data;
857 u64 sample_type = event->attr.sample_type;
858 u16 size = 0;
859
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200860 if (sample_type & PERF_SAMPLE_TID)
861 size += sizeof(data->tid_entry);
862
863 if (sample_type & PERF_SAMPLE_TIME)
864 size += sizeof(data->time);
865
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200866 if (sample_type & PERF_SAMPLE_ID)
867 size += sizeof(data->id);
868
869 if (sample_type & PERF_SAMPLE_STREAM_ID)
870 size += sizeof(data->stream_id);
871
872 if (sample_type & PERF_SAMPLE_CPU)
873 size += sizeof(data->cpu_entry);
874
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200875 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200876}
877
Peter Zijlstra8a495422010-05-27 15:47:49 +0200878static void perf_group_attach(struct perf_event *event)
879{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200880 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200881
Peter Zijlstra74c33372010-10-15 11:40:29 +0200882 /*
883 * We can have double attach due to group movement in perf_event_open.
884 */
885 if (event->attach_state & PERF_ATTACH_GROUP)
886 return;
887
Peter Zijlstra8a495422010-05-27 15:47:49 +0200888 event->attach_state |= PERF_ATTACH_GROUP;
889
890 if (group_leader == event)
891 return;
892
893 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
894 !is_software_event(event))
895 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
896
897 list_add_tail(&event->group_entry, &group_leader->sibling_list);
898 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200899
900 perf_event__header_size(group_leader);
901
902 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
903 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200904}
905
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200906/*
907 * Remove a event from the lists for its context.
908 * Must be called with ctx->mutex and ctx->lock held.
909 */
910static void
911list_del_event(struct perf_event *event, struct perf_event_context *ctx)
912{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200913 /*
914 * We can have double detach due to exit/hot-unplug + close.
915 */
916 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200917 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200918
919 event->attach_state &= ~PERF_ATTACH_CONTEXT;
920
Stephane Eraniane5d13672011-02-14 11:20:01 +0200921 if (is_cgroup_event(event)) {
922 ctx->nr_cgroups--;
923 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
924 jump_label_dec(&perf_sched_events);
925 }
926
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200927 ctx->nr_events--;
928 if (event->attr.inherit_stat)
929 ctx->nr_stat--;
930
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200931 list_del_rcu(&event->event_entry);
932
Peter Zijlstra8a495422010-05-27 15:47:49 +0200933 if (event->group_leader == event)
934 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200935
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200936 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800937
938 /*
939 * If event was in error state, then keep it
940 * that way, otherwise bogus counts will be
941 * returned on read(). The only way to get out
942 * of error state is by explicit re-enabling
943 * of the event
944 */
945 if (event->state > PERF_EVENT_STATE_OFF)
946 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200947}
948
Peter Zijlstra8a495422010-05-27 15:47:49 +0200949static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200950{
951 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200952 struct list_head *list = NULL;
953
954 /*
955 * We can have double detach due to exit/hot-unplug + close.
956 */
957 if (!(event->attach_state & PERF_ATTACH_GROUP))
958 return;
959
960 event->attach_state &= ~PERF_ATTACH_GROUP;
961
962 /*
963 * If this is a sibling, remove it from its group.
964 */
965 if (event->group_leader != event) {
966 list_del_init(&event->group_entry);
967 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200968 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200969 }
970
971 if (!list_empty(&event->group_entry))
972 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100973
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200974 /*
975 * If this was a group event with sibling events then
976 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +0200977 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200978 */
979 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +0200980 if (list)
981 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200982 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100983
984 /* Inherit group flags from the previous leader */
985 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200986 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200987
988out:
989 perf_event__header_size(event->group_leader);
990
991 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
992 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200993}
994
Stephane Eranianfa66f072010-08-26 16:40:01 +0200995static inline int
996event_filter_match(struct perf_event *event)
997{
Stephane Eraniane5d13672011-02-14 11:20:01 +0200998 return (event->cpu == -1 || event->cpu == smp_processor_id())
999 && perf_cgroup_match(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001000}
1001
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001002static void
1003event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001004 struct perf_cpu_context *cpuctx,
1005 struct perf_event_context *ctx)
1006{
Stephane Eranian41587552011-01-03 18:20:01 +02001007 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +02001008 u64 delta;
1009 /*
1010 * An event which could not be activated because of
1011 * filter mismatch still needs to have its timings
1012 * maintained, otherwise bogus information is return
1013 * via read() for time_enabled, time_running:
1014 */
1015 if (event->state == PERF_EVENT_STATE_INACTIVE
1016 && !event_filter_match(event)) {
Stephane Eraniane5d13672011-02-14 11:20:01 +02001017 delta = tstamp - event->tstamp_stopped;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001018 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +02001019 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001020 }
1021
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001022 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001023 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001024
1025 event->state = PERF_EVENT_STATE_INACTIVE;
1026 if (event->pending_disable) {
1027 event->pending_disable = 0;
1028 event->state = PERF_EVENT_STATE_OFF;
1029 }
Stephane Eranian41587552011-01-03 18:20:01 +02001030 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001031 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001032 event->oncpu = -1;
1033
1034 if (!is_software_event(event))
1035 cpuctx->active_oncpu--;
1036 ctx->nr_active--;
1037 if (event->attr.exclusive || !cpuctx->active_oncpu)
1038 cpuctx->exclusive = 0;
1039}
1040
1041static void
1042group_sched_out(struct perf_event *group_event,
1043 struct perf_cpu_context *cpuctx,
1044 struct perf_event_context *ctx)
1045{
1046 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +02001047 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001048
1049 event_sched_out(group_event, cpuctx, ctx);
1050
1051 /*
1052 * Schedule out siblings (if any):
1053 */
1054 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1055 event_sched_out(event, cpuctx, ctx);
1056
Stephane Eranianfa66f072010-08-26 16:40:01 +02001057 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001058 cpuctx->exclusive = 0;
1059}
1060
1061/*
1062 * Cross CPU call to remove a performance event
1063 *
1064 * We disable the event on the hardware level first. After that we
1065 * remove it from the context list.
1066 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001067static int __perf_remove_from_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001068{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001069 struct perf_event *event = info;
1070 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001071 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001072
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001073 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001074 event_sched_out(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001075 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001076 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001077
1078 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001079}
1080
1081
1082/*
1083 * Remove the event from a task's (or a CPU's) list of events.
1084 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001085 * CPU events are removed with a smp call. For task events we only
1086 * call when the task is on a CPU.
1087 *
1088 * If event->ctx is a cloned context, callers must make sure that
1089 * every task struct that event->ctx->task could possibly point to
1090 * remains valid. This is OK when called from perf_release since
1091 * that only calls us on the top-level context, which can't be a clone.
1092 * When called from perf_event_exit_task, it's OK because the
1093 * context has been detached from its task.
1094 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001095static void perf_remove_from_context(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001096{
1097 struct perf_event_context *ctx = event->ctx;
1098 struct task_struct *task = ctx->task;
1099
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001100 lockdep_assert_held(&ctx->mutex);
1101
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001102 if (!task) {
1103 /*
1104 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001105 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001106 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001107 cpu_function_call(event->cpu, __perf_remove_from_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001108 return;
1109 }
1110
1111retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001112 if (!task_function_call(task, __perf_remove_from_context, event))
1113 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001114
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001115 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001116 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001117 * If we failed to find a running task, but find the context active now
1118 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001119 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001120 if (ctx->is_active) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001121 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001122 goto retry;
1123 }
1124
1125 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001126 * Since the task isn't running, its safe to remove the event, us
1127 * holding the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001128 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001129 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001130 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001131}
1132
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001133/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001134 * Cross CPU call to disable a performance event
1135 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001136static int __perf_event_disable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001137{
1138 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001139 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001140 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001141
1142 /*
1143 * If this is a per-task event, need to check whether this
1144 * event's task is the current task on this cpu.
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001145 *
1146 * Can trigger due to concurrent perf_event_context_sched_out()
1147 * flipping contexts around.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001148 */
1149 if (ctx->task && cpuctx->task_ctx != ctx)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001150 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001151
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001152 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001153
1154 /*
1155 * If the event is on, turn it off.
1156 * If it is in error state, leave it in error state.
1157 */
1158 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
1159 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001160 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001161 update_group_times(event);
1162 if (event == event->group_leader)
1163 group_sched_out(event, cpuctx, ctx);
1164 else
1165 event_sched_out(event, cpuctx, ctx);
1166 event->state = PERF_EVENT_STATE_OFF;
1167 }
1168
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001169 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001170
1171 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001172}
1173
1174/*
1175 * Disable a event.
1176 *
1177 * If event->ctx is a cloned context, callers must make sure that
1178 * every task struct that event->ctx->task could possibly point to
1179 * remains valid. This condition is satisifed when called through
1180 * perf_event_for_each_child or perf_event_for_each because they
1181 * hold the top-level event's child_mutex, so any descendant that
1182 * goes to exit will block in sync_child_event.
1183 * When called from perf_pending_event it's OK because event->ctx
1184 * is the current context on this CPU and preemption is disabled,
1185 * hence we can't get into perf_event_task_sched_out for this context.
1186 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001187void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001188{
1189 struct perf_event_context *ctx = event->ctx;
1190 struct task_struct *task = ctx->task;
1191
1192 if (!task) {
1193 /*
1194 * Disable the event on the cpu that it's on
1195 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001196 cpu_function_call(event->cpu, __perf_event_disable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001197 return;
1198 }
1199
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001200retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001201 if (!task_function_call(task, __perf_event_disable, event))
1202 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001203
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001204 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001205 /*
1206 * If the event is still active, we need to retry the cross-call.
1207 */
1208 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001209 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001210 /*
1211 * Reload the task pointer, it might have been changed by
1212 * a concurrent perf_event_context_sched_out().
1213 */
1214 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001215 goto retry;
1216 }
1217
1218 /*
1219 * Since we have the lock this context can't be scheduled
1220 * in, so we can change the state safely.
1221 */
1222 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1223 update_group_times(event);
1224 event->state = PERF_EVENT_STATE_OFF;
1225 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001226 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001227}
1228
Stephane Eraniane5d13672011-02-14 11:20:01 +02001229static void perf_set_shadow_time(struct perf_event *event,
1230 struct perf_event_context *ctx,
1231 u64 tstamp)
1232{
1233 /*
1234 * use the correct time source for the time snapshot
1235 *
1236 * We could get by without this by leveraging the
1237 * fact that to get to this function, the caller
1238 * has most likely already called update_context_time()
1239 * and update_cgrp_time_xx() and thus both timestamp
1240 * are identical (or very close). Given that tstamp is,
1241 * already adjusted for cgroup, we could say that:
1242 * tstamp - ctx->timestamp
1243 * is equivalent to
1244 * tstamp - cgrp->timestamp.
1245 *
1246 * Then, in perf_output_read(), the calculation would
1247 * work with no changes because:
1248 * - event is guaranteed scheduled in
1249 * - no scheduled out in between
1250 * - thus the timestamp would be the same
1251 *
1252 * But this is a bit hairy.
1253 *
1254 * So instead, we have an explicit cgroup call to remain
1255 * within the time time source all along. We believe it
1256 * is cleaner and simpler to understand.
1257 */
1258 if (is_cgroup_event(event))
1259 perf_cgroup_set_shadow_time(event, tstamp);
1260 else
1261 event->shadow_ctx_time = tstamp - ctx->timestamp;
1262}
1263
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001264#define MAX_INTERRUPTS (~0ULL)
1265
1266static void perf_log_throttle(struct perf_event *event, int enable);
1267
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001268static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001269event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001270 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001271 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001272{
Stephane Eranian41587552011-01-03 18:20:01 +02001273 u64 tstamp = perf_event_time(event);
1274
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001275 if (event->state <= PERF_EVENT_STATE_OFF)
1276 return 0;
1277
1278 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001279 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +01001280
1281 /*
1282 * Unthrottle events, since we scheduled we might have missed several
1283 * ticks already, also for a heavily scheduling task there is little
1284 * guarantee it'll get a tick in a timely manner.
1285 */
1286 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1287 perf_log_throttle(event, 1);
1288 event->hw.interrupts = 0;
1289 }
1290
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001291 /*
1292 * The new state must be visible before we turn it on in the hardware:
1293 */
1294 smp_wmb();
1295
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001296 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001297 event->state = PERF_EVENT_STATE_INACTIVE;
1298 event->oncpu = -1;
1299 return -EAGAIN;
1300 }
1301
Stephane Eranian41587552011-01-03 18:20:01 +02001302 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001303
Stephane Eraniane5d13672011-02-14 11:20:01 +02001304 perf_set_shadow_time(event, ctx, tstamp);
Stephane Eranianeed01522010-10-26 16:08:01 +02001305
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001306 if (!is_software_event(event))
1307 cpuctx->active_oncpu++;
1308 ctx->nr_active++;
1309
1310 if (event->attr.exclusive)
1311 cpuctx->exclusive = 1;
1312
1313 return 0;
1314}
1315
1316static int
1317group_sched_in(struct perf_event *group_event,
1318 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001319 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001320{
Lin Ming6bde9b62010-04-23 13:56:00 +08001321 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02001322 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +02001323 u64 now = ctx->time;
1324 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001325
1326 if (group_event->state == PERF_EVENT_STATE_OFF)
1327 return 0;
1328
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001329 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +08001330
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001331 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001332 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001333 return -EAGAIN;
Stephane Eranian90151c32010-05-25 16:23:10 +02001334 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001335
1336 /*
1337 * Schedule in siblings as one group (if any):
1338 */
1339 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001340 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001341 partial_group = event;
1342 goto group_error;
1343 }
1344 }
1345
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001346 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +10001347 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001348
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001349group_error:
1350 /*
1351 * Groups can be scheduled in as one unit only, so undo any
1352 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +02001353 * The events up to the failed event are scheduled out normally,
1354 * tstamp_stopped will be updated.
1355 *
1356 * The failed events and the remaining siblings need to have
1357 * their timings updated as if they had gone thru event_sched_in()
1358 * and event_sched_out(). This is required to get consistent timings
1359 * across the group. This also takes care of the case where the group
1360 * could never be scheduled by ensuring tstamp_stopped is set to mark
1361 * the time the event was actually stopped, such that time delta
1362 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001363 */
1364 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
1365 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +02001366 simulate = true;
1367
1368 if (simulate) {
1369 event->tstamp_running += now - event->tstamp_stopped;
1370 event->tstamp_stopped = now;
1371 } else {
1372 event_sched_out(event, cpuctx, ctx);
1373 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001374 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +02001375 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001376
Peter Zijlstraad5133b2010-06-15 12:22:39 +02001377 pmu->cancel_txn(pmu);
Stephane Eranian90151c32010-05-25 16:23:10 +02001378
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001379 return -EAGAIN;
1380}
1381
1382/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001383 * Work out whether we can put this event group on the CPU now.
1384 */
1385static int group_can_go_on(struct perf_event *event,
1386 struct perf_cpu_context *cpuctx,
1387 int can_add_hw)
1388{
1389 /*
1390 * Groups consisting entirely of software events can always go on.
1391 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +01001392 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001393 return 1;
1394 /*
1395 * If an exclusive group is already on, no other hardware
1396 * events can go on.
1397 */
1398 if (cpuctx->exclusive)
1399 return 0;
1400 /*
1401 * If this group is exclusive and there are already
1402 * events on the CPU, it can't go on.
1403 */
1404 if (event->attr.exclusive && cpuctx->active_oncpu)
1405 return 0;
1406 /*
1407 * Otherwise, try to add it if all previous groups were able
1408 * to go on.
1409 */
1410 return can_add_hw;
1411}
1412
1413static void add_event_to_ctx(struct perf_event *event,
1414 struct perf_event_context *ctx)
1415{
Stephane Eranian41587552011-01-03 18:20:01 +02001416 u64 tstamp = perf_event_time(event);
1417
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001418 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +02001419 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +02001420 event->tstamp_enabled = tstamp;
1421 event->tstamp_running = tstamp;
1422 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001423}
1424
Stephane Eraniane5d13672011-02-14 11:20:01 +02001425static void perf_event_context_sched_in(struct perf_event_context *ctx,
1426 struct task_struct *tsk);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001427
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001428/*
1429 * Cross CPU call to install and enable a performance event
1430 *
1431 * Must be called with ctx->mutex held
1432 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001433static int __perf_install_in_context(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001434{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001435 struct perf_event *event = info;
1436 struct perf_event_context *ctx = event->ctx;
1437 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001438 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001439 int err;
1440
1441 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001442 * In case we're installing a new context to an already running task,
1443 * could also happen before perf_event_task_sched_in() on architectures
1444 * which do context switches with IRQs enabled.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001445 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001446 if (ctx->task && !cpuctx->task_ctx)
Stephane Eraniane5d13672011-02-14 11:20:01 +02001447 perf_event_context_sched_in(ctx, ctx->task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001448
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001449 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001450 ctx->is_active = 1;
1451 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001452 /*
1453 * update cgrp time only if current cgrp
1454 * matches event->cgrp. Must be done before
1455 * calling add_event_to_ctx()
1456 */
1457 update_cgrp_time_from_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001458
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001459 add_event_to_ctx(event, ctx);
1460
Stephane Eranian5632ab12011-01-03 18:20:01 +02001461 if (!event_filter_match(event))
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001462 goto unlock;
1463
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001464 /*
1465 * Don't put the event on if it is disabled or if
1466 * it is in a group and the group isn't on.
1467 */
1468 if (event->state != PERF_EVENT_STATE_INACTIVE ||
1469 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
1470 goto unlock;
1471
1472 /*
1473 * An exclusive event can't go on if there are already active
1474 * hardware events, and no hardware event can go on if there
1475 * is already an exclusive event on.
1476 */
1477 if (!group_can_go_on(event, cpuctx, 1))
1478 err = -EEXIST;
1479 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001480 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001481
1482 if (err) {
1483 /*
1484 * This event couldn't go on. If it is in a group
1485 * then we have to pull the whole group off.
1486 * If the event group is pinned then put it in error state.
1487 */
1488 if (leader != event)
1489 group_sched_out(leader, cpuctx, ctx);
1490 if (leader->attr.pinned) {
1491 update_group_times(leader);
1492 leader->state = PERF_EVENT_STATE_ERROR;
1493 }
1494 }
1495
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001496unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001497 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001498
1499 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001500}
1501
1502/*
1503 * Attach a performance event to a context
1504 *
1505 * First we add the event to the list with the hardware enable bit
1506 * in event->hw_config cleared.
1507 *
1508 * If the event is attached to a task which is on a CPU we use a smp
1509 * call to enable it in the task context. The task might have been
1510 * scheduled away, but we check this in the smp call again.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001511 */
1512static void
1513perf_install_in_context(struct perf_event_context *ctx,
1514 struct perf_event *event,
1515 int cpu)
1516{
1517 struct task_struct *task = ctx->task;
1518
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001519 lockdep_assert_held(&ctx->mutex);
1520
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001521 event->ctx = ctx;
1522
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001523 if (!task) {
1524 /*
1525 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001526 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001527 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001528 cpu_function_call(cpu, __perf_install_in_context, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001529 return;
1530 }
1531
1532retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001533 if (!task_function_call(task, __perf_install_in_context, event))
1534 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001535
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001536 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001537 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001538 * If we failed to find a running task, but find the context active now
1539 * that we've acquired the ctx->lock, retry.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001540 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001541 if (ctx->is_active) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001542 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001543 goto retry;
1544 }
1545
1546 /*
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001547 * Since the task isn't running, its safe to add the event, us holding
1548 * the ctx->lock ensures the task won't get scheduled in.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001549 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001550 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001551 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001552}
1553
1554/*
1555 * Put a event into inactive state and update time fields.
1556 * Enabling the leader of a group effectively enables all
1557 * the group members that aren't explicitly disabled, so we
1558 * have to update their ->tstamp_enabled also.
1559 * Note: this works for group members as well as group leaders
1560 * since the non-leader members' sibling_lists will be empty.
1561 */
1562static void __perf_event_mark_enabled(struct perf_event *event,
1563 struct perf_event_context *ctx)
1564{
1565 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001566 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001567
1568 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001569 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001570 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001571 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1572 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001573 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001574}
1575
1576/*
1577 * Cross CPU call to enable a performance event
1578 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001579static int __perf_event_enable(void *info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001580{
1581 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001582 struct perf_event_context *ctx = event->ctx;
1583 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001584 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001585 int err;
1586
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001587 if (WARN_ON_ONCE(!ctx->is_active))
1588 return -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001589
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001590 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001591 update_context_time(ctx);
1592
1593 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1594 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001595
1596 /*
1597 * set current task's cgroup time reference point
1598 */
1599 perf_cgroup_set_timestamp(current, perf_clock());
1600
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001601 __perf_event_mark_enabled(event, ctx);
1602
Stephane Eraniane5d13672011-02-14 11:20:01 +02001603 if (!event_filter_match(event)) {
1604 if (is_cgroup_event(event))
1605 perf_cgroup_defer_enabled(event);
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001606 goto unlock;
Stephane Eraniane5d13672011-02-14 11:20:01 +02001607 }
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001608
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001609 /*
1610 * If the event is in a group and isn't the group leader,
1611 * then don't put it on unless the group is on.
1612 */
1613 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1614 goto unlock;
1615
1616 if (!group_can_go_on(event, cpuctx, 1)) {
1617 err = -EEXIST;
1618 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001619 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001620 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001621 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001622 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001623 }
1624
1625 if (err) {
1626 /*
1627 * If this event can't go on and it's part of a
1628 * group, then the whole group has to come off.
1629 */
1630 if (leader != event)
1631 group_sched_out(leader, cpuctx, ctx);
1632 if (leader->attr.pinned) {
1633 update_group_times(leader);
1634 leader->state = PERF_EVENT_STATE_ERROR;
1635 }
1636 }
1637
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001638unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001639 raw_spin_unlock(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001640
1641 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001642}
1643
1644/*
1645 * Enable a event.
1646 *
1647 * If event->ctx is a cloned context, callers must make sure that
1648 * every task struct that event->ctx->task could possibly point to
1649 * remains valid. This condition is satisfied when called through
1650 * perf_event_for_each_child or perf_event_for_each as described
1651 * for perf_event_disable.
1652 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001653void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001654{
1655 struct perf_event_context *ctx = event->ctx;
1656 struct task_struct *task = ctx->task;
1657
1658 if (!task) {
1659 /*
1660 * Enable the event on the cpu that it's on
1661 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001662 cpu_function_call(event->cpu, __perf_event_enable, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001663 return;
1664 }
1665
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001666 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001667 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1668 goto out;
1669
1670 /*
1671 * If the event is in error state, clear that first.
1672 * That way, if we see the event in error state below, we
1673 * know that it has gone back into error state, as distinct
1674 * from the task having been scheduled away before the
1675 * cross-call arrived.
1676 */
1677 if (event->state == PERF_EVENT_STATE_ERROR)
1678 event->state = PERF_EVENT_STATE_OFF;
1679
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001680retry:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001681 if (!ctx->is_active) {
1682 __perf_event_mark_enabled(event, ctx);
1683 goto out;
1684 }
1685
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001686 raw_spin_unlock_irq(&ctx->lock);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001687
1688 if (!task_function_call(task, __perf_event_enable, event))
1689 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001690
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001691 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001692
1693 /*
1694 * If the context is active and the event is still off,
1695 * we need to retry the cross-call.
1696 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001697 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
1698 /*
1699 * task could have been flipped by a concurrent
1700 * perf_event_context_sched_out()
1701 */
1702 task = ctx->task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001703 goto retry;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001704 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001705
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001706out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001707 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001708}
1709
1710static int perf_event_refresh(struct perf_event *event, int refresh)
1711{
1712 /*
1713 * not supported on inherited events
1714 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01001715 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001716 return -EINVAL;
1717
1718 atomic_add(refresh, &event->event_limit);
1719 perf_event_enable(event);
1720
1721 return 0;
1722}
1723
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001724static void ctx_sched_out(struct perf_event_context *ctx,
1725 struct perf_cpu_context *cpuctx,
1726 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001727{
1728 struct perf_event *event;
1729
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001730 raw_spin_lock(&ctx->lock);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001731 perf_pmu_disable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001732 ctx->is_active = 0;
1733 if (likely(!ctx->nr_events))
1734 goto out;
1735 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001736 update_cgrp_time_from_cpuctx(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001737
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001738 if (!ctx->nr_active)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001739 goto out;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001740
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001741 if (event_type & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001742 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1743 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001744 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001745
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001746 if (event_type & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001747 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001748 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001749 }
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001750out:
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001751 perf_pmu_enable(ctx->pmu);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001752 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001753}
1754
1755/*
1756 * Test whether two contexts are equivalent, i.e. whether they
1757 * have both been cloned from the same version of the same context
1758 * and they both have the same number of enabled events.
1759 * If the number of enabled events is the same, then the set
1760 * of enabled events should be the same, because these are both
1761 * inherited contexts, therefore we can't access individual events
1762 * in them directly with an fd; we can only enable/disable all
1763 * events via prctl, or enable/disable all events in a family
1764 * via ioctl, which will have the same effect on both contexts.
1765 */
1766static int context_equiv(struct perf_event_context *ctx1,
1767 struct perf_event_context *ctx2)
1768{
1769 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1770 && ctx1->parent_gen == ctx2->parent_gen
1771 && !ctx1->pin_count && !ctx2->pin_count;
1772}
1773
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001774static void __perf_event_sync_stat(struct perf_event *event,
1775 struct perf_event *next_event)
1776{
1777 u64 value;
1778
1779 if (!event->attr.inherit_stat)
1780 return;
1781
1782 /*
1783 * Update the event value, we cannot use perf_event_read()
1784 * because we're in the middle of a context switch and have IRQs
1785 * disabled, which upsets smp_call_function_single(), however
1786 * we know the event must be on the current CPU, therefore we
1787 * don't need to use it.
1788 */
1789 switch (event->state) {
1790 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001791 event->pmu->read(event);
1792 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001793
1794 case PERF_EVENT_STATE_INACTIVE:
1795 update_event_times(event);
1796 break;
1797
1798 default:
1799 break;
1800 }
1801
1802 /*
1803 * In order to keep per-task stats reliable we need to flip the event
1804 * values when we flip the contexts.
1805 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001806 value = local64_read(&next_event->count);
1807 value = local64_xchg(&event->count, value);
1808 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001809
1810 swap(event->total_time_enabled, next_event->total_time_enabled);
1811 swap(event->total_time_running, next_event->total_time_running);
1812
1813 /*
1814 * Since we swizzled the values, update the user visible data too.
1815 */
1816 perf_event_update_userpage(event);
1817 perf_event_update_userpage(next_event);
1818}
1819
1820#define list_next_entry(pos, member) \
1821 list_entry(pos->member.next, typeof(*pos), member)
1822
1823static void perf_event_sync_stat(struct perf_event_context *ctx,
1824 struct perf_event_context *next_ctx)
1825{
1826 struct perf_event *event, *next_event;
1827
1828 if (!ctx->nr_stat)
1829 return;
1830
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001831 update_context_time(ctx);
1832
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001833 event = list_first_entry(&ctx->event_list,
1834 struct perf_event, event_entry);
1835
1836 next_event = list_first_entry(&next_ctx->event_list,
1837 struct perf_event, event_entry);
1838
1839 while (&event->event_entry != &ctx->event_list &&
1840 &next_event->event_entry != &next_ctx->event_list) {
1841
1842 __perf_event_sync_stat(event, next_event);
1843
1844 event = list_next_entry(event, event_entry);
1845 next_event = list_next_entry(next_event, event_entry);
1846 }
1847}
1848
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01001849static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1850 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001851{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001852 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001853 struct perf_event_context *next_ctx;
1854 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001855 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001856 int do_switch = 1;
1857
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001858 if (likely(!ctx))
1859 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001860
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001861 cpuctx = __get_cpu_context(ctx);
1862 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001863 return;
1864
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001865 rcu_read_lock();
1866 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001867 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001868 if (parent && next_ctx &&
1869 rcu_dereference(next_ctx->parent_ctx) == parent) {
1870 /*
1871 * Looks like the two contexts are clones, so we might be
1872 * able to optimize the context switch. We lock both
1873 * contexts and check that they are clones under the
1874 * lock (including re-checking that neither has been
1875 * uncloned in the meantime). It doesn't matter which
1876 * order we take the locks because no other cpu could
1877 * be trying to lock both of these tasks.
1878 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001879 raw_spin_lock(&ctx->lock);
1880 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001881 if (context_equiv(ctx, next_ctx)) {
1882 /*
1883 * XXX do we need a memory barrier of sorts
1884 * wrt to rcu_dereference() of perf_event_ctxp
1885 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001886 task->perf_event_ctxp[ctxn] = next_ctx;
1887 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001888 ctx->task = next;
1889 next_ctx->task = task;
1890 do_switch = 0;
1891
1892 perf_event_sync_stat(ctx, next_ctx);
1893 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001894 raw_spin_unlock(&next_ctx->lock);
1895 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001896 }
1897 rcu_read_unlock();
1898
1899 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001900 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001901 cpuctx->task_ctx = NULL;
1902 }
1903}
1904
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001905#define for_each_task_context_nr(ctxn) \
1906 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1907
1908/*
1909 * Called from scheduler to remove the events of the current task,
1910 * with interrupts disabled.
1911 *
1912 * We stop each event and update the event value in event->count.
1913 *
1914 * This does not protect us against NMI, but disable()
1915 * sets the disabled bit in the control field of event _before_
1916 * accessing the event control register. If a NMI hits, then it will
1917 * not restart the event.
1918 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001919void __perf_event_task_sched_out(struct task_struct *task,
1920 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001921{
1922 int ctxn;
1923
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001924 for_each_task_context_nr(ctxn)
1925 perf_event_context_sched_out(task, ctxn, next);
Stephane Eraniane5d13672011-02-14 11:20:01 +02001926
1927 /*
1928 * if cgroup events exist on this CPU, then we need
1929 * to check if we have to switch out PMU state.
1930 * cgroup event are system-wide mode only
1931 */
1932 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
1933 perf_cgroup_sched_out(task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001934}
1935
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001936static void task_ctx_sched_out(struct perf_event_context *ctx,
1937 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001938{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001939 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001940
1941 if (!cpuctx->task_ctx)
1942 return;
1943
1944 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1945 return;
1946
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001947 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001948 cpuctx->task_ctx = NULL;
1949}
1950
1951/*
1952 * Called with IRQs disabled
1953 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001954static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1955 enum event_type_t event_type)
1956{
1957 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001958}
1959
1960static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001961ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001962 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001963{
1964 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001965
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001966 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1967 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001968 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02001969 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001970 continue;
1971
Stephane Eraniane5d13672011-02-14 11:20:01 +02001972 /* may need to reset tstamp_enabled */
1973 if (is_cgroup_event(event))
1974 perf_cgroup_mark_enabled(event, ctx);
1975
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001976 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001977 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001978
1979 /*
1980 * If this pinned group hasn't been scheduled,
1981 * put it in error state.
1982 */
1983 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1984 update_group_times(event);
1985 event->state = PERF_EVENT_STATE_ERROR;
1986 }
1987 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001988}
1989
1990static void
1991ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001992 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001993{
1994 struct perf_event *event;
1995 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001996
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001997 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1998 /* Ignore events in OFF or ERROR state */
1999 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002000 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002001 /*
2002 * Listen to the 'cpu' scheduling filter constraint
2003 * of events:
2004 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02002005 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002006 continue;
2007
Stephane Eraniane5d13672011-02-14 11:20:01 +02002008 /* may need to reset tstamp_enabled */
2009 if (is_cgroup_event(event))
2010 perf_cgroup_mark_enabled(event, ctx);
2011
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002012 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01002013 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002014 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002015 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002016 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002017}
2018
2019static void
2020ctx_sched_in(struct perf_event_context *ctx,
2021 struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002022 enum event_type_t event_type,
2023 struct task_struct *task)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002024{
Stephane Eraniane5d13672011-02-14 11:20:01 +02002025 u64 now;
2026
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002027 raw_spin_lock(&ctx->lock);
2028 ctx->is_active = 1;
2029 if (likely(!ctx->nr_events))
2030 goto out;
2031
Stephane Eraniane5d13672011-02-14 11:20:01 +02002032 now = perf_clock();
2033 ctx->timestamp = now;
2034 perf_cgroup_set_timestamp(task, now);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002035 /*
2036 * First go through the list and put on any pinned groups
2037 * in order to give them the best chance of going on.
2038 */
2039 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002040 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002041
2042 /* Then walk through the lower prio flexible groups */
2043 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01002044 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002045
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002046out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002047 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002048}
2049
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002050static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
Stephane Eraniane5d13672011-02-14 11:20:01 +02002051 enum event_type_t event_type,
2052 struct task_struct *task)
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002053{
2054 struct perf_event_context *ctx = &cpuctx->ctx;
2055
Stephane Eraniane5d13672011-02-14 11:20:01 +02002056 ctx_sched_in(ctx, cpuctx, event_type, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002057}
2058
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002059static void task_ctx_sched_in(struct perf_event_context *ctx,
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002060 enum event_type_t event_type)
2061{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002062 struct perf_cpu_context *cpuctx;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002063
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002064 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002065 if (cpuctx->task_ctx == ctx)
2066 return;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002067
Stephane Eraniane5d13672011-02-14 11:20:01 +02002068 ctx_sched_in(ctx, cpuctx, event_type, NULL);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01002069 cpuctx->task_ctx = ctx;
2070}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002071
Stephane Eraniane5d13672011-02-14 11:20:01 +02002072static void perf_event_context_sched_in(struct perf_event_context *ctx,
2073 struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002074{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002075 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002076
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002077 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002078 if (cpuctx->task_ctx == ctx)
2079 return;
2080
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002081 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002082 /*
2083 * We want to keep the following priority order:
2084 * cpu pinned (that don't need to move), task pinned,
2085 * cpu flexible, task flexible.
2086 */
2087 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
2088
Stephane Eraniane5d13672011-02-14 11:20:01 +02002089 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2090 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2091 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01002092
2093 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08002094
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002095 /*
2096 * Since these rotations are per-cpu, we need to ensure the
2097 * cpu-context we got scheduled on is actually rotating.
2098 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002099 perf_pmu_rotate_start(ctx->pmu);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002100 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002101}
2102
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002103/*
2104 * Called from scheduler to add the events of the current task
2105 * with interrupts disabled.
2106 *
2107 * We restore the event value and then enable it.
2108 *
2109 * This does not protect us against NMI, but enable()
2110 * sets the enabled bit in the control field of event _before_
2111 * accessing the event control register. If a NMI hits, then it will
2112 * keep the event running.
2113 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002114void __perf_event_task_sched_in(struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002115{
2116 struct perf_event_context *ctx;
2117 int ctxn;
2118
2119 for_each_task_context_nr(ctxn) {
2120 ctx = task->perf_event_ctxp[ctxn];
2121 if (likely(!ctx))
2122 continue;
2123
Stephane Eraniane5d13672011-02-14 11:20:01 +02002124 perf_event_context_sched_in(ctx, task);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002125 }
Stephane Eraniane5d13672011-02-14 11:20:01 +02002126 /*
2127 * if cgroup events exist on this CPU, then we need
2128 * to check if we have to switch in PMU state.
2129 * cgroup event are system-wide mode only
2130 */
2131 if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
2132 perf_cgroup_sched_in(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002133}
2134
Peter Zijlstraabd50712010-01-26 18:50:16 +01002135static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
2136{
2137 u64 frequency = event->attr.sample_freq;
2138 u64 sec = NSEC_PER_SEC;
2139 u64 divisor, dividend;
2140
2141 int count_fls, nsec_fls, frequency_fls, sec_fls;
2142
2143 count_fls = fls64(count);
2144 nsec_fls = fls64(nsec);
2145 frequency_fls = fls64(frequency);
2146 sec_fls = 30;
2147
2148 /*
2149 * We got @count in @nsec, with a target of sample_freq HZ
2150 * the target period becomes:
2151 *
2152 * @count * 10^9
2153 * period = -------------------
2154 * @nsec * sample_freq
2155 *
2156 */
2157
2158 /*
2159 * Reduce accuracy by one bit such that @a and @b converge
2160 * to a similar magnitude.
2161 */
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002162#define REDUCE_FLS(a, b) \
Peter Zijlstraabd50712010-01-26 18:50:16 +01002163do { \
2164 if (a##_fls > b##_fls) { \
2165 a >>= 1; \
2166 a##_fls--; \
2167 } else { \
2168 b >>= 1; \
2169 b##_fls--; \
2170 } \
2171} while (0)
2172
2173 /*
2174 * Reduce accuracy until either term fits in a u64, then proceed with
2175 * the other, so that finally we can do a u64/u64 division.
2176 */
2177 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
2178 REDUCE_FLS(nsec, frequency);
2179 REDUCE_FLS(sec, count);
2180 }
2181
2182 if (count_fls + sec_fls > 64) {
2183 divisor = nsec * frequency;
2184
2185 while (count_fls + sec_fls > 64) {
2186 REDUCE_FLS(count, sec);
2187 divisor >>= 1;
2188 }
2189
2190 dividend = count * sec;
2191 } else {
2192 dividend = count * sec;
2193
2194 while (nsec_fls + frequency_fls > 64) {
2195 REDUCE_FLS(nsec, frequency);
2196 dividend >>= 1;
2197 }
2198
2199 divisor = nsec * frequency;
2200 }
2201
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002202 if (!divisor)
2203 return dividend;
2204
Peter Zijlstraabd50712010-01-26 18:50:16 +01002205 return div64_u64(dividend, divisor);
2206}
2207
2208static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002209{
2210 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02002211 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002212 s64 delta;
2213
Peter Zijlstraabd50712010-01-26 18:50:16 +01002214 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002215
2216 delta = (s64)(period - hwc->sample_period);
2217 delta = (delta + 7) / 8; /* low pass filter */
2218
2219 sample_period = hwc->sample_period + delta;
2220
2221 if (!sample_period)
2222 sample_period = 1;
2223
2224 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002225
Peter Zijlstrae7850592010-05-21 14:43:08 +02002226 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002227 event->pmu->stop(event, PERF_EF_UPDATE);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002228 local64_set(&hwc->period_left, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002229 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002230 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002231}
2232
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002233static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002234{
2235 struct perf_event *event;
2236 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01002237 u64 interrupts, now;
2238 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002239
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002240 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11002241 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002242 if (event->state != PERF_EVENT_STATE_ACTIVE)
2243 continue;
2244
Stephane Eranian5632ab12011-01-03 18:20:01 +02002245 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01002246 continue;
2247
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002248 hwc = &event->hw;
2249
2250 interrupts = hwc->interrupts;
2251 hwc->interrupts = 0;
2252
2253 /*
2254 * unthrottle events on the tick
2255 */
2256 if (interrupts == MAX_INTERRUPTS) {
2257 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002258 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002259 }
2260
2261 if (!event->attr.freq || !event->attr.sample_freq)
2262 continue;
2263
Peter Zijlstraabd50712010-01-26 18:50:16 +01002264 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002265 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01002266 delta = now - hwc->freq_count_stamp;
2267 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002268
Peter Zijlstraabd50712010-01-26 18:50:16 +01002269 if (delta > 0)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002270 perf_adjust_period(event, period, delta);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002271 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002272 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002273}
2274
2275/*
2276 * Round-robin a context's events:
2277 */
2278static void rotate_ctx(struct perf_event_context *ctx)
2279{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002280 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002281
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01002282 /*
2283 * Rotate the first entry last of non-pinned groups. Rotation might be
2284 * disabled by the inheritance code.
2285 */
2286 if (!ctx->rotate_disable)
2287 list_rotate_left(&ctx->flexible_groups);
Frederic Weisbeckere2864172010-01-09 21:05:28 +01002288
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002289 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002290}
2291
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002292/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002293 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
2294 * because they're strictly cpu affine and rotate_start is called with IRQs
2295 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002296 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002297static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002298{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002299 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002300 struct perf_event_context *ctx = NULL;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002301 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002302
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002303 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002304 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002305 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
2306 rotate = 1;
2307 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002308
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002309 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002310 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002311 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002312 if (ctx->nr_events != ctx->nr_active)
2313 rotate = 1;
2314 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002315
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02002316 perf_pmu_disable(cpuctx->ctx.pmu);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002317 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002318 if (ctx)
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002319 perf_ctx_adjust_freq(ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002320
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002321 if (!rotate)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002322 goto done;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01002323
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01002324 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002325 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01002326 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002327
2328 rotate_ctx(&cpuctx->ctx);
2329 if (ctx)
2330 rotate_ctx(ctx);
2331
Stephane Eraniane5d13672011-02-14 11:20:01 +02002332 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, current);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002333 if (ctx)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002334 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002335
2336done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002337 if (remove)
2338 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02002339
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02002340 perf_pmu_enable(cpuctx->ctx.pmu);
2341}
2342
2343void perf_event_task_tick(void)
2344{
2345 struct list_head *head = &__get_cpu_var(rotation_list);
2346 struct perf_cpu_context *cpuctx, *tmp;
2347
2348 WARN_ON(!irqs_disabled());
2349
2350 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
2351 if (cpuctx->jiffies_interval == 1 ||
2352 !(jiffies % cpuctx->jiffies_interval))
2353 perf_rotate_context(cpuctx);
2354 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002355}
2356
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002357static int event_enable_on_exec(struct perf_event *event,
2358 struct perf_event_context *ctx)
2359{
2360 if (!event->attr.enable_on_exec)
2361 return 0;
2362
2363 event->attr.enable_on_exec = 0;
2364 if (event->state >= PERF_EVENT_STATE_INACTIVE)
2365 return 0;
2366
2367 __perf_event_mark_enabled(event, ctx);
2368
2369 return 1;
2370}
2371
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002372/*
2373 * Enable all of a task's events that have been marked enable-on-exec.
2374 * This expects task == current.
2375 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002376static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002377{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002378 struct perf_event *event;
2379 unsigned long flags;
2380 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002381 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002382
2383 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002384 if (!ctx || !ctx->nr_events)
2385 goto out;
2386
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002387 task_ctx_sched_out(ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002388
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002389 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002390
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002391 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2392 ret = event_enable_on_exec(event, ctx);
2393 if (ret)
2394 enabled = 1;
2395 }
2396
2397 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2398 ret = event_enable_on_exec(event, ctx);
2399 if (ret)
2400 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002401 }
2402
2403 /*
2404 * Unclone this context if we enabled any event.
2405 */
2406 if (enabled)
2407 unclone_ctx(ctx);
2408
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002409 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002410
Stephane Eraniane5d13672011-02-14 11:20:01 +02002411 perf_event_context_sched_in(ctx, ctx->task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002412out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002413 local_irq_restore(flags);
2414}
2415
2416/*
2417 * Cross CPU call to read the hardware event
2418 */
2419static void __perf_event_read(void *info)
2420{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002421 struct perf_event *event = info;
2422 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002423 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002424
2425 /*
2426 * If this is a task context, we need to check whether it is
2427 * the current task context of this cpu. If not it has been
2428 * scheduled out before the smp call arrived. In that case
2429 * event->count would have been updated to a recent sample
2430 * when the event was scheduled out.
2431 */
2432 if (ctx->task && cpuctx->task_ctx != ctx)
2433 return;
2434
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002435 raw_spin_lock(&ctx->lock);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002436 if (ctx->is_active) {
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002437 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002438 update_cgrp_time_from_event(event);
2439 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002440 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01002441 if (event->state == PERF_EVENT_STATE_ACTIVE)
2442 event->pmu->read(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002443 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002444}
2445
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002446static inline u64 perf_event_count(struct perf_event *event)
2447{
Peter Zijlstrae7850592010-05-21 14:43:08 +02002448 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002449}
2450
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002451static u64 perf_event_read(struct perf_event *event)
2452{
2453 /*
2454 * If event is enabled and currently active on a CPU, update the
2455 * value in the event structure:
2456 */
2457 if (event->state == PERF_EVENT_STATE_ACTIVE) {
2458 smp_call_function_single(event->oncpu,
2459 __perf_event_read, event, 1);
2460 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01002461 struct perf_event_context *ctx = event->ctx;
2462 unsigned long flags;
2463
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002464 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002465 /*
2466 * may read while context is not active
2467 * (e.g., thread is blocked), in that case
2468 * we cannot update context time
2469 */
Stephane Eraniane5d13672011-02-14 11:20:01 +02002470 if (ctx->is_active) {
Stephane Eranianc530ccd2010-10-15 15:26:01 +02002471 update_context_time(ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02002472 update_cgrp_time_from_event(event);
2473 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002474 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002475 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002476 }
2477
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002478 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002479}
2480
2481/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002482 * Callchain support
2483 */
2484
2485struct callchain_cpus_entries {
2486 struct rcu_head rcu_head;
2487 struct perf_callchain_entry *cpu_entries[0];
2488};
2489
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02002490static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002491static atomic_t nr_callchain_events;
2492static DEFINE_MUTEX(callchain_mutex);
2493struct callchain_cpus_entries *callchain_cpus_entries;
2494
2495
2496__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
2497 struct pt_regs *regs)
2498{
2499}
2500
2501__weak void perf_callchain_user(struct perf_callchain_entry *entry,
2502 struct pt_regs *regs)
2503{
2504}
2505
2506static void release_callchain_buffers_rcu(struct rcu_head *head)
2507{
2508 struct callchain_cpus_entries *entries;
2509 int cpu;
2510
2511 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
2512
2513 for_each_possible_cpu(cpu)
2514 kfree(entries->cpu_entries[cpu]);
2515
2516 kfree(entries);
2517}
2518
2519static void release_callchain_buffers(void)
2520{
2521 struct callchain_cpus_entries *entries;
2522
2523 entries = callchain_cpus_entries;
2524 rcu_assign_pointer(callchain_cpus_entries, NULL);
2525 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2526}
2527
2528static int alloc_callchain_buffers(void)
2529{
2530 int cpu;
2531 int size;
2532 struct callchain_cpus_entries *entries;
2533
2534 /*
2535 * We can't use the percpu allocation API for data that can be
2536 * accessed from NMI. Use a temporary manual per cpu allocation
2537 * until that gets sorted out.
2538 */
Eric Dumazet88d4f0d2011-01-25 19:40:51 +01002539 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002540
2541 entries = kzalloc(size, GFP_KERNEL);
2542 if (!entries)
2543 return -ENOMEM;
2544
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02002545 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002546
2547 for_each_possible_cpu(cpu) {
2548 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2549 cpu_to_node(cpu));
2550 if (!entries->cpu_entries[cpu])
2551 goto fail;
2552 }
2553
2554 rcu_assign_pointer(callchain_cpus_entries, entries);
2555
2556 return 0;
2557
2558fail:
2559 for_each_possible_cpu(cpu)
2560 kfree(entries->cpu_entries[cpu]);
2561 kfree(entries);
2562
2563 return -ENOMEM;
2564}
2565
2566static int get_callchain_buffers(void)
2567{
2568 int err = 0;
2569 int count;
2570
2571 mutex_lock(&callchain_mutex);
2572
2573 count = atomic_inc_return(&nr_callchain_events);
2574 if (WARN_ON_ONCE(count < 1)) {
2575 err = -EINVAL;
2576 goto exit;
2577 }
2578
2579 if (count > 1) {
2580 /* If the allocation failed, give up */
2581 if (!callchain_cpus_entries)
2582 err = -ENOMEM;
2583 goto exit;
2584 }
2585
2586 err = alloc_callchain_buffers();
2587 if (err)
2588 release_callchain_buffers();
2589exit:
2590 mutex_unlock(&callchain_mutex);
2591
2592 return err;
2593}
2594
2595static void put_callchain_buffers(void)
2596{
2597 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2598 release_callchain_buffers();
2599 mutex_unlock(&callchain_mutex);
2600 }
2601}
2602
2603static int get_recursion_context(int *recursion)
2604{
2605 int rctx;
2606
2607 if (in_nmi())
2608 rctx = 3;
2609 else if (in_irq())
2610 rctx = 2;
2611 else if (in_softirq())
2612 rctx = 1;
2613 else
2614 rctx = 0;
2615
2616 if (recursion[rctx])
2617 return -1;
2618
2619 recursion[rctx]++;
2620 barrier();
2621
2622 return rctx;
2623}
2624
2625static inline void put_recursion_context(int *recursion, int rctx)
2626{
2627 barrier();
2628 recursion[rctx]--;
2629}
2630
2631static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2632{
2633 int cpu;
2634 struct callchain_cpus_entries *entries;
2635
2636 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2637 if (*rctx == -1)
2638 return NULL;
2639
2640 entries = rcu_dereference(callchain_cpus_entries);
2641 if (!entries)
2642 return NULL;
2643
2644 cpu = smp_processor_id();
2645
2646 return &entries->cpu_entries[cpu][*rctx];
2647}
2648
2649static void
2650put_callchain_entry(int rctx)
2651{
2652 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2653}
2654
2655static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2656{
2657 int rctx;
2658 struct perf_callchain_entry *entry;
2659
2660
2661 entry = get_callchain_entry(&rctx);
2662 if (rctx == -1)
2663 return NULL;
2664
2665 if (!entry)
2666 goto exit_put;
2667
2668 entry->nr = 0;
2669
2670 if (!user_mode(regs)) {
2671 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2672 perf_callchain_kernel(entry, regs);
2673 if (current->mm)
2674 regs = task_pt_regs(current);
2675 else
2676 regs = NULL;
2677 }
2678
2679 if (regs) {
2680 perf_callchain_store(entry, PERF_CONTEXT_USER);
2681 perf_callchain_user(entry, regs);
2682 }
2683
2684exit_put:
2685 put_callchain_entry(rctx);
2686
2687 return entry;
2688}
2689
2690/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002691 * Initialize the perf_event context in a task_struct:
2692 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002693static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002694{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002695 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002696 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002697 INIT_LIST_HEAD(&ctx->pinned_groups);
2698 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002699 INIT_LIST_HEAD(&ctx->event_list);
2700 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002701}
2702
Peter Zijlstraeb184472010-09-07 15:55:13 +02002703static struct perf_event_context *
2704alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002705{
2706 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002707
2708 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2709 if (!ctx)
2710 return NULL;
2711
2712 __perf_event_init_context(ctx);
2713 if (task) {
2714 ctx->task = task;
2715 get_task_struct(task);
2716 }
2717 ctx->pmu = pmu;
2718
2719 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002720}
2721
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002722static struct task_struct *
2723find_lively_task_by_vpid(pid_t vpid)
2724{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002725 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002726 int err;
2727
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002728 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002729 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002730 task = current;
2731 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002732 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002733 if (task)
2734 get_task_struct(task);
2735 rcu_read_unlock();
2736
2737 if (!task)
2738 return ERR_PTR(-ESRCH);
2739
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002740 /* Reuse ptrace permission checks for now. */
2741 err = -EACCES;
2742 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2743 goto errout;
2744
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002745 return task;
2746errout:
2747 put_task_struct(task);
2748 return ERR_PTR(err);
2749
2750}
2751
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002752/*
2753 * Returns a matching context with refcount and pincount.
2754 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002755static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002756find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002757{
2758 struct perf_event_context *ctx;
2759 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002760 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002761 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002762
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01002763 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002764 /* Must be root to operate on a CPU event: */
2765 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2766 return ERR_PTR(-EACCES);
2767
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002768 /*
2769 * We could be clever and allow to attach a event to an
2770 * offline CPU and activate it when the CPU comes up, but
2771 * that's for later.
2772 */
2773 if (!cpu_online(cpu))
2774 return ERR_PTR(-ENODEV);
2775
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002776 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002777 ctx = &cpuctx->ctx;
2778 get_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002779 ++ctx->pin_count;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002780
2781 return ctx;
2782 }
2783
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002784 err = -EINVAL;
2785 ctxn = pmu->task_ctx_nr;
2786 if (ctxn < 0)
2787 goto errout;
2788
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002789retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002790 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002791 if (ctx) {
2792 unclone_ctx(ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002793 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002794 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002795 }
2796
2797 if (!ctx) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002798 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002799 err = -ENOMEM;
2800 if (!ctx)
2801 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002802
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002803 get_ctx(ctx);
Peter Zijlstraeb184472010-09-07 15:55:13 +02002804
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002805 err = 0;
2806 mutex_lock(&task->perf_event_mutex);
2807 /*
2808 * If it has already passed perf_event_exit_task().
2809 * we must see PF_EXITING, it takes this mutex too.
2810 */
2811 if (task->flags & PF_EXITING)
2812 err = -ESRCH;
2813 else if (task->perf_event_ctxp[ctxn])
2814 err = -EAGAIN;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002815 else {
2816 ++ctx->pin_count;
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002817 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01002818 }
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002819 mutex_unlock(&task->perf_event_mutex);
2820
2821 if (unlikely(err)) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002822 put_task_struct(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002823 kfree(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002824
2825 if (err == -EAGAIN)
2826 goto retry;
2827 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002828 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002829 }
2830
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002831 return ctx;
2832
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002833errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002834 return ERR_PTR(err);
2835}
2836
Li Zefan6fb29152009-10-15 11:21:42 +08002837static void perf_event_free_filter(struct perf_event *event);
2838
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002839static void free_event_rcu(struct rcu_head *head)
2840{
2841 struct perf_event *event;
2842
2843 event = container_of(head, struct perf_event, rcu_head);
2844 if (event->ns)
2845 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002846 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002847 kfree(event);
2848}
2849
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002850static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002851
2852static void free_event(struct perf_event *event)
2853{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08002854 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002855
2856 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002857 if (event->attach_state & PERF_ATTACH_TASK)
Stephane Eraniane5d13672011-02-14 11:20:01 +02002858 jump_label_dec(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002859 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002860 atomic_dec(&nr_mmap_events);
2861 if (event->attr.comm)
2862 atomic_dec(&nr_comm_events);
2863 if (event->attr.task)
2864 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002865 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2866 put_callchain_buffers();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002867 }
2868
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002869 if (event->buffer) {
2870 perf_buffer_put(event->buffer);
2871 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002872 }
2873
Stephane Eraniane5d13672011-02-14 11:20:01 +02002874 if (is_cgroup_event(event))
2875 perf_detach_cgroup(event);
2876
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002877 if (event->destroy)
2878 event->destroy(event);
2879
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002880 if (event->ctx)
2881 put_ctx(event->ctx);
2882
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002883 call_rcu(&event->rcu_head, free_event_rcu);
2884}
2885
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002886int perf_event_release_kernel(struct perf_event *event)
2887{
2888 struct perf_event_context *ctx = event->ctx;
2889
Peter Zijlstra050735b2010-05-11 11:51:53 +02002890 /*
2891 * Remove from the PMU, can't get re-enabled since we got
2892 * here because the last ref went.
2893 */
2894 perf_event_disable(event);
2895
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002896 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002897 /*
2898 * There are two ways this annotation is useful:
2899 *
2900 * 1) there is a lock recursion from perf_event_exit_task
2901 * see the comment there.
2902 *
2903 * 2) there is a lock-inversion with mmap_sem through
2904 * perf_event_read_group(), which takes faults while
2905 * holding ctx->mutex, however this is called after
2906 * the last filedesc died, so there is no possibility
2907 * to trigger the AB-BA case.
2908 */
2909 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002910 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002911 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002912 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002913 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002914 mutex_unlock(&ctx->mutex);
2915
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002916 free_event(event);
2917
2918 return 0;
2919}
2920EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2921
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002922/*
2923 * Called when the last reference to the file is gone.
2924 */
2925static int perf_release(struct inode *inode, struct file *file)
2926{
2927 struct perf_event *event = file->private_data;
Peter Zijlstra88821352010-11-09 19:01:43 +01002928 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002929
2930 file->private_data = NULL;
2931
Peter Zijlstra88821352010-11-09 19:01:43 +01002932 rcu_read_lock();
2933 owner = ACCESS_ONCE(event->owner);
2934 /*
2935 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2936 * !owner it means the list deletion is complete and we can indeed
2937 * free this event, otherwise we need to serialize on
2938 * owner->perf_event_mutex.
2939 */
2940 smp_read_barrier_depends();
2941 if (owner) {
2942 /*
2943 * Since delayed_put_task_struct() also drops the last
2944 * task reference we can safely take a new reference
2945 * while holding the rcu_read_lock().
2946 */
2947 get_task_struct(owner);
2948 }
2949 rcu_read_unlock();
2950
2951 if (owner) {
2952 mutex_lock(&owner->perf_event_mutex);
2953 /*
2954 * We have to re-check the event->owner field, if it is cleared
2955 * we raced with perf_event_exit_task(), acquiring the mutex
2956 * ensured they're done, and we can proceed with freeing the
2957 * event.
2958 */
2959 if (event->owner)
2960 list_del_init(&event->owner_entry);
2961 mutex_unlock(&owner->perf_event_mutex);
2962 put_task_struct(owner);
2963 }
2964
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002965 return perf_event_release_kernel(event);
2966}
2967
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002968u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002969{
2970 struct perf_event *child;
2971 u64 total = 0;
2972
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002973 *enabled = 0;
2974 *running = 0;
2975
Peter Zijlstra6f105812009-11-20 22:19:56 +01002976 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002977 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002978 *enabled += event->total_time_enabled +
2979 atomic64_read(&event->child_total_time_enabled);
2980 *running += event->total_time_running +
2981 atomic64_read(&event->child_total_time_running);
2982
2983 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002984 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002985 *enabled += child->total_time_enabled;
2986 *running += child->total_time_running;
2987 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002988 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002989
2990 return total;
2991}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002992EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002993
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002994static int perf_event_read_group(struct perf_event *event,
2995 u64 read_format, char __user *buf)
2996{
2997 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01002998 int n = 0, size = 0, ret = -EFAULT;
2999 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003000 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003001 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003002
Peter Zijlstra6f105812009-11-20 22:19:56 +01003003 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003004 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003005
3006 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003007 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3008 values[n++] = enabled;
3009 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3010 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01003011 values[n++] = count;
3012 if (read_format & PERF_FORMAT_ID)
3013 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003014
3015 size = n * sizeof(u64);
3016
3017 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01003018 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003019
Peter Zijlstra6f105812009-11-20 22:19:56 +01003020 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003021
3022 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01003023 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003024
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003025 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01003026 if (read_format & PERF_FORMAT_ID)
3027 values[n++] = primary_event_id(sub);
3028
3029 size = n * sizeof(u64);
3030
Stephane Eranian184d3da2009-11-23 21:40:49 -08003031 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01003032 ret = -EFAULT;
3033 goto unlock;
3034 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01003035
3036 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003037 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01003038unlock:
3039 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003040
Peter Zijlstraabf48682009-11-20 22:19:49 +01003041 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003042}
3043
3044static int perf_event_read_one(struct perf_event *event,
3045 u64 read_format, char __user *buf)
3046{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003047 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003048 u64 values[4];
3049 int n = 0;
3050
Peter Zijlstra59ed4462009-11-20 22:19:55 +01003051 values[n++] = perf_event_read_value(event, &enabled, &running);
3052 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3053 values[n++] = enabled;
3054 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3055 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003056 if (read_format & PERF_FORMAT_ID)
3057 values[n++] = primary_event_id(event);
3058
3059 if (copy_to_user(buf, values, n * sizeof(u64)))
3060 return -EFAULT;
3061
3062 return n * sizeof(u64);
3063}
3064
3065/*
3066 * Read the performance event - simple non blocking version for now
3067 */
3068static ssize_t
3069perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
3070{
3071 u64 read_format = event->attr.read_format;
3072 int ret;
3073
3074 /*
3075 * Return end-of-file for a read on a event that is in
3076 * error state (i.e. because it was pinned but it couldn't be
3077 * scheduled on to the CPU at some point).
3078 */
3079 if (event->state == PERF_EVENT_STATE_ERROR)
3080 return 0;
3081
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003082 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003083 return -ENOSPC;
3084
3085 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003086 if (read_format & PERF_FORMAT_GROUP)
3087 ret = perf_event_read_group(event, read_format, buf);
3088 else
3089 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003090
3091 return ret;
3092}
3093
3094static ssize_t
3095perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
3096{
3097 struct perf_event *event = file->private_data;
3098
3099 return perf_read_hw(event, buf, count);
3100}
3101
3102static unsigned int perf_poll(struct file *file, poll_table *wait)
3103{
3104 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003105 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003106 unsigned int events = POLL_HUP;
3107
3108 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003109 buffer = rcu_dereference(event->buffer);
3110 if (buffer)
3111 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003112 rcu_read_unlock();
3113
3114 poll_wait(file, &event->waitq, wait);
3115
3116 return events;
3117}
3118
3119static void perf_event_reset(struct perf_event *event)
3120{
3121 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02003122 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003123 perf_event_update_userpage(event);
3124}
3125
3126/*
3127 * Holding the top-level event's child_mutex means that any
3128 * descendant process that has inherited this event will block
3129 * in sync_child_event if it goes to exit, thus satisfying the
3130 * task existence requirements of perf_event_enable/disable.
3131 */
3132static void perf_event_for_each_child(struct perf_event *event,
3133 void (*func)(struct perf_event *))
3134{
3135 struct perf_event *child;
3136
3137 WARN_ON_ONCE(event->ctx->parent_ctx);
3138 mutex_lock(&event->child_mutex);
3139 func(event);
3140 list_for_each_entry(child, &event->child_list, child_list)
3141 func(child);
3142 mutex_unlock(&event->child_mutex);
3143}
3144
3145static void perf_event_for_each(struct perf_event *event,
3146 void (*func)(struct perf_event *))
3147{
3148 struct perf_event_context *ctx = event->ctx;
3149 struct perf_event *sibling;
3150
3151 WARN_ON_ONCE(ctx->parent_ctx);
3152 mutex_lock(&ctx->mutex);
3153 event = event->group_leader;
3154
3155 perf_event_for_each_child(event, func);
3156 func(event);
3157 list_for_each_entry(sibling, &event->sibling_list, group_entry)
3158 perf_event_for_each_child(event, func);
3159 mutex_unlock(&ctx->mutex);
3160}
3161
3162static int perf_event_period(struct perf_event *event, u64 __user *arg)
3163{
3164 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003165 int ret = 0;
3166 u64 value;
3167
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01003168 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003169 return -EINVAL;
3170
John Blackwoodad0cf342010-09-28 18:03:11 -04003171 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003172 return -EFAULT;
3173
3174 if (!value)
3175 return -EINVAL;
3176
Thomas Gleixnere625cce2009-11-17 18:02:06 +01003177 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003178 if (event->attr.freq) {
3179 if (value > sysctl_perf_event_sample_rate) {
3180 ret = -EINVAL;
3181 goto unlock;
3182 }
3183
3184 event->attr.sample_freq = value;
3185 } else {
3186 event->attr.sample_period = value;
3187 event->hw.sample_period = value;
3188 }
3189unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01003190 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003191
3192 return ret;
3193}
3194
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003195static const struct file_operations perf_fops;
3196
3197static struct perf_event *perf_fget_light(int fd, int *fput_needed)
3198{
3199 struct file *file;
3200
3201 file = fget_light(fd, fput_needed);
3202 if (!file)
3203 return ERR_PTR(-EBADF);
3204
3205 if (file->f_op != &perf_fops) {
3206 fput_light(file, *fput_needed);
3207 *fput_needed = 0;
3208 return ERR_PTR(-EBADF);
3209 }
3210
3211 return file->private_data;
3212}
3213
3214static int perf_event_set_output(struct perf_event *event,
3215 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08003216static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003217
3218static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3219{
3220 struct perf_event *event = file->private_data;
3221 void (*func)(struct perf_event *);
3222 u32 flags = arg;
3223
3224 switch (cmd) {
3225 case PERF_EVENT_IOC_ENABLE:
3226 func = perf_event_enable;
3227 break;
3228 case PERF_EVENT_IOC_DISABLE:
3229 func = perf_event_disable;
3230 break;
3231 case PERF_EVENT_IOC_RESET:
3232 func = perf_event_reset;
3233 break;
3234
3235 case PERF_EVENT_IOC_REFRESH:
3236 return perf_event_refresh(event, arg);
3237
3238 case PERF_EVENT_IOC_PERIOD:
3239 return perf_event_period(event, (u64 __user *)arg);
3240
3241 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003242 {
3243 struct perf_event *output_event = NULL;
3244 int fput_needed = 0;
3245 int ret;
3246
3247 if (arg != -1) {
3248 output_event = perf_fget_light(arg, &fput_needed);
3249 if (IS_ERR(output_event))
3250 return PTR_ERR(output_event);
3251 }
3252
3253 ret = perf_event_set_output(event, output_event);
3254 if (output_event)
3255 fput_light(output_event->filp, fput_needed);
3256
3257 return ret;
3258 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003259
Li Zefan6fb29152009-10-15 11:21:42 +08003260 case PERF_EVENT_IOC_SET_FILTER:
3261 return perf_event_set_filter(event, (void __user *)arg);
3262
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003263 default:
3264 return -ENOTTY;
3265 }
3266
3267 if (flags & PERF_IOC_FLAG_GROUP)
3268 perf_event_for_each(event, func);
3269 else
3270 perf_event_for_each_child(event, func);
3271
3272 return 0;
3273}
3274
3275int perf_event_task_enable(void)
3276{
3277 struct perf_event *event;
3278
3279 mutex_lock(&current->perf_event_mutex);
3280 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3281 perf_event_for_each_child(event, perf_event_enable);
3282 mutex_unlock(&current->perf_event_mutex);
3283
3284 return 0;
3285}
3286
3287int perf_event_task_disable(void)
3288{
3289 struct perf_event *event;
3290
3291 mutex_lock(&current->perf_event_mutex);
3292 list_for_each_entry(event, &current->perf_event_list, owner_entry)
3293 perf_event_for_each_child(event, perf_event_disable);
3294 mutex_unlock(&current->perf_event_mutex);
3295
3296 return 0;
3297}
3298
3299#ifndef PERF_EVENT_INDEX_OFFSET
3300# define PERF_EVENT_INDEX_OFFSET 0
3301#endif
3302
3303static int perf_event_index(struct perf_event *event)
3304{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02003305 if (event->hw.state & PERF_HES_STOPPED)
3306 return 0;
3307
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003308 if (event->state != PERF_EVENT_STATE_ACTIVE)
3309 return 0;
3310
3311 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
3312}
3313
3314/*
3315 * Callers need to ensure there can be no nesting of this function, otherwise
3316 * the seqlock logic goes bad. We can not serialize this because the arch
3317 * code calls this from NMI context.
3318 */
3319void perf_event_update_userpage(struct perf_event *event)
3320{
3321 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003322 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003323
3324 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003325 buffer = rcu_dereference(event->buffer);
3326 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003327 goto unlock;
3328
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003329 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003330
3331 /*
3332 * Disable preemption so as to not let the corresponding user-space
3333 * spin too long if we get preempted.
3334 */
3335 preempt_disable();
3336 ++userpg->lock;
3337 barrier();
3338 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003339 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003340 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02003341 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003342
3343 userpg->time_enabled = event->total_time_enabled +
3344 atomic64_read(&event->child_total_time_enabled);
3345
3346 userpg->time_running = event->total_time_running +
3347 atomic64_read(&event->child_total_time_running);
3348
3349 barrier();
3350 ++userpg->lock;
3351 preempt_enable();
3352unlock:
3353 rcu_read_unlock();
3354}
3355
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003356static unsigned long perf_data_size(struct perf_buffer *buffer);
3357
3358static void
3359perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
3360{
3361 long max_size = perf_data_size(buffer);
3362
3363 if (watermark)
3364 buffer->watermark = min(max_size, watermark);
3365
3366 if (!buffer->watermark)
3367 buffer->watermark = max_size / 2;
3368
3369 if (flags & PERF_BUFFER_WRITABLE)
3370 buffer->writable = 1;
3371
3372 atomic_set(&buffer->refcount, 1);
3373}
3374
Peter Zijlstra906010b2009-09-21 16:08:49 +02003375#ifndef CONFIG_PERF_USE_VMALLOC
3376
3377/*
3378 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
3379 */
3380
3381static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003382perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003383{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003384 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003385 return NULL;
3386
3387 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003388 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003389
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003390 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003391}
3392
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02003393static void *perf_mmap_alloc_page(int cpu)
3394{
3395 struct page *page;
3396 int node;
3397
3398 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
3399 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
3400 if (!page)
3401 return NULL;
3402
3403 return page_address(page);
3404}
3405
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003406static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003407perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003408{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003409 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003410 unsigned long size;
3411 int i;
3412
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003413 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003414 size += nr_pages * sizeof(void *);
3415
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003416 buffer = kzalloc(size, GFP_KERNEL);
3417 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003418 goto fail;
3419
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003420 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003421 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003422 goto fail_user_page;
3423
3424 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003425 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003426 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003427 goto fail_data_pages;
3428 }
3429
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003430 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003431
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003432 perf_buffer_init(buffer, watermark, flags);
3433
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003434 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003435
3436fail_data_pages:
3437 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003438 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003439
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003440 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003441
3442fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003443 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003444
3445fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02003446 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003447}
3448
3449static void perf_mmap_free_page(unsigned long addr)
3450{
3451 struct page *page = virt_to_page((void *)addr);
3452
3453 page->mapping = NULL;
3454 __free_page(page);
3455}
3456
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003457static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003458{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003459 int i;
3460
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003461 perf_mmap_free_page((unsigned long)buffer->user_page);
3462 for (i = 0; i < buffer->nr_pages; i++)
3463 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
3464 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003465}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003466
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003467static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003468{
3469 return 0;
3470}
3471
Peter Zijlstra906010b2009-09-21 16:08:49 +02003472#else
3473
3474/*
3475 * Back perf_mmap() with vmalloc memory.
3476 *
3477 * Required for architectures that have d-cache aliasing issues.
3478 */
3479
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003480static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003481{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003482 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003483}
3484
Peter Zijlstra906010b2009-09-21 16:08:49 +02003485static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003486perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003487{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003488 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02003489 return NULL;
3490
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003491 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003492}
3493
3494static void perf_mmap_unmark_page(void *addr)
3495{
3496 struct page *page = vmalloc_to_page(addr);
3497
3498 page->mapping = NULL;
3499}
3500
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003501static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003502{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003503 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003504 void *base;
3505 int i, nr;
3506
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003507 buffer = container_of(work, struct perf_buffer, work);
3508 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003509
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003510 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003511 for (i = 0; i < nr + 1; i++)
3512 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
3513
3514 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003515 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003516}
3517
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003518static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003519{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003520 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003521}
3522
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003523static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003524perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003525{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003526 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003527 unsigned long size;
3528 void *all_buf;
3529
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003530 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003531 size += sizeof(void *);
3532
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003533 buffer = kzalloc(size, GFP_KERNEL);
3534 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003535 goto fail;
3536
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003537 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003538
3539 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
3540 if (!all_buf)
3541 goto fail_all_buf;
3542
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003543 buffer->user_page = all_buf;
3544 buffer->data_pages[0] = all_buf + PAGE_SIZE;
3545 buffer->page_order = ilog2(nr_pages);
3546 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003547
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003548 perf_buffer_init(buffer, watermark, flags);
3549
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003550 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003551
3552fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003553 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003554
3555fail:
3556 return NULL;
3557}
3558
3559#endif
3560
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003561static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003562{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003563 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003564}
3565
Peter Zijlstra906010b2009-09-21 16:08:49 +02003566static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3567{
3568 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003569 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003570 int ret = VM_FAULT_SIGBUS;
3571
3572 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3573 if (vmf->pgoff == 0)
3574 ret = 0;
3575 return ret;
3576 }
3577
3578 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003579 buffer = rcu_dereference(event->buffer);
3580 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003581 goto unlock;
3582
3583 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3584 goto unlock;
3585
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003586 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003587 if (!vmf->page)
3588 goto unlock;
3589
3590 get_page(vmf->page);
3591 vmf->page->mapping = vma->vm_file->f_mapping;
3592 vmf->page->index = vmf->pgoff;
3593
3594 ret = 0;
3595unlock:
3596 rcu_read_unlock();
3597
3598 return ret;
3599}
3600
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003601static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003602{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003603 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003604
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003605 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3606 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003607}
3608
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003609static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003610{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003611 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003612
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003613 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003614 buffer = rcu_dereference(event->buffer);
3615 if (buffer) {
3616 if (!atomic_inc_not_zero(&buffer->refcount))
3617 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003618 }
3619 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003620
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003621 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003622}
3623
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003624static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003625{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003626 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003627 return;
3628
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003629 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003630}
3631
3632static void perf_mmap_open(struct vm_area_struct *vma)
3633{
3634 struct perf_event *event = vma->vm_file->private_data;
3635
3636 atomic_inc(&event->mmap_count);
3637}
3638
3639static void perf_mmap_close(struct vm_area_struct *vma)
3640{
3641 struct perf_event *event = vma->vm_file->private_data;
3642
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003643 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003644 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003645 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003646 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003647
Peter Zijlstra906010b2009-09-21 16:08:49 +02003648 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003649 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003650 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003651 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003652
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003653 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003654 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003655 }
3656}
3657
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003658static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003659 .open = perf_mmap_open,
3660 .close = perf_mmap_close,
3661 .fault = perf_mmap_fault,
3662 .page_mkwrite = perf_mmap_fault,
3663};
3664
3665static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3666{
3667 struct perf_event *event = file->private_data;
3668 unsigned long user_locked, user_lock_limit;
3669 struct user_struct *user = current_user();
3670 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003671 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003672 unsigned long vma_size;
3673 unsigned long nr_pages;
3674 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003675 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003676
Peter Zijlstrac7920612010-05-18 10:33:24 +02003677 /*
3678 * Don't allow mmap() of inherited per-task counters. This would
3679 * create a performance issue due to all children writing to the
3680 * same buffer.
3681 */
3682 if (event->cpu == -1 && event->attr.inherit)
3683 return -EINVAL;
3684
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003685 if (!(vma->vm_flags & VM_SHARED))
3686 return -EINVAL;
3687
3688 vma_size = vma->vm_end - vma->vm_start;
3689 nr_pages = (vma_size / PAGE_SIZE) - 1;
3690
3691 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003692 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003693 * can do bitmasks instead of modulo.
3694 */
3695 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3696 return -EINVAL;
3697
3698 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3699 return -EINVAL;
3700
3701 if (vma->vm_pgoff != 0)
3702 return -EINVAL;
3703
3704 WARN_ON_ONCE(event->ctx->parent_ctx);
3705 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003706 if (event->buffer) {
3707 if (event->buffer->nr_pages == nr_pages)
3708 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003709 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003710 ret = -EINVAL;
3711 goto unlock;
3712 }
3713
3714 user_extra = nr_pages + 1;
3715 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3716
3717 /*
3718 * Increase the limit linearly with more CPUs:
3719 */
3720 user_lock_limit *= num_online_cpus();
3721
3722 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3723
3724 extra = 0;
3725 if (user_locked > user_lock_limit)
3726 extra = user_locked - user_lock_limit;
3727
Jiri Slaby78d7d402010-03-05 13:42:54 -08003728 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003729 lock_limit >>= PAGE_SHIFT;
3730 locked = vma->vm_mm->locked_vm + extra;
3731
3732 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3733 !capable(CAP_IPC_LOCK)) {
3734 ret = -EPERM;
3735 goto unlock;
3736 }
3737
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003738 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003739
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003740 if (vma->vm_flags & VM_WRITE)
3741 flags |= PERF_BUFFER_WRITABLE;
3742
3743 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3744 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003745 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003746 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003747 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003748 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003749 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003750
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003751 atomic_long_add(user_extra, &user->locked_vm);
3752 event->mmap_locked = extra;
3753 event->mmap_user = get_current_user();
3754 vma->vm_mm->locked_vm += event->mmap_locked;
3755
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003756unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003757 if (!ret)
3758 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003759 mutex_unlock(&event->mmap_mutex);
3760
3761 vma->vm_flags |= VM_RESERVED;
3762 vma->vm_ops = &perf_mmap_vmops;
3763
3764 return ret;
3765}
3766
3767static int perf_fasync(int fd, struct file *filp, int on)
3768{
3769 struct inode *inode = filp->f_path.dentry->d_inode;
3770 struct perf_event *event = filp->private_data;
3771 int retval;
3772
3773 mutex_lock(&inode->i_mutex);
3774 retval = fasync_helper(fd, filp, on, &event->fasync);
3775 mutex_unlock(&inode->i_mutex);
3776
3777 if (retval < 0)
3778 return retval;
3779
3780 return 0;
3781}
3782
3783static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003784 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003785 .release = perf_release,
3786 .read = perf_read,
3787 .poll = perf_poll,
3788 .unlocked_ioctl = perf_ioctl,
3789 .compat_ioctl = perf_ioctl,
3790 .mmap = perf_mmap,
3791 .fasync = perf_fasync,
3792};
3793
3794/*
3795 * Perf event wakeup
3796 *
3797 * If there's data, ensure we set the poll() state and publish everything
3798 * to user-space before waking everybody up.
3799 */
3800
3801void perf_event_wakeup(struct perf_event *event)
3802{
3803 wake_up_all(&event->waitq);
3804
3805 if (event->pending_kill) {
3806 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3807 event->pending_kill = 0;
3808 }
3809}
3810
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003811static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003812{
3813 struct perf_event *event = container_of(entry,
3814 struct perf_event, pending);
3815
3816 if (event->pending_disable) {
3817 event->pending_disable = 0;
3818 __perf_event_disable(event);
3819 }
3820
3821 if (event->pending_wakeup) {
3822 event->pending_wakeup = 0;
3823 perf_event_wakeup(event);
3824 }
3825}
3826
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003827/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003828 * We assume there is only KVM supporting the callbacks.
3829 * Later on, we might change it to a list if there is
3830 * another virtualization implementation supporting the callbacks.
3831 */
3832struct perf_guest_info_callbacks *perf_guest_cbs;
3833
3834int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3835{
3836 perf_guest_cbs = cbs;
3837 return 0;
3838}
3839EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3840
3841int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3842{
3843 perf_guest_cbs = NULL;
3844 return 0;
3845}
3846EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3847
3848/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003849 * Output
3850 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003851static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003852 unsigned long offset, unsigned long head)
3853{
3854 unsigned long mask;
3855
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003856 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003857 return true;
3858
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003859 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003860
3861 offset = (offset - tail) & mask;
3862 head = (head - tail) & mask;
3863
3864 if ((int)(head - offset) < 0)
3865 return false;
3866
3867 return true;
3868}
3869
3870static void perf_output_wakeup(struct perf_output_handle *handle)
3871{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003872 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003873
3874 if (handle->nmi) {
3875 handle->event->pending_wakeup = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003876 irq_work_queue(&handle->event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003877 } else
3878 perf_event_wakeup(handle->event);
3879}
3880
3881/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003882 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003883 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003884 * cannot fully serialize things.
3885 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003886 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003887 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003888 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003889static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003890{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003891 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003892
Peter Zijlstraef607772010-05-18 10:50:41 +02003893 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003894 local_inc(&buffer->nest);
3895 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003896}
3897
Peter Zijlstraef607772010-05-18 10:50:41 +02003898static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003899{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003900 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003901 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003902
3903again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003904 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003905
3906 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003907 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003908 */
3909
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003910 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003911 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003912
3913 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003914 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003915 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003916 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003917 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003918 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003919
Peter Zijlstraef607772010-05-18 10:50:41 +02003920 /*
3921 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003922 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003923 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003924 if (unlikely(head != local_read(&buffer->head))) {
3925 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003926 goto again;
3927 }
3928
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003929 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003930 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003931
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003932out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003933 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003934}
3935
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003936__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003937 const void *buf, unsigned int len)
3938{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003939 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003940 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003941
3942 memcpy(handle->addr, buf, size);
3943
3944 len -= size;
3945 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003946 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003947 handle->size -= size;
3948 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003949 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003950
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003951 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003952 handle->page &= buffer->nr_pages - 1;
3953 handle->addr = buffer->data_pages[handle->page];
3954 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003955 }
3956 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003957}
3958
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003959static void __perf_event_header__init_id(struct perf_event_header *header,
3960 struct perf_sample_data *data,
3961 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02003962{
3963 u64 sample_type = event->attr.sample_type;
3964
3965 data->type = sample_type;
3966 header->size += event->id_header_size;
3967
3968 if (sample_type & PERF_SAMPLE_TID) {
3969 /* namespace issues */
3970 data->tid_entry.pid = perf_event_pid(event, current);
3971 data->tid_entry.tid = perf_event_tid(event, current);
3972 }
3973
3974 if (sample_type & PERF_SAMPLE_TIME)
3975 data->time = perf_clock();
3976
3977 if (sample_type & PERF_SAMPLE_ID)
3978 data->id = primary_event_id(event);
3979
3980 if (sample_type & PERF_SAMPLE_STREAM_ID)
3981 data->stream_id = event->id;
3982
3983 if (sample_type & PERF_SAMPLE_CPU) {
3984 data->cpu_entry.cpu = raw_smp_processor_id();
3985 data->cpu_entry.reserved = 0;
3986 }
3987}
3988
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003989static void perf_event_header__init_id(struct perf_event_header *header,
3990 struct perf_sample_data *data,
3991 struct perf_event *event)
3992{
3993 if (event->attr.sample_id_all)
3994 __perf_event_header__init_id(header, data, event);
3995}
3996
3997static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3998 struct perf_sample_data *data)
3999{
4000 u64 sample_type = data->type;
4001
4002 if (sample_type & PERF_SAMPLE_TID)
4003 perf_output_put(handle, data->tid_entry);
4004
4005 if (sample_type & PERF_SAMPLE_TIME)
4006 perf_output_put(handle, data->time);
4007
4008 if (sample_type & PERF_SAMPLE_ID)
4009 perf_output_put(handle, data->id);
4010
4011 if (sample_type & PERF_SAMPLE_STREAM_ID)
4012 perf_output_put(handle, data->stream_id);
4013
4014 if (sample_type & PERF_SAMPLE_CPU)
4015 perf_output_put(handle, data->cpu_entry);
4016}
4017
4018static void perf_event__output_id_sample(struct perf_event *event,
4019 struct perf_output_handle *handle,
4020 struct perf_sample_data *sample)
4021{
4022 if (event->attr.sample_id_all)
4023 __perf_event__output_id_sample(handle, sample);
4024}
4025
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004026int perf_output_begin(struct perf_output_handle *handle,
4027 struct perf_event *event, unsigned int size,
4028 int nmi, int sample)
4029{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004030 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004031 unsigned long tail, offset, head;
4032 int have_lost;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004033 struct perf_sample_data sample_data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004034 struct {
4035 struct perf_event_header header;
4036 u64 id;
4037 u64 lost;
4038 } lost_event;
4039
4040 rcu_read_lock();
4041 /*
4042 * For inherited events we send all the output towards the parent.
4043 */
4044 if (event->parent)
4045 event = event->parent;
4046
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004047 buffer = rcu_dereference(event->buffer);
4048 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004049 goto out;
4050
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004051 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004052 handle->event = event;
4053 handle->nmi = nmi;
4054 handle->sample = sample;
4055
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004056 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02004057 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004058
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004059 have_lost = local_read(&buffer->lost);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004060 if (have_lost) {
4061 lost_event.header.size = sizeof(lost_event);
4062 perf_event_header__init_id(&lost_event.header, &sample_data,
4063 event);
4064 size += lost_event.header.size;
4065 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004066
Peter Zijlstraef607772010-05-18 10:50:41 +02004067 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004068
4069 do {
4070 /*
4071 * Userspace could choose to issue a mb() before updating the
4072 * tail pointer. So that all reads will be completed before the
4073 * write is issued.
4074 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004075 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004076 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004077 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004078 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004079 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004080 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004081 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004082
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004083 if (head - local_read(&buffer->wakeup) > buffer->watermark)
4084 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004085
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004086 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
4087 handle->page &= buffer->nr_pages - 1;
4088 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
4089 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02004090 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004091 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02004092
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004093 if (have_lost) {
4094 lost_event.header.type = PERF_RECORD_LOST;
4095 lost_event.header.misc = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004096 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004097 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004098
4099 perf_output_put(handle, lost_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004100 perf_event__output_id_sample(event, handle, &sample_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004101 }
4102
4103 return 0;
4104
4105fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004106 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02004107 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004108out:
4109 rcu_read_unlock();
4110
4111 return -ENOSPC;
4112}
4113
4114void perf_output_end(struct perf_output_handle *handle)
4115{
4116 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004117 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004118
4119 int wakeup_events = event->attr.wakeup_events;
4120
4121 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004122 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004123 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02004124 local_sub(wakeup_events, &buffer->events);
4125 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004126 }
4127 }
4128
Peter Zijlstraef607772010-05-18 10:50:41 +02004129 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004130 rcu_read_unlock();
4131}
4132
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004133static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004134 struct perf_event *event,
4135 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004136{
4137 u64 read_format = event->attr.read_format;
4138 u64 values[4];
4139 int n = 0;
4140
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004141 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004142 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004143 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004144 atomic64_read(&event->child_total_time_enabled);
4145 }
4146 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02004147 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004148 atomic64_read(&event->child_total_time_running);
4149 }
4150 if (read_format & PERF_FORMAT_ID)
4151 values[n++] = primary_event_id(event);
4152
4153 perf_output_copy(handle, values, n * sizeof(u64));
4154}
4155
4156/*
4157 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
4158 */
4159static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02004160 struct perf_event *event,
4161 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004162{
4163 struct perf_event *leader = event->group_leader, *sub;
4164 u64 read_format = event->attr.read_format;
4165 u64 values[5];
4166 int n = 0;
4167
4168 values[n++] = 1 + leader->nr_siblings;
4169
4170 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02004171 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004172
4173 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02004174 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004175
4176 if (leader != event)
4177 leader->pmu->read(leader);
4178
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004179 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004180 if (read_format & PERF_FORMAT_ID)
4181 values[n++] = primary_event_id(leader);
4182
4183 perf_output_copy(handle, values, n * sizeof(u64));
4184
4185 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4186 n = 0;
4187
4188 if (sub != event)
4189 sub->pmu->read(sub);
4190
Peter Zijlstrab5e58792010-05-21 14:43:12 +02004191 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004192 if (read_format & PERF_FORMAT_ID)
4193 values[n++] = primary_event_id(sub);
4194
4195 perf_output_copy(handle, values, n * sizeof(u64));
4196 }
4197}
4198
Stephane Eranianeed01522010-10-26 16:08:01 +02004199#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
4200 PERF_FORMAT_TOTAL_TIME_RUNNING)
4201
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004202static void perf_output_read(struct perf_output_handle *handle,
4203 struct perf_event *event)
4204{
Stephane Eranianeed01522010-10-26 16:08:01 +02004205 u64 enabled = 0, running = 0, now, ctx_time;
4206 u64 read_format = event->attr.read_format;
4207
4208 /*
4209 * compute total_time_enabled, total_time_running
4210 * based on snapshot values taken when the event
4211 * was last scheduled in.
4212 *
4213 * we cannot simply called update_context_time()
4214 * because of locking issue as we are called in
4215 * NMI context
4216 */
4217 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
4218 now = perf_clock();
4219 ctx_time = event->shadow_ctx_time + now;
4220 enabled = ctx_time - event->tstamp_enabled;
4221 running = ctx_time - event->tstamp_running;
4222 }
4223
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004224 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02004225 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004226 else
Stephane Eranianeed01522010-10-26 16:08:01 +02004227 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004228}
4229
4230void perf_output_sample(struct perf_output_handle *handle,
4231 struct perf_event_header *header,
4232 struct perf_sample_data *data,
4233 struct perf_event *event)
4234{
4235 u64 sample_type = data->type;
4236
4237 perf_output_put(handle, *header);
4238
4239 if (sample_type & PERF_SAMPLE_IP)
4240 perf_output_put(handle, data->ip);
4241
4242 if (sample_type & PERF_SAMPLE_TID)
4243 perf_output_put(handle, data->tid_entry);
4244
4245 if (sample_type & PERF_SAMPLE_TIME)
4246 perf_output_put(handle, data->time);
4247
4248 if (sample_type & PERF_SAMPLE_ADDR)
4249 perf_output_put(handle, data->addr);
4250
4251 if (sample_type & PERF_SAMPLE_ID)
4252 perf_output_put(handle, data->id);
4253
4254 if (sample_type & PERF_SAMPLE_STREAM_ID)
4255 perf_output_put(handle, data->stream_id);
4256
4257 if (sample_type & PERF_SAMPLE_CPU)
4258 perf_output_put(handle, data->cpu_entry);
4259
4260 if (sample_type & PERF_SAMPLE_PERIOD)
4261 perf_output_put(handle, data->period);
4262
4263 if (sample_type & PERF_SAMPLE_READ)
4264 perf_output_read(handle, event);
4265
4266 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4267 if (data->callchain) {
4268 int size = 1;
4269
4270 if (data->callchain)
4271 size += data->callchain->nr;
4272
4273 size *= sizeof(u64);
4274
4275 perf_output_copy(handle, data->callchain, size);
4276 } else {
4277 u64 nr = 0;
4278 perf_output_put(handle, nr);
4279 }
4280 }
4281
4282 if (sample_type & PERF_SAMPLE_RAW) {
4283 if (data->raw) {
4284 perf_output_put(handle, data->raw->size);
4285 perf_output_copy(handle, data->raw->data,
4286 data->raw->size);
4287 } else {
4288 struct {
4289 u32 size;
4290 u32 data;
4291 } raw = {
4292 .size = sizeof(u32),
4293 .data = 0,
4294 };
4295 perf_output_put(handle, raw);
4296 }
4297 }
4298}
4299
4300void perf_prepare_sample(struct perf_event_header *header,
4301 struct perf_sample_data *data,
4302 struct perf_event *event,
4303 struct pt_regs *regs)
4304{
4305 u64 sample_type = event->attr.sample_type;
4306
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004307 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004308 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004309
4310 header->misc = 0;
4311 header->misc |= perf_misc_flags(regs);
4312
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004313 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02004314
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004315 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004316 data->ip = perf_instruction_pointer(regs);
4317
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004318 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
4319 int size = 1;
4320
4321 data->callchain = perf_callchain(regs);
4322
4323 if (data->callchain)
4324 size += data->callchain->nr;
4325
4326 header->size += size * sizeof(u64);
4327 }
4328
4329 if (sample_type & PERF_SAMPLE_RAW) {
4330 int size = sizeof(u32);
4331
4332 if (data->raw)
4333 size += data->raw->size;
4334 else
4335 size += sizeof(u32);
4336
4337 WARN_ON_ONCE(size & (sizeof(u64)-1));
4338 header->size += size;
4339 }
4340}
4341
4342static void perf_event_output(struct perf_event *event, int nmi,
4343 struct perf_sample_data *data,
4344 struct pt_regs *regs)
4345{
4346 struct perf_output_handle handle;
4347 struct perf_event_header header;
4348
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004349 /* protect the callchain buffers */
4350 rcu_read_lock();
4351
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004352 perf_prepare_sample(&header, data, event, regs);
4353
4354 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004355 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004356
4357 perf_output_sample(&handle, &header, data, event);
4358
4359 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004360
4361exit:
4362 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004363}
4364
4365/*
4366 * read event_id
4367 */
4368
4369struct perf_read_event {
4370 struct perf_event_header header;
4371
4372 u32 pid;
4373 u32 tid;
4374};
4375
4376static void
4377perf_event_read_event(struct perf_event *event,
4378 struct task_struct *task)
4379{
4380 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004381 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004382 struct perf_read_event read_event = {
4383 .header = {
4384 .type = PERF_RECORD_READ,
4385 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02004386 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004387 },
4388 .pid = perf_event_pid(event, task),
4389 .tid = perf_event_tid(event, task),
4390 };
4391 int ret;
4392
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004393 perf_event_header__init_id(&read_event.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004394 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
4395 if (ret)
4396 return;
4397
4398 perf_output_put(&handle, read_event);
4399 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004400 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004401
4402 perf_output_end(&handle);
4403}
4404
4405/*
4406 * task tracking -- fork/exit
4407 *
Eric B Munson3af9e852010-05-18 15:30:49 +01004408 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004409 */
4410
4411struct perf_task_event {
4412 struct task_struct *task;
4413 struct perf_event_context *task_ctx;
4414
4415 struct {
4416 struct perf_event_header header;
4417
4418 u32 pid;
4419 u32 ppid;
4420 u32 tid;
4421 u32 ptid;
4422 u64 time;
4423 } event_id;
4424};
4425
4426static void perf_event_task_output(struct perf_event *event,
4427 struct perf_task_event *task_event)
4428{
4429 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004430 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004431 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004432 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01004433
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004434 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004435
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004436 ret = perf_output_begin(&handle, event,
4437 task_event->event_id.header.size, 0, 0);
Peter Zijlstraef607772010-05-18 10:50:41 +02004438 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004439 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004440
4441 task_event->event_id.pid = perf_event_pid(event, task);
4442 task_event->event_id.ppid = perf_event_pid(event, current);
4443
4444 task_event->event_id.tid = perf_event_tid(event, task);
4445 task_event->event_id.ptid = perf_event_tid(event, current);
4446
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004447 perf_output_put(&handle, task_event->event_id);
4448
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004449 perf_event__output_id_sample(event, &handle, &sample);
4450
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004451 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004452out:
4453 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004454}
4455
4456static int perf_event_task_match(struct perf_event *event)
4457{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004458 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004459 return 0;
4460
Stephane Eranian5632ab12011-01-03 18:20:01 +02004461 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004462 return 0;
4463
Eric B Munson3af9e852010-05-18 15:30:49 +01004464 if (event->attr.comm || event->attr.mmap ||
4465 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004466 return 1;
4467
4468 return 0;
4469}
4470
4471static void perf_event_task_ctx(struct perf_event_context *ctx,
4472 struct perf_task_event *task_event)
4473{
4474 struct perf_event *event;
4475
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004476 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4477 if (perf_event_task_match(event))
4478 perf_event_task_output(event, task_event);
4479 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004480}
4481
4482static void perf_event_task_event(struct perf_task_event *task_event)
4483{
4484 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004485 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004486 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004487 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004488
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01004489 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004490 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004491 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004492 if (cpuctx->active_pmu != pmu)
4493 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004494 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004495
4496 ctx = task_event->task_ctx;
4497 if (!ctx) {
4498 ctxn = pmu->task_ctx_nr;
4499 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004500 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004501 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4502 }
4503 if (ctx)
4504 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004505next:
4506 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004507 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004508 rcu_read_unlock();
4509}
4510
4511static void perf_event_task(struct task_struct *task,
4512 struct perf_event_context *task_ctx,
4513 int new)
4514{
4515 struct perf_task_event task_event;
4516
4517 if (!atomic_read(&nr_comm_events) &&
4518 !atomic_read(&nr_mmap_events) &&
4519 !atomic_read(&nr_task_events))
4520 return;
4521
4522 task_event = (struct perf_task_event){
4523 .task = task,
4524 .task_ctx = task_ctx,
4525 .event_id = {
4526 .header = {
4527 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
4528 .misc = 0,
4529 .size = sizeof(task_event.event_id),
4530 },
4531 /* .pid */
4532 /* .ppid */
4533 /* .tid */
4534 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004535 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004536 },
4537 };
4538
4539 perf_event_task_event(&task_event);
4540}
4541
4542void perf_event_fork(struct task_struct *task)
4543{
4544 perf_event_task(task, NULL, 1);
4545}
4546
4547/*
4548 * comm tracking
4549 */
4550
4551struct perf_comm_event {
4552 struct task_struct *task;
4553 char *comm;
4554 int comm_size;
4555
4556 struct {
4557 struct perf_event_header header;
4558
4559 u32 pid;
4560 u32 tid;
4561 } event_id;
4562};
4563
4564static void perf_event_comm_output(struct perf_event *event,
4565 struct perf_comm_event *comm_event)
4566{
4567 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004568 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004569 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004570 int ret;
4571
4572 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4573 ret = perf_output_begin(&handle, event,
4574 comm_event->event_id.header.size, 0, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004575
4576 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004577 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004578
4579 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4580 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4581
4582 perf_output_put(&handle, comm_event->event_id);
4583 perf_output_copy(&handle, comm_event->comm,
4584 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004585
4586 perf_event__output_id_sample(event, &handle, &sample);
4587
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004588 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004589out:
4590 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004591}
4592
4593static int perf_event_comm_match(struct perf_event *event)
4594{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004595 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004596 return 0;
4597
Stephane Eranian5632ab12011-01-03 18:20:01 +02004598 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004599 return 0;
4600
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004601 if (event->attr.comm)
4602 return 1;
4603
4604 return 0;
4605}
4606
4607static void perf_event_comm_ctx(struct perf_event_context *ctx,
4608 struct perf_comm_event *comm_event)
4609{
4610 struct perf_event *event;
4611
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004612 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4613 if (perf_event_comm_match(event))
4614 perf_event_comm_output(event, comm_event);
4615 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004616}
4617
4618static void perf_event_comm_event(struct perf_comm_event *comm_event)
4619{
4620 struct perf_cpu_context *cpuctx;
4621 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004622 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004623 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004624 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004625 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004626
4627 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01004628 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004629 size = ALIGN(strlen(comm)+1, sizeof(u64));
4630
4631 comm_event->comm = comm;
4632 comm_event->comm_size = size;
4633
4634 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstraf6595f32009-11-20 22:19:47 +01004635 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004636 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004637 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004638 if (cpuctx->active_pmu != pmu)
4639 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004640 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004641
4642 ctxn = pmu->task_ctx_nr;
4643 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004644 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004645
4646 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4647 if (ctx)
4648 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004649next:
4650 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004651 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004652 rcu_read_unlock();
4653}
4654
4655void perf_event_comm(struct task_struct *task)
4656{
4657 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004658 struct perf_event_context *ctx;
4659 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004660
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004661 for_each_task_context_nr(ctxn) {
4662 ctx = task->perf_event_ctxp[ctxn];
4663 if (!ctx)
4664 continue;
4665
4666 perf_event_enable_on_exec(ctx);
4667 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004668
4669 if (!atomic_read(&nr_comm_events))
4670 return;
4671
4672 comm_event = (struct perf_comm_event){
4673 .task = task,
4674 /* .comm */
4675 /* .comm_size */
4676 .event_id = {
4677 .header = {
4678 .type = PERF_RECORD_COMM,
4679 .misc = 0,
4680 /* .size */
4681 },
4682 /* .pid */
4683 /* .tid */
4684 },
4685 };
4686
4687 perf_event_comm_event(&comm_event);
4688}
4689
4690/*
4691 * mmap tracking
4692 */
4693
4694struct perf_mmap_event {
4695 struct vm_area_struct *vma;
4696
4697 const char *file_name;
4698 int file_size;
4699
4700 struct {
4701 struct perf_event_header header;
4702
4703 u32 pid;
4704 u32 tid;
4705 u64 start;
4706 u64 len;
4707 u64 pgoff;
4708 } event_id;
4709};
4710
4711static void perf_event_mmap_output(struct perf_event *event,
4712 struct perf_mmap_event *mmap_event)
4713{
4714 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004715 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004716 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004717 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004718
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004719 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4720 ret = perf_output_begin(&handle, event,
4721 mmap_event->event_id.header.size, 0, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004722 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004723 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004724
4725 mmap_event->event_id.pid = perf_event_pid(event, current);
4726 mmap_event->event_id.tid = perf_event_tid(event, current);
4727
4728 perf_output_put(&handle, mmap_event->event_id);
4729 perf_output_copy(&handle, mmap_event->file_name,
4730 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004731
4732 perf_event__output_id_sample(event, &handle, &sample);
4733
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004734 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004735out:
4736 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004737}
4738
4739static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01004740 struct perf_mmap_event *mmap_event,
4741 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004742{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004743 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004744 return 0;
4745
Stephane Eranian5632ab12011-01-03 18:20:01 +02004746 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004747 return 0;
4748
Eric B Munson3af9e852010-05-18 15:30:49 +01004749 if ((!executable && event->attr.mmap_data) ||
4750 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004751 return 1;
4752
4753 return 0;
4754}
4755
4756static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004757 struct perf_mmap_event *mmap_event,
4758 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004759{
4760 struct perf_event *event;
4761
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004762 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004763 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004764 perf_event_mmap_output(event, mmap_event);
4765 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004766}
4767
4768static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4769{
4770 struct perf_cpu_context *cpuctx;
4771 struct perf_event_context *ctx;
4772 struct vm_area_struct *vma = mmap_event->vma;
4773 struct file *file = vma->vm_file;
4774 unsigned int size;
4775 char tmp[16];
4776 char *buf = NULL;
4777 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004778 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004779 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004780
4781 memset(tmp, 0, sizeof(tmp));
4782
4783 if (file) {
4784 /*
4785 * d_path works from the end of the buffer backwards, so we
4786 * need to add enough zero bytes after the string to handle
4787 * the 64bit alignment we do later.
4788 */
4789 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4790 if (!buf) {
4791 name = strncpy(tmp, "//enomem", sizeof(tmp));
4792 goto got_name;
4793 }
4794 name = d_path(&file->f_path, buf, PATH_MAX);
4795 if (IS_ERR(name)) {
4796 name = strncpy(tmp, "//toolong", sizeof(tmp));
4797 goto got_name;
4798 }
4799 } else {
4800 if (arch_vma_name(mmap_event->vma)) {
4801 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4802 sizeof(tmp));
4803 goto got_name;
4804 }
4805
4806 if (!vma->vm_mm) {
4807 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4808 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004809 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4810 vma->vm_end >= vma->vm_mm->brk) {
4811 name = strncpy(tmp, "[heap]", sizeof(tmp));
4812 goto got_name;
4813 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4814 vma->vm_end >= vma->vm_mm->start_stack) {
4815 name = strncpy(tmp, "[stack]", sizeof(tmp));
4816 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004817 }
4818
4819 name = strncpy(tmp, "//anon", sizeof(tmp));
4820 goto got_name;
4821 }
4822
4823got_name:
4824 size = ALIGN(strlen(name)+1, sizeof(u64));
4825
4826 mmap_event->file_name = name;
4827 mmap_event->file_size = size;
4828
4829 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4830
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004831 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004832 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004833 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004834 if (cpuctx->active_pmu != pmu)
4835 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004836 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4837 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004838
4839 ctxn = pmu->task_ctx_nr;
4840 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004841 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004842
4843 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4844 if (ctx) {
4845 perf_event_mmap_ctx(ctx, mmap_event,
4846 vma->vm_flags & VM_EXEC);
4847 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004848next:
4849 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004850 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004851 rcu_read_unlock();
4852
4853 kfree(buf);
4854}
4855
Eric B Munson3af9e852010-05-18 15:30:49 +01004856void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004857{
4858 struct perf_mmap_event mmap_event;
4859
4860 if (!atomic_read(&nr_mmap_events))
4861 return;
4862
4863 mmap_event = (struct perf_mmap_event){
4864 .vma = vma,
4865 /* .file_name */
4866 /* .file_size */
4867 .event_id = {
4868 .header = {
4869 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004870 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004871 /* .size */
4872 },
4873 /* .pid */
4874 /* .tid */
4875 .start = vma->vm_start,
4876 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004877 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004878 },
4879 };
4880
4881 perf_event_mmap_event(&mmap_event);
4882}
4883
4884/*
4885 * IRQ throttle logging
4886 */
4887
4888static void perf_log_throttle(struct perf_event *event, int enable)
4889{
4890 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004891 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004892 int ret;
4893
4894 struct {
4895 struct perf_event_header header;
4896 u64 time;
4897 u64 id;
4898 u64 stream_id;
4899 } throttle_event = {
4900 .header = {
4901 .type = PERF_RECORD_THROTTLE,
4902 .misc = 0,
4903 .size = sizeof(throttle_event),
4904 },
4905 .time = perf_clock(),
4906 .id = primary_event_id(event),
4907 .stream_id = event->id,
4908 };
4909
4910 if (enable)
4911 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4912
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004913 perf_event_header__init_id(&throttle_event.header, &sample, event);
4914
4915 ret = perf_output_begin(&handle, event,
4916 throttle_event.header.size, 1, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004917 if (ret)
4918 return;
4919
4920 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004921 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004922 perf_output_end(&handle);
4923}
4924
4925/*
4926 * Generic event overflow handling, sampling.
4927 */
4928
4929static int __perf_event_overflow(struct perf_event *event, int nmi,
4930 int throttle, struct perf_sample_data *data,
4931 struct pt_regs *regs)
4932{
4933 int events = atomic_read(&event->event_limit);
4934 struct hw_perf_event *hwc = &event->hw;
4935 int ret = 0;
4936
Peter Zijlstra96398822010-11-24 18:55:29 +01004937 /*
4938 * Non-sampling counters might still use the PMI to fold short
4939 * hardware counters, ignore those.
4940 */
4941 if (unlikely(!is_sampling_event(event)))
4942 return 0;
4943
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004944 if (!throttle) {
4945 hwc->interrupts++;
4946 } else {
4947 if (hwc->interrupts != MAX_INTERRUPTS) {
4948 hwc->interrupts++;
4949 if (HZ * hwc->interrupts >
4950 (u64)sysctl_perf_event_sample_rate) {
4951 hwc->interrupts = MAX_INTERRUPTS;
4952 perf_log_throttle(event, 0);
4953 ret = 1;
4954 }
4955 } else {
4956 /*
4957 * Keep re-disabling events even though on the previous
4958 * pass we disabled it - just in case we raced with a
4959 * sched-in and the event got enabled again:
4960 */
4961 ret = 1;
4962 }
4963 }
4964
4965 if (event->attr.freq) {
4966 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004967 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004968
Peter Zijlstraabd50712010-01-26 18:50:16 +01004969 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004970
Peter Zijlstraabd50712010-01-26 18:50:16 +01004971 if (delta > 0 && delta < 2*TICK_NSEC)
4972 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004973 }
4974
4975 /*
4976 * XXX event_limit might not quite work as expected on inherited
4977 * events
4978 */
4979
4980 event->pending_kill = POLL_IN;
4981 if (events && atomic_dec_and_test(&event->event_limit)) {
4982 ret = 1;
4983 event->pending_kill = POLL_HUP;
4984 if (nmi) {
4985 event->pending_disable = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004986 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004987 } else
4988 perf_event_disable(event);
4989 }
4990
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004991 if (event->overflow_handler)
4992 event->overflow_handler(event, nmi, data, regs);
4993 else
4994 perf_event_output(event, nmi, data, regs);
4995
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004996 return ret;
4997}
4998
4999int perf_event_overflow(struct perf_event *event, int nmi,
5000 struct perf_sample_data *data,
5001 struct pt_regs *regs)
5002{
5003 return __perf_event_overflow(event, nmi, 1, data, regs);
5004}
5005
5006/*
5007 * Generic software event infrastructure
5008 */
5009
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005010struct swevent_htable {
5011 struct swevent_hlist *swevent_hlist;
5012 struct mutex hlist_mutex;
5013 int hlist_refcount;
5014
5015 /* Recursion avoidance in each contexts */
5016 int recursion[PERF_NR_CONTEXTS];
5017};
5018
5019static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
5020
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005021/*
5022 * We directly increment event->count and keep a second value in
5023 * event->hw.period_left to count intervals. This period event
5024 * is kept in the range [-sample_period, 0] so that we can use the
5025 * sign as trigger.
5026 */
5027
5028static u64 perf_swevent_set_period(struct perf_event *event)
5029{
5030 struct hw_perf_event *hwc = &event->hw;
5031 u64 period = hwc->last_period;
5032 u64 nr, offset;
5033 s64 old, val;
5034
5035 hwc->last_period = hwc->sample_period;
5036
5037again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02005038 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005039 if (val < 0)
5040 return 0;
5041
5042 nr = div64_u64(period + val, period);
5043 offset = nr * period;
5044 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02005045 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005046 goto again;
5047
5048 return nr;
5049}
5050
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005051static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005052 int nmi, struct perf_sample_data *data,
5053 struct pt_regs *regs)
5054{
5055 struct hw_perf_event *hwc = &event->hw;
5056 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005057
5058 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005059 if (!overflow)
5060 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005061
5062 if (hwc->interrupts == MAX_INTERRUPTS)
5063 return;
5064
5065 for (; overflow; overflow--) {
5066 if (__perf_event_overflow(event, nmi, throttle,
5067 data, regs)) {
5068 /*
5069 * We inhibit the overflow from happening when
5070 * hwc->interrupts == MAX_INTERRUPTS.
5071 */
5072 break;
5073 }
5074 throttle = 1;
5075 }
5076}
5077
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005078static void perf_swevent_event(struct perf_event *event, u64 nr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005079 int nmi, struct perf_sample_data *data,
5080 struct pt_regs *regs)
5081{
5082 struct hw_perf_event *hwc = &event->hw;
5083
Peter Zijlstrae7850592010-05-21 14:43:08 +02005084 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005085
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005086 if (!regs)
5087 return;
5088
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005089 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005090 return;
5091
5092 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
5093 return perf_swevent_overflow(event, 1, nmi, data, regs);
5094
Peter Zijlstrae7850592010-05-21 14:43:08 +02005095 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01005096 return;
5097
5098 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005099}
5100
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005101static int perf_exclude_event(struct perf_event *event,
5102 struct pt_regs *regs)
5103{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005104 if (event->hw.state & PERF_HES_STOPPED)
5105 return 0;
5106
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005107 if (regs) {
5108 if (event->attr.exclude_user && user_mode(regs))
5109 return 1;
5110
5111 if (event->attr.exclude_kernel && !user_mode(regs))
5112 return 1;
5113 }
5114
5115 return 0;
5116}
5117
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005118static int perf_swevent_match(struct perf_event *event,
5119 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08005120 u32 event_id,
5121 struct perf_sample_data *data,
5122 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005123{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005124 if (event->attr.type != type)
5125 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005126
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005127 if (event->attr.config != event_id)
5128 return 0;
5129
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005130 if (perf_exclude_event(event, regs))
5131 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005132
5133 return 1;
5134}
5135
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005136static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005137{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005138 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005139
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005140 return hash_64(val, SWEVENT_HLIST_BITS);
5141}
5142
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005143static inline struct hlist_head *
5144__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005145{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005146 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005147
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005148 return &hlist->heads[hash];
5149}
5150
5151/* For the read side: events when they trigger */
5152static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005153find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005154{
5155 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005156
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005157 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005158 if (!hlist)
5159 return NULL;
5160
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005161 return __find_swevent_head(hlist, type, event_id);
5162}
5163
5164/* For the event head insertion and removal in the hlist */
5165static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005166find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005167{
5168 struct swevent_hlist *hlist;
5169 u32 event_id = event->attr.config;
5170 u64 type = event->attr.type;
5171
5172 /*
5173 * Event scheduling is always serialized against hlist allocation
5174 * and release. Which makes the protected version suitable here.
5175 * The context lock guarantees that.
5176 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005177 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005178 lockdep_is_held(&event->ctx->lock));
5179 if (!hlist)
5180 return NULL;
5181
5182 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005183}
5184
5185static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
5186 u64 nr, int nmi,
5187 struct perf_sample_data *data,
5188 struct pt_regs *regs)
5189{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005190 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005191 struct perf_event *event;
5192 struct hlist_node *node;
5193 struct hlist_head *head;
5194
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005195 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005196 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005197 if (!head)
5198 goto end;
5199
5200 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08005201 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005202 perf_swevent_event(event, nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005203 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005204end:
5205 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005206}
5207
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005208int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005209{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005210 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005211
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005212 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005213}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01005214EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005215
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01005216inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005217{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005218 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005219
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005220 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01005221}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005222
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005223void __perf_sw_event(u32 event_id, u64 nr, int nmi,
5224 struct pt_regs *regs, u64 addr)
5225{
Ingo Molnara4234bf2009-11-23 10:57:59 +01005226 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005227 int rctx;
5228
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005229 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005230 rctx = perf_swevent_get_recursion_context();
5231 if (rctx < 0)
5232 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005233
Peter Zijlstradc1d6282010-03-03 15:55:04 +01005234 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01005235
5236 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01005237
5238 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005239 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005240}
5241
5242static void perf_swevent_read(struct perf_event *event)
5243{
5244}
5245
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005246static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005247{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005248 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005249 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005250 struct hlist_head *head;
5251
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005252 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005253 hwc->last_period = hwc->sample_period;
5254 perf_swevent_set_period(event);
5255 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005256
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005257 hwc->state = !(flags & PERF_EF_START);
5258
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005259 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005260 if (WARN_ON_ONCE(!head))
5261 return -EINVAL;
5262
5263 hlist_add_head_rcu(&event->hlist_entry, head);
5264
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005265 return 0;
5266}
5267
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005268static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005269{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005270 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005271}
5272
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005273static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005274{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005275 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005276}
5277
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005278static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005279{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005280 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02005281}
5282
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005283/* Deref the hlist from the update side */
5284static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005285swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005286{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005287 return rcu_dereference_protected(swhash->swevent_hlist,
5288 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005289}
5290
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005291static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
5292{
5293 struct swevent_hlist *hlist;
5294
5295 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
5296 kfree(hlist);
5297}
5298
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005299static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005300{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005301 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005302
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02005303 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005304 return;
5305
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005306 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005307 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
5308}
5309
5310static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
5311{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005312 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005313
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005314 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005315
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005316 if (!--swhash->hlist_refcount)
5317 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005318
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005319 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005320}
5321
5322static void swevent_hlist_put(struct perf_event *event)
5323{
5324 int cpu;
5325
5326 if (event->cpu != -1) {
5327 swevent_hlist_put_cpu(event, event->cpu);
5328 return;
5329 }
5330
5331 for_each_possible_cpu(cpu)
5332 swevent_hlist_put_cpu(event, cpu);
5333}
5334
5335static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
5336{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005337 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005338 int err = 0;
5339
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005340 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005341
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005342 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005343 struct swevent_hlist *hlist;
5344
5345 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5346 if (!hlist) {
5347 err = -ENOMEM;
5348 goto exit;
5349 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005350 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005351 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005352 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005353exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02005354 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005355
5356 return err;
5357}
5358
5359static int swevent_hlist_get(struct perf_event *event)
5360{
5361 int err;
5362 int cpu, failed_cpu;
5363
5364 if (event->cpu != -1)
5365 return swevent_hlist_get_cpu(event, event->cpu);
5366
5367 get_online_cpus();
5368 for_each_possible_cpu(cpu) {
5369 err = swevent_hlist_get_cpu(event, cpu);
5370 if (err) {
5371 failed_cpu = cpu;
5372 goto fail;
5373 }
5374 }
5375 put_online_cpus();
5376
5377 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02005378fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005379 for_each_possible_cpu(cpu) {
5380 if (cpu == failed_cpu)
5381 break;
5382 swevent_hlist_put_cpu(event, cpu);
5383 }
5384
5385 put_online_cpus();
5386 return err;
5387}
5388
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005389atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005390
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005391static void sw_perf_event_destroy(struct perf_event *event)
5392{
5393 u64 event_id = event->attr.config;
5394
5395 WARN_ON(event->parent);
5396
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02005397 jump_label_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005398 swevent_hlist_put(event);
5399}
5400
5401static int perf_swevent_init(struct perf_event *event)
5402{
5403 int event_id = event->attr.config;
5404
5405 if (event->attr.type != PERF_TYPE_SOFTWARE)
5406 return -ENOENT;
5407
5408 switch (event_id) {
5409 case PERF_COUNT_SW_CPU_CLOCK:
5410 case PERF_COUNT_SW_TASK_CLOCK:
5411 return -ENOENT;
5412
5413 default:
5414 break;
5415 }
5416
Dan Carpenterce677832010-10-24 21:50:42 +02005417 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005418 return -ENOENT;
5419
5420 if (!event->parent) {
5421 int err;
5422
5423 err = swevent_hlist_get(event);
5424 if (err)
5425 return err;
5426
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02005427 jump_label_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005428 event->destroy = sw_perf_event_destroy;
5429 }
5430
5431 return 0;
5432}
5433
5434static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005435 .task_ctx_nr = perf_sw_context,
5436
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005437 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005438 .add = perf_swevent_add,
5439 .del = perf_swevent_del,
5440 .start = perf_swevent_start,
5441 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005442 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005443};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005444
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005445#ifdef CONFIG_EVENT_TRACING
5446
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005447static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02005448 struct perf_sample_data *data)
5449{
5450 void *record = data->raw->data;
5451
5452 if (likely(!event->filter) || filter_match_preds(event->filter, record))
5453 return 1;
5454 return 0;
5455}
5456
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005457static int perf_tp_event_match(struct perf_event *event,
5458 struct perf_sample_data *data,
5459 struct pt_regs *regs)
5460{
Peter Zijlstra580d6072010-05-20 20:54:31 +02005461 /*
5462 * All tracepoints are from kernel-space.
5463 */
5464 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005465 return 0;
5466
5467 if (!perf_tp_filter_match(event, data))
5468 return 0;
5469
5470 return 1;
5471}
5472
5473void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005474 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005475{
5476 struct perf_sample_data data;
5477 struct perf_event *event;
5478 struct hlist_node *node;
5479
5480 struct perf_raw_record raw = {
5481 .size = entry_size,
5482 .data = record,
5483 };
5484
5485 perf_sample_data_init(&data, addr);
5486 data.raw = &raw;
5487
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005488 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
5489 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005490 perf_swevent_event(event, count, 1, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005491 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02005492
5493 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005494}
5495EXPORT_SYMBOL_GPL(perf_tp_event);
5496
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005497static void tp_perf_event_destroy(struct perf_event *event)
5498{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005499 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005500}
5501
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005502static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005503{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005504 int err;
5505
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005506 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5507 return -ENOENT;
5508
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02005509 err = perf_trace_init(event);
5510 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005511 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005512
5513 event->destroy = tp_perf_event_destroy;
5514
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005515 return 0;
5516}
5517
5518static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005519 .task_ctx_nr = perf_sw_context,
5520
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005521 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005522 .add = perf_trace_add,
5523 .del = perf_trace_del,
5524 .start = perf_swevent_start,
5525 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005526 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005527};
5528
5529static inline void perf_tp_register(void)
5530{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005531 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005532}
Li Zefan6fb29152009-10-15 11:21:42 +08005533
5534static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5535{
5536 char *filter_str;
5537 int ret;
5538
5539 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5540 return -EINVAL;
5541
5542 filter_str = strndup_user(arg, PAGE_SIZE);
5543 if (IS_ERR(filter_str))
5544 return PTR_ERR(filter_str);
5545
5546 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5547
5548 kfree(filter_str);
5549 return ret;
5550}
5551
5552static void perf_event_free_filter(struct perf_event *event)
5553{
5554 ftrace_profile_free_filter(event);
5555}
5556
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005557#else
Li Zefan6fb29152009-10-15 11:21:42 +08005558
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005559static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005560{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005561}
Li Zefan6fb29152009-10-15 11:21:42 +08005562
5563static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5564{
5565 return -ENOENT;
5566}
5567
5568static void perf_event_free_filter(struct perf_event *event)
5569{
5570}
5571
Li Zefan07b139c2009-12-21 14:27:35 +08005572#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005573
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005574#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005575void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005576{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005577 struct perf_sample_data sample;
5578 struct pt_regs *regs = data;
5579
Peter Zijlstradc1d6282010-03-03 15:55:04 +01005580 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005581
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005582 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5583 perf_swevent_event(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005584}
5585#endif
5586
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005587/*
5588 * hrtimer based swevent callback
5589 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005590
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005591static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005592{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005593 enum hrtimer_restart ret = HRTIMER_RESTART;
5594 struct perf_sample_data data;
5595 struct pt_regs *regs;
5596 struct perf_event *event;
5597 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005598
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005599 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5600 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005601
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005602 perf_sample_data_init(&data, 0);
5603 data.period = event->hw.last_period;
5604 regs = get_irq_regs();
5605
5606 if (regs && !perf_exclude_event(event, regs)) {
5607 if (!(event->attr.exclude_idle && current->pid == 0))
5608 if (perf_event_overflow(event, 0, &data, regs))
5609 ret = HRTIMER_NORESTART;
5610 }
5611
5612 period = max_t(u64, 10000, event->hw.sample_period);
5613 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5614
5615 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005616}
5617
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005618static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005619{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005620 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005621 s64 period;
5622
5623 if (!is_sampling_event(event))
5624 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005625
5626 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5627 hwc->hrtimer.function = perf_swevent_hrtimer;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005628
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005629 period = local64_read(&hwc->period_left);
5630 if (period) {
5631 if (period < 0)
5632 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005633
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005634 local64_set(&hwc->period_left, 0);
5635 } else {
5636 period = max_t(u64, 10000, hwc->sample_period);
5637 }
5638 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005639 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02005640 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005641}
5642
5643static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5644{
5645 struct hw_perf_event *hwc = &event->hw;
5646
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005647 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005648 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005649 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005650
5651 hrtimer_cancel(&hwc->hrtimer);
5652 }
5653}
5654
5655/*
5656 * Software event: cpu wall time clock
5657 */
5658
5659static void cpu_clock_event_update(struct perf_event *event)
5660{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005661 s64 prev;
5662 u64 now;
5663
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005664 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005665 prev = local64_xchg(&event->hw.prev_count, now);
5666 local64_add(now - prev, &event->count);
5667}
5668
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005669static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005670{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005671 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005672 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005673}
5674
5675static void cpu_clock_event_stop(struct perf_event *event, int flags)
5676{
5677 perf_swevent_cancel_hrtimer(event);
5678 cpu_clock_event_update(event);
5679}
5680
5681static int cpu_clock_event_add(struct perf_event *event, int flags)
5682{
5683 if (flags & PERF_EF_START)
5684 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005685
5686 return 0;
5687}
5688
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005689static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005690{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005691 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005692}
5693
5694static void cpu_clock_event_read(struct perf_event *event)
5695{
5696 cpu_clock_event_update(event);
5697}
5698
5699static int cpu_clock_event_init(struct perf_event *event)
5700{
5701 if (event->attr.type != PERF_TYPE_SOFTWARE)
5702 return -ENOENT;
5703
5704 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5705 return -ENOENT;
5706
5707 return 0;
5708}
5709
5710static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005711 .task_ctx_nr = perf_sw_context,
5712
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005713 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005714 .add = cpu_clock_event_add,
5715 .del = cpu_clock_event_del,
5716 .start = cpu_clock_event_start,
5717 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005718 .read = cpu_clock_event_read,
5719};
5720
5721/*
5722 * Software event: task time clock
5723 */
5724
5725static void task_clock_event_update(struct perf_event *event, u64 now)
5726{
5727 u64 prev;
5728 s64 delta;
5729
5730 prev = local64_xchg(&event->hw.prev_count, now);
5731 delta = now - prev;
5732 local64_add(delta, &event->count);
5733}
5734
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005735static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005736{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005737 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005738 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005739}
5740
5741static void task_clock_event_stop(struct perf_event *event, int flags)
5742{
5743 perf_swevent_cancel_hrtimer(event);
5744 task_clock_event_update(event, event->ctx->time);
5745}
5746
5747static int task_clock_event_add(struct perf_event *event, int flags)
5748{
5749 if (flags & PERF_EF_START)
5750 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005751
5752 return 0;
5753}
5754
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005755static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005756{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005757 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005758}
5759
5760static void task_clock_event_read(struct perf_event *event)
5761{
5762 u64 time;
5763
5764 if (!in_nmi()) {
5765 update_context_time(event->ctx);
Stephane Eraniane5d13672011-02-14 11:20:01 +02005766 update_cgrp_time_from_event(event);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005767 time = event->ctx->time;
5768 } else {
5769 u64 now = perf_clock();
5770 u64 delta = now - event->ctx->timestamp;
5771 time = event->ctx->time + delta;
5772 }
5773
5774 task_clock_event_update(event, time);
5775}
5776
5777static int task_clock_event_init(struct perf_event *event)
5778{
5779 if (event->attr.type != PERF_TYPE_SOFTWARE)
5780 return -ENOENT;
5781
5782 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5783 return -ENOENT;
5784
5785 return 0;
5786}
5787
5788static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005789 .task_ctx_nr = perf_sw_context,
5790
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005791 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005792 .add = task_clock_event_add,
5793 .del = task_clock_event_del,
5794 .start = task_clock_event_start,
5795 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005796 .read = task_clock_event_read,
5797};
5798
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005799static void perf_pmu_nop_void(struct pmu *pmu)
5800{
5801}
5802
5803static int perf_pmu_nop_int(struct pmu *pmu)
5804{
5805 return 0;
5806}
5807
5808static void perf_pmu_start_txn(struct pmu *pmu)
5809{
5810 perf_pmu_disable(pmu);
5811}
5812
5813static int perf_pmu_commit_txn(struct pmu *pmu)
5814{
5815 perf_pmu_enable(pmu);
5816 return 0;
5817}
5818
5819static void perf_pmu_cancel_txn(struct pmu *pmu)
5820{
5821 perf_pmu_enable(pmu);
5822}
5823
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005824/*
5825 * Ensures all contexts with the same task_ctx_nr have the same
5826 * pmu_cpu_context too.
5827 */
5828static void *find_pmu_context(int ctxn)
5829{
5830 struct pmu *pmu;
5831
5832 if (ctxn < 0)
5833 return NULL;
5834
5835 list_for_each_entry(pmu, &pmus, entry) {
5836 if (pmu->task_ctx_nr == ctxn)
5837 return pmu->pmu_cpu_context;
5838 }
5839
5840 return NULL;
5841}
5842
Peter Zijlstra51676952010-12-07 14:18:20 +01005843static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005844{
Peter Zijlstra51676952010-12-07 14:18:20 +01005845 int cpu;
5846
5847 for_each_possible_cpu(cpu) {
5848 struct perf_cpu_context *cpuctx;
5849
5850 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5851
5852 if (cpuctx->active_pmu == old_pmu)
5853 cpuctx->active_pmu = pmu;
5854 }
5855}
5856
5857static void free_pmu_context(struct pmu *pmu)
5858{
5859 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005860
5861 mutex_lock(&pmus_lock);
5862 /*
5863 * Like a real lame refcount.
5864 */
Peter Zijlstra51676952010-12-07 14:18:20 +01005865 list_for_each_entry(i, &pmus, entry) {
5866 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5867 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005868 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01005869 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005870 }
5871
Peter Zijlstra51676952010-12-07 14:18:20 +01005872 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005873out:
5874 mutex_unlock(&pmus_lock);
5875}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005876static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005877
Peter Zijlstraabe43402010-11-17 23:17:37 +01005878static ssize_t
5879type_show(struct device *dev, struct device_attribute *attr, char *page)
5880{
5881 struct pmu *pmu = dev_get_drvdata(dev);
5882
5883 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5884}
5885
5886static struct device_attribute pmu_dev_attrs[] = {
5887 __ATTR_RO(type),
5888 __ATTR_NULL,
5889};
5890
5891static int pmu_bus_running;
5892static struct bus_type pmu_bus = {
5893 .name = "event_source",
5894 .dev_attrs = pmu_dev_attrs,
5895};
5896
5897static void pmu_dev_release(struct device *dev)
5898{
5899 kfree(dev);
5900}
5901
5902static int pmu_dev_alloc(struct pmu *pmu)
5903{
5904 int ret = -ENOMEM;
5905
5906 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5907 if (!pmu->dev)
5908 goto out;
5909
5910 device_initialize(pmu->dev);
5911 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5912 if (ret)
5913 goto free_dev;
5914
5915 dev_set_drvdata(pmu->dev, pmu);
5916 pmu->dev->bus = &pmu_bus;
5917 pmu->dev->release = pmu_dev_release;
5918 ret = device_add(pmu->dev);
5919 if (ret)
5920 goto free_dev;
5921
5922out:
5923 return ret;
5924
5925free_dev:
5926 put_device(pmu->dev);
5927 goto out;
5928}
5929
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005930static struct lock_class_key cpuctx_mutex;
5931
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005932int perf_pmu_register(struct pmu *pmu, char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005933{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005934 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005935
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005936 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005937 ret = -ENOMEM;
5938 pmu->pmu_disable_count = alloc_percpu(int);
5939 if (!pmu->pmu_disable_count)
5940 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005941
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005942 pmu->type = -1;
5943 if (!name)
5944 goto skip_type;
5945 pmu->name = name;
5946
5947 if (type < 0) {
5948 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5949 if (!err)
5950 goto free_pdc;
5951
5952 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5953 if (err) {
5954 ret = err;
5955 goto free_pdc;
5956 }
5957 }
5958 pmu->type = type;
5959
Peter Zijlstraabe43402010-11-17 23:17:37 +01005960 if (pmu_bus_running) {
5961 ret = pmu_dev_alloc(pmu);
5962 if (ret)
5963 goto free_idr;
5964 }
5965
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005966skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005967 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5968 if (pmu->pmu_cpu_context)
5969 goto got_cpu_context;
5970
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005971 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5972 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01005973 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005974
5975 for_each_possible_cpu(cpu) {
5976 struct perf_cpu_context *cpuctx;
5977
5978 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02005979 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005980 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005981 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005982 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02005983 cpuctx->jiffies_interval = 1;
5984 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra51676952010-12-07 14:18:20 +01005985 cpuctx->active_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005986 }
5987
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005988got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005989 if (!pmu->start_txn) {
5990 if (pmu->pmu_enable) {
5991 /*
5992 * If we have pmu_enable/pmu_disable calls, install
5993 * transaction stubs that use that to try and batch
5994 * hardware accesses.
5995 */
5996 pmu->start_txn = perf_pmu_start_txn;
5997 pmu->commit_txn = perf_pmu_commit_txn;
5998 pmu->cancel_txn = perf_pmu_cancel_txn;
5999 } else {
6000 pmu->start_txn = perf_pmu_nop_void;
6001 pmu->commit_txn = perf_pmu_nop_int;
6002 pmu->cancel_txn = perf_pmu_nop_void;
6003 }
6004 }
6005
6006 if (!pmu->pmu_enable) {
6007 pmu->pmu_enable = perf_pmu_nop_void;
6008 pmu->pmu_disable = perf_pmu_nop_void;
6009 }
6010
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006011 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006012 ret = 0;
6013unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006014 mutex_unlock(&pmus_lock);
6015
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006016 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006017
Peter Zijlstraabe43402010-11-17 23:17:37 +01006018free_dev:
6019 device_del(pmu->dev);
6020 put_device(pmu->dev);
6021
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006022free_idr:
6023 if (pmu->type >= PERF_TYPE_MAX)
6024 idr_remove(&pmu_idr, pmu->type);
6025
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006026free_pdc:
6027 free_percpu(pmu->pmu_disable_count);
6028 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006029}
6030
6031void perf_pmu_unregister(struct pmu *pmu)
6032{
6033 mutex_lock(&pmus_lock);
6034 list_del_rcu(&pmu->entry);
6035 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006036
6037 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02006038 * We dereference the pmu list under both SRCU and regular RCU, so
6039 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006040 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006041 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02006042 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006043
Peter Zijlstra33696fc2010-06-14 08:49:00 +02006044 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006045 if (pmu->type >= PERF_TYPE_MAX)
6046 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01006047 device_del(pmu->dev);
6048 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01006049 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006050}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006051
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006052struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006053{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006054 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006055 int idx;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006056
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006057 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006058
6059 rcu_read_lock();
6060 pmu = idr_find(&pmu_idr, event->attr.type);
6061 rcu_read_unlock();
6062 if (pmu)
6063 goto unlock;
6064
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006065 list_for_each_entry_rcu(pmu, &pmus, entry) {
6066 int ret = pmu->event_init(event);
6067 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006068 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006069
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006070 if (ret != -ENOENT) {
6071 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006072 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006073 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006074 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02006075 pmu = ERR_PTR(-ENOENT);
6076unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006077 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006078
6079 return pmu;
6080}
6081
6082/*
6083 * Allocate and initialize a event structure
6084 */
6085static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006086perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006087 struct task_struct *task,
6088 struct perf_event *group_leader,
6089 struct perf_event *parent_event,
6090 perf_overflow_handler_t overflow_handler)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006091{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02006092 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006093 struct perf_event *event;
6094 struct hw_perf_event *hwc;
6095 long err;
6096
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006097 if ((unsigned)cpu >= nr_cpu_ids) {
6098 if (!task || cpu != -1)
6099 return ERR_PTR(-EINVAL);
6100 }
6101
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006102 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006103 if (!event)
6104 return ERR_PTR(-ENOMEM);
6105
6106 /*
6107 * Single events are their own group leaders, with an
6108 * empty sibling list:
6109 */
6110 if (!group_leader)
6111 group_leader = event;
6112
6113 mutex_init(&event->child_mutex);
6114 INIT_LIST_HEAD(&event->child_list);
6115
6116 INIT_LIST_HEAD(&event->group_entry);
6117 INIT_LIST_HEAD(&event->event_entry);
6118 INIT_LIST_HEAD(&event->sibling_list);
6119 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08006120 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006121
6122 mutex_init(&event->mmap_mutex);
6123
6124 event->cpu = cpu;
6125 event->attr = *attr;
6126 event->group_leader = group_leader;
6127 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006128 event->oncpu = -1;
6129
6130 event->parent = parent_event;
6131
6132 event->ns = get_pid_ns(current->nsproxy->pid_ns);
6133 event->id = atomic64_inc_return(&perf_event_id);
6134
6135 event->state = PERF_EVENT_STATE_INACTIVE;
6136
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006137 if (task) {
6138 event->attach_state = PERF_ATTACH_TASK;
6139#ifdef CONFIG_HAVE_HW_BREAKPOINT
6140 /*
6141 * hw_breakpoint is a bit difficult here..
6142 */
6143 if (attr->type == PERF_TYPE_BREAKPOINT)
6144 event->hw.bp_target = task;
6145#endif
6146 }
6147
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006148 if (!overflow_handler && parent_event)
6149 overflow_handler = parent_event->overflow_handler;
Oleg Nesterov66832eb2011-01-18 17:10:32 +01006150
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006151 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02006152
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006153 if (attr->disabled)
6154 event->state = PERF_EVENT_STATE_OFF;
6155
6156 pmu = NULL;
6157
6158 hwc = &event->hw;
6159 hwc->sample_period = attr->sample_period;
6160 if (attr->freq && attr->sample_freq)
6161 hwc->sample_period = 1;
6162 hwc->last_period = hwc->sample_period;
6163
Peter Zijlstrae7850592010-05-21 14:43:08 +02006164 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006165
6166 /*
6167 * we currently do not support PERF_FORMAT_GROUP on inherited events
6168 */
6169 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
6170 goto done;
6171
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006172 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006173
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006174done:
6175 err = 0;
6176 if (!pmu)
6177 err = -EINVAL;
6178 else if (IS_ERR(pmu))
6179 err = PTR_ERR(pmu);
6180
6181 if (err) {
6182 if (event->ns)
6183 put_pid_ns(event->ns);
6184 kfree(event);
6185 return ERR_PTR(err);
6186 }
6187
6188 event->pmu = pmu;
6189
6190 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02006191 if (event->attach_state & PERF_ATTACH_TASK)
Stephane Eraniane5d13672011-02-14 11:20:01 +02006192 jump_label_inc(&perf_sched_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01006193 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006194 atomic_inc(&nr_mmap_events);
6195 if (event->attr.comm)
6196 atomic_inc(&nr_comm_events);
6197 if (event->attr.task)
6198 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02006199 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
6200 err = get_callchain_buffers();
6201 if (err) {
6202 free_event(event);
6203 return ERR_PTR(err);
6204 }
6205 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006206 }
6207
6208 return event;
6209}
6210
6211static int perf_copy_attr(struct perf_event_attr __user *uattr,
6212 struct perf_event_attr *attr)
6213{
6214 u32 size;
6215 int ret;
6216
6217 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
6218 return -EFAULT;
6219
6220 /*
6221 * zero the full structure, so that a short copy will be nice.
6222 */
6223 memset(attr, 0, sizeof(*attr));
6224
6225 ret = get_user(size, &uattr->size);
6226 if (ret)
6227 return ret;
6228
6229 if (size > PAGE_SIZE) /* silly large */
6230 goto err_size;
6231
6232 if (!size) /* abi compat */
6233 size = PERF_ATTR_SIZE_VER0;
6234
6235 if (size < PERF_ATTR_SIZE_VER0)
6236 goto err_size;
6237
6238 /*
6239 * If we're handed a bigger struct than we know of,
6240 * ensure all the unknown bits are 0 - i.e. new
6241 * user-space does not rely on any kernel feature
6242 * extensions we dont know about yet.
6243 */
6244 if (size > sizeof(*attr)) {
6245 unsigned char __user *addr;
6246 unsigned char __user *end;
6247 unsigned char val;
6248
6249 addr = (void __user *)uattr + sizeof(*attr);
6250 end = (void __user *)uattr + size;
6251
6252 for (; addr < end; addr++) {
6253 ret = get_user(val, addr);
6254 if (ret)
6255 return ret;
6256 if (val)
6257 goto err_size;
6258 }
6259 size = sizeof(*attr);
6260 }
6261
6262 ret = copy_from_user(attr, uattr, size);
6263 if (ret)
6264 return -EFAULT;
6265
6266 /*
6267 * If the type exists, the corresponding creation will verify
6268 * the attr->config.
6269 */
6270 if (attr->type >= PERF_TYPE_MAX)
6271 return -EINVAL;
6272
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05306273 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006274 return -EINVAL;
6275
6276 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
6277 return -EINVAL;
6278
6279 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
6280 return -EINVAL;
6281
6282out:
6283 return ret;
6284
6285err_size:
6286 put_user(sizeof(*attr), &uattr->size);
6287 ret = -E2BIG;
6288 goto out;
6289}
6290
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006291static int
6292perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006293{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006294 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006295 int ret = -EINVAL;
6296
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006297 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006298 goto set;
6299
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006300 /* don't allow circular references */
6301 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006302 goto out;
6303
Peter Zijlstra0f139302010-05-20 14:35:15 +02006304 /*
6305 * Don't allow cross-cpu buffers
6306 */
6307 if (output_event->cpu != event->cpu)
6308 goto out;
6309
6310 /*
6311 * If its not a per-cpu buffer, it must be the same task.
6312 */
6313 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
6314 goto out;
6315
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006316set:
6317 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006318 /* Can't redirect output if we've got an active mmap() */
6319 if (atomic_read(&event->mmap_count))
6320 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006321
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006322 if (output_event) {
6323 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006324 buffer = perf_buffer_get(output_event);
6325 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006326 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006327 }
6328
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006329 old_buffer = event->buffer;
6330 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006331 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006332unlock:
6333 mutex_unlock(&event->mmap_mutex);
6334
Peter Zijlstraca5135e2010-05-28 19:33:23 +02006335 if (old_buffer)
6336 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006337out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006338 return ret;
6339}
6340
6341/**
6342 * sys_perf_event_open - open a performance event, associate it to a task/cpu
6343 *
6344 * @attr_uptr: event_id type attributes for monitoring/sampling
6345 * @pid: target pid
6346 * @cpu: target cpu
6347 * @group_fd: group leader event fd
6348 */
6349SYSCALL_DEFINE5(perf_event_open,
6350 struct perf_event_attr __user *, attr_uptr,
6351 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
6352{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006353 struct perf_event *group_leader = NULL, *output_event = NULL;
6354 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006355 struct perf_event_attr attr;
6356 struct perf_event_context *ctx;
6357 struct file *event_file = NULL;
6358 struct file *group_file = NULL;
Matt Helsley38a81da2010-09-13 13:01:20 -07006359 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006360 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04006361 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006362 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006363 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006364 int err;
6365
6366 /* for future expandability... */
Stephane Eraniane5d13672011-02-14 11:20:01 +02006367 if (flags & ~PERF_FLAG_ALL)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006368 return -EINVAL;
6369
6370 err = perf_copy_attr(attr_uptr, &attr);
6371 if (err)
6372 return err;
6373
6374 if (!attr.exclude_kernel) {
6375 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
6376 return -EACCES;
6377 }
6378
6379 if (attr.freq) {
6380 if (attr.sample_freq > sysctl_perf_event_sample_rate)
6381 return -EINVAL;
6382 }
6383
Stephane Eraniane5d13672011-02-14 11:20:01 +02006384 /*
6385 * In cgroup mode, the pid argument is used to pass the fd
6386 * opened to the cgroup directory in cgroupfs. The cpu argument
6387 * designates the cpu on which to monitor threads from that
6388 * cgroup.
6389 */
6390 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
6391 return -EINVAL;
6392
Al Viroea635c62010-05-26 17:40:29 -04006393 event_fd = get_unused_fd_flags(O_RDWR);
6394 if (event_fd < 0)
6395 return event_fd;
6396
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006397 if (group_fd != -1) {
6398 group_leader = perf_fget_light(group_fd, &fput_needed);
6399 if (IS_ERR(group_leader)) {
6400 err = PTR_ERR(group_leader);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006401 goto err_fd;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006402 }
6403 group_file = group_leader->filp;
6404 if (flags & PERF_FLAG_FD_OUTPUT)
6405 output_event = group_leader;
6406 if (flags & PERF_FLAG_FD_NO_GROUP)
6407 group_leader = NULL;
6408 }
6409
Stephane Eraniane5d13672011-02-14 11:20:01 +02006410 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006411 task = find_lively_task_by_vpid(pid);
6412 if (IS_ERR(task)) {
6413 err = PTR_ERR(task);
6414 goto err_group_fd;
6415 }
6416 }
6417
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006418 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006419 if (IS_ERR(event)) {
6420 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006421 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02006422 }
6423
Stephane Eraniane5d13672011-02-14 11:20:01 +02006424 if (flags & PERF_FLAG_PID_CGROUP) {
6425 err = perf_cgroup_connect(pid, event, &attr, group_leader);
6426 if (err)
6427 goto err_alloc;
6428 }
6429
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006430 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006431 * Special case software events and allow them to be part of
6432 * any hardware group.
6433 */
6434 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006435
6436 if (group_leader &&
6437 (is_software_event(event) != is_software_event(group_leader))) {
6438 if (is_software_event(event)) {
6439 /*
6440 * If event and group_leader are not both a software
6441 * event, and event is, then group leader is not.
6442 *
6443 * Allow the addition of software events to !software
6444 * groups, this is safe because software events never
6445 * fail to schedule.
6446 */
6447 pmu = group_leader->pmu;
6448 } else if (is_software_event(group_leader) &&
6449 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
6450 /*
6451 * In case the group is a pure software group, and we
6452 * try to add a hardware event, move the whole group to
6453 * the hardware context.
6454 */
6455 move_group = 1;
6456 }
6457 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006458
6459 /*
6460 * Get the target context (task or percpu):
6461 */
Matt Helsley38a81da2010-09-13 13:01:20 -07006462 ctx = find_get_context(pmu, task, cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006463 if (IS_ERR(ctx)) {
6464 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006465 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006466 }
6467
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006468 /*
6469 * Look up the group leader (we will attach this event to it):
6470 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006471 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006472 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006473
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006474 /*
6475 * Do not allow a recursive hierarchy (this new sibling
6476 * becoming part of another group-sibling):
6477 */
6478 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006479 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006480 /*
6481 * Do not allow to attach to a group in a different
6482 * task or CPU context:
6483 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006484 if (move_group) {
6485 if (group_leader->ctx->type != ctx->type)
6486 goto err_context;
6487 } else {
6488 if (group_leader->ctx != ctx)
6489 goto err_context;
6490 }
6491
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006492 /*
6493 * Only a group leader can be exclusive or pinned
6494 */
6495 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006496 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006497 }
6498
6499 if (output_event) {
6500 err = perf_event_set_output(event, output_event);
6501 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006502 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02006503 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006504
Al Viroea635c62010-05-26 17:40:29 -04006505 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
6506 if (IS_ERR(event_file)) {
6507 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006508 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04006509 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006510
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006511 if (move_group) {
6512 struct perf_event_context *gctx = group_leader->ctx;
6513
6514 mutex_lock(&gctx->mutex);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006515 perf_remove_from_context(group_leader);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006516 list_for_each_entry(sibling, &group_leader->sibling_list,
6517 group_entry) {
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006518 perf_remove_from_context(sibling);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006519 put_ctx(gctx);
6520 }
6521 mutex_unlock(&gctx->mutex);
6522 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006523 }
6524
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006525 event->filp = event_file;
6526 WARN_ON_ONCE(ctx->parent_ctx);
6527 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02006528
6529 if (move_group) {
6530 perf_install_in_context(ctx, group_leader, cpu);
6531 get_ctx(ctx);
6532 list_for_each_entry(sibling, &group_leader->sibling_list,
6533 group_entry) {
6534 perf_install_in_context(ctx, sibling, cpu);
6535 get_ctx(ctx);
6536 }
6537 }
6538
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006539 perf_install_in_context(ctx, event, cpu);
6540 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006541 perf_unpin_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006542 mutex_unlock(&ctx->mutex);
6543
6544 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01006545
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006546 mutex_lock(&current->perf_event_mutex);
6547 list_add_tail(&event->owner_entry, &current->perf_event_list);
6548 mutex_unlock(&current->perf_event_mutex);
6549
Peter Zijlstra8a495422010-05-27 15:47:49 +02006550 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006551 * Precalculate sample_data sizes
6552 */
6553 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006554 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006555
6556 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02006557 * Drop the reference on the group_event after placing the
6558 * new event on the sibling_list. This ensures destruction
6559 * of the group leader will find the pointer to itself in
6560 * perf_group_detach().
6561 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006562 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006563 fd_install(event_fd, event_file);
6564 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006565
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006566err_context:
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006567 perf_unpin_context(ctx);
Al Viroea635c62010-05-26 17:40:29 -04006568 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006569err_alloc:
6570 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02006571err_task:
6572 if (task)
6573 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006574err_group_fd:
6575 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006576err_fd:
6577 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006578 return err;
6579}
6580
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006581/**
6582 * perf_event_create_kernel_counter
6583 *
6584 * @attr: attributes of the counter to create
6585 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07006586 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006587 */
6588struct perf_event *
6589perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07006590 struct task_struct *task,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006591 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006592{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006593 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006594 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006595 int err;
6596
6597 /*
6598 * Get the target context (task or percpu):
6599 */
6600
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006601 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006602 if (IS_ERR(event)) {
6603 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006604 goto err;
6605 }
6606
Matt Helsley38a81da2010-09-13 13:01:20 -07006607 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006608 if (IS_ERR(ctx)) {
6609 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006610 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006611 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006612
6613 event->filp = NULL;
6614 WARN_ON_ONCE(ctx->parent_ctx);
6615 mutex_lock(&ctx->mutex);
6616 perf_install_in_context(ctx, event, cpu);
6617 ++ctx->generation;
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006618 perf_unpin_context(ctx);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006619 mutex_unlock(&ctx->mutex);
6620
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006621 return event;
6622
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006623err_free:
6624 free_event(event);
6625err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006626 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006627}
6628EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6629
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006630static void sync_child_event(struct perf_event *child_event,
6631 struct task_struct *child)
6632{
6633 struct perf_event *parent_event = child_event->parent;
6634 u64 child_val;
6635
6636 if (child_event->attr.inherit_stat)
6637 perf_event_read_event(child_event, child);
6638
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006639 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006640
6641 /*
6642 * Add back the child's count to the parent's count:
6643 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02006644 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006645 atomic64_add(child_event->total_time_enabled,
6646 &parent_event->child_total_time_enabled);
6647 atomic64_add(child_event->total_time_running,
6648 &parent_event->child_total_time_running);
6649
6650 /*
6651 * Remove this event from the parent's list
6652 */
6653 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6654 mutex_lock(&parent_event->child_mutex);
6655 list_del_init(&child_event->child_list);
6656 mutex_unlock(&parent_event->child_mutex);
6657
6658 /*
6659 * Release the parent event, if this was the last
6660 * reference to it.
6661 */
6662 fput(parent_event->filp);
6663}
6664
6665static void
6666__perf_event_exit_task(struct perf_event *child_event,
6667 struct perf_event_context *child_ctx,
6668 struct task_struct *child)
6669{
6670 struct perf_event *parent_event;
6671
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006672 perf_remove_from_context(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006673
6674 parent_event = child_event->parent;
6675 /*
6676 * It can happen that parent exits first, and has events
6677 * that are still around due to the child reference. These
6678 * events need to be zapped - but otherwise linger.
6679 */
6680 if (parent_event) {
6681 sync_child_event(child_event, child);
6682 free_event(child_event);
6683 }
6684}
6685
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006686static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006687{
6688 struct perf_event *child_event, *tmp;
6689 struct perf_event_context *child_ctx;
6690 unsigned long flags;
6691
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006692 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006693 perf_event_task(child, NULL, 0);
6694 return;
6695 }
6696
6697 local_irq_save(flags);
6698 /*
6699 * We can't reschedule here because interrupts are disabled,
6700 * and either child is current or it is a task that can't be
6701 * scheduled, so we are now safe from rescheduling changing
6702 * our context.
6703 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01006704 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02006705 task_ctx_sched_out(child_ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006706
6707 /*
6708 * Take the context lock here so that if find_get_context is
6709 * reading child->perf_event_ctxp, we wait until it has
6710 * incremented the context's refcount before we do put_ctx below.
6711 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01006712 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006713 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006714 /*
6715 * If this context is a clone; unclone it so it can't get
6716 * swapped to another process while we're removing all
6717 * the events from it.
6718 */
6719 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01006720 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01006721 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006722
6723 /*
6724 * Report the task dead after unscheduling the events so that we
6725 * won't get any samples after PERF_RECORD_EXIT. We can however still
6726 * get a few PERF_RECORD_READ events.
6727 */
6728 perf_event_task(child, child_ctx, 0);
6729
6730 /*
6731 * We can recurse on the same lock type through:
6732 *
6733 * __perf_event_exit_task()
6734 * sync_child_event()
6735 * fput(parent_event->filp)
6736 * perf_release()
6737 * mutex_lock(&ctx->mutex)
6738 *
6739 * But since its the parent context it won't be the same instance.
6740 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02006741 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006742
6743again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006744 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6745 group_entry)
6746 __perf_event_exit_task(child_event, child_ctx, child);
6747
6748 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006749 group_entry)
6750 __perf_event_exit_task(child_event, child_ctx, child);
6751
6752 /*
6753 * If the last event was a group event, it will have appended all
6754 * its siblings to the list, but we obtained 'tmp' before that which
6755 * will still point to the list head terminating the iteration.
6756 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006757 if (!list_empty(&child_ctx->pinned_groups) ||
6758 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006759 goto again;
6760
6761 mutex_unlock(&child_ctx->mutex);
6762
6763 put_ctx(child_ctx);
6764}
6765
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006766/*
6767 * When a child task exits, feed back event values to parent events.
6768 */
6769void perf_event_exit_task(struct task_struct *child)
6770{
Peter Zijlstra88821352010-11-09 19:01:43 +01006771 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006772 int ctxn;
6773
Peter Zijlstra88821352010-11-09 19:01:43 +01006774 mutex_lock(&child->perf_event_mutex);
6775 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6776 owner_entry) {
6777 list_del_init(&event->owner_entry);
6778
6779 /*
6780 * Ensure the list deletion is visible before we clear
6781 * the owner, closes a race against perf_release() where
6782 * we need to serialize on the owner->perf_event_mutex.
6783 */
6784 smp_wmb();
6785 event->owner = NULL;
6786 }
6787 mutex_unlock(&child->perf_event_mutex);
6788
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006789 for_each_task_context_nr(ctxn)
6790 perf_event_exit_task_context(child, ctxn);
6791}
6792
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006793static void perf_free_event(struct perf_event *event,
6794 struct perf_event_context *ctx)
6795{
6796 struct perf_event *parent = event->parent;
6797
6798 if (WARN_ON_ONCE(!parent))
6799 return;
6800
6801 mutex_lock(&parent->child_mutex);
6802 list_del_init(&event->child_list);
6803 mutex_unlock(&parent->child_mutex);
6804
6805 fput(parent->filp);
6806
Peter Zijlstra8a495422010-05-27 15:47:49 +02006807 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006808 list_del_event(event, ctx);
6809 free_event(event);
6810}
6811
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006812/*
6813 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006814 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006815 */
6816void perf_event_free_task(struct task_struct *task)
6817{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006818 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006819 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006820 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006821
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006822 for_each_task_context_nr(ctxn) {
6823 ctx = task->perf_event_ctxp[ctxn];
6824 if (!ctx)
6825 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006826
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006827 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006828again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006829 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6830 group_entry)
6831 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006832
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006833 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6834 group_entry)
6835 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006836
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006837 if (!list_empty(&ctx->pinned_groups) ||
6838 !list_empty(&ctx->flexible_groups))
6839 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006840
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006841 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006842
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006843 put_ctx(ctx);
6844 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006845}
6846
Peter Zijlstra4e231c72010-09-09 21:01:59 +02006847void perf_event_delayed_put(struct task_struct *task)
6848{
6849 int ctxn;
6850
6851 for_each_task_context_nr(ctxn)
6852 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6853}
6854
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006855/*
6856 * inherit a event from parent task to child task:
6857 */
6858static struct perf_event *
6859inherit_event(struct perf_event *parent_event,
6860 struct task_struct *parent,
6861 struct perf_event_context *parent_ctx,
6862 struct task_struct *child,
6863 struct perf_event *group_leader,
6864 struct perf_event_context *child_ctx)
6865{
6866 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02006867 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006868
6869 /*
6870 * Instead of creating recursive hierarchies of events,
6871 * we link inherited events back to the original parent,
6872 * which has a filp for sure, which we use as the reference
6873 * count:
6874 */
6875 if (parent_event->parent)
6876 parent_event = parent_event->parent;
6877
6878 child_event = perf_event_alloc(&parent_event->attr,
6879 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006880 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006881 group_leader, parent_event,
6882 NULL);
6883 if (IS_ERR(child_event))
6884 return child_event;
6885 get_ctx(child_ctx);
6886
6887 /*
6888 * Make the child state follow the state of the parent event,
6889 * not its attr.disabled bit. We hold the parent's mutex,
6890 * so we won't race with perf_event_{en, dis}able_family.
6891 */
6892 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6893 child_event->state = PERF_EVENT_STATE_INACTIVE;
6894 else
6895 child_event->state = PERF_EVENT_STATE_OFF;
6896
6897 if (parent_event->attr.freq) {
6898 u64 sample_period = parent_event->hw.sample_period;
6899 struct hw_perf_event *hwc = &child_event->hw;
6900
6901 hwc->sample_period = sample_period;
6902 hwc->last_period = sample_period;
6903
6904 local64_set(&hwc->period_left, sample_period);
6905 }
6906
6907 child_event->ctx = child_ctx;
6908 child_event->overflow_handler = parent_event->overflow_handler;
6909
6910 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02006911 * Precalculate sample_data sizes
6912 */
6913 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006914 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02006915
6916 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006917 * Link it up in the child's context:
6918 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02006919 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006920 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02006921 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006922
6923 /*
6924 * Get a reference to the parent filp - we will fput it
6925 * when the child event exits. This is safe to do because
6926 * we are in the parent and we know that the filp still
6927 * exists and has a nonzero count:
6928 */
6929 atomic_long_inc(&parent_event->filp->f_count);
6930
6931 /*
6932 * Link this into the parent event's child list
6933 */
6934 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6935 mutex_lock(&parent_event->child_mutex);
6936 list_add_tail(&child_event->child_list, &parent_event->child_list);
6937 mutex_unlock(&parent_event->child_mutex);
6938
6939 return child_event;
6940}
6941
6942static int inherit_group(struct perf_event *parent_event,
6943 struct task_struct *parent,
6944 struct perf_event_context *parent_ctx,
6945 struct task_struct *child,
6946 struct perf_event_context *child_ctx)
6947{
6948 struct perf_event *leader;
6949 struct perf_event *sub;
6950 struct perf_event *child_ctr;
6951
6952 leader = inherit_event(parent_event, parent, parent_ctx,
6953 child, NULL, child_ctx);
6954 if (IS_ERR(leader))
6955 return PTR_ERR(leader);
6956 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6957 child_ctr = inherit_event(sub, parent, parent_ctx,
6958 child, leader, child_ctx);
6959 if (IS_ERR(child_ctr))
6960 return PTR_ERR(child_ctr);
6961 }
6962 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006963}
6964
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006965static int
6966inherit_task_group(struct perf_event *event, struct task_struct *parent,
6967 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006968 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006969 int *inherited_all)
6970{
6971 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006972 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006973
6974 if (!event->attr.inherit) {
6975 *inherited_all = 0;
6976 return 0;
6977 }
6978
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01006979 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006980 if (!child_ctx) {
6981 /*
6982 * This is executed from the parent task context, so
6983 * inherit events that have been marked for cloning.
6984 * First allocate and initialize a context for the
6985 * child.
6986 */
6987
Peter Zijlstraeb184472010-09-07 15:55:13 +02006988 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006989 if (!child_ctx)
6990 return -ENOMEM;
6991
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006992 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006993 }
6994
6995 ret = inherit_group(event, parent, parent_ctx,
6996 child, child_ctx);
6997
6998 if (ret)
6999 *inherited_all = 0;
7000
7001 return ret;
7002}
7003
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007004/*
7005 * Initialize the perf_event context in task_struct
7006 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007007int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007008{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007009 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007010 struct perf_event_context *cloned_ctx;
7011 struct perf_event *event;
7012 struct task_struct *parent = current;
7013 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007014 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007015 int ret = 0;
7016
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007017 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007018 return 0;
7019
7020 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007021 * If the parent's context is a clone, pin it so it won't get
7022 * swapped under us.
7023 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007024 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007025
7026 /*
7027 * No need to check if parent_ctx != NULL here; since we saw
7028 * it non-NULL earlier, the only reason for it to become NULL
7029 * is if we exit, and since we're currently in the middle of
7030 * a fork we can't be exiting at the same time.
7031 */
7032
7033 /*
7034 * Lock the parent list. No need to lock the child - not PID
7035 * hashed yet and not running, so nobody can access it.
7036 */
7037 mutex_lock(&parent_ctx->mutex);
7038
7039 /*
7040 * We dont have to disable NMIs - we are only looking at
7041 * the list, not manipulating it:
7042 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007043 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007044 ret = inherit_task_group(event, parent, parent_ctx,
7045 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007046 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007047 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007048 }
7049
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007050 /*
7051 * We can't hold ctx->lock when iterating the ->flexible_group list due
7052 * to allocations, but we need to prevent rotation because
7053 * rotate_ctx() will change the list from interrupt context.
7054 */
7055 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7056 parent_ctx->rotate_disable = 1;
7057 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
7058
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007059 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007060 ret = inherit_task_group(event, parent, parent_ctx,
7061 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007062 if (ret)
7063 break;
7064 }
7065
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007066 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
7067 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01007068
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007069 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007070
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01007071 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007072 /*
7073 * Mark the child context as a clone of the parent
7074 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007075 *
7076 * Note that if the parent is a clone, the holding of
7077 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007078 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007079 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007080 if (cloned_ctx) {
7081 child_ctx->parent_ctx = cloned_ctx;
7082 child_ctx->parent_gen = parent_ctx->parent_gen;
7083 } else {
7084 child_ctx->parent_ctx = parent_ctx;
7085 child_ctx->parent_gen = parent_ctx->generation;
7086 }
7087 get_ctx(child_ctx->parent_ctx);
7088 }
7089
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01007090 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007091 mutex_unlock(&parent_ctx->mutex);
7092
7093 perf_unpin_context(parent_ctx);
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007094 put_ctx(parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007095
7096 return ret;
7097}
7098
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007099/*
7100 * Initialize the perf_event context in task_struct
7101 */
7102int perf_event_init_task(struct task_struct *child)
7103{
7104 int ctxn, ret;
7105
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01007106 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
7107 mutex_init(&child->perf_event_mutex);
7108 INIT_LIST_HEAD(&child->perf_event_list);
7109
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02007110 for_each_task_context_nr(ctxn) {
7111 ret = perf_event_init_context(child, ctxn);
7112 if (ret)
7113 return ret;
7114 }
7115
7116 return 0;
7117}
7118
Paul Mackerras220b1402010-03-10 20:45:52 +11007119static void __init perf_event_init_all_cpus(void)
7120{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007121 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11007122 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11007123
7124 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007125 swhash = &per_cpu(swevent_htable, cpu);
7126 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007127 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11007128 }
7129}
7130
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007131static void __cpuinit perf_event_init_cpu(int cpu)
7132{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007133 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007134
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007135 mutex_lock(&swhash->hlist_mutex);
7136 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007137 struct swevent_hlist *hlist;
7138
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007139 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
7140 WARN_ON(!hlist);
7141 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007142 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007143 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007144}
7145
Peter Zijlstrac2774432010-12-08 15:29:02 +01007146#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007147static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007148{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02007149 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
7150
7151 WARN_ON(!irqs_disabled());
7152
7153 list_del_init(&cpuctx->rotation_list);
7154}
7155
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007156static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007157{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007158 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007159 struct perf_event *event, *tmp;
7160
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007161 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02007162
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007163 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007164 __perf_remove_from_context(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01007165 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Peter Zijlstrafe4b04f2011-02-02 13:19:09 +01007166 __perf_remove_from_context(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007167}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007168
7169static void perf_event_exit_cpu_context(int cpu)
7170{
7171 struct perf_event_context *ctx;
7172 struct pmu *pmu;
7173 int idx;
7174
7175 idx = srcu_read_lock(&pmus_srcu);
7176 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02007177 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007178
7179 mutex_lock(&ctx->mutex);
7180 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
7181 mutex_unlock(&ctx->mutex);
7182 }
7183 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007184}
7185
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007186static void perf_event_exit_cpu(int cpu)
7187{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007188 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007189
Peter Zijlstrab28ab832010-09-06 14:48:15 +02007190 mutex_lock(&swhash->hlist_mutex);
7191 swevent_hlist_release(swhash);
7192 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02007193
Peter Zijlstra108b02c2010-09-06 14:32:03 +02007194 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007195}
7196#else
7197static inline void perf_event_exit_cpu(int cpu) { }
7198#endif
7199
Peter Zijlstrac2774432010-12-08 15:29:02 +01007200static int
7201perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
7202{
7203 int cpu;
7204
7205 for_each_online_cpu(cpu)
7206 perf_event_exit_cpu(cpu);
7207
7208 return NOTIFY_OK;
7209}
7210
7211/*
7212 * Run the perf reboot notifier at the very last possible moment so that
7213 * the generic watchdog code runs as long as possible.
7214 */
7215static struct notifier_block perf_reboot_notifier = {
7216 .notifier_call = perf_reboot,
7217 .priority = INT_MIN,
7218};
7219
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007220static int __cpuinit
7221perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
7222{
7223 unsigned int cpu = (long)hcpu;
7224
Peter Zijlstra5e116372010-06-11 13:35:08 +02007225 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007226
7227 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02007228 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007229 perf_event_init_cpu(cpu);
7230 break;
7231
Peter Zijlstra5e116372010-06-11 13:35:08 +02007232 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007233 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007234 perf_event_exit_cpu(cpu);
7235 break;
7236
7237 default:
7238 break;
7239 }
7240
7241 return NOTIFY_OK;
7242}
7243
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007244void __init perf_event_init(void)
7245{
Jason Wessel3c502e72010-11-04 17:33:01 -05007246 int ret;
7247
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007248 idr_init(&pmu_idr);
7249
Paul Mackerras220b1402010-03-10 20:45:52 +11007250 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007251 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01007252 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
7253 perf_pmu_register(&perf_cpu_clock, NULL, -1);
7254 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02007255 perf_tp_register();
7256 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01007257 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05007258
7259 ret = init_hw_breakpoint();
7260 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02007261}
Peter Zijlstraabe43402010-11-17 23:17:37 +01007262
7263static int __init perf_event_sysfs_init(void)
7264{
7265 struct pmu *pmu;
7266 int ret;
7267
7268 mutex_lock(&pmus_lock);
7269
7270 ret = bus_register(&pmu_bus);
7271 if (ret)
7272 goto unlock;
7273
7274 list_for_each_entry(pmu, &pmus, entry) {
7275 if (!pmu->name || pmu->type < 0)
7276 continue;
7277
7278 ret = pmu_dev_alloc(pmu);
7279 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
7280 }
7281 pmu_bus_running = 1;
7282 ret = 0;
7283
7284unlock:
7285 mutex_unlock(&pmus_lock);
7286
7287 return ret;
7288}
7289device_initcall(perf_event_sysfs_init);
Stephane Eraniane5d13672011-02-14 11:20:01 +02007290
7291#ifdef CONFIG_CGROUP_PERF
7292static struct cgroup_subsys_state *perf_cgroup_create(
7293 struct cgroup_subsys *ss, struct cgroup *cont)
7294{
7295 struct perf_cgroup *jc;
7296 struct perf_cgroup_info *t;
7297 int c;
7298
7299 jc = kmalloc(sizeof(*jc), GFP_KERNEL);
7300 if (!jc)
7301 return ERR_PTR(-ENOMEM);
7302
7303 memset(jc, 0, sizeof(*jc));
7304
7305 jc->info = alloc_percpu(struct perf_cgroup_info);
7306 if (!jc->info) {
7307 kfree(jc);
7308 return ERR_PTR(-ENOMEM);
7309 }
7310
7311 for_each_possible_cpu(c) {
7312 t = per_cpu_ptr(jc->info, c);
7313 t->time = 0;
7314 t->timestamp = 0;
7315 }
7316 return &jc->css;
7317}
7318
7319static void perf_cgroup_destroy(struct cgroup_subsys *ss,
7320 struct cgroup *cont)
7321{
7322 struct perf_cgroup *jc;
7323 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id),
7324 struct perf_cgroup, css);
7325 free_percpu(jc->info);
7326 kfree(jc);
7327}
7328
7329static int __perf_cgroup_move(void *info)
7330{
7331 struct task_struct *task = info;
7332 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
7333 return 0;
7334}
7335
7336static void perf_cgroup_move(struct task_struct *task)
7337{
7338 task_function_call(task, __perf_cgroup_move, task);
7339}
7340
7341static void perf_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
7342 struct cgroup *old_cgrp, struct task_struct *task,
7343 bool threadgroup)
7344{
7345 perf_cgroup_move(task);
7346 if (threadgroup) {
7347 struct task_struct *c;
7348 rcu_read_lock();
7349 list_for_each_entry_rcu(c, &task->thread_group, thread_group) {
7350 perf_cgroup_move(c);
7351 }
7352 rcu_read_unlock();
7353 }
7354}
7355
7356static void perf_cgroup_exit(struct cgroup_subsys *ss, struct cgroup *cgrp,
7357 struct cgroup *old_cgrp, struct task_struct *task)
7358{
7359 /*
7360 * cgroup_exit() is called in the copy_process() failure path.
7361 * Ignore this case since the task hasn't ran yet, this avoids
7362 * trying to poke a half freed task state from generic code.
7363 */
7364 if (!(task->flags & PF_EXITING))
7365 return;
7366
7367 perf_cgroup_move(task);
7368}
7369
7370struct cgroup_subsys perf_subsys = {
7371 .name = "perf_event",
7372 .subsys_id = perf_subsys_id,
7373 .create = perf_cgroup_create,
7374 .destroy = perf_cgroup_destroy,
7375 .exit = perf_cgroup_exit,
7376 .attach = perf_cgroup_attach,
7377};
7378#endif /* CONFIG_CGROUP_PERF */