blob: eae6ff69360477c8fb0f610fe016a017bf9203a8 [file] [log] [blame]
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001/*
Ingo Molnar57c0c152009-09-21 12:20:38 +02002 * Performance events core code:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003 *
4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
5 * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
6 * Copyright (C) 2008-2009 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
8 *
Ingo Molnar57c0c152009-09-21 12:20:38 +02009 * For licensing details see kernel-base/COPYING
Ingo Molnarcdd6c482009-09-21 12:02:48 +020010 */
11
12#include <linux/fs.h>
13#include <linux/mm.h>
14#include <linux/cpu.h>
15#include <linux/smp.h>
16#include <linux/file.h>
17#include <linux/poll.h>
18#include <linux/sysfs.h>
19#include <linux/dcache.h>
20#include <linux/percpu.h>
21#include <linux/ptrace.h>
22#include <linux/vmstat.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020023#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020024#include <linux/hardirq.h>
25#include <linux/rculist.h>
26#include <linux/uaccess.h>
27#include <linux/syscalls.h>
28#include <linux/anon_inodes.h>
29#include <linux/kernel_stat.h>
30#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080031#include <linux/ftrace_event.h>
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020032#include <linux/hw_breakpoint.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020033
34#include <asm/irq_regs.h>
35
36/*
37 * Each CPU has a list of per CPU events:
38 */
Xiao Guangrongaa5452d2009-12-09 11:28:13 +080039static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020040
41int perf_max_events __read_mostly = 1;
42static int perf_reserved_percpu __read_mostly;
43static int perf_overcommit __read_mostly = 1;
44
45static atomic_t nr_events __read_mostly;
46static atomic_t nr_mmap_events __read_mostly;
47static atomic_t nr_comm_events __read_mostly;
48static atomic_t nr_task_events __read_mostly;
49
50/*
51 * perf event paranoia level:
52 * -1 - not paranoid at all
53 * 0 - disallow raw tracepoint access for unpriv
54 * 1 - disallow cpu events for unpriv
55 * 2 - disallow kernel profiling for unpriv
56 */
57int sysctl_perf_event_paranoid __read_mostly = 1;
58
59static inline bool perf_paranoid_tracepoint_raw(void)
60{
61 return sysctl_perf_event_paranoid > -1;
62}
63
64static inline bool perf_paranoid_cpu(void)
65{
66 return sysctl_perf_event_paranoid > 0;
67}
68
69static inline bool perf_paranoid_kernel(void)
70{
71 return sysctl_perf_event_paranoid > 1;
72}
73
74int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
75
76/*
77 * max perf event sample rate
78 */
79int sysctl_perf_event_sample_rate __read_mostly = 100000;
80
81static atomic64_t perf_event_id;
82
83/*
84 * Lock for (sysadmin-configurable) event reservations:
85 */
86static DEFINE_SPINLOCK(perf_resource_lock);
87
88/*
89 * Architecture provided APIs - weak aliases:
90 */
91extern __weak const struct pmu *hw_perf_event_init(struct perf_event *event)
92{
93 return NULL;
94}
95
96void __weak hw_perf_disable(void) { barrier(); }
97void __weak hw_perf_enable(void) { barrier(); }
98
99void __weak hw_perf_event_setup(int cpu) { barrier(); }
100void __weak hw_perf_event_setup_online(int cpu) { barrier(); }
101
102int __weak
103hw_perf_group_sched_in(struct perf_event *group_leader,
104 struct perf_cpu_context *cpuctx,
105 struct perf_event_context *ctx, int cpu)
106{
107 return 0;
108}
109
110void __weak perf_event_print_debug(void) { }
111
112static DEFINE_PER_CPU(int, perf_disable_count);
113
114void __perf_disable(void)
115{
116 __get_cpu_var(perf_disable_count)++;
117}
118
119bool __perf_enable(void)
120{
121 return !--__get_cpu_var(perf_disable_count);
122}
123
124void perf_disable(void)
125{
126 __perf_disable();
127 hw_perf_disable();
128}
129
130void perf_enable(void)
131{
132 if (__perf_enable())
133 hw_perf_enable();
134}
135
136static void get_ctx(struct perf_event_context *ctx)
137{
138 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
139}
140
141static void free_ctx(struct rcu_head *head)
142{
143 struct perf_event_context *ctx;
144
145 ctx = container_of(head, struct perf_event_context, rcu_head);
146 kfree(ctx);
147}
148
149static void put_ctx(struct perf_event_context *ctx)
150{
151 if (atomic_dec_and_test(&ctx->refcount)) {
152 if (ctx->parent_ctx)
153 put_ctx(ctx->parent_ctx);
154 if (ctx->task)
155 put_task_struct(ctx->task);
156 call_rcu(&ctx->rcu_head, free_ctx);
157 }
158}
159
160static void unclone_ctx(struct perf_event_context *ctx)
161{
162 if (ctx->parent_ctx) {
163 put_ctx(ctx->parent_ctx);
164 ctx->parent_ctx = NULL;
165 }
166}
167
168/*
169 * If we inherit events we want to return the parent event id
170 * to userspace.
171 */
172static u64 primary_event_id(struct perf_event *event)
173{
174 u64 id = event->id;
175
176 if (event->parent)
177 id = event->parent->id;
178
179 return id;
180}
181
182/*
183 * Get the perf_event_context for a task and lock it.
184 * This has to cope with with the fact that until it is locked,
185 * the context could get moved to another task.
186 */
187static struct perf_event_context *
188perf_lock_task_context(struct task_struct *task, unsigned long *flags)
189{
190 struct perf_event_context *ctx;
191
192 rcu_read_lock();
193 retry:
194 ctx = rcu_dereference(task->perf_event_ctxp);
195 if (ctx) {
196 /*
197 * If this context is a clone of another, it might
198 * get swapped for another underneath us by
199 * perf_event_task_sched_out, though the
200 * rcu_read_lock() protects us from any context
201 * getting freed. Lock the context and check if it
202 * got swapped before we could get the lock, and retry
203 * if so. If we locked the right context, then it
204 * can't get swapped on us any more.
205 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100206 raw_spin_lock_irqsave(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200207 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100208 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200209 goto retry;
210 }
211
212 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100213 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200214 ctx = NULL;
215 }
216 }
217 rcu_read_unlock();
218 return ctx;
219}
220
221/*
222 * Get the context for a task and increment its pin_count so it
223 * can't get swapped to another task. This also increments its
224 * reference count so that the context can't get freed.
225 */
226static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
227{
228 struct perf_event_context *ctx;
229 unsigned long flags;
230
231 ctx = perf_lock_task_context(task, &flags);
232 if (ctx) {
233 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100234 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200235 }
236 return ctx;
237}
238
239static void perf_unpin_context(struct perf_event_context *ctx)
240{
241 unsigned long flags;
242
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100243 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200244 --ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100245 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200246 put_ctx(ctx);
247}
248
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100249static inline u64 perf_clock(void)
250{
251 return cpu_clock(smp_processor_id());
252}
253
254/*
255 * Update the record of the current time in a context.
256 */
257static void update_context_time(struct perf_event_context *ctx)
258{
259 u64 now = perf_clock();
260
261 ctx->time += now - ctx->timestamp;
262 ctx->timestamp = now;
263}
264
265/*
266 * Update the total_time_enabled and total_time_running fields for a event.
267 */
268static void update_event_times(struct perf_event *event)
269{
270 struct perf_event_context *ctx = event->ctx;
271 u64 run_end;
272
273 if (event->state < PERF_EVENT_STATE_INACTIVE ||
274 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
275 return;
276
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100277 if (ctx->is_active)
278 run_end = ctx->time;
279 else
280 run_end = event->tstamp_stopped;
281
282 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100283
284 if (event->state == PERF_EVENT_STATE_INACTIVE)
285 run_end = event->tstamp_stopped;
286 else
287 run_end = ctx->time;
288
289 event->total_time_running = run_end - event->tstamp_running;
290}
291
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100292static struct list_head *
293ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
294{
295 if (event->attr.pinned)
296 return &ctx->pinned_groups;
297 else
298 return &ctx->flexible_groups;
299}
300
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200301/*
302 * Add a event from the lists for its context.
303 * Must be called with ctx->mutex and ctx->lock held.
304 */
305static void
306list_add_event(struct perf_event *event, struct perf_event_context *ctx)
307{
308 struct perf_event *group_leader = event->group_leader;
309
310 /*
311 * Depending on whether it is a standalone or sibling event,
312 * add it straight to the context's event list, or to the group
313 * leader's sibling list:
314 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100315 if (group_leader == event) {
316 struct list_head *list;
317
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100318 if (is_software_event(event))
319 event->group_flags |= PERF_GROUP_SOFTWARE;
320
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100321 list = ctx_group_list(event, ctx);
322 list_add_tail(&event->group_entry, list);
323 } else {
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100324 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
325 !is_software_event(event))
326 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
327
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200328 list_add_tail(&event->group_entry, &group_leader->sibling_list);
329 group_leader->nr_siblings++;
330 }
331
332 list_add_rcu(&event->event_entry, &ctx->event_list);
333 ctx->nr_events++;
334 if (event->attr.inherit_stat)
335 ctx->nr_stat++;
336}
337
338/*
339 * Remove a event from the lists for its context.
340 * Must be called with ctx->mutex and ctx->lock held.
341 */
342static void
343list_del_event(struct perf_event *event, struct perf_event_context *ctx)
344{
345 struct perf_event *sibling, *tmp;
346
347 if (list_empty(&event->group_entry))
348 return;
349 ctx->nr_events--;
350 if (event->attr.inherit_stat)
351 ctx->nr_stat--;
352
353 list_del_init(&event->group_entry);
354 list_del_rcu(&event->event_entry);
355
356 if (event->group_leader != event)
357 event->group_leader->nr_siblings--;
358
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100359 update_event_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800360
361 /*
362 * If event was in error state, then keep it
363 * that way, otherwise bogus counts will be
364 * returned on read(). The only way to get out
365 * of error state is by explicit re-enabling
366 * of the event
367 */
368 if (event->state > PERF_EVENT_STATE_OFF)
369 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100370
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200371 /*
372 * If this was a group event with sibling events then
373 * upgrade the siblings to singleton events by adding them
374 * to the context list directly:
375 */
376 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100377 struct list_head *list;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200378
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100379 list = ctx_group_list(event, ctx);
380 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200381 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100382
383 /* Inherit group flags from the previous leader */
384 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200385 }
386}
387
388static void
389event_sched_out(struct perf_event *event,
390 struct perf_cpu_context *cpuctx,
391 struct perf_event_context *ctx)
392{
393 if (event->state != PERF_EVENT_STATE_ACTIVE)
394 return;
395
396 event->state = PERF_EVENT_STATE_INACTIVE;
397 if (event->pending_disable) {
398 event->pending_disable = 0;
399 event->state = PERF_EVENT_STATE_OFF;
400 }
401 event->tstamp_stopped = ctx->time;
402 event->pmu->disable(event);
403 event->oncpu = -1;
404
405 if (!is_software_event(event))
406 cpuctx->active_oncpu--;
407 ctx->nr_active--;
408 if (event->attr.exclusive || !cpuctx->active_oncpu)
409 cpuctx->exclusive = 0;
410}
411
412static void
413group_sched_out(struct perf_event *group_event,
414 struct perf_cpu_context *cpuctx,
415 struct perf_event_context *ctx)
416{
417 struct perf_event *event;
418
419 if (group_event->state != PERF_EVENT_STATE_ACTIVE)
420 return;
421
422 event_sched_out(group_event, cpuctx, ctx);
423
424 /*
425 * Schedule out siblings (if any):
426 */
427 list_for_each_entry(event, &group_event->sibling_list, group_entry)
428 event_sched_out(event, cpuctx, ctx);
429
430 if (group_event->attr.exclusive)
431 cpuctx->exclusive = 0;
432}
433
434/*
435 * Cross CPU call to remove a performance event
436 *
437 * We disable the event on the hardware level first. After that we
438 * remove it from the context list.
439 */
440static void __perf_event_remove_from_context(void *info)
441{
442 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
443 struct perf_event *event = info;
444 struct perf_event_context *ctx = event->ctx;
445
446 /*
447 * If this is a task context, we need to check whether it is
448 * the current task context of this cpu. If not it has been
449 * scheduled out before the smp call arrived.
450 */
451 if (ctx->task && cpuctx->task_ctx != ctx)
452 return;
453
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100454 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200455 /*
456 * Protect the list operation against NMI by disabling the
457 * events on a global level.
458 */
459 perf_disable();
460
461 event_sched_out(event, cpuctx, ctx);
462
463 list_del_event(event, ctx);
464
465 if (!ctx->task) {
466 /*
467 * Allow more per task events with respect to the
468 * reservation:
469 */
470 cpuctx->max_pertask =
471 min(perf_max_events - ctx->nr_events,
472 perf_max_events - perf_reserved_percpu);
473 }
474
475 perf_enable();
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100476 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200477}
478
479
480/*
481 * Remove the event from a task's (or a CPU's) list of events.
482 *
483 * Must be called with ctx->mutex held.
484 *
485 * CPU events are removed with a smp call. For task events we only
486 * call when the task is on a CPU.
487 *
488 * If event->ctx is a cloned context, callers must make sure that
489 * every task struct that event->ctx->task could possibly point to
490 * remains valid. This is OK when called from perf_release since
491 * that only calls us on the top-level context, which can't be a clone.
492 * When called from perf_event_exit_task, it's OK because the
493 * context has been detached from its task.
494 */
495static void perf_event_remove_from_context(struct perf_event *event)
496{
497 struct perf_event_context *ctx = event->ctx;
498 struct task_struct *task = ctx->task;
499
500 if (!task) {
501 /*
502 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200503 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200504 */
505 smp_call_function_single(event->cpu,
506 __perf_event_remove_from_context,
507 event, 1);
508 return;
509 }
510
511retry:
512 task_oncpu_function_call(task, __perf_event_remove_from_context,
513 event);
514
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100515 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200516 /*
517 * If the context is active we need to retry the smp call.
518 */
519 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100520 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200521 goto retry;
522 }
523
524 /*
525 * The lock prevents that this context is scheduled in so we
526 * can remove the event safely, if the call above did not
527 * succeed.
528 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100529 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200530 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100531 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200532}
533
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200534/*
535 * Update total_time_enabled and total_time_running for all events in a group.
536 */
537static void update_group_times(struct perf_event *leader)
538{
539 struct perf_event *event;
540
541 update_event_times(leader);
542 list_for_each_entry(event, &leader->sibling_list, group_entry)
543 update_event_times(event);
544}
545
546/*
547 * Cross CPU call to disable a performance event
548 */
549static void __perf_event_disable(void *info)
550{
551 struct perf_event *event = info;
552 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
553 struct perf_event_context *ctx = event->ctx;
554
555 /*
556 * If this is a per-task event, need to check whether this
557 * event's task is the current task on this cpu.
558 */
559 if (ctx->task && cpuctx->task_ctx != ctx)
560 return;
561
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100562 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200563
564 /*
565 * If the event is on, turn it off.
566 * If it is in error state, leave it in error state.
567 */
568 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
569 update_context_time(ctx);
570 update_group_times(event);
571 if (event == event->group_leader)
572 group_sched_out(event, cpuctx, ctx);
573 else
574 event_sched_out(event, cpuctx, ctx);
575 event->state = PERF_EVENT_STATE_OFF;
576 }
577
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100578 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200579}
580
581/*
582 * Disable a event.
583 *
584 * If event->ctx is a cloned context, callers must make sure that
585 * every task struct that event->ctx->task could possibly point to
586 * remains valid. This condition is satisifed when called through
587 * perf_event_for_each_child or perf_event_for_each because they
588 * hold the top-level event's child_mutex, so any descendant that
589 * goes to exit will block in sync_child_event.
590 * When called from perf_pending_event it's OK because event->ctx
591 * is the current context on this CPU and preemption is disabled,
592 * hence we can't get into perf_event_task_sched_out for this context.
593 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100594void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200595{
596 struct perf_event_context *ctx = event->ctx;
597 struct task_struct *task = ctx->task;
598
599 if (!task) {
600 /*
601 * Disable the event on the cpu that it's on
602 */
603 smp_call_function_single(event->cpu, __perf_event_disable,
604 event, 1);
605 return;
606 }
607
608 retry:
609 task_oncpu_function_call(task, __perf_event_disable, event);
610
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100611 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200612 /*
613 * If the event is still active, we need to retry the cross-call.
614 */
615 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100616 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200617 goto retry;
618 }
619
620 /*
621 * Since we have the lock this context can't be scheduled
622 * in, so we can change the state safely.
623 */
624 if (event->state == PERF_EVENT_STATE_INACTIVE) {
625 update_group_times(event);
626 event->state = PERF_EVENT_STATE_OFF;
627 }
628
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100629 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200630}
631
632static int
633event_sched_in(struct perf_event *event,
634 struct perf_cpu_context *cpuctx,
635 struct perf_event_context *ctx,
636 int cpu)
637{
638 if (event->state <= PERF_EVENT_STATE_OFF)
639 return 0;
640
641 event->state = PERF_EVENT_STATE_ACTIVE;
642 event->oncpu = cpu; /* TODO: put 'cpu' into cpuctx->cpu */
643 /*
644 * The new state must be visible before we turn it on in the hardware:
645 */
646 smp_wmb();
647
648 if (event->pmu->enable(event)) {
649 event->state = PERF_EVENT_STATE_INACTIVE;
650 event->oncpu = -1;
651 return -EAGAIN;
652 }
653
654 event->tstamp_running += ctx->time - event->tstamp_stopped;
655
656 if (!is_software_event(event))
657 cpuctx->active_oncpu++;
658 ctx->nr_active++;
659
660 if (event->attr.exclusive)
661 cpuctx->exclusive = 1;
662
663 return 0;
664}
665
666static int
667group_sched_in(struct perf_event *group_event,
668 struct perf_cpu_context *cpuctx,
669 struct perf_event_context *ctx,
670 int cpu)
671{
672 struct perf_event *event, *partial_group;
673 int ret;
674
675 if (group_event->state == PERF_EVENT_STATE_OFF)
676 return 0;
677
678 ret = hw_perf_group_sched_in(group_event, cpuctx, ctx, cpu);
679 if (ret)
680 return ret < 0 ? ret : 0;
681
682 if (event_sched_in(group_event, cpuctx, ctx, cpu))
683 return -EAGAIN;
684
685 /*
686 * Schedule in siblings as one group (if any):
687 */
688 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
689 if (event_sched_in(event, cpuctx, ctx, cpu)) {
690 partial_group = event;
691 goto group_error;
692 }
693 }
694
695 return 0;
696
697group_error:
698 /*
699 * Groups can be scheduled in as one unit only, so undo any
700 * partial group before returning:
701 */
702 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
703 if (event == partial_group)
704 break;
705 event_sched_out(event, cpuctx, ctx);
706 }
707 event_sched_out(group_event, cpuctx, ctx);
708
709 return -EAGAIN;
710}
711
712/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200713 * Work out whether we can put this event group on the CPU now.
714 */
715static int group_can_go_on(struct perf_event *event,
716 struct perf_cpu_context *cpuctx,
717 int can_add_hw)
718{
719 /*
720 * Groups consisting entirely of software events can always go on.
721 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100722 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200723 return 1;
724 /*
725 * If an exclusive group is already on, no other hardware
726 * events can go on.
727 */
728 if (cpuctx->exclusive)
729 return 0;
730 /*
731 * If this group is exclusive and there are already
732 * events on the CPU, it can't go on.
733 */
734 if (event->attr.exclusive && cpuctx->active_oncpu)
735 return 0;
736 /*
737 * Otherwise, try to add it if all previous groups were able
738 * to go on.
739 */
740 return can_add_hw;
741}
742
743static void add_event_to_ctx(struct perf_event *event,
744 struct perf_event_context *ctx)
745{
746 list_add_event(event, ctx);
747 event->tstamp_enabled = ctx->time;
748 event->tstamp_running = ctx->time;
749 event->tstamp_stopped = ctx->time;
750}
751
752/*
753 * Cross CPU call to install and enable a performance event
754 *
755 * Must be called with ctx->mutex held
756 */
757static void __perf_install_in_context(void *info)
758{
759 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
760 struct perf_event *event = info;
761 struct perf_event_context *ctx = event->ctx;
762 struct perf_event *leader = event->group_leader;
763 int cpu = smp_processor_id();
764 int err;
765
766 /*
767 * If this is a task context, we need to check whether it is
768 * the current task context of this cpu. If not it has been
769 * scheduled out before the smp call arrived.
770 * Or possibly this is the right context but it isn't
771 * on this cpu because it had no events.
772 */
773 if (ctx->task && cpuctx->task_ctx != ctx) {
774 if (cpuctx->task_ctx || ctx->task != current)
775 return;
776 cpuctx->task_ctx = ctx;
777 }
778
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100779 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200780 ctx->is_active = 1;
781 update_context_time(ctx);
782
783 /*
784 * Protect the list operation against NMI by disabling the
785 * events on a global level. NOP for non NMI based events.
786 */
787 perf_disable();
788
789 add_event_to_ctx(event, ctx);
790
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100791 if (event->cpu != -1 && event->cpu != smp_processor_id())
792 goto unlock;
793
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200794 /*
795 * Don't put the event on if it is disabled or if
796 * it is in a group and the group isn't on.
797 */
798 if (event->state != PERF_EVENT_STATE_INACTIVE ||
799 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
800 goto unlock;
801
802 /*
803 * An exclusive event can't go on if there are already active
804 * hardware events, and no hardware event can go on if there
805 * is already an exclusive event on.
806 */
807 if (!group_can_go_on(event, cpuctx, 1))
808 err = -EEXIST;
809 else
810 err = event_sched_in(event, cpuctx, ctx, cpu);
811
812 if (err) {
813 /*
814 * This event couldn't go on. If it is in a group
815 * then we have to pull the whole group off.
816 * If the event group is pinned then put it in error state.
817 */
818 if (leader != event)
819 group_sched_out(leader, cpuctx, ctx);
820 if (leader->attr.pinned) {
821 update_group_times(leader);
822 leader->state = PERF_EVENT_STATE_ERROR;
823 }
824 }
825
826 if (!err && !ctx->task && cpuctx->max_pertask)
827 cpuctx->max_pertask--;
828
829 unlock:
830 perf_enable();
831
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100832 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200833}
834
835/*
836 * Attach a performance event to a context
837 *
838 * First we add the event to the list with the hardware enable bit
839 * in event->hw_config cleared.
840 *
841 * If the event is attached to a task which is on a CPU we use a smp
842 * call to enable it in the task context. The task might have been
843 * scheduled away, but we check this in the smp call again.
844 *
845 * Must be called with ctx->mutex held.
846 */
847static void
848perf_install_in_context(struct perf_event_context *ctx,
849 struct perf_event *event,
850 int cpu)
851{
852 struct task_struct *task = ctx->task;
853
854 if (!task) {
855 /*
856 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200857 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200858 */
859 smp_call_function_single(cpu, __perf_install_in_context,
860 event, 1);
861 return;
862 }
863
864retry:
865 task_oncpu_function_call(task, __perf_install_in_context,
866 event);
867
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100868 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200869 /*
870 * we need to retry the smp call.
871 */
872 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100873 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200874 goto retry;
875 }
876
877 /*
878 * The lock prevents that this context is scheduled in so we
879 * can add the event safely, if it the call above did not
880 * succeed.
881 */
882 if (list_empty(&event->group_entry))
883 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100884 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200885}
886
887/*
888 * Put a event into inactive state and update time fields.
889 * Enabling the leader of a group effectively enables all
890 * the group members that aren't explicitly disabled, so we
891 * have to update their ->tstamp_enabled also.
892 * Note: this works for group members as well as group leaders
893 * since the non-leader members' sibling_lists will be empty.
894 */
895static void __perf_event_mark_enabled(struct perf_event *event,
896 struct perf_event_context *ctx)
897{
898 struct perf_event *sub;
899
900 event->state = PERF_EVENT_STATE_INACTIVE;
901 event->tstamp_enabled = ctx->time - event->total_time_enabled;
902 list_for_each_entry(sub, &event->sibling_list, group_entry)
903 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
904 sub->tstamp_enabled =
905 ctx->time - sub->total_time_enabled;
906}
907
908/*
909 * Cross CPU call to enable a performance event
910 */
911static void __perf_event_enable(void *info)
912{
913 struct perf_event *event = info;
914 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
915 struct perf_event_context *ctx = event->ctx;
916 struct perf_event *leader = event->group_leader;
917 int err;
918
919 /*
920 * If this is a per-task event, need to check whether this
921 * event's task is the current task on this cpu.
922 */
923 if (ctx->task && cpuctx->task_ctx != ctx) {
924 if (cpuctx->task_ctx || ctx->task != current)
925 return;
926 cpuctx->task_ctx = ctx;
927 }
928
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100929 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200930 ctx->is_active = 1;
931 update_context_time(ctx);
932
933 if (event->state >= PERF_EVENT_STATE_INACTIVE)
934 goto unlock;
935 __perf_event_mark_enabled(event, ctx);
936
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100937 if (event->cpu != -1 && event->cpu != smp_processor_id())
938 goto unlock;
939
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200940 /*
941 * If the event is in a group and isn't the group leader,
942 * then don't put it on unless the group is on.
943 */
944 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
945 goto unlock;
946
947 if (!group_can_go_on(event, cpuctx, 1)) {
948 err = -EEXIST;
949 } else {
950 perf_disable();
951 if (event == leader)
952 err = group_sched_in(event, cpuctx, ctx,
953 smp_processor_id());
954 else
955 err = event_sched_in(event, cpuctx, ctx,
956 smp_processor_id());
957 perf_enable();
958 }
959
960 if (err) {
961 /*
962 * If this event can't go on and it's part of a
963 * group, then the whole group has to come off.
964 */
965 if (leader != event)
966 group_sched_out(leader, cpuctx, ctx);
967 if (leader->attr.pinned) {
968 update_group_times(leader);
969 leader->state = PERF_EVENT_STATE_ERROR;
970 }
971 }
972
973 unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100974 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200975}
976
977/*
978 * Enable a event.
979 *
980 * If event->ctx is a cloned context, callers must make sure that
981 * every task struct that event->ctx->task could possibly point to
982 * remains valid. This condition is satisfied when called through
983 * perf_event_for_each_child or perf_event_for_each as described
984 * for perf_event_disable.
985 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100986void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200987{
988 struct perf_event_context *ctx = event->ctx;
989 struct task_struct *task = ctx->task;
990
991 if (!task) {
992 /*
993 * Enable the event on the cpu that it's on
994 */
995 smp_call_function_single(event->cpu, __perf_event_enable,
996 event, 1);
997 return;
998 }
999
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001000 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001001 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1002 goto out;
1003
1004 /*
1005 * If the event is in error state, clear that first.
1006 * That way, if we see the event in error state below, we
1007 * know that it has gone back into error state, as distinct
1008 * from the task having been scheduled away before the
1009 * cross-call arrived.
1010 */
1011 if (event->state == PERF_EVENT_STATE_ERROR)
1012 event->state = PERF_EVENT_STATE_OFF;
1013
1014 retry:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001015 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001016 task_oncpu_function_call(task, __perf_event_enable, event);
1017
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001018 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001019
1020 /*
1021 * If the context is active and the event is still off,
1022 * we need to retry the cross-call.
1023 */
1024 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1025 goto retry;
1026
1027 /*
1028 * Since we have the lock this context can't be scheduled
1029 * in, so we can change the state safely.
1030 */
1031 if (event->state == PERF_EVENT_STATE_OFF)
1032 __perf_event_mark_enabled(event, ctx);
1033
1034 out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001035 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001036}
1037
1038static int perf_event_refresh(struct perf_event *event, int refresh)
1039{
1040 /*
1041 * not supported on inherited events
1042 */
1043 if (event->attr.inherit)
1044 return -EINVAL;
1045
1046 atomic_add(refresh, &event->event_limit);
1047 perf_event_enable(event);
1048
1049 return 0;
1050}
1051
1052void __perf_event_sched_out(struct perf_event_context *ctx,
1053 struct perf_cpu_context *cpuctx)
1054{
1055 struct perf_event *event;
1056
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001057 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001058 ctx->is_active = 0;
1059 if (likely(!ctx->nr_events))
1060 goto out;
1061 update_context_time(ctx);
1062
1063 perf_disable();
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +01001064 if (ctx->nr_active) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001065 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1066 group_sched_out(event, cpuctx, ctx);
1067
1068 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001069 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +01001070 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001071 perf_enable();
1072 out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001073 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001074}
1075
1076/*
1077 * Test whether two contexts are equivalent, i.e. whether they
1078 * have both been cloned from the same version of the same context
1079 * and they both have the same number of enabled events.
1080 * If the number of enabled events is the same, then the set
1081 * of enabled events should be the same, because these are both
1082 * inherited contexts, therefore we can't access individual events
1083 * in them directly with an fd; we can only enable/disable all
1084 * events via prctl, or enable/disable all events in a family
1085 * via ioctl, which will have the same effect on both contexts.
1086 */
1087static int context_equiv(struct perf_event_context *ctx1,
1088 struct perf_event_context *ctx2)
1089{
1090 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1091 && ctx1->parent_gen == ctx2->parent_gen
1092 && !ctx1->pin_count && !ctx2->pin_count;
1093}
1094
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001095static void __perf_event_sync_stat(struct perf_event *event,
1096 struct perf_event *next_event)
1097{
1098 u64 value;
1099
1100 if (!event->attr.inherit_stat)
1101 return;
1102
1103 /*
1104 * Update the event value, we cannot use perf_event_read()
1105 * because we're in the middle of a context switch and have IRQs
1106 * disabled, which upsets smp_call_function_single(), however
1107 * we know the event must be on the current CPU, therefore we
1108 * don't need to use it.
1109 */
1110 switch (event->state) {
1111 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001112 event->pmu->read(event);
1113 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001114
1115 case PERF_EVENT_STATE_INACTIVE:
1116 update_event_times(event);
1117 break;
1118
1119 default:
1120 break;
1121 }
1122
1123 /*
1124 * In order to keep per-task stats reliable we need to flip the event
1125 * values when we flip the contexts.
1126 */
1127 value = atomic64_read(&next_event->count);
1128 value = atomic64_xchg(&event->count, value);
1129 atomic64_set(&next_event->count, value);
1130
1131 swap(event->total_time_enabled, next_event->total_time_enabled);
1132 swap(event->total_time_running, next_event->total_time_running);
1133
1134 /*
1135 * Since we swizzled the values, update the user visible data too.
1136 */
1137 perf_event_update_userpage(event);
1138 perf_event_update_userpage(next_event);
1139}
1140
1141#define list_next_entry(pos, member) \
1142 list_entry(pos->member.next, typeof(*pos), member)
1143
1144static void perf_event_sync_stat(struct perf_event_context *ctx,
1145 struct perf_event_context *next_ctx)
1146{
1147 struct perf_event *event, *next_event;
1148
1149 if (!ctx->nr_stat)
1150 return;
1151
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001152 update_context_time(ctx);
1153
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001154 event = list_first_entry(&ctx->event_list,
1155 struct perf_event, event_entry);
1156
1157 next_event = list_first_entry(&next_ctx->event_list,
1158 struct perf_event, event_entry);
1159
1160 while (&event->event_entry != &ctx->event_list &&
1161 &next_event->event_entry != &next_ctx->event_list) {
1162
1163 __perf_event_sync_stat(event, next_event);
1164
1165 event = list_next_entry(event, event_entry);
1166 next_event = list_next_entry(next_event, event_entry);
1167 }
1168}
1169
1170/*
1171 * Called from scheduler to remove the events of the current task,
1172 * with interrupts disabled.
1173 *
1174 * We stop each event and update the event value in event->count.
1175 *
1176 * This does not protect us against NMI, but disable()
1177 * sets the disabled bit in the control field of event _before_
1178 * accessing the event control register. If a NMI hits, then it will
1179 * not restart the event.
1180 */
1181void perf_event_task_sched_out(struct task_struct *task,
Peter Zijlstra49f47432009-12-27 11:51:52 +01001182 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001183{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001184 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001185 struct perf_event_context *ctx = task->perf_event_ctxp;
1186 struct perf_event_context *next_ctx;
1187 struct perf_event_context *parent;
1188 struct pt_regs *regs;
1189 int do_switch = 1;
1190
1191 regs = task_pt_regs(task);
1192 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
1193
1194 if (likely(!ctx || !cpuctx->task_ctx))
1195 return;
1196
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001197 rcu_read_lock();
1198 parent = rcu_dereference(ctx->parent_ctx);
1199 next_ctx = next->perf_event_ctxp;
1200 if (parent && next_ctx &&
1201 rcu_dereference(next_ctx->parent_ctx) == parent) {
1202 /*
1203 * Looks like the two contexts are clones, so we might be
1204 * able to optimize the context switch. We lock both
1205 * contexts and check that they are clones under the
1206 * lock (including re-checking that neither has been
1207 * uncloned in the meantime). It doesn't matter which
1208 * order we take the locks because no other cpu could
1209 * be trying to lock both of these tasks.
1210 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001211 raw_spin_lock(&ctx->lock);
1212 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001213 if (context_equiv(ctx, next_ctx)) {
1214 /*
1215 * XXX do we need a memory barrier of sorts
1216 * wrt to rcu_dereference() of perf_event_ctxp
1217 */
1218 task->perf_event_ctxp = next_ctx;
1219 next->perf_event_ctxp = ctx;
1220 ctx->task = next;
1221 next_ctx->task = task;
1222 do_switch = 0;
1223
1224 perf_event_sync_stat(ctx, next_ctx);
1225 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001226 raw_spin_unlock(&next_ctx->lock);
1227 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001228 }
1229 rcu_read_unlock();
1230
1231 if (do_switch) {
1232 __perf_event_sched_out(ctx, cpuctx);
1233 cpuctx->task_ctx = NULL;
1234 }
1235}
1236
1237/*
1238 * Called with IRQs disabled
1239 */
1240static void __perf_event_task_sched_out(struct perf_event_context *ctx)
1241{
1242 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1243
1244 if (!cpuctx->task_ctx)
1245 return;
1246
1247 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1248 return;
1249
1250 __perf_event_sched_out(ctx, cpuctx);
1251 cpuctx->task_ctx = NULL;
1252}
1253
1254/*
1255 * Called with IRQs disabled
1256 */
1257static void perf_event_cpu_sched_out(struct perf_cpu_context *cpuctx)
1258{
1259 __perf_event_sched_out(&cpuctx->ctx, cpuctx);
1260}
1261
1262static void
1263__perf_event_sched_in(struct perf_event_context *ctx,
Peter Zijlstra49f47432009-12-27 11:51:52 +01001264 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001265{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001266 int cpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001267 struct perf_event *event;
1268 int can_add_hw = 1;
1269
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001270 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001271 ctx->is_active = 1;
1272 if (likely(!ctx->nr_events))
1273 goto out;
1274
1275 ctx->timestamp = perf_clock();
1276
1277 perf_disable();
1278
1279 /*
1280 * First go through the list and put on any pinned groups
1281 * in order to give them the best chance of going on.
1282 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001283 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1284 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001285 continue;
1286 if (event->cpu != -1 && event->cpu != cpu)
1287 continue;
1288
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001289 if (group_can_go_on(event, cpuctx, 1))
1290 group_sched_in(event, cpuctx, ctx, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001291
1292 /*
1293 * If this pinned group hasn't been scheduled,
1294 * put it in error state.
1295 */
1296 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1297 update_group_times(event);
1298 event->state = PERF_EVENT_STATE_ERROR;
1299 }
1300 }
1301
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001302 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1303 /* Ignore events in OFF or ERROR state */
1304 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001305 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001306 /*
1307 * Listen to the 'cpu' scheduling filter constraint
1308 * of events:
1309 */
1310 if (event->cpu != -1 && event->cpu != cpu)
1311 continue;
1312
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001313 if (group_can_go_on(event, cpuctx, can_add_hw))
1314 if (group_sched_in(event, cpuctx, ctx, cpu))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001315 can_add_hw = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001316 }
1317 perf_enable();
1318 out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001319 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001320}
1321
1322/*
1323 * Called from scheduler to add the events of the current task
1324 * with interrupts disabled.
1325 *
1326 * We restore the event value and then enable it.
1327 *
1328 * This does not protect us against NMI, but enable()
1329 * sets the enabled bit in the control field of event _before_
1330 * accessing the event control register. If a NMI hits, then it will
1331 * keep the event running.
1332 */
Peter Zijlstra49f47432009-12-27 11:51:52 +01001333void perf_event_task_sched_in(struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001334{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001335 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001336 struct perf_event_context *ctx = task->perf_event_ctxp;
1337
1338 if (likely(!ctx))
1339 return;
1340 if (cpuctx->task_ctx == ctx)
1341 return;
Peter Zijlstra49f47432009-12-27 11:51:52 +01001342 __perf_event_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001343 cpuctx->task_ctx = ctx;
1344}
1345
Peter Zijlstra49f47432009-12-27 11:51:52 +01001346static void perf_event_cpu_sched_in(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001347{
1348 struct perf_event_context *ctx = &cpuctx->ctx;
1349
Peter Zijlstra49f47432009-12-27 11:51:52 +01001350 __perf_event_sched_in(ctx, cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001351}
1352
1353#define MAX_INTERRUPTS (~0ULL)
1354
1355static void perf_log_throttle(struct perf_event *event, int enable);
1356
1357static void perf_adjust_period(struct perf_event *event, u64 events)
1358{
1359 struct hw_perf_event *hwc = &event->hw;
1360 u64 period, sample_period;
1361 s64 delta;
1362
1363 events *= hwc->sample_period;
1364 period = div64_u64(events, event->attr.sample_freq);
1365
1366 delta = (s64)(period - hwc->sample_period);
1367 delta = (delta + 7) / 8; /* low pass filter */
1368
1369 sample_period = hwc->sample_period + delta;
1370
1371 if (!sample_period)
1372 sample_period = 1;
1373
1374 hwc->sample_period = sample_period;
1375}
1376
1377static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1378{
1379 struct perf_event *event;
1380 struct hw_perf_event *hwc;
1381 u64 interrupts, freq;
1382
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001383 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001384 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001385 if (event->state != PERF_EVENT_STATE_ACTIVE)
1386 continue;
1387
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001388 if (event->cpu != -1 && event->cpu != smp_processor_id())
1389 continue;
1390
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001391 hwc = &event->hw;
1392
1393 interrupts = hwc->interrupts;
1394 hwc->interrupts = 0;
1395
1396 /*
1397 * unthrottle events on the tick
1398 */
1399 if (interrupts == MAX_INTERRUPTS) {
1400 perf_log_throttle(event, 1);
1401 event->pmu->unthrottle(event);
1402 interrupts = 2*sysctl_perf_event_sample_rate/HZ;
1403 }
1404
1405 if (!event->attr.freq || !event->attr.sample_freq)
1406 continue;
1407
1408 /*
1409 * if the specified freq < HZ then we need to skip ticks
1410 */
1411 if (event->attr.sample_freq < HZ) {
1412 freq = event->attr.sample_freq;
1413
1414 hwc->freq_count += freq;
1415 hwc->freq_interrupts += interrupts;
1416
1417 if (hwc->freq_count < HZ)
1418 continue;
1419
1420 interrupts = hwc->freq_interrupts;
1421 hwc->freq_interrupts = 0;
1422 hwc->freq_count -= HZ;
1423 } else
1424 freq = HZ;
1425
1426 perf_adjust_period(event, freq * interrupts);
1427
1428 /*
1429 * In order to avoid being stalled by an (accidental) huge
1430 * sample period, force reset the sample period if we didn't
1431 * get any events in this freq period.
1432 */
1433 if (!interrupts) {
1434 perf_disable();
1435 event->pmu->disable(event);
1436 atomic64_set(&hwc->period_left, 0);
1437 event->pmu->enable(event);
1438 perf_enable();
1439 }
1440 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001441 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001442}
1443
1444/*
1445 * Round-robin a context's events:
1446 */
1447static void rotate_ctx(struct perf_event_context *ctx)
1448{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001449 if (!ctx->nr_events)
1450 return;
1451
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001452 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001453
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001454 /* Rotate the first entry last of non-pinned groups */
1455 perf_disable();
1456
1457 list_rotate_left(&ctx->flexible_groups);
1458
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001459 perf_enable();
1460
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001461 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001462}
1463
Peter Zijlstra49f47432009-12-27 11:51:52 +01001464void perf_event_task_tick(struct task_struct *curr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001465{
1466 struct perf_cpu_context *cpuctx;
1467 struct perf_event_context *ctx;
1468
1469 if (!atomic_read(&nr_events))
1470 return;
1471
Peter Zijlstra49f47432009-12-27 11:51:52 +01001472 cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001473 ctx = curr->perf_event_ctxp;
1474
1475 perf_ctx_adjust_freq(&cpuctx->ctx);
1476 if (ctx)
1477 perf_ctx_adjust_freq(ctx);
1478
1479 perf_event_cpu_sched_out(cpuctx);
1480 if (ctx)
1481 __perf_event_task_sched_out(ctx);
1482
1483 rotate_ctx(&cpuctx->ctx);
1484 if (ctx)
1485 rotate_ctx(ctx);
1486
Peter Zijlstra49f47432009-12-27 11:51:52 +01001487 perf_event_cpu_sched_in(cpuctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001488 if (ctx)
Peter Zijlstra49f47432009-12-27 11:51:52 +01001489 perf_event_task_sched_in(curr);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001490}
1491
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001492static int event_enable_on_exec(struct perf_event *event,
1493 struct perf_event_context *ctx)
1494{
1495 if (!event->attr.enable_on_exec)
1496 return 0;
1497
1498 event->attr.enable_on_exec = 0;
1499 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1500 return 0;
1501
1502 __perf_event_mark_enabled(event, ctx);
1503
1504 return 1;
1505}
1506
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001507/*
1508 * Enable all of a task's events that have been marked enable-on-exec.
1509 * This expects task == current.
1510 */
1511static void perf_event_enable_on_exec(struct task_struct *task)
1512{
1513 struct perf_event_context *ctx;
1514 struct perf_event *event;
1515 unsigned long flags;
1516 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001517 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001518
1519 local_irq_save(flags);
1520 ctx = task->perf_event_ctxp;
1521 if (!ctx || !ctx->nr_events)
1522 goto out;
1523
1524 __perf_event_task_sched_out(ctx);
1525
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001526 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001527
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001528 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1529 ret = event_enable_on_exec(event, ctx);
1530 if (ret)
1531 enabled = 1;
1532 }
1533
1534 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1535 ret = event_enable_on_exec(event, ctx);
1536 if (ret)
1537 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001538 }
1539
1540 /*
1541 * Unclone this context if we enabled any event.
1542 */
1543 if (enabled)
1544 unclone_ctx(ctx);
1545
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001546 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001547
Peter Zijlstra49f47432009-12-27 11:51:52 +01001548 perf_event_task_sched_in(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001549 out:
1550 local_irq_restore(flags);
1551}
1552
1553/*
1554 * Cross CPU call to read the hardware event
1555 */
1556static void __perf_event_read(void *info)
1557{
1558 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1559 struct perf_event *event = info;
1560 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001561
1562 /*
1563 * If this is a task context, we need to check whether it is
1564 * the current task context of this cpu. If not it has been
1565 * scheduled out before the smp call arrived. In that case
1566 * event->count would have been updated to a recent sample
1567 * when the event was scheduled out.
1568 */
1569 if (ctx->task && cpuctx->task_ctx != ctx)
1570 return;
1571
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001572 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001573 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001574 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001575 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001576
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001577 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001578}
1579
1580static u64 perf_event_read(struct perf_event *event)
1581{
1582 /*
1583 * If event is enabled and currently active on a CPU, update the
1584 * value in the event structure:
1585 */
1586 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1587 smp_call_function_single(event->oncpu,
1588 __perf_event_read, event, 1);
1589 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001590 struct perf_event_context *ctx = event->ctx;
1591 unsigned long flags;
1592
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001593 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001594 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001595 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001596 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001597 }
1598
1599 return atomic64_read(&event->count);
1600}
1601
1602/*
1603 * Initialize the perf_event context in a task_struct:
1604 */
1605static void
1606__perf_event_init_context(struct perf_event_context *ctx,
1607 struct task_struct *task)
1608{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001609 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001610 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001611 INIT_LIST_HEAD(&ctx->pinned_groups);
1612 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001613 INIT_LIST_HEAD(&ctx->event_list);
1614 atomic_set(&ctx->refcount, 1);
1615 ctx->task = task;
1616}
1617
1618static struct perf_event_context *find_get_context(pid_t pid, int cpu)
1619{
1620 struct perf_event_context *ctx;
1621 struct perf_cpu_context *cpuctx;
1622 struct task_struct *task;
1623 unsigned long flags;
1624 int err;
1625
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001626 if (pid == -1 && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001627 /* Must be root to operate on a CPU event: */
1628 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
1629 return ERR_PTR(-EACCES);
1630
Paul Mackerras0f624e72009-12-15 19:40:32 +11001631 if (cpu < 0 || cpu >= nr_cpumask_bits)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001632 return ERR_PTR(-EINVAL);
1633
1634 /*
1635 * We could be clever and allow to attach a event to an
1636 * offline CPU and activate it when the CPU comes up, but
1637 * that's for later.
1638 */
Rusty Russellf6325e32009-12-17 11:43:08 -06001639 if (!cpu_online(cpu))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001640 return ERR_PTR(-ENODEV);
1641
1642 cpuctx = &per_cpu(perf_cpu_context, cpu);
1643 ctx = &cpuctx->ctx;
1644 get_ctx(ctx);
1645
1646 return ctx;
1647 }
1648
1649 rcu_read_lock();
1650 if (!pid)
1651 task = current;
1652 else
1653 task = find_task_by_vpid(pid);
1654 if (task)
1655 get_task_struct(task);
1656 rcu_read_unlock();
1657
1658 if (!task)
1659 return ERR_PTR(-ESRCH);
1660
1661 /*
1662 * Can't attach events to a dying task.
1663 */
1664 err = -ESRCH;
1665 if (task->flags & PF_EXITING)
1666 goto errout;
1667
1668 /* Reuse ptrace permission checks for now. */
1669 err = -EACCES;
1670 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1671 goto errout;
1672
1673 retry:
1674 ctx = perf_lock_task_context(task, &flags);
1675 if (ctx) {
1676 unclone_ctx(ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001677 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001678 }
1679
1680 if (!ctx) {
Xiao Guangrongaa5452d2009-12-09 11:28:13 +08001681 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001682 err = -ENOMEM;
1683 if (!ctx)
1684 goto errout;
1685 __perf_event_init_context(ctx, task);
1686 get_ctx(ctx);
1687 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
1688 /*
1689 * We raced with some other task; use
1690 * the context they set.
1691 */
1692 kfree(ctx);
1693 goto retry;
1694 }
1695 get_task_struct(task);
1696 }
1697
1698 put_task_struct(task);
1699 return ctx;
1700
1701 errout:
1702 put_task_struct(task);
1703 return ERR_PTR(err);
1704}
1705
Li Zefan6fb29152009-10-15 11:21:42 +08001706static void perf_event_free_filter(struct perf_event *event);
1707
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001708static void free_event_rcu(struct rcu_head *head)
1709{
1710 struct perf_event *event;
1711
1712 event = container_of(head, struct perf_event, rcu_head);
1713 if (event->ns)
1714 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08001715 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001716 kfree(event);
1717}
1718
1719static void perf_pending_sync(struct perf_event *event);
1720
1721static void free_event(struct perf_event *event)
1722{
1723 perf_pending_sync(event);
1724
1725 if (!event->parent) {
1726 atomic_dec(&nr_events);
1727 if (event->attr.mmap)
1728 atomic_dec(&nr_mmap_events);
1729 if (event->attr.comm)
1730 atomic_dec(&nr_comm_events);
1731 if (event->attr.task)
1732 atomic_dec(&nr_task_events);
1733 }
1734
1735 if (event->output) {
1736 fput(event->output->filp);
1737 event->output = NULL;
1738 }
1739
1740 if (event->destroy)
1741 event->destroy(event);
1742
1743 put_ctx(event->ctx);
1744 call_rcu(&event->rcu_head, free_event_rcu);
1745}
1746
Arjan van de Venfb0459d2009-09-25 12:25:56 +02001747int perf_event_release_kernel(struct perf_event *event)
1748{
1749 struct perf_event_context *ctx = event->ctx;
1750
1751 WARN_ON_ONCE(ctx->parent_ctx);
1752 mutex_lock(&ctx->mutex);
1753 perf_event_remove_from_context(event);
1754 mutex_unlock(&ctx->mutex);
1755
1756 mutex_lock(&event->owner->perf_event_mutex);
1757 list_del_init(&event->owner_entry);
1758 mutex_unlock(&event->owner->perf_event_mutex);
1759 put_task_struct(event->owner);
1760
1761 free_event(event);
1762
1763 return 0;
1764}
1765EXPORT_SYMBOL_GPL(perf_event_release_kernel);
1766
Peter Zijlstraa66a3052009-11-23 11:37:23 +01001767/*
1768 * Called when the last reference to the file is gone.
1769 */
1770static int perf_release(struct inode *inode, struct file *file)
1771{
1772 struct perf_event *event = file->private_data;
1773
1774 file->private_data = NULL;
1775
1776 return perf_event_release_kernel(event);
1777}
1778
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001779static int perf_event_read_size(struct perf_event *event)
1780{
1781 int entry = sizeof(u64); /* value */
1782 int size = 0;
1783 int nr = 1;
1784
1785 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1786 size += sizeof(u64);
1787
1788 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1789 size += sizeof(u64);
1790
1791 if (event->attr.read_format & PERF_FORMAT_ID)
1792 entry += sizeof(u64);
1793
1794 if (event->attr.read_format & PERF_FORMAT_GROUP) {
1795 nr += event->group_leader->nr_siblings;
1796 size += sizeof(u64);
1797 }
1798
1799 size += entry * nr;
1800
1801 return size;
1802}
1803
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001804u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001805{
1806 struct perf_event *child;
1807 u64 total = 0;
1808
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001809 *enabled = 0;
1810 *running = 0;
1811
Peter Zijlstra6f105812009-11-20 22:19:56 +01001812 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001813 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001814 *enabled += event->total_time_enabled +
1815 atomic64_read(&event->child_total_time_enabled);
1816 *running += event->total_time_running +
1817 atomic64_read(&event->child_total_time_running);
1818
1819 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001820 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001821 *enabled += child->total_time_enabled;
1822 *running += child->total_time_running;
1823 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01001824 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001825
1826 return total;
1827}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02001828EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001829
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001830static int perf_event_read_group(struct perf_event *event,
1831 u64 read_format, char __user *buf)
1832{
1833 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01001834 int n = 0, size = 0, ret = -EFAULT;
1835 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01001836 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001837 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01001838
Peter Zijlstra6f105812009-11-20 22:19:56 +01001839 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001840 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001841
1842 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001843 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1844 values[n++] = enabled;
1845 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1846 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01001847 values[n++] = count;
1848 if (read_format & PERF_FORMAT_ID)
1849 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001850
1851 size = n * sizeof(u64);
1852
1853 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01001854 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001855
Peter Zijlstra6f105812009-11-20 22:19:56 +01001856 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001857
1858 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01001859 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001860
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001861 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01001862 if (read_format & PERF_FORMAT_ID)
1863 values[n++] = primary_event_id(sub);
1864
1865 size = n * sizeof(u64);
1866
Stephane Eranian184d3da2009-11-23 21:40:49 -08001867 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01001868 ret = -EFAULT;
1869 goto unlock;
1870 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01001871
1872 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001873 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01001874unlock:
1875 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001876
Peter Zijlstraabf48682009-11-20 22:19:49 +01001877 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001878}
1879
1880static int perf_event_read_one(struct perf_event *event,
1881 u64 read_format, char __user *buf)
1882{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001883 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001884 u64 values[4];
1885 int n = 0;
1886
Peter Zijlstra59ed4462009-11-20 22:19:55 +01001887 values[n++] = perf_event_read_value(event, &enabled, &running);
1888 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1889 values[n++] = enabled;
1890 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1891 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001892 if (read_format & PERF_FORMAT_ID)
1893 values[n++] = primary_event_id(event);
1894
1895 if (copy_to_user(buf, values, n * sizeof(u64)))
1896 return -EFAULT;
1897
1898 return n * sizeof(u64);
1899}
1900
1901/*
1902 * Read the performance event - simple non blocking version for now
1903 */
1904static ssize_t
1905perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
1906{
1907 u64 read_format = event->attr.read_format;
1908 int ret;
1909
1910 /*
1911 * Return end-of-file for a read on a event that is in
1912 * error state (i.e. because it was pinned but it couldn't be
1913 * scheduled on to the CPU at some point).
1914 */
1915 if (event->state == PERF_EVENT_STATE_ERROR)
1916 return 0;
1917
1918 if (count < perf_event_read_size(event))
1919 return -ENOSPC;
1920
1921 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001922 if (read_format & PERF_FORMAT_GROUP)
1923 ret = perf_event_read_group(event, read_format, buf);
1924 else
1925 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001926
1927 return ret;
1928}
1929
1930static ssize_t
1931perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
1932{
1933 struct perf_event *event = file->private_data;
1934
1935 return perf_read_hw(event, buf, count);
1936}
1937
1938static unsigned int perf_poll(struct file *file, poll_table *wait)
1939{
1940 struct perf_event *event = file->private_data;
1941 struct perf_mmap_data *data;
1942 unsigned int events = POLL_HUP;
1943
1944 rcu_read_lock();
1945 data = rcu_dereference(event->data);
1946 if (data)
1947 events = atomic_xchg(&data->poll, 0);
1948 rcu_read_unlock();
1949
1950 poll_wait(file, &event->waitq, wait);
1951
1952 return events;
1953}
1954
1955static void perf_event_reset(struct perf_event *event)
1956{
1957 (void)perf_event_read(event);
1958 atomic64_set(&event->count, 0);
1959 perf_event_update_userpage(event);
1960}
1961
1962/*
1963 * Holding the top-level event's child_mutex means that any
1964 * descendant process that has inherited this event will block
1965 * in sync_child_event if it goes to exit, thus satisfying the
1966 * task existence requirements of perf_event_enable/disable.
1967 */
1968static void perf_event_for_each_child(struct perf_event *event,
1969 void (*func)(struct perf_event *))
1970{
1971 struct perf_event *child;
1972
1973 WARN_ON_ONCE(event->ctx->parent_ctx);
1974 mutex_lock(&event->child_mutex);
1975 func(event);
1976 list_for_each_entry(child, &event->child_list, child_list)
1977 func(child);
1978 mutex_unlock(&event->child_mutex);
1979}
1980
1981static void perf_event_for_each(struct perf_event *event,
1982 void (*func)(struct perf_event *))
1983{
1984 struct perf_event_context *ctx = event->ctx;
1985 struct perf_event *sibling;
1986
1987 WARN_ON_ONCE(ctx->parent_ctx);
1988 mutex_lock(&ctx->mutex);
1989 event = event->group_leader;
1990
1991 perf_event_for_each_child(event, func);
1992 func(event);
1993 list_for_each_entry(sibling, &event->sibling_list, group_entry)
1994 perf_event_for_each_child(event, func);
1995 mutex_unlock(&ctx->mutex);
1996}
1997
1998static int perf_event_period(struct perf_event *event, u64 __user *arg)
1999{
2000 struct perf_event_context *ctx = event->ctx;
2001 unsigned long size;
2002 int ret = 0;
2003 u64 value;
2004
2005 if (!event->attr.sample_period)
2006 return -EINVAL;
2007
2008 size = copy_from_user(&value, arg, sizeof(value));
2009 if (size != sizeof(value))
2010 return -EFAULT;
2011
2012 if (!value)
2013 return -EINVAL;
2014
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002015 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002016 if (event->attr.freq) {
2017 if (value > sysctl_perf_event_sample_rate) {
2018 ret = -EINVAL;
2019 goto unlock;
2020 }
2021
2022 event->attr.sample_freq = value;
2023 } else {
2024 event->attr.sample_period = value;
2025 event->hw.sample_period = value;
2026 }
2027unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002028 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002029
2030 return ret;
2031}
2032
Li Zefan6fb29152009-10-15 11:21:42 +08002033static int perf_event_set_output(struct perf_event *event, int output_fd);
2034static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002035
2036static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2037{
2038 struct perf_event *event = file->private_data;
2039 void (*func)(struct perf_event *);
2040 u32 flags = arg;
2041
2042 switch (cmd) {
2043 case PERF_EVENT_IOC_ENABLE:
2044 func = perf_event_enable;
2045 break;
2046 case PERF_EVENT_IOC_DISABLE:
2047 func = perf_event_disable;
2048 break;
2049 case PERF_EVENT_IOC_RESET:
2050 func = perf_event_reset;
2051 break;
2052
2053 case PERF_EVENT_IOC_REFRESH:
2054 return perf_event_refresh(event, arg);
2055
2056 case PERF_EVENT_IOC_PERIOD:
2057 return perf_event_period(event, (u64 __user *)arg);
2058
2059 case PERF_EVENT_IOC_SET_OUTPUT:
2060 return perf_event_set_output(event, arg);
2061
Li Zefan6fb29152009-10-15 11:21:42 +08002062 case PERF_EVENT_IOC_SET_FILTER:
2063 return perf_event_set_filter(event, (void __user *)arg);
2064
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002065 default:
2066 return -ENOTTY;
2067 }
2068
2069 if (flags & PERF_IOC_FLAG_GROUP)
2070 perf_event_for_each(event, func);
2071 else
2072 perf_event_for_each_child(event, func);
2073
2074 return 0;
2075}
2076
2077int perf_event_task_enable(void)
2078{
2079 struct perf_event *event;
2080
2081 mutex_lock(&current->perf_event_mutex);
2082 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2083 perf_event_for_each_child(event, perf_event_enable);
2084 mutex_unlock(&current->perf_event_mutex);
2085
2086 return 0;
2087}
2088
2089int perf_event_task_disable(void)
2090{
2091 struct perf_event *event;
2092
2093 mutex_lock(&current->perf_event_mutex);
2094 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2095 perf_event_for_each_child(event, perf_event_disable);
2096 mutex_unlock(&current->perf_event_mutex);
2097
2098 return 0;
2099}
2100
2101#ifndef PERF_EVENT_INDEX_OFFSET
2102# define PERF_EVENT_INDEX_OFFSET 0
2103#endif
2104
2105static int perf_event_index(struct perf_event *event)
2106{
2107 if (event->state != PERF_EVENT_STATE_ACTIVE)
2108 return 0;
2109
2110 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2111}
2112
2113/*
2114 * Callers need to ensure there can be no nesting of this function, otherwise
2115 * the seqlock logic goes bad. We can not serialize this because the arch
2116 * code calls this from NMI context.
2117 */
2118void perf_event_update_userpage(struct perf_event *event)
2119{
2120 struct perf_event_mmap_page *userpg;
2121 struct perf_mmap_data *data;
2122
2123 rcu_read_lock();
2124 data = rcu_dereference(event->data);
2125 if (!data)
2126 goto unlock;
2127
2128 userpg = data->user_page;
2129
2130 /*
2131 * Disable preemption so as to not let the corresponding user-space
2132 * spin too long if we get preempted.
2133 */
2134 preempt_disable();
2135 ++userpg->lock;
2136 barrier();
2137 userpg->index = perf_event_index(event);
2138 userpg->offset = atomic64_read(&event->count);
2139 if (event->state == PERF_EVENT_STATE_ACTIVE)
2140 userpg->offset -= atomic64_read(&event->hw.prev_count);
2141
2142 userpg->time_enabled = event->total_time_enabled +
2143 atomic64_read(&event->child_total_time_enabled);
2144
2145 userpg->time_running = event->total_time_running +
2146 atomic64_read(&event->child_total_time_running);
2147
2148 barrier();
2149 ++userpg->lock;
2150 preempt_enable();
2151unlock:
2152 rcu_read_unlock();
2153}
2154
Peter Zijlstra906010b2009-09-21 16:08:49 +02002155static unsigned long perf_data_size(struct perf_mmap_data *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002156{
Peter Zijlstra906010b2009-09-21 16:08:49 +02002157 return data->nr_pages << (PAGE_SHIFT + data->data_order);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002158}
2159
Peter Zijlstra906010b2009-09-21 16:08:49 +02002160#ifndef CONFIG_PERF_USE_VMALLOC
2161
2162/*
2163 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2164 */
2165
2166static struct page *
2167perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2168{
2169 if (pgoff > data->nr_pages)
2170 return NULL;
2171
2172 if (pgoff == 0)
2173 return virt_to_page(data->user_page);
2174
2175 return virt_to_page(data->data_pages[pgoff - 1]);
2176}
2177
2178static struct perf_mmap_data *
2179perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002180{
2181 struct perf_mmap_data *data;
2182 unsigned long size;
2183 int i;
2184
2185 WARN_ON(atomic_read(&event->mmap_count));
2186
2187 size = sizeof(struct perf_mmap_data);
2188 size += nr_pages * sizeof(void *);
2189
2190 data = kzalloc(size, GFP_KERNEL);
2191 if (!data)
2192 goto fail;
2193
2194 data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
2195 if (!data->user_page)
2196 goto fail_user_page;
2197
2198 for (i = 0; i < nr_pages; i++) {
2199 data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
2200 if (!data->data_pages[i])
2201 goto fail_data_pages;
2202 }
2203
Peter Zijlstra906010b2009-09-21 16:08:49 +02002204 data->data_order = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002205 data->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002206
Peter Zijlstra906010b2009-09-21 16:08:49 +02002207 return data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002208
2209fail_data_pages:
2210 for (i--; i >= 0; i--)
2211 free_page((unsigned long)data->data_pages[i]);
2212
2213 free_page((unsigned long)data->user_page);
2214
2215fail_user_page:
2216 kfree(data);
2217
2218fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002219 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002220}
2221
2222static void perf_mmap_free_page(unsigned long addr)
2223{
2224 struct page *page = virt_to_page((void *)addr);
2225
2226 page->mapping = NULL;
2227 __free_page(page);
2228}
2229
Peter Zijlstra906010b2009-09-21 16:08:49 +02002230static void perf_mmap_data_free(struct perf_mmap_data *data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002231{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002232 int i;
2233
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002234 perf_mmap_free_page((unsigned long)data->user_page);
2235 for (i = 0; i < data->nr_pages; i++)
2236 perf_mmap_free_page((unsigned long)data->data_pages[i]);
Kristian Høgsbergec70ccd2009-12-01 15:05:01 -05002237 kfree(data);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002238}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002239
Peter Zijlstra906010b2009-09-21 16:08:49 +02002240#else
2241
2242/*
2243 * Back perf_mmap() with vmalloc memory.
2244 *
2245 * Required for architectures that have d-cache aliasing issues.
2246 */
2247
2248static struct page *
2249perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
2250{
2251 if (pgoff > (1UL << data->data_order))
2252 return NULL;
2253
2254 return vmalloc_to_page((void *)data->user_page + pgoff * PAGE_SIZE);
2255}
2256
2257static void perf_mmap_unmark_page(void *addr)
2258{
2259 struct page *page = vmalloc_to_page(addr);
2260
2261 page->mapping = NULL;
2262}
2263
2264static void perf_mmap_data_free_work(struct work_struct *work)
2265{
2266 struct perf_mmap_data *data;
2267 void *base;
2268 int i, nr;
2269
2270 data = container_of(work, struct perf_mmap_data, work);
2271 nr = 1 << data->data_order;
2272
2273 base = data->user_page;
2274 for (i = 0; i < nr + 1; i++)
2275 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2276
2277 vfree(base);
Kristian Høgsbergec70ccd2009-12-01 15:05:01 -05002278 kfree(data);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002279}
2280
2281static void perf_mmap_data_free(struct perf_mmap_data *data)
2282{
2283 schedule_work(&data->work);
2284}
2285
2286static struct perf_mmap_data *
2287perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
2288{
2289 struct perf_mmap_data *data;
2290 unsigned long size;
2291 void *all_buf;
2292
2293 WARN_ON(atomic_read(&event->mmap_count));
2294
2295 size = sizeof(struct perf_mmap_data);
2296 size += sizeof(void *);
2297
2298 data = kzalloc(size, GFP_KERNEL);
2299 if (!data)
2300 goto fail;
2301
2302 INIT_WORK(&data->work, perf_mmap_data_free_work);
2303
2304 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2305 if (!all_buf)
2306 goto fail_all_buf;
2307
2308 data->user_page = all_buf;
2309 data->data_pages[0] = all_buf + PAGE_SIZE;
2310 data->data_order = ilog2(nr_pages);
2311 data->nr_pages = 1;
2312
2313 return data;
2314
2315fail_all_buf:
2316 kfree(data);
2317
2318fail:
2319 return NULL;
2320}
2321
2322#endif
2323
2324static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2325{
2326 struct perf_event *event = vma->vm_file->private_data;
2327 struct perf_mmap_data *data;
2328 int ret = VM_FAULT_SIGBUS;
2329
2330 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2331 if (vmf->pgoff == 0)
2332 ret = 0;
2333 return ret;
2334 }
2335
2336 rcu_read_lock();
2337 data = rcu_dereference(event->data);
2338 if (!data)
2339 goto unlock;
2340
2341 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2342 goto unlock;
2343
2344 vmf->page = perf_mmap_to_page(data, vmf->pgoff);
2345 if (!vmf->page)
2346 goto unlock;
2347
2348 get_page(vmf->page);
2349 vmf->page->mapping = vma->vm_file->f_mapping;
2350 vmf->page->index = vmf->pgoff;
2351
2352 ret = 0;
2353unlock:
2354 rcu_read_unlock();
2355
2356 return ret;
2357}
2358
2359static void
2360perf_mmap_data_init(struct perf_event *event, struct perf_mmap_data *data)
2361{
2362 long max_size = perf_data_size(data);
2363
2364 atomic_set(&data->lock, -1);
2365
2366 if (event->attr.watermark) {
2367 data->watermark = min_t(long, max_size,
2368 event->attr.wakeup_watermark);
2369 }
2370
2371 if (!data->watermark)
Stephane Eranian8904b182009-11-20 22:19:57 +01002372 data->watermark = max_size / 2;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002373
2374
2375 rcu_assign_pointer(event->data, data);
2376}
2377
2378static void perf_mmap_data_free_rcu(struct rcu_head *rcu_head)
2379{
2380 struct perf_mmap_data *data;
2381
2382 data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
2383 perf_mmap_data_free(data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002384}
2385
Peter Zijlstra906010b2009-09-21 16:08:49 +02002386static void perf_mmap_data_release(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002387{
2388 struct perf_mmap_data *data = event->data;
2389
2390 WARN_ON(atomic_read(&event->mmap_count));
2391
2392 rcu_assign_pointer(event->data, NULL);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002393 call_rcu(&data->rcu_head, perf_mmap_data_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002394}
2395
2396static void perf_mmap_open(struct vm_area_struct *vma)
2397{
2398 struct perf_event *event = vma->vm_file->private_data;
2399
2400 atomic_inc(&event->mmap_count);
2401}
2402
2403static void perf_mmap_close(struct vm_area_struct *vma)
2404{
2405 struct perf_event *event = vma->vm_file->private_data;
2406
2407 WARN_ON_ONCE(event->ctx->parent_ctx);
2408 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstra906010b2009-09-21 16:08:49 +02002409 unsigned long size = perf_data_size(event->data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002410 struct user_struct *user = current_user();
2411
Peter Zijlstra906010b2009-09-21 16:08:49 +02002412 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002413 vma->vm_mm->locked_vm -= event->data->nr_locked;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002414 perf_mmap_data_release(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002415 mutex_unlock(&event->mmap_mutex);
2416 }
2417}
2418
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002419static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002420 .open = perf_mmap_open,
2421 .close = perf_mmap_close,
2422 .fault = perf_mmap_fault,
2423 .page_mkwrite = perf_mmap_fault,
2424};
2425
2426static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2427{
2428 struct perf_event *event = file->private_data;
2429 unsigned long user_locked, user_lock_limit;
2430 struct user_struct *user = current_user();
2431 unsigned long locked, lock_limit;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002432 struct perf_mmap_data *data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002433 unsigned long vma_size;
2434 unsigned long nr_pages;
2435 long user_extra, extra;
2436 int ret = 0;
2437
2438 if (!(vma->vm_flags & VM_SHARED))
2439 return -EINVAL;
2440
2441 vma_size = vma->vm_end - vma->vm_start;
2442 nr_pages = (vma_size / PAGE_SIZE) - 1;
2443
2444 /*
2445 * If we have data pages ensure they're a power-of-two number, so we
2446 * can do bitmasks instead of modulo.
2447 */
2448 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2449 return -EINVAL;
2450
2451 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2452 return -EINVAL;
2453
2454 if (vma->vm_pgoff != 0)
2455 return -EINVAL;
2456
2457 WARN_ON_ONCE(event->ctx->parent_ctx);
2458 mutex_lock(&event->mmap_mutex);
2459 if (event->output) {
2460 ret = -EINVAL;
2461 goto unlock;
2462 }
2463
2464 if (atomic_inc_not_zero(&event->mmap_count)) {
2465 if (nr_pages != event->data->nr_pages)
2466 ret = -EINVAL;
2467 goto unlock;
2468 }
2469
2470 user_extra = nr_pages + 1;
2471 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2472
2473 /*
2474 * Increase the limit linearly with more CPUs:
2475 */
2476 user_lock_limit *= num_online_cpus();
2477
2478 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2479
2480 extra = 0;
2481 if (user_locked > user_lock_limit)
2482 extra = user_locked - user_lock_limit;
2483
2484 lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
2485 lock_limit >>= PAGE_SHIFT;
2486 locked = vma->vm_mm->locked_vm + extra;
2487
2488 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2489 !capable(CAP_IPC_LOCK)) {
2490 ret = -EPERM;
2491 goto unlock;
2492 }
2493
2494 WARN_ON(event->data);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002495
2496 data = perf_mmap_data_alloc(event, nr_pages);
2497 ret = -ENOMEM;
2498 if (!data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002499 goto unlock;
2500
Peter Zijlstra906010b2009-09-21 16:08:49 +02002501 ret = 0;
2502 perf_mmap_data_init(event, data);
2503
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002504 atomic_set(&event->mmap_count, 1);
2505 atomic_long_add(user_extra, &user->locked_vm);
2506 vma->vm_mm->locked_vm += extra;
2507 event->data->nr_locked = extra;
2508 if (vma->vm_flags & VM_WRITE)
2509 event->data->writable = 1;
2510
2511unlock:
2512 mutex_unlock(&event->mmap_mutex);
2513
2514 vma->vm_flags |= VM_RESERVED;
2515 vma->vm_ops = &perf_mmap_vmops;
2516
2517 return ret;
2518}
2519
2520static int perf_fasync(int fd, struct file *filp, int on)
2521{
2522 struct inode *inode = filp->f_path.dentry->d_inode;
2523 struct perf_event *event = filp->private_data;
2524 int retval;
2525
2526 mutex_lock(&inode->i_mutex);
2527 retval = fasync_helper(fd, filp, on, &event->fasync);
2528 mutex_unlock(&inode->i_mutex);
2529
2530 if (retval < 0)
2531 return retval;
2532
2533 return 0;
2534}
2535
2536static const struct file_operations perf_fops = {
2537 .release = perf_release,
2538 .read = perf_read,
2539 .poll = perf_poll,
2540 .unlocked_ioctl = perf_ioctl,
2541 .compat_ioctl = perf_ioctl,
2542 .mmap = perf_mmap,
2543 .fasync = perf_fasync,
2544};
2545
2546/*
2547 * Perf event wakeup
2548 *
2549 * If there's data, ensure we set the poll() state and publish everything
2550 * to user-space before waking everybody up.
2551 */
2552
2553void perf_event_wakeup(struct perf_event *event)
2554{
2555 wake_up_all(&event->waitq);
2556
2557 if (event->pending_kill) {
2558 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
2559 event->pending_kill = 0;
2560 }
2561}
2562
2563/*
2564 * Pending wakeups
2565 *
2566 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
2567 *
2568 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
2569 * single linked list and use cmpxchg() to add entries lockless.
2570 */
2571
2572static void perf_pending_event(struct perf_pending_entry *entry)
2573{
2574 struct perf_event *event = container_of(entry,
2575 struct perf_event, pending);
2576
2577 if (event->pending_disable) {
2578 event->pending_disable = 0;
2579 __perf_event_disable(event);
2580 }
2581
2582 if (event->pending_wakeup) {
2583 event->pending_wakeup = 0;
2584 perf_event_wakeup(event);
2585 }
2586}
2587
2588#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
2589
2590static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
2591 PENDING_TAIL,
2592};
2593
2594static void perf_pending_queue(struct perf_pending_entry *entry,
2595 void (*func)(struct perf_pending_entry *))
2596{
2597 struct perf_pending_entry **head;
2598
2599 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
2600 return;
2601
2602 entry->func = func;
2603
2604 head = &get_cpu_var(perf_pending_head);
2605
2606 do {
2607 entry->next = *head;
2608 } while (cmpxchg(head, entry->next, entry) != entry->next);
2609
2610 set_perf_event_pending();
2611
2612 put_cpu_var(perf_pending_head);
2613}
2614
2615static int __perf_pending_run(void)
2616{
2617 struct perf_pending_entry *list;
2618 int nr = 0;
2619
2620 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
2621 while (list != PENDING_TAIL) {
2622 void (*func)(struct perf_pending_entry *);
2623 struct perf_pending_entry *entry = list;
2624
2625 list = list->next;
2626
2627 func = entry->func;
2628 entry->next = NULL;
2629 /*
2630 * Ensure we observe the unqueue before we issue the wakeup,
2631 * so that we won't be waiting forever.
2632 * -- see perf_not_pending().
2633 */
2634 smp_wmb();
2635
2636 func(entry);
2637 nr++;
2638 }
2639
2640 return nr;
2641}
2642
2643static inline int perf_not_pending(struct perf_event *event)
2644{
2645 /*
2646 * If we flush on whatever cpu we run, there is a chance we don't
2647 * need to wait.
2648 */
2649 get_cpu();
2650 __perf_pending_run();
2651 put_cpu();
2652
2653 /*
2654 * Ensure we see the proper queue state before going to sleep
2655 * so that we do not miss the wakeup. -- see perf_pending_handle()
2656 */
2657 smp_rmb();
2658 return event->pending.next == NULL;
2659}
2660
2661static void perf_pending_sync(struct perf_event *event)
2662{
2663 wait_event(event->waitq, perf_not_pending(event));
2664}
2665
2666void perf_event_do_pending(void)
2667{
2668 __perf_pending_run();
2669}
2670
2671/*
2672 * Callchain support -- arch specific
2673 */
2674
2675__weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2676{
2677 return NULL;
2678}
2679
2680/*
2681 * Output
2682 */
2683static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
2684 unsigned long offset, unsigned long head)
2685{
2686 unsigned long mask;
2687
2688 if (!data->writable)
2689 return true;
2690
Peter Zijlstra906010b2009-09-21 16:08:49 +02002691 mask = perf_data_size(data) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002692
2693 offset = (offset - tail) & mask;
2694 head = (head - tail) & mask;
2695
2696 if ((int)(head - offset) < 0)
2697 return false;
2698
2699 return true;
2700}
2701
2702static void perf_output_wakeup(struct perf_output_handle *handle)
2703{
2704 atomic_set(&handle->data->poll, POLL_IN);
2705
2706 if (handle->nmi) {
2707 handle->event->pending_wakeup = 1;
2708 perf_pending_queue(&handle->event->pending,
2709 perf_pending_event);
2710 } else
2711 perf_event_wakeup(handle->event);
2712}
2713
2714/*
2715 * Curious locking construct.
2716 *
2717 * We need to ensure a later event_id doesn't publish a head when a former
2718 * event_id isn't done writing. However since we need to deal with NMIs we
2719 * cannot fully serialize things.
2720 *
2721 * What we do is serialize between CPUs so we only have to deal with NMI
2722 * nesting on a single CPU.
2723 *
2724 * We only publish the head (and generate a wakeup) when the outer-most
2725 * event_id completes.
2726 */
2727static void perf_output_lock(struct perf_output_handle *handle)
2728{
2729 struct perf_mmap_data *data = handle->data;
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002730 int cur, cpu = get_cpu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002731
2732 handle->locked = 0;
2733
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002734 for (;;) {
2735 cur = atomic_cmpxchg(&data->lock, -1, cpu);
2736 if (cur == -1) {
2737 handle->locked = 1;
2738 break;
2739 }
2740 if (cur == cpu)
2741 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002742
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002743 cpu_relax();
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002744 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002745}
2746
2747static void perf_output_unlock(struct perf_output_handle *handle)
2748{
2749 struct perf_mmap_data *data = handle->data;
2750 unsigned long head;
2751 int cpu;
2752
2753 data->done_head = data->head;
2754
2755 if (!handle->locked)
2756 goto out;
2757
2758again:
2759 /*
2760 * The xchg implies a full barrier that ensures all writes are done
2761 * before we publish the new head, matched by a rmb() in userspace when
2762 * reading this position.
2763 */
2764 while ((head = atomic_long_xchg(&data->done_head, 0)))
2765 data->user_page->data_head = head;
2766
2767 /*
2768 * NMI can happen here, which means we can miss a done_head update.
2769 */
2770
2771 cpu = atomic_xchg(&data->lock, -1);
2772 WARN_ON_ONCE(cpu != smp_processor_id());
2773
2774 /*
2775 * Therefore we have to validate we did not indeed do so.
2776 */
2777 if (unlikely(atomic_long_read(&data->done_head))) {
2778 /*
2779 * Since we had it locked, we can lock it again.
2780 */
2781 while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
2782 cpu_relax();
2783
2784 goto again;
2785 }
2786
2787 if (atomic_xchg(&data->wakeup, 0))
2788 perf_output_wakeup(handle);
2789out:
Peter Zijlstra559fdc32009-11-16 12:45:14 +01002790 put_cpu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002791}
2792
2793void perf_output_copy(struct perf_output_handle *handle,
2794 const void *buf, unsigned int len)
2795{
2796 unsigned int pages_mask;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002797 unsigned long offset;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002798 unsigned int size;
2799 void **pages;
2800
2801 offset = handle->offset;
2802 pages_mask = handle->data->nr_pages - 1;
2803 pages = handle->data->data_pages;
2804
2805 do {
Peter Zijlstra906010b2009-09-21 16:08:49 +02002806 unsigned long page_offset;
2807 unsigned long page_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002808 int nr;
2809
2810 nr = (offset >> PAGE_SHIFT) & pages_mask;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002811 page_size = 1UL << (handle->data->data_order + PAGE_SHIFT);
2812 page_offset = offset & (page_size - 1);
2813 size = min_t(unsigned int, page_size - page_offset, len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002814
2815 memcpy(pages[nr] + page_offset, buf, size);
2816
2817 len -= size;
2818 buf += size;
2819 offset += size;
2820 } while (len);
2821
2822 handle->offset = offset;
2823
2824 /*
2825 * Check we didn't copy past our reservation window, taking the
2826 * possible unsigned int wrap into account.
2827 */
2828 WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
2829}
2830
2831int perf_output_begin(struct perf_output_handle *handle,
2832 struct perf_event *event, unsigned int size,
2833 int nmi, int sample)
2834{
2835 struct perf_event *output_event;
2836 struct perf_mmap_data *data;
2837 unsigned long tail, offset, head;
2838 int have_lost;
2839 struct {
2840 struct perf_event_header header;
2841 u64 id;
2842 u64 lost;
2843 } lost_event;
2844
2845 rcu_read_lock();
2846 /*
2847 * For inherited events we send all the output towards the parent.
2848 */
2849 if (event->parent)
2850 event = event->parent;
2851
2852 output_event = rcu_dereference(event->output);
2853 if (output_event)
2854 event = output_event;
2855
2856 data = rcu_dereference(event->data);
2857 if (!data)
2858 goto out;
2859
2860 handle->data = data;
2861 handle->event = event;
2862 handle->nmi = nmi;
2863 handle->sample = sample;
2864
2865 if (!data->nr_pages)
2866 goto fail;
2867
2868 have_lost = atomic_read(&data->lost);
2869 if (have_lost)
2870 size += sizeof(lost_event);
2871
2872 perf_output_lock(handle);
2873
2874 do {
2875 /*
2876 * Userspace could choose to issue a mb() before updating the
2877 * tail pointer. So that all reads will be completed before the
2878 * write is issued.
2879 */
2880 tail = ACCESS_ONCE(data->user_page->data_tail);
2881 smp_rmb();
2882 offset = head = atomic_long_read(&data->head);
2883 head += size;
2884 if (unlikely(!perf_output_space(data, tail, offset, head)))
2885 goto fail;
2886 } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
2887
2888 handle->offset = offset;
2889 handle->head = head;
2890
2891 if (head - tail > data->watermark)
2892 atomic_set(&data->wakeup, 1);
2893
2894 if (have_lost) {
2895 lost_event.header.type = PERF_RECORD_LOST;
2896 lost_event.header.misc = 0;
2897 lost_event.header.size = sizeof(lost_event);
2898 lost_event.id = event->id;
2899 lost_event.lost = atomic_xchg(&data->lost, 0);
2900
2901 perf_output_put(handle, lost_event);
2902 }
2903
2904 return 0;
2905
2906fail:
2907 atomic_inc(&data->lost);
2908 perf_output_unlock(handle);
2909out:
2910 rcu_read_unlock();
2911
2912 return -ENOSPC;
2913}
2914
2915void perf_output_end(struct perf_output_handle *handle)
2916{
2917 struct perf_event *event = handle->event;
2918 struct perf_mmap_data *data = handle->data;
2919
2920 int wakeup_events = event->attr.wakeup_events;
2921
2922 if (handle->sample && wakeup_events) {
2923 int events = atomic_inc_return(&data->events);
2924 if (events >= wakeup_events) {
2925 atomic_sub(wakeup_events, &data->events);
2926 atomic_set(&data->wakeup, 1);
2927 }
2928 }
2929
2930 perf_output_unlock(handle);
2931 rcu_read_unlock();
2932}
2933
2934static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
2935{
2936 /*
2937 * only top level events have the pid namespace they were created in
2938 */
2939 if (event->parent)
2940 event = event->parent;
2941
2942 return task_tgid_nr_ns(p, event->ns);
2943}
2944
2945static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
2946{
2947 /*
2948 * only top level events have the pid namespace they were created in
2949 */
2950 if (event->parent)
2951 event = event->parent;
2952
2953 return task_pid_nr_ns(p, event->ns);
2954}
2955
2956static void perf_output_read_one(struct perf_output_handle *handle,
2957 struct perf_event *event)
2958{
2959 u64 read_format = event->attr.read_format;
2960 u64 values[4];
2961 int n = 0;
2962
2963 values[n++] = atomic64_read(&event->count);
2964 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
2965 values[n++] = event->total_time_enabled +
2966 atomic64_read(&event->child_total_time_enabled);
2967 }
2968 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
2969 values[n++] = event->total_time_running +
2970 atomic64_read(&event->child_total_time_running);
2971 }
2972 if (read_format & PERF_FORMAT_ID)
2973 values[n++] = primary_event_id(event);
2974
2975 perf_output_copy(handle, values, n * sizeof(u64));
2976}
2977
2978/*
2979 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
2980 */
2981static void perf_output_read_group(struct perf_output_handle *handle,
2982 struct perf_event *event)
2983{
2984 struct perf_event *leader = event->group_leader, *sub;
2985 u64 read_format = event->attr.read_format;
2986 u64 values[5];
2987 int n = 0;
2988
2989 values[n++] = 1 + leader->nr_siblings;
2990
2991 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2992 values[n++] = leader->total_time_enabled;
2993
2994 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2995 values[n++] = leader->total_time_running;
2996
2997 if (leader != event)
2998 leader->pmu->read(leader);
2999
3000 values[n++] = atomic64_read(&leader->count);
3001 if (read_format & PERF_FORMAT_ID)
3002 values[n++] = primary_event_id(leader);
3003
3004 perf_output_copy(handle, values, n * sizeof(u64));
3005
3006 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3007 n = 0;
3008
3009 if (sub != event)
3010 sub->pmu->read(sub);
3011
3012 values[n++] = atomic64_read(&sub->count);
3013 if (read_format & PERF_FORMAT_ID)
3014 values[n++] = primary_event_id(sub);
3015
3016 perf_output_copy(handle, values, n * sizeof(u64));
3017 }
3018}
3019
3020static void perf_output_read(struct perf_output_handle *handle,
3021 struct perf_event *event)
3022{
3023 if (event->attr.read_format & PERF_FORMAT_GROUP)
3024 perf_output_read_group(handle, event);
3025 else
3026 perf_output_read_one(handle, event);
3027}
3028
3029void perf_output_sample(struct perf_output_handle *handle,
3030 struct perf_event_header *header,
3031 struct perf_sample_data *data,
3032 struct perf_event *event)
3033{
3034 u64 sample_type = data->type;
3035
3036 perf_output_put(handle, *header);
3037
3038 if (sample_type & PERF_SAMPLE_IP)
3039 perf_output_put(handle, data->ip);
3040
3041 if (sample_type & PERF_SAMPLE_TID)
3042 perf_output_put(handle, data->tid_entry);
3043
3044 if (sample_type & PERF_SAMPLE_TIME)
3045 perf_output_put(handle, data->time);
3046
3047 if (sample_type & PERF_SAMPLE_ADDR)
3048 perf_output_put(handle, data->addr);
3049
3050 if (sample_type & PERF_SAMPLE_ID)
3051 perf_output_put(handle, data->id);
3052
3053 if (sample_type & PERF_SAMPLE_STREAM_ID)
3054 perf_output_put(handle, data->stream_id);
3055
3056 if (sample_type & PERF_SAMPLE_CPU)
3057 perf_output_put(handle, data->cpu_entry);
3058
3059 if (sample_type & PERF_SAMPLE_PERIOD)
3060 perf_output_put(handle, data->period);
3061
3062 if (sample_type & PERF_SAMPLE_READ)
3063 perf_output_read(handle, event);
3064
3065 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3066 if (data->callchain) {
3067 int size = 1;
3068
3069 if (data->callchain)
3070 size += data->callchain->nr;
3071
3072 size *= sizeof(u64);
3073
3074 perf_output_copy(handle, data->callchain, size);
3075 } else {
3076 u64 nr = 0;
3077 perf_output_put(handle, nr);
3078 }
3079 }
3080
3081 if (sample_type & PERF_SAMPLE_RAW) {
3082 if (data->raw) {
3083 perf_output_put(handle, data->raw->size);
3084 perf_output_copy(handle, data->raw->data,
3085 data->raw->size);
3086 } else {
3087 struct {
3088 u32 size;
3089 u32 data;
3090 } raw = {
3091 .size = sizeof(u32),
3092 .data = 0,
3093 };
3094 perf_output_put(handle, raw);
3095 }
3096 }
3097}
3098
3099void perf_prepare_sample(struct perf_event_header *header,
3100 struct perf_sample_data *data,
3101 struct perf_event *event,
3102 struct pt_regs *regs)
3103{
3104 u64 sample_type = event->attr.sample_type;
3105
3106 data->type = sample_type;
3107
3108 header->type = PERF_RECORD_SAMPLE;
3109 header->size = sizeof(*header);
3110
3111 header->misc = 0;
3112 header->misc |= perf_misc_flags(regs);
3113
3114 if (sample_type & PERF_SAMPLE_IP) {
3115 data->ip = perf_instruction_pointer(regs);
3116
3117 header->size += sizeof(data->ip);
3118 }
3119
3120 if (sample_type & PERF_SAMPLE_TID) {
3121 /* namespace issues */
3122 data->tid_entry.pid = perf_event_pid(event, current);
3123 data->tid_entry.tid = perf_event_tid(event, current);
3124
3125 header->size += sizeof(data->tid_entry);
3126 }
3127
3128 if (sample_type & PERF_SAMPLE_TIME) {
3129 data->time = perf_clock();
3130
3131 header->size += sizeof(data->time);
3132 }
3133
3134 if (sample_type & PERF_SAMPLE_ADDR)
3135 header->size += sizeof(data->addr);
3136
3137 if (sample_type & PERF_SAMPLE_ID) {
3138 data->id = primary_event_id(event);
3139
3140 header->size += sizeof(data->id);
3141 }
3142
3143 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3144 data->stream_id = event->id;
3145
3146 header->size += sizeof(data->stream_id);
3147 }
3148
3149 if (sample_type & PERF_SAMPLE_CPU) {
3150 data->cpu_entry.cpu = raw_smp_processor_id();
3151 data->cpu_entry.reserved = 0;
3152
3153 header->size += sizeof(data->cpu_entry);
3154 }
3155
3156 if (sample_type & PERF_SAMPLE_PERIOD)
3157 header->size += sizeof(data->period);
3158
3159 if (sample_type & PERF_SAMPLE_READ)
3160 header->size += perf_event_read_size(event);
3161
3162 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3163 int size = 1;
3164
3165 data->callchain = perf_callchain(regs);
3166
3167 if (data->callchain)
3168 size += data->callchain->nr;
3169
3170 header->size += size * sizeof(u64);
3171 }
3172
3173 if (sample_type & PERF_SAMPLE_RAW) {
3174 int size = sizeof(u32);
3175
3176 if (data->raw)
3177 size += data->raw->size;
3178 else
3179 size += sizeof(u32);
3180
3181 WARN_ON_ONCE(size & (sizeof(u64)-1));
3182 header->size += size;
3183 }
3184}
3185
3186static void perf_event_output(struct perf_event *event, int nmi,
3187 struct perf_sample_data *data,
3188 struct pt_regs *regs)
3189{
3190 struct perf_output_handle handle;
3191 struct perf_event_header header;
3192
3193 perf_prepare_sample(&header, data, event, regs);
3194
3195 if (perf_output_begin(&handle, event, header.size, nmi, 1))
3196 return;
3197
3198 perf_output_sample(&handle, &header, data, event);
3199
3200 perf_output_end(&handle);
3201}
3202
3203/*
3204 * read event_id
3205 */
3206
3207struct perf_read_event {
3208 struct perf_event_header header;
3209
3210 u32 pid;
3211 u32 tid;
3212};
3213
3214static void
3215perf_event_read_event(struct perf_event *event,
3216 struct task_struct *task)
3217{
3218 struct perf_output_handle handle;
3219 struct perf_read_event read_event = {
3220 .header = {
3221 .type = PERF_RECORD_READ,
3222 .misc = 0,
3223 .size = sizeof(read_event) + perf_event_read_size(event),
3224 },
3225 .pid = perf_event_pid(event, task),
3226 .tid = perf_event_tid(event, task),
3227 };
3228 int ret;
3229
3230 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3231 if (ret)
3232 return;
3233
3234 perf_output_put(&handle, read_event);
3235 perf_output_read(&handle, event);
3236
3237 perf_output_end(&handle);
3238}
3239
3240/*
3241 * task tracking -- fork/exit
3242 *
3243 * enabled by: attr.comm | attr.mmap | attr.task
3244 */
3245
3246struct perf_task_event {
3247 struct task_struct *task;
3248 struct perf_event_context *task_ctx;
3249
3250 struct {
3251 struct perf_event_header header;
3252
3253 u32 pid;
3254 u32 ppid;
3255 u32 tid;
3256 u32 ptid;
3257 u64 time;
3258 } event_id;
3259};
3260
3261static void perf_event_task_output(struct perf_event *event,
3262 struct perf_task_event *task_event)
3263{
3264 struct perf_output_handle handle;
3265 int size;
3266 struct task_struct *task = task_event->task;
3267 int ret;
3268
3269 size = task_event->event_id.header.size;
3270 ret = perf_output_begin(&handle, event, size, 0, 0);
3271
3272 if (ret)
3273 return;
3274
3275 task_event->event_id.pid = perf_event_pid(event, task);
3276 task_event->event_id.ppid = perf_event_pid(event, current);
3277
3278 task_event->event_id.tid = perf_event_tid(event, task);
3279 task_event->event_id.ptid = perf_event_tid(event, current);
3280
3281 task_event->event_id.time = perf_clock();
3282
3283 perf_output_put(&handle, task_event->event_id);
3284
3285 perf_output_end(&handle);
3286}
3287
3288static int perf_event_task_match(struct perf_event *event)
3289{
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003290 if (event->cpu != -1 && event->cpu != smp_processor_id())
3291 return 0;
3292
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003293 if (event->attr.comm || event->attr.mmap || event->attr.task)
3294 return 1;
3295
3296 return 0;
3297}
3298
3299static void perf_event_task_ctx(struct perf_event_context *ctx,
3300 struct perf_task_event *task_event)
3301{
3302 struct perf_event *event;
3303
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003304 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3305 if (perf_event_task_match(event))
3306 perf_event_task_output(event, task_event);
3307 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003308}
3309
3310static void perf_event_task_event(struct perf_task_event *task_event)
3311{
3312 struct perf_cpu_context *cpuctx;
3313 struct perf_event_context *ctx = task_event->task_ctx;
3314
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01003315 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003316 cpuctx = &get_cpu_var(perf_cpu_context);
3317 perf_event_task_ctx(&cpuctx->ctx, task_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003318 if (!ctx)
3319 ctx = rcu_dereference(task_event->task->perf_event_ctxp);
3320 if (ctx)
3321 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003322 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003323 rcu_read_unlock();
3324}
3325
3326static void perf_event_task(struct task_struct *task,
3327 struct perf_event_context *task_ctx,
3328 int new)
3329{
3330 struct perf_task_event task_event;
3331
3332 if (!atomic_read(&nr_comm_events) &&
3333 !atomic_read(&nr_mmap_events) &&
3334 !atomic_read(&nr_task_events))
3335 return;
3336
3337 task_event = (struct perf_task_event){
3338 .task = task,
3339 .task_ctx = task_ctx,
3340 .event_id = {
3341 .header = {
3342 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3343 .misc = 0,
3344 .size = sizeof(task_event.event_id),
3345 },
3346 /* .pid */
3347 /* .ppid */
3348 /* .tid */
3349 /* .ptid */
3350 },
3351 };
3352
3353 perf_event_task_event(&task_event);
3354}
3355
3356void perf_event_fork(struct task_struct *task)
3357{
3358 perf_event_task(task, NULL, 1);
3359}
3360
3361/*
3362 * comm tracking
3363 */
3364
3365struct perf_comm_event {
3366 struct task_struct *task;
3367 char *comm;
3368 int comm_size;
3369
3370 struct {
3371 struct perf_event_header header;
3372
3373 u32 pid;
3374 u32 tid;
3375 } event_id;
3376};
3377
3378static void perf_event_comm_output(struct perf_event *event,
3379 struct perf_comm_event *comm_event)
3380{
3381 struct perf_output_handle handle;
3382 int size = comm_event->event_id.header.size;
3383 int ret = perf_output_begin(&handle, event, size, 0, 0);
3384
3385 if (ret)
3386 return;
3387
3388 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3389 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3390
3391 perf_output_put(&handle, comm_event->event_id);
3392 perf_output_copy(&handle, comm_event->comm,
3393 comm_event->comm_size);
3394 perf_output_end(&handle);
3395}
3396
3397static int perf_event_comm_match(struct perf_event *event)
3398{
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003399 if (event->cpu != -1 && event->cpu != smp_processor_id())
3400 return 0;
3401
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402 if (event->attr.comm)
3403 return 1;
3404
3405 return 0;
3406}
3407
3408static void perf_event_comm_ctx(struct perf_event_context *ctx,
3409 struct perf_comm_event *comm_event)
3410{
3411 struct perf_event *event;
3412
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003413 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3414 if (perf_event_comm_match(event))
3415 perf_event_comm_output(event, comm_event);
3416 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003417}
3418
3419static void perf_event_comm_event(struct perf_comm_event *comm_event)
3420{
3421 struct perf_cpu_context *cpuctx;
3422 struct perf_event_context *ctx;
3423 unsigned int size;
3424 char comm[TASK_COMM_LEN];
3425
3426 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01003427 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003428 size = ALIGN(strlen(comm)+1, sizeof(u64));
3429
3430 comm_event->comm = comm;
3431 comm_event->comm_size = size;
3432
3433 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3434
Peter Zijlstraf6595f32009-11-20 22:19:47 +01003435 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003436 cpuctx = &get_cpu_var(perf_cpu_context);
3437 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003438 ctx = rcu_dereference(current->perf_event_ctxp);
3439 if (ctx)
3440 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003441 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003442 rcu_read_unlock();
3443}
3444
3445void perf_event_comm(struct task_struct *task)
3446{
3447 struct perf_comm_event comm_event;
3448
3449 if (task->perf_event_ctxp)
3450 perf_event_enable_on_exec(task);
3451
3452 if (!atomic_read(&nr_comm_events))
3453 return;
3454
3455 comm_event = (struct perf_comm_event){
3456 .task = task,
3457 /* .comm */
3458 /* .comm_size */
3459 .event_id = {
3460 .header = {
3461 .type = PERF_RECORD_COMM,
3462 .misc = 0,
3463 /* .size */
3464 },
3465 /* .pid */
3466 /* .tid */
3467 },
3468 };
3469
3470 perf_event_comm_event(&comm_event);
3471}
3472
3473/*
3474 * mmap tracking
3475 */
3476
3477struct perf_mmap_event {
3478 struct vm_area_struct *vma;
3479
3480 const char *file_name;
3481 int file_size;
3482
3483 struct {
3484 struct perf_event_header header;
3485
3486 u32 pid;
3487 u32 tid;
3488 u64 start;
3489 u64 len;
3490 u64 pgoff;
3491 } event_id;
3492};
3493
3494static void perf_event_mmap_output(struct perf_event *event,
3495 struct perf_mmap_event *mmap_event)
3496{
3497 struct perf_output_handle handle;
3498 int size = mmap_event->event_id.header.size;
3499 int ret = perf_output_begin(&handle, event, size, 0, 0);
3500
3501 if (ret)
3502 return;
3503
3504 mmap_event->event_id.pid = perf_event_pid(event, current);
3505 mmap_event->event_id.tid = perf_event_tid(event, current);
3506
3507 perf_output_put(&handle, mmap_event->event_id);
3508 perf_output_copy(&handle, mmap_event->file_name,
3509 mmap_event->file_size);
3510 perf_output_end(&handle);
3511}
3512
3513static int perf_event_mmap_match(struct perf_event *event,
3514 struct perf_mmap_event *mmap_event)
3515{
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003516 if (event->cpu != -1 && event->cpu != smp_processor_id())
3517 return 0;
3518
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003519 if (event->attr.mmap)
3520 return 1;
3521
3522 return 0;
3523}
3524
3525static void perf_event_mmap_ctx(struct perf_event_context *ctx,
3526 struct perf_mmap_event *mmap_event)
3527{
3528 struct perf_event *event;
3529
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003530 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3531 if (perf_event_mmap_match(event, mmap_event))
3532 perf_event_mmap_output(event, mmap_event);
3533 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003534}
3535
3536static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
3537{
3538 struct perf_cpu_context *cpuctx;
3539 struct perf_event_context *ctx;
3540 struct vm_area_struct *vma = mmap_event->vma;
3541 struct file *file = vma->vm_file;
3542 unsigned int size;
3543 char tmp[16];
3544 char *buf = NULL;
3545 const char *name;
3546
3547 memset(tmp, 0, sizeof(tmp));
3548
3549 if (file) {
3550 /*
3551 * d_path works from the end of the buffer backwards, so we
3552 * need to add enough zero bytes after the string to handle
3553 * the 64bit alignment we do later.
3554 */
3555 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
3556 if (!buf) {
3557 name = strncpy(tmp, "//enomem", sizeof(tmp));
3558 goto got_name;
3559 }
3560 name = d_path(&file->f_path, buf, PATH_MAX);
3561 if (IS_ERR(name)) {
3562 name = strncpy(tmp, "//toolong", sizeof(tmp));
3563 goto got_name;
3564 }
3565 } else {
3566 if (arch_vma_name(mmap_event->vma)) {
3567 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
3568 sizeof(tmp));
3569 goto got_name;
3570 }
3571
3572 if (!vma->vm_mm) {
3573 name = strncpy(tmp, "[vdso]", sizeof(tmp));
3574 goto got_name;
3575 }
3576
3577 name = strncpy(tmp, "//anon", sizeof(tmp));
3578 goto got_name;
3579 }
3580
3581got_name:
3582 size = ALIGN(strlen(name)+1, sizeof(u64));
3583
3584 mmap_event->file_name = name;
3585 mmap_event->file_size = size;
3586
3587 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
3588
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01003589 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003590 cpuctx = &get_cpu_var(perf_cpu_context);
3591 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003592 ctx = rcu_dereference(current->perf_event_ctxp);
3593 if (ctx)
3594 perf_event_mmap_ctx(ctx, mmap_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003595 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003596 rcu_read_unlock();
3597
3598 kfree(buf);
3599}
3600
3601void __perf_event_mmap(struct vm_area_struct *vma)
3602{
3603 struct perf_mmap_event mmap_event;
3604
3605 if (!atomic_read(&nr_mmap_events))
3606 return;
3607
3608 mmap_event = (struct perf_mmap_event){
3609 .vma = vma,
3610 /* .file_name */
3611 /* .file_size */
3612 .event_id = {
3613 .header = {
3614 .type = PERF_RECORD_MMAP,
3615 .misc = 0,
3616 /* .size */
3617 },
3618 /* .pid */
3619 /* .tid */
3620 .start = vma->vm_start,
3621 .len = vma->vm_end - vma->vm_start,
3622 .pgoff = vma->vm_pgoff,
3623 },
3624 };
3625
3626 perf_event_mmap_event(&mmap_event);
3627}
3628
3629/*
3630 * IRQ throttle logging
3631 */
3632
3633static void perf_log_throttle(struct perf_event *event, int enable)
3634{
3635 struct perf_output_handle handle;
3636 int ret;
3637
3638 struct {
3639 struct perf_event_header header;
3640 u64 time;
3641 u64 id;
3642 u64 stream_id;
3643 } throttle_event = {
3644 .header = {
3645 .type = PERF_RECORD_THROTTLE,
3646 .misc = 0,
3647 .size = sizeof(throttle_event),
3648 },
3649 .time = perf_clock(),
3650 .id = primary_event_id(event),
3651 .stream_id = event->id,
3652 };
3653
3654 if (enable)
3655 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
3656
3657 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
3658 if (ret)
3659 return;
3660
3661 perf_output_put(&handle, throttle_event);
3662 perf_output_end(&handle);
3663}
3664
3665/*
3666 * Generic event overflow handling, sampling.
3667 */
3668
3669static int __perf_event_overflow(struct perf_event *event, int nmi,
3670 int throttle, struct perf_sample_data *data,
3671 struct pt_regs *regs)
3672{
3673 int events = atomic_read(&event->event_limit);
3674 struct hw_perf_event *hwc = &event->hw;
3675 int ret = 0;
3676
3677 throttle = (throttle && event->pmu->unthrottle != NULL);
3678
3679 if (!throttle) {
3680 hwc->interrupts++;
3681 } else {
3682 if (hwc->interrupts != MAX_INTERRUPTS) {
3683 hwc->interrupts++;
3684 if (HZ * hwc->interrupts >
3685 (u64)sysctl_perf_event_sample_rate) {
3686 hwc->interrupts = MAX_INTERRUPTS;
3687 perf_log_throttle(event, 0);
3688 ret = 1;
3689 }
3690 } else {
3691 /*
3692 * Keep re-disabling events even though on the previous
3693 * pass we disabled it - just in case we raced with a
3694 * sched-in and the event got enabled again:
3695 */
3696 ret = 1;
3697 }
3698 }
3699
3700 if (event->attr.freq) {
3701 u64 now = perf_clock();
3702 s64 delta = now - hwc->freq_stamp;
3703
3704 hwc->freq_stamp = now;
3705
3706 if (delta > 0 && delta < TICK_NSEC)
3707 perf_adjust_period(event, NSEC_PER_SEC / (int)delta);
3708 }
3709
3710 /*
3711 * XXX event_limit might not quite work as expected on inherited
3712 * events
3713 */
3714
3715 event->pending_kill = POLL_IN;
3716 if (events && atomic_dec_and_test(&event->event_limit)) {
3717 ret = 1;
3718 event->pending_kill = POLL_HUP;
3719 if (nmi) {
3720 event->pending_disable = 1;
3721 perf_pending_queue(&event->pending,
3722 perf_pending_event);
3723 } else
3724 perf_event_disable(event);
3725 }
3726
Peter Zijlstra453f19e2009-11-20 22:19:43 +01003727 if (event->overflow_handler)
3728 event->overflow_handler(event, nmi, data, regs);
3729 else
3730 perf_event_output(event, nmi, data, regs);
3731
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003732 return ret;
3733}
3734
3735int perf_event_overflow(struct perf_event *event, int nmi,
3736 struct perf_sample_data *data,
3737 struct pt_regs *regs)
3738{
3739 return __perf_event_overflow(event, nmi, 1, data, regs);
3740}
3741
3742/*
3743 * Generic software event infrastructure
3744 */
3745
3746/*
3747 * We directly increment event->count and keep a second value in
3748 * event->hw.period_left to count intervals. This period event
3749 * is kept in the range [-sample_period, 0] so that we can use the
3750 * sign as trigger.
3751 */
3752
3753static u64 perf_swevent_set_period(struct perf_event *event)
3754{
3755 struct hw_perf_event *hwc = &event->hw;
3756 u64 period = hwc->last_period;
3757 u64 nr, offset;
3758 s64 old, val;
3759
3760 hwc->last_period = hwc->sample_period;
3761
3762again:
3763 old = val = atomic64_read(&hwc->period_left);
3764 if (val < 0)
3765 return 0;
3766
3767 nr = div64_u64(period + val, period);
3768 offset = nr * period;
3769 val -= offset;
3770 if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
3771 goto again;
3772
3773 return nr;
3774}
3775
Peter Zijlstra0cff7842009-11-20 22:19:44 +01003776static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003777 int nmi, struct perf_sample_data *data,
3778 struct pt_regs *regs)
3779{
3780 struct hw_perf_event *hwc = &event->hw;
3781 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003782
3783 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01003784 if (!overflow)
3785 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003786
3787 if (hwc->interrupts == MAX_INTERRUPTS)
3788 return;
3789
3790 for (; overflow; overflow--) {
3791 if (__perf_event_overflow(event, nmi, throttle,
3792 data, regs)) {
3793 /*
3794 * We inhibit the overflow from happening when
3795 * hwc->interrupts == MAX_INTERRUPTS.
3796 */
3797 break;
3798 }
3799 throttle = 1;
3800 }
3801}
3802
3803static void perf_swevent_unthrottle(struct perf_event *event)
3804{
3805 /*
3806 * Nothing to do, we already reset hwc->interrupts.
3807 */
3808}
3809
3810static void perf_swevent_add(struct perf_event *event, u64 nr,
3811 int nmi, struct perf_sample_data *data,
3812 struct pt_regs *regs)
3813{
3814 struct hw_perf_event *hwc = &event->hw;
3815
3816 atomic64_add(nr, &event->count);
3817
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003818 if (!regs)
3819 return;
3820
Peter Zijlstra0cff7842009-11-20 22:19:44 +01003821 if (!hwc->sample_period)
3822 return;
3823
3824 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
3825 return perf_swevent_overflow(event, 1, nmi, data, regs);
3826
3827 if (atomic64_add_negative(nr, &hwc->period_left))
3828 return;
3829
3830 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003831}
3832
3833static int perf_swevent_is_counting(struct perf_event *event)
3834{
3835 /*
3836 * The event is active, we're good!
3837 */
3838 if (event->state == PERF_EVENT_STATE_ACTIVE)
3839 return 1;
3840
3841 /*
3842 * The event is off/error, not counting.
3843 */
3844 if (event->state != PERF_EVENT_STATE_INACTIVE)
3845 return 0;
3846
3847 /*
3848 * The event is inactive, if the context is active
3849 * we're part of a group that didn't make it on the 'pmu',
3850 * not counting.
3851 */
3852 if (event->ctx->is_active)
3853 return 0;
3854
3855 /*
3856 * We're inactive and the context is too, this means the
3857 * task is scheduled out, we're counting events that happen
3858 * to us, like migration events.
3859 */
3860 return 1;
3861}
3862
Li Zefan6fb29152009-10-15 11:21:42 +08003863static int perf_tp_event_match(struct perf_event *event,
3864 struct perf_sample_data *data);
3865
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01003866static int perf_exclude_event(struct perf_event *event,
3867 struct pt_regs *regs)
3868{
3869 if (regs) {
3870 if (event->attr.exclude_user && user_mode(regs))
3871 return 1;
3872
3873 if (event->attr.exclude_kernel && !user_mode(regs))
3874 return 1;
3875 }
3876
3877 return 0;
3878}
3879
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003880static int perf_swevent_match(struct perf_event *event,
3881 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08003882 u32 event_id,
3883 struct perf_sample_data *data,
3884 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003885{
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003886 if (event->cpu != -1 && event->cpu != smp_processor_id())
3887 return 0;
3888
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003889 if (!perf_swevent_is_counting(event))
3890 return 0;
3891
3892 if (event->attr.type != type)
3893 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01003894
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003895 if (event->attr.config != event_id)
3896 return 0;
3897
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01003898 if (perf_exclude_event(event, regs))
3899 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003900
Li Zefan6fb29152009-10-15 11:21:42 +08003901 if (event->attr.type == PERF_TYPE_TRACEPOINT &&
3902 !perf_tp_event_match(event, data))
3903 return 0;
3904
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003905 return 1;
3906}
3907
3908static void perf_swevent_ctx_event(struct perf_event_context *ctx,
3909 enum perf_type_id type,
3910 u32 event_id, u64 nr, int nmi,
3911 struct perf_sample_data *data,
3912 struct pt_regs *regs)
3913{
3914 struct perf_event *event;
3915
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003916 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08003917 if (perf_swevent_match(event, type, event_id, data, regs))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003918 perf_swevent_add(event, nr, nmi, data, regs);
3919 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003920}
3921
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003922int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003923{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003924 struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
3925 int rctx;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003926
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003927 if (in_nmi())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003928 rctx = 3;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003929 else if (in_irq())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003930 rctx = 2;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003931 else if (in_softirq())
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003932 rctx = 1;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003933 else
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003934 rctx = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003935
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003936 if (cpuctx->recursion[rctx]) {
3937 put_cpu_var(perf_cpu_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003938 return -1;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003939 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003940
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003941 cpuctx->recursion[rctx]++;
3942 barrier();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003943
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003944 return rctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003945}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01003946EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003947
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003948void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003949{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003950 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
3951 barrier();
Frederic Weisbeckerfe612672009-11-24 20:38:22 +01003952 cpuctx->recursion[rctx]--;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003953 put_cpu_var(perf_cpu_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003954}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01003955EXPORT_SYMBOL_GPL(perf_swevent_put_recursion_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003956
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003957static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
3958 u64 nr, int nmi,
3959 struct perf_sample_data *data,
3960 struct pt_regs *regs)
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003961{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003962 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003963 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003964
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003965 cpuctx = &__get_cpu_var(perf_cpu_context);
Peter Zijlstra81520182009-11-20 22:19:45 +01003966 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003967 perf_swevent_ctx_event(&cpuctx->ctx, type, event_id,
3968 nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003969 /*
3970 * doesn't really matter which of the child contexts the
3971 * events ends up in.
3972 */
3973 ctx = rcu_dereference(current->perf_event_ctxp);
3974 if (ctx)
3975 perf_swevent_ctx_event(ctx, type, event_id, nr, nmi, data, regs);
3976 rcu_read_unlock();
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01003977}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003978
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003979void __perf_sw_event(u32 event_id, u64 nr, int nmi,
3980 struct pt_regs *regs, u64 addr)
3981{
Ingo Molnara4234bf2009-11-23 10:57:59 +01003982 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003983 int rctx;
3984
3985 rctx = perf_swevent_get_recursion_context();
3986 if (rctx < 0)
3987 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003988
Ingo Molnara4234bf2009-11-23 10:57:59 +01003989 data.addr = addr;
3990 data.raw = NULL;
3991
3992 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01003993
3994 perf_swevent_put_recursion_context(rctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003995}
3996
3997static void perf_swevent_read(struct perf_event *event)
3998{
3999}
4000
4001static int perf_swevent_enable(struct perf_event *event)
4002{
4003 struct hw_perf_event *hwc = &event->hw;
4004
4005 if (hwc->sample_period) {
4006 hwc->last_period = hwc->sample_period;
4007 perf_swevent_set_period(event);
4008 }
4009 return 0;
4010}
4011
4012static void perf_swevent_disable(struct perf_event *event)
4013{
4014}
4015
4016static const struct pmu perf_ops_generic = {
4017 .enable = perf_swevent_enable,
4018 .disable = perf_swevent_disable,
4019 .read = perf_swevent_read,
4020 .unthrottle = perf_swevent_unthrottle,
4021};
4022
4023/*
4024 * hrtimer based swevent callback
4025 */
4026
4027static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4028{
4029 enum hrtimer_restart ret = HRTIMER_RESTART;
4030 struct perf_sample_data data;
4031 struct pt_regs *regs;
4032 struct perf_event *event;
4033 u64 period;
4034
4035 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4036 event->pmu->read(event);
4037
4038 data.addr = 0;
Xiao Guangrong21140f42009-12-10 14:00:51 +08004039 data.raw = NULL;
Xiao Guangrong59d069e2009-12-01 17:30:08 +08004040 data.period = event->hw.last_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004041 regs = get_irq_regs();
4042 /*
4043 * In case we exclude kernel IPs or are somehow not in interrupt
4044 * context, provide the next best thing, the user IP.
4045 */
4046 if ((event->attr.exclude_kernel || !regs) &&
4047 !event->attr.exclude_user)
4048 regs = task_pt_regs(current);
4049
4050 if (regs) {
Soeren Sandmann54f44072009-10-22 18:34:08 +02004051 if (!(event->attr.exclude_idle && current->pid == 0))
4052 if (perf_event_overflow(event, 0, &data, regs))
4053 ret = HRTIMER_NORESTART;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004054 }
4055
4056 period = max_t(u64, 10000, event->hw.sample_period);
4057 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4058
4059 return ret;
4060}
4061
Soeren Sandmann721a6692009-09-15 14:33:08 +02004062static void perf_swevent_start_hrtimer(struct perf_event *event)
4063{
4064 struct hw_perf_event *hwc = &event->hw;
4065
4066 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4067 hwc->hrtimer.function = perf_swevent_hrtimer;
4068 if (hwc->sample_period) {
4069 u64 period;
4070
4071 if (hwc->remaining) {
4072 if (hwc->remaining < 0)
4073 period = 10000;
4074 else
4075 period = hwc->remaining;
4076 hwc->remaining = 0;
4077 } else {
4078 period = max_t(u64, 10000, hwc->sample_period);
4079 }
4080 __hrtimer_start_range_ns(&hwc->hrtimer,
4081 ns_to_ktime(period), 0,
4082 HRTIMER_MODE_REL, 0);
4083 }
4084}
4085
4086static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4087{
4088 struct hw_perf_event *hwc = &event->hw;
4089
4090 if (hwc->sample_period) {
4091 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4092 hwc->remaining = ktime_to_ns(remaining);
4093
4094 hrtimer_cancel(&hwc->hrtimer);
4095 }
4096}
4097
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004098/*
4099 * Software event: cpu wall time clock
4100 */
4101
4102static void cpu_clock_perf_event_update(struct perf_event *event)
4103{
4104 int cpu = raw_smp_processor_id();
4105 s64 prev;
4106 u64 now;
4107
4108 now = cpu_clock(cpu);
Xiao Guangrongec89a062009-12-09 11:30:36 +08004109 prev = atomic64_xchg(&event->hw.prev_count, now);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004110 atomic64_add(now - prev, &event->count);
4111}
4112
4113static int cpu_clock_perf_event_enable(struct perf_event *event)
4114{
4115 struct hw_perf_event *hwc = &event->hw;
4116 int cpu = raw_smp_processor_id();
4117
4118 atomic64_set(&hwc->prev_count, cpu_clock(cpu));
Soeren Sandmann721a6692009-09-15 14:33:08 +02004119 perf_swevent_start_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004120
4121 return 0;
4122}
4123
4124static void cpu_clock_perf_event_disable(struct perf_event *event)
4125{
Soeren Sandmann721a6692009-09-15 14:33:08 +02004126 perf_swevent_cancel_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004127 cpu_clock_perf_event_update(event);
4128}
4129
4130static void cpu_clock_perf_event_read(struct perf_event *event)
4131{
4132 cpu_clock_perf_event_update(event);
4133}
4134
4135static const struct pmu perf_ops_cpu_clock = {
4136 .enable = cpu_clock_perf_event_enable,
4137 .disable = cpu_clock_perf_event_disable,
4138 .read = cpu_clock_perf_event_read,
4139};
4140
4141/*
4142 * Software event: task time clock
4143 */
4144
4145static void task_clock_perf_event_update(struct perf_event *event, u64 now)
4146{
4147 u64 prev;
4148 s64 delta;
4149
4150 prev = atomic64_xchg(&event->hw.prev_count, now);
4151 delta = now - prev;
4152 atomic64_add(delta, &event->count);
4153}
4154
4155static int task_clock_perf_event_enable(struct perf_event *event)
4156{
4157 struct hw_perf_event *hwc = &event->hw;
4158 u64 now;
4159
4160 now = event->ctx->time;
4161
4162 atomic64_set(&hwc->prev_count, now);
Soeren Sandmann721a6692009-09-15 14:33:08 +02004163
4164 perf_swevent_start_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004165
4166 return 0;
4167}
4168
4169static void task_clock_perf_event_disable(struct perf_event *event)
4170{
Soeren Sandmann721a6692009-09-15 14:33:08 +02004171 perf_swevent_cancel_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004172 task_clock_perf_event_update(event, event->ctx->time);
4173
4174}
4175
4176static void task_clock_perf_event_read(struct perf_event *event)
4177{
4178 u64 time;
4179
4180 if (!in_nmi()) {
4181 update_context_time(event->ctx);
4182 time = event->ctx->time;
4183 } else {
4184 u64 now = perf_clock();
4185 u64 delta = now - event->ctx->timestamp;
4186 time = event->ctx->time + delta;
4187 }
4188
4189 task_clock_perf_event_update(event, time);
4190}
4191
4192static const struct pmu perf_ops_task_clock = {
4193 .enable = task_clock_perf_event_enable,
4194 .disable = task_clock_perf_event_disable,
4195 .read = task_clock_perf_event_read,
4196};
4197
Li Zefan07b139c2009-12-21 14:27:35 +08004198#ifdef CONFIG_EVENT_TRACING
Li Zefan6fb29152009-10-15 11:21:42 +08004199
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004200void perf_tp_event(int event_id, u64 addr, u64 count, void *record,
4201 int entry_size)
4202{
4203 struct perf_raw_record raw = {
4204 .size = entry_size,
4205 .data = record,
4206 };
4207
4208 struct perf_sample_data data = {
4209 .addr = addr,
4210 .raw = &raw,
4211 };
4212
4213 struct pt_regs *regs = get_irq_regs();
4214
4215 if (!regs)
4216 regs = task_pt_regs(current);
4217
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004218 /* Trace events already protected against recursion */
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004219 do_perf_sw_event(PERF_TYPE_TRACEPOINT, event_id, count, 1,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004220 &data, regs);
4221}
4222EXPORT_SYMBOL_GPL(perf_tp_event);
4223
Li Zefan6fb29152009-10-15 11:21:42 +08004224static int perf_tp_event_match(struct perf_event *event,
4225 struct perf_sample_data *data)
4226{
4227 void *record = data->raw->data;
4228
4229 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4230 return 1;
4231 return 0;
4232}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004233
4234static void tp_perf_event_destroy(struct perf_event *event)
4235{
4236 ftrace_profile_disable(event->attr.config);
4237}
4238
4239static const struct pmu *tp_perf_event_init(struct perf_event *event)
4240{
4241 /*
4242 * Raw tracepoint data is a severe data leak, only allow root to
4243 * have these.
4244 */
4245 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4246 perf_paranoid_tracepoint_raw() &&
4247 !capable(CAP_SYS_ADMIN))
4248 return ERR_PTR(-EPERM);
4249
4250 if (ftrace_profile_enable(event->attr.config))
4251 return NULL;
4252
4253 event->destroy = tp_perf_event_destroy;
4254
4255 return &perf_ops_generic;
4256}
Li Zefan6fb29152009-10-15 11:21:42 +08004257
4258static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4259{
4260 char *filter_str;
4261 int ret;
4262
4263 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4264 return -EINVAL;
4265
4266 filter_str = strndup_user(arg, PAGE_SIZE);
4267 if (IS_ERR(filter_str))
4268 return PTR_ERR(filter_str);
4269
4270 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4271
4272 kfree(filter_str);
4273 return ret;
4274}
4275
4276static void perf_event_free_filter(struct perf_event *event)
4277{
4278 ftrace_profile_free_filter(event);
4279}
4280
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004281#else
Li Zefan6fb29152009-10-15 11:21:42 +08004282
4283static int perf_tp_event_match(struct perf_event *event,
4284 struct perf_sample_data *data)
4285{
4286 return 1;
4287}
4288
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004289static const struct pmu *tp_perf_event_init(struct perf_event *event)
4290{
4291 return NULL;
4292}
Li Zefan6fb29152009-10-15 11:21:42 +08004293
4294static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4295{
4296 return -ENOENT;
4297}
4298
4299static void perf_event_free_filter(struct perf_event *event)
4300{
4301}
4302
Li Zefan07b139c2009-12-21 14:27:35 +08004303#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004304
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004305#ifdef CONFIG_HAVE_HW_BREAKPOINT
4306static void bp_perf_event_destroy(struct perf_event *event)
4307{
4308 release_bp_slot(event);
4309}
4310
4311static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4312{
4313 int err;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004314
4315 err = register_perf_hw_breakpoint(bp);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004316 if (err)
4317 return ERR_PTR(err);
4318
4319 bp->destroy = bp_perf_event_destroy;
4320
4321 return &perf_ops_bp;
4322}
4323
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004324void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004325{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004326 struct perf_sample_data sample;
4327 struct pt_regs *regs = data;
4328
Xiao Guangrong5e855db2009-12-10 17:08:54 +08004329 sample.raw = NULL;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004330 sample.addr = bp->attr.bp_addr;
4331
4332 if (!perf_exclude_event(bp, regs))
4333 perf_swevent_add(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004334}
4335#else
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004336static const struct pmu *bp_perf_event_init(struct perf_event *bp)
4337{
4338 return NULL;
4339}
4340
4341void perf_bp_event(struct perf_event *bp, void *regs)
4342{
4343}
4344#endif
4345
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004346atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4347
4348static void sw_perf_event_destroy(struct perf_event *event)
4349{
4350 u64 event_id = event->attr.config;
4351
4352 WARN_ON(event->parent);
4353
4354 atomic_dec(&perf_swevent_enabled[event_id]);
4355}
4356
4357static const struct pmu *sw_perf_event_init(struct perf_event *event)
4358{
4359 const struct pmu *pmu = NULL;
4360 u64 event_id = event->attr.config;
4361
4362 /*
4363 * Software events (currently) can't in general distinguish
4364 * between user, kernel and hypervisor events.
4365 * However, context switches and cpu migrations are considered
4366 * to be kernel events, and page faults are never hypervisor
4367 * events.
4368 */
4369 switch (event_id) {
4370 case PERF_COUNT_SW_CPU_CLOCK:
4371 pmu = &perf_ops_cpu_clock;
4372
4373 break;
4374 case PERF_COUNT_SW_TASK_CLOCK:
4375 /*
4376 * If the user instantiates this as a per-cpu event,
4377 * use the cpu_clock event instead.
4378 */
4379 if (event->ctx->task)
4380 pmu = &perf_ops_task_clock;
4381 else
4382 pmu = &perf_ops_cpu_clock;
4383
4384 break;
4385 case PERF_COUNT_SW_PAGE_FAULTS:
4386 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4387 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4388 case PERF_COUNT_SW_CONTEXT_SWITCHES:
4389 case PERF_COUNT_SW_CPU_MIGRATIONS:
Anton Blanchardf7d79862009-10-18 01:09:29 +00004390 case PERF_COUNT_SW_ALIGNMENT_FAULTS:
4391 case PERF_COUNT_SW_EMULATION_FAULTS:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004392 if (!event->parent) {
4393 atomic_inc(&perf_swevent_enabled[event_id]);
4394 event->destroy = sw_perf_event_destroy;
4395 }
4396 pmu = &perf_ops_generic;
4397 break;
4398 }
4399
4400 return pmu;
4401}
4402
4403/*
4404 * Allocate and initialize a event structure
4405 */
4406static struct perf_event *
4407perf_event_alloc(struct perf_event_attr *attr,
4408 int cpu,
4409 struct perf_event_context *ctx,
4410 struct perf_event *group_leader,
4411 struct perf_event *parent_event,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004412 perf_overflow_handler_t overflow_handler,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004413 gfp_t gfpflags)
4414{
4415 const struct pmu *pmu;
4416 struct perf_event *event;
4417 struct hw_perf_event *hwc;
4418 long err;
4419
4420 event = kzalloc(sizeof(*event), gfpflags);
4421 if (!event)
4422 return ERR_PTR(-ENOMEM);
4423
4424 /*
4425 * Single events are their own group leaders, with an
4426 * empty sibling list:
4427 */
4428 if (!group_leader)
4429 group_leader = event;
4430
4431 mutex_init(&event->child_mutex);
4432 INIT_LIST_HEAD(&event->child_list);
4433
4434 INIT_LIST_HEAD(&event->group_entry);
4435 INIT_LIST_HEAD(&event->event_entry);
4436 INIT_LIST_HEAD(&event->sibling_list);
4437 init_waitqueue_head(&event->waitq);
4438
4439 mutex_init(&event->mmap_mutex);
4440
4441 event->cpu = cpu;
4442 event->attr = *attr;
4443 event->group_leader = group_leader;
4444 event->pmu = NULL;
4445 event->ctx = ctx;
4446 event->oncpu = -1;
4447
4448 event->parent = parent_event;
4449
4450 event->ns = get_pid_ns(current->nsproxy->pid_ns);
4451 event->id = atomic64_inc_return(&perf_event_id);
4452
4453 event->state = PERF_EVENT_STATE_INACTIVE;
4454
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004455 if (!overflow_handler && parent_event)
4456 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004457
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004458 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004459
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004460 if (attr->disabled)
4461 event->state = PERF_EVENT_STATE_OFF;
4462
4463 pmu = NULL;
4464
4465 hwc = &event->hw;
4466 hwc->sample_period = attr->sample_period;
4467 if (attr->freq && attr->sample_freq)
4468 hwc->sample_period = 1;
4469 hwc->last_period = hwc->sample_period;
4470
4471 atomic64_set(&hwc->period_left, hwc->sample_period);
4472
4473 /*
4474 * we currently do not support PERF_FORMAT_GROUP on inherited events
4475 */
4476 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
4477 goto done;
4478
4479 switch (attr->type) {
4480 case PERF_TYPE_RAW:
4481 case PERF_TYPE_HARDWARE:
4482 case PERF_TYPE_HW_CACHE:
4483 pmu = hw_perf_event_init(event);
4484 break;
4485
4486 case PERF_TYPE_SOFTWARE:
4487 pmu = sw_perf_event_init(event);
4488 break;
4489
4490 case PERF_TYPE_TRACEPOINT:
4491 pmu = tp_perf_event_init(event);
4492 break;
4493
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004494 case PERF_TYPE_BREAKPOINT:
4495 pmu = bp_perf_event_init(event);
4496 break;
4497
4498
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004499 default:
4500 break;
4501 }
4502done:
4503 err = 0;
4504 if (!pmu)
4505 err = -EINVAL;
4506 else if (IS_ERR(pmu))
4507 err = PTR_ERR(pmu);
4508
4509 if (err) {
4510 if (event->ns)
4511 put_pid_ns(event->ns);
4512 kfree(event);
4513 return ERR_PTR(err);
4514 }
4515
4516 event->pmu = pmu;
4517
4518 if (!event->parent) {
4519 atomic_inc(&nr_events);
4520 if (event->attr.mmap)
4521 atomic_inc(&nr_mmap_events);
4522 if (event->attr.comm)
4523 atomic_inc(&nr_comm_events);
4524 if (event->attr.task)
4525 atomic_inc(&nr_task_events);
4526 }
4527
4528 return event;
4529}
4530
4531static int perf_copy_attr(struct perf_event_attr __user *uattr,
4532 struct perf_event_attr *attr)
4533{
4534 u32 size;
4535 int ret;
4536
4537 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
4538 return -EFAULT;
4539
4540 /*
4541 * zero the full structure, so that a short copy will be nice.
4542 */
4543 memset(attr, 0, sizeof(*attr));
4544
4545 ret = get_user(size, &uattr->size);
4546 if (ret)
4547 return ret;
4548
4549 if (size > PAGE_SIZE) /* silly large */
4550 goto err_size;
4551
4552 if (!size) /* abi compat */
4553 size = PERF_ATTR_SIZE_VER0;
4554
4555 if (size < PERF_ATTR_SIZE_VER0)
4556 goto err_size;
4557
4558 /*
4559 * If we're handed a bigger struct than we know of,
4560 * ensure all the unknown bits are 0 - i.e. new
4561 * user-space does not rely on any kernel feature
4562 * extensions we dont know about yet.
4563 */
4564 if (size > sizeof(*attr)) {
4565 unsigned char __user *addr;
4566 unsigned char __user *end;
4567 unsigned char val;
4568
4569 addr = (void __user *)uattr + sizeof(*attr);
4570 end = (void __user *)uattr + size;
4571
4572 for (; addr < end; addr++) {
4573 ret = get_user(val, addr);
4574 if (ret)
4575 return ret;
4576 if (val)
4577 goto err_size;
4578 }
4579 size = sizeof(*attr);
4580 }
4581
4582 ret = copy_from_user(attr, uattr, size);
4583 if (ret)
4584 return -EFAULT;
4585
4586 /*
4587 * If the type exists, the corresponding creation will verify
4588 * the attr->config.
4589 */
4590 if (attr->type >= PERF_TYPE_MAX)
4591 return -EINVAL;
4592
Peter Zijlstraf13c12c2009-12-15 19:43:11 +01004593 if (attr->__reserved_1 || attr->__reserved_2)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004594 return -EINVAL;
4595
4596 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
4597 return -EINVAL;
4598
4599 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
4600 return -EINVAL;
4601
4602out:
4603 return ret;
4604
4605err_size:
4606 put_user(sizeof(*attr), &uattr->size);
4607 ret = -E2BIG;
4608 goto out;
4609}
4610
Li Zefan6fb29152009-10-15 11:21:42 +08004611static int perf_event_set_output(struct perf_event *event, int output_fd)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004612{
4613 struct perf_event *output_event = NULL;
4614 struct file *output_file = NULL;
4615 struct perf_event *old_output;
4616 int fput_needed = 0;
4617 int ret = -EINVAL;
4618
4619 if (!output_fd)
4620 goto set;
4621
4622 output_file = fget_light(output_fd, &fput_needed);
4623 if (!output_file)
4624 return -EBADF;
4625
4626 if (output_file->f_op != &perf_fops)
4627 goto out;
4628
4629 output_event = output_file->private_data;
4630
4631 /* Don't chain output fds */
4632 if (output_event->output)
4633 goto out;
4634
4635 /* Don't set an output fd when we already have an output channel */
4636 if (event->data)
4637 goto out;
4638
4639 atomic_long_inc(&output_file->f_count);
4640
4641set:
4642 mutex_lock(&event->mmap_mutex);
4643 old_output = event->output;
4644 rcu_assign_pointer(event->output, output_event);
4645 mutex_unlock(&event->mmap_mutex);
4646
4647 if (old_output) {
4648 /*
4649 * we need to make sure no existing perf_output_*()
4650 * is still referencing this event.
4651 */
4652 synchronize_rcu();
4653 fput(old_output->filp);
4654 }
4655
4656 ret = 0;
4657out:
4658 fput_light(output_file, fput_needed);
4659 return ret;
4660}
4661
4662/**
4663 * sys_perf_event_open - open a performance event, associate it to a task/cpu
4664 *
4665 * @attr_uptr: event_id type attributes for monitoring/sampling
4666 * @pid: target pid
4667 * @cpu: target cpu
4668 * @group_fd: group leader event fd
4669 */
4670SYSCALL_DEFINE5(perf_event_open,
4671 struct perf_event_attr __user *, attr_uptr,
4672 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
4673{
4674 struct perf_event *event, *group_leader;
4675 struct perf_event_attr attr;
4676 struct perf_event_context *ctx;
4677 struct file *event_file = NULL;
4678 struct file *group_file = NULL;
4679 int fput_needed = 0;
4680 int fput_needed2 = 0;
4681 int err;
4682
4683 /* for future expandability... */
4684 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
4685 return -EINVAL;
4686
4687 err = perf_copy_attr(attr_uptr, &attr);
4688 if (err)
4689 return err;
4690
4691 if (!attr.exclude_kernel) {
4692 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
4693 return -EACCES;
4694 }
4695
4696 if (attr.freq) {
4697 if (attr.sample_freq > sysctl_perf_event_sample_rate)
4698 return -EINVAL;
4699 }
4700
4701 /*
4702 * Get the target context (task or percpu):
4703 */
4704 ctx = find_get_context(pid, cpu);
4705 if (IS_ERR(ctx))
4706 return PTR_ERR(ctx);
4707
4708 /*
4709 * Look up the group leader (we will attach this event to it):
4710 */
4711 group_leader = NULL;
4712 if (group_fd != -1 && !(flags & PERF_FLAG_FD_NO_GROUP)) {
4713 err = -EINVAL;
4714 group_file = fget_light(group_fd, &fput_needed);
4715 if (!group_file)
4716 goto err_put_context;
4717 if (group_file->f_op != &perf_fops)
4718 goto err_put_context;
4719
4720 group_leader = group_file->private_data;
4721 /*
4722 * Do not allow a recursive hierarchy (this new sibling
4723 * becoming part of another group-sibling):
4724 */
4725 if (group_leader->group_leader != group_leader)
4726 goto err_put_context;
4727 /*
4728 * Do not allow to attach to a group in a different
4729 * task or CPU context:
4730 */
4731 if (group_leader->ctx != ctx)
4732 goto err_put_context;
4733 /*
4734 * Only a group leader can be exclusive or pinned
4735 */
4736 if (attr.exclusive || attr.pinned)
4737 goto err_put_context;
4738 }
4739
4740 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004741 NULL, NULL, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004742 err = PTR_ERR(event);
4743 if (IS_ERR(event))
4744 goto err_put_context;
4745
Roland Dreier628ff7c2009-12-18 09:41:24 -08004746 err = anon_inode_getfd("[perf_event]", &perf_fops, event, O_RDWR);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004747 if (err < 0)
4748 goto err_free_put_context;
4749
4750 event_file = fget_light(err, &fput_needed2);
4751 if (!event_file)
4752 goto err_free_put_context;
4753
4754 if (flags & PERF_FLAG_FD_OUTPUT) {
4755 err = perf_event_set_output(event, group_fd);
4756 if (err)
4757 goto err_fput_free_put_context;
4758 }
4759
4760 event->filp = event_file;
4761 WARN_ON_ONCE(ctx->parent_ctx);
4762 mutex_lock(&ctx->mutex);
4763 perf_install_in_context(ctx, event, cpu);
4764 ++ctx->generation;
4765 mutex_unlock(&ctx->mutex);
4766
4767 event->owner = current;
4768 get_task_struct(current);
4769 mutex_lock(&current->perf_event_mutex);
4770 list_add_tail(&event->owner_entry, &current->perf_event_list);
4771 mutex_unlock(&current->perf_event_mutex);
4772
4773err_fput_free_put_context:
4774 fput_light(event_file, fput_needed2);
4775
4776err_free_put_context:
4777 if (err < 0)
4778 kfree(event);
4779
4780err_put_context:
4781 if (err < 0)
4782 put_ctx(ctx);
4783
4784 fput_light(group_file, fput_needed);
4785
4786 return err;
4787}
4788
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004789/**
4790 * perf_event_create_kernel_counter
4791 *
4792 * @attr: attributes of the counter to create
4793 * @cpu: cpu in which the counter is bound
4794 * @pid: task to profile
4795 */
4796struct perf_event *
4797perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004798 pid_t pid,
4799 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004800{
4801 struct perf_event *event;
4802 struct perf_event_context *ctx;
4803 int err;
4804
4805 /*
4806 * Get the target context (task or percpu):
4807 */
4808
4809 ctx = find_get_context(pid, cpu);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01004810 if (IS_ERR(ctx)) {
4811 err = PTR_ERR(ctx);
4812 goto err_exit;
4813 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004814
4815 event = perf_event_alloc(attr, cpu, ctx, NULL,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004816 NULL, overflow_handler, GFP_KERNEL);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01004817 if (IS_ERR(event)) {
4818 err = PTR_ERR(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004819 goto err_put_context;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01004820 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004821
4822 event->filp = NULL;
4823 WARN_ON_ONCE(ctx->parent_ctx);
4824 mutex_lock(&ctx->mutex);
4825 perf_install_in_context(ctx, event, cpu);
4826 ++ctx->generation;
4827 mutex_unlock(&ctx->mutex);
4828
4829 event->owner = current;
4830 get_task_struct(current);
4831 mutex_lock(&current->perf_event_mutex);
4832 list_add_tail(&event->owner_entry, &current->perf_event_list);
4833 mutex_unlock(&current->perf_event_mutex);
4834
4835 return event;
4836
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01004837 err_put_context:
4838 put_ctx(ctx);
4839 err_exit:
4840 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02004841}
4842EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
4843
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004844/*
4845 * inherit a event from parent task to child task:
4846 */
4847static struct perf_event *
4848inherit_event(struct perf_event *parent_event,
4849 struct task_struct *parent,
4850 struct perf_event_context *parent_ctx,
4851 struct task_struct *child,
4852 struct perf_event *group_leader,
4853 struct perf_event_context *child_ctx)
4854{
4855 struct perf_event *child_event;
4856
4857 /*
4858 * Instead of creating recursive hierarchies of events,
4859 * we link inherited events back to the original parent,
4860 * which has a filp for sure, which we use as the reference
4861 * count:
4862 */
4863 if (parent_event->parent)
4864 parent_event = parent_event->parent;
4865
4866 child_event = perf_event_alloc(&parent_event->attr,
4867 parent_event->cpu, child_ctx,
4868 group_leader, parent_event,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02004869 NULL, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004870 if (IS_ERR(child_event))
4871 return child_event;
4872 get_ctx(child_ctx);
4873
4874 /*
4875 * Make the child state follow the state of the parent event,
4876 * not its attr.disabled bit. We hold the parent's mutex,
4877 * so we won't race with perf_event_{en, dis}able_family.
4878 */
4879 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
4880 child_event->state = PERF_EVENT_STATE_INACTIVE;
4881 else
4882 child_event->state = PERF_EVENT_STATE_OFF;
4883
4884 if (parent_event->attr.freq)
4885 child_event->hw.sample_period = parent_event->hw.sample_period;
4886
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004887 child_event->overflow_handler = parent_event->overflow_handler;
4888
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004889 /*
4890 * Link it up in the child's context:
4891 */
4892 add_event_to_ctx(child_event, child_ctx);
4893
4894 /*
4895 * Get a reference to the parent filp - we will fput it
4896 * when the child event exits. This is safe to do because
4897 * we are in the parent and we know that the filp still
4898 * exists and has a nonzero count:
4899 */
4900 atomic_long_inc(&parent_event->filp->f_count);
4901
4902 /*
4903 * Link this into the parent event's child list
4904 */
4905 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
4906 mutex_lock(&parent_event->child_mutex);
4907 list_add_tail(&child_event->child_list, &parent_event->child_list);
4908 mutex_unlock(&parent_event->child_mutex);
4909
4910 return child_event;
4911}
4912
4913static int inherit_group(struct perf_event *parent_event,
4914 struct task_struct *parent,
4915 struct perf_event_context *parent_ctx,
4916 struct task_struct *child,
4917 struct perf_event_context *child_ctx)
4918{
4919 struct perf_event *leader;
4920 struct perf_event *sub;
4921 struct perf_event *child_ctr;
4922
4923 leader = inherit_event(parent_event, parent, parent_ctx,
4924 child, NULL, child_ctx);
4925 if (IS_ERR(leader))
4926 return PTR_ERR(leader);
4927 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
4928 child_ctr = inherit_event(sub, parent, parent_ctx,
4929 child, leader, child_ctx);
4930 if (IS_ERR(child_ctr))
4931 return PTR_ERR(child_ctr);
4932 }
4933 return 0;
4934}
4935
4936static void sync_child_event(struct perf_event *child_event,
4937 struct task_struct *child)
4938{
4939 struct perf_event *parent_event = child_event->parent;
4940 u64 child_val;
4941
4942 if (child_event->attr.inherit_stat)
4943 perf_event_read_event(child_event, child);
4944
4945 child_val = atomic64_read(&child_event->count);
4946
4947 /*
4948 * Add back the child's count to the parent's count:
4949 */
4950 atomic64_add(child_val, &parent_event->count);
4951 atomic64_add(child_event->total_time_enabled,
4952 &parent_event->child_total_time_enabled);
4953 atomic64_add(child_event->total_time_running,
4954 &parent_event->child_total_time_running);
4955
4956 /*
4957 * Remove this event from the parent's list
4958 */
4959 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
4960 mutex_lock(&parent_event->child_mutex);
4961 list_del_init(&child_event->child_list);
4962 mutex_unlock(&parent_event->child_mutex);
4963
4964 /*
4965 * Release the parent event, if this was the last
4966 * reference to it.
4967 */
4968 fput(parent_event->filp);
4969}
4970
4971static void
4972__perf_event_exit_task(struct perf_event *child_event,
4973 struct perf_event_context *child_ctx,
4974 struct task_struct *child)
4975{
4976 struct perf_event *parent_event;
4977
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004978 perf_event_remove_from_context(child_event);
4979
4980 parent_event = child_event->parent;
4981 /*
4982 * It can happen that parent exits first, and has events
4983 * that are still around due to the child reference. These
4984 * events need to be zapped - but otherwise linger.
4985 */
4986 if (parent_event) {
4987 sync_child_event(child_event, child);
4988 free_event(child_event);
4989 }
4990}
4991
4992/*
4993 * When a child task exits, feed back event values to parent events.
4994 */
4995void perf_event_exit_task(struct task_struct *child)
4996{
4997 struct perf_event *child_event, *tmp;
4998 struct perf_event_context *child_ctx;
4999 unsigned long flags;
5000
5001 if (likely(!child->perf_event_ctxp)) {
5002 perf_event_task(child, NULL, 0);
5003 return;
5004 }
5005
5006 local_irq_save(flags);
5007 /*
5008 * We can't reschedule here because interrupts are disabled,
5009 * and either child is current or it is a task that can't be
5010 * scheduled, so we are now safe from rescheduling changing
5011 * our context.
5012 */
5013 child_ctx = child->perf_event_ctxp;
5014 __perf_event_task_sched_out(child_ctx);
5015
5016 /*
5017 * Take the context lock here so that if find_get_context is
5018 * reading child->perf_event_ctxp, we wait until it has
5019 * incremented the context's refcount before we do put_ctx below.
5020 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005021 raw_spin_lock(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005022 child->perf_event_ctxp = NULL;
5023 /*
5024 * If this context is a clone; unclone it so it can't get
5025 * swapped to another process while we're removing all
5026 * the events from it.
5027 */
5028 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01005029 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005030 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005031
5032 /*
5033 * Report the task dead after unscheduling the events so that we
5034 * won't get any samples after PERF_RECORD_EXIT. We can however still
5035 * get a few PERF_RECORD_READ events.
5036 */
5037 perf_event_task(child, child_ctx, 0);
5038
5039 /*
5040 * We can recurse on the same lock type through:
5041 *
5042 * __perf_event_exit_task()
5043 * sync_child_event()
5044 * fput(parent_event->filp)
5045 * perf_release()
5046 * mutex_lock(&ctx->mutex)
5047 *
5048 * But since its the parent context it won't be the same instance.
5049 */
5050 mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
5051
5052again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005053 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5054 group_entry)
5055 __perf_event_exit_task(child_event, child_ctx, child);
5056
5057 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005058 group_entry)
5059 __perf_event_exit_task(child_event, child_ctx, child);
5060
5061 /*
5062 * If the last event was a group event, it will have appended all
5063 * its siblings to the list, but we obtained 'tmp' before that which
5064 * will still point to the list head terminating the iteration.
5065 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005066 if (!list_empty(&child_ctx->pinned_groups) ||
5067 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005068 goto again;
5069
5070 mutex_unlock(&child_ctx->mutex);
5071
5072 put_ctx(child_ctx);
5073}
5074
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005075static void perf_free_event(struct perf_event *event,
5076 struct perf_event_context *ctx)
5077{
5078 struct perf_event *parent = event->parent;
5079
5080 if (WARN_ON_ONCE(!parent))
5081 return;
5082
5083 mutex_lock(&parent->child_mutex);
5084 list_del_init(&event->child_list);
5085 mutex_unlock(&parent->child_mutex);
5086
5087 fput(parent->filp);
5088
5089 list_del_event(event, ctx);
5090 free_event(event);
5091}
5092
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005093/*
5094 * free an unexposed, unused context as created by inheritance by
5095 * init_task below, used by fork() in case of fail.
5096 */
5097void perf_event_free_task(struct task_struct *task)
5098{
5099 struct perf_event_context *ctx = task->perf_event_ctxp;
5100 struct perf_event *event, *tmp;
5101
5102 if (!ctx)
5103 return;
5104
5105 mutex_lock(&ctx->mutex);
5106again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005107 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5108 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005109
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005110 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5111 group_entry)
5112 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005113
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005114 if (!list_empty(&ctx->pinned_groups) ||
5115 !list_empty(&ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005116 goto again;
5117
5118 mutex_unlock(&ctx->mutex);
5119
5120 put_ctx(ctx);
5121}
5122
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005123static int
5124inherit_task_group(struct perf_event *event, struct task_struct *parent,
5125 struct perf_event_context *parent_ctx,
5126 struct task_struct *child,
5127 int *inherited_all)
5128{
5129 int ret;
5130 struct perf_event_context *child_ctx = child->perf_event_ctxp;
5131
5132 if (!event->attr.inherit) {
5133 *inherited_all = 0;
5134 return 0;
5135 }
5136
5137 if (!child_ctx) {
5138 /*
5139 * This is executed from the parent task context, so
5140 * inherit events that have been marked for cloning.
5141 * First allocate and initialize a context for the
5142 * child.
5143 */
5144
5145 child_ctx = kzalloc(sizeof(struct perf_event_context),
5146 GFP_KERNEL);
5147 if (!child_ctx)
5148 return -ENOMEM;
5149
5150 __perf_event_init_context(child_ctx, child);
5151 child->perf_event_ctxp = child_ctx;
5152 get_task_struct(child);
5153 }
5154
5155 ret = inherit_group(event, parent, parent_ctx,
5156 child, child_ctx);
5157
5158 if (ret)
5159 *inherited_all = 0;
5160
5161 return ret;
5162}
5163
5164
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005165/*
5166 * Initialize the perf_event context in task_struct
5167 */
5168int perf_event_init_task(struct task_struct *child)
5169{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005170 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005171 struct perf_event_context *cloned_ctx;
5172 struct perf_event *event;
5173 struct task_struct *parent = current;
5174 int inherited_all = 1;
5175 int ret = 0;
5176
5177 child->perf_event_ctxp = NULL;
5178
5179 mutex_init(&child->perf_event_mutex);
5180 INIT_LIST_HEAD(&child->perf_event_list);
5181
5182 if (likely(!parent->perf_event_ctxp))
5183 return 0;
5184
5185 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005186 * If the parent's context is a clone, pin it so it won't get
5187 * swapped under us.
5188 */
5189 parent_ctx = perf_pin_task_context(parent);
5190
5191 /*
5192 * No need to check if parent_ctx != NULL here; since we saw
5193 * it non-NULL earlier, the only reason for it to become NULL
5194 * is if we exit, and since we're currently in the middle of
5195 * a fork we can't be exiting at the same time.
5196 */
5197
5198 /*
5199 * Lock the parent list. No need to lock the child - not PID
5200 * hashed yet and not running, so nobody can access it.
5201 */
5202 mutex_lock(&parent_ctx->mutex);
5203
5204 /*
5205 * We dont have to disable NMIs - we are only looking at
5206 * the list, not manipulating it:
5207 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005208 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
5209 ret = inherit_task_group(event, parent, parent_ctx, child,
5210 &inherited_all);
5211 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005212 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005213 }
5214
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005215 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
5216 ret = inherit_task_group(event, parent, parent_ctx, child,
5217 &inherited_all);
5218 if (ret)
5219 break;
5220 }
5221
5222 child_ctx = child->perf_event_ctxp;
5223
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01005224 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005225 /*
5226 * Mark the child context as a clone of the parent
5227 * context, or of whatever the parent is a clone of.
5228 * Note that if the parent is a clone, it could get
5229 * uncloned at any point, but that doesn't matter
5230 * because the list of events and the generation
5231 * count can't have changed since we took the mutex.
5232 */
5233 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5234 if (cloned_ctx) {
5235 child_ctx->parent_ctx = cloned_ctx;
5236 child_ctx->parent_gen = parent_ctx->parent_gen;
5237 } else {
5238 child_ctx->parent_ctx = parent_ctx;
5239 child_ctx->parent_gen = parent_ctx->generation;
5240 }
5241 get_ctx(child_ctx->parent_ctx);
5242 }
5243
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005244 mutex_unlock(&parent_ctx->mutex);
5245
5246 perf_unpin_context(parent_ctx);
5247
5248 return ret;
5249}
5250
5251static void __cpuinit perf_event_init_cpu(int cpu)
5252{
5253 struct perf_cpu_context *cpuctx;
5254
5255 cpuctx = &per_cpu(perf_cpu_context, cpu);
5256 __perf_event_init_context(&cpuctx->ctx, NULL);
5257
5258 spin_lock(&perf_resource_lock);
5259 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5260 spin_unlock(&perf_resource_lock);
5261
5262 hw_perf_event_setup(cpu);
5263}
5264
5265#ifdef CONFIG_HOTPLUG_CPU
5266static void __perf_event_exit_cpu(void *info)
5267{
5268 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5269 struct perf_event_context *ctx = &cpuctx->ctx;
5270 struct perf_event *event, *tmp;
5271
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005272 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5273 __perf_event_remove_from_context(event);
5274 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005275 __perf_event_remove_from_context(event);
5276}
5277static void perf_event_exit_cpu(int cpu)
5278{
5279 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5280 struct perf_event_context *ctx = &cpuctx->ctx;
5281
5282 mutex_lock(&ctx->mutex);
5283 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5284 mutex_unlock(&ctx->mutex);
5285}
5286#else
5287static inline void perf_event_exit_cpu(int cpu) { }
5288#endif
5289
5290static int __cpuinit
5291perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5292{
5293 unsigned int cpu = (long)hcpu;
5294
5295 switch (action) {
5296
5297 case CPU_UP_PREPARE:
5298 case CPU_UP_PREPARE_FROZEN:
5299 perf_event_init_cpu(cpu);
5300 break;
5301
5302 case CPU_ONLINE:
5303 case CPU_ONLINE_FROZEN:
5304 hw_perf_event_setup_online(cpu);
5305 break;
5306
5307 case CPU_DOWN_PREPARE:
5308 case CPU_DOWN_PREPARE_FROZEN:
5309 perf_event_exit_cpu(cpu);
5310 break;
5311
5312 default:
5313 break;
5314 }
5315
5316 return NOTIFY_OK;
5317}
5318
5319/*
5320 * This has to have a higher priority than migration_notifier in sched.c.
5321 */
5322static struct notifier_block __cpuinitdata perf_cpu_nb = {
5323 .notifier_call = perf_cpu_notify,
5324 .priority = 20,
5325};
5326
5327void __init perf_event_init(void)
5328{
5329 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
5330 (void *)(long)smp_processor_id());
5331 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
5332 (void *)(long)smp_processor_id());
5333 register_cpu_notifier(&perf_cpu_nb);
5334}
5335
5336static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
5337{
5338 return sprintf(buf, "%d\n", perf_reserved_percpu);
5339}
5340
5341static ssize_t
5342perf_set_reserve_percpu(struct sysdev_class *class,
5343 const char *buf,
5344 size_t count)
5345{
5346 struct perf_cpu_context *cpuctx;
5347 unsigned long val;
5348 int err, cpu, mpt;
5349
5350 err = strict_strtoul(buf, 10, &val);
5351 if (err)
5352 return err;
5353 if (val > perf_max_events)
5354 return -EINVAL;
5355
5356 spin_lock(&perf_resource_lock);
5357 perf_reserved_percpu = val;
5358 for_each_online_cpu(cpu) {
5359 cpuctx = &per_cpu(perf_cpu_context, cpu);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005360 raw_spin_lock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005361 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
5362 perf_max_events - perf_reserved_percpu);
5363 cpuctx->max_pertask = mpt;
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005364 raw_spin_unlock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005365 }
5366 spin_unlock(&perf_resource_lock);
5367
5368 return count;
5369}
5370
5371static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
5372{
5373 return sprintf(buf, "%d\n", perf_overcommit);
5374}
5375
5376static ssize_t
5377perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
5378{
5379 unsigned long val;
5380 int err;
5381
5382 err = strict_strtoul(buf, 10, &val);
5383 if (err)
5384 return err;
5385 if (val > 1)
5386 return -EINVAL;
5387
5388 spin_lock(&perf_resource_lock);
5389 perf_overcommit = val;
5390 spin_unlock(&perf_resource_lock);
5391
5392 return count;
5393}
5394
5395static SYSDEV_CLASS_ATTR(
5396 reserve_percpu,
5397 0644,
5398 perf_show_reserve_percpu,
5399 perf_set_reserve_percpu
5400 );
5401
5402static SYSDEV_CLASS_ATTR(
5403 overcommit,
5404 0644,
5405 perf_show_overcommit,
5406 perf_set_overcommit
5407 );
5408
5409static struct attribute *perfclass_attrs[] = {
5410 &attr_reserve_percpu.attr,
5411 &attr_overcommit.attr,
5412 NULL
5413};
5414
5415static struct attribute_group perfclass_attr_group = {
5416 .attrs = perfclass_attrs,
5417 .name = "perf_events",
5418};
5419
5420static int __init perf_event_sysfs_init(void)
5421{
5422 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
5423 &perfclass_attr_group);
5424}
5425device_initcall(perf_event_sysfs_init);