blob: 5b987b4a98a8d90ca508eeeeba6d02cc610e081f [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
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200292/*
293 * Add a event from the lists for its context.
294 * Must be called with ctx->mutex and ctx->lock held.
295 */
296static void
297list_add_event(struct perf_event *event, struct perf_event_context *ctx)
298{
299 struct perf_event *group_leader = event->group_leader;
300
301 /*
302 * Depending on whether it is a standalone or sibling event,
303 * add it straight to the context's event list, or to the group
304 * leader's sibling list:
305 */
306 if (group_leader == event)
307 list_add_tail(&event->group_entry, &ctx->group_list);
308 else {
309 list_add_tail(&event->group_entry, &group_leader->sibling_list);
310 group_leader->nr_siblings++;
311 }
312
313 list_add_rcu(&event->event_entry, &ctx->event_list);
314 ctx->nr_events++;
315 if (event->attr.inherit_stat)
316 ctx->nr_stat++;
317}
318
319/*
320 * Remove a event from the lists for its context.
321 * Must be called with ctx->mutex and ctx->lock held.
322 */
323static void
324list_del_event(struct perf_event *event, struct perf_event_context *ctx)
325{
326 struct perf_event *sibling, *tmp;
327
328 if (list_empty(&event->group_entry))
329 return;
330 ctx->nr_events--;
331 if (event->attr.inherit_stat)
332 ctx->nr_stat--;
333
334 list_del_init(&event->group_entry);
335 list_del_rcu(&event->event_entry);
336
337 if (event->group_leader != event)
338 event->group_leader->nr_siblings--;
339
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100340 update_event_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800341
342 /*
343 * If event was in error state, then keep it
344 * that way, otherwise bogus counts will be
345 * returned on read(). The only way to get out
346 * of error state is by explicit re-enabling
347 * of the event
348 */
349 if (event->state > PERF_EVENT_STATE_OFF)
350 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100351
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200352 /*
353 * If this was a group event with sibling events then
354 * upgrade the siblings to singleton events by adding them
355 * to the context list directly:
356 */
357 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
358
359 list_move_tail(&sibling->group_entry, &ctx->group_list);
360 sibling->group_leader = sibling;
361 }
362}
363
364static void
365event_sched_out(struct perf_event *event,
366 struct perf_cpu_context *cpuctx,
367 struct perf_event_context *ctx)
368{
369 if (event->state != PERF_EVENT_STATE_ACTIVE)
370 return;
371
372 event->state = PERF_EVENT_STATE_INACTIVE;
373 if (event->pending_disable) {
374 event->pending_disable = 0;
375 event->state = PERF_EVENT_STATE_OFF;
376 }
377 event->tstamp_stopped = ctx->time;
378 event->pmu->disable(event);
379 event->oncpu = -1;
380
381 if (!is_software_event(event))
382 cpuctx->active_oncpu--;
383 ctx->nr_active--;
384 if (event->attr.exclusive || !cpuctx->active_oncpu)
385 cpuctx->exclusive = 0;
386}
387
388static void
389group_sched_out(struct perf_event *group_event,
390 struct perf_cpu_context *cpuctx,
391 struct perf_event_context *ctx)
392{
393 struct perf_event *event;
394
395 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
396 return;
397
398 event_sched_out(group_event, cpuctx, ctx);
399
400 /*
401 * Schedule out siblings (if any):
402 */
403 list_for_each_entry(event, &group_event->sibling_list, group_entry)
404 event_sched_out(event, cpuctx, ctx);
405
406 if (group_event->attr.exclusive)
407 cpuctx->exclusive = 0;
408}
409
410/*
411 * Cross CPU call to remove a performance event
412 *
413 * We disable the event on the hardware level first. After that we
414 * remove it from the context list.
415 */
416static void __perf_event_remove_from_context(void *info)
417{
418 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
419 struct perf_event *event = info;
420 struct perf_event_context *ctx = event->ctx;
421
422 /*
423 * If this is a task context, we need to check whether it is
424 * the current task context of this cpu. If not it has been
425 * scheduled out before the smp call arrived.
426 */
427 if (ctx->task && cpuctx->task_ctx != ctx)
428 return;
429
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100430 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200431 /*
432 * Protect the list operation against NMI by disabling the
433 * events on a global level.
434 */
435 perf_disable();
436
437 event_sched_out(event, cpuctx, ctx);
438
439 list_del_event(event, ctx);
440
441 if (!ctx->task) {
442 /*
443 * Allow more per task events with respect to the
444 * reservation:
445 */
446 cpuctx->max_pertask =
447 min(perf_max_events - ctx->nr_events,
448 perf_max_events - perf_reserved_percpu);
449 }
450
451 perf_enable();
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100452 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200453}
454
455
456/*
457 * Remove the event from a task's (or a CPU's) list of events.
458 *
459 * Must be called with ctx->mutex held.
460 *
461 * CPU events are removed with a smp call. For task events we only
462 * call when the task is on a CPU.
463 *
464 * If event->ctx is a cloned context, callers must make sure that
465 * every task struct that event->ctx->task could possibly point to
466 * remains valid. This is OK when called from perf_release since
467 * that only calls us on the top-level context, which can't be a clone.
468 * When called from perf_event_exit_task, it's OK because the
469 * context has been detached from its task.
470 */
471static void perf_event_remove_from_context(struct perf_event *event)
472{
473 struct perf_event_context *ctx = event->ctx;
474 struct task_struct *task = ctx->task;
475
476 if (!task) {
477 /*
478 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200479 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200480 */
481 smp_call_function_single(event->cpu,
482 __perf_event_remove_from_context,
483 event, 1);
484 return;
485 }
486
487retry:
488 task_oncpu_function_call(task, __perf_event_remove_from_context,
489 event);
490
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100491 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200492 /*
493 * If the context is active we need to retry the smp call.
494 */
495 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100496 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200497 goto retry;
498 }
499
500 /*
501 * The lock prevents that this context is scheduled in so we
502 * can remove the event safely, if the call above did not
503 * succeed.
504 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100505 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200506 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100507 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200508}
509
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200510/*
511 * Update total_time_enabled and total_time_running for all events in a group.
512 */
513static void update_group_times(struct perf_event *leader)
514{
515 struct perf_event *event;
516
517 update_event_times(leader);
518 list_for_each_entry(event, &leader->sibling_list, group_entry)
519 update_event_times(event);
520}
521
522/*
523 * Cross CPU call to disable a performance event
524 */
525static void __perf_event_disable(void *info)
526{
527 struct perf_event *event = info;
528 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
529 struct perf_event_context *ctx = event->ctx;
530
531 /*
532 * If this is a per-task event, need to check whether this
533 * event's task is the current task on this cpu.
534 */
535 if (ctx->task && cpuctx->task_ctx != ctx)
536 return;
537
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100538 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200539
540 /*
541 * If the event is on, turn it off.
542 * If it is in error state, leave it in error state.
543 */
544 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
545 update_context_time(ctx);
546 update_group_times(event);
547 if (event == event->group_leader)
548 group_sched_out(event, cpuctx, ctx);
549 else
550 event_sched_out(event, cpuctx, ctx);
551 event->state = PERF_EVENT_STATE_OFF;
552 }
553
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100554 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200555}
556
557/*
558 * Disable a event.
559 *
560 * If event->ctx is a cloned context, callers must make sure that
561 * every task struct that event->ctx->task could possibly point to
562 * remains valid. This condition is satisifed when called through
563 * perf_event_for_each_child or perf_event_for_each because they
564 * hold the top-level event's child_mutex, so any descendant that
565 * goes to exit will block in sync_child_event.
566 * When called from perf_pending_event it's OK because event->ctx
567 * is the current context on this CPU and preemption is disabled,
568 * hence we can't get into perf_event_task_sched_out for this context.
569 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100570void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200571{
572 struct perf_event_context *ctx = event->ctx;
573 struct task_struct *task = ctx->task;
574
575 if (!task) {
576 /*
577 * Disable the event on the cpu that it's on
578 */
579 smp_call_function_single(event->cpu, __perf_event_disable,
580 event, 1);
581 return;
582 }
583
584 retry:
585 task_oncpu_function_call(task, __perf_event_disable, event);
586
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100587 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200588 /*
589 * If the event is still active, we need to retry the cross-call.
590 */
591 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100592 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200593 goto retry;
594 }
595
596 /*
597 * Since we have the lock this context can't be scheduled
598 * in, so we can change the state safely.
599 */
600 if (event->state == PERF_EVENT_STATE_INACTIVE) {
601 update_group_times(event);
602 event->state = PERF_EVENT_STATE_OFF;
603 }
604
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100605 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200606}
607
608static int
609event_sched_in(struct perf_event *event,
610 struct perf_cpu_context *cpuctx,
611 struct perf_event_context *ctx,
612 int cpu)
613{
614 if (event->state <= PERF_EVENT_STATE_OFF)
615 return 0;
616
617 event->state = PERF_EVENT_STATE_ACTIVE;
618 event->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
619 /*
620 * The new state must be visible before we turn it on in the hardware:
621 */
622 smp_wmb();
623
624 if (event->pmu->enable(event)) {
625 event->state = PERF_EVENT_STATE_INACTIVE;
626 event->oncpu = -1;
627 return -EAGAIN;
628 }
629
630 event->tstamp_running += ctx->time - event->tstamp_stopped;
631
632 if (!is_software_event(event))
633 cpuctx->active_oncpu++;
634 ctx->nr_active++;
635
636 if (event->attr.exclusive)
637 cpuctx->exclusive = 1;
638
639 return 0;
640}
641
642static int
643group_sched_in(struct perf_event *group_event,
644 struct perf_cpu_context *cpuctx,
645 struct perf_event_context *ctx,
646 int cpu)
647{
648 struct perf_event *event, *partial_group;
649 int ret;
650
651 if (group_event->state == PERF_EVENT_STATE_OFF)
652 return 0;
653
654 ret = hw_perf_group_sched_in(group_event, cpuctx, ctx, cpu);
655 if (ret)
656 return ret < 0 ? ret : 0;
657
658 if (event_sched_in(group_event, cpuctx, ctx, cpu))
659 return -EAGAIN;
660
661 /*
662 * Schedule in siblings as one group (if any):
663 */
664 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
665 if (event_sched_in(event, cpuctx, ctx, cpu)) {
666 partial_group = event;
667 goto group_error;
668 }
669 }
670
671 return 0;
672
673group_error:
674 /*
675 * Groups can be scheduled in as one unit only, so undo any
676 * partial group before returning:
677 */
678 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
679 if (event == partial_group)
680 break;
681 event_sched_out(event, cpuctx, ctx);
682 }
683 event_sched_out(group_event, cpuctx, ctx);
684
685 return -EAGAIN;
686}
687
688/*
689 * Return 1 for a group consisting entirely of software events,
690 * 0 if the group contains any hardware events.
691 */
692static int is_software_only_group(struct perf_event *leader)
693{
694 struct perf_event *event;
695
696 if (!is_software_event(leader))
697 return 0;
698
699 list_for_each_entry(event, &leader->sibling_list, group_entry)
700 if (!is_software_event(event))
701 return 0;
702
703 return 1;
704}
705
706/*
707 * Work out whether we can put this event group on the CPU now.
708 */
709static int group_can_go_on(struct perf_event *event,
710 struct perf_cpu_context *cpuctx,
711 int can_add_hw)
712{
713 /*
714 * Groups consisting entirely of software events can always go on.
715 */
716 if (is_software_only_group(event))
717 return 1;
718 /*
719 * If an exclusive group is already on, no other hardware
720 * events can go on.
721 */
722 if (cpuctx->exclusive)
723 return 0;
724 /*
725 * If this group is exclusive and there are already
726 * events on the CPU, it can't go on.
727 */
728 if (event->attr.exclusive && cpuctx->active_oncpu)
729 return 0;
730 /*
731 * Otherwise, try to add it if all previous groups were able
732 * to go on.
733 */
734 return can_add_hw;
735}
736
737static void add_event_to_ctx(struct perf_event *event,
738 struct perf_event_context *ctx)
739{
740 list_add_event(event, ctx);
741 event->tstamp_enabled = ctx->time;
742 event->tstamp_running = ctx->time;
743 event->tstamp_stopped = ctx->time;
744}
745
746/*
747 * Cross CPU call to install and enable a performance event
748 *
749 * Must be called with ctx->mutex held
750 */
751static void __perf_install_in_context(void *info)
752{
753 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
754 struct perf_event *event = info;
755 struct perf_event_context *ctx = event->ctx;
756 struct perf_event *leader = event->group_leader;
757 int cpu = smp_processor_id();
758 int err;
759
760 /*
761 * If this is a task context, we need to check whether it is
762 * the current task context of this cpu. If not it has been
763 * scheduled out before the smp call arrived.
764 * Or possibly this is the right context but it isn't
765 * on this cpu because it had no events.
766 */
767 if (ctx->task && cpuctx->task_ctx != ctx) {
768 if (cpuctx->task_ctx || ctx->task != current)
769 return;
770 cpuctx->task_ctx = ctx;
771 }
772
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100773 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200774 ctx->is_active = 1;
775 update_context_time(ctx);
776
777 /*
778 * Protect the list operation against NMI by disabling the
779 * events on a global level. NOP for non NMI based events.
780 */
781 perf_disable();
782
783 add_event_to_ctx(event, ctx);
784
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100785 if (event->cpu != -1 && event->cpu != smp_processor_id())
786 goto unlock;
787
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200788 /*
789 * Don't put the event on if it is disabled or if
790 * it is in a group and the group isn't on.
791 */
792 if (event->state != PERF_EVENT_STATE_INACTIVE ||
793 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
794 goto unlock;
795
796 /*
797 * An exclusive event can't go on if there are already active
798 * hardware events, and no hardware event can go on if there
799 * is already an exclusive event on.
800 */
801 if (!group_can_go_on(event, cpuctx, 1))
802 err = -EEXIST;
803 else
804 err = event_sched_in(event, cpuctx, ctx, cpu);
805
806 if (err) {
807 /*
808 * This event couldn't go on. If it is in a group
809 * then we have to pull the whole group off.
810 * If the event group is pinned then put it in error state.
811 */
812 if (leader != event)
813 group_sched_out(leader, cpuctx, ctx);
814 if (leader->attr.pinned) {
815 update_group_times(leader);
816 leader->state = PERF_EVENT_STATE_ERROR;
817 }
818 }
819
820 if (!err && !ctx->task && cpuctx->max_pertask)
821 cpuctx->max_pertask--;
822
823 unlock:
824 perf_enable();
825
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100826 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200827}
828
829/*
830 * Attach a performance event to a context
831 *
832 * First we add the event to the list with the hardware enable bit
833 * in event->hw_config cleared.
834 *
835 * If the event is attached to a task which is on a CPU we use a smp
836 * call to enable it in the task context. The task might have been
837 * scheduled away, but we check this in the smp call again.
838 *
839 * Must be called with ctx->mutex held.
840 */
841static void
842perf_install_in_context(struct perf_event_context *ctx,
843 struct perf_event *event,
844 int cpu)
845{
846 struct task_struct *task = ctx->task;
847
848 if (!task) {
849 /*
850 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200851 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200852 */
853 smp_call_function_single(cpu, __perf_install_in_context,
854 event, 1);
855 return;
856 }
857
858retry:
859 task_oncpu_function_call(task, __perf_install_in_context,
860 event);
861
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100862 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200863 /*
864 * we need to retry the smp call.
865 */
866 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100867 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200868 goto retry;
869 }
870
871 /*
872 * The lock prevents that this context is scheduled in so we
873 * can add the event safely, if it the call above did not
874 * succeed.
875 */
876 if (list_empty(&event->group_entry))
877 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100878 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200879}
880
881/*
882 * Put a event into inactive state and update time fields.
883 * Enabling the leader of a group effectively enables all
884 * the group members that aren't explicitly disabled, so we
885 * have to update their ->tstamp_enabled also.
886 * Note: this works for group members as well as group leaders
887 * since the non-leader members' sibling_lists will be empty.
888 */
889static void __perf_event_mark_enabled(struct perf_event *event,
890 struct perf_event_context *ctx)
891{
892 struct perf_event *sub;
893
894 event->state = PERF_EVENT_STATE_INACTIVE;
895 event->tstamp_enabled = ctx->time - event->total_time_enabled;
896 list_for_each_entry(sub, &event->sibling_list, group_entry)
897 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
898 sub->tstamp_enabled =
899 ctx->time - sub->total_time_enabled;
900}
901
902/*
903 * Cross CPU call to enable a performance event
904 */
905static void __perf_event_enable(void *info)
906{
907 struct perf_event *event = info;
908 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
909 struct perf_event_context *ctx = event->ctx;
910 struct perf_event *leader = event->group_leader;
911 int err;
912
913 /*
914 * If this is a per-task event, need to check whether this
915 * event's task is the current task on this cpu.
916 */
917 if (ctx->task && cpuctx->task_ctx != ctx) {
918 if (cpuctx->task_ctx || ctx->task != current)
919 return;
920 cpuctx->task_ctx = ctx;
921 }
922
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100923 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200924 ctx->is_active = 1;
925 update_context_time(ctx);
926
927 if (event->state >= PERF_EVENT_STATE_INACTIVE)
928 goto unlock;
929 __perf_event_mark_enabled(event, ctx);
930
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100931 if (event->cpu != -1 && event->cpu != smp_processor_id())
932 goto unlock;
933
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200934 /*
935 * If the event is in a group and isn't the group leader,
936 * then don't put it on unless the group is on.
937 */
938 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
939 goto unlock;
940
941 if (!group_can_go_on(event, cpuctx, 1)) {
942 err = -EEXIST;
943 } else {
944 perf_disable();
945 if (event == leader)
946 err = group_sched_in(event, cpuctx, ctx,
947 smp_processor_id());
948 else
949 err = event_sched_in(event, cpuctx, ctx,
950 smp_processor_id());
951 perf_enable();
952 }
953
954 if (err) {
955 /*
956 * If this event can't go on and it's part of a
957 * group, then the whole group has to come off.
958 */
959 if (leader != event)
960 group_sched_out(leader, cpuctx, ctx);
961 if (leader->attr.pinned) {
962 update_group_times(leader);
963 leader->state = PERF_EVENT_STATE_ERROR;
964 }
965 }
966
967 unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100968 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200969}
970
971/*
972 * Enable a event.
973 *
974 * If event->ctx is a cloned context, callers must make sure that
975 * every task struct that event->ctx->task could possibly point to
976 * remains valid. This condition is satisfied when called through
977 * perf_event_for_each_child or perf_event_for_each as described
978 * for perf_event_disable.
979 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100980void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200981{
982 struct perf_event_context *ctx = event->ctx;
983 struct task_struct *task = ctx->task;
984
985 if (!task) {
986 /*
987 * Enable the event on the cpu that it's on
988 */
989 smp_call_function_single(event->cpu, __perf_event_enable,
990 event, 1);
991 return;
992 }
993
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100994 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200995 if (event->state >= PERF_EVENT_STATE_INACTIVE)
996 goto out;
997
998 /*
999 * If the event is in error state, clear that first.
1000 * That way, if we see the event in error state below, we
1001 * know that it has gone back into error state, as distinct
1002 * from the task having been scheduled away before the
1003 * cross-call arrived.
1004 */
1005 if (event->state == PERF_EVENT_STATE_ERROR)
1006 event->state = PERF_EVENT_STATE_OFF;
1007
1008 retry:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001009 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001010 task_oncpu_function_call(task, __perf_event_enable, event);
1011
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001012 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001013
1014 /*
1015 * If the context is active and the event is still off,
1016 * we need to retry the cross-call.
1017 */
1018 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1019 goto retry;
1020
1021 /*
1022 * Since we have the lock this context can't be scheduled
1023 * in, so we can change the state safely.
1024 */
1025 if (event->state == PERF_EVENT_STATE_OFF)
1026 __perf_event_mark_enabled(event, ctx);
1027
1028 out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001029 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001030}
1031
1032static int perf_event_refresh(struct perf_event *event, int refresh)
1033{
1034 /*
1035 * not supported on inherited events
1036 */
1037 if (event->attr.inherit)
1038 return -EINVAL;
1039
1040 atomic_add(refresh, &event->event_limit);
1041 perf_event_enable(event);
1042
1043 return 0;
1044}
1045
1046void __perf_event_sched_out(struct perf_event_context *ctx,
1047 struct perf_cpu_context *cpuctx)
1048{
1049 struct perf_event *event;
1050
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001051 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001052 ctx->is_active = 0;
1053 if (likely(!ctx->nr_events))
1054 goto out;
1055 update_context_time(ctx);
1056
1057 perf_disable();
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +01001058 if (ctx->nr_active) {
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001059 list_for_each_entry(event, &ctx->group_list, group_entry)
1060 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +01001061 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001062 perf_enable();
1063 out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001064 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001065}
1066
1067/*
1068 * Test whether two contexts are equivalent, i.e. whether they
1069 * have both been cloned from the same version of the same context
1070 * and they both have the same number of enabled events.
1071 * If the number of enabled events is the same, then the set
1072 * of enabled events should be the same, because these are both
1073 * inherited contexts, therefore we can't access individual events
1074 * in them directly with an fd; we can only enable/disable all
1075 * events via prctl, or enable/disable all events in a family
1076 * via ioctl, which will have the same effect on both contexts.
1077 */
1078static int context_equiv(struct perf_event_context *ctx1,
1079 struct perf_event_context *ctx2)
1080{
1081 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1082 && ctx1->parent_gen == ctx2->parent_gen
1083 && !ctx1->pin_count && !ctx2->pin_count;
1084}
1085
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001086static void __perf_event_sync_stat(struct perf_event *event,
1087 struct perf_event *next_event)
1088{
1089 u64 value;
1090
1091 if (!event->attr.inherit_stat)
1092 return;
1093
1094 /*
1095 * Update the event value, we cannot use perf_event_read()
1096 * because we're in the middle of a context switch and have IRQs
1097 * disabled, which upsets smp_call_function_single(), however
1098 * we know the event must be on the current CPU, therefore we
1099 * don't need to use it.
1100 */
1101 switch (event->state) {
1102 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001103 event->pmu->read(event);
1104 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001105
1106 case PERF_EVENT_STATE_INACTIVE:
1107 update_event_times(event);
1108 break;
1109
1110 default:
1111 break;
1112 }
1113
1114 /*
1115 * In order to keep per-task stats reliable we need to flip the event
1116 * values when we flip the contexts.
1117 */
1118 value = atomic64_read(&next_event->count);
1119 value = atomic64_xchg(&event->count, value);
1120 atomic64_set(&next_event->count, value);
1121
1122 swap(event->total_time_enabled, next_event->total_time_enabled);
1123 swap(event->total_time_running, next_event->total_time_running);
1124
1125 /*
1126 * Since we swizzled the values, update the user visible data too.
1127 */
1128 perf_event_update_userpage(event);
1129 perf_event_update_userpage(next_event);
1130}
1131
1132#define list_next_entry(pos, member) \
1133 list_entry(pos->member.next, typeof(*pos), member)
1134
1135static void perf_event_sync_stat(struct perf_event_context *ctx,
1136 struct perf_event_context *next_ctx)
1137{
1138 struct perf_event *event, *next_event;
1139
1140 if (!ctx->nr_stat)
1141 return;
1142
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001143 update_context_time(ctx);
1144
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001145 event = list_first_entry(&ctx->event_list,
1146 struct perf_event, event_entry);
1147
1148 next_event = list_first_entry(&next_ctx->event_list,
1149 struct perf_event, event_entry);
1150
1151 while (&event->event_entry != &ctx->event_list &&
1152 &next_event->event_entry != &next_ctx->event_list) {
1153
1154 __perf_event_sync_stat(event, next_event);
1155
1156 event = list_next_entry(event, event_entry);
1157 next_event = list_next_entry(next_event, event_entry);
1158 }
1159}
1160
1161/*
1162 * Called from scheduler to remove the events of the current task,
1163 * with interrupts disabled.
1164 *
1165 * We stop each event and update the event value in event->count.
1166 *
1167 * This does not protect us against NMI, but disable()
1168 * sets the disabled bit in the control field of event _before_
1169 * accessing the event control register. If a NMI hits, then it will
1170 * not restart the event.
1171 */
1172void perf_event_task_sched_out(struct task_struct *task,
Peter Zijlstra49f47432009-12-27 11:51:52 +01001173 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001174{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001175 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001176 struct perf_event_context *ctx = task->perf_event_ctxp;
1177 struct perf_event_context *next_ctx;
1178 struct perf_event_context *parent;
1179 struct pt_regs *regs;
1180 int do_switch = 1;
1181
1182 regs = task_pt_regs(task);
1183 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
1184
1185 if (likely(!ctx || !cpuctx->task_ctx))
1186 return;
1187
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001188 rcu_read_lock();
1189 parent = rcu_dereference(ctx->parent_ctx);
1190 next_ctx = next->perf_event_ctxp;
1191 if (parent && next_ctx &&
1192 rcu_dereference(next_ctx->parent_ctx) == parent) {
1193 /*
1194 * Looks like the two contexts are clones, so we might be
1195 * able to optimize the context switch. We lock both
1196 * contexts and check that they are clones under the
1197 * lock (including re-checking that neither has been
1198 * uncloned in the meantime). It doesn't matter which
1199 * order we take the locks because no other cpu could
1200 * be trying to lock both of these tasks.
1201 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001202 raw_spin_lock(&ctx->lock);
1203 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001204 if (context_equiv(ctx, next_ctx)) {
1205 /*
1206 * XXX do we need a memory barrier of sorts
1207 * wrt to rcu_dereference() of perf_event_ctxp
1208 */
1209 task->perf_event_ctxp = next_ctx;
1210 next->perf_event_ctxp = ctx;
1211 ctx->task = next;
1212 next_ctx->task = task;
1213 do_switch = 0;
1214
1215 perf_event_sync_stat(ctx, next_ctx);
1216 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001217 raw_spin_unlock(&next_ctx->lock);
1218 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001219 }
1220 rcu_read_unlock();
1221
1222 if (do_switch) {
1223 __perf_event_sched_out(ctx, cpuctx);
1224 cpuctx->task_ctx = NULL;
1225 }
1226}
1227
1228/*
1229 * Called with IRQs disabled
1230 */
1231static void __perf_event_task_sched_out(struct perf_event_context *ctx)
1232{
1233 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1234
1235 if (!cpuctx->task_ctx)
1236 return;
1237
1238 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1239 return;
1240
1241 __perf_event_sched_out(ctx, cpuctx);
1242 cpuctx->task_ctx = NULL;
1243}
1244
1245/*
1246 * Called with IRQs disabled
1247 */
1248static void perf_event_cpu_sched_out(struct perf_cpu_context *cpuctx)
1249{
1250 __perf_event_sched_out(&cpuctx->ctx, cpuctx);
1251}
1252
1253static void
1254__perf_event_sched_in(struct perf_event_context *ctx,
Peter Zijlstra49f47432009-12-27 11:51:52 +01001255 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001256{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001257 int cpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001258 struct perf_event *event;
1259 int can_add_hw = 1;
1260
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001261 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001262 ctx->is_active = 1;
1263 if (likely(!ctx->nr_events))
1264 goto out;
1265
1266 ctx->timestamp = perf_clock();
1267
1268 perf_disable();
1269
1270 /*
1271 * First go through the list and put on any pinned groups
1272 * in order to give them the best chance of going on.
1273 */
1274 list_for_each_entry(event, &ctx->group_list, group_entry) {
1275 if (event->state <= PERF_EVENT_STATE_OFF ||
1276 !event->attr.pinned)
1277 continue;
1278 if (event->cpu != -1 && event->cpu != cpu)
1279 continue;
1280
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001281 if (group_can_go_on(event, cpuctx, 1))
1282 group_sched_in(event, cpuctx, ctx, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001283
1284 /*
1285 * If this pinned group hasn't been scheduled,
1286 * put it in error state.
1287 */
1288 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1289 update_group_times(event);
1290 event->state = PERF_EVENT_STATE_ERROR;
1291 }
1292 }
1293
1294 list_for_each_entry(event, &ctx->group_list, group_entry) {
1295 /*
1296 * Ignore events in OFF or ERROR state, and
1297 * ignore pinned events since we did them already.
1298 */
1299 if (event->state <= PERF_EVENT_STATE_OFF ||
1300 event->attr.pinned)
1301 continue;
1302
1303 /*
1304 * Listen to the 'cpu' scheduling filter constraint
1305 * of events:
1306 */
1307 if (event->cpu != -1 && event->cpu != cpu)
1308 continue;
1309
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001310 if (group_can_go_on(event, cpuctx, can_add_hw))
1311 if (group_sched_in(event, cpuctx, ctx, cpu))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001312 can_add_hw = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001313 }
1314 perf_enable();
1315 out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001316 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001317}
1318
1319/*
1320 * Called from scheduler to add the events of the current task
1321 * with interrupts disabled.
1322 *
1323 * We restore the event value and then enable it.
1324 *
1325 * This does not protect us against NMI, but enable()
1326 * sets the enabled bit in the control field of event _before_
1327 * accessing the event control register. If a NMI hits, then it will
1328 * keep the event running.
1329 */
Peter Zijlstra49f47432009-12-27 11:51:52 +01001330void perf_event_task_sched_in(struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001331{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001332 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001333 struct perf_event_context *ctx = task->perf_event_ctxp;
1334
1335 if (likely(!ctx))
1336 return;
1337 if (cpuctx->task_ctx == ctx)
1338 return;
Peter Zijlstra49f47432009-12-27 11:51:52 +01001339 __perf_event_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001340 cpuctx->task_ctx = ctx;
1341}
1342
Peter Zijlstra49f47432009-12-27 11:51:52 +01001343static void perf_event_cpu_sched_in(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001344{
1345 struct perf_event_context *ctx = &cpuctx->ctx;
1346
Peter Zijlstra49f47432009-12-27 11:51:52 +01001347 __perf_event_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001348}
1349
1350#define MAX_INTERRUPTS (~0ULL)
1351
1352static void perf_log_throttle(struct perf_event *event, int enable);
1353
1354static void perf_adjust_period(struct perf_event *event, u64 events)
1355{
1356 struct hw_perf_event *hwc = &event->hw;
1357 u64 period, sample_period;
1358 s64 delta;
1359
1360 events *= hwc->sample_period;
1361 period = div64_u64(events, event->attr.sample_freq);
1362
1363 delta = (s64)(period - hwc->sample_period);
1364 delta = (delta + 7) / 8; /* low pass filter */
1365
1366 sample_period = hwc->sample_period + delta;
1367
1368 if (!sample_period)
1369 sample_period = 1;
1370
1371 hwc->sample_period = sample_period;
1372}
1373
1374static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1375{
1376 struct perf_event *event;
1377 struct hw_perf_event *hwc;
1378 u64 interrupts, freq;
1379
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001380 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001381 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001382 if (event->state != PERF_EVENT_STATE_ACTIVE)
1383 continue;
1384
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001385 if (event->cpu != -1 && event->cpu != smp_processor_id())
1386 continue;
1387
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001388 hwc = &event->hw;
1389
1390 interrupts = hwc->interrupts;
1391 hwc->interrupts = 0;
1392
1393 /*
1394 * unthrottle events on the tick
1395 */
1396 if (interrupts == MAX_INTERRUPTS) {
1397 perf_log_throttle(event, 1);
1398 event->pmu->unthrottle(event);
1399 interrupts = 2*sysctl_perf_event_sample_rate/HZ;
1400 }
1401
1402 if (!event->attr.freq || !event->attr.sample_freq)
1403 continue;
1404
1405 /*
1406 * if the specified freq < HZ then we need to skip ticks
1407 */
1408 if (event->attr.sample_freq < HZ) {
1409 freq = event->attr.sample_freq;
1410
1411 hwc->freq_count += freq;
1412 hwc->freq_interrupts += interrupts;
1413
1414 if (hwc->freq_count < HZ)
1415 continue;
1416
1417 interrupts = hwc->freq_interrupts;
1418 hwc->freq_interrupts = 0;
1419 hwc->freq_count -= HZ;
1420 } else
1421 freq = HZ;
1422
1423 perf_adjust_period(event, freq * interrupts);
1424
1425 /*
1426 * In order to avoid being stalled by an (accidental) huge
1427 * sample period, force reset the sample period if we didn't
1428 * get any events in this freq period.
1429 */
1430 if (!interrupts) {
1431 perf_disable();
1432 event->pmu->disable(event);
1433 atomic64_set(&hwc->period_left, 0);
1434 event->pmu->enable(event);
1435 perf_enable();
1436 }
1437 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001438 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001439}
1440
1441/*
1442 * Round-robin a context's events:
1443 */
1444static void rotate_ctx(struct perf_event_context *ctx)
1445{
1446 struct perf_event *event;
1447
1448 if (!ctx->nr_events)
1449 return;
1450
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001451 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001452 /*
1453 * Rotate the first entry last (works just fine for group events too):
1454 */
1455 perf_disable();
1456 list_for_each_entry(event, &ctx->group_list, group_entry) {
1457 list_move_tail(&event->group_entry, &ctx->group_list);
1458 break;
1459 }
1460 perf_enable();
1461
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001462 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001463}
1464
Peter Zijlstra49f47432009-12-27 11:51:52 +01001465void perf_event_task_tick(struct task_struct *curr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001466{
1467 struct perf_cpu_context *cpuctx;
1468 struct perf_event_context *ctx;
1469
1470 if (!atomic_read(&nr_events))
1471 return;
1472
Peter Zijlstra49f47432009-12-27 11:51:52 +01001473 cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001474 ctx = curr->perf_event_ctxp;
1475
1476 perf_ctx_adjust_freq(&cpuctx->ctx);
1477 if (ctx)
1478 perf_ctx_adjust_freq(ctx);
1479
1480 perf_event_cpu_sched_out(cpuctx);
1481 if (ctx)
1482 __perf_event_task_sched_out(ctx);
1483
1484 rotate_ctx(&cpuctx->ctx);
1485 if (ctx)
1486 rotate_ctx(ctx);
1487
Peter Zijlstra49f47432009-12-27 11:51:52 +01001488 perf_event_cpu_sched_in(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001489 if (ctx)
Peter Zijlstra49f47432009-12-27 11:51:52 +01001490 perf_event_task_sched_in(curr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001491}
1492
1493/*
1494 * Enable all of a task's events that have been marked enable-on-exec.
1495 * This expects task == current.
1496 */
1497static void perf_event_enable_on_exec(struct task_struct *task)
1498{
1499 struct perf_event_context *ctx;
1500 struct perf_event *event;
1501 unsigned long flags;
1502 int enabled = 0;
1503
1504 local_irq_save(flags);
1505 ctx = task->perf_event_ctxp;
1506 if (!ctx || !ctx->nr_events)
1507 goto out;
1508
1509 __perf_event_task_sched_out(ctx);
1510
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001511 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001512
1513 list_for_each_entry(event, &ctx->group_list, group_entry) {
1514 if (!event->attr.enable_on_exec)
1515 continue;
1516 event->attr.enable_on_exec = 0;
1517 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1518 continue;
1519 __perf_event_mark_enabled(event, ctx);
1520 enabled = 1;
1521 }
1522
1523 /*
1524 * Unclone this context if we enabled any event.
1525 */
1526 if (enabled)
1527 unclone_ctx(ctx);
1528
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001529 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001530
Peter Zijlstra49f47432009-12-27 11:51:52 +01001531 perf_event_task_sched_in(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001532 out:
1533 local_irq_restore(flags);
1534}
1535
1536/*
1537 * Cross CPU call to read the hardware event
1538 */
1539static void __perf_event_read(void *info)
1540{
1541 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1542 struct perf_event *event = info;
1543 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001544
1545 /*
1546 * If this is a task context, we need to check whether it is
1547 * the current task context of this cpu. If not it has been
1548 * scheduled out before the smp call arrived. In that case
1549 * event->count would have been updated to a recent sample
1550 * when the event was scheduled out.
1551 */
1552 if (ctx->task && cpuctx->task_ctx != ctx)
1553 return;
1554
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001555 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001556 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001557 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001558 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001559
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001560 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001561}
1562
1563static u64 perf_event_read(struct perf_event *event)
1564{
1565 /*
1566 * If event is enabled and currently active on a CPU, update the
1567 * value in the event structure:
1568 */
1569 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1570 smp_call_function_single(event->oncpu,
1571 __perf_event_read, event, 1);
1572 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001573 struct perf_event_context *ctx = event->ctx;
1574 unsigned long flags;
1575
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001576 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001577 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001578 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001579 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001580 }
1581
1582 return atomic64_read(&event->count);
1583}
1584
1585/*
1586 * Initialize the perf_event context in a task_struct:
1587 */
1588static void
1589__perf_event_init_context(struct perf_event_context *ctx,
1590 struct task_struct *task)
1591{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001592 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001593 mutex_init(&ctx->mutex);
1594 INIT_LIST_HEAD(&ctx->group_list);
1595 INIT_LIST_HEAD(&ctx->event_list);
1596 atomic_set(&ctx->refcount, 1);
1597 ctx->task = task;
1598}
1599
1600static struct perf_event_context *find_get_context(pid_t pid, int cpu)
1601{
1602 struct perf_event_context *ctx;
1603 struct perf_cpu_context *cpuctx;
1604 struct task_struct *task;
1605 unsigned long flags;
1606 int err;
1607
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001608 if (pid == -1 && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001609 /* Must be root to operate on a CPU event: */
1610 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1611 return ERR_PTR(-EACCES);
1612
Paul Mackerras0f624e72009-12-15 19:40:32 +11001613 if (cpu < 0 || cpu >= nr_cpumask_bits)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001614 return ERR_PTR(-EINVAL);
1615
1616 /*
1617 * We could be clever and allow to attach a event to an
1618 * offline CPU and activate it when the CPU comes up, but
1619 * that's for later.
1620 */
1621 if (!cpu_isset(cpu, cpu_online_map))
1622 return ERR_PTR(-ENODEV);
1623
1624 cpuctx = &per_cpu(perf_cpu_context, cpu);
1625 ctx = &cpuctx->ctx;
1626 get_ctx(ctx);
1627
1628 return ctx;
1629 }
1630
1631 rcu_read_lock();
1632 if (!pid)
1633 task = current;
1634 else
1635 task = find_task_by_vpid(pid);
1636 if (task)
1637 get_task_struct(task);
1638 rcu_read_unlock();
1639
1640 if (!task)
1641 return ERR_PTR(-ESRCH);
1642
1643 /*
1644 * Can't attach events to a dying task.
1645 */
1646 err = -ESRCH;
1647 if (task->flags & PF_EXITING)
1648 goto errout;
1649
1650 /* Reuse ptrace permission checks for now. */
1651 err = -EACCES;
1652 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1653 goto errout;
1654
1655 retry:
1656 ctx = perf_lock_task_context(task, &flags);
1657 if (ctx) {
1658 unclone_ctx(ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001659 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001660 }
1661
1662 if (!ctx) {
Xiao Guangrongaa5452d2009-12-09 11:28:13 +08001663 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001664 err = -ENOMEM;
1665 if (!ctx)
1666 goto errout;
1667 __perf_event_init_context(ctx, task);
1668 get_ctx(ctx);
1669 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
1670 /*
1671 * We raced with some other task; use
1672 * the context they set.
1673 */
1674 kfree(ctx);
1675 goto retry;
1676 }
1677 get_task_struct(task);
1678 }
1679
1680 put_task_struct(task);
1681 return ctx;
1682
1683 errout:
1684 put_task_struct(task);
1685 return ERR_PTR(err);
1686}
1687
Li Zefan6fb29152009-10-15 11:21:42 +08001688static void perf_event_free_filter(struct perf_event *event);
1689
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001690static void free_event_rcu(struct rcu_head *head)
1691{
1692 struct perf_event *event;
1693
1694 event = container_of(head, struct perf_event, rcu_head);
1695 if (event->ns)
1696 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08001697 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001698 kfree(event);
1699}
1700
1701static void perf_pending_sync(struct perf_event *event);
1702
1703static void free_event(struct perf_event *event)
1704{
1705 perf_pending_sync(event);
1706
1707 if (!event->parent) {
1708 atomic_dec(&nr_events);
1709 if (event->attr.mmap)
1710 atomic_dec(&nr_mmap_events);
1711 if (event->attr.comm)
1712 atomic_dec(&nr_comm_events);
1713 if (event->attr.task)
1714 atomic_dec(&nr_task_events);
1715 }
1716
1717 if (event->output) {
1718 fput(event->output->filp);
1719 event->output = NULL;
1720 }
1721
1722 if (event->destroy)
1723 event->destroy(event);
1724
1725 put_ctx(event->ctx);
1726 call_rcu(&event->rcu_head, free_event_rcu);
1727}
1728
Arjan van de Venfb0459d2009-09-25 12:25:56 +02001729int perf_event_release_kernel(struct perf_event *event)
1730{
1731 struct perf_event_context *ctx = event->ctx;
1732
1733 WARN_ON_ONCE(ctx->parent_ctx);
1734 mutex_lock(&ctx->mutex);
1735 perf_event_remove_from_context(event);
1736 mutex_unlock(&ctx->mutex);
1737
1738 mutex_lock(&event->owner->perf_event_mutex);
1739 list_del_init(&event->owner_entry);
1740 mutex_unlock(&event->owner->perf_event_mutex);
1741 put_task_struct(event->owner);
1742
1743 free_event(event);
1744
1745 return 0;
1746}
1747EXPORT_SYMBOL_GPL(perf_event_release_kernel);
1748
Peter Zijlstraa66a3052009-11-23 11:37:23 +01001749/*
1750 * Called when the last reference to the file is gone.
1751 */
1752static int perf_release(struct inode *inode, struct file *file)
1753{
1754 struct perf_event *event = file->private_data;
1755
1756 file->private_data = NULL;
1757
1758 return perf_event_release_kernel(event);
1759}
1760
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001761static int perf_event_read_size(struct perf_event *event)
1762{
1763 int entry = sizeof(u64); /* value */
1764 int size = 0;
1765 int nr = 1;
1766
1767 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1768 size += sizeof(u64);
1769
1770 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1771 size += sizeof(u64);
1772
1773 if (event->attr.read_format & PERF_FORMAT_ID)
1774 entry += sizeof(u64);
1775
1776 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1777 nr += event->group_leader->nr_siblings;
1778 size += sizeof(u64);
1779 }
1780
1781 size += entry * nr;
1782
1783 return size;
1784}
1785
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01001786u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001787{
1788 struct perf_event *child;
1789 u64 total = 0;
1790
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01001791 *enabled = 0;
1792 *running = 0;
1793
Peter Zijlstra6f105812009-11-20 22:19:56 +01001794 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001795 total += perf_event_read(event);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01001796 *enabled += event->total_time_enabled +
1797 atomic64_read(&event->child_total_time_enabled);
1798 *running += event->total_time_running +
1799 atomic64_read(&event->child_total_time_running);
1800
1801 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001802 total += perf_event_read(child);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01001803 *enabled += child->total_time_enabled;
1804 *running += child->total_time_running;
1805 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01001806 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001807
1808 return total;
1809}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02001810EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001811
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001812static int perf_event_read_group(struct perf_event *event,
1813 u64 read_format, char __user *buf)
1814{
1815 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01001816 int n = 0, size = 0, ret = -EFAULT;
1817 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01001818 u64 values[5];
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01001819 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01001820
Peter Zijlstra6f105812009-11-20 22:19:56 +01001821 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01001822 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001823
1824 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01001825 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1826 values[n++] = enabled;
1827 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1828 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01001829 values[n++] = count;
1830 if (read_format & PERF_FORMAT_ID)
1831 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001832
1833 size = n * sizeof(u64);
1834
1835 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01001836 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001837
Peter Zijlstra6f105812009-11-20 22:19:56 +01001838 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001839
1840 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01001841 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001842
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01001843 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01001844 if (read_format & PERF_FORMAT_ID)
1845 values[n++] = primary_event_id(sub);
1846
1847 size = n * sizeof(u64);
1848
Stephane Eranian184d3da2009-11-23 21:40:49 -08001849 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01001850 ret = -EFAULT;
1851 goto unlock;
1852 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01001853
1854 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001855 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01001856unlock:
1857 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001858
Peter Zijlstraabf48682009-11-20 22:19:49 +01001859 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001860}
1861
1862static int perf_event_read_one(struct perf_event *event,
1863 u64 read_format, char __user *buf)
1864{
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01001865 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001866 u64 values[4];
1867 int n = 0;
1868
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01001869 values[n++] = perf_event_read_value(event, &enabled, &running);
1870 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1871 values[n++] = enabled;
1872 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1873 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001874 if (read_format & PERF_FORMAT_ID)
1875 values[n++] = primary_event_id(event);
1876
1877 if (copy_to_user(buf, values, n * sizeof(u64)))
1878 return -EFAULT;
1879
1880 return n * sizeof(u64);
1881}
1882
1883/*
1884 * Read the performance event - simple non blocking version for now
1885 */
1886static ssize_t
1887perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
1888{
1889 u64 read_format = event->attr.read_format;
1890 int ret;
1891
1892 /*
1893 * Return end-of-file for a read on a event that is in
1894 * error state (i.e. because it was pinned but it couldn't be
1895 * scheduled on to the CPU at some point).
1896 */
1897 if (event->state == PERF_EVENT_STATE_ERROR)
1898 return 0;
1899
1900 if (count < perf_event_read_size(event))
1901 return -ENOSPC;
1902
1903 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001904 if (read_format & PERF_FORMAT_GROUP)
1905 ret = perf_event_read_group(event, read_format, buf);
1906 else
1907 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001908
1909 return ret;
1910}
1911
1912static ssize_t
1913perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1914{
1915 struct perf_event *event = file->private_data;
1916
1917 return perf_read_hw(event, buf, count);
1918}
1919
1920static unsigned int perf_poll(struct file *file, poll_table *wait)
1921{
1922 struct perf_event *event = file->private_data;
1923 struct perf_mmap_data *data;
1924 unsigned int events = POLL_HUP;
1925
1926 rcu_read_lock();
1927 data = rcu_dereference(event->data);
1928 if (data)
1929 events = atomic_xchg(&data->poll, 0);
1930 rcu_read_unlock();
1931
1932 poll_wait(file, &event->waitq, wait);
1933
1934 return events;
1935}
1936
1937static void perf_event_reset(struct perf_event *event)
1938{
1939 (void)perf_event_read(event);
1940 atomic64_set(&event->count, 0);
1941 perf_event_update_userpage(event);
1942}
1943
1944/*
1945 * Holding the top-level event's child_mutex means that any
1946 * descendant process that has inherited this event will block
1947 * in sync_child_event if it goes to exit, thus satisfying the
1948 * task existence requirements of perf_event_enable/disable.
1949 */
1950static void perf_event_for_each_child(struct perf_event *event,
1951 void (*func)(struct perf_event *))
1952{
1953 struct perf_event *child;
1954
1955 WARN_ON_ONCE(event->ctx->parent_ctx);
1956 mutex_lock(&event->child_mutex);
1957 func(event);
1958 list_for_each_entry(child, &event->child_list, child_list)
1959 func(child);
1960 mutex_unlock(&event->child_mutex);
1961}
1962
1963static void perf_event_for_each(struct perf_event *event,
1964 void (*func)(struct perf_event *))
1965{
1966 struct perf_event_context *ctx = event->ctx;
1967 struct perf_event *sibling;
1968
1969 WARN_ON_ONCE(ctx->parent_ctx);
1970 mutex_lock(&ctx->mutex);
1971 event = event->group_leader;
1972
1973 perf_event_for_each_child(event, func);
1974 func(event);
1975 list_for_each_entry(sibling, &event->sibling_list, group_entry)
1976 perf_event_for_each_child(event, func);
1977 mutex_unlock(&ctx->mutex);
1978}
1979
1980static int perf_event_period(struct perf_event *event, u64 __user *arg)
1981{
1982 struct perf_event_context *ctx = event->ctx;
1983 unsigned long size;
1984 int ret = 0;
1985 u64 value;
1986
1987 if (!event->attr.sample_period)
1988 return -EINVAL;
1989
1990 size = copy_from_user(&value, arg, sizeof(value));
1991 if (size != sizeof(value))
1992 return -EFAULT;
1993
1994 if (!value)
1995 return -EINVAL;
1996
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001997 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001998 if (event->attr.freq) {
1999 if (value > sysctl_perf_event_sample_rate) {
2000 ret = -EINVAL;
2001 goto unlock;
2002 }
2003
2004 event->attr.sample_freq = value;
2005 } else {
2006 event->attr.sample_period = value;
2007 event->hw.sample_period = value;
2008 }
2009unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002010 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002011
2012 return ret;
2013}
2014
Li Zefan6fb29152009-10-15 11:21:42 +08002015static int perf_event_set_output(struct perf_event *event, int output_fd);
2016static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002017
2018static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2019{
2020 struct perf_event *event = file->private_data;
2021 void (*func)(struct perf_event *);
2022 u32 flags = arg;
2023
2024 switch (cmd) {
2025 case PERF_EVENT_IOC_ENABLE:
2026 func = perf_event_enable;
2027 break;
2028 case PERF_EVENT_IOC_DISABLE:
2029 func = perf_event_disable;
2030 break;
2031 case PERF_EVENT_IOC_RESET:
2032 func = perf_event_reset;
2033 break;
2034
2035 case PERF_EVENT_IOC_REFRESH:
2036 return perf_event_refresh(event, arg);
2037
2038 case PERF_EVENT_IOC_PERIOD:
2039 return perf_event_period(event, (u64 __user *)arg);
2040
2041 case PERF_EVENT_IOC_SET_OUTPUT:
2042 return perf_event_set_output(event, arg);
2043
Li Zefan6fb29152009-10-15 11:21:42 +08002044 case PERF_EVENT_IOC_SET_FILTER:
2045 return perf_event_set_filter(event, (void __user *)arg);
2046
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002047 default:
2048 return -ENOTTY;
2049 }
2050
2051 if (flags & PERF_IOC_FLAG_GROUP)
2052 perf_event_for_each(event, func);
2053 else
2054 perf_event_for_each_child(event, func);
2055
2056 return 0;
2057}
2058
2059int perf_event_task_enable(void)
2060{
2061 struct perf_event *event;
2062
2063 mutex_lock(&current->perf_event_mutex);
2064 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2065 perf_event_for_each_child(event, perf_event_enable);
2066 mutex_unlock(&current->perf_event_mutex);
2067
2068 return 0;
2069}
2070
2071int perf_event_task_disable(void)
2072{
2073 struct perf_event *event;
2074
2075 mutex_lock(&current->perf_event_mutex);
2076 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2077 perf_event_for_each_child(event, perf_event_disable);
2078 mutex_unlock(&current->perf_event_mutex);
2079
2080 return 0;
2081}
2082
2083#ifndef PERF_EVENT_INDEX_OFFSET
2084# define PERF_EVENT_INDEX_OFFSET 0
2085#endif
2086
2087static int perf_event_index(struct perf_event *event)
2088{
2089 if (event->state != PERF_EVENT_STATE_ACTIVE)
2090 return 0;
2091
2092 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2093}
2094
2095/*
2096 * Callers need to ensure there can be no nesting of this function, otherwise
2097 * the seqlock logic goes bad. We can not serialize this because the arch
2098 * code calls this from NMI context.
2099 */
2100void perf_event_update_userpage(struct perf_event *event)
2101{
2102 struct perf_event_mmap_page *userpg;
2103 struct perf_mmap_data *data;
2104
2105 rcu_read_lock();
2106 data = rcu_dereference(event->data);
2107 if (!data)
2108 goto unlock;
2109
2110 userpg = data->user_page;
2111
2112 /*
2113 * Disable preemption so as to not let the corresponding user-space
2114 * spin too long if we get preempted.
2115 */
2116 preempt_disable();
2117 ++userpg->lock;
2118 barrier();
2119 userpg->index = perf_event_index(event);
2120 userpg->offset = atomic64_read(&event->count);
2121 if (event->state == PERF_EVENT_STATE_ACTIVE)
2122 userpg->offset -= atomic64_read(&event->hw.prev_count);
2123
2124 userpg->time_enabled = event->total_time_enabled +
2125 atomic64_read(&event->child_total_time_enabled);
2126
2127 userpg->time_running = event->total_time_running +
2128 atomic64_read(&event->child_total_time_running);
2129
2130 barrier();
2131 ++userpg->lock;
2132 preempt_enable();
2133unlock:
2134 rcu_read_unlock();
2135}
2136
Peter Zijlstra906010b2009-09-21 16:08:49 +02002137static unsigned long perf_data_size(struct perf_mmap_data *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002138{
Peter Zijlstra906010b2009-09-21 16:08:49 +02002139 return data->nr_pages << (PAGE_SHIFT + data->data_order);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002140}
2141
Peter Zijlstra906010b2009-09-21 16:08:49 +02002142#ifndef CONFIG_PERF_USE_VMALLOC
2143
2144/*
2145 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2146 */
2147
2148static struct page *
2149perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2150{
2151 if (pgoff > data->nr_pages)
2152 return NULL;
2153
2154 if (pgoff == 0)
2155 return virt_to_page(data->user_page);
2156
2157 return virt_to_page(data->data_pages[pgoff - 1]);
2158}
2159
2160static struct perf_mmap_data *
2161perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002162{
2163 struct perf_mmap_data *data;
2164 unsigned long size;
2165 int i;
2166
2167 WARN_ON(atomic_read(&event->mmap_count));
2168
2169 size = sizeof(struct perf_mmap_data);
2170 size += nr_pages * sizeof(void *);
2171
2172 data = kzalloc(size, GFP_KERNEL);
2173 if (!data)
2174 goto fail;
2175
2176 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2177 if (!data->user_page)
2178 goto fail_user_page;
2179
2180 for (i = 0; i < nr_pages; i++) {
2181 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2182 if (!data->data_pages[i])
2183 goto fail_data_pages;
2184 }
2185
Peter Zijlstra906010b2009-09-21 16:08:49 +02002186 data->data_order = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002187 data->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002188
Peter Zijlstra906010b2009-09-21 16:08:49 +02002189 return data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002190
2191fail_data_pages:
2192 for (i--; i >= 0; i--)
2193 free_page((unsigned long)data->data_pages[i]);
2194
2195 free_page((unsigned long)data->user_page);
2196
2197fail_user_page:
2198 kfree(data);
2199
2200fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002201 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002202}
2203
2204static void perf_mmap_free_page(unsigned long addr)
2205{
2206 struct page *page = virt_to_page((void *)addr);
2207
2208 page->mapping = NULL;
2209 __free_page(page);
2210}
2211
Peter Zijlstra906010b2009-09-21 16:08:49 +02002212static void perf_mmap_data_free(struct perf_mmap_data *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002213{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002214 int i;
2215
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002216 perf_mmap_free_page((unsigned long)data->user_page);
2217 for (i = 0; i < data->nr_pages; i++)
2218 perf_mmap_free_page((unsigned long)data->data_pages[i]);
Kristian Høgsbergec70ccd2009-12-01 15:05:01 -05002219 kfree(data);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002220}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002221
Peter Zijlstra906010b2009-09-21 16:08:49 +02002222#else
2223
2224/*
2225 * Back perf_mmap() with vmalloc memory.
2226 *
2227 * Required for architectures that have d-cache aliasing issues.
2228 */
2229
2230static struct page *
2231perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2232{
2233 if (pgoff > (1UL << data->data_order))
2234 return NULL;
2235
2236 return vmalloc_to_page((void *)data->user_page + pgoff * PAGE_SIZE);
2237}
2238
2239static void perf_mmap_unmark_page(void *addr)
2240{
2241 struct page *page = vmalloc_to_page(addr);
2242
2243 page->mapping = NULL;
2244}
2245
2246static void perf_mmap_data_free_work(struct work_struct *work)
2247{
2248 struct perf_mmap_data *data;
2249 void *base;
2250 int i, nr;
2251
2252 data = container_of(work, struct perf_mmap_data, work);
2253 nr = 1 << data->data_order;
2254
2255 base = data->user_page;
2256 for (i = 0; i < nr + 1; i++)
2257 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2258
2259 vfree(base);
Kristian Høgsbergec70ccd2009-12-01 15:05:01 -05002260 kfree(data);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002261}
2262
2263static void perf_mmap_data_free(struct perf_mmap_data *data)
2264{
2265 schedule_work(&data->work);
2266}
2267
2268static struct perf_mmap_data *
2269perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2270{
2271 struct perf_mmap_data *data;
2272 unsigned long size;
2273 void *all_buf;
2274
2275 WARN_ON(atomic_read(&event->mmap_count));
2276
2277 size = sizeof(struct perf_mmap_data);
2278 size += sizeof(void *);
2279
2280 data = kzalloc(size, GFP_KERNEL);
2281 if (!data)
2282 goto fail;
2283
2284 INIT_WORK(&data->work, perf_mmap_data_free_work);
2285
2286 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2287 if (!all_buf)
2288 goto fail_all_buf;
2289
2290 data->user_page = all_buf;
2291 data->data_pages[0] = all_buf + PAGE_SIZE;
2292 data->data_order = ilog2(nr_pages);
2293 data->nr_pages = 1;
2294
2295 return data;
2296
2297fail_all_buf:
2298 kfree(data);
2299
2300fail:
2301 return NULL;
2302}
2303
2304#endif
2305
2306static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2307{
2308 struct perf_event *event = vma->vm_file->private_data;
2309 struct perf_mmap_data *data;
2310 int ret = VM_FAULT_SIGBUS;
2311
2312 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2313 if (vmf->pgoff == 0)
2314 ret = 0;
2315 return ret;
2316 }
2317
2318 rcu_read_lock();
2319 data = rcu_dereference(event->data);
2320 if (!data)
2321 goto unlock;
2322
2323 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2324 goto unlock;
2325
2326 vmf->page = perf_mmap_to_page(data, vmf->pgoff);
2327 if (!vmf->page)
2328 goto unlock;
2329
2330 get_page(vmf->page);
2331 vmf->page->mapping = vma->vm_file->f_mapping;
2332 vmf->page->index = vmf->pgoff;
2333
2334 ret = 0;
2335unlock:
2336 rcu_read_unlock();
2337
2338 return ret;
2339}
2340
2341static void
2342perf_mmap_data_init(struct perf_event *event, struct perf_mmap_data *data)
2343{
2344 long max_size = perf_data_size(data);
2345
2346 atomic_set(&data->lock, -1);
2347
2348 if (event->attr.watermark) {
2349 data->watermark = min_t(long, max_size,
2350 event->attr.wakeup_watermark);
2351 }
2352
2353 if (!data->watermark)
Stephane Eranian8904b182009-11-20 22:19:57 +01002354 data->watermark = max_size / 2;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002355
2356
2357 rcu_assign_pointer(event->data, data);
2358}
2359
2360static void perf_mmap_data_free_rcu(struct rcu_head *rcu_head)
2361{
2362 struct perf_mmap_data *data;
2363
2364 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2365 perf_mmap_data_free(data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002366}
2367
Peter Zijlstra906010b2009-09-21 16:08:49 +02002368static void perf_mmap_data_release(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002369{
2370 struct perf_mmap_data *data = event->data;
2371
2372 WARN_ON(atomic_read(&event->mmap_count));
2373
2374 rcu_assign_pointer(event->data, NULL);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002375 call_rcu(&data->rcu_head, perf_mmap_data_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002376}
2377
2378static void perf_mmap_open(struct vm_area_struct *vma)
2379{
2380 struct perf_event *event = vma->vm_file->private_data;
2381
2382 atomic_inc(&event->mmap_count);
2383}
2384
2385static void perf_mmap_close(struct vm_area_struct *vma)
2386{
2387 struct perf_event *event = vma->vm_file->private_data;
2388
2389 WARN_ON_ONCE(event->ctx->parent_ctx);
2390 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstra906010b2009-09-21 16:08:49 +02002391 unsigned long size = perf_data_size(event->data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002392 struct user_struct *user = current_user();
2393
Peter Zijlstra906010b2009-09-21 16:08:49 +02002394 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002395 vma->vm_mm->locked_vm -= event->data->nr_locked;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002396 perf_mmap_data_release(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002397 mutex_unlock(&event->mmap_mutex);
2398 }
2399}
2400
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002401static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002402 .open = perf_mmap_open,
2403 .close = perf_mmap_close,
2404 .fault = perf_mmap_fault,
2405 .page_mkwrite = perf_mmap_fault,
2406};
2407
2408static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2409{
2410 struct perf_event *event = file->private_data;
2411 unsigned long user_locked, user_lock_limit;
2412 struct user_struct *user = current_user();
2413 unsigned long locked, lock_limit;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002414 struct perf_mmap_data *data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002415 unsigned long vma_size;
2416 unsigned long nr_pages;
2417 long user_extra, extra;
2418 int ret = 0;
2419
2420 if (!(vma->vm_flags & VM_SHARED))
2421 return -EINVAL;
2422
2423 vma_size = vma->vm_end - vma->vm_start;
2424 nr_pages = (vma_size / PAGE_SIZE) - 1;
2425
2426 /*
2427 * If we have data pages ensure they're a power-of-two number, so we
2428 * can do bitmasks instead of modulo.
2429 */
2430 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2431 return -EINVAL;
2432
2433 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2434 return -EINVAL;
2435
2436 if (vma->vm_pgoff != 0)
2437 return -EINVAL;
2438
2439 WARN_ON_ONCE(event->ctx->parent_ctx);
2440 mutex_lock(&event->mmap_mutex);
2441 if (event->output) {
2442 ret = -EINVAL;
2443 goto unlock;
2444 }
2445
2446 if (atomic_inc_not_zero(&event->mmap_count)) {
2447 if (nr_pages != event->data->nr_pages)
2448 ret = -EINVAL;
2449 goto unlock;
2450 }
2451
2452 user_extra = nr_pages + 1;
2453 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2454
2455 /*
2456 * Increase the limit linearly with more CPUs:
2457 */
2458 user_lock_limit *= num_online_cpus();
2459
2460 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2461
2462 extra = 0;
2463 if (user_locked > user_lock_limit)
2464 extra = user_locked - user_lock_limit;
2465
2466 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2467 lock_limit >>= PAGE_SHIFT;
2468 locked = vma->vm_mm->locked_vm + extra;
2469
2470 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2471 !capable(CAP_IPC_LOCK)) {
2472 ret = -EPERM;
2473 goto unlock;
2474 }
2475
2476 WARN_ON(event->data);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002477
2478 data = perf_mmap_data_alloc(event, nr_pages);
2479 ret = -ENOMEM;
2480 if (!data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002481 goto unlock;
2482
Peter Zijlstra906010b2009-09-21 16:08:49 +02002483 ret = 0;
2484 perf_mmap_data_init(event, data);
2485
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002486 atomic_set(&event->mmap_count, 1);
2487 atomic_long_add(user_extra, &user->locked_vm);
2488 vma->vm_mm->locked_vm += extra;
2489 event->data->nr_locked = extra;
2490 if (vma->vm_flags & VM_WRITE)
2491 event->data->writable = 1;
2492
2493unlock:
2494 mutex_unlock(&event->mmap_mutex);
2495
2496 vma->vm_flags |= VM_RESERVED;
2497 vma->vm_ops = &perf_mmap_vmops;
2498
2499 return ret;
2500}
2501
2502static int perf_fasync(int fd, struct file *filp, int on)
2503{
2504 struct inode *inode = filp->f_path.dentry->d_inode;
2505 struct perf_event *event = filp->private_data;
2506 int retval;
2507
2508 mutex_lock(&inode->i_mutex);
2509 retval = fasync_helper(fd, filp, on, &event->fasync);
2510 mutex_unlock(&inode->i_mutex);
2511
2512 if (retval < 0)
2513 return retval;
2514
2515 return 0;
2516}
2517
2518static const struct file_operations perf_fops = {
2519 .release = perf_release,
2520 .read = perf_read,
2521 .poll = perf_poll,
2522 .unlocked_ioctl = perf_ioctl,
2523 .compat_ioctl = perf_ioctl,
2524 .mmap = perf_mmap,
2525 .fasync = perf_fasync,
2526};
2527
2528/*
2529 * Perf event wakeup
2530 *
2531 * If there's data, ensure we set the poll() state and publish everything
2532 * to user-space before waking everybody up.
2533 */
2534
2535void perf_event_wakeup(struct perf_event *event)
2536{
2537 wake_up_all(&event->waitq);
2538
2539 if (event->pending_kill) {
2540 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
2541 event->pending_kill = 0;
2542 }
2543}
2544
2545/*
2546 * Pending wakeups
2547 *
2548 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2549 *
2550 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2551 * single linked list and use cmpxchg() to add entries lockless.
2552 */
2553
2554static void perf_pending_event(struct perf_pending_entry *entry)
2555{
2556 struct perf_event *event = container_of(entry,
2557 struct perf_event, pending);
2558
2559 if (event->pending_disable) {
2560 event->pending_disable = 0;
2561 __perf_event_disable(event);
2562 }
2563
2564 if (event->pending_wakeup) {
2565 event->pending_wakeup = 0;
2566 perf_event_wakeup(event);
2567 }
2568}
2569
2570#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2571
2572static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2573 PENDING_TAIL,
2574};
2575
2576static void perf_pending_queue(struct perf_pending_entry *entry,
2577 void (*func)(struct perf_pending_entry *))
2578{
2579 struct perf_pending_entry **head;
2580
2581 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2582 return;
2583
2584 entry->func = func;
2585
2586 head = &get_cpu_var(perf_pending_head);
2587
2588 do {
2589 entry->next = *head;
2590 } while (cmpxchg(head, entry->next, entry) != entry->next);
2591
2592 set_perf_event_pending();
2593
2594 put_cpu_var(perf_pending_head);
2595}
2596
2597static int __perf_pending_run(void)
2598{
2599 struct perf_pending_entry *list;
2600 int nr = 0;
2601
2602 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2603 while (list != PENDING_TAIL) {
2604 void (*func)(struct perf_pending_entry *);
2605 struct perf_pending_entry *entry = list;
2606
2607 list = list->next;
2608
2609 func = entry->func;
2610 entry->next = NULL;
2611 /*
2612 * Ensure we observe the unqueue before we issue the wakeup,
2613 * so that we won't be waiting forever.
2614 * -- see perf_not_pending().
2615 */
2616 smp_wmb();
2617
2618 func(entry);
2619 nr++;
2620 }
2621
2622 return nr;
2623}
2624
2625static inline int perf_not_pending(struct perf_event *event)
2626{
2627 /*
2628 * If we flush on whatever cpu we run, there is a chance we don't
2629 * need to wait.
2630 */
2631 get_cpu();
2632 __perf_pending_run();
2633 put_cpu();
2634
2635 /*
2636 * Ensure we see the proper queue state before going to sleep
2637 * so that we do not miss the wakeup. -- see perf_pending_handle()
2638 */
2639 smp_rmb();
2640 return event->pending.next == NULL;
2641}
2642
2643static void perf_pending_sync(struct perf_event *event)
2644{
2645 wait_event(event->waitq, perf_not_pending(event));
2646}
2647
2648void perf_event_do_pending(void)
2649{
2650 __perf_pending_run();
2651}
2652
2653/*
2654 * Callchain support -- arch specific
2655 */
2656
2657__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2658{
2659 return NULL;
2660}
2661
2662/*
2663 * Output
2664 */
2665static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
2666 unsigned long offset, unsigned long head)
2667{
2668 unsigned long mask;
2669
2670 if (!data->writable)
2671 return true;
2672
Peter Zijlstra906010b2009-09-21 16:08:49 +02002673 mask = perf_data_size(data) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002674
2675 offset = (offset - tail) & mask;
2676 head = (head - tail) & mask;
2677
2678 if ((int)(head - offset) < 0)
2679 return false;
2680
2681 return true;
2682}
2683
2684static void perf_output_wakeup(struct perf_output_handle *handle)
2685{
2686 atomic_set(&handle->data->poll, POLL_IN);
2687
2688 if (handle->nmi) {
2689 handle->event->pending_wakeup = 1;
2690 perf_pending_queue(&handle->event->pending,
2691 perf_pending_event);
2692 } else
2693 perf_event_wakeup(handle->event);
2694}
2695
2696/*
2697 * Curious locking construct.
2698 *
2699 * We need to ensure a later event_id doesn't publish a head when a former
2700 * event_id isn't done writing. However since we need to deal with NMIs we
2701 * cannot fully serialize things.
2702 *
2703 * What we do is serialize between CPUs so we only have to deal with NMI
2704 * nesting on a single CPU.
2705 *
2706 * We only publish the head (and generate a wakeup) when the outer-most
2707 * event_id completes.
2708 */
2709static void perf_output_lock(struct perf_output_handle *handle)
2710{
2711 struct perf_mmap_data *data = handle->data;
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002712 int cur, cpu = get_cpu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002713
2714 handle->locked = 0;
2715
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002716 for (;;) {
2717 cur = atomic_cmpxchg(&data->lock, -1, cpu);
2718 if (cur == -1) {
2719 handle->locked = 1;
2720 break;
2721 }
2722 if (cur == cpu)
2723 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002724
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002725 cpu_relax();
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002726 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002727}
2728
2729static void perf_output_unlock(struct perf_output_handle *handle)
2730{
2731 struct perf_mmap_data *data = handle->data;
2732 unsigned long head;
2733 int cpu;
2734
2735 data->done_head = data->head;
2736
2737 if (!handle->locked)
2738 goto out;
2739
2740again:
2741 /*
2742 * The xchg implies a full barrier that ensures all writes are done
2743 * before we publish the new head, matched by a rmb() in userspace when
2744 * reading this position.
2745 */
2746 while ((head = atomic_long_xchg(&data->done_head, 0)))
2747 data->user_page->data_head = head;
2748
2749 /*
2750 * NMI can happen here, which means we can miss a done_head update.
2751 */
2752
2753 cpu = atomic_xchg(&data->lock, -1);
2754 WARN_ON_ONCE(cpu != smp_processor_id());
2755
2756 /*
2757 * Therefore we have to validate we did not indeed do so.
2758 */
2759 if (unlikely(atomic_long_read(&data->done_head))) {
2760 /*
2761 * Since we had it locked, we can lock it again.
2762 */
2763 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2764 cpu_relax();
2765
2766 goto again;
2767 }
2768
2769 if (atomic_xchg(&data->wakeup, 0))
2770 perf_output_wakeup(handle);
2771out:
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002772 put_cpu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002773}
2774
2775void perf_output_copy(struct perf_output_handle *handle,
2776 const void *buf, unsigned int len)
2777{
2778 unsigned int pages_mask;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002779 unsigned long offset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002780 unsigned int size;
2781 void **pages;
2782
2783 offset = handle->offset;
2784 pages_mask = handle->data->nr_pages - 1;
2785 pages = handle->data->data_pages;
2786
2787 do {
Peter Zijlstra906010b2009-09-21 16:08:49 +02002788 unsigned long page_offset;
2789 unsigned long page_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002790 int nr;
2791
2792 nr = (offset >> PAGE_SHIFT) & pages_mask;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002793 page_size = 1UL << (handle->data->data_order + PAGE_SHIFT);
2794 page_offset = offset & (page_size - 1);
2795 size = min_t(unsigned int, page_size - page_offset, len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002796
2797 memcpy(pages[nr] + page_offset, buf, size);
2798
2799 len -= size;
2800 buf += size;
2801 offset += size;
2802 } while (len);
2803
2804 handle->offset = offset;
2805
2806 /*
2807 * Check we didn't copy past our reservation window, taking the
2808 * possible unsigned int wrap into account.
2809 */
2810 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
2811}
2812
2813int perf_output_begin(struct perf_output_handle *handle,
2814 struct perf_event *event, unsigned int size,
2815 int nmi, int sample)
2816{
2817 struct perf_event *output_event;
2818 struct perf_mmap_data *data;
2819 unsigned long tail, offset, head;
2820 int have_lost;
2821 struct {
2822 struct perf_event_header header;
2823 u64 id;
2824 u64 lost;
2825 } lost_event;
2826
2827 rcu_read_lock();
2828 /*
2829 * For inherited events we send all the output towards the parent.
2830 */
2831 if (event->parent)
2832 event = event->parent;
2833
2834 output_event = rcu_dereference(event->output);
2835 if (output_event)
2836 event = output_event;
2837
2838 data = rcu_dereference(event->data);
2839 if (!data)
2840 goto out;
2841
2842 handle->data = data;
2843 handle->event = event;
2844 handle->nmi = nmi;
2845 handle->sample = sample;
2846
2847 if (!data->nr_pages)
2848 goto fail;
2849
2850 have_lost = atomic_read(&data->lost);
2851 if (have_lost)
2852 size += sizeof(lost_event);
2853
2854 perf_output_lock(handle);
2855
2856 do {
2857 /*
2858 * Userspace could choose to issue a mb() before updating the
2859 * tail pointer. So that all reads will be completed before the
2860 * write is issued.
2861 */
2862 tail = ACCESS_ONCE(data->user_page->data_tail);
2863 smp_rmb();
2864 offset = head = atomic_long_read(&data->head);
2865 head += size;
2866 if (unlikely(!perf_output_space(data, tail, offset, head)))
2867 goto fail;
2868 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2869
2870 handle->offset = offset;
2871 handle->head = head;
2872
2873 if (head - tail > data->watermark)
2874 atomic_set(&data->wakeup, 1);
2875
2876 if (have_lost) {
2877 lost_event.header.type = PERF_RECORD_LOST;
2878 lost_event.header.misc = 0;
2879 lost_event.header.size = sizeof(lost_event);
2880 lost_event.id = event->id;
2881 lost_event.lost = atomic_xchg(&data->lost, 0);
2882
2883 perf_output_put(handle, lost_event);
2884 }
2885
2886 return 0;
2887
2888fail:
2889 atomic_inc(&data->lost);
2890 perf_output_unlock(handle);
2891out:
2892 rcu_read_unlock();
2893
2894 return -ENOSPC;
2895}
2896
2897void perf_output_end(struct perf_output_handle *handle)
2898{
2899 struct perf_event *event = handle->event;
2900 struct perf_mmap_data *data = handle->data;
2901
2902 int wakeup_events = event->attr.wakeup_events;
2903
2904 if (handle->sample && wakeup_events) {
2905 int events = atomic_inc_return(&data->events);
2906 if (events >= wakeup_events) {
2907 atomic_sub(wakeup_events, &data->events);
2908 atomic_set(&data->wakeup, 1);
2909 }
2910 }
2911
2912 perf_output_unlock(handle);
2913 rcu_read_unlock();
2914}
2915
2916static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
2917{
2918 /*
2919 * only top level events have the pid namespace they were created in
2920 */
2921 if (event->parent)
2922 event = event->parent;
2923
2924 return task_tgid_nr_ns(p, event->ns);
2925}
2926
2927static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
2928{
2929 /*
2930 * only top level events have the pid namespace they were created in
2931 */
2932 if (event->parent)
2933 event = event->parent;
2934
2935 return task_pid_nr_ns(p, event->ns);
2936}
2937
2938static void perf_output_read_one(struct perf_output_handle *handle,
2939 struct perf_event *event)
2940{
2941 u64 read_format = event->attr.read_format;
2942 u64 values[4];
2943 int n = 0;
2944
2945 values[n++] = atomic64_read(&event->count);
2946 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2947 values[n++] = event->total_time_enabled +
2948 atomic64_read(&event->child_total_time_enabled);
2949 }
2950 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2951 values[n++] = event->total_time_running +
2952 atomic64_read(&event->child_total_time_running);
2953 }
2954 if (read_format & PERF_FORMAT_ID)
2955 values[n++] = primary_event_id(event);
2956
2957 perf_output_copy(handle, values, n * sizeof(u64));
2958}
2959
2960/*
2961 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
2962 */
2963static void perf_output_read_group(struct perf_output_handle *handle,
2964 struct perf_event *event)
2965{
2966 struct perf_event *leader = event->group_leader, *sub;
2967 u64 read_format = event->attr.read_format;
2968 u64 values[5];
2969 int n = 0;
2970
2971 values[n++] = 1 + leader->nr_siblings;
2972
2973 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2974 values[n++] = leader->total_time_enabled;
2975
2976 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2977 values[n++] = leader->total_time_running;
2978
2979 if (leader != event)
2980 leader->pmu->read(leader);
2981
2982 values[n++] = atomic64_read(&leader->count);
2983 if (read_format & PERF_FORMAT_ID)
2984 values[n++] = primary_event_id(leader);
2985
2986 perf_output_copy(handle, values, n * sizeof(u64));
2987
2988 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
2989 n = 0;
2990
2991 if (sub != event)
2992 sub->pmu->read(sub);
2993
2994 values[n++] = atomic64_read(&sub->count);
2995 if (read_format & PERF_FORMAT_ID)
2996 values[n++] = primary_event_id(sub);
2997
2998 perf_output_copy(handle, values, n * sizeof(u64));
2999 }
3000}
3001
3002static void perf_output_read(struct perf_output_handle *handle,
3003 struct perf_event *event)
3004{
3005 if (event->attr.read_format & PERF_FORMAT_GROUP)
3006 perf_output_read_group(handle, event);
3007 else
3008 perf_output_read_one(handle, event);
3009}
3010
3011void perf_output_sample(struct perf_output_handle *handle,
3012 struct perf_event_header *header,
3013 struct perf_sample_data *data,
3014 struct perf_event *event)
3015{
3016 u64 sample_type = data->type;
3017
3018 perf_output_put(handle, *header);
3019
3020 if (sample_type & PERF_SAMPLE_IP)
3021 perf_output_put(handle, data->ip);
3022
3023 if (sample_type & PERF_SAMPLE_TID)
3024 perf_output_put(handle, data->tid_entry);
3025
3026 if (sample_type & PERF_SAMPLE_TIME)
3027 perf_output_put(handle, data->time);
3028
3029 if (sample_type & PERF_SAMPLE_ADDR)
3030 perf_output_put(handle, data->addr);
3031
3032 if (sample_type & PERF_SAMPLE_ID)
3033 perf_output_put(handle, data->id);
3034
3035 if (sample_type & PERF_SAMPLE_STREAM_ID)
3036 perf_output_put(handle, data->stream_id);
3037
3038 if (sample_type & PERF_SAMPLE_CPU)
3039 perf_output_put(handle, data->cpu_entry);
3040
3041 if (sample_type & PERF_SAMPLE_PERIOD)
3042 perf_output_put(handle, data->period);
3043
3044 if (sample_type & PERF_SAMPLE_READ)
3045 perf_output_read(handle, event);
3046
3047 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3048 if (data->callchain) {
3049 int size = 1;
3050
3051 if (data->callchain)
3052 size += data->callchain->nr;
3053
3054 size *= sizeof(u64);
3055
3056 perf_output_copy(handle, data->callchain, size);
3057 } else {
3058 u64 nr = 0;
3059 perf_output_put(handle, nr);
3060 }
3061 }
3062
3063 if (sample_type & PERF_SAMPLE_RAW) {
3064 if (data->raw) {
3065 perf_output_put(handle, data->raw->size);
3066 perf_output_copy(handle, data->raw->data,
3067 data->raw->size);
3068 } else {
3069 struct {
3070 u32 size;
3071 u32 data;
3072 } raw = {
3073 .size = sizeof(u32),
3074 .data = 0,
3075 };
3076 perf_output_put(handle, raw);
3077 }
3078 }
3079}
3080
3081void perf_prepare_sample(struct perf_event_header *header,
3082 struct perf_sample_data *data,
3083 struct perf_event *event,
3084 struct pt_regs *regs)
3085{
3086 u64 sample_type = event->attr.sample_type;
3087
3088 data->type = sample_type;
3089
3090 header->type = PERF_RECORD_SAMPLE;
3091 header->size = sizeof(*header);
3092
3093 header->misc = 0;
3094 header->misc |= perf_misc_flags(regs);
3095
3096 if (sample_type & PERF_SAMPLE_IP) {
3097 data->ip = perf_instruction_pointer(regs);
3098
3099 header->size += sizeof(data->ip);
3100 }
3101
3102 if (sample_type & PERF_SAMPLE_TID) {
3103 /* namespace issues */
3104 data->tid_entry.pid = perf_event_pid(event, current);
3105 data->tid_entry.tid = perf_event_tid(event, current);
3106
3107 header->size += sizeof(data->tid_entry);
3108 }
3109
3110 if (sample_type & PERF_SAMPLE_TIME) {
3111 data->time = perf_clock();
3112
3113 header->size += sizeof(data->time);
3114 }
3115
3116 if (sample_type & PERF_SAMPLE_ADDR)
3117 header->size += sizeof(data->addr);
3118
3119 if (sample_type & PERF_SAMPLE_ID) {
3120 data->id = primary_event_id(event);
3121
3122 header->size += sizeof(data->id);
3123 }
3124
3125 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3126 data->stream_id = event->id;
3127
3128 header->size += sizeof(data->stream_id);
3129 }
3130
3131 if (sample_type & PERF_SAMPLE_CPU) {
3132 data->cpu_entry.cpu = raw_smp_processor_id();
3133 data->cpu_entry.reserved = 0;
3134
3135 header->size += sizeof(data->cpu_entry);
3136 }
3137
3138 if (sample_type & PERF_SAMPLE_PERIOD)
3139 header->size += sizeof(data->period);
3140
3141 if (sample_type & PERF_SAMPLE_READ)
3142 header->size += perf_event_read_size(event);
3143
3144 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3145 int size = 1;
3146
3147 data->callchain = perf_callchain(regs);
3148
3149 if (data->callchain)
3150 size += data->callchain->nr;
3151
3152 header->size += size * sizeof(u64);
3153 }
3154
3155 if (sample_type & PERF_SAMPLE_RAW) {
3156 int size = sizeof(u32);
3157
3158 if (data->raw)
3159 size += data->raw->size;
3160 else
3161 size += sizeof(u32);
3162
3163 WARN_ON_ONCE(size & (sizeof(u64)-1));
3164 header->size += size;
3165 }
3166}
3167
3168static void perf_event_output(struct perf_event *event, int nmi,
3169 struct perf_sample_data *data,
3170 struct pt_regs *regs)
3171{
3172 struct perf_output_handle handle;
3173 struct perf_event_header header;
3174
3175 perf_prepare_sample(&header, data, event, regs);
3176
3177 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3178 return;
3179
3180 perf_output_sample(&handle, &header, data, event);
3181
3182 perf_output_end(&handle);
3183}
3184
3185/*
3186 * read event_id
3187 */
3188
3189struct perf_read_event {
3190 struct perf_event_header header;
3191
3192 u32 pid;
3193 u32 tid;
3194};
3195
3196static void
3197perf_event_read_event(struct perf_event *event,
3198 struct task_struct *task)
3199{
3200 struct perf_output_handle handle;
3201 struct perf_read_event read_event = {
3202 .header = {
3203 .type = PERF_RECORD_READ,
3204 .misc = 0,
3205 .size = sizeof(read_event) + perf_event_read_size(event),
3206 },
3207 .pid = perf_event_pid(event, task),
3208 .tid = perf_event_tid(event, task),
3209 };
3210 int ret;
3211
3212 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3213 if (ret)
3214 return;
3215
3216 perf_output_put(&handle, read_event);
3217 perf_output_read(&handle, event);
3218
3219 perf_output_end(&handle);
3220}
3221
3222/*
3223 * task tracking -- fork/exit
3224 *
3225 * enabled by: attr.comm | attr.mmap | attr.task
3226 */
3227
3228struct perf_task_event {
3229 struct task_struct *task;
3230 struct perf_event_context *task_ctx;
3231
3232 struct {
3233 struct perf_event_header header;
3234
3235 u32 pid;
3236 u32 ppid;
3237 u32 tid;
3238 u32 ptid;
3239 u64 time;
3240 } event_id;
3241};
3242
3243static void perf_event_task_output(struct perf_event *event,
3244 struct perf_task_event *task_event)
3245{
3246 struct perf_output_handle handle;
3247 int size;
3248 struct task_struct *task = task_event->task;
3249 int ret;
3250
3251 size = task_event->event_id.header.size;
3252 ret = perf_output_begin(&handle, event, size, 0, 0);
3253
3254 if (ret)
3255 return;
3256
3257 task_event->event_id.pid = perf_event_pid(event, task);
3258 task_event->event_id.ppid = perf_event_pid(event, current);
3259
3260 task_event->event_id.tid = perf_event_tid(event, task);
3261 task_event->event_id.ptid = perf_event_tid(event, current);
3262
3263 task_event->event_id.time = perf_clock();
3264
3265 perf_output_put(&handle, task_event->event_id);
3266
3267 perf_output_end(&handle);
3268}
3269
3270static int perf_event_task_match(struct perf_event *event)
3271{
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003272 if (event->cpu != -1 && event->cpu != smp_processor_id())
3273 return 0;
3274
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003275 if (event->attr.comm || event->attr.mmap || event->attr.task)
3276 return 1;
3277
3278 return 0;
3279}
3280
3281static void perf_event_task_ctx(struct perf_event_context *ctx,
3282 struct perf_task_event *task_event)
3283{
3284 struct perf_event *event;
3285
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003286 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3287 if (perf_event_task_match(event))
3288 perf_event_task_output(event, task_event);
3289 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003290}
3291
3292static void perf_event_task_event(struct perf_task_event *task_event)
3293{
3294 struct perf_cpu_context *cpuctx;
3295 struct perf_event_context *ctx = task_event->task_ctx;
3296
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01003297 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003298 cpuctx = &get_cpu_var(perf_cpu_context);
3299 perf_event_task_ctx(&cpuctx->ctx, task_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003300 if (!ctx)
3301 ctx = rcu_dereference(task_event->task->perf_event_ctxp);
3302 if (ctx)
3303 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003304 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003305 rcu_read_unlock();
3306}
3307
3308static void perf_event_task(struct task_struct *task,
3309 struct perf_event_context *task_ctx,
3310 int new)
3311{
3312 struct perf_task_event task_event;
3313
3314 if (!atomic_read(&nr_comm_events) &&
3315 !atomic_read(&nr_mmap_events) &&
3316 !atomic_read(&nr_task_events))
3317 return;
3318
3319 task_event = (struct perf_task_event){
3320 .task = task,
3321 .task_ctx = task_ctx,
3322 .event_id = {
3323 .header = {
3324 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3325 .misc = 0,
3326 .size = sizeof(task_event.event_id),
3327 },
3328 /* .pid */
3329 /* .ppid */
3330 /* .tid */
3331 /* .ptid */
3332 },
3333 };
3334
3335 perf_event_task_event(&task_event);
3336}
3337
3338void perf_event_fork(struct task_struct *task)
3339{
3340 perf_event_task(task, NULL, 1);
3341}
3342
3343/*
3344 * comm tracking
3345 */
3346
3347struct perf_comm_event {
3348 struct task_struct *task;
3349 char *comm;
3350 int comm_size;
3351
3352 struct {
3353 struct perf_event_header header;
3354
3355 u32 pid;
3356 u32 tid;
3357 } event_id;
3358};
3359
3360static void perf_event_comm_output(struct perf_event *event,
3361 struct perf_comm_event *comm_event)
3362{
3363 struct perf_output_handle handle;
3364 int size = comm_event->event_id.header.size;
3365 int ret = perf_output_begin(&handle, event, size, 0, 0);
3366
3367 if (ret)
3368 return;
3369
3370 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3371 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3372
3373 perf_output_put(&handle, comm_event->event_id);
3374 perf_output_copy(&handle, comm_event->comm,
3375 comm_event->comm_size);
3376 perf_output_end(&handle);
3377}
3378
3379static int perf_event_comm_match(struct perf_event *event)
3380{
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003381 if (event->cpu != -1 && event->cpu != smp_processor_id())
3382 return 0;
3383
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003384 if (event->attr.comm)
3385 return 1;
3386
3387 return 0;
3388}
3389
3390static void perf_event_comm_ctx(struct perf_event_context *ctx,
3391 struct perf_comm_event *comm_event)
3392{
3393 struct perf_event *event;
3394
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003395 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3396 if (perf_event_comm_match(event))
3397 perf_event_comm_output(event, comm_event);
3398 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003399}
3400
3401static void perf_event_comm_event(struct perf_comm_event *comm_event)
3402{
3403 struct perf_cpu_context *cpuctx;
3404 struct perf_event_context *ctx;
3405 unsigned int size;
3406 char comm[TASK_COMM_LEN];
3407
3408 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01003409 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003410 size = ALIGN(strlen(comm)+1, sizeof(u64));
3411
3412 comm_event->comm = comm;
3413 comm_event->comm_size = size;
3414
3415 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3416
Peter Zijlstraf6595f32009-11-20 22:19:47 +01003417 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003418 cpuctx = &get_cpu_var(perf_cpu_context);
3419 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003420 ctx = rcu_dereference(current->perf_event_ctxp);
3421 if (ctx)
3422 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003423 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003424 rcu_read_unlock();
3425}
3426
3427void perf_event_comm(struct task_struct *task)
3428{
3429 struct perf_comm_event comm_event;
3430
3431 if (task->perf_event_ctxp)
3432 perf_event_enable_on_exec(task);
3433
3434 if (!atomic_read(&nr_comm_events))
3435 return;
3436
3437 comm_event = (struct perf_comm_event){
3438 .task = task,
3439 /* .comm */
3440 /* .comm_size */
3441 .event_id = {
3442 .header = {
3443 .type = PERF_RECORD_COMM,
3444 .misc = 0,
3445 /* .size */
3446 },
3447 /* .pid */
3448 /* .tid */
3449 },
3450 };
3451
3452 perf_event_comm_event(&comm_event);
3453}
3454
3455/*
3456 * mmap tracking
3457 */
3458
3459struct perf_mmap_event {
3460 struct vm_area_struct *vma;
3461
3462 const char *file_name;
3463 int file_size;
3464
3465 struct {
3466 struct perf_event_header header;
3467
3468 u32 pid;
3469 u32 tid;
3470 u64 start;
3471 u64 len;
3472 u64 pgoff;
3473 } event_id;
3474};
3475
3476static void perf_event_mmap_output(struct perf_event *event,
3477 struct perf_mmap_event *mmap_event)
3478{
3479 struct perf_output_handle handle;
3480 int size = mmap_event->event_id.header.size;
3481 int ret = perf_output_begin(&handle, event, size, 0, 0);
3482
3483 if (ret)
3484 return;
3485
3486 mmap_event->event_id.pid = perf_event_pid(event, current);
3487 mmap_event->event_id.tid = perf_event_tid(event, current);
3488
3489 perf_output_put(&handle, mmap_event->event_id);
3490 perf_output_copy(&handle, mmap_event->file_name,
3491 mmap_event->file_size);
3492 perf_output_end(&handle);
3493}
3494
3495static int perf_event_mmap_match(struct perf_event *event,
3496 struct perf_mmap_event *mmap_event)
3497{
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003498 if (event->cpu != -1 && event->cpu != smp_processor_id())
3499 return 0;
3500
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003501 if (event->attr.mmap)
3502 return 1;
3503
3504 return 0;
3505}
3506
3507static void perf_event_mmap_ctx(struct perf_event_context *ctx,
3508 struct perf_mmap_event *mmap_event)
3509{
3510 struct perf_event *event;
3511
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003512 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3513 if (perf_event_mmap_match(event, mmap_event))
3514 perf_event_mmap_output(event, mmap_event);
3515 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003516}
3517
3518static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
3519{
3520 struct perf_cpu_context *cpuctx;
3521 struct perf_event_context *ctx;
3522 struct vm_area_struct *vma = mmap_event->vma;
3523 struct file *file = vma->vm_file;
3524 unsigned int size;
3525 char tmp[16];
3526 char *buf = NULL;
3527 const char *name;
3528
3529 memset(tmp, 0, sizeof(tmp));
3530
3531 if (file) {
3532 /*
3533 * d_path works from the end of the buffer backwards, so we
3534 * need to add enough zero bytes after the string to handle
3535 * the 64bit alignment we do later.
3536 */
3537 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3538 if (!buf) {
3539 name = strncpy(tmp, "//enomem", sizeof(tmp));
3540 goto got_name;
3541 }
3542 name = d_path(&file->f_path, buf, PATH_MAX);
3543 if (IS_ERR(name)) {
3544 name = strncpy(tmp, "//toolong", sizeof(tmp));
3545 goto got_name;
3546 }
3547 } else {
3548 if (arch_vma_name(mmap_event->vma)) {
3549 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3550 sizeof(tmp));
3551 goto got_name;
3552 }
3553
3554 if (!vma->vm_mm) {
3555 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3556 goto got_name;
3557 }
3558
3559 name = strncpy(tmp, "//anon", sizeof(tmp));
3560 goto got_name;
3561 }
3562
3563got_name:
3564 size = ALIGN(strlen(name)+1, sizeof(u64));
3565
3566 mmap_event->file_name = name;
3567 mmap_event->file_size = size;
3568
3569 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
3570
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01003571 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003572 cpuctx = &get_cpu_var(perf_cpu_context);
3573 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003574 ctx = rcu_dereference(current->perf_event_ctxp);
3575 if (ctx)
3576 perf_event_mmap_ctx(ctx, mmap_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003577 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003578 rcu_read_unlock();
3579
3580 kfree(buf);
3581}
3582
3583void __perf_event_mmap(struct vm_area_struct *vma)
3584{
3585 struct perf_mmap_event mmap_event;
3586
3587 if (!atomic_read(&nr_mmap_events))
3588 return;
3589
3590 mmap_event = (struct perf_mmap_event){
3591 .vma = vma,
3592 /* .file_name */
3593 /* .file_size */
3594 .event_id = {
3595 .header = {
3596 .type = PERF_RECORD_MMAP,
3597 .misc = 0,
3598 /* .size */
3599 },
3600 /* .pid */
3601 /* .tid */
3602 .start = vma->vm_start,
3603 .len = vma->vm_end - vma->vm_start,
3604 .pgoff = vma->vm_pgoff,
3605 },
3606 };
3607
3608 perf_event_mmap_event(&mmap_event);
3609}
3610
3611/*
3612 * IRQ throttle logging
3613 */
3614
3615static void perf_log_throttle(struct perf_event *event, int enable)
3616{
3617 struct perf_output_handle handle;
3618 int ret;
3619
3620 struct {
3621 struct perf_event_header header;
3622 u64 time;
3623 u64 id;
3624 u64 stream_id;
3625 } throttle_event = {
3626 .header = {
3627 .type = PERF_RECORD_THROTTLE,
3628 .misc = 0,
3629 .size = sizeof(throttle_event),
3630 },
3631 .time = perf_clock(),
3632 .id = primary_event_id(event),
3633 .stream_id = event->id,
3634 };
3635
3636 if (enable)
3637 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
3638
3639 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
3640 if (ret)
3641 return;
3642
3643 perf_output_put(&handle, throttle_event);
3644 perf_output_end(&handle);
3645}
3646
3647/*
3648 * Generic event overflow handling, sampling.
3649 */
3650
3651static int __perf_event_overflow(struct perf_event *event, int nmi,
3652 int throttle, struct perf_sample_data *data,
3653 struct pt_regs *regs)
3654{
3655 int events = atomic_read(&event->event_limit);
3656 struct hw_perf_event *hwc = &event->hw;
3657 int ret = 0;
3658
3659 throttle = (throttle && event->pmu->unthrottle != NULL);
3660
3661 if (!throttle) {
3662 hwc->interrupts++;
3663 } else {
3664 if (hwc->interrupts != MAX_INTERRUPTS) {
3665 hwc->interrupts++;
3666 if (HZ * hwc->interrupts >
3667 (u64)sysctl_perf_event_sample_rate) {
3668 hwc->interrupts = MAX_INTERRUPTS;
3669 perf_log_throttle(event, 0);
3670 ret = 1;
3671 }
3672 } else {
3673 /*
3674 * Keep re-disabling events even though on the previous
3675 * pass we disabled it - just in case we raced with a
3676 * sched-in and the event got enabled again:
3677 */
3678 ret = 1;
3679 }
3680 }
3681
3682 if (event->attr.freq) {
3683 u64 now = perf_clock();
3684 s64 delta = now - hwc->freq_stamp;
3685
3686 hwc->freq_stamp = now;
3687
3688 if (delta > 0 && delta < TICK_NSEC)
3689 perf_adjust_period(event, NSEC_PER_SEC / (int)delta);
3690 }
3691
3692 /*
3693 * XXX event_limit might not quite work as expected on inherited
3694 * events
3695 */
3696
3697 event->pending_kill = POLL_IN;
3698 if (events && atomic_dec_and_test(&event->event_limit)) {
3699 ret = 1;
3700 event->pending_kill = POLL_HUP;
3701 if (nmi) {
3702 event->pending_disable = 1;
3703 perf_pending_queue(&event->pending,
3704 perf_pending_event);
3705 } else
3706 perf_event_disable(event);
3707 }
3708
Peter Zijlstra453f19e2009-11-20 22:19:43 +01003709 if (event->overflow_handler)
3710 event->overflow_handler(event, nmi, data, regs);
3711 else
3712 perf_event_output(event, nmi, data, regs);
3713
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003714 return ret;
3715}
3716
3717int perf_event_overflow(struct perf_event *event, int nmi,
3718 struct perf_sample_data *data,
3719 struct pt_regs *regs)
3720{
3721 return __perf_event_overflow(event, nmi, 1, data, regs);
3722}
3723
3724/*
3725 * Generic software event infrastructure
3726 */
3727
3728/*
3729 * We directly increment event->count and keep a second value in
3730 * event->hw.period_left to count intervals. This period event
3731 * is kept in the range [-sample_period, 0] so that we can use the
3732 * sign as trigger.
3733 */
3734
3735static u64 perf_swevent_set_period(struct perf_event *event)
3736{
3737 struct hw_perf_event *hwc = &event->hw;
3738 u64 period = hwc->last_period;
3739 u64 nr, offset;
3740 s64 old, val;
3741
3742 hwc->last_period = hwc->sample_period;
3743
3744again:
3745 old = val = atomic64_read(&hwc->period_left);
3746 if (val < 0)
3747 return 0;
3748
3749 nr = div64_u64(period + val, period);
3750 offset = nr * period;
3751 val -= offset;
3752 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
3753 goto again;
3754
3755 return nr;
3756}
3757
Peter Zijlstra0cff7842009-11-20 22:19:44 +01003758static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003759 int nmi, struct perf_sample_data *data,
3760 struct pt_regs *regs)
3761{
3762 struct hw_perf_event *hwc = &event->hw;
3763 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003764
3765 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01003766 if (!overflow)
3767 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003768
3769 if (hwc->interrupts == MAX_INTERRUPTS)
3770 return;
3771
3772 for (; overflow; overflow--) {
3773 if (__perf_event_overflow(event, nmi, throttle,
3774 data, regs)) {
3775 /*
3776 * We inhibit the overflow from happening when
3777 * hwc->interrupts == MAX_INTERRUPTS.
3778 */
3779 break;
3780 }
3781 throttle = 1;
3782 }
3783}
3784
3785static void perf_swevent_unthrottle(struct perf_event *event)
3786{
3787 /*
3788 * Nothing to do, we already reset hwc->interrupts.
3789 */
3790}
3791
3792static void perf_swevent_add(struct perf_event *event, u64 nr,
3793 int nmi, struct perf_sample_data *data,
3794 struct pt_regs *regs)
3795{
3796 struct hw_perf_event *hwc = &event->hw;
3797
3798 atomic64_add(nr, &event->count);
3799
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003800 if (!regs)
3801 return;
3802
Peter Zijlstra0cff7842009-11-20 22:19:44 +01003803 if (!hwc->sample_period)
3804 return;
3805
3806 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
3807 return perf_swevent_overflow(event, 1, nmi, data, regs);
3808
3809 if (atomic64_add_negative(nr, &hwc->period_left))
3810 return;
3811
3812 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003813}
3814
3815static int perf_swevent_is_counting(struct perf_event *event)
3816{
3817 /*
3818 * The event is active, we're good!
3819 */
3820 if (event->state == PERF_EVENT_STATE_ACTIVE)
3821 return 1;
3822
3823 /*
3824 * The event is off/error, not counting.
3825 */
3826 if (event->state != PERF_EVENT_STATE_INACTIVE)
3827 return 0;
3828
3829 /*
3830 * The event is inactive, if the context is active
3831 * we're part of a group that didn't make it on the 'pmu',
3832 * not counting.
3833 */
3834 if (event->ctx->is_active)
3835 return 0;
3836
3837 /*
3838 * We're inactive and the context is too, this means the
3839 * task is scheduled out, we're counting events that happen
3840 * to us, like migration events.
3841 */
3842 return 1;
3843}
3844
Li Zefan6fb29152009-10-15 11:21:42 +08003845static int perf_tp_event_match(struct perf_event *event,
3846 struct perf_sample_data *data);
3847
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01003848static int perf_exclude_event(struct perf_event *event,
3849 struct pt_regs *regs)
3850{
3851 if (regs) {
3852 if (event->attr.exclude_user && user_mode(regs))
3853 return 1;
3854
3855 if (event->attr.exclude_kernel && !user_mode(regs))
3856 return 1;
3857 }
3858
3859 return 0;
3860}
3861
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003862static int perf_swevent_match(struct perf_event *event,
3863 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08003864 u32 event_id,
3865 struct perf_sample_data *data,
3866 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003867{
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003868 if (event->cpu != -1 && event->cpu != smp_processor_id())
3869 return 0;
3870
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003871 if (!perf_swevent_is_counting(event))
3872 return 0;
3873
3874 if (event->attr.type != type)
3875 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01003876
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003877 if (event->attr.config != event_id)
3878 return 0;
3879
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01003880 if (perf_exclude_event(event, regs))
3881 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003882
Li Zefan6fb29152009-10-15 11:21:42 +08003883 if (event->attr.type == PERF_TYPE_TRACEPOINT &&
3884 !perf_tp_event_match(event, data))
3885 return 0;
3886
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003887 return 1;
3888}
3889
3890static void perf_swevent_ctx_event(struct perf_event_context *ctx,
3891 enum perf_type_id type,
3892 u32 event_id, u64 nr, int nmi,
3893 struct perf_sample_data *data,
3894 struct pt_regs *regs)
3895{
3896 struct perf_event *event;
3897
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003898 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08003899 if (perf_swevent_match(event, type, event_id, data, regs))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003900 perf_swevent_add(event, nr, nmi, data, regs);
3901 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003902}
3903
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003904int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003905{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003906 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
3907 int rctx;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003908
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003909 if (in_nmi())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003910 rctx = 3;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003911 else if (in_irq())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003912 rctx = 2;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003913 else if (in_softirq())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003914 rctx = 1;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003915 else
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003916 rctx = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003917
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003918 if (cpuctx->recursion[rctx]) {
3919 put_cpu_var(perf_cpu_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003920 return -1;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003921 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003922
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003923 cpuctx->recursion[rctx]++;
3924 barrier();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003925
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003926 return rctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003927}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01003928EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003929
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003930void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003931{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003932 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3933 barrier();
Frederic Weisbeckerfe612672009-11-24 20:38:22 +01003934 cpuctx->recursion[rctx]--;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003935 put_cpu_var(perf_cpu_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003936}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01003937EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003938
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003939static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
3940 u64 nr, int nmi,
3941 struct perf_sample_data *data,
3942 struct pt_regs *regs)
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003943{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003944 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003945 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003946
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003947 cpuctx = &__get_cpu_var(perf_cpu_context);
Peter Zijlstra81520182009-11-20 22:19:45 +01003948 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003949 perf_swevent_ctx_event(&cpuctx->ctx, type, event_id,
3950 nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003951 /*
3952 * doesn't really matter which of the child contexts the
3953 * events ends up in.
3954 */
3955 ctx = rcu_dereference(current->perf_event_ctxp);
3956 if (ctx)
3957 perf_swevent_ctx_event(ctx, type, event_id, nr, nmi, data, regs);
3958 rcu_read_unlock();
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003959}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003960
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003961void __perf_sw_event(u32 event_id, u64 nr, int nmi,
3962 struct pt_regs *regs, u64 addr)
3963{
Ingo Molnara4234bf2009-11-23 10:57:59 +01003964 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003965 int rctx;
3966
3967 rctx = perf_swevent_get_recursion_context();
3968 if (rctx < 0)
3969 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003970
Ingo Molnara4234bf2009-11-23 10:57:59 +01003971 data.addr = addr;
3972 data.raw = NULL;
3973
3974 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003975
3976 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003977}
3978
3979static void perf_swevent_read(struct perf_event *event)
3980{
3981}
3982
3983static int perf_swevent_enable(struct perf_event *event)
3984{
3985 struct hw_perf_event *hwc = &event->hw;
3986
3987 if (hwc->sample_period) {
3988 hwc->last_period = hwc->sample_period;
3989 perf_swevent_set_period(event);
3990 }
3991 return 0;
3992}
3993
3994static void perf_swevent_disable(struct perf_event *event)
3995{
3996}
3997
3998static const struct pmu perf_ops_generic = {
3999 .enable = perf_swevent_enable,
4000 .disable = perf_swevent_disable,
4001 .read = perf_swevent_read,
4002 .unthrottle = perf_swevent_unthrottle,
4003};
4004
4005/*
4006 * hrtimer based swevent callback
4007 */
4008
4009static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4010{
4011 enum hrtimer_restart ret = HRTIMER_RESTART;
4012 struct perf_sample_data data;
4013 struct pt_regs *regs;
4014 struct perf_event *event;
4015 u64 period;
4016
4017 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4018 event->pmu->read(event);
4019
4020 data.addr = 0;
Xiao Guangrong21140f42009-12-10 14:00:51 +08004021 data.raw = NULL;
Xiao Guangrong59d069e2009-12-01 17:30:08 +08004022 data.period = event->hw.last_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004023 regs = get_irq_regs();
4024 /*
4025 * In case we exclude kernel IPs or are somehow not in interrupt
4026 * context, provide the next best thing, the user IP.
4027 */
4028 if ((event->attr.exclude_kernel || !regs) &&
4029 !event->attr.exclude_user)
4030 regs = task_pt_regs(current);
4031
4032 if (regs) {
Soeren Sandmann54f44072009-10-22 18:34:08 +02004033 if (!(event->attr.exclude_idle && current->pid == 0))
4034 if (perf_event_overflow(event, 0, &data, regs))
4035 ret = HRTIMER_NORESTART;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004036 }
4037
4038 period = max_t(u64, 10000, event->hw.sample_period);
4039 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4040
4041 return ret;
4042}
4043
Soeren Sandmann721a6692009-09-15 14:33:08 +02004044static void perf_swevent_start_hrtimer(struct perf_event *event)
4045{
4046 struct hw_perf_event *hwc = &event->hw;
4047
4048 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4049 hwc->hrtimer.function = perf_swevent_hrtimer;
4050 if (hwc->sample_period) {
4051 u64 period;
4052
4053 if (hwc->remaining) {
4054 if (hwc->remaining < 0)
4055 period = 10000;
4056 else
4057 period = hwc->remaining;
4058 hwc->remaining = 0;
4059 } else {
4060 period = max_t(u64, 10000, hwc->sample_period);
4061 }
4062 __hrtimer_start_range_ns(&hwc->hrtimer,
4063 ns_to_ktime(period), 0,
4064 HRTIMER_MODE_REL, 0);
4065 }
4066}
4067
4068static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4069{
4070 struct hw_perf_event *hwc = &event->hw;
4071
4072 if (hwc->sample_period) {
4073 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4074 hwc->remaining = ktime_to_ns(remaining);
4075
4076 hrtimer_cancel(&hwc->hrtimer);
4077 }
4078}
4079
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004080/*
4081 * Software event: cpu wall time clock
4082 */
4083
4084static void cpu_clock_perf_event_update(struct perf_event *event)
4085{
4086 int cpu = raw_smp_processor_id();
4087 s64 prev;
4088 u64 now;
4089
4090 now = cpu_clock(cpu);
Xiao Guangrongec89a06f2009-12-09 11:30:36 +08004091 prev = atomic64_xchg(&event->hw.prev_count, now);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004092 atomic64_add(now - prev, &event->count);
4093}
4094
4095static int cpu_clock_perf_event_enable(struct perf_event *event)
4096{
4097 struct hw_perf_event *hwc = &event->hw;
4098 int cpu = raw_smp_processor_id();
4099
4100 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Soeren Sandmann721a6692009-09-15 14:33:08 +02004101 perf_swevent_start_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004102
4103 return 0;
4104}
4105
4106static void cpu_clock_perf_event_disable(struct perf_event *event)
4107{
Soeren Sandmann721a6692009-09-15 14:33:08 +02004108 perf_swevent_cancel_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004109 cpu_clock_perf_event_update(event);
4110}
4111
4112static void cpu_clock_perf_event_read(struct perf_event *event)
4113{
4114 cpu_clock_perf_event_update(event);
4115}
4116
4117static const struct pmu perf_ops_cpu_clock = {
4118 .enable = cpu_clock_perf_event_enable,
4119 .disable = cpu_clock_perf_event_disable,
4120 .read = cpu_clock_perf_event_read,
4121};
4122
4123/*
4124 * Software event: task time clock
4125 */
4126
4127static void task_clock_perf_event_update(struct perf_event *event, u64 now)
4128{
4129 u64 prev;
4130 s64 delta;
4131
4132 prev = atomic64_xchg(&event->hw.prev_count, now);
4133 delta = now - prev;
4134 atomic64_add(delta, &event->count);
4135}
4136
4137static int task_clock_perf_event_enable(struct perf_event *event)
4138{
4139 struct hw_perf_event *hwc = &event->hw;
4140 u64 now;
4141
4142 now = event->ctx->time;
4143
4144 atomic64_set(&hwc->prev_count, now);
Soeren Sandmann721a6692009-09-15 14:33:08 +02004145
4146 perf_swevent_start_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004147
4148 return 0;
4149}
4150
4151static void task_clock_perf_event_disable(struct perf_event *event)
4152{
Soeren Sandmann721a6692009-09-15 14:33:08 +02004153 perf_swevent_cancel_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004154 task_clock_perf_event_update(event, event->ctx->time);
4155
4156}
4157
4158static void task_clock_perf_event_read(struct perf_event *event)
4159{
4160 u64 time;
4161
4162 if (!in_nmi()) {
4163 update_context_time(event->ctx);
4164 time = event->ctx->time;
4165 } else {
4166 u64 now = perf_clock();
4167 u64 delta = now - event->ctx->timestamp;
4168 time = event->ctx->time + delta;
4169 }
4170
4171 task_clock_perf_event_update(event, time);
4172}
4173
4174static const struct pmu perf_ops_task_clock = {
4175 .enable = task_clock_perf_event_enable,
4176 .disable = task_clock_perf_event_disable,
4177 .read = task_clock_perf_event_read,
4178};
4179
Li Zefan07b139c2009-12-21 14:27:35 +08004180#ifdef CONFIG_EVENT_TRACING
Li Zefan6fb29152009-10-15 11:21:42 +08004181
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004182void perf_tp_event(int event_id, u64 addr, u64 count, void *record,
4183 int entry_size)
4184{
4185 struct perf_raw_record raw = {
4186 .size = entry_size,
4187 .data = record,
4188 };
4189
4190 struct perf_sample_data data = {
4191 .addr = addr,
4192 .raw = &raw,
4193 };
4194
4195 struct pt_regs *regs = get_irq_regs();
4196
4197 if (!regs)
4198 regs = task_pt_regs(current);
4199
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004200 /* Trace events already protected against recursion */
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004201 do_perf_sw_event(PERF_TYPE_TRACEPOINT, event_id, count, 1,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004202 &data, regs);
4203}
4204EXPORT_SYMBOL_GPL(perf_tp_event);
4205
Li Zefan6fb29152009-10-15 11:21:42 +08004206static int perf_tp_event_match(struct perf_event *event,
4207 struct perf_sample_data *data)
4208{
4209 void *record = data->raw->data;
4210
4211 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4212 return 1;
4213 return 0;
4214}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004215
4216static void tp_perf_event_destroy(struct perf_event *event)
4217{
4218 ftrace_profile_disable(event->attr.config);
4219}
4220
4221static const struct pmu *tp_perf_event_init(struct perf_event *event)
4222{
4223 /*
4224 * Raw tracepoint data is a severe data leak, only allow root to
4225 * have these.
4226 */
4227 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4228 perf_paranoid_tracepoint_raw() &&
4229 !capable(CAP_SYS_ADMIN))
4230 return ERR_PTR(-EPERM);
4231
4232 if (ftrace_profile_enable(event->attr.config))
4233 return NULL;
4234
4235 event->destroy = tp_perf_event_destroy;
4236
4237 return &perf_ops_generic;
4238}
Li Zefan6fb29152009-10-15 11:21:42 +08004239
4240static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4241{
4242 char *filter_str;
4243 int ret;
4244
4245 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4246 return -EINVAL;
4247
4248 filter_str = strndup_user(arg, PAGE_SIZE);
4249 if (IS_ERR(filter_str))
4250 return PTR_ERR(filter_str);
4251
4252 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4253
4254 kfree(filter_str);
4255 return ret;
4256}
4257
4258static void perf_event_free_filter(struct perf_event *event)
4259{
4260 ftrace_profile_free_filter(event);
4261}
4262
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004263#else
Li Zefan6fb29152009-10-15 11:21:42 +08004264
4265static int perf_tp_event_match(struct perf_event *event,
4266 struct perf_sample_data *data)
4267{
4268 return 1;
4269}
4270
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004271static const struct pmu *tp_perf_event_init(struct perf_event *event)
4272{
4273 return NULL;
4274}
Li Zefan6fb29152009-10-15 11:21:42 +08004275
4276static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4277{
4278 return -ENOENT;
4279}
4280
4281static void perf_event_free_filter(struct perf_event *event)
4282{
4283}
4284
Li Zefan07b139c2009-12-21 14:27:35 +08004285#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004286
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004287#ifdef CONFIG_HAVE_HW_BREAKPOINT
4288static void bp_perf_event_destroy(struct perf_event *event)
4289{
4290 release_bp_slot(event);
4291}
4292
4293static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4294{
4295 int err;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004296
4297 err = register_perf_hw_breakpoint(bp);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004298 if (err)
4299 return ERR_PTR(err);
4300
4301 bp->destroy = bp_perf_event_destroy;
4302
4303 return &perf_ops_bp;
4304}
4305
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004306void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004307{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004308 struct perf_sample_data sample;
4309 struct pt_regs *regs = data;
4310
Xiao Guangrong5e855db2009-12-10 17:08:54 +08004311 sample.raw = NULL;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004312 sample.addr = bp->attr.bp_addr;
4313
4314 if (!perf_exclude_event(bp, regs))
4315 perf_swevent_add(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004316}
4317#else
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004318static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4319{
4320 return NULL;
4321}
4322
4323void perf_bp_event(struct perf_event *bp, void *regs)
4324{
4325}
4326#endif
4327
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004328atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4329
4330static void sw_perf_event_destroy(struct perf_event *event)
4331{
4332 u64 event_id = event->attr.config;
4333
4334 WARN_ON(event->parent);
4335
4336 atomic_dec(&perf_swevent_enabled[event_id]);
4337}
4338
4339static const struct pmu *sw_perf_event_init(struct perf_event *event)
4340{
4341 const struct pmu *pmu = NULL;
4342 u64 event_id = event->attr.config;
4343
4344 /*
4345 * Software events (currently) can't in general distinguish
4346 * between user, kernel and hypervisor events.
4347 * However, context switches and cpu migrations are considered
4348 * to be kernel events, and page faults are never hypervisor
4349 * events.
4350 */
4351 switch (event_id) {
4352 case PERF_COUNT_SW_CPU_CLOCK:
4353 pmu = &perf_ops_cpu_clock;
4354
4355 break;
4356 case PERF_COUNT_SW_TASK_CLOCK:
4357 /*
4358 * If the user instantiates this as a per-cpu event,
4359 * use the cpu_clock event instead.
4360 */
4361 if (event->ctx->task)
4362 pmu = &perf_ops_task_clock;
4363 else
4364 pmu = &perf_ops_cpu_clock;
4365
4366 break;
4367 case PERF_COUNT_SW_PAGE_FAULTS:
4368 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4369 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4370 case PERF_COUNT_SW_CONTEXT_SWITCHES:
4371 case PERF_COUNT_SW_CPU_MIGRATIONS:
Anton Blanchardf7d79862009-10-18 01:09:29 +00004372 case PERF_COUNT_SW_ALIGNMENT_FAULTS:
4373 case PERF_COUNT_SW_EMULATION_FAULTS:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004374 if (!event->parent) {
4375 atomic_inc(&perf_swevent_enabled[event_id]);
4376 event->destroy = sw_perf_event_destroy;
4377 }
4378 pmu = &perf_ops_generic;
4379 break;
4380 }
4381
4382 return pmu;
4383}
4384
4385/*
4386 * Allocate and initialize a event structure
4387 */
4388static struct perf_event *
4389perf_event_alloc(struct perf_event_attr *attr,
4390 int cpu,
4391 struct perf_event_context *ctx,
4392 struct perf_event *group_leader,
4393 struct perf_event *parent_event,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004394 perf_overflow_handler_t overflow_handler,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004395 gfp_t gfpflags)
4396{
4397 const struct pmu *pmu;
4398 struct perf_event *event;
4399 struct hw_perf_event *hwc;
4400 long err;
4401
4402 event = kzalloc(sizeof(*event), gfpflags);
4403 if (!event)
4404 return ERR_PTR(-ENOMEM);
4405
4406 /*
4407 * Single events are their own group leaders, with an
4408 * empty sibling list:
4409 */
4410 if (!group_leader)
4411 group_leader = event;
4412
4413 mutex_init(&event->child_mutex);
4414 INIT_LIST_HEAD(&event->child_list);
4415
4416 INIT_LIST_HEAD(&event->group_entry);
4417 INIT_LIST_HEAD(&event->event_entry);
4418 INIT_LIST_HEAD(&event->sibling_list);
4419 init_waitqueue_head(&event->waitq);
4420
4421 mutex_init(&event->mmap_mutex);
4422
4423 event->cpu = cpu;
4424 event->attr = *attr;
4425 event->group_leader = group_leader;
4426 event->pmu = NULL;
4427 event->ctx = ctx;
4428 event->oncpu = -1;
4429
4430 event->parent = parent_event;
4431
4432 event->ns = get_pid_ns(current->nsproxy->pid_ns);
4433 event->id = atomic64_inc_return(&perf_event_id);
4434
4435 event->state = PERF_EVENT_STATE_INACTIVE;
4436
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004437 if (!overflow_handler && parent_event)
4438 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004439
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004440 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004441
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004442 if (attr->disabled)
4443 event->state = PERF_EVENT_STATE_OFF;
4444
4445 pmu = NULL;
4446
4447 hwc = &event->hw;
4448 hwc->sample_period = attr->sample_period;
4449 if (attr->freq && attr->sample_freq)
4450 hwc->sample_period = 1;
4451 hwc->last_period = hwc->sample_period;
4452
4453 atomic64_set(&hwc->period_left, hwc->sample_period);
4454
4455 /*
4456 * we currently do not support PERF_FORMAT_GROUP on inherited events
4457 */
4458 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
4459 goto done;
4460
4461 switch (attr->type) {
4462 case PERF_TYPE_RAW:
4463 case PERF_TYPE_HARDWARE:
4464 case PERF_TYPE_HW_CACHE:
4465 pmu = hw_perf_event_init(event);
4466 break;
4467
4468 case PERF_TYPE_SOFTWARE:
4469 pmu = sw_perf_event_init(event);
4470 break;
4471
4472 case PERF_TYPE_TRACEPOINT:
4473 pmu = tp_perf_event_init(event);
4474 break;
4475
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004476 case PERF_TYPE_BREAKPOINT:
4477 pmu = bp_perf_event_init(event);
4478 break;
4479
4480
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004481 default:
4482 break;
4483 }
4484done:
4485 err = 0;
4486 if (!pmu)
4487 err = -EINVAL;
4488 else if (IS_ERR(pmu))
4489 err = PTR_ERR(pmu);
4490
4491 if (err) {
4492 if (event->ns)
4493 put_pid_ns(event->ns);
4494 kfree(event);
4495 return ERR_PTR(err);
4496 }
4497
4498 event->pmu = pmu;
4499
4500 if (!event->parent) {
4501 atomic_inc(&nr_events);
4502 if (event->attr.mmap)
4503 atomic_inc(&nr_mmap_events);
4504 if (event->attr.comm)
4505 atomic_inc(&nr_comm_events);
4506 if (event->attr.task)
4507 atomic_inc(&nr_task_events);
4508 }
4509
4510 return event;
4511}
4512
4513static int perf_copy_attr(struct perf_event_attr __user *uattr,
4514 struct perf_event_attr *attr)
4515{
4516 u32 size;
4517 int ret;
4518
4519 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4520 return -EFAULT;
4521
4522 /*
4523 * zero the full structure, so that a short copy will be nice.
4524 */
4525 memset(attr, 0, sizeof(*attr));
4526
4527 ret = get_user(size, &uattr->size);
4528 if (ret)
4529 return ret;
4530
4531 if (size > PAGE_SIZE) /* silly large */
4532 goto err_size;
4533
4534 if (!size) /* abi compat */
4535 size = PERF_ATTR_SIZE_VER0;
4536
4537 if (size < PERF_ATTR_SIZE_VER0)
4538 goto err_size;
4539
4540 /*
4541 * If we're handed a bigger struct than we know of,
4542 * ensure all the unknown bits are 0 - i.e. new
4543 * user-space does not rely on any kernel feature
4544 * extensions we dont know about yet.
4545 */
4546 if (size > sizeof(*attr)) {
4547 unsigned char __user *addr;
4548 unsigned char __user *end;
4549 unsigned char val;
4550
4551 addr = (void __user *)uattr + sizeof(*attr);
4552 end = (void __user *)uattr + size;
4553
4554 for (; addr < end; addr++) {
4555 ret = get_user(val, addr);
4556 if (ret)
4557 return ret;
4558 if (val)
4559 goto err_size;
4560 }
4561 size = sizeof(*attr);
4562 }
4563
4564 ret = copy_from_user(attr, uattr, size);
4565 if (ret)
4566 return -EFAULT;
4567
4568 /*
4569 * If the type exists, the corresponding creation will verify
4570 * the attr->config.
4571 */
4572 if (attr->type >= PERF_TYPE_MAX)
4573 return -EINVAL;
4574
Peter Zijlstraf13c12c2009-12-15 19:43:11 +01004575 if (attr->__reserved_1 || attr->__reserved_2)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004576 return -EINVAL;
4577
4578 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4579 return -EINVAL;
4580
4581 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4582 return -EINVAL;
4583
4584out:
4585 return ret;
4586
4587err_size:
4588 put_user(sizeof(*attr), &uattr->size);
4589 ret = -E2BIG;
4590 goto out;
4591}
4592
Li Zefan6fb29152009-10-15 11:21:42 +08004593static int perf_event_set_output(struct perf_event *event, int output_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004594{
4595 struct perf_event *output_event = NULL;
4596 struct file *output_file = NULL;
4597 struct perf_event *old_output;
4598 int fput_needed = 0;
4599 int ret = -EINVAL;
4600
4601 if (!output_fd)
4602 goto set;
4603
4604 output_file = fget_light(output_fd, &fput_needed);
4605 if (!output_file)
4606 return -EBADF;
4607
4608 if (output_file->f_op != &perf_fops)
4609 goto out;
4610
4611 output_event = output_file->private_data;
4612
4613 /* Don't chain output fds */
4614 if (output_event->output)
4615 goto out;
4616
4617 /* Don't set an output fd when we already have an output channel */
4618 if (event->data)
4619 goto out;
4620
4621 atomic_long_inc(&output_file->f_count);
4622
4623set:
4624 mutex_lock(&event->mmap_mutex);
4625 old_output = event->output;
4626 rcu_assign_pointer(event->output, output_event);
4627 mutex_unlock(&event->mmap_mutex);
4628
4629 if (old_output) {
4630 /*
4631 * we need to make sure no existing perf_output_*()
4632 * is still referencing this event.
4633 */
4634 synchronize_rcu();
4635 fput(old_output->filp);
4636 }
4637
4638 ret = 0;
4639out:
4640 fput_light(output_file, fput_needed);
4641 return ret;
4642}
4643
4644/**
4645 * sys_perf_event_open - open a performance event, associate it to a task/cpu
4646 *
4647 * @attr_uptr: event_id type attributes for monitoring/sampling
4648 * @pid: target pid
4649 * @cpu: target cpu
4650 * @group_fd: group leader event fd
4651 */
4652SYSCALL_DEFINE5(perf_event_open,
4653 struct perf_event_attr __user *, attr_uptr,
4654 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
4655{
4656 struct perf_event *event, *group_leader;
4657 struct perf_event_attr attr;
4658 struct perf_event_context *ctx;
4659 struct file *event_file = NULL;
4660 struct file *group_file = NULL;
4661 int fput_needed = 0;
4662 int fput_needed2 = 0;
4663 int err;
4664
4665 /* for future expandability... */
4666 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
4667 return -EINVAL;
4668
4669 err = perf_copy_attr(attr_uptr, &attr);
4670 if (err)
4671 return err;
4672
4673 if (!attr.exclude_kernel) {
4674 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4675 return -EACCES;
4676 }
4677
4678 if (attr.freq) {
4679 if (attr.sample_freq > sysctl_perf_event_sample_rate)
4680 return -EINVAL;
4681 }
4682
4683 /*
4684 * Get the target context (task or percpu):
4685 */
4686 ctx = find_get_context(pid, cpu);
4687 if (IS_ERR(ctx))
4688 return PTR_ERR(ctx);
4689
4690 /*
4691 * Look up the group leader (we will attach this event to it):
4692 */
4693 group_leader = NULL;
4694 if (group_fd != -1 && !(flags & PERF_FLAG_FD_NO_GROUP)) {
4695 err = -EINVAL;
4696 group_file = fget_light(group_fd, &fput_needed);
4697 if (!group_file)
4698 goto err_put_context;
4699 if (group_file->f_op != &perf_fops)
4700 goto err_put_context;
4701
4702 group_leader = group_file->private_data;
4703 /*
4704 * Do not allow a recursive hierarchy (this new sibling
4705 * becoming part of another group-sibling):
4706 */
4707 if (group_leader->group_leader != group_leader)
4708 goto err_put_context;
4709 /*
4710 * Do not allow to attach to a group in a different
4711 * task or CPU context:
4712 */
4713 if (group_leader->ctx != ctx)
4714 goto err_put_context;
4715 /*
4716 * Only a group leader can be exclusive or pinned
4717 */
4718 if (attr.exclusive || attr.pinned)
4719 goto err_put_context;
4720 }
4721
4722 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004723 NULL, NULL, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004724 err = PTR_ERR(event);
4725 if (IS_ERR(event))
4726 goto err_put_context;
4727
4728 err = anon_inode_getfd("[perf_event]", &perf_fops, event, 0);
4729 if (err < 0)
4730 goto err_free_put_context;
4731
4732 event_file = fget_light(err, &fput_needed2);
4733 if (!event_file)
4734 goto err_free_put_context;
4735
4736 if (flags & PERF_FLAG_FD_OUTPUT) {
4737 err = perf_event_set_output(event, group_fd);
4738 if (err)
4739 goto err_fput_free_put_context;
4740 }
4741
4742 event->filp = event_file;
4743 WARN_ON_ONCE(ctx->parent_ctx);
4744 mutex_lock(&ctx->mutex);
4745 perf_install_in_context(ctx, event, cpu);
4746 ++ctx->generation;
4747 mutex_unlock(&ctx->mutex);
4748
4749 event->owner = current;
4750 get_task_struct(current);
4751 mutex_lock(&current->perf_event_mutex);
4752 list_add_tail(&event->owner_entry, &current->perf_event_list);
4753 mutex_unlock(&current->perf_event_mutex);
4754
4755err_fput_free_put_context:
4756 fput_light(event_file, fput_needed2);
4757
4758err_free_put_context:
4759 if (err < 0)
4760 kfree(event);
4761
4762err_put_context:
4763 if (err < 0)
4764 put_ctx(ctx);
4765
4766 fput_light(group_file, fput_needed);
4767
4768 return err;
4769}
4770
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004771/**
4772 * perf_event_create_kernel_counter
4773 *
4774 * @attr: attributes of the counter to create
4775 * @cpu: cpu in which the counter is bound
4776 * @pid: task to profile
4777 */
4778struct perf_event *
4779perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004780 pid_t pid,
4781 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004782{
4783 struct perf_event *event;
4784 struct perf_event_context *ctx;
4785 int err;
4786
4787 /*
4788 * Get the target context (task or percpu):
4789 */
4790
4791 ctx = find_get_context(pid, cpu);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01004792 if (IS_ERR(ctx)) {
4793 err = PTR_ERR(ctx);
4794 goto err_exit;
4795 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004796
4797 event = perf_event_alloc(attr, cpu, ctx, NULL,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004798 NULL, overflow_handler, GFP_KERNEL);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01004799 if (IS_ERR(event)) {
4800 err = PTR_ERR(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004801 goto err_put_context;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01004802 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004803
4804 event->filp = NULL;
4805 WARN_ON_ONCE(ctx->parent_ctx);
4806 mutex_lock(&ctx->mutex);
4807 perf_install_in_context(ctx, event, cpu);
4808 ++ctx->generation;
4809 mutex_unlock(&ctx->mutex);
4810
4811 event->owner = current;
4812 get_task_struct(current);
4813 mutex_lock(&current->perf_event_mutex);
4814 list_add_tail(&event->owner_entry, &current->perf_event_list);
4815 mutex_unlock(&current->perf_event_mutex);
4816
4817 return event;
4818
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01004819 err_put_context:
4820 put_ctx(ctx);
4821 err_exit:
4822 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004823}
4824EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
4825
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004826/*
4827 * inherit a event from parent task to child task:
4828 */
4829static struct perf_event *
4830inherit_event(struct perf_event *parent_event,
4831 struct task_struct *parent,
4832 struct perf_event_context *parent_ctx,
4833 struct task_struct *child,
4834 struct perf_event *group_leader,
4835 struct perf_event_context *child_ctx)
4836{
4837 struct perf_event *child_event;
4838
4839 /*
4840 * Instead of creating recursive hierarchies of events,
4841 * we link inherited events back to the original parent,
4842 * which has a filp for sure, which we use as the reference
4843 * count:
4844 */
4845 if (parent_event->parent)
4846 parent_event = parent_event->parent;
4847
4848 child_event = perf_event_alloc(&parent_event->attr,
4849 parent_event->cpu, child_ctx,
4850 group_leader, parent_event,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004851 NULL, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004852 if (IS_ERR(child_event))
4853 return child_event;
4854 get_ctx(child_ctx);
4855
4856 /*
4857 * Make the child state follow the state of the parent event,
4858 * not its attr.disabled bit. We hold the parent's mutex,
4859 * so we won't race with perf_event_{en, dis}able_family.
4860 */
4861 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
4862 child_event->state = PERF_EVENT_STATE_INACTIVE;
4863 else
4864 child_event->state = PERF_EVENT_STATE_OFF;
4865
4866 if (parent_event->attr.freq)
4867 child_event->hw.sample_period = parent_event->hw.sample_period;
4868
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004869 child_event->overflow_handler = parent_event->overflow_handler;
4870
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004871 /*
4872 * Link it up in the child's context:
4873 */
4874 add_event_to_ctx(child_event, child_ctx);
4875
4876 /*
4877 * Get a reference to the parent filp - we will fput it
4878 * when the child event exits. This is safe to do because
4879 * we are in the parent and we know that the filp still
4880 * exists and has a nonzero count:
4881 */
4882 atomic_long_inc(&parent_event->filp->f_count);
4883
4884 /*
4885 * Link this into the parent event's child list
4886 */
4887 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
4888 mutex_lock(&parent_event->child_mutex);
4889 list_add_tail(&child_event->child_list, &parent_event->child_list);
4890 mutex_unlock(&parent_event->child_mutex);
4891
4892 return child_event;
4893}
4894
4895static int inherit_group(struct perf_event *parent_event,
4896 struct task_struct *parent,
4897 struct perf_event_context *parent_ctx,
4898 struct task_struct *child,
4899 struct perf_event_context *child_ctx)
4900{
4901 struct perf_event *leader;
4902 struct perf_event *sub;
4903 struct perf_event *child_ctr;
4904
4905 leader = inherit_event(parent_event, parent, parent_ctx,
4906 child, NULL, child_ctx);
4907 if (IS_ERR(leader))
4908 return PTR_ERR(leader);
4909 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
4910 child_ctr = inherit_event(sub, parent, parent_ctx,
4911 child, leader, child_ctx);
4912 if (IS_ERR(child_ctr))
4913 return PTR_ERR(child_ctr);
4914 }
4915 return 0;
4916}
4917
4918static void sync_child_event(struct perf_event *child_event,
4919 struct task_struct *child)
4920{
4921 struct perf_event *parent_event = child_event->parent;
4922 u64 child_val;
4923
4924 if (child_event->attr.inherit_stat)
4925 perf_event_read_event(child_event, child);
4926
4927 child_val = atomic64_read(&child_event->count);
4928
4929 /*
4930 * Add back the child's count to the parent's count:
4931 */
4932 atomic64_add(child_val, &parent_event->count);
4933 atomic64_add(child_event->total_time_enabled,
4934 &parent_event->child_total_time_enabled);
4935 atomic64_add(child_event->total_time_running,
4936 &parent_event->child_total_time_running);
4937
4938 /*
4939 * Remove this event from the parent's list
4940 */
4941 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
4942 mutex_lock(&parent_event->child_mutex);
4943 list_del_init(&child_event->child_list);
4944 mutex_unlock(&parent_event->child_mutex);
4945
4946 /*
4947 * Release the parent event, if this was the last
4948 * reference to it.
4949 */
4950 fput(parent_event->filp);
4951}
4952
4953static void
4954__perf_event_exit_task(struct perf_event *child_event,
4955 struct perf_event_context *child_ctx,
4956 struct task_struct *child)
4957{
4958 struct perf_event *parent_event;
4959
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004960 perf_event_remove_from_context(child_event);
4961
4962 parent_event = child_event->parent;
4963 /*
4964 * It can happen that parent exits first, and has events
4965 * that are still around due to the child reference. These
4966 * events need to be zapped - but otherwise linger.
4967 */
4968 if (parent_event) {
4969 sync_child_event(child_event, child);
4970 free_event(child_event);
4971 }
4972}
4973
4974/*
4975 * When a child task exits, feed back event values to parent events.
4976 */
4977void perf_event_exit_task(struct task_struct *child)
4978{
4979 struct perf_event *child_event, *tmp;
4980 struct perf_event_context *child_ctx;
4981 unsigned long flags;
4982
4983 if (likely(!child->perf_event_ctxp)) {
4984 perf_event_task(child, NULL, 0);
4985 return;
4986 }
4987
4988 local_irq_save(flags);
4989 /*
4990 * We can't reschedule here because interrupts are disabled,
4991 * and either child is current or it is a task that can't be
4992 * scheduled, so we are now safe from rescheduling changing
4993 * our context.
4994 */
4995 child_ctx = child->perf_event_ctxp;
4996 __perf_event_task_sched_out(child_ctx);
4997
4998 /*
4999 * Take the context lock here so that if find_get_context is
5000 * reading child->perf_event_ctxp, we wait until it has
5001 * incremented the context's refcount before we do put_ctx below.
5002 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005003 raw_spin_lock(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005004 child->perf_event_ctxp = NULL;
5005 /*
5006 * If this context is a clone; unclone it so it can't get
5007 * swapped to another process while we're removing all
5008 * the events from it.
5009 */
5010 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01005011 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005012 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005013
5014 /*
5015 * Report the task dead after unscheduling the events so that we
5016 * won't get any samples after PERF_RECORD_EXIT. We can however still
5017 * get a few PERF_RECORD_READ events.
5018 */
5019 perf_event_task(child, child_ctx, 0);
5020
5021 /*
5022 * We can recurse on the same lock type through:
5023 *
5024 * __perf_event_exit_task()
5025 * sync_child_event()
5026 * fput(parent_event->filp)
5027 * perf_release()
5028 * mutex_lock(&ctx->mutex)
5029 *
5030 * But since its the parent context it won't be the same instance.
5031 */
5032 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
5033
5034again:
5035 list_for_each_entry_safe(child_event, tmp, &child_ctx->group_list,
5036 group_entry)
5037 __perf_event_exit_task(child_event, child_ctx, child);
5038
5039 /*
5040 * If the last event was a group event, it will have appended all
5041 * its siblings to the list, but we obtained 'tmp' before that which
5042 * will still point to the list head terminating the iteration.
5043 */
5044 if (!list_empty(&child_ctx->group_list))
5045 goto again;
5046
5047 mutex_unlock(&child_ctx->mutex);
5048
5049 put_ctx(child_ctx);
5050}
5051
5052/*
5053 * free an unexposed, unused context as created by inheritance by
5054 * init_task below, used by fork() in case of fail.
5055 */
5056void perf_event_free_task(struct task_struct *task)
5057{
5058 struct perf_event_context *ctx = task->perf_event_ctxp;
5059 struct perf_event *event, *tmp;
5060
5061 if (!ctx)
5062 return;
5063
5064 mutex_lock(&ctx->mutex);
5065again:
5066 list_for_each_entry_safe(event, tmp, &ctx->group_list, group_entry) {
5067 struct perf_event *parent = event->parent;
5068
5069 if (WARN_ON_ONCE(!parent))
5070 continue;
5071
5072 mutex_lock(&parent->child_mutex);
5073 list_del_init(&event->child_list);
5074 mutex_unlock(&parent->child_mutex);
5075
5076 fput(parent->filp);
5077
5078 list_del_event(event, ctx);
5079 free_event(event);
5080 }
5081
5082 if (!list_empty(&ctx->group_list))
5083 goto again;
5084
5085 mutex_unlock(&ctx->mutex);
5086
5087 put_ctx(ctx);
5088}
5089
5090/*
5091 * Initialize the perf_event context in task_struct
5092 */
5093int perf_event_init_task(struct task_struct *child)
5094{
Xiao Guangrongb93f7972009-12-09 11:29:44 +08005095 struct perf_event_context *child_ctx = NULL, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005096 struct perf_event_context *cloned_ctx;
5097 struct perf_event *event;
5098 struct task_struct *parent = current;
5099 int inherited_all = 1;
5100 int ret = 0;
5101
5102 child->perf_event_ctxp = NULL;
5103
5104 mutex_init(&child->perf_event_mutex);
5105 INIT_LIST_HEAD(&child->perf_event_list);
5106
5107 if (likely(!parent->perf_event_ctxp))
5108 return 0;
5109
5110 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005111 * If the parent's context is a clone, pin it so it won't get
5112 * swapped under us.
5113 */
5114 parent_ctx = perf_pin_task_context(parent);
5115
5116 /*
5117 * No need to check if parent_ctx != NULL here; since we saw
5118 * it non-NULL earlier, the only reason for it to become NULL
5119 * is if we exit, and since we're currently in the middle of
5120 * a fork we can't be exiting at the same time.
5121 */
5122
5123 /*
5124 * Lock the parent list. No need to lock the child - not PID
5125 * hashed yet and not running, so nobody can access it.
5126 */
5127 mutex_lock(&parent_ctx->mutex);
5128
5129 /*
5130 * We dont have to disable NMIs - we are only looking at
5131 * the list, not manipulating it:
5132 */
Xiao Guangrong27f99942009-09-25 13:54:01 +08005133 list_for_each_entry(event, &parent_ctx->group_list, group_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005134
5135 if (!event->attr.inherit) {
5136 inherited_all = 0;
5137 continue;
5138 }
5139
Xiao Guangrongb93f7972009-12-09 11:29:44 +08005140 if (!child->perf_event_ctxp) {
5141 /*
5142 * This is executed from the parent task context, so
5143 * inherit events that have been marked for cloning.
5144 * First allocate and initialize a context for the
5145 * child.
5146 */
5147
5148 child_ctx = kzalloc(sizeof(struct perf_event_context),
5149 GFP_KERNEL);
5150 if (!child_ctx) {
5151 ret = -ENOMEM;
5152 goto exit;
5153 }
5154
5155 __perf_event_init_context(child_ctx, child);
5156 child->perf_event_ctxp = child_ctx;
5157 get_task_struct(child);
5158 }
5159
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005160 ret = inherit_group(event, parent, parent_ctx,
5161 child, child_ctx);
5162 if (ret) {
5163 inherited_all = 0;
5164 break;
5165 }
5166 }
5167
5168 if (inherited_all) {
5169 /*
5170 * Mark the child context as a clone of the parent
5171 * context, or of whatever the parent is a clone of.
5172 * Note that if the parent is a clone, it could get
5173 * uncloned at any point, but that doesn't matter
5174 * because the list of events and the generation
5175 * count can't have changed since we took the mutex.
5176 */
5177 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5178 if (cloned_ctx) {
5179 child_ctx->parent_ctx = cloned_ctx;
5180 child_ctx->parent_gen = parent_ctx->parent_gen;
5181 } else {
5182 child_ctx->parent_ctx = parent_ctx;
5183 child_ctx->parent_gen = parent_ctx->generation;
5184 }
5185 get_ctx(child_ctx->parent_ctx);
5186 }
5187
Xiao Guangrongb93f7972009-12-09 11:29:44 +08005188exit:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005189 mutex_unlock(&parent_ctx->mutex);
5190
5191 perf_unpin_context(parent_ctx);
5192
5193 return ret;
5194}
5195
5196static void __cpuinit perf_event_init_cpu(int cpu)
5197{
5198 struct perf_cpu_context *cpuctx;
5199
5200 cpuctx = &per_cpu(perf_cpu_context, cpu);
5201 __perf_event_init_context(&cpuctx->ctx, NULL);
5202
5203 spin_lock(&perf_resource_lock);
5204 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5205 spin_unlock(&perf_resource_lock);
5206
5207 hw_perf_event_setup(cpu);
5208}
5209
5210#ifdef CONFIG_HOTPLUG_CPU
5211static void __perf_event_exit_cpu(void *info)
5212{
5213 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5214 struct perf_event_context *ctx = &cpuctx->ctx;
5215 struct perf_event *event, *tmp;
5216
5217 list_for_each_entry_safe(event, tmp, &ctx->group_list, group_entry)
5218 __perf_event_remove_from_context(event);
5219}
5220static void perf_event_exit_cpu(int cpu)
5221{
5222 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5223 struct perf_event_context *ctx = &cpuctx->ctx;
5224
5225 mutex_lock(&ctx->mutex);
5226 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5227 mutex_unlock(&ctx->mutex);
5228}
5229#else
5230static inline void perf_event_exit_cpu(int cpu) { }
5231#endif
5232
5233static int __cpuinit
5234perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5235{
5236 unsigned int cpu = (long)hcpu;
5237
5238 switch (action) {
5239
5240 case CPU_UP_PREPARE:
5241 case CPU_UP_PREPARE_FROZEN:
5242 perf_event_init_cpu(cpu);
5243 break;
5244
5245 case CPU_ONLINE:
5246 case CPU_ONLINE_FROZEN:
5247 hw_perf_event_setup_online(cpu);
5248 break;
5249
5250 case CPU_DOWN_PREPARE:
5251 case CPU_DOWN_PREPARE_FROZEN:
5252 perf_event_exit_cpu(cpu);
5253 break;
5254
5255 default:
5256 break;
5257 }
5258
5259 return NOTIFY_OK;
5260}
5261
5262/*
5263 * This has to have a higher priority than migration_notifier in sched.c.
5264 */
5265static struct notifier_block __cpuinitdata perf_cpu_nb = {
5266 .notifier_call = perf_cpu_notify,
5267 .priority = 20,
5268};
5269
5270void __init perf_event_init(void)
5271{
5272 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
5273 (void *)(long)smp_processor_id());
5274 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
5275 (void *)(long)smp_processor_id());
5276 register_cpu_notifier(&perf_cpu_nb);
5277}
5278
5279static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
5280{
5281 return sprintf(buf, "%d\n", perf_reserved_percpu);
5282}
5283
5284static ssize_t
5285perf_set_reserve_percpu(struct sysdev_class *class,
5286 const char *buf,
5287 size_t count)
5288{
5289 struct perf_cpu_context *cpuctx;
5290 unsigned long val;
5291 int err, cpu, mpt;
5292
5293 err = strict_strtoul(buf, 10, &val);
5294 if (err)
5295 return err;
5296 if (val > perf_max_events)
5297 return -EINVAL;
5298
5299 spin_lock(&perf_resource_lock);
5300 perf_reserved_percpu = val;
5301 for_each_online_cpu(cpu) {
5302 cpuctx = &per_cpu(perf_cpu_context, cpu);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005303 raw_spin_lock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005304 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
5305 perf_max_events - perf_reserved_percpu);
5306 cpuctx->max_pertask = mpt;
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005307 raw_spin_unlock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005308 }
5309 spin_unlock(&perf_resource_lock);
5310
5311 return count;
5312}
5313
5314static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
5315{
5316 return sprintf(buf, "%d\n", perf_overcommit);
5317}
5318
5319static ssize_t
5320perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
5321{
5322 unsigned long val;
5323 int err;
5324
5325 err = strict_strtoul(buf, 10, &val);
5326 if (err)
5327 return err;
5328 if (val > 1)
5329 return -EINVAL;
5330
5331 spin_lock(&perf_resource_lock);
5332 perf_overcommit = val;
5333 spin_unlock(&perf_resource_lock);
5334
5335 return count;
5336}
5337
5338static SYSDEV_CLASS_ATTR(
5339 reserve_percpu,
5340 0644,
5341 perf_show_reserve_percpu,
5342 perf_set_reserve_percpu
5343 );
5344
5345static SYSDEV_CLASS_ATTR(
5346 overcommit,
5347 0644,
5348 perf_show_overcommit,
5349 perf_set_overcommit
5350 );
5351
5352static struct attribute *perfclass_attrs[] = {
5353 &attr_reserve_percpu.attr,
5354 &attr_overcommit.attr,
5355 NULL
5356};
5357
5358static struct attribute_group perfclass_attr_group = {
5359 .attrs = perfclass_attrs,
5360 .name = "perf_events",
5361};
5362
5363static int __init perf_event_sysfs_init(void)
5364{
5365 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
5366 &perfclass_attr_group);
5367}
5368device_initcall(perf_event_sysfs_init);