blob: c9f8a757649d1e6e988b8cb4721bebe143e8d29e [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>
16#include <linux/file.h>
17#include <linux/poll.h>
18#include <linux/sysfs.h>
19#include <linux/dcache.h>
20#include <linux/percpu.h>
21#include <linux/ptrace.h>
22#include <linux/vmstat.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020023#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020024#include <linux/hardirq.h>
25#include <linux/rculist.h>
26#include <linux/uaccess.h>
27#include <linux/syscalls.h>
28#include <linux/anon_inodes.h>
29#include <linux/kernel_stat.h>
30#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080031#include <linux/ftrace_event.h>
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020032#include <linux/hw_breakpoint.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020033
34#include <asm/irq_regs.h>
35
36/*
37 * Each CPU has a list of per CPU events:
38 */
Xiao Guangrongaa5452d2009-12-09 11:28:13 +080039static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020040
41int perf_max_events __read_mostly = 1;
42static int perf_reserved_percpu __read_mostly;
43static int perf_overcommit __read_mostly = 1;
44
45static atomic_t nr_events __read_mostly;
46static atomic_t nr_mmap_events __read_mostly;
47static atomic_t nr_comm_events __read_mostly;
48static atomic_t nr_task_events __read_mostly;
49
50/*
51 * perf event paranoia level:
52 * -1 - not paranoid at all
53 * 0 - disallow raw tracepoint access for unpriv
54 * 1 - disallow cpu events for unpriv
55 * 2 - disallow kernel profiling for unpriv
56 */
57int sysctl_perf_event_paranoid __read_mostly = 1;
58
59static inline bool perf_paranoid_tracepoint_raw(void)
60{
61 return sysctl_perf_event_paranoid > -1;
62}
63
64static inline bool perf_paranoid_cpu(void)
65{
66 return sysctl_perf_event_paranoid > 0;
67}
68
69static inline bool perf_paranoid_kernel(void)
70{
71 return sysctl_perf_event_paranoid > 1;
72}
73
74int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
75
76/*
77 * max perf event sample rate
78 */
79int sysctl_perf_event_sample_rate __read_mostly = 100000;
80
81static atomic64_t perf_event_id;
82
83/*
84 * Lock for (sysadmin-configurable) event reservations:
85 */
86static DEFINE_SPINLOCK(perf_resource_lock);
87
88/*
89 * Architecture provided APIs - weak aliases:
90 */
91extern __weak const struct pmu *hw_perf_event_init(struct perf_event *event)
92{
93 return NULL;
94}
95
96void __weak hw_perf_disable(void) { barrier(); }
97void __weak hw_perf_enable(void) { barrier(); }
98
99void __weak hw_perf_event_setup(int cpu) { barrier(); }
100void __weak hw_perf_event_setup_online(int cpu) { barrier(); }
101
102int __weak
103hw_perf_group_sched_in(struct perf_event *group_leader,
104 struct perf_cpu_context *cpuctx,
105 struct perf_event_context *ctx, int cpu)
106{
107 return 0;
108}
109
110void __weak perf_event_print_debug(void) { }
111
112static DEFINE_PER_CPU(int, perf_disable_count);
113
114void __perf_disable(void)
115{
116 __get_cpu_var(perf_disable_count)++;
117}
118
119bool __perf_enable(void)
120{
121 return !--__get_cpu_var(perf_disable_count);
122}
123
124void perf_disable(void)
125{
126 __perf_disable();
127 hw_perf_disable();
128}
129
130void perf_enable(void)
131{
132 if (__perf_enable())
133 hw_perf_enable();
134}
135
136static void get_ctx(struct perf_event_context *ctx)
137{
138 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
139}
140
141static void free_ctx(struct rcu_head *head)
142{
143 struct perf_event_context *ctx;
144
145 ctx = container_of(head, struct perf_event_context, rcu_head);
146 kfree(ctx);
147}
148
149static void put_ctx(struct perf_event_context *ctx)
150{
151 if (atomic_dec_and_test(&ctx->refcount)) {
152 if (ctx->parent_ctx)
153 put_ctx(ctx->parent_ctx);
154 if (ctx->task)
155 put_task_struct(ctx->task);
156 call_rcu(&ctx->rcu_head, free_ctx);
157 }
158}
159
160static void unclone_ctx(struct perf_event_context *ctx)
161{
162 if (ctx->parent_ctx) {
163 put_ctx(ctx->parent_ctx);
164 ctx->parent_ctx = NULL;
165 }
166}
167
168/*
169 * If we inherit events we want to return the parent event id
170 * to userspace.
171 */
172static u64 primary_event_id(struct perf_event *event)
173{
174 u64 id = event->id;
175
176 if (event->parent)
177 id = event->parent->id;
178
179 return id;
180}
181
182/*
183 * Get the perf_event_context for a task and lock it.
184 * This has to cope with with the fact that until it is locked,
185 * the context could get moved to another task.
186 */
187static struct perf_event_context *
188perf_lock_task_context(struct task_struct *task, unsigned long *flags)
189{
190 struct perf_event_context *ctx;
191
192 rcu_read_lock();
193 retry:
194 ctx = rcu_dereference(task->perf_event_ctxp);
195 if (ctx) {
196 /*
197 * If this context is a clone of another, it might
198 * get swapped for another underneath us by
199 * perf_event_task_sched_out, though the
200 * rcu_read_lock() protects us from any context
201 * getting freed. Lock the context and check if it
202 * got swapped before we could get the lock, and retry
203 * if so. If we locked the right context, then it
204 * can't get swapped on us any more.
205 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100206 raw_spin_lock_irqsave(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200207 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100208 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200209 goto retry;
210 }
211
212 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100213 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200214 ctx = NULL;
215 }
216 }
217 rcu_read_unlock();
218 return ctx;
219}
220
221/*
222 * Get the context for a task and increment its pin_count so it
223 * can't get swapped to another task. This also increments its
224 * reference count so that the context can't get freed.
225 */
226static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
227{
228 struct perf_event_context *ctx;
229 unsigned long flags;
230
231 ctx = perf_lock_task_context(task, &flags);
232 if (ctx) {
233 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100234 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200235 }
236 return ctx;
237}
238
239static void perf_unpin_context(struct perf_event_context *ctx)
240{
241 unsigned long flags;
242
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100243 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200244 --ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100245 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200246 put_ctx(ctx);
247}
248
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100249static inline u64 perf_clock(void)
250{
251 return cpu_clock(smp_processor_id());
252}
253
254/*
255 * Update the record of the current time in a context.
256 */
257static void update_context_time(struct perf_event_context *ctx)
258{
259 u64 now = perf_clock();
260
261 ctx->time += now - ctx->timestamp;
262 ctx->timestamp = now;
263}
264
265/*
266 * Update the total_time_enabled and total_time_running fields for a event.
267 */
268static void update_event_times(struct perf_event *event)
269{
270 struct perf_event_context *ctx = event->ctx;
271 u64 run_end;
272
273 if (event->state < PERF_EVENT_STATE_INACTIVE ||
274 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
275 return;
276
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100277 if (ctx->is_active)
278 run_end = ctx->time;
279 else
280 run_end = event->tstamp_stopped;
281
282 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100283
284 if (event->state == PERF_EVENT_STATE_INACTIVE)
285 run_end = event->tstamp_stopped;
286 else
287 run_end = ctx->time;
288
289 event->total_time_running = run_end - event->tstamp_running;
290}
291
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100292static struct list_head *
293ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
294{
295 if (event->attr.pinned)
296 return &ctx->pinned_groups;
297 else
298 return &ctx->flexible_groups;
299}
300
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200301/*
302 * Add a event from the lists for its context.
303 * Must be called with ctx->mutex and ctx->lock held.
304 */
305static void
306list_add_event(struct perf_event *event, struct perf_event_context *ctx)
307{
308 struct perf_event *group_leader = event->group_leader;
309
310 /*
311 * Depending on whether it is a standalone or sibling event,
312 * add it straight to the context's event list, or to the group
313 * leader's sibling list:
314 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100315 if (group_leader == event) {
316 struct list_head *list;
317
318 list = ctx_group_list(event, ctx);
319 list_add_tail(&event->group_entry, list);
320 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200321 list_add_tail(&event->group_entry, &group_leader->sibling_list);
322 group_leader->nr_siblings++;
323 }
324
325 list_add_rcu(&event->event_entry, &ctx->event_list);
326 ctx->nr_events++;
327 if (event->attr.inherit_stat)
328 ctx->nr_stat++;
329}
330
331/*
332 * Remove a event from the lists for its context.
333 * Must be called with ctx->mutex and ctx->lock held.
334 */
335static void
336list_del_event(struct perf_event *event, struct perf_event_context *ctx)
337{
338 struct perf_event *sibling, *tmp;
339
340 if (list_empty(&event->group_entry))
341 return;
342 ctx->nr_events--;
343 if (event->attr.inherit_stat)
344 ctx->nr_stat--;
345
346 list_del_init(&event->group_entry);
347 list_del_rcu(&event->event_entry);
348
349 if (event->group_leader != event)
350 event->group_leader->nr_siblings--;
351
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100352 update_event_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800353
354 /*
355 * If event was in error state, then keep it
356 * that way, otherwise bogus counts will be
357 * returned on read(). The only way to get out
358 * of error state is by explicit re-enabling
359 * of the event
360 */
361 if (event->state > PERF_EVENT_STATE_OFF)
362 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100363
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200364 /*
365 * If this was a group event with sibling events then
366 * upgrade the siblings to singleton events by adding them
367 * to the context list directly:
368 */
369 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100370 struct list_head *list;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200371
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100372 list = ctx_group_list(event, ctx);
373 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200374 sibling->group_leader = sibling;
375 }
376}
377
378static void
379event_sched_out(struct perf_event *event,
380 struct perf_cpu_context *cpuctx,
381 struct perf_event_context *ctx)
382{
383 if (event->state != PERF_EVENT_STATE_ACTIVE)
384 return;
385
386 event->state = PERF_EVENT_STATE_INACTIVE;
387 if (event->pending_disable) {
388 event->pending_disable = 0;
389 event->state = PERF_EVENT_STATE_OFF;
390 }
391 event->tstamp_stopped = ctx->time;
392 event->pmu->disable(event);
393 event->oncpu = -1;
394
395 if (!is_software_event(event))
396 cpuctx->active_oncpu--;
397 ctx->nr_active--;
398 if (event->attr.exclusive || !cpuctx->active_oncpu)
399 cpuctx->exclusive = 0;
400}
401
402static void
403group_sched_out(struct perf_event *group_event,
404 struct perf_cpu_context *cpuctx,
405 struct perf_event_context *ctx)
406{
407 struct perf_event *event;
408
409 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
410 return;
411
412 event_sched_out(group_event, cpuctx, ctx);
413
414 /*
415 * Schedule out siblings (if any):
416 */
417 list_for_each_entry(event, &group_event->sibling_list, group_entry)
418 event_sched_out(event, cpuctx, ctx);
419
420 if (group_event->attr.exclusive)
421 cpuctx->exclusive = 0;
422}
423
424/*
425 * Cross CPU call to remove a performance event
426 *
427 * We disable the event on the hardware level first. After that we
428 * remove it from the context list.
429 */
430static void __perf_event_remove_from_context(void *info)
431{
432 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
433 struct perf_event *event = info;
434 struct perf_event_context *ctx = event->ctx;
435
436 /*
437 * If this is a task context, we need to check whether it is
438 * the current task context of this cpu. If not it has been
439 * scheduled out before the smp call arrived.
440 */
441 if (ctx->task && cpuctx->task_ctx != ctx)
442 return;
443
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100444 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200445 /*
446 * Protect the list operation against NMI by disabling the
447 * events on a global level.
448 */
449 perf_disable();
450
451 event_sched_out(event, cpuctx, ctx);
452
453 list_del_event(event, ctx);
454
455 if (!ctx->task) {
456 /*
457 * Allow more per task events with respect to the
458 * reservation:
459 */
460 cpuctx->max_pertask =
461 min(perf_max_events - ctx->nr_events,
462 perf_max_events - perf_reserved_percpu);
463 }
464
465 perf_enable();
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100466 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200467}
468
469
470/*
471 * Remove the event from a task's (or a CPU's) list of events.
472 *
473 * Must be called with ctx->mutex held.
474 *
475 * CPU events are removed with a smp call. For task events we only
476 * call when the task is on a CPU.
477 *
478 * If event->ctx is a cloned context, callers must make sure that
479 * every task struct that event->ctx->task could possibly point to
480 * remains valid. This is OK when called from perf_release since
481 * that only calls us on the top-level context, which can't be a clone.
482 * When called from perf_event_exit_task, it's OK because the
483 * context has been detached from its task.
484 */
485static void perf_event_remove_from_context(struct perf_event *event)
486{
487 struct perf_event_context *ctx = event->ctx;
488 struct task_struct *task = ctx->task;
489
490 if (!task) {
491 /*
492 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200493 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200494 */
495 smp_call_function_single(event->cpu,
496 __perf_event_remove_from_context,
497 event, 1);
498 return;
499 }
500
501retry:
502 task_oncpu_function_call(task, __perf_event_remove_from_context,
503 event);
504
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100505 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200506 /*
507 * If the context is active we need to retry the smp call.
508 */
509 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100510 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200511 goto retry;
512 }
513
514 /*
515 * The lock prevents that this context is scheduled in so we
516 * can remove the event safely, if the call above did not
517 * succeed.
518 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100519 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200520 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100521 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200522}
523
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200524/*
525 * Update total_time_enabled and total_time_running for all events in a group.
526 */
527static void update_group_times(struct perf_event *leader)
528{
529 struct perf_event *event;
530
531 update_event_times(leader);
532 list_for_each_entry(event, &leader->sibling_list, group_entry)
533 update_event_times(event);
534}
535
536/*
537 * Cross CPU call to disable a performance event
538 */
539static void __perf_event_disable(void *info)
540{
541 struct perf_event *event = info;
542 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
543 struct perf_event_context *ctx = event->ctx;
544
545 /*
546 * If this is a per-task event, need to check whether this
547 * event's task is the current task on this cpu.
548 */
549 if (ctx->task && cpuctx->task_ctx != ctx)
550 return;
551
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100552 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200553
554 /*
555 * If the event is on, turn it off.
556 * If it is in error state, leave it in error state.
557 */
558 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
559 update_context_time(ctx);
560 update_group_times(event);
561 if (event == event->group_leader)
562 group_sched_out(event, cpuctx, ctx);
563 else
564 event_sched_out(event, cpuctx, ctx);
565 event->state = PERF_EVENT_STATE_OFF;
566 }
567
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100568 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200569}
570
571/*
572 * Disable a event.
573 *
574 * If event->ctx is a cloned context, callers must make sure that
575 * every task struct that event->ctx->task could possibly point to
576 * remains valid. This condition is satisifed when called through
577 * perf_event_for_each_child or perf_event_for_each because they
578 * hold the top-level event's child_mutex, so any descendant that
579 * goes to exit will block in sync_child_event.
580 * When called from perf_pending_event it's OK because event->ctx
581 * is the current context on this CPU and preemption is disabled,
582 * hence we can't get into perf_event_task_sched_out for this context.
583 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100584void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200585{
586 struct perf_event_context *ctx = event->ctx;
587 struct task_struct *task = ctx->task;
588
589 if (!task) {
590 /*
591 * Disable the event on the cpu that it's on
592 */
593 smp_call_function_single(event->cpu, __perf_event_disable,
594 event, 1);
595 return;
596 }
597
598 retry:
599 task_oncpu_function_call(task, __perf_event_disable, event);
600
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100601 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200602 /*
603 * If the event is still active, we need to retry the cross-call.
604 */
605 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100606 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200607 goto retry;
608 }
609
610 /*
611 * Since we have the lock this context can't be scheduled
612 * in, so we can change the state safely.
613 */
614 if (event->state == PERF_EVENT_STATE_INACTIVE) {
615 update_group_times(event);
616 event->state = PERF_EVENT_STATE_OFF;
617 }
618
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100619 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200620}
621
622static int
623event_sched_in(struct perf_event *event,
624 struct perf_cpu_context *cpuctx,
625 struct perf_event_context *ctx,
626 int cpu)
627{
628 if (event->state <= PERF_EVENT_STATE_OFF)
629 return 0;
630
631 event->state = PERF_EVENT_STATE_ACTIVE;
632 event->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
633 /*
634 * The new state must be visible before we turn it on in the hardware:
635 */
636 smp_wmb();
637
638 if (event->pmu->enable(event)) {
639 event->state = PERF_EVENT_STATE_INACTIVE;
640 event->oncpu = -1;
641 return -EAGAIN;
642 }
643
644 event->tstamp_running += ctx->time - event->tstamp_stopped;
645
646 if (!is_software_event(event))
647 cpuctx->active_oncpu++;
648 ctx->nr_active++;
649
650 if (event->attr.exclusive)
651 cpuctx->exclusive = 1;
652
653 return 0;
654}
655
656static int
657group_sched_in(struct perf_event *group_event,
658 struct perf_cpu_context *cpuctx,
659 struct perf_event_context *ctx,
660 int cpu)
661{
662 struct perf_event *event, *partial_group;
663 int ret;
664
665 if (group_event->state == PERF_EVENT_STATE_OFF)
666 return 0;
667
668 ret = hw_perf_group_sched_in(group_event, cpuctx, ctx, cpu);
669 if (ret)
670 return ret < 0 ? ret : 0;
671
672 if (event_sched_in(group_event, cpuctx, ctx, cpu))
673 return -EAGAIN;
674
675 /*
676 * Schedule in siblings as one group (if any):
677 */
678 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
679 if (event_sched_in(event, cpuctx, ctx, cpu)) {
680 partial_group = event;
681 goto group_error;
682 }
683 }
684
685 return 0;
686
687group_error:
688 /*
689 * Groups can be scheduled in as one unit only, so undo any
690 * partial group before returning:
691 */
692 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
693 if (event == partial_group)
694 break;
695 event_sched_out(event, cpuctx, ctx);
696 }
697 event_sched_out(group_event, cpuctx, ctx);
698
699 return -EAGAIN;
700}
701
702/*
703 * Return 1 for a group consisting entirely of software events,
704 * 0 if the group contains any hardware events.
705 */
706static int is_software_only_group(struct perf_event *leader)
707{
708 struct perf_event *event;
709
710 if (!is_software_event(leader))
711 return 0;
712
713 list_for_each_entry(event, &leader->sibling_list, group_entry)
714 if (!is_software_event(event))
715 return 0;
716
717 return 1;
718}
719
720/*
721 * Work out whether we can put this event group on the CPU now.
722 */
723static int group_can_go_on(struct perf_event *event,
724 struct perf_cpu_context *cpuctx,
725 int can_add_hw)
726{
727 /*
728 * Groups consisting entirely of software events can always go on.
729 */
730 if (is_software_only_group(event))
731 return 1;
732 /*
733 * If an exclusive group is already on, no other hardware
734 * events can go on.
735 */
736 if (cpuctx->exclusive)
737 return 0;
738 /*
739 * If this group is exclusive and there are already
740 * events on the CPU, it can't go on.
741 */
742 if (event->attr.exclusive && cpuctx->active_oncpu)
743 return 0;
744 /*
745 * Otherwise, try to add it if all previous groups were able
746 * to go on.
747 */
748 return can_add_hw;
749}
750
751static void add_event_to_ctx(struct perf_event *event,
752 struct perf_event_context *ctx)
753{
754 list_add_event(event, ctx);
755 event->tstamp_enabled = ctx->time;
756 event->tstamp_running = ctx->time;
757 event->tstamp_stopped = ctx->time;
758}
759
760/*
761 * Cross CPU call to install and enable a performance event
762 *
763 * Must be called with ctx->mutex held
764 */
765static void __perf_install_in_context(void *info)
766{
767 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
768 struct perf_event *event = info;
769 struct perf_event_context *ctx = event->ctx;
770 struct perf_event *leader = event->group_leader;
771 int cpu = smp_processor_id();
772 int err;
773
774 /*
775 * If this is a task context, we need to check whether it is
776 * the current task context of this cpu. If not it has been
777 * scheduled out before the smp call arrived.
778 * Or possibly this is the right context but it isn't
779 * on this cpu because it had no events.
780 */
781 if (ctx->task && cpuctx->task_ctx != ctx) {
782 if (cpuctx->task_ctx || ctx->task != current)
783 return;
784 cpuctx->task_ctx = ctx;
785 }
786
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100787 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200788 ctx->is_active = 1;
789 update_context_time(ctx);
790
791 /*
792 * Protect the list operation against NMI by disabling the
793 * events on a global level. NOP for non NMI based events.
794 */
795 perf_disable();
796
797 add_event_to_ctx(event, ctx);
798
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100799 if (event->cpu != -1 && event->cpu != smp_processor_id())
800 goto unlock;
801
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200802 /*
803 * Don't put the event on if it is disabled or if
804 * it is in a group and the group isn't on.
805 */
806 if (event->state != PERF_EVENT_STATE_INACTIVE ||
807 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
808 goto unlock;
809
810 /*
811 * An exclusive event can't go on if there are already active
812 * hardware events, and no hardware event can go on if there
813 * is already an exclusive event on.
814 */
815 if (!group_can_go_on(event, cpuctx, 1))
816 err = -EEXIST;
817 else
818 err = event_sched_in(event, cpuctx, ctx, cpu);
819
820 if (err) {
821 /*
822 * This event couldn't go on. If it is in a group
823 * then we have to pull the whole group off.
824 * If the event group is pinned then put it in error state.
825 */
826 if (leader != event)
827 group_sched_out(leader, cpuctx, ctx);
828 if (leader->attr.pinned) {
829 update_group_times(leader);
830 leader->state = PERF_EVENT_STATE_ERROR;
831 }
832 }
833
834 if (!err && !ctx->task && cpuctx->max_pertask)
835 cpuctx->max_pertask--;
836
837 unlock:
838 perf_enable();
839
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100840 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200841}
842
843/*
844 * Attach a performance event to a context
845 *
846 * First we add the event to the list with the hardware enable bit
847 * in event->hw_config cleared.
848 *
849 * If the event is attached to a task which is on a CPU we use a smp
850 * call to enable it in the task context. The task might have been
851 * scheduled away, but we check this in the smp call again.
852 *
853 * Must be called with ctx->mutex held.
854 */
855static void
856perf_install_in_context(struct perf_event_context *ctx,
857 struct perf_event *event,
858 int cpu)
859{
860 struct task_struct *task = ctx->task;
861
862 if (!task) {
863 /*
864 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200865 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200866 */
867 smp_call_function_single(cpu, __perf_install_in_context,
868 event, 1);
869 return;
870 }
871
872retry:
873 task_oncpu_function_call(task, __perf_install_in_context,
874 event);
875
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100876 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200877 /*
878 * we need to retry the smp call.
879 */
880 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100881 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200882 goto retry;
883 }
884
885 /*
886 * The lock prevents that this context is scheduled in so we
887 * can add the event safely, if it the call above did not
888 * succeed.
889 */
890 if (list_empty(&event->group_entry))
891 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100892 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200893}
894
895/*
896 * Put a event into inactive state and update time fields.
897 * Enabling the leader of a group effectively enables all
898 * the group members that aren't explicitly disabled, so we
899 * have to update their ->tstamp_enabled also.
900 * Note: this works for group members as well as group leaders
901 * since the non-leader members' sibling_lists will be empty.
902 */
903static void __perf_event_mark_enabled(struct perf_event *event,
904 struct perf_event_context *ctx)
905{
906 struct perf_event *sub;
907
908 event->state = PERF_EVENT_STATE_INACTIVE;
909 event->tstamp_enabled = ctx->time - event->total_time_enabled;
910 list_for_each_entry(sub, &event->sibling_list, group_entry)
911 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
912 sub->tstamp_enabled =
913 ctx->time - sub->total_time_enabled;
914}
915
916/*
917 * Cross CPU call to enable a performance event
918 */
919static void __perf_event_enable(void *info)
920{
921 struct perf_event *event = info;
922 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
923 struct perf_event_context *ctx = event->ctx;
924 struct perf_event *leader = event->group_leader;
925 int err;
926
927 /*
928 * If this is a per-task event, need to check whether this
929 * event's task is the current task on this cpu.
930 */
931 if (ctx->task && cpuctx->task_ctx != ctx) {
932 if (cpuctx->task_ctx || ctx->task != current)
933 return;
934 cpuctx->task_ctx = ctx;
935 }
936
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100937 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200938 ctx->is_active = 1;
939 update_context_time(ctx);
940
941 if (event->state >= PERF_EVENT_STATE_INACTIVE)
942 goto unlock;
943 __perf_event_mark_enabled(event, ctx);
944
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100945 if (event->cpu != -1 && event->cpu != smp_processor_id())
946 goto unlock;
947
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200948 /*
949 * If the event is in a group and isn't the group leader,
950 * then don't put it on unless the group is on.
951 */
952 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
953 goto unlock;
954
955 if (!group_can_go_on(event, cpuctx, 1)) {
956 err = -EEXIST;
957 } else {
958 perf_disable();
959 if (event == leader)
960 err = group_sched_in(event, cpuctx, ctx,
961 smp_processor_id());
962 else
963 err = event_sched_in(event, cpuctx, ctx,
964 smp_processor_id());
965 perf_enable();
966 }
967
968 if (err) {
969 /*
970 * If this event can't go on and it's part of a
971 * group, then the whole group has to come off.
972 */
973 if (leader != event)
974 group_sched_out(leader, cpuctx, ctx);
975 if (leader->attr.pinned) {
976 update_group_times(leader);
977 leader->state = PERF_EVENT_STATE_ERROR;
978 }
979 }
980
981 unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100982 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200983}
984
985/*
986 * Enable a event.
987 *
988 * If event->ctx is a cloned context, callers must make sure that
989 * every task struct that event->ctx->task could possibly point to
990 * remains valid. This condition is satisfied when called through
991 * perf_event_for_each_child or perf_event_for_each as described
992 * for perf_event_disable.
993 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100994void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200995{
996 struct perf_event_context *ctx = event->ctx;
997 struct task_struct *task = ctx->task;
998
999 if (!task) {
1000 /*
1001 * Enable the event on the cpu that it's on
1002 */
1003 smp_call_function_single(event->cpu, __perf_event_enable,
1004 event, 1);
1005 return;
1006 }
1007
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001008 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001009 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1010 goto out;
1011
1012 /*
1013 * If the event is in error state, clear that first.
1014 * That way, if we see the event in error state below, we
1015 * know that it has gone back into error state, as distinct
1016 * from the task having been scheduled away before the
1017 * cross-call arrived.
1018 */
1019 if (event->state == PERF_EVENT_STATE_ERROR)
1020 event->state = PERF_EVENT_STATE_OFF;
1021
1022 retry:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001023 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001024 task_oncpu_function_call(task, __perf_event_enable, event);
1025
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001026 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001027
1028 /*
1029 * If the context is active and the event is still off,
1030 * we need to retry the cross-call.
1031 */
1032 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1033 goto retry;
1034
1035 /*
1036 * Since we have the lock this context can't be scheduled
1037 * in, so we can change the state safely.
1038 */
1039 if (event->state == PERF_EVENT_STATE_OFF)
1040 __perf_event_mark_enabled(event, ctx);
1041
1042 out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001043 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001044}
1045
1046static int perf_event_refresh(struct perf_event *event, int refresh)
1047{
1048 /*
1049 * not supported on inherited events
1050 */
1051 if (event->attr.inherit)
1052 return -EINVAL;
1053
1054 atomic_add(refresh, &event->event_limit);
1055 perf_event_enable(event);
1056
1057 return 0;
1058}
1059
1060void __perf_event_sched_out(struct perf_event_context *ctx,
1061 struct perf_cpu_context *cpuctx)
1062{
1063 struct perf_event *event;
1064
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001065 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001066 ctx->is_active = 0;
1067 if (likely(!ctx->nr_events))
1068 goto out;
1069 update_context_time(ctx);
1070
1071 perf_disable();
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +01001072 if (ctx->nr_active) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001073 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1074 group_sched_out(event, cpuctx, ctx);
1075
1076 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001077 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +01001078 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001079 perf_enable();
1080 out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001081 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001082}
1083
1084/*
1085 * Test whether two contexts are equivalent, i.e. whether they
1086 * have both been cloned from the same version of the same context
1087 * and they both have the same number of enabled events.
1088 * If the number of enabled events is the same, then the set
1089 * of enabled events should be the same, because these are both
1090 * inherited contexts, therefore we can't access individual events
1091 * in them directly with an fd; we can only enable/disable all
1092 * events via prctl, or enable/disable all events in a family
1093 * via ioctl, which will have the same effect on both contexts.
1094 */
1095static int context_equiv(struct perf_event_context *ctx1,
1096 struct perf_event_context *ctx2)
1097{
1098 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1099 && ctx1->parent_gen == ctx2->parent_gen
1100 && !ctx1->pin_count && !ctx2->pin_count;
1101}
1102
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001103static void __perf_event_sync_stat(struct perf_event *event,
1104 struct perf_event *next_event)
1105{
1106 u64 value;
1107
1108 if (!event->attr.inherit_stat)
1109 return;
1110
1111 /*
1112 * Update the event value, we cannot use perf_event_read()
1113 * because we're in the middle of a context switch and have IRQs
1114 * disabled, which upsets smp_call_function_single(), however
1115 * we know the event must be on the current CPU, therefore we
1116 * don't need to use it.
1117 */
1118 switch (event->state) {
1119 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001120 event->pmu->read(event);
1121 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001122
1123 case PERF_EVENT_STATE_INACTIVE:
1124 update_event_times(event);
1125 break;
1126
1127 default:
1128 break;
1129 }
1130
1131 /*
1132 * In order to keep per-task stats reliable we need to flip the event
1133 * values when we flip the contexts.
1134 */
1135 value = atomic64_read(&next_event->count);
1136 value = atomic64_xchg(&event->count, value);
1137 atomic64_set(&next_event->count, value);
1138
1139 swap(event->total_time_enabled, next_event->total_time_enabled);
1140 swap(event->total_time_running, next_event->total_time_running);
1141
1142 /*
1143 * Since we swizzled the values, update the user visible data too.
1144 */
1145 perf_event_update_userpage(event);
1146 perf_event_update_userpage(next_event);
1147}
1148
1149#define list_next_entry(pos, member) \
1150 list_entry(pos->member.next, typeof(*pos), member)
1151
1152static void perf_event_sync_stat(struct perf_event_context *ctx,
1153 struct perf_event_context *next_ctx)
1154{
1155 struct perf_event *event, *next_event;
1156
1157 if (!ctx->nr_stat)
1158 return;
1159
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001160 update_context_time(ctx);
1161
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001162 event = list_first_entry(&ctx->event_list,
1163 struct perf_event, event_entry);
1164
1165 next_event = list_first_entry(&next_ctx->event_list,
1166 struct perf_event, event_entry);
1167
1168 while (&event->event_entry != &ctx->event_list &&
1169 &next_event->event_entry != &next_ctx->event_list) {
1170
1171 __perf_event_sync_stat(event, next_event);
1172
1173 event = list_next_entry(event, event_entry);
1174 next_event = list_next_entry(next_event, event_entry);
1175 }
1176}
1177
1178/*
1179 * Called from scheduler to remove the events of the current task,
1180 * with interrupts disabled.
1181 *
1182 * We stop each event and update the event value in event->count.
1183 *
1184 * This does not protect us against NMI, but disable()
1185 * sets the disabled bit in the control field of event _before_
1186 * accessing the event control register. If a NMI hits, then it will
1187 * not restart the event.
1188 */
1189void perf_event_task_sched_out(struct task_struct *task,
Peter Zijlstra49f47432009-12-27 11:51:52 +01001190 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001191{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001192 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001193 struct perf_event_context *ctx = task->perf_event_ctxp;
1194 struct perf_event_context *next_ctx;
1195 struct perf_event_context *parent;
1196 struct pt_regs *regs;
1197 int do_switch = 1;
1198
1199 regs = task_pt_regs(task);
1200 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
1201
1202 if (likely(!ctx || !cpuctx->task_ctx))
1203 return;
1204
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001205 rcu_read_lock();
1206 parent = rcu_dereference(ctx->parent_ctx);
1207 next_ctx = next->perf_event_ctxp;
1208 if (parent && next_ctx &&
1209 rcu_dereference(next_ctx->parent_ctx) == parent) {
1210 /*
1211 * Looks like the two contexts are clones, so we might be
1212 * able to optimize the context switch. We lock both
1213 * contexts and check that they are clones under the
1214 * lock (including re-checking that neither has been
1215 * uncloned in the meantime). It doesn't matter which
1216 * order we take the locks because no other cpu could
1217 * be trying to lock both of these tasks.
1218 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001219 raw_spin_lock(&ctx->lock);
1220 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001221 if (context_equiv(ctx, next_ctx)) {
1222 /*
1223 * XXX do we need a memory barrier of sorts
1224 * wrt to rcu_dereference() of perf_event_ctxp
1225 */
1226 task->perf_event_ctxp = next_ctx;
1227 next->perf_event_ctxp = ctx;
1228 ctx->task = next;
1229 next_ctx->task = task;
1230 do_switch = 0;
1231
1232 perf_event_sync_stat(ctx, next_ctx);
1233 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001234 raw_spin_unlock(&next_ctx->lock);
1235 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001236 }
1237 rcu_read_unlock();
1238
1239 if (do_switch) {
1240 __perf_event_sched_out(ctx, cpuctx);
1241 cpuctx->task_ctx = NULL;
1242 }
1243}
1244
1245/*
1246 * Called with IRQs disabled
1247 */
1248static void __perf_event_task_sched_out(struct perf_event_context *ctx)
1249{
1250 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1251
1252 if (!cpuctx->task_ctx)
1253 return;
1254
1255 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1256 return;
1257
1258 __perf_event_sched_out(ctx, cpuctx);
1259 cpuctx->task_ctx = NULL;
1260}
1261
1262/*
1263 * Called with IRQs disabled
1264 */
1265static void perf_event_cpu_sched_out(struct perf_cpu_context *cpuctx)
1266{
1267 __perf_event_sched_out(&cpuctx->ctx, cpuctx);
1268}
1269
1270static void
1271__perf_event_sched_in(struct perf_event_context *ctx,
Peter Zijlstra49f47432009-12-27 11:51:52 +01001272 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001273{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001274 int cpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001275 struct perf_event *event;
1276 int can_add_hw = 1;
1277
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001278 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001279 ctx->is_active = 1;
1280 if (likely(!ctx->nr_events))
1281 goto out;
1282
1283 ctx->timestamp = perf_clock();
1284
1285 perf_disable();
1286
1287 /*
1288 * First go through the list and put on any pinned groups
1289 * in order to give them the best chance of going on.
1290 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001291 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1292 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001293 continue;
1294 if (event->cpu != -1 && event->cpu != cpu)
1295 continue;
1296
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001297 if (group_can_go_on(event, cpuctx, 1))
1298 group_sched_in(event, cpuctx, ctx, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001299
1300 /*
1301 * If this pinned group hasn't been scheduled,
1302 * put it in error state.
1303 */
1304 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1305 update_group_times(event);
1306 event->state = PERF_EVENT_STATE_ERROR;
1307 }
1308 }
1309
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001310 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1311 /* Ignore events in OFF or ERROR state */
1312 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001313 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001314 /*
1315 * Listen to the 'cpu' scheduling filter constraint
1316 * of events:
1317 */
1318 if (event->cpu != -1 && event->cpu != cpu)
1319 continue;
1320
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001321 if (group_can_go_on(event, cpuctx, can_add_hw))
1322 if (group_sched_in(event, cpuctx, ctx, cpu))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001323 can_add_hw = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001324 }
1325 perf_enable();
1326 out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001327 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001328}
1329
1330/*
1331 * Called from scheduler to add the events of the current task
1332 * with interrupts disabled.
1333 *
1334 * We restore the event value and then enable it.
1335 *
1336 * This does not protect us against NMI, but enable()
1337 * sets the enabled bit in the control field of event _before_
1338 * accessing the event control register. If a NMI hits, then it will
1339 * keep the event running.
1340 */
Peter Zijlstra49f47432009-12-27 11:51:52 +01001341void perf_event_task_sched_in(struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001342{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001343 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001344 struct perf_event_context *ctx = task->perf_event_ctxp;
1345
1346 if (likely(!ctx))
1347 return;
1348 if (cpuctx->task_ctx == ctx)
1349 return;
Peter Zijlstra49f47432009-12-27 11:51:52 +01001350 __perf_event_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001351 cpuctx->task_ctx = ctx;
1352}
1353
Peter Zijlstra49f47432009-12-27 11:51:52 +01001354static void perf_event_cpu_sched_in(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001355{
1356 struct perf_event_context *ctx = &cpuctx->ctx;
1357
Peter Zijlstra49f47432009-12-27 11:51:52 +01001358 __perf_event_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001359}
1360
1361#define MAX_INTERRUPTS (~0ULL)
1362
1363static void perf_log_throttle(struct perf_event *event, int enable);
1364
1365static void perf_adjust_period(struct perf_event *event, u64 events)
1366{
1367 struct hw_perf_event *hwc = &event->hw;
1368 u64 period, sample_period;
1369 s64 delta;
1370
1371 events *= hwc->sample_period;
1372 period = div64_u64(events, event->attr.sample_freq);
1373
1374 delta = (s64)(period - hwc->sample_period);
1375 delta = (delta + 7) / 8; /* low pass filter */
1376
1377 sample_period = hwc->sample_period + delta;
1378
1379 if (!sample_period)
1380 sample_period = 1;
1381
1382 hwc->sample_period = sample_period;
1383}
1384
1385static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1386{
1387 struct perf_event *event;
1388 struct hw_perf_event *hwc;
1389 u64 interrupts, freq;
1390
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001391 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001392 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001393 if (event->state != PERF_EVENT_STATE_ACTIVE)
1394 continue;
1395
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001396 if (event->cpu != -1 && event->cpu != smp_processor_id())
1397 continue;
1398
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001399 hwc = &event->hw;
1400
1401 interrupts = hwc->interrupts;
1402 hwc->interrupts = 0;
1403
1404 /*
1405 * unthrottle events on the tick
1406 */
1407 if (interrupts == MAX_INTERRUPTS) {
1408 perf_log_throttle(event, 1);
1409 event->pmu->unthrottle(event);
1410 interrupts = 2*sysctl_perf_event_sample_rate/HZ;
1411 }
1412
1413 if (!event->attr.freq || !event->attr.sample_freq)
1414 continue;
1415
1416 /*
1417 * if the specified freq < HZ then we need to skip ticks
1418 */
1419 if (event->attr.sample_freq < HZ) {
1420 freq = event->attr.sample_freq;
1421
1422 hwc->freq_count += freq;
1423 hwc->freq_interrupts += interrupts;
1424
1425 if (hwc->freq_count < HZ)
1426 continue;
1427
1428 interrupts = hwc->freq_interrupts;
1429 hwc->freq_interrupts = 0;
1430 hwc->freq_count -= HZ;
1431 } else
1432 freq = HZ;
1433
1434 perf_adjust_period(event, freq * interrupts);
1435
1436 /*
1437 * In order to avoid being stalled by an (accidental) huge
1438 * sample period, force reset the sample period if we didn't
1439 * get any events in this freq period.
1440 */
1441 if (!interrupts) {
1442 perf_disable();
1443 event->pmu->disable(event);
1444 atomic64_set(&hwc->period_left, 0);
1445 event->pmu->enable(event);
1446 perf_enable();
1447 }
1448 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001449 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001450}
1451
1452/*
1453 * Round-robin a context's events:
1454 */
1455static void rotate_ctx(struct perf_event_context *ctx)
1456{
1457 struct perf_event *event;
1458
1459 if (!ctx->nr_events)
1460 return;
1461
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001462 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001463 /*
1464 * Rotate the first entry last (works just fine for group events too):
1465 */
1466 perf_disable();
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001467 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1468 list_move_tail(&event->group_entry, &ctx->pinned_groups);
1469 break;
1470 }
1471
1472 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1473 list_move_tail(&event->group_entry, &ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001474 break;
1475 }
1476 perf_enable();
1477
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001478 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001479}
1480
Peter Zijlstra49f47432009-12-27 11:51:52 +01001481void perf_event_task_tick(struct task_struct *curr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001482{
1483 struct perf_cpu_context *cpuctx;
1484 struct perf_event_context *ctx;
1485
1486 if (!atomic_read(&nr_events))
1487 return;
1488
Peter Zijlstra49f47432009-12-27 11:51:52 +01001489 cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001490 ctx = curr->perf_event_ctxp;
1491
1492 perf_ctx_adjust_freq(&cpuctx->ctx);
1493 if (ctx)
1494 perf_ctx_adjust_freq(ctx);
1495
1496 perf_event_cpu_sched_out(cpuctx);
1497 if (ctx)
1498 __perf_event_task_sched_out(ctx);
1499
1500 rotate_ctx(&cpuctx->ctx);
1501 if (ctx)
1502 rotate_ctx(ctx);
1503
Peter Zijlstra49f47432009-12-27 11:51:52 +01001504 perf_event_cpu_sched_in(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001505 if (ctx)
Peter Zijlstra49f47432009-12-27 11:51:52 +01001506 perf_event_task_sched_in(curr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001507}
1508
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001509static int event_enable_on_exec(struct perf_event *event,
1510 struct perf_event_context *ctx)
1511{
1512 if (!event->attr.enable_on_exec)
1513 return 0;
1514
1515 event->attr.enable_on_exec = 0;
1516 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1517 return 0;
1518
1519 __perf_event_mark_enabled(event, ctx);
1520
1521 return 1;
1522}
1523
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001524/*
1525 * Enable all of a task's events that have been marked enable-on-exec.
1526 * This expects task == current.
1527 */
1528static void perf_event_enable_on_exec(struct task_struct *task)
1529{
1530 struct perf_event_context *ctx;
1531 struct perf_event *event;
1532 unsigned long flags;
1533 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001534 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001535
1536 local_irq_save(flags);
1537 ctx = task->perf_event_ctxp;
1538 if (!ctx || !ctx->nr_events)
1539 goto out;
1540
1541 __perf_event_task_sched_out(ctx);
1542
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001543 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001544
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001545 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1546 ret = event_enable_on_exec(event, ctx);
1547 if (ret)
1548 enabled = 1;
1549 }
1550
1551 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1552 ret = event_enable_on_exec(event, ctx);
1553 if (ret)
1554 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001555 }
1556
1557 /*
1558 * Unclone this context if we enabled any event.
1559 */
1560 if (enabled)
1561 unclone_ctx(ctx);
1562
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001563 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001564
Peter Zijlstra49f47432009-12-27 11:51:52 +01001565 perf_event_task_sched_in(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001566 out:
1567 local_irq_restore(flags);
1568}
1569
1570/*
1571 * Cross CPU call to read the hardware event
1572 */
1573static void __perf_event_read(void *info)
1574{
1575 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1576 struct perf_event *event = info;
1577 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001578
1579 /*
1580 * If this is a task context, we need to check whether it is
1581 * the current task context of this cpu. If not it has been
1582 * scheduled out before the smp call arrived. In that case
1583 * event->count would have been updated to a recent sample
1584 * when the event was scheduled out.
1585 */
1586 if (ctx->task && cpuctx->task_ctx != ctx)
1587 return;
1588
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001589 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001590 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001591 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001592 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001593
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001594 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001595}
1596
1597static u64 perf_event_read(struct perf_event *event)
1598{
1599 /*
1600 * If event is enabled and currently active on a CPU, update the
1601 * value in the event structure:
1602 */
1603 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1604 smp_call_function_single(event->oncpu,
1605 __perf_event_read, event, 1);
1606 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001607 struct perf_event_context *ctx = event->ctx;
1608 unsigned long flags;
1609
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001610 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001611 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001612 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001613 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001614 }
1615
1616 return atomic64_read(&event->count);
1617}
1618
1619/*
1620 * Initialize the perf_event context in a task_struct:
1621 */
1622static void
1623__perf_event_init_context(struct perf_event_context *ctx,
1624 struct task_struct *task)
1625{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001626 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001627 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001628 INIT_LIST_HEAD(&ctx->pinned_groups);
1629 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001630 INIT_LIST_HEAD(&ctx->event_list);
1631 atomic_set(&ctx->refcount, 1);
1632 ctx->task = task;
1633}
1634
1635static struct perf_event_context *find_get_context(pid_t pid, int cpu)
1636{
1637 struct perf_event_context *ctx;
1638 struct perf_cpu_context *cpuctx;
1639 struct task_struct *task;
1640 unsigned long flags;
1641 int err;
1642
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001643 if (pid == -1 && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001644 /* Must be root to operate on a CPU event: */
1645 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1646 return ERR_PTR(-EACCES);
1647
Paul Mackerras0f624e72009-12-15 19:40:32 +11001648 if (cpu < 0 || cpu >= nr_cpumask_bits)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001649 return ERR_PTR(-EINVAL);
1650
1651 /*
1652 * We could be clever and allow to attach a event to an
1653 * offline CPU and activate it when the CPU comes up, but
1654 * that's for later.
1655 */
Rusty Russellf6325e32009-12-17 11:43:08 -06001656 if (!cpu_online(cpu))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001657 return ERR_PTR(-ENODEV);
1658
1659 cpuctx = &per_cpu(perf_cpu_context, cpu);
1660 ctx = &cpuctx->ctx;
1661 get_ctx(ctx);
1662
1663 return ctx;
1664 }
1665
1666 rcu_read_lock();
1667 if (!pid)
1668 task = current;
1669 else
1670 task = find_task_by_vpid(pid);
1671 if (task)
1672 get_task_struct(task);
1673 rcu_read_unlock();
1674
1675 if (!task)
1676 return ERR_PTR(-ESRCH);
1677
1678 /*
1679 * Can't attach events to a dying task.
1680 */
1681 err = -ESRCH;
1682 if (task->flags & PF_EXITING)
1683 goto errout;
1684
1685 /* Reuse ptrace permission checks for now. */
1686 err = -EACCES;
1687 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1688 goto errout;
1689
1690 retry:
1691 ctx = perf_lock_task_context(task, &flags);
1692 if (ctx) {
1693 unclone_ctx(ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001694 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001695 }
1696
1697 if (!ctx) {
Xiao Guangrongaa5452d2009-12-09 11:28:13 +08001698 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001699 err = -ENOMEM;
1700 if (!ctx)
1701 goto errout;
1702 __perf_event_init_context(ctx, task);
1703 get_ctx(ctx);
1704 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
1705 /*
1706 * We raced with some other task; use
1707 * the context they set.
1708 */
1709 kfree(ctx);
1710 goto retry;
1711 }
1712 get_task_struct(task);
1713 }
1714
1715 put_task_struct(task);
1716 return ctx;
1717
1718 errout:
1719 put_task_struct(task);
1720 return ERR_PTR(err);
1721}
1722
Li Zefan6fb29152009-10-15 11:21:42 +08001723static void perf_event_free_filter(struct perf_event *event);
1724
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001725static void free_event_rcu(struct rcu_head *head)
1726{
1727 struct perf_event *event;
1728
1729 event = container_of(head, struct perf_event, rcu_head);
1730 if (event->ns)
1731 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08001732 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001733 kfree(event);
1734}
1735
1736static void perf_pending_sync(struct perf_event *event);
1737
1738static void free_event(struct perf_event *event)
1739{
1740 perf_pending_sync(event);
1741
1742 if (!event->parent) {
1743 atomic_dec(&nr_events);
1744 if (event->attr.mmap)
1745 atomic_dec(&nr_mmap_events);
1746 if (event->attr.comm)
1747 atomic_dec(&nr_comm_events);
1748 if (event->attr.task)
1749 atomic_dec(&nr_task_events);
1750 }
1751
1752 if (event->output) {
1753 fput(event->output->filp);
1754 event->output = NULL;
1755 }
1756
1757 if (event->destroy)
1758 event->destroy(event);
1759
1760 put_ctx(event->ctx);
1761 call_rcu(&event->rcu_head, free_event_rcu);
1762}
1763
Arjan van de Venfb0459d2009-09-25 12:25:56 +02001764int perf_event_release_kernel(struct perf_event *event)
1765{
1766 struct perf_event_context *ctx = event->ctx;
1767
1768 WARN_ON_ONCE(ctx->parent_ctx);
1769 mutex_lock(&ctx->mutex);
1770 perf_event_remove_from_context(event);
1771 mutex_unlock(&ctx->mutex);
1772
1773 mutex_lock(&event->owner->perf_event_mutex);
1774 list_del_init(&event->owner_entry);
1775 mutex_unlock(&event->owner->perf_event_mutex);
1776 put_task_struct(event->owner);
1777
1778 free_event(event);
1779
1780 return 0;
1781}
1782EXPORT_SYMBOL_GPL(perf_event_release_kernel);
1783
Peter Zijlstraa66a3052009-11-23 11:37:23 +01001784/*
1785 * Called when the last reference to the file is gone.
1786 */
1787static int perf_release(struct inode *inode, struct file *file)
1788{
1789 struct perf_event *event = file->private_data;
1790
1791 file->private_data = NULL;
1792
1793 return perf_event_release_kernel(event);
1794}
1795
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001796static int perf_event_read_size(struct perf_event *event)
1797{
1798 int entry = sizeof(u64); /* value */
1799 int size = 0;
1800 int nr = 1;
1801
1802 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1803 size += sizeof(u64);
1804
1805 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1806 size += sizeof(u64);
1807
1808 if (event->attr.read_format & PERF_FORMAT_ID)
1809 entry += sizeof(u64);
1810
1811 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1812 nr += event->group_leader->nr_siblings;
1813 size += sizeof(u64);
1814 }
1815
1816 size += entry * nr;
1817
1818 return size;
1819}
1820
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001821u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001822{
1823 struct perf_event *child;
1824 u64 total = 0;
1825
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001826 *enabled = 0;
1827 *running = 0;
1828
Peter Zijlstra6f105812009-11-20 22:19:56 +01001829 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001830 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001831 *enabled += event->total_time_enabled +
1832 atomic64_read(&event->child_total_time_enabled);
1833 *running += event->total_time_running +
1834 atomic64_read(&event->child_total_time_running);
1835
1836 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001837 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001838 *enabled += child->total_time_enabled;
1839 *running += child->total_time_running;
1840 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01001841 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001842
1843 return total;
1844}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02001845EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001846
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001847static int perf_event_read_group(struct perf_event *event,
1848 u64 read_format, char __user *buf)
1849{
1850 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01001851 int n = 0, size = 0, ret = -EFAULT;
1852 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01001853 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001854 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01001855
Peter Zijlstra6f105812009-11-20 22:19:56 +01001856 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001857 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001858
1859 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001860 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1861 values[n++] = enabled;
1862 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1863 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01001864 values[n++] = count;
1865 if (read_format & PERF_FORMAT_ID)
1866 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001867
1868 size = n * sizeof(u64);
1869
1870 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01001871 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001872
Peter Zijlstra6f105812009-11-20 22:19:56 +01001873 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001874
1875 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01001876 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001877
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001878 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01001879 if (read_format & PERF_FORMAT_ID)
1880 values[n++] = primary_event_id(sub);
1881
1882 size = n * sizeof(u64);
1883
Stephane Eranian184d3da2009-11-23 21:40:49 -08001884 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01001885 ret = -EFAULT;
1886 goto unlock;
1887 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01001888
1889 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001890 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01001891unlock:
1892 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001893
Peter Zijlstraabf48682009-11-20 22:19:49 +01001894 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001895}
1896
1897static int perf_event_read_one(struct perf_event *event,
1898 u64 read_format, char __user *buf)
1899{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001900 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001901 u64 values[4];
1902 int n = 0;
1903
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001904 values[n++] = perf_event_read_value(event, &enabled, &running);
1905 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1906 values[n++] = enabled;
1907 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1908 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001909 if (read_format & PERF_FORMAT_ID)
1910 values[n++] = primary_event_id(event);
1911
1912 if (copy_to_user(buf, values, n * sizeof(u64)))
1913 return -EFAULT;
1914
1915 return n * sizeof(u64);
1916}
1917
1918/*
1919 * Read the performance event - simple non blocking version for now
1920 */
1921static ssize_t
1922perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
1923{
1924 u64 read_format = event->attr.read_format;
1925 int ret;
1926
1927 /*
1928 * Return end-of-file for a read on a event that is in
1929 * error state (i.e. because it was pinned but it couldn't be
1930 * scheduled on to the CPU at some point).
1931 */
1932 if (event->state == PERF_EVENT_STATE_ERROR)
1933 return 0;
1934
1935 if (count < perf_event_read_size(event))
1936 return -ENOSPC;
1937
1938 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001939 if (read_format & PERF_FORMAT_GROUP)
1940 ret = perf_event_read_group(event, read_format, buf);
1941 else
1942 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001943
1944 return ret;
1945}
1946
1947static ssize_t
1948perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1949{
1950 struct perf_event *event = file->private_data;
1951
1952 return perf_read_hw(event, buf, count);
1953}
1954
1955static unsigned int perf_poll(struct file *file, poll_table *wait)
1956{
1957 struct perf_event *event = file->private_data;
1958 struct perf_mmap_data *data;
1959 unsigned int events = POLL_HUP;
1960
1961 rcu_read_lock();
1962 data = rcu_dereference(event->data);
1963 if (data)
1964 events = atomic_xchg(&data->poll, 0);
1965 rcu_read_unlock();
1966
1967 poll_wait(file, &event->waitq, wait);
1968
1969 return events;
1970}
1971
1972static void perf_event_reset(struct perf_event *event)
1973{
1974 (void)perf_event_read(event);
1975 atomic64_set(&event->count, 0);
1976 perf_event_update_userpage(event);
1977}
1978
1979/*
1980 * Holding the top-level event's child_mutex means that any
1981 * descendant process that has inherited this event will block
1982 * in sync_child_event if it goes to exit, thus satisfying the
1983 * task existence requirements of perf_event_enable/disable.
1984 */
1985static void perf_event_for_each_child(struct perf_event *event,
1986 void (*func)(struct perf_event *))
1987{
1988 struct perf_event *child;
1989
1990 WARN_ON_ONCE(event->ctx->parent_ctx);
1991 mutex_lock(&event->child_mutex);
1992 func(event);
1993 list_for_each_entry(child, &event->child_list, child_list)
1994 func(child);
1995 mutex_unlock(&event->child_mutex);
1996}
1997
1998static void perf_event_for_each(struct perf_event *event,
1999 void (*func)(struct perf_event *))
2000{
2001 struct perf_event_context *ctx = event->ctx;
2002 struct perf_event *sibling;
2003
2004 WARN_ON_ONCE(ctx->parent_ctx);
2005 mutex_lock(&ctx->mutex);
2006 event = event->group_leader;
2007
2008 perf_event_for_each_child(event, func);
2009 func(event);
2010 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2011 perf_event_for_each_child(event, func);
2012 mutex_unlock(&ctx->mutex);
2013}
2014
2015static int perf_event_period(struct perf_event *event, u64 __user *arg)
2016{
2017 struct perf_event_context *ctx = event->ctx;
2018 unsigned long size;
2019 int ret = 0;
2020 u64 value;
2021
2022 if (!event->attr.sample_period)
2023 return -EINVAL;
2024
2025 size = copy_from_user(&value, arg, sizeof(value));
2026 if (size != sizeof(value))
2027 return -EFAULT;
2028
2029 if (!value)
2030 return -EINVAL;
2031
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002032 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002033 if (event->attr.freq) {
2034 if (value > sysctl_perf_event_sample_rate) {
2035 ret = -EINVAL;
2036 goto unlock;
2037 }
2038
2039 event->attr.sample_freq = value;
2040 } else {
2041 event->attr.sample_period = value;
2042 event->hw.sample_period = value;
2043 }
2044unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002045 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002046
2047 return ret;
2048}
2049
Li Zefan6fb29152009-10-15 11:21:42 +08002050static int perf_event_set_output(struct perf_event *event, int output_fd);
2051static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002052
2053static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2054{
2055 struct perf_event *event = file->private_data;
2056 void (*func)(struct perf_event *);
2057 u32 flags = arg;
2058
2059 switch (cmd) {
2060 case PERF_EVENT_IOC_ENABLE:
2061 func = perf_event_enable;
2062 break;
2063 case PERF_EVENT_IOC_DISABLE:
2064 func = perf_event_disable;
2065 break;
2066 case PERF_EVENT_IOC_RESET:
2067 func = perf_event_reset;
2068 break;
2069
2070 case PERF_EVENT_IOC_REFRESH:
2071 return perf_event_refresh(event, arg);
2072
2073 case PERF_EVENT_IOC_PERIOD:
2074 return perf_event_period(event, (u64 __user *)arg);
2075
2076 case PERF_EVENT_IOC_SET_OUTPUT:
2077 return perf_event_set_output(event, arg);
2078
Li Zefan6fb29152009-10-15 11:21:42 +08002079 case PERF_EVENT_IOC_SET_FILTER:
2080 return perf_event_set_filter(event, (void __user *)arg);
2081
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002082 default:
2083 return -ENOTTY;
2084 }
2085
2086 if (flags & PERF_IOC_FLAG_GROUP)
2087 perf_event_for_each(event, func);
2088 else
2089 perf_event_for_each_child(event, func);
2090
2091 return 0;
2092}
2093
2094int perf_event_task_enable(void)
2095{
2096 struct perf_event *event;
2097
2098 mutex_lock(&current->perf_event_mutex);
2099 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2100 perf_event_for_each_child(event, perf_event_enable);
2101 mutex_unlock(&current->perf_event_mutex);
2102
2103 return 0;
2104}
2105
2106int perf_event_task_disable(void)
2107{
2108 struct perf_event *event;
2109
2110 mutex_lock(&current->perf_event_mutex);
2111 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2112 perf_event_for_each_child(event, perf_event_disable);
2113 mutex_unlock(&current->perf_event_mutex);
2114
2115 return 0;
2116}
2117
2118#ifndef PERF_EVENT_INDEX_OFFSET
2119# define PERF_EVENT_INDEX_OFFSET 0
2120#endif
2121
2122static int perf_event_index(struct perf_event *event)
2123{
2124 if (event->state != PERF_EVENT_STATE_ACTIVE)
2125 return 0;
2126
2127 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2128}
2129
2130/*
2131 * Callers need to ensure there can be no nesting of this function, otherwise
2132 * the seqlock logic goes bad. We can not serialize this because the arch
2133 * code calls this from NMI context.
2134 */
2135void perf_event_update_userpage(struct perf_event *event)
2136{
2137 struct perf_event_mmap_page *userpg;
2138 struct perf_mmap_data *data;
2139
2140 rcu_read_lock();
2141 data = rcu_dereference(event->data);
2142 if (!data)
2143 goto unlock;
2144
2145 userpg = data->user_page;
2146
2147 /*
2148 * Disable preemption so as to not let the corresponding user-space
2149 * spin too long if we get preempted.
2150 */
2151 preempt_disable();
2152 ++userpg->lock;
2153 barrier();
2154 userpg->index = perf_event_index(event);
2155 userpg->offset = atomic64_read(&event->count);
2156 if (event->state == PERF_EVENT_STATE_ACTIVE)
2157 userpg->offset -= atomic64_read(&event->hw.prev_count);
2158
2159 userpg->time_enabled = event->total_time_enabled +
2160 atomic64_read(&event->child_total_time_enabled);
2161
2162 userpg->time_running = event->total_time_running +
2163 atomic64_read(&event->child_total_time_running);
2164
2165 barrier();
2166 ++userpg->lock;
2167 preempt_enable();
2168unlock:
2169 rcu_read_unlock();
2170}
2171
Peter Zijlstra906010b2009-09-21 16:08:49 +02002172static unsigned long perf_data_size(struct perf_mmap_data *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002173{
Peter Zijlstra906010b2009-09-21 16:08:49 +02002174 return data->nr_pages << (PAGE_SHIFT + data->data_order);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002175}
2176
Peter Zijlstra906010b2009-09-21 16:08:49 +02002177#ifndef CONFIG_PERF_USE_VMALLOC
2178
2179/*
2180 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2181 */
2182
2183static struct page *
2184perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2185{
2186 if (pgoff > data->nr_pages)
2187 return NULL;
2188
2189 if (pgoff == 0)
2190 return virt_to_page(data->user_page);
2191
2192 return virt_to_page(data->data_pages[pgoff - 1]);
2193}
2194
2195static struct perf_mmap_data *
2196perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002197{
2198 struct perf_mmap_data *data;
2199 unsigned long size;
2200 int i;
2201
2202 WARN_ON(atomic_read(&event->mmap_count));
2203
2204 size = sizeof(struct perf_mmap_data);
2205 size += nr_pages * sizeof(void *);
2206
2207 data = kzalloc(size, GFP_KERNEL);
2208 if (!data)
2209 goto fail;
2210
2211 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2212 if (!data->user_page)
2213 goto fail_user_page;
2214
2215 for (i = 0; i < nr_pages; i++) {
2216 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2217 if (!data->data_pages[i])
2218 goto fail_data_pages;
2219 }
2220
Peter Zijlstra906010b2009-09-21 16:08:49 +02002221 data->data_order = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002222 data->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002223
Peter Zijlstra906010b2009-09-21 16:08:49 +02002224 return data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002225
2226fail_data_pages:
2227 for (i--; i >= 0; i--)
2228 free_page((unsigned long)data->data_pages[i]);
2229
2230 free_page((unsigned long)data->user_page);
2231
2232fail_user_page:
2233 kfree(data);
2234
2235fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002236 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002237}
2238
2239static void perf_mmap_free_page(unsigned long addr)
2240{
2241 struct page *page = virt_to_page((void *)addr);
2242
2243 page->mapping = NULL;
2244 __free_page(page);
2245}
2246
Peter Zijlstra906010b2009-09-21 16:08:49 +02002247static void perf_mmap_data_free(struct perf_mmap_data *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002248{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002249 int i;
2250
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002251 perf_mmap_free_page((unsigned long)data->user_page);
2252 for (i = 0; i < data->nr_pages; i++)
2253 perf_mmap_free_page((unsigned long)data->data_pages[i]);
Kristian Høgsbergec70ccd2009-12-01 15:05:01 -05002254 kfree(data);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002255}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002256
Peter Zijlstra906010b2009-09-21 16:08:49 +02002257#else
2258
2259/*
2260 * Back perf_mmap() with vmalloc memory.
2261 *
2262 * Required for architectures that have d-cache aliasing issues.
2263 */
2264
2265static struct page *
2266perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2267{
2268 if (pgoff > (1UL << data->data_order))
2269 return NULL;
2270
2271 return vmalloc_to_page((void *)data->user_page + pgoff * PAGE_SIZE);
2272}
2273
2274static void perf_mmap_unmark_page(void *addr)
2275{
2276 struct page *page = vmalloc_to_page(addr);
2277
2278 page->mapping = NULL;
2279}
2280
2281static void perf_mmap_data_free_work(struct work_struct *work)
2282{
2283 struct perf_mmap_data *data;
2284 void *base;
2285 int i, nr;
2286
2287 data = container_of(work, struct perf_mmap_data, work);
2288 nr = 1 << data->data_order;
2289
2290 base = data->user_page;
2291 for (i = 0; i < nr + 1; i++)
2292 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2293
2294 vfree(base);
Kristian Høgsbergec70ccd2009-12-01 15:05:01 -05002295 kfree(data);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002296}
2297
2298static void perf_mmap_data_free(struct perf_mmap_data *data)
2299{
2300 schedule_work(&data->work);
2301}
2302
2303static struct perf_mmap_data *
2304perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2305{
2306 struct perf_mmap_data *data;
2307 unsigned long size;
2308 void *all_buf;
2309
2310 WARN_ON(atomic_read(&event->mmap_count));
2311
2312 size = sizeof(struct perf_mmap_data);
2313 size += sizeof(void *);
2314
2315 data = kzalloc(size, GFP_KERNEL);
2316 if (!data)
2317 goto fail;
2318
2319 INIT_WORK(&data->work, perf_mmap_data_free_work);
2320
2321 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2322 if (!all_buf)
2323 goto fail_all_buf;
2324
2325 data->user_page = all_buf;
2326 data->data_pages[0] = all_buf + PAGE_SIZE;
2327 data->data_order = ilog2(nr_pages);
2328 data->nr_pages = 1;
2329
2330 return data;
2331
2332fail_all_buf:
2333 kfree(data);
2334
2335fail:
2336 return NULL;
2337}
2338
2339#endif
2340
2341static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2342{
2343 struct perf_event *event = vma->vm_file->private_data;
2344 struct perf_mmap_data *data;
2345 int ret = VM_FAULT_SIGBUS;
2346
2347 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2348 if (vmf->pgoff == 0)
2349 ret = 0;
2350 return ret;
2351 }
2352
2353 rcu_read_lock();
2354 data = rcu_dereference(event->data);
2355 if (!data)
2356 goto unlock;
2357
2358 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2359 goto unlock;
2360
2361 vmf->page = perf_mmap_to_page(data, vmf->pgoff);
2362 if (!vmf->page)
2363 goto unlock;
2364
2365 get_page(vmf->page);
2366 vmf->page->mapping = vma->vm_file->f_mapping;
2367 vmf->page->index = vmf->pgoff;
2368
2369 ret = 0;
2370unlock:
2371 rcu_read_unlock();
2372
2373 return ret;
2374}
2375
2376static void
2377perf_mmap_data_init(struct perf_event *event, struct perf_mmap_data *data)
2378{
2379 long max_size = perf_data_size(data);
2380
2381 atomic_set(&data->lock, -1);
2382
2383 if (event->attr.watermark) {
2384 data->watermark = min_t(long, max_size,
2385 event->attr.wakeup_watermark);
2386 }
2387
2388 if (!data->watermark)
Stephane Eranian8904b182009-11-20 22:19:57 +01002389 data->watermark = max_size / 2;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002390
2391
2392 rcu_assign_pointer(event->data, data);
2393}
2394
2395static void perf_mmap_data_free_rcu(struct rcu_head *rcu_head)
2396{
2397 struct perf_mmap_data *data;
2398
2399 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2400 perf_mmap_data_free(data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002401}
2402
Peter Zijlstra906010b2009-09-21 16:08:49 +02002403static void perf_mmap_data_release(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002404{
2405 struct perf_mmap_data *data = event->data;
2406
2407 WARN_ON(atomic_read(&event->mmap_count));
2408
2409 rcu_assign_pointer(event->data, NULL);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002410 call_rcu(&data->rcu_head, perf_mmap_data_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002411}
2412
2413static void perf_mmap_open(struct vm_area_struct *vma)
2414{
2415 struct perf_event *event = vma->vm_file->private_data;
2416
2417 atomic_inc(&event->mmap_count);
2418}
2419
2420static void perf_mmap_close(struct vm_area_struct *vma)
2421{
2422 struct perf_event *event = vma->vm_file->private_data;
2423
2424 WARN_ON_ONCE(event->ctx->parent_ctx);
2425 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstra906010b2009-09-21 16:08:49 +02002426 unsigned long size = perf_data_size(event->data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002427 struct user_struct *user = current_user();
2428
Peter Zijlstra906010b2009-09-21 16:08:49 +02002429 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002430 vma->vm_mm->locked_vm -= event->data->nr_locked;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002431 perf_mmap_data_release(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002432 mutex_unlock(&event->mmap_mutex);
2433 }
2434}
2435
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002436static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002437 .open = perf_mmap_open,
2438 .close = perf_mmap_close,
2439 .fault = perf_mmap_fault,
2440 .page_mkwrite = perf_mmap_fault,
2441};
2442
2443static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2444{
2445 struct perf_event *event = file->private_data;
2446 unsigned long user_locked, user_lock_limit;
2447 struct user_struct *user = current_user();
2448 unsigned long locked, lock_limit;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002449 struct perf_mmap_data *data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002450 unsigned long vma_size;
2451 unsigned long nr_pages;
2452 long user_extra, extra;
2453 int ret = 0;
2454
2455 if (!(vma->vm_flags & VM_SHARED))
2456 return -EINVAL;
2457
2458 vma_size = vma->vm_end - vma->vm_start;
2459 nr_pages = (vma_size / PAGE_SIZE) - 1;
2460
2461 /*
2462 * If we have data pages ensure they're a power-of-two number, so we
2463 * can do bitmasks instead of modulo.
2464 */
2465 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2466 return -EINVAL;
2467
2468 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2469 return -EINVAL;
2470
2471 if (vma->vm_pgoff != 0)
2472 return -EINVAL;
2473
2474 WARN_ON_ONCE(event->ctx->parent_ctx);
2475 mutex_lock(&event->mmap_mutex);
2476 if (event->output) {
2477 ret = -EINVAL;
2478 goto unlock;
2479 }
2480
2481 if (atomic_inc_not_zero(&event->mmap_count)) {
2482 if (nr_pages != event->data->nr_pages)
2483 ret = -EINVAL;
2484 goto unlock;
2485 }
2486
2487 user_extra = nr_pages + 1;
2488 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2489
2490 /*
2491 * Increase the limit linearly with more CPUs:
2492 */
2493 user_lock_limit *= num_online_cpus();
2494
2495 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2496
2497 extra = 0;
2498 if (user_locked > user_lock_limit)
2499 extra = user_locked - user_lock_limit;
2500
2501 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2502 lock_limit >>= PAGE_SHIFT;
2503 locked = vma->vm_mm->locked_vm + extra;
2504
2505 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2506 !capable(CAP_IPC_LOCK)) {
2507 ret = -EPERM;
2508 goto unlock;
2509 }
2510
2511 WARN_ON(event->data);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002512
2513 data = perf_mmap_data_alloc(event, nr_pages);
2514 ret = -ENOMEM;
2515 if (!data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002516 goto unlock;
2517
Peter Zijlstra906010b2009-09-21 16:08:49 +02002518 ret = 0;
2519 perf_mmap_data_init(event, data);
2520
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002521 atomic_set(&event->mmap_count, 1);
2522 atomic_long_add(user_extra, &user->locked_vm);
2523 vma->vm_mm->locked_vm += extra;
2524 event->data->nr_locked = extra;
2525 if (vma->vm_flags & VM_WRITE)
2526 event->data->writable = 1;
2527
2528unlock:
2529 mutex_unlock(&event->mmap_mutex);
2530
2531 vma->vm_flags |= VM_RESERVED;
2532 vma->vm_ops = &perf_mmap_vmops;
2533
2534 return ret;
2535}
2536
2537static int perf_fasync(int fd, struct file *filp, int on)
2538{
2539 struct inode *inode = filp->f_path.dentry->d_inode;
2540 struct perf_event *event = filp->private_data;
2541 int retval;
2542
2543 mutex_lock(&inode->i_mutex);
2544 retval = fasync_helper(fd, filp, on, &event->fasync);
2545 mutex_unlock(&inode->i_mutex);
2546
2547 if (retval < 0)
2548 return retval;
2549
2550 return 0;
2551}
2552
2553static const struct file_operations perf_fops = {
2554 .release = perf_release,
2555 .read = perf_read,
2556 .poll = perf_poll,
2557 .unlocked_ioctl = perf_ioctl,
2558 .compat_ioctl = perf_ioctl,
2559 .mmap = perf_mmap,
2560 .fasync = perf_fasync,
2561};
2562
2563/*
2564 * Perf event wakeup
2565 *
2566 * If there's data, ensure we set the poll() state and publish everything
2567 * to user-space before waking everybody up.
2568 */
2569
2570void perf_event_wakeup(struct perf_event *event)
2571{
2572 wake_up_all(&event->waitq);
2573
2574 if (event->pending_kill) {
2575 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
2576 event->pending_kill = 0;
2577 }
2578}
2579
2580/*
2581 * Pending wakeups
2582 *
2583 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2584 *
2585 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2586 * single linked list and use cmpxchg() to add entries lockless.
2587 */
2588
2589static void perf_pending_event(struct perf_pending_entry *entry)
2590{
2591 struct perf_event *event = container_of(entry,
2592 struct perf_event, pending);
2593
2594 if (event->pending_disable) {
2595 event->pending_disable = 0;
2596 __perf_event_disable(event);
2597 }
2598
2599 if (event->pending_wakeup) {
2600 event->pending_wakeup = 0;
2601 perf_event_wakeup(event);
2602 }
2603}
2604
2605#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2606
2607static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2608 PENDING_TAIL,
2609};
2610
2611static void perf_pending_queue(struct perf_pending_entry *entry,
2612 void (*func)(struct perf_pending_entry *))
2613{
2614 struct perf_pending_entry **head;
2615
2616 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2617 return;
2618
2619 entry->func = func;
2620
2621 head = &get_cpu_var(perf_pending_head);
2622
2623 do {
2624 entry->next = *head;
2625 } while (cmpxchg(head, entry->next, entry) != entry->next);
2626
2627 set_perf_event_pending();
2628
2629 put_cpu_var(perf_pending_head);
2630}
2631
2632static int __perf_pending_run(void)
2633{
2634 struct perf_pending_entry *list;
2635 int nr = 0;
2636
2637 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2638 while (list != PENDING_TAIL) {
2639 void (*func)(struct perf_pending_entry *);
2640 struct perf_pending_entry *entry = list;
2641
2642 list = list->next;
2643
2644 func = entry->func;
2645 entry->next = NULL;
2646 /*
2647 * Ensure we observe the unqueue before we issue the wakeup,
2648 * so that we won't be waiting forever.
2649 * -- see perf_not_pending().
2650 */
2651 smp_wmb();
2652
2653 func(entry);
2654 nr++;
2655 }
2656
2657 return nr;
2658}
2659
2660static inline int perf_not_pending(struct perf_event *event)
2661{
2662 /*
2663 * If we flush on whatever cpu we run, there is a chance we don't
2664 * need to wait.
2665 */
2666 get_cpu();
2667 __perf_pending_run();
2668 put_cpu();
2669
2670 /*
2671 * Ensure we see the proper queue state before going to sleep
2672 * so that we do not miss the wakeup. -- see perf_pending_handle()
2673 */
2674 smp_rmb();
2675 return event->pending.next == NULL;
2676}
2677
2678static void perf_pending_sync(struct perf_event *event)
2679{
2680 wait_event(event->waitq, perf_not_pending(event));
2681}
2682
2683void perf_event_do_pending(void)
2684{
2685 __perf_pending_run();
2686}
2687
2688/*
2689 * Callchain support -- arch specific
2690 */
2691
2692__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2693{
2694 return NULL;
2695}
2696
2697/*
2698 * Output
2699 */
2700static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
2701 unsigned long offset, unsigned long head)
2702{
2703 unsigned long mask;
2704
2705 if (!data->writable)
2706 return true;
2707
Peter Zijlstra906010b2009-09-21 16:08:49 +02002708 mask = perf_data_size(data) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002709
2710 offset = (offset - tail) & mask;
2711 head = (head - tail) & mask;
2712
2713 if ((int)(head - offset) < 0)
2714 return false;
2715
2716 return true;
2717}
2718
2719static void perf_output_wakeup(struct perf_output_handle *handle)
2720{
2721 atomic_set(&handle->data->poll, POLL_IN);
2722
2723 if (handle->nmi) {
2724 handle->event->pending_wakeup = 1;
2725 perf_pending_queue(&handle->event->pending,
2726 perf_pending_event);
2727 } else
2728 perf_event_wakeup(handle->event);
2729}
2730
2731/*
2732 * Curious locking construct.
2733 *
2734 * We need to ensure a later event_id doesn't publish a head when a former
2735 * event_id isn't done writing. However since we need to deal with NMIs we
2736 * cannot fully serialize things.
2737 *
2738 * What we do is serialize between CPUs so we only have to deal with NMI
2739 * nesting on a single CPU.
2740 *
2741 * We only publish the head (and generate a wakeup) when the outer-most
2742 * event_id completes.
2743 */
2744static void perf_output_lock(struct perf_output_handle *handle)
2745{
2746 struct perf_mmap_data *data = handle->data;
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002747 int cur, cpu = get_cpu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002748
2749 handle->locked = 0;
2750
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002751 for (;;) {
2752 cur = atomic_cmpxchg(&data->lock, -1, cpu);
2753 if (cur == -1) {
2754 handle->locked = 1;
2755 break;
2756 }
2757 if (cur == cpu)
2758 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002759
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002760 cpu_relax();
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002761 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002762}
2763
2764static void perf_output_unlock(struct perf_output_handle *handle)
2765{
2766 struct perf_mmap_data *data = handle->data;
2767 unsigned long head;
2768 int cpu;
2769
2770 data->done_head = data->head;
2771
2772 if (!handle->locked)
2773 goto out;
2774
2775again:
2776 /*
2777 * The xchg implies a full barrier that ensures all writes are done
2778 * before we publish the new head, matched by a rmb() in userspace when
2779 * reading this position.
2780 */
2781 while ((head = atomic_long_xchg(&data->done_head, 0)))
2782 data->user_page->data_head = head;
2783
2784 /*
2785 * NMI can happen here, which means we can miss a done_head update.
2786 */
2787
2788 cpu = atomic_xchg(&data->lock, -1);
2789 WARN_ON_ONCE(cpu != smp_processor_id());
2790
2791 /*
2792 * Therefore we have to validate we did not indeed do so.
2793 */
2794 if (unlikely(atomic_long_read(&data->done_head))) {
2795 /*
2796 * Since we had it locked, we can lock it again.
2797 */
2798 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2799 cpu_relax();
2800
2801 goto again;
2802 }
2803
2804 if (atomic_xchg(&data->wakeup, 0))
2805 perf_output_wakeup(handle);
2806out:
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002807 put_cpu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002808}
2809
2810void perf_output_copy(struct perf_output_handle *handle,
2811 const void *buf, unsigned int len)
2812{
2813 unsigned int pages_mask;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002814 unsigned long offset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002815 unsigned int size;
2816 void **pages;
2817
2818 offset = handle->offset;
2819 pages_mask = handle->data->nr_pages - 1;
2820 pages = handle->data->data_pages;
2821
2822 do {
Peter Zijlstra906010b2009-09-21 16:08:49 +02002823 unsigned long page_offset;
2824 unsigned long page_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002825 int nr;
2826
2827 nr = (offset >> PAGE_SHIFT) & pages_mask;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002828 page_size = 1UL << (handle->data->data_order + PAGE_SHIFT);
2829 page_offset = offset & (page_size - 1);
2830 size = min_t(unsigned int, page_size - page_offset, len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002831
2832 memcpy(pages[nr] + page_offset, buf, size);
2833
2834 len -= size;
2835 buf += size;
2836 offset += size;
2837 } while (len);
2838
2839 handle->offset = offset;
2840
2841 /*
2842 * Check we didn't copy past our reservation window, taking the
2843 * possible unsigned int wrap into account.
2844 */
2845 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
2846}
2847
2848int perf_output_begin(struct perf_output_handle *handle,
2849 struct perf_event *event, unsigned int size,
2850 int nmi, int sample)
2851{
2852 struct perf_event *output_event;
2853 struct perf_mmap_data *data;
2854 unsigned long tail, offset, head;
2855 int have_lost;
2856 struct {
2857 struct perf_event_header header;
2858 u64 id;
2859 u64 lost;
2860 } lost_event;
2861
2862 rcu_read_lock();
2863 /*
2864 * For inherited events we send all the output towards the parent.
2865 */
2866 if (event->parent)
2867 event = event->parent;
2868
2869 output_event = rcu_dereference(event->output);
2870 if (output_event)
2871 event = output_event;
2872
2873 data = rcu_dereference(event->data);
2874 if (!data)
2875 goto out;
2876
2877 handle->data = data;
2878 handle->event = event;
2879 handle->nmi = nmi;
2880 handle->sample = sample;
2881
2882 if (!data->nr_pages)
2883 goto fail;
2884
2885 have_lost = atomic_read(&data->lost);
2886 if (have_lost)
2887 size += sizeof(lost_event);
2888
2889 perf_output_lock(handle);
2890
2891 do {
2892 /*
2893 * Userspace could choose to issue a mb() before updating the
2894 * tail pointer. So that all reads will be completed before the
2895 * write is issued.
2896 */
2897 tail = ACCESS_ONCE(data->user_page->data_tail);
2898 smp_rmb();
2899 offset = head = atomic_long_read(&data->head);
2900 head += size;
2901 if (unlikely(!perf_output_space(data, tail, offset, head)))
2902 goto fail;
2903 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2904
2905 handle->offset = offset;
2906 handle->head = head;
2907
2908 if (head - tail > data->watermark)
2909 atomic_set(&data->wakeup, 1);
2910
2911 if (have_lost) {
2912 lost_event.header.type = PERF_RECORD_LOST;
2913 lost_event.header.misc = 0;
2914 lost_event.header.size = sizeof(lost_event);
2915 lost_event.id = event->id;
2916 lost_event.lost = atomic_xchg(&data->lost, 0);
2917
2918 perf_output_put(handle, lost_event);
2919 }
2920
2921 return 0;
2922
2923fail:
2924 atomic_inc(&data->lost);
2925 perf_output_unlock(handle);
2926out:
2927 rcu_read_unlock();
2928
2929 return -ENOSPC;
2930}
2931
2932void perf_output_end(struct perf_output_handle *handle)
2933{
2934 struct perf_event *event = handle->event;
2935 struct perf_mmap_data *data = handle->data;
2936
2937 int wakeup_events = event->attr.wakeup_events;
2938
2939 if (handle->sample && wakeup_events) {
2940 int events = atomic_inc_return(&data->events);
2941 if (events >= wakeup_events) {
2942 atomic_sub(wakeup_events, &data->events);
2943 atomic_set(&data->wakeup, 1);
2944 }
2945 }
2946
2947 perf_output_unlock(handle);
2948 rcu_read_unlock();
2949}
2950
2951static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
2952{
2953 /*
2954 * only top level events have the pid namespace they were created in
2955 */
2956 if (event->parent)
2957 event = event->parent;
2958
2959 return task_tgid_nr_ns(p, event->ns);
2960}
2961
2962static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
2963{
2964 /*
2965 * only top level events have the pid namespace they were created in
2966 */
2967 if (event->parent)
2968 event = event->parent;
2969
2970 return task_pid_nr_ns(p, event->ns);
2971}
2972
2973static void perf_output_read_one(struct perf_output_handle *handle,
2974 struct perf_event *event)
2975{
2976 u64 read_format = event->attr.read_format;
2977 u64 values[4];
2978 int n = 0;
2979
2980 values[n++] = atomic64_read(&event->count);
2981 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2982 values[n++] = event->total_time_enabled +
2983 atomic64_read(&event->child_total_time_enabled);
2984 }
2985 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2986 values[n++] = event->total_time_running +
2987 atomic64_read(&event->child_total_time_running);
2988 }
2989 if (read_format & PERF_FORMAT_ID)
2990 values[n++] = primary_event_id(event);
2991
2992 perf_output_copy(handle, values, n * sizeof(u64));
2993}
2994
2995/*
2996 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
2997 */
2998static void perf_output_read_group(struct perf_output_handle *handle,
2999 struct perf_event *event)
3000{
3001 struct perf_event *leader = event->group_leader, *sub;
3002 u64 read_format = event->attr.read_format;
3003 u64 values[5];
3004 int n = 0;
3005
3006 values[n++] = 1 + leader->nr_siblings;
3007
3008 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3009 values[n++] = leader->total_time_enabled;
3010
3011 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3012 values[n++] = leader->total_time_running;
3013
3014 if (leader != event)
3015 leader->pmu->read(leader);
3016
3017 values[n++] = atomic64_read(&leader->count);
3018 if (read_format & PERF_FORMAT_ID)
3019 values[n++] = primary_event_id(leader);
3020
3021 perf_output_copy(handle, values, n * sizeof(u64));
3022
3023 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3024 n = 0;
3025
3026 if (sub != event)
3027 sub->pmu->read(sub);
3028
3029 values[n++] = atomic64_read(&sub->count);
3030 if (read_format & PERF_FORMAT_ID)
3031 values[n++] = primary_event_id(sub);
3032
3033 perf_output_copy(handle, values, n * sizeof(u64));
3034 }
3035}
3036
3037static void perf_output_read(struct perf_output_handle *handle,
3038 struct perf_event *event)
3039{
3040 if (event->attr.read_format & PERF_FORMAT_GROUP)
3041 perf_output_read_group(handle, event);
3042 else
3043 perf_output_read_one(handle, event);
3044}
3045
3046void perf_output_sample(struct perf_output_handle *handle,
3047 struct perf_event_header *header,
3048 struct perf_sample_data *data,
3049 struct perf_event *event)
3050{
3051 u64 sample_type = data->type;
3052
3053 perf_output_put(handle, *header);
3054
3055 if (sample_type & PERF_SAMPLE_IP)
3056 perf_output_put(handle, data->ip);
3057
3058 if (sample_type & PERF_SAMPLE_TID)
3059 perf_output_put(handle, data->tid_entry);
3060
3061 if (sample_type & PERF_SAMPLE_TIME)
3062 perf_output_put(handle, data->time);
3063
3064 if (sample_type & PERF_SAMPLE_ADDR)
3065 perf_output_put(handle, data->addr);
3066
3067 if (sample_type & PERF_SAMPLE_ID)
3068 perf_output_put(handle, data->id);
3069
3070 if (sample_type & PERF_SAMPLE_STREAM_ID)
3071 perf_output_put(handle, data->stream_id);
3072
3073 if (sample_type & PERF_SAMPLE_CPU)
3074 perf_output_put(handle, data->cpu_entry);
3075
3076 if (sample_type & PERF_SAMPLE_PERIOD)
3077 perf_output_put(handle, data->period);
3078
3079 if (sample_type & PERF_SAMPLE_READ)
3080 perf_output_read(handle, event);
3081
3082 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3083 if (data->callchain) {
3084 int size = 1;
3085
3086 if (data->callchain)
3087 size += data->callchain->nr;
3088
3089 size *= sizeof(u64);
3090
3091 perf_output_copy(handle, data->callchain, size);
3092 } else {
3093 u64 nr = 0;
3094 perf_output_put(handle, nr);
3095 }
3096 }
3097
3098 if (sample_type & PERF_SAMPLE_RAW) {
3099 if (data->raw) {
3100 perf_output_put(handle, data->raw->size);
3101 perf_output_copy(handle, data->raw->data,
3102 data->raw->size);
3103 } else {
3104 struct {
3105 u32 size;
3106 u32 data;
3107 } raw = {
3108 .size = sizeof(u32),
3109 .data = 0,
3110 };
3111 perf_output_put(handle, raw);
3112 }
3113 }
3114}
3115
3116void perf_prepare_sample(struct perf_event_header *header,
3117 struct perf_sample_data *data,
3118 struct perf_event *event,
3119 struct pt_regs *regs)
3120{
3121 u64 sample_type = event->attr.sample_type;
3122
3123 data->type = sample_type;
3124
3125 header->type = PERF_RECORD_SAMPLE;
3126 header->size = sizeof(*header);
3127
3128 header->misc = 0;
3129 header->misc |= perf_misc_flags(regs);
3130
3131 if (sample_type & PERF_SAMPLE_IP) {
3132 data->ip = perf_instruction_pointer(regs);
3133
3134 header->size += sizeof(data->ip);
3135 }
3136
3137 if (sample_type & PERF_SAMPLE_TID) {
3138 /* namespace issues */
3139 data->tid_entry.pid = perf_event_pid(event, current);
3140 data->tid_entry.tid = perf_event_tid(event, current);
3141
3142 header->size += sizeof(data->tid_entry);
3143 }
3144
3145 if (sample_type & PERF_SAMPLE_TIME) {
3146 data->time = perf_clock();
3147
3148 header->size += sizeof(data->time);
3149 }
3150
3151 if (sample_type & PERF_SAMPLE_ADDR)
3152 header->size += sizeof(data->addr);
3153
3154 if (sample_type & PERF_SAMPLE_ID) {
3155 data->id = primary_event_id(event);
3156
3157 header->size += sizeof(data->id);
3158 }
3159
3160 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3161 data->stream_id = event->id;
3162
3163 header->size += sizeof(data->stream_id);
3164 }
3165
3166 if (sample_type & PERF_SAMPLE_CPU) {
3167 data->cpu_entry.cpu = raw_smp_processor_id();
3168 data->cpu_entry.reserved = 0;
3169
3170 header->size += sizeof(data->cpu_entry);
3171 }
3172
3173 if (sample_type & PERF_SAMPLE_PERIOD)
3174 header->size += sizeof(data->period);
3175
3176 if (sample_type & PERF_SAMPLE_READ)
3177 header->size += perf_event_read_size(event);
3178
3179 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3180 int size = 1;
3181
3182 data->callchain = perf_callchain(regs);
3183
3184 if (data->callchain)
3185 size += data->callchain->nr;
3186
3187 header->size += size * sizeof(u64);
3188 }
3189
3190 if (sample_type & PERF_SAMPLE_RAW) {
3191 int size = sizeof(u32);
3192
3193 if (data->raw)
3194 size += data->raw->size;
3195 else
3196 size += sizeof(u32);
3197
3198 WARN_ON_ONCE(size & (sizeof(u64)-1));
3199 header->size += size;
3200 }
3201}
3202
3203static void perf_event_output(struct perf_event *event, int nmi,
3204 struct perf_sample_data *data,
3205 struct pt_regs *regs)
3206{
3207 struct perf_output_handle handle;
3208 struct perf_event_header header;
3209
3210 perf_prepare_sample(&header, data, event, regs);
3211
3212 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3213 return;
3214
3215 perf_output_sample(&handle, &header, data, event);
3216
3217 perf_output_end(&handle);
3218}
3219
3220/*
3221 * read event_id
3222 */
3223
3224struct perf_read_event {
3225 struct perf_event_header header;
3226
3227 u32 pid;
3228 u32 tid;
3229};
3230
3231static void
3232perf_event_read_event(struct perf_event *event,
3233 struct task_struct *task)
3234{
3235 struct perf_output_handle handle;
3236 struct perf_read_event read_event = {
3237 .header = {
3238 .type = PERF_RECORD_READ,
3239 .misc = 0,
3240 .size = sizeof(read_event) + perf_event_read_size(event),
3241 },
3242 .pid = perf_event_pid(event, task),
3243 .tid = perf_event_tid(event, task),
3244 };
3245 int ret;
3246
3247 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3248 if (ret)
3249 return;
3250
3251 perf_output_put(&handle, read_event);
3252 perf_output_read(&handle, event);
3253
3254 perf_output_end(&handle);
3255}
3256
3257/*
3258 * task tracking -- fork/exit
3259 *
3260 * enabled by: attr.comm | attr.mmap | attr.task
3261 */
3262
3263struct perf_task_event {
3264 struct task_struct *task;
3265 struct perf_event_context *task_ctx;
3266
3267 struct {
3268 struct perf_event_header header;
3269
3270 u32 pid;
3271 u32 ppid;
3272 u32 tid;
3273 u32 ptid;
3274 u64 time;
3275 } event_id;
3276};
3277
3278static void perf_event_task_output(struct perf_event *event,
3279 struct perf_task_event *task_event)
3280{
3281 struct perf_output_handle handle;
3282 int size;
3283 struct task_struct *task = task_event->task;
3284 int ret;
3285
3286 size = task_event->event_id.header.size;
3287 ret = perf_output_begin(&handle, event, size, 0, 0);
3288
3289 if (ret)
3290 return;
3291
3292 task_event->event_id.pid = perf_event_pid(event, task);
3293 task_event->event_id.ppid = perf_event_pid(event, current);
3294
3295 task_event->event_id.tid = perf_event_tid(event, task);
3296 task_event->event_id.ptid = perf_event_tid(event, current);
3297
3298 task_event->event_id.time = perf_clock();
3299
3300 perf_output_put(&handle, task_event->event_id);
3301
3302 perf_output_end(&handle);
3303}
3304
3305static int perf_event_task_match(struct perf_event *event)
3306{
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003307 if (event->cpu != -1 && event->cpu != smp_processor_id())
3308 return 0;
3309
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003310 if (event->attr.comm || event->attr.mmap || event->attr.task)
3311 return 1;
3312
3313 return 0;
3314}
3315
3316static void perf_event_task_ctx(struct perf_event_context *ctx,
3317 struct perf_task_event *task_event)
3318{
3319 struct perf_event *event;
3320
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003321 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3322 if (perf_event_task_match(event))
3323 perf_event_task_output(event, task_event);
3324 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003325}
3326
3327static void perf_event_task_event(struct perf_task_event *task_event)
3328{
3329 struct perf_cpu_context *cpuctx;
3330 struct perf_event_context *ctx = task_event->task_ctx;
3331
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01003332 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003333 cpuctx = &get_cpu_var(perf_cpu_context);
3334 perf_event_task_ctx(&cpuctx->ctx, task_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003335 if (!ctx)
3336 ctx = rcu_dereference(task_event->task->perf_event_ctxp);
3337 if (ctx)
3338 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003339 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003340 rcu_read_unlock();
3341}
3342
3343static void perf_event_task(struct task_struct *task,
3344 struct perf_event_context *task_ctx,
3345 int new)
3346{
3347 struct perf_task_event task_event;
3348
3349 if (!atomic_read(&nr_comm_events) &&
3350 !atomic_read(&nr_mmap_events) &&
3351 !atomic_read(&nr_task_events))
3352 return;
3353
3354 task_event = (struct perf_task_event){
3355 .task = task,
3356 .task_ctx = task_ctx,
3357 .event_id = {
3358 .header = {
3359 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3360 .misc = 0,
3361 .size = sizeof(task_event.event_id),
3362 },
3363 /* .pid */
3364 /* .ppid */
3365 /* .tid */
3366 /* .ptid */
3367 },
3368 };
3369
3370 perf_event_task_event(&task_event);
3371}
3372
3373void perf_event_fork(struct task_struct *task)
3374{
3375 perf_event_task(task, NULL, 1);
3376}
3377
3378/*
3379 * comm tracking
3380 */
3381
3382struct perf_comm_event {
3383 struct task_struct *task;
3384 char *comm;
3385 int comm_size;
3386
3387 struct {
3388 struct perf_event_header header;
3389
3390 u32 pid;
3391 u32 tid;
3392 } event_id;
3393};
3394
3395static void perf_event_comm_output(struct perf_event *event,
3396 struct perf_comm_event *comm_event)
3397{
3398 struct perf_output_handle handle;
3399 int size = comm_event->event_id.header.size;
3400 int ret = perf_output_begin(&handle, event, size, 0, 0);
3401
3402 if (ret)
3403 return;
3404
3405 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3406 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3407
3408 perf_output_put(&handle, comm_event->event_id);
3409 perf_output_copy(&handle, comm_event->comm,
3410 comm_event->comm_size);
3411 perf_output_end(&handle);
3412}
3413
3414static int perf_event_comm_match(struct perf_event *event)
3415{
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003416 if (event->cpu != -1 && event->cpu != smp_processor_id())
3417 return 0;
3418
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003419 if (event->attr.comm)
3420 return 1;
3421
3422 return 0;
3423}
3424
3425static void perf_event_comm_ctx(struct perf_event_context *ctx,
3426 struct perf_comm_event *comm_event)
3427{
3428 struct perf_event *event;
3429
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003430 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3431 if (perf_event_comm_match(event))
3432 perf_event_comm_output(event, comm_event);
3433 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003434}
3435
3436static void perf_event_comm_event(struct perf_comm_event *comm_event)
3437{
3438 struct perf_cpu_context *cpuctx;
3439 struct perf_event_context *ctx;
3440 unsigned int size;
3441 char comm[TASK_COMM_LEN];
3442
3443 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01003444 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003445 size = ALIGN(strlen(comm)+1, sizeof(u64));
3446
3447 comm_event->comm = comm;
3448 comm_event->comm_size = size;
3449
3450 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3451
Peter Zijlstraf6595f32009-11-20 22:19:47 +01003452 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003453 cpuctx = &get_cpu_var(perf_cpu_context);
3454 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003455 ctx = rcu_dereference(current->perf_event_ctxp);
3456 if (ctx)
3457 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003458 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003459 rcu_read_unlock();
3460}
3461
3462void perf_event_comm(struct task_struct *task)
3463{
3464 struct perf_comm_event comm_event;
3465
3466 if (task->perf_event_ctxp)
3467 perf_event_enable_on_exec(task);
3468
3469 if (!atomic_read(&nr_comm_events))
3470 return;
3471
3472 comm_event = (struct perf_comm_event){
3473 .task = task,
3474 /* .comm */
3475 /* .comm_size */
3476 .event_id = {
3477 .header = {
3478 .type = PERF_RECORD_COMM,
3479 .misc = 0,
3480 /* .size */
3481 },
3482 /* .pid */
3483 /* .tid */
3484 },
3485 };
3486
3487 perf_event_comm_event(&comm_event);
3488}
3489
3490/*
3491 * mmap tracking
3492 */
3493
3494struct perf_mmap_event {
3495 struct vm_area_struct *vma;
3496
3497 const char *file_name;
3498 int file_size;
3499
3500 struct {
3501 struct perf_event_header header;
3502
3503 u32 pid;
3504 u32 tid;
3505 u64 start;
3506 u64 len;
3507 u64 pgoff;
3508 } event_id;
3509};
3510
3511static void perf_event_mmap_output(struct perf_event *event,
3512 struct perf_mmap_event *mmap_event)
3513{
3514 struct perf_output_handle handle;
3515 int size = mmap_event->event_id.header.size;
3516 int ret = perf_output_begin(&handle, event, size, 0, 0);
3517
3518 if (ret)
3519 return;
3520
3521 mmap_event->event_id.pid = perf_event_pid(event, current);
3522 mmap_event->event_id.tid = perf_event_tid(event, current);
3523
3524 perf_output_put(&handle, mmap_event->event_id);
3525 perf_output_copy(&handle, mmap_event->file_name,
3526 mmap_event->file_size);
3527 perf_output_end(&handle);
3528}
3529
3530static int perf_event_mmap_match(struct perf_event *event,
3531 struct perf_mmap_event *mmap_event)
3532{
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003533 if (event->cpu != -1 && event->cpu != smp_processor_id())
3534 return 0;
3535
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003536 if (event->attr.mmap)
3537 return 1;
3538
3539 return 0;
3540}
3541
3542static void perf_event_mmap_ctx(struct perf_event_context *ctx,
3543 struct perf_mmap_event *mmap_event)
3544{
3545 struct perf_event *event;
3546
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003547 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3548 if (perf_event_mmap_match(event, mmap_event))
3549 perf_event_mmap_output(event, mmap_event);
3550 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003551}
3552
3553static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
3554{
3555 struct perf_cpu_context *cpuctx;
3556 struct perf_event_context *ctx;
3557 struct vm_area_struct *vma = mmap_event->vma;
3558 struct file *file = vma->vm_file;
3559 unsigned int size;
3560 char tmp[16];
3561 char *buf = NULL;
3562 const char *name;
3563
3564 memset(tmp, 0, sizeof(tmp));
3565
3566 if (file) {
3567 /*
3568 * d_path works from the end of the buffer backwards, so we
3569 * need to add enough zero bytes after the string to handle
3570 * the 64bit alignment we do later.
3571 */
3572 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3573 if (!buf) {
3574 name = strncpy(tmp, "//enomem", sizeof(tmp));
3575 goto got_name;
3576 }
3577 name = d_path(&file->f_path, buf, PATH_MAX);
3578 if (IS_ERR(name)) {
3579 name = strncpy(tmp, "//toolong", sizeof(tmp));
3580 goto got_name;
3581 }
3582 } else {
3583 if (arch_vma_name(mmap_event->vma)) {
3584 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3585 sizeof(tmp));
3586 goto got_name;
3587 }
3588
3589 if (!vma->vm_mm) {
3590 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3591 goto got_name;
3592 }
3593
3594 name = strncpy(tmp, "//anon", sizeof(tmp));
3595 goto got_name;
3596 }
3597
3598got_name:
3599 size = ALIGN(strlen(name)+1, sizeof(u64));
3600
3601 mmap_event->file_name = name;
3602 mmap_event->file_size = size;
3603
3604 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
3605
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01003606 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003607 cpuctx = &get_cpu_var(perf_cpu_context);
3608 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003609 ctx = rcu_dereference(current->perf_event_ctxp);
3610 if (ctx)
3611 perf_event_mmap_ctx(ctx, mmap_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003612 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003613 rcu_read_unlock();
3614
3615 kfree(buf);
3616}
3617
3618void __perf_event_mmap(struct vm_area_struct *vma)
3619{
3620 struct perf_mmap_event mmap_event;
3621
3622 if (!atomic_read(&nr_mmap_events))
3623 return;
3624
3625 mmap_event = (struct perf_mmap_event){
3626 .vma = vma,
3627 /* .file_name */
3628 /* .file_size */
3629 .event_id = {
3630 .header = {
3631 .type = PERF_RECORD_MMAP,
3632 .misc = 0,
3633 /* .size */
3634 },
3635 /* .pid */
3636 /* .tid */
3637 .start = vma->vm_start,
3638 .len = vma->vm_end - vma->vm_start,
3639 .pgoff = vma->vm_pgoff,
3640 },
3641 };
3642
3643 perf_event_mmap_event(&mmap_event);
3644}
3645
3646/*
3647 * IRQ throttle logging
3648 */
3649
3650static void perf_log_throttle(struct perf_event *event, int enable)
3651{
3652 struct perf_output_handle handle;
3653 int ret;
3654
3655 struct {
3656 struct perf_event_header header;
3657 u64 time;
3658 u64 id;
3659 u64 stream_id;
3660 } throttle_event = {
3661 .header = {
3662 .type = PERF_RECORD_THROTTLE,
3663 .misc = 0,
3664 .size = sizeof(throttle_event),
3665 },
3666 .time = perf_clock(),
3667 .id = primary_event_id(event),
3668 .stream_id = event->id,
3669 };
3670
3671 if (enable)
3672 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
3673
3674 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
3675 if (ret)
3676 return;
3677
3678 perf_output_put(&handle, throttle_event);
3679 perf_output_end(&handle);
3680}
3681
3682/*
3683 * Generic event overflow handling, sampling.
3684 */
3685
3686static int __perf_event_overflow(struct perf_event *event, int nmi,
3687 int throttle, struct perf_sample_data *data,
3688 struct pt_regs *regs)
3689{
3690 int events = atomic_read(&event->event_limit);
3691 struct hw_perf_event *hwc = &event->hw;
3692 int ret = 0;
3693
3694 throttle = (throttle && event->pmu->unthrottle != NULL);
3695
3696 if (!throttle) {
3697 hwc->interrupts++;
3698 } else {
3699 if (hwc->interrupts != MAX_INTERRUPTS) {
3700 hwc->interrupts++;
3701 if (HZ * hwc->interrupts >
3702 (u64)sysctl_perf_event_sample_rate) {
3703 hwc->interrupts = MAX_INTERRUPTS;
3704 perf_log_throttle(event, 0);
3705 ret = 1;
3706 }
3707 } else {
3708 /*
3709 * Keep re-disabling events even though on the previous
3710 * pass we disabled it - just in case we raced with a
3711 * sched-in and the event got enabled again:
3712 */
3713 ret = 1;
3714 }
3715 }
3716
3717 if (event->attr.freq) {
3718 u64 now = perf_clock();
3719 s64 delta = now - hwc->freq_stamp;
3720
3721 hwc->freq_stamp = now;
3722
3723 if (delta > 0 && delta < TICK_NSEC)
3724 perf_adjust_period(event, NSEC_PER_SEC / (int)delta);
3725 }
3726
3727 /*
3728 * XXX event_limit might not quite work as expected on inherited
3729 * events
3730 */
3731
3732 event->pending_kill = POLL_IN;
3733 if (events && atomic_dec_and_test(&event->event_limit)) {
3734 ret = 1;
3735 event->pending_kill = POLL_HUP;
3736 if (nmi) {
3737 event->pending_disable = 1;
3738 perf_pending_queue(&event->pending,
3739 perf_pending_event);
3740 } else
3741 perf_event_disable(event);
3742 }
3743
Peter Zijlstra453f19e2009-11-20 22:19:43 +01003744 if (event->overflow_handler)
3745 event->overflow_handler(event, nmi, data, regs);
3746 else
3747 perf_event_output(event, nmi, data, regs);
3748
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003749 return ret;
3750}
3751
3752int perf_event_overflow(struct perf_event *event, int nmi,
3753 struct perf_sample_data *data,
3754 struct pt_regs *regs)
3755{
3756 return __perf_event_overflow(event, nmi, 1, data, regs);
3757}
3758
3759/*
3760 * Generic software event infrastructure
3761 */
3762
3763/*
3764 * We directly increment event->count and keep a second value in
3765 * event->hw.period_left to count intervals. This period event
3766 * is kept in the range [-sample_period, 0] so that we can use the
3767 * sign as trigger.
3768 */
3769
3770static u64 perf_swevent_set_period(struct perf_event *event)
3771{
3772 struct hw_perf_event *hwc = &event->hw;
3773 u64 period = hwc->last_period;
3774 u64 nr, offset;
3775 s64 old, val;
3776
3777 hwc->last_period = hwc->sample_period;
3778
3779again:
3780 old = val = atomic64_read(&hwc->period_left);
3781 if (val < 0)
3782 return 0;
3783
3784 nr = div64_u64(period + val, period);
3785 offset = nr * period;
3786 val -= offset;
3787 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
3788 goto again;
3789
3790 return nr;
3791}
3792
Peter Zijlstra0cff7842009-11-20 22:19:44 +01003793static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003794 int nmi, struct perf_sample_data *data,
3795 struct pt_regs *regs)
3796{
3797 struct hw_perf_event *hwc = &event->hw;
3798 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003799
3800 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01003801 if (!overflow)
3802 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003803
3804 if (hwc->interrupts == MAX_INTERRUPTS)
3805 return;
3806
3807 for (; overflow; overflow--) {
3808 if (__perf_event_overflow(event, nmi, throttle,
3809 data, regs)) {
3810 /*
3811 * We inhibit the overflow from happening when
3812 * hwc->interrupts == MAX_INTERRUPTS.
3813 */
3814 break;
3815 }
3816 throttle = 1;
3817 }
3818}
3819
3820static void perf_swevent_unthrottle(struct perf_event *event)
3821{
3822 /*
3823 * Nothing to do, we already reset hwc->interrupts.
3824 */
3825}
3826
3827static void perf_swevent_add(struct perf_event *event, u64 nr,
3828 int nmi, struct perf_sample_data *data,
3829 struct pt_regs *regs)
3830{
3831 struct hw_perf_event *hwc = &event->hw;
3832
3833 atomic64_add(nr, &event->count);
3834
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003835 if (!regs)
3836 return;
3837
Peter Zijlstra0cff7842009-11-20 22:19:44 +01003838 if (!hwc->sample_period)
3839 return;
3840
3841 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
3842 return perf_swevent_overflow(event, 1, nmi, data, regs);
3843
3844 if (atomic64_add_negative(nr, &hwc->period_left))
3845 return;
3846
3847 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003848}
3849
3850static int perf_swevent_is_counting(struct perf_event *event)
3851{
3852 /*
3853 * The event is active, we're good!
3854 */
3855 if (event->state == PERF_EVENT_STATE_ACTIVE)
3856 return 1;
3857
3858 /*
3859 * The event is off/error, not counting.
3860 */
3861 if (event->state != PERF_EVENT_STATE_INACTIVE)
3862 return 0;
3863
3864 /*
3865 * The event is inactive, if the context is active
3866 * we're part of a group that didn't make it on the 'pmu',
3867 * not counting.
3868 */
3869 if (event->ctx->is_active)
3870 return 0;
3871
3872 /*
3873 * We're inactive and the context is too, this means the
3874 * task is scheduled out, we're counting events that happen
3875 * to us, like migration events.
3876 */
3877 return 1;
3878}
3879
Li Zefan6fb29152009-10-15 11:21:42 +08003880static int perf_tp_event_match(struct perf_event *event,
3881 struct perf_sample_data *data);
3882
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01003883static int perf_exclude_event(struct perf_event *event,
3884 struct pt_regs *regs)
3885{
3886 if (regs) {
3887 if (event->attr.exclude_user && user_mode(regs))
3888 return 1;
3889
3890 if (event->attr.exclude_kernel && !user_mode(regs))
3891 return 1;
3892 }
3893
3894 return 0;
3895}
3896
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003897static int perf_swevent_match(struct perf_event *event,
3898 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08003899 u32 event_id,
3900 struct perf_sample_data *data,
3901 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003902{
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003903 if (event->cpu != -1 && event->cpu != smp_processor_id())
3904 return 0;
3905
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003906 if (!perf_swevent_is_counting(event))
3907 return 0;
3908
3909 if (event->attr.type != type)
3910 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01003911
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003912 if (event->attr.config != event_id)
3913 return 0;
3914
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01003915 if (perf_exclude_event(event, regs))
3916 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003917
Li Zefan6fb29152009-10-15 11:21:42 +08003918 if (event->attr.type == PERF_TYPE_TRACEPOINT &&
3919 !perf_tp_event_match(event, data))
3920 return 0;
3921
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003922 return 1;
3923}
3924
3925static void perf_swevent_ctx_event(struct perf_event_context *ctx,
3926 enum perf_type_id type,
3927 u32 event_id, u64 nr, int nmi,
3928 struct perf_sample_data *data,
3929 struct pt_regs *regs)
3930{
3931 struct perf_event *event;
3932
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003933 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08003934 if (perf_swevent_match(event, type, event_id, data, regs))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003935 perf_swevent_add(event, nr, nmi, data, regs);
3936 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003937}
3938
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003939int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003940{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003941 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
3942 int rctx;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003943
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003944 if (in_nmi())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003945 rctx = 3;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003946 else if (in_irq())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003947 rctx = 2;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003948 else if (in_softirq())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003949 rctx = 1;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003950 else
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003951 rctx = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003952
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003953 if (cpuctx->recursion[rctx]) {
3954 put_cpu_var(perf_cpu_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003955 return -1;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003956 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003957
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003958 cpuctx->recursion[rctx]++;
3959 barrier();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003960
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003961 return rctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003962}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01003963EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003964
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003965void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003966{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003967 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3968 barrier();
Frederic Weisbeckerfe612672009-11-24 20:38:22 +01003969 cpuctx->recursion[rctx]--;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003970 put_cpu_var(perf_cpu_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003971}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01003972EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003973
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003974static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
3975 u64 nr, int nmi,
3976 struct perf_sample_data *data,
3977 struct pt_regs *regs)
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003978{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003979 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003980 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003981
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003982 cpuctx = &__get_cpu_var(perf_cpu_context);
Peter Zijlstra81520182009-11-20 22:19:45 +01003983 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003984 perf_swevent_ctx_event(&cpuctx->ctx, type, event_id,
3985 nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003986 /*
3987 * doesn't really matter which of the child contexts the
3988 * events ends up in.
3989 */
3990 ctx = rcu_dereference(current->perf_event_ctxp);
3991 if (ctx)
3992 perf_swevent_ctx_event(ctx, type, event_id, nr, nmi, data, regs);
3993 rcu_read_unlock();
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003994}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003995
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003996void __perf_sw_event(u32 event_id, u64 nr, int nmi,
3997 struct pt_regs *regs, u64 addr)
3998{
Ingo Molnara4234bf2009-11-23 10:57:59 +01003999 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004000 int rctx;
4001
4002 rctx = perf_swevent_get_recursion_context();
4003 if (rctx < 0)
4004 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004005
Ingo Molnara4234bf2009-11-23 10:57:59 +01004006 data.addr = addr;
4007 data.raw = NULL;
4008
4009 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004010
4011 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004012}
4013
4014static void perf_swevent_read(struct perf_event *event)
4015{
4016}
4017
4018static int perf_swevent_enable(struct perf_event *event)
4019{
4020 struct hw_perf_event *hwc = &event->hw;
4021
4022 if (hwc->sample_period) {
4023 hwc->last_period = hwc->sample_period;
4024 perf_swevent_set_period(event);
4025 }
4026 return 0;
4027}
4028
4029static void perf_swevent_disable(struct perf_event *event)
4030{
4031}
4032
4033static const struct pmu perf_ops_generic = {
4034 .enable = perf_swevent_enable,
4035 .disable = perf_swevent_disable,
4036 .read = perf_swevent_read,
4037 .unthrottle = perf_swevent_unthrottle,
4038};
4039
4040/*
4041 * hrtimer based swevent callback
4042 */
4043
4044static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4045{
4046 enum hrtimer_restart ret = HRTIMER_RESTART;
4047 struct perf_sample_data data;
4048 struct pt_regs *regs;
4049 struct perf_event *event;
4050 u64 period;
4051
4052 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4053 event->pmu->read(event);
4054
4055 data.addr = 0;
Xiao Guangrong21140f42009-12-10 14:00:51 +08004056 data.raw = NULL;
Xiao Guangrong59d069e2009-12-01 17:30:08 +08004057 data.period = event->hw.last_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004058 regs = get_irq_regs();
4059 /*
4060 * In case we exclude kernel IPs or are somehow not in interrupt
4061 * context, provide the next best thing, the user IP.
4062 */
4063 if ((event->attr.exclude_kernel || !regs) &&
4064 !event->attr.exclude_user)
4065 regs = task_pt_regs(current);
4066
4067 if (regs) {
Soeren Sandmann54f44072009-10-22 18:34:08 +02004068 if (!(event->attr.exclude_idle && current->pid == 0))
4069 if (perf_event_overflow(event, 0, &data, regs))
4070 ret = HRTIMER_NORESTART;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004071 }
4072
4073 period = max_t(u64, 10000, event->hw.sample_period);
4074 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4075
4076 return ret;
4077}
4078
Soeren Sandmann721a6692009-09-15 14:33:08 +02004079static void perf_swevent_start_hrtimer(struct perf_event *event)
4080{
4081 struct hw_perf_event *hwc = &event->hw;
4082
4083 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4084 hwc->hrtimer.function = perf_swevent_hrtimer;
4085 if (hwc->sample_period) {
4086 u64 period;
4087
4088 if (hwc->remaining) {
4089 if (hwc->remaining < 0)
4090 period = 10000;
4091 else
4092 period = hwc->remaining;
4093 hwc->remaining = 0;
4094 } else {
4095 period = max_t(u64, 10000, hwc->sample_period);
4096 }
4097 __hrtimer_start_range_ns(&hwc->hrtimer,
4098 ns_to_ktime(period), 0,
4099 HRTIMER_MODE_REL, 0);
4100 }
4101}
4102
4103static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4104{
4105 struct hw_perf_event *hwc = &event->hw;
4106
4107 if (hwc->sample_period) {
4108 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4109 hwc->remaining = ktime_to_ns(remaining);
4110
4111 hrtimer_cancel(&hwc->hrtimer);
4112 }
4113}
4114
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004115/*
4116 * Software event: cpu wall time clock
4117 */
4118
4119static void cpu_clock_perf_event_update(struct perf_event *event)
4120{
4121 int cpu = raw_smp_processor_id();
4122 s64 prev;
4123 u64 now;
4124
4125 now = cpu_clock(cpu);
Xiao Guangrongec89a062009-12-09 11:30:36 +08004126 prev = atomic64_xchg(&event->hw.prev_count, now);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004127 atomic64_add(now - prev, &event->count);
4128}
4129
4130static int cpu_clock_perf_event_enable(struct perf_event *event)
4131{
4132 struct hw_perf_event *hwc = &event->hw;
4133 int cpu = raw_smp_processor_id();
4134
4135 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Soeren Sandmann721a6692009-09-15 14:33:08 +02004136 perf_swevent_start_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004137
4138 return 0;
4139}
4140
4141static void cpu_clock_perf_event_disable(struct perf_event *event)
4142{
Soeren Sandmann721a6692009-09-15 14:33:08 +02004143 perf_swevent_cancel_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004144 cpu_clock_perf_event_update(event);
4145}
4146
4147static void cpu_clock_perf_event_read(struct perf_event *event)
4148{
4149 cpu_clock_perf_event_update(event);
4150}
4151
4152static const struct pmu perf_ops_cpu_clock = {
4153 .enable = cpu_clock_perf_event_enable,
4154 .disable = cpu_clock_perf_event_disable,
4155 .read = cpu_clock_perf_event_read,
4156};
4157
4158/*
4159 * Software event: task time clock
4160 */
4161
4162static void task_clock_perf_event_update(struct perf_event *event, u64 now)
4163{
4164 u64 prev;
4165 s64 delta;
4166
4167 prev = atomic64_xchg(&event->hw.prev_count, now);
4168 delta = now - prev;
4169 atomic64_add(delta, &event->count);
4170}
4171
4172static int task_clock_perf_event_enable(struct perf_event *event)
4173{
4174 struct hw_perf_event *hwc = &event->hw;
4175 u64 now;
4176
4177 now = event->ctx->time;
4178
4179 atomic64_set(&hwc->prev_count, now);
Soeren Sandmann721a6692009-09-15 14:33:08 +02004180
4181 perf_swevent_start_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004182
4183 return 0;
4184}
4185
4186static void task_clock_perf_event_disable(struct perf_event *event)
4187{
Soeren Sandmann721a6692009-09-15 14:33:08 +02004188 perf_swevent_cancel_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004189 task_clock_perf_event_update(event, event->ctx->time);
4190
4191}
4192
4193static void task_clock_perf_event_read(struct perf_event *event)
4194{
4195 u64 time;
4196
4197 if (!in_nmi()) {
4198 update_context_time(event->ctx);
4199 time = event->ctx->time;
4200 } else {
4201 u64 now = perf_clock();
4202 u64 delta = now - event->ctx->timestamp;
4203 time = event->ctx->time + delta;
4204 }
4205
4206 task_clock_perf_event_update(event, time);
4207}
4208
4209static const struct pmu perf_ops_task_clock = {
4210 .enable = task_clock_perf_event_enable,
4211 .disable = task_clock_perf_event_disable,
4212 .read = task_clock_perf_event_read,
4213};
4214
Li Zefan07b139c2009-12-21 14:27:35 +08004215#ifdef CONFIG_EVENT_TRACING
Li Zefan6fb29152009-10-15 11:21:42 +08004216
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004217void perf_tp_event(int event_id, u64 addr, u64 count, void *record,
4218 int entry_size)
4219{
4220 struct perf_raw_record raw = {
4221 .size = entry_size,
4222 .data = record,
4223 };
4224
4225 struct perf_sample_data data = {
4226 .addr = addr,
4227 .raw = &raw,
4228 };
4229
4230 struct pt_regs *regs = get_irq_regs();
4231
4232 if (!regs)
4233 regs = task_pt_regs(current);
4234
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004235 /* Trace events already protected against recursion */
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004236 do_perf_sw_event(PERF_TYPE_TRACEPOINT, event_id, count, 1,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004237 &data, regs);
4238}
4239EXPORT_SYMBOL_GPL(perf_tp_event);
4240
Li Zefan6fb29152009-10-15 11:21:42 +08004241static int perf_tp_event_match(struct perf_event *event,
4242 struct perf_sample_data *data)
4243{
4244 void *record = data->raw->data;
4245
4246 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4247 return 1;
4248 return 0;
4249}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004250
4251static void tp_perf_event_destroy(struct perf_event *event)
4252{
4253 ftrace_profile_disable(event->attr.config);
4254}
4255
4256static const struct pmu *tp_perf_event_init(struct perf_event *event)
4257{
4258 /*
4259 * Raw tracepoint data is a severe data leak, only allow root to
4260 * have these.
4261 */
4262 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4263 perf_paranoid_tracepoint_raw() &&
4264 !capable(CAP_SYS_ADMIN))
4265 return ERR_PTR(-EPERM);
4266
4267 if (ftrace_profile_enable(event->attr.config))
4268 return NULL;
4269
4270 event->destroy = tp_perf_event_destroy;
4271
4272 return &perf_ops_generic;
4273}
Li Zefan6fb29152009-10-15 11:21:42 +08004274
4275static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4276{
4277 char *filter_str;
4278 int ret;
4279
4280 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4281 return -EINVAL;
4282
4283 filter_str = strndup_user(arg, PAGE_SIZE);
4284 if (IS_ERR(filter_str))
4285 return PTR_ERR(filter_str);
4286
4287 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4288
4289 kfree(filter_str);
4290 return ret;
4291}
4292
4293static void perf_event_free_filter(struct perf_event *event)
4294{
4295 ftrace_profile_free_filter(event);
4296}
4297
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004298#else
Li Zefan6fb29152009-10-15 11:21:42 +08004299
4300static int perf_tp_event_match(struct perf_event *event,
4301 struct perf_sample_data *data)
4302{
4303 return 1;
4304}
4305
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004306static const struct pmu *tp_perf_event_init(struct perf_event *event)
4307{
4308 return NULL;
4309}
Li Zefan6fb29152009-10-15 11:21:42 +08004310
4311static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4312{
4313 return -ENOENT;
4314}
4315
4316static void perf_event_free_filter(struct perf_event *event)
4317{
4318}
4319
Li Zefan07b139c2009-12-21 14:27:35 +08004320#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004321
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004322#ifdef CONFIG_HAVE_HW_BREAKPOINT
4323static void bp_perf_event_destroy(struct perf_event *event)
4324{
4325 release_bp_slot(event);
4326}
4327
4328static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4329{
4330 int err;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004331
4332 err = register_perf_hw_breakpoint(bp);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004333 if (err)
4334 return ERR_PTR(err);
4335
4336 bp->destroy = bp_perf_event_destroy;
4337
4338 return &perf_ops_bp;
4339}
4340
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004341void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004342{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004343 struct perf_sample_data sample;
4344 struct pt_regs *regs = data;
4345
Xiao Guangrong5e855db52009-12-10 17:08:54 +08004346 sample.raw = NULL;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004347 sample.addr = bp->attr.bp_addr;
4348
4349 if (!perf_exclude_event(bp, regs))
4350 perf_swevent_add(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004351}
4352#else
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004353static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4354{
4355 return NULL;
4356}
4357
4358void perf_bp_event(struct perf_event *bp, void *regs)
4359{
4360}
4361#endif
4362
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004363atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4364
4365static void sw_perf_event_destroy(struct perf_event *event)
4366{
4367 u64 event_id = event->attr.config;
4368
4369 WARN_ON(event->parent);
4370
4371 atomic_dec(&perf_swevent_enabled[event_id]);
4372}
4373
4374static const struct pmu *sw_perf_event_init(struct perf_event *event)
4375{
4376 const struct pmu *pmu = NULL;
4377 u64 event_id = event->attr.config;
4378
4379 /*
4380 * Software events (currently) can't in general distinguish
4381 * between user, kernel and hypervisor events.
4382 * However, context switches and cpu migrations are considered
4383 * to be kernel events, and page faults are never hypervisor
4384 * events.
4385 */
4386 switch (event_id) {
4387 case PERF_COUNT_SW_CPU_CLOCK:
4388 pmu = &perf_ops_cpu_clock;
4389
4390 break;
4391 case PERF_COUNT_SW_TASK_CLOCK:
4392 /*
4393 * If the user instantiates this as a per-cpu event,
4394 * use the cpu_clock event instead.
4395 */
4396 if (event->ctx->task)
4397 pmu = &perf_ops_task_clock;
4398 else
4399 pmu = &perf_ops_cpu_clock;
4400
4401 break;
4402 case PERF_COUNT_SW_PAGE_FAULTS:
4403 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4404 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4405 case PERF_COUNT_SW_CONTEXT_SWITCHES:
4406 case PERF_COUNT_SW_CPU_MIGRATIONS:
Anton Blanchardf7d79862009-10-18 01:09:29 +00004407 case PERF_COUNT_SW_ALIGNMENT_FAULTS:
4408 case PERF_COUNT_SW_EMULATION_FAULTS:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004409 if (!event->parent) {
4410 atomic_inc(&perf_swevent_enabled[event_id]);
4411 event->destroy = sw_perf_event_destroy;
4412 }
4413 pmu = &perf_ops_generic;
4414 break;
4415 }
4416
4417 return pmu;
4418}
4419
4420/*
4421 * Allocate and initialize a event structure
4422 */
4423static struct perf_event *
4424perf_event_alloc(struct perf_event_attr *attr,
4425 int cpu,
4426 struct perf_event_context *ctx,
4427 struct perf_event *group_leader,
4428 struct perf_event *parent_event,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004429 perf_overflow_handler_t overflow_handler,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004430 gfp_t gfpflags)
4431{
4432 const struct pmu *pmu;
4433 struct perf_event *event;
4434 struct hw_perf_event *hwc;
4435 long err;
4436
4437 event = kzalloc(sizeof(*event), gfpflags);
4438 if (!event)
4439 return ERR_PTR(-ENOMEM);
4440
4441 /*
4442 * Single events are their own group leaders, with an
4443 * empty sibling list:
4444 */
4445 if (!group_leader)
4446 group_leader = event;
4447
4448 mutex_init(&event->child_mutex);
4449 INIT_LIST_HEAD(&event->child_list);
4450
4451 INIT_LIST_HEAD(&event->group_entry);
4452 INIT_LIST_HEAD(&event->event_entry);
4453 INIT_LIST_HEAD(&event->sibling_list);
4454 init_waitqueue_head(&event->waitq);
4455
4456 mutex_init(&event->mmap_mutex);
4457
4458 event->cpu = cpu;
4459 event->attr = *attr;
4460 event->group_leader = group_leader;
4461 event->pmu = NULL;
4462 event->ctx = ctx;
4463 event->oncpu = -1;
4464
4465 event->parent = parent_event;
4466
4467 event->ns = get_pid_ns(current->nsproxy->pid_ns);
4468 event->id = atomic64_inc_return(&perf_event_id);
4469
4470 event->state = PERF_EVENT_STATE_INACTIVE;
4471
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004472 if (!overflow_handler && parent_event)
4473 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004474
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004475 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004476
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004477 if (attr->disabled)
4478 event->state = PERF_EVENT_STATE_OFF;
4479
4480 pmu = NULL;
4481
4482 hwc = &event->hw;
4483 hwc->sample_period = attr->sample_period;
4484 if (attr->freq && attr->sample_freq)
4485 hwc->sample_period = 1;
4486 hwc->last_period = hwc->sample_period;
4487
4488 atomic64_set(&hwc->period_left, hwc->sample_period);
4489
4490 /*
4491 * we currently do not support PERF_FORMAT_GROUP on inherited events
4492 */
4493 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
4494 goto done;
4495
4496 switch (attr->type) {
4497 case PERF_TYPE_RAW:
4498 case PERF_TYPE_HARDWARE:
4499 case PERF_TYPE_HW_CACHE:
4500 pmu = hw_perf_event_init(event);
4501 break;
4502
4503 case PERF_TYPE_SOFTWARE:
4504 pmu = sw_perf_event_init(event);
4505 break;
4506
4507 case PERF_TYPE_TRACEPOINT:
4508 pmu = tp_perf_event_init(event);
4509 break;
4510
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004511 case PERF_TYPE_BREAKPOINT:
4512 pmu = bp_perf_event_init(event);
4513 break;
4514
4515
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004516 default:
4517 break;
4518 }
4519done:
4520 err = 0;
4521 if (!pmu)
4522 err = -EINVAL;
4523 else if (IS_ERR(pmu))
4524 err = PTR_ERR(pmu);
4525
4526 if (err) {
4527 if (event->ns)
4528 put_pid_ns(event->ns);
4529 kfree(event);
4530 return ERR_PTR(err);
4531 }
4532
4533 event->pmu = pmu;
4534
4535 if (!event->parent) {
4536 atomic_inc(&nr_events);
4537 if (event->attr.mmap)
4538 atomic_inc(&nr_mmap_events);
4539 if (event->attr.comm)
4540 atomic_inc(&nr_comm_events);
4541 if (event->attr.task)
4542 atomic_inc(&nr_task_events);
4543 }
4544
4545 return event;
4546}
4547
4548static int perf_copy_attr(struct perf_event_attr __user *uattr,
4549 struct perf_event_attr *attr)
4550{
4551 u32 size;
4552 int ret;
4553
4554 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4555 return -EFAULT;
4556
4557 /*
4558 * zero the full structure, so that a short copy will be nice.
4559 */
4560 memset(attr, 0, sizeof(*attr));
4561
4562 ret = get_user(size, &uattr->size);
4563 if (ret)
4564 return ret;
4565
4566 if (size > PAGE_SIZE) /* silly large */
4567 goto err_size;
4568
4569 if (!size) /* abi compat */
4570 size = PERF_ATTR_SIZE_VER0;
4571
4572 if (size < PERF_ATTR_SIZE_VER0)
4573 goto err_size;
4574
4575 /*
4576 * If we're handed a bigger struct than we know of,
4577 * ensure all the unknown bits are 0 - i.e. new
4578 * user-space does not rely on any kernel feature
4579 * extensions we dont know about yet.
4580 */
4581 if (size > sizeof(*attr)) {
4582 unsigned char __user *addr;
4583 unsigned char __user *end;
4584 unsigned char val;
4585
4586 addr = (void __user *)uattr + sizeof(*attr);
4587 end = (void __user *)uattr + size;
4588
4589 for (; addr < end; addr++) {
4590 ret = get_user(val, addr);
4591 if (ret)
4592 return ret;
4593 if (val)
4594 goto err_size;
4595 }
4596 size = sizeof(*attr);
4597 }
4598
4599 ret = copy_from_user(attr, uattr, size);
4600 if (ret)
4601 return -EFAULT;
4602
4603 /*
4604 * If the type exists, the corresponding creation will verify
4605 * the attr->config.
4606 */
4607 if (attr->type >= PERF_TYPE_MAX)
4608 return -EINVAL;
4609
Peter Zijlstraf13c12c2009-12-15 19:43:11 +01004610 if (attr->__reserved_1 || attr->__reserved_2)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004611 return -EINVAL;
4612
4613 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4614 return -EINVAL;
4615
4616 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4617 return -EINVAL;
4618
4619out:
4620 return ret;
4621
4622err_size:
4623 put_user(sizeof(*attr), &uattr->size);
4624 ret = -E2BIG;
4625 goto out;
4626}
4627
Li Zefan6fb29152009-10-15 11:21:42 +08004628static int perf_event_set_output(struct perf_event *event, int output_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004629{
4630 struct perf_event *output_event = NULL;
4631 struct file *output_file = NULL;
4632 struct perf_event *old_output;
4633 int fput_needed = 0;
4634 int ret = -EINVAL;
4635
4636 if (!output_fd)
4637 goto set;
4638
4639 output_file = fget_light(output_fd, &fput_needed);
4640 if (!output_file)
4641 return -EBADF;
4642
4643 if (output_file->f_op != &perf_fops)
4644 goto out;
4645
4646 output_event = output_file->private_data;
4647
4648 /* Don't chain output fds */
4649 if (output_event->output)
4650 goto out;
4651
4652 /* Don't set an output fd when we already have an output channel */
4653 if (event->data)
4654 goto out;
4655
4656 atomic_long_inc(&output_file->f_count);
4657
4658set:
4659 mutex_lock(&event->mmap_mutex);
4660 old_output = event->output;
4661 rcu_assign_pointer(event->output, output_event);
4662 mutex_unlock(&event->mmap_mutex);
4663
4664 if (old_output) {
4665 /*
4666 * we need to make sure no existing perf_output_*()
4667 * is still referencing this event.
4668 */
4669 synchronize_rcu();
4670 fput(old_output->filp);
4671 }
4672
4673 ret = 0;
4674out:
4675 fput_light(output_file, fput_needed);
4676 return ret;
4677}
4678
4679/**
4680 * sys_perf_event_open - open a performance event, associate it to a task/cpu
4681 *
4682 * @attr_uptr: event_id type attributes for monitoring/sampling
4683 * @pid: target pid
4684 * @cpu: target cpu
4685 * @group_fd: group leader event fd
4686 */
4687SYSCALL_DEFINE5(perf_event_open,
4688 struct perf_event_attr __user *, attr_uptr,
4689 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
4690{
4691 struct perf_event *event, *group_leader;
4692 struct perf_event_attr attr;
4693 struct perf_event_context *ctx;
4694 struct file *event_file = NULL;
4695 struct file *group_file = NULL;
4696 int fput_needed = 0;
4697 int fput_needed2 = 0;
4698 int err;
4699
4700 /* for future expandability... */
4701 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
4702 return -EINVAL;
4703
4704 err = perf_copy_attr(attr_uptr, &attr);
4705 if (err)
4706 return err;
4707
4708 if (!attr.exclude_kernel) {
4709 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4710 return -EACCES;
4711 }
4712
4713 if (attr.freq) {
4714 if (attr.sample_freq > sysctl_perf_event_sample_rate)
4715 return -EINVAL;
4716 }
4717
4718 /*
4719 * Get the target context (task or percpu):
4720 */
4721 ctx = find_get_context(pid, cpu);
4722 if (IS_ERR(ctx))
4723 return PTR_ERR(ctx);
4724
4725 /*
4726 * Look up the group leader (we will attach this event to it):
4727 */
4728 group_leader = NULL;
4729 if (group_fd != -1 && !(flags & PERF_FLAG_FD_NO_GROUP)) {
4730 err = -EINVAL;
4731 group_file = fget_light(group_fd, &fput_needed);
4732 if (!group_file)
4733 goto err_put_context;
4734 if (group_file->f_op != &perf_fops)
4735 goto err_put_context;
4736
4737 group_leader = group_file->private_data;
4738 /*
4739 * Do not allow a recursive hierarchy (this new sibling
4740 * becoming part of another group-sibling):
4741 */
4742 if (group_leader->group_leader != group_leader)
4743 goto err_put_context;
4744 /*
4745 * Do not allow to attach to a group in a different
4746 * task or CPU context:
4747 */
4748 if (group_leader->ctx != ctx)
4749 goto err_put_context;
4750 /*
4751 * Only a group leader can be exclusive or pinned
4752 */
4753 if (attr.exclusive || attr.pinned)
4754 goto err_put_context;
4755 }
4756
4757 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004758 NULL, NULL, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004759 err = PTR_ERR(event);
4760 if (IS_ERR(event))
4761 goto err_put_context;
4762
Roland Dreier628ff7c2009-12-18 09:41:24 -08004763 err = anon_inode_getfd("[perf_event]", &perf_fops, event, O_RDWR);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004764 if (err < 0)
4765 goto err_free_put_context;
4766
4767 event_file = fget_light(err, &fput_needed2);
4768 if (!event_file)
4769 goto err_free_put_context;
4770
4771 if (flags & PERF_FLAG_FD_OUTPUT) {
4772 err = perf_event_set_output(event, group_fd);
4773 if (err)
4774 goto err_fput_free_put_context;
4775 }
4776
4777 event->filp = event_file;
4778 WARN_ON_ONCE(ctx->parent_ctx);
4779 mutex_lock(&ctx->mutex);
4780 perf_install_in_context(ctx, event, cpu);
4781 ++ctx->generation;
4782 mutex_unlock(&ctx->mutex);
4783
4784 event->owner = current;
4785 get_task_struct(current);
4786 mutex_lock(&current->perf_event_mutex);
4787 list_add_tail(&event->owner_entry, &current->perf_event_list);
4788 mutex_unlock(&current->perf_event_mutex);
4789
4790err_fput_free_put_context:
4791 fput_light(event_file, fput_needed2);
4792
4793err_free_put_context:
4794 if (err < 0)
4795 kfree(event);
4796
4797err_put_context:
4798 if (err < 0)
4799 put_ctx(ctx);
4800
4801 fput_light(group_file, fput_needed);
4802
4803 return err;
4804}
4805
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004806/**
4807 * perf_event_create_kernel_counter
4808 *
4809 * @attr: attributes of the counter to create
4810 * @cpu: cpu in which the counter is bound
4811 * @pid: task to profile
4812 */
4813struct perf_event *
4814perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004815 pid_t pid,
4816 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004817{
4818 struct perf_event *event;
4819 struct perf_event_context *ctx;
4820 int err;
4821
4822 /*
4823 * Get the target context (task or percpu):
4824 */
4825
4826 ctx = find_get_context(pid, cpu);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01004827 if (IS_ERR(ctx)) {
4828 err = PTR_ERR(ctx);
4829 goto err_exit;
4830 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004831
4832 event = perf_event_alloc(attr, cpu, ctx, NULL,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004833 NULL, overflow_handler, GFP_KERNEL);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01004834 if (IS_ERR(event)) {
4835 err = PTR_ERR(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004836 goto err_put_context;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01004837 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004838
4839 event->filp = NULL;
4840 WARN_ON_ONCE(ctx->parent_ctx);
4841 mutex_lock(&ctx->mutex);
4842 perf_install_in_context(ctx, event, cpu);
4843 ++ctx->generation;
4844 mutex_unlock(&ctx->mutex);
4845
4846 event->owner = current;
4847 get_task_struct(current);
4848 mutex_lock(&current->perf_event_mutex);
4849 list_add_tail(&event->owner_entry, &current->perf_event_list);
4850 mutex_unlock(&current->perf_event_mutex);
4851
4852 return event;
4853
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01004854 err_put_context:
4855 put_ctx(ctx);
4856 err_exit:
4857 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004858}
4859EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
4860
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004861/*
4862 * inherit a event from parent task to child task:
4863 */
4864static struct perf_event *
4865inherit_event(struct perf_event *parent_event,
4866 struct task_struct *parent,
4867 struct perf_event_context *parent_ctx,
4868 struct task_struct *child,
4869 struct perf_event *group_leader,
4870 struct perf_event_context *child_ctx)
4871{
4872 struct perf_event *child_event;
4873
4874 /*
4875 * Instead of creating recursive hierarchies of events,
4876 * we link inherited events back to the original parent,
4877 * which has a filp for sure, which we use as the reference
4878 * count:
4879 */
4880 if (parent_event->parent)
4881 parent_event = parent_event->parent;
4882
4883 child_event = perf_event_alloc(&parent_event->attr,
4884 parent_event->cpu, child_ctx,
4885 group_leader, parent_event,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004886 NULL, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004887 if (IS_ERR(child_event))
4888 return child_event;
4889 get_ctx(child_ctx);
4890
4891 /*
4892 * Make the child state follow the state of the parent event,
4893 * not its attr.disabled bit. We hold the parent's mutex,
4894 * so we won't race with perf_event_{en, dis}able_family.
4895 */
4896 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
4897 child_event->state = PERF_EVENT_STATE_INACTIVE;
4898 else
4899 child_event->state = PERF_EVENT_STATE_OFF;
4900
4901 if (parent_event->attr.freq)
4902 child_event->hw.sample_period = parent_event->hw.sample_period;
4903
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004904 child_event->overflow_handler = parent_event->overflow_handler;
4905
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004906 /*
4907 * Link it up in the child's context:
4908 */
4909 add_event_to_ctx(child_event, child_ctx);
4910
4911 /*
4912 * Get a reference to the parent filp - we will fput it
4913 * when the child event exits. This is safe to do because
4914 * we are in the parent and we know that the filp still
4915 * exists and has a nonzero count:
4916 */
4917 atomic_long_inc(&parent_event->filp->f_count);
4918
4919 /*
4920 * Link this into the parent event's child list
4921 */
4922 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
4923 mutex_lock(&parent_event->child_mutex);
4924 list_add_tail(&child_event->child_list, &parent_event->child_list);
4925 mutex_unlock(&parent_event->child_mutex);
4926
4927 return child_event;
4928}
4929
4930static int inherit_group(struct perf_event *parent_event,
4931 struct task_struct *parent,
4932 struct perf_event_context *parent_ctx,
4933 struct task_struct *child,
4934 struct perf_event_context *child_ctx)
4935{
4936 struct perf_event *leader;
4937 struct perf_event *sub;
4938 struct perf_event *child_ctr;
4939
4940 leader = inherit_event(parent_event, parent, parent_ctx,
4941 child, NULL, child_ctx);
4942 if (IS_ERR(leader))
4943 return PTR_ERR(leader);
4944 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
4945 child_ctr = inherit_event(sub, parent, parent_ctx,
4946 child, leader, child_ctx);
4947 if (IS_ERR(child_ctr))
4948 return PTR_ERR(child_ctr);
4949 }
4950 return 0;
4951}
4952
4953static void sync_child_event(struct perf_event *child_event,
4954 struct task_struct *child)
4955{
4956 struct perf_event *parent_event = child_event->parent;
4957 u64 child_val;
4958
4959 if (child_event->attr.inherit_stat)
4960 perf_event_read_event(child_event, child);
4961
4962 child_val = atomic64_read(&child_event->count);
4963
4964 /*
4965 * Add back the child's count to the parent's count:
4966 */
4967 atomic64_add(child_val, &parent_event->count);
4968 atomic64_add(child_event->total_time_enabled,
4969 &parent_event->child_total_time_enabled);
4970 atomic64_add(child_event->total_time_running,
4971 &parent_event->child_total_time_running);
4972
4973 /*
4974 * Remove this event from the parent's list
4975 */
4976 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
4977 mutex_lock(&parent_event->child_mutex);
4978 list_del_init(&child_event->child_list);
4979 mutex_unlock(&parent_event->child_mutex);
4980
4981 /*
4982 * Release the parent event, if this was the last
4983 * reference to it.
4984 */
4985 fput(parent_event->filp);
4986}
4987
4988static void
4989__perf_event_exit_task(struct perf_event *child_event,
4990 struct perf_event_context *child_ctx,
4991 struct task_struct *child)
4992{
4993 struct perf_event *parent_event;
4994
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004995 perf_event_remove_from_context(child_event);
4996
4997 parent_event = child_event->parent;
4998 /*
4999 * It can happen that parent exits first, and has events
5000 * that are still around due to the child reference. These
5001 * events need to be zapped - but otherwise linger.
5002 */
5003 if (parent_event) {
5004 sync_child_event(child_event, child);
5005 free_event(child_event);
5006 }
5007}
5008
5009/*
5010 * When a child task exits, feed back event values to parent events.
5011 */
5012void perf_event_exit_task(struct task_struct *child)
5013{
5014 struct perf_event *child_event, *tmp;
5015 struct perf_event_context *child_ctx;
5016 unsigned long flags;
5017
5018 if (likely(!child->perf_event_ctxp)) {
5019 perf_event_task(child, NULL, 0);
5020 return;
5021 }
5022
5023 local_irq_save(flags);
5024 /*
5025 * We can't reschedule here because interrupts are disabled,
5026 * and either child is current or it is a task that can't be
5027 * scheduled, so we are now safe from rescheduling changing
5028 * our context.
5029 */
5030 child_ctx = child->perf_event_ctxp;
5031 __perf_event_task_sched_out(child_ctx);
5032
5033 /*
5034 * Take the context lock here so that if find_get_context is
5035 * reading child->perf_event_ctxp, we wait until it has
5036 * incremented the context's refcount before we do put_ctx below.
5037 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005038 raw_spin_lock(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005039 child->perf_event_ctxp = NULL;
5040 /*
5041 * If this context is a clone; unclone it so it can't get
5042 * swapped to another process while we're removing all
5043 * the events from it.
5044 */
5045 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01005046 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005047 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005048
5049 /*
5050 * Report the task dead after unscheduling the events so that we
5051 * won't get any samples after PERF_RECORD_EXIT. We can however still
5052 * get a few PERF_RECORD_READ events.
5053 */
5054 perf_event_task(child, child_ctx, 0);
5055
5056 /*
5057 * We can recurse on the same lock type through:
5058 *
5059 * __perf_event_exit_task()
5060 * sync_child_event()
5061 * fput(parent_event->filp)
5062 * perf_release()
5063 * mutex_lock(&ctx->mutex)
5064 *
5065 * But since its the parent context it won't be the same instance.
5066 */
5067 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
5068
5069again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005070 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5071 group_entry)
5072 __perf_event_exit_task(child_event, child_ctx, child);
5073
5074 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005075 group_entry)
5076 __perf_event_exit_task(child_event, child_ctx, child);
5077
5078 /*
5079 * If the last event was a group event, it will have appended all
5080 * its siblings to the list, but we obtained 'tmp' before that which
5081 * will still point to the list head terminating the iteration.
5082 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005083 if (!list_empty(&child_ctx->pinned_groups) ||
5084 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005085 goto again;
5086
5087 mutex_unlock(&child_ctx->mutex);
5088
5089 put_ctx(child_ctx);
5090}
5091
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005092static void perf_free_event(struct perf_event *event,
5093 struct perf_event_context *ctx)
5094{
5095 struct perf_event *parent = event->parent;
5096
5097 if (WARN_ON_ONCE(!parent))
5098 return;
5099
5100 mutex_lock(&parent->child_mutex);
5101 list_del_init(&event->child_list);
5102 mutex_unlock(&parent->child_mutex);
5103
5104 fput(parent->filp);
5105
5106 list_del_event(event, ctx);
5107 free_event(event);
5108}
5109
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005110/*
5111 * free an unexposed, unused context as created by inheritance by
5112 * init_task below, used by fork() in case of fail.
5113 */
5114void perf_event_free_task(struct task_struct *task)
5115{
5116 struct perf_event_context *ctx = task->perf_event_ctxp;
5117 struct perf_event *event, *tmp;
5118
5119 if (!ctx)
5120 return;
5121
5122 mutex_lock(&ctx->mutex);
5123again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005124 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5125 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005126
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005127 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5128 group_entry)
5129 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005130
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005131 if (!list_empty(&ctx->pinned_groups) ||
5132 !list_empty(&ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005133 goto again;
5134
5135 mutex_unlock(&ctx->mutex);
5136
5137 put_ctx(ctx);
5138}
5139
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005140static int
5141inherit_task_group(struct perf_event *event, struct task_struct *parent,
5142 struct perf_event_context *parent_ctx,
5143 struct task_struct *child,
5144 int *inherited_all)
5145{
5146 int ret;
5147 struct perf_event_context *child_ctx = child->perf_event_ctxp;
5148
5149 if (!event->attr.inherit) {
5150 *inherited_all = 0;
5151 return 0;
5152 }
5153
5154 if (!child_ctx) {
5155 /*
5156 * This is executed from the parent task context, so
5157 * inherit events that have been marked for cloning.
5158 * First allocate and initialize a context for the
5159 * child.
5160 */
5161
5162 child_ctx = kzalloc(sizeof(struct perf_event_context),
5163 GFP_KERNEL);
5164 if (!child_ctx)
5165 return -ENOMEM;
5166
5167 __perf_event_init_context(child_ctx, child);
5168 child->perf_event_ctxp = child_ctx;
5169 get_task_struct(child);
5170 }
5171
5172 ret = inherit_group(event, parent, parent_ctx,
5173 child, child_ctx);
5174
5175 if (ret)
5176 *inherited_all = 0;
5177
5178 return ret;
5179}
5180
5181
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005182/*
5183 * Initialize the perf_event context in task_struct
5184 */
5185int perf_event_init_task(struct task_struct *child)
5186{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005187 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005188 struct perf_event_context *cloned_ctx;
5189 struct perf_event *event;
5190 struct task_struct *parent = current;
5191 int inherited_all = 1;
5192 int ret = 0;
5193
5194 child->perf_event_ctxp = NULL;
5195
5196 mutex_init(&child->perf_event_mutex);
5197 INIT_LIST_HEAD(&child->perf_event_list);
5198
5199 if (likely(!parent->perf_event_ctxp))
5200 return 0;
5201
5202 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005203 * If the parent's context is a clone, pin it so it won't get
5204 * swapped under us.
5205 */
5206 parent_ctx = perf_pin_task_context(parent);
5207
5208 /*
5209 * No need to check if parent_ctx != NULL here; since we saw
5210 * it non-NULL earlier, the only reason for it to become NULL
5211 * is if we exit, and since we're currently in the middle of
5212 * a fork we can't be exiting at the same time.
5213 */
5214
5215 /*
5216 * Lock the parent list. No need to lock the child - not PID
5217 * hashed yet and not running, so nobody can access it.
5218 */
5219 mutex_lock(&parent_ctx->mutex);
5220
5221 /*
5222 * We dont have to disable NMIs - we are only looking at
5223 * the list, not manipulating it:
5224 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005225 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
5226 ret = inherit_task_group(event, parent, parent_ctx, child,
5227 &inherited_all);
5228 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005229 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005230 }
5231
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005232 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
5233 ret = inherit_task_group(event, parent, parent_ctx, child,
5234 &inherited_all);
5235 if (ret)
5236 break;
5237 }
5238
5239 child_ctx = child->perf_event_ctxp;
5240
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01005241 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005242 /*
5243 * Mark the child context as a clone of the parent
5244 * context, or of whatever the parent is a clone of.
5245 * Note that if the parent is a clone, it could get
5246 * uncloned at any point, but that doesn't matter
5247 * because the list of events and the generation
5248 * count can't have changed since we took the mutex.
5249 */
5250 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5251 if (cloned_ctx) {
5252 child_ctx->parent_ctx = cloned_ctx;
5253 child_ctx->parent_gen = parent_ctx->parent_gen;
5254 } else {
5255 child_ctx->parent_ctx = parent_ctx;
5256 child_ctx->parent_gen = parent_ctx->generation;
5257 }
5258 get_ctx(child_ctx->parent_ctx);
5259 }
5260
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005261 mutex_unlock(&parent_ctx->mutex);
5262
5263 perf_unpin_context(parent_ctx);
5264
5265 return ret;
5266}
5267
5268static void __cpuinit perf_event_init_cpu(int cpu)
5269{
5270 struct perf_cpu_context *cpuctx;
5271
5272 cpuctx = &per_cpu(perf_cpu_context, cpu);
5273 __perf_event_init_context(&cpuctx->ctx, NULL);
5274
5275 spin_lock(&perf_resource_lock);
5276 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5277 spin_unlock(&perf_resource_lock);
5278
5279 hw_perf_event_setup(cpu);
5280}
5281
5282#ifdef CONFIG_HOTPLUG_CPU
5283static void __perf_event_exit_cpu(void *info)
5284{
5285 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5286 struct perf_event_context *ctx = &cpuctx->ctx;
5287 struct perf_event *event, *tmp;
5288
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005289 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5290 __perf_event_remove_from_context(event);
5291 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005292 __perf_event_remove_from_context(event);
5293}
5294static void perf_event_exit_cpu(int cpu)
5295{
5296 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5297 struct perf_event_context *ctx = &cpuctx->ctx;
5298
5299 mutex_lock(&ctx->mutex);
5300 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5301 mutex_unlock(&ctx->mutex);
5302}
5303#else
5304static inline void perf_event_exit_cpu(int cpu) { }
5305#endif
5306
5307static int __cpuinit
5308perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5309{
5310 unsigned int cpu = (long)hcpu;
5311
5312 switch (action) {
5313
5314 case CPU_UP_PREPARE:
5315 case CPU_UP_PREPARE_FROZEN:
5316 perf_event_init_cpu(cpu);
5317 break;
5318
5319 case CPU_ONLINE:
5320 case CPU_ONLINE_FROZEN:
5321 hw_perf_event_setup_online(cpu);
5322 break;
5323
5324 case CPU_DOWN_PREPARE:
5325 case CPU_DOWN_PREPARE_FROZEN:
5326 perf_event_exit_cpu(cpu);
5327 break;
5328
5329 default:
5330 break;
5331 }
5332
5333 return NOTIFY_OK;
5334}
5335
5336/*
5337 * This has to have a higher priority than migration_notifier in sched.c.
5338 */
5339static struct notifier_block __cpuinitdata perf_cpu_nb = {
5340 .notifier_call = perf_cpu_notify,
5341 .priority = 20,
5342};
5343
5344void __init perf_event_init(void)
5345{
5346 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
5347 (void *)(long)smp_processor_id());
5348 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
5349 (void *)(long)smp_processor_id());
5350 register_cpu_notifier(&perf_cpu_nb);
5351}
5352
5353static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
5354{
5355 return sprintf(buf, "%d\n", perf_reserved_percpu);
5356}
5357
5358static ssize_t
5359perf_set_reserve_percpu(struct sysdev_class *class,
5360 const char *buf,
5361 size_t count)
5362{
5363 struct perf_cpu_context *cpuctx;
5364 unsigned long val;
5365 int err, cpu, mpt;
5366
5367 err = strict_strtoul(buf, 10, &val);
5368 if (err)
5369 return err;
5370 if (val > perf_max_events)
5371 return -EINVAL;
5372
5373 spin_lock(&perf_resource_lock);
5374 perf_reserved_percpu = val;
5375 for_each_online_cpu(cpu) {
5376 cpuctx = &per_cpu(perf_cpu_context, cpu);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005377 raw_spin_lock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005378 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
5379 perf_max_events - perf_reserved_percpu);
5380 cpuctx->max_pertask = mpt;
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005381 raw_spin_unlock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005382 }
5383 spin_unlock(&perf_resource_lock);
5384
5385 return count;
5386}
5387
5388static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
5389{
5390 return sprintf(buf, "%d\n", perf_overcommit);
5391}
5392
5393static ssize_t
5394perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
5395{
5396 unsigned long val;
5397 int err;
5398
5399 err = strict_strtoul(buf, 10, &val);
5400 if (err)
5401 return err;
5402 if (val > 1)
5403 return -EINVAL;
5404
5405 spin_lock(&perf_resource_lock);
5406 perf_overcommit = val;
5407 spin_unlock(&perf_resource_lock);
5408
5409 return count;
5410}
5411
5412static SYSDEV_CLASS_ATTR(
5413 reserve_percpu,
5414 0644,
5415 perf_show_reserve_percpu,
5416 perf_set_reserve_percpu
5417 );
5418
5419static SYSDEV_CLASS_ATTR(
5420 overcommit,
5421 0644,
5422 perf_show_overcommit,
5423 perf_set_overcommit
5424 );
5425
5426static struct attribute *perfclass_attrs[] = {
5427 &attr_reserve_percpu.attr,
5428 &attr_overcommit.attr,
5429 NULL
5430};
5431
5432static struct attribute_group perfclass_attr_group = {
5433 .attrs = perfclass_attrs,
5434 .name = "perf_events",
5435};
5436
5437static int __init perf_event_sysfs_init(void)
5438{
5439 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
5440 &perfclass_attr_group);
5441}
5442device_initcall(perf_event_sysfs_init);