blob: fb46fd13f31fb75fd6eca190a51b6a80646770ec [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020019#include <linux/hash.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020020#include <linux/sysfs.h>
21#include <linux/dcache.h>
22#include <linux/percpu.h>
23#include <linux/ptrace.h>
24#include <linux/vmstat.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020025#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020026#include <linux/hardirq.h>
27#include <linux/rculist.h>
28#include <linux/uaccess.h>
29#include <linux/syscalls.h>
30#include <linux/anon_inodes.h>
31#include <linux/kernel_stat.h>
32#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080033#include <linux/ftrace_event.h>
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020034#include <linux/hw_breakpoint.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020035
36#include <asm/irq_regs.h>
37
38/*
39 * Each CPU has a list of per CPU events:
40 */
Xiao Guangrongaa5452d2009-12-09 11:28:13 +080041static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020042
43int perf_max_events __read_mostly = 1;
44static int perf_reserved_percpu __read_mostly;
45static int perf_overcommit __read_mostly = 1;
46
47static atomic_t nr_events __read_mostly;
48static atomic_t nr_mmap_events __read_mostly;
49static atomic_t nr_comm_events __read_mostly;
50static atomic_t nr_task_events __read_mostly;
51
52/*
53 * perf event paranoia level:
54 * -1 - not paranoid at all
55 * 0 - disallow raw tracepoint access for unpriv
56 * 1 - disallow cpu events for unpriv
57 * 2 - disallow kernel profiling for unpriv
58 */
59int sysctl_perf_event_paranoid __read_mostly = 1;
60
Ingo Molnarcdd6c482009-09-21 12:02:48 +020061int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
62
63/*
64 * max perf event sample rate
65 */
66int sysctl_perf_event_sample_rate __read_mostly = 100000;
67
68static atomic64_t perf_event_id;
69
70/*
71 * Lock for (sysadmin-configurable) event reservations:
72 */
73static DEFINE_SPINLOCK(perf_resource_lock);
74
75/*
76 * Architecture provided APIs - weak aliases:
77 */
Peter Zijlstra51b0fe32010-06-11 13:35:57 +020078extern __weak struct pmu *hw_perf_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020079{
80 return NULL;
81}
82
83void __weak hw_perf_disable(void) { barrier(); }
84void __weak hw_perf_enable(void) { barrier(); }
85
Ingo Molnarcdd6c482009-09-21 12:02:48 +020086void __weak perf_event_print_debug(void) { }
87
88static DEFINE_PER_CPU(int, perf_disable_count);
89
Ingo Molnarcdd6c482009-09-21 12:02:48 +020090void perf_disable(void)
91{
Peter Zijlstra32975a42010-03-06 19:49:19 +010092 if (!__get_cpu_var(perf_disable_count)++)
93 hw_perf_disable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +020094}
95
96void perf_enable(void)
97{
Peter Zijlstra32975a42010-03-06 19:49:19 +010098 if (!--__get_cpu_var(perf_disable_count))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020099 hw_perf_enable();
100}
101
102static void get_ctx(struct perf_event_context *ctx)
103{
104 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
105}
106
107static void free_ctx(struct rcu_head *head)
108{
109 struct perf_event_context *ctx;
110
111 ctx = container_of(head, struct perf_event_context, rcu_head);
112 kfree(ctx);
113}
114
115static void put_ctx(struct perf_event_context *ctx)
116{
117 if (atomic_dec_and_test(&ctx->refcount)) {
118 if (ctx->parent_ctx)
119 put_ctx(ctx->parent_ctx);
120 if (ctx->task)
121 put_task_struct(ctx->task);
122 call_rcu(&ctx->rcu_head, free_ctx);
123 }
124}
125
126static void unclone_ctx(struct perf_event_context *ctx)
127{
128 if (ctx->parent_ctx) {
129 put_ctx(ctx->parent_ctx);
130 ctx->parent_ctx = NULL;
131 }
132}
133
134/*
135 * If we inherit events we want to return the parent event id
136 * to userspace.
137 */
138static u64 primary_event_id(struct perf_event *event)
139{
140 u64 id = event->id;
141
142 if (event->parent)
143 id = event->parent->id;
144
145 return id;
146}
147
148/*
149 * Get the perf_event_context for a task and lock it.
150 * This has to cope with with the fact that until it is locked,
151 * the context could get moved to another task.
152 */
153static struct perf_event_context *
154perf_lock_task_context(struct task_struct *task, unsigned long *flags)
155{
156 struct perf_event_context *ctx;
157
158 rcu_read_lock();
159 retry:
160 ctx = rcu_dereference(task->perf_event_ctxp);
161 if (ctx) {
162 /*
163 * If this context is a clone of another, it might
164 * get swapped for another underneath us by
165 * perf_event_task_sched_out, though the
166 * rcu_read_lock() protects us from any context
167 * getting freed. Lock the context and check if it
168 * got swapped before we could get the lock, and retry
169 * if so. If we locked the right context, then it
170 * can't get swapped on us any more.
171 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100172 raw_spin_lock_irqsave(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200173 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100174 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200175 goto retry;
176 }
177
178 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100179 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200180 ctx = NULL;
181 }
182 }
183 rcu_read_unlock();
184 return ctx;
185}
186
187/*
188 * Get the context for a task and increment its pin_count so it
189 * can't get swapped to another task. This also increments its
190 * reference count so that the context can't get freed.
191 */
192static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
193{
194 struct perf_event_context *ctx;
195 unsigned long flags;
196
197 ctx = perf_lock_task_context(task, &flags);
198 if (ctx) {
199 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100200 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200201 }
202 return ctx;
203}
204
205static void perf_unpin_context(struct perf_event_context *ctx)
206{
207 unsigned long flags;
208
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100209 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200210 --ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100211 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200212 put_ctx(ctx);
213}
214
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100215static inline u64 perf_clock(void)
216{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200217 return local_clock();
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100218}
219
220/*
221 * Update the record of the current time in a context.
222 */
223static void update_context_time(struct perf_event_context *ctx)
224{
225 u64 now = perf_clock();
226
227 ctx->time += now - ctx->timestamp;
228 ctx->timestamp = now;
229}
230
231/*
232 * Update the total_time_enabled and total_time_running fields for a event.
233 */
234static void update_event_times(struct perf_event *event)
235{
236 struct perf_event_context *ctx = event->ctx;
237 u64 run_end;
238
239 if (event->state < PERF_EVENT_STATE_INACTIVE ||
240 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
241 return;
242
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100243 if (ctx->is_active)
244 run_end = ctx->time;
245 else
246 run_end = event->tstamp_stopped;
247
248 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100249
250 if (event->state == PERF_EVENT_STATE_INACTIVE)
251 run_end = event->tstamp_stopped;
252 else
253 run_end = ctx->time;
254
255 event->total_time_running = run_end - event->tstamp_running;
256}
257
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200258/*
259 * Update total_time_enabled and total_time_running for all events in a group.
260 */
261static void update_group_times(struct perf_event *leader)
262{
263 struct perf_event *event;
264
265 update_event_times(leader);
266 list_for_each_entry(event, &leader->sibling_list, group_entry)
267 update_event_times(event);
268}
269
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100270static struct list_head *
271ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
272{
273 if (event->attr.pinned)
274 return &ctx->pinned_groups;
275 else
276 return &ctx->flexible_groups;
277}
278
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200279/*
280 * Add a event from the lists for its context.
281 * Must be called with ctx->mutex and ctx->lock held.
282 */
283static void
284list_add_event(struct perf_event *event, struct perf_event_context *ctx)
285{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200286 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
287 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200288
289 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200290 * If we're a stand alone event or group leader, we go to the context
291 * list, group events are kept attached to the group so that
292 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200293 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200294 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100295 struct list_head *list;
296
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100297 if (is_software_event(event))
298 event->group_flags |= PERF_GROUP_SOFTWARE;
299
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100300 list = ctx_group_list(event, ctx);
301 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200302 }
303
304 list_add_rcu(&event->event_entry, &ctx->event_list);
305 ctx->nr_events++;
306 if (event->attr.inherit_stat)
307 ctx->nr_stat++;
308}
309
Peter Zijlstra8a495422010-05-27 15:47:49 +0200310static void perf_group_attach(struct perf_event *event)
311{
312 struct perf_event *group_leader = event->group_leader;
313
314 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP);
315 event->attach_state |= PERF_ATTACH_GROUP;
316
317 if (group_leader == event)
318 return;
319
320 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
321 !is_software_event(event))
322 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
323
324 list_add_tail(&event->group_entry, &group_leader->sibling_list);
325 group_leader->nr_siblings++;
326}
327
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200328/*
329 * Remove a event from the lists for its context.
330 * Must be called with ctx->mutex and ctx->lock held.
331 */
332static void
333list_del_event(struct perf_event *event, struct perf_event_context *ctx)
334{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200335 /*
336 * We can have double detach due to exit/hot-unplug + close.
337 */
338 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200339 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200340
341 event->attach_state &= ~PERF_ATTACH_CONTEXT;
342
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200343 ctx->nr_events--;
344 if (event->attr.inherit_stat)
345 ctx->nr_stat--;
346
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200347 list_del_rcu(&event->event_entry);
348
Peter Zijlstra8a495422010-05-27 15:47:49 +0200349 if (event->group_leader == event)
350 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200351
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200352 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800353
354 /*
355 * If event was in error state, then keep it
356 * that way, otherwise bogus counts will be
357 * returned on read(). The only way to get out
358 * of error state is by explicit re-enabling
359 * of the event
360 */
361 if (event->state > PERF_EVENT_STATE_OFF)
362 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200363}
364
Peter Zijlstra8a495422010-05-27 15:47:49 +0200365static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200366{
367 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200368 struct list_head *list = NULL;
369
370 /*
371 * We can have double detach due to exit/hot-unplug + close.
372 */
373 if (!(event->attach_state & PERF_ATTACH_GROUP))
374 return;
375
376 event->attach_state &= ~PERF_ATTACH_GROUP;
377
378 /*
379 * If this is a sibling, remove it from its group.
380 */
381 if (event->group_leader != event) {
382 list_del_init(&event->group_entry);
383 event->group_leader->nr_siblings--;
384 return;
385 }
386
387 if (!list_empty(&event->group_entry))
388 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100389
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200390 /*
391 * If this was a group event with sibling events then
392 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +0200393 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200394 */
395 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +0200396 if (list)
397 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200398 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100399
400 /* Inherit group flags from the previous leader */
401 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200402 }
403}
404
Stephane Eranianfa66f072010-08-26 16:40:01 +0200405static inline int
406event_filter_match(struct perf_event *event)
407{
408 return event->cpu == -1 || event->cpu == smp_processor_id();
409}
410
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200411static void
412event_sched_out(struct perf_event *event,
413 struct perf_cpu_context *cpuctx,
414 struct perf_event_context *ctx)
415{
Stephane Eranianfa66f072010-08-26 16:40:01 +0200416 u64 delta;
417 /*
418 * An event which could not be activated because of
419 * filter mismatch still needs to have its timings
420 * maintained, otherwise bogus information is return
421 * via read() for time_enabled, time_running:
422 */
423 if (event->state == PERF_EVENT_STATE_INACTIVE
424 && !event_filter_match(event)) {
425 delta = ctx->time - event->tstamp_stopped;
426 event->tstamp_running += delta;
427 event->tstamp_stopped = ctx->time;
428 }
429
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200430 if (event->state != PERF_EVENT_STATE_ACTIVE)
431 return;
432
433 event->state = PERF_EVENT_STATE_INACTIVE;
434 if (event->pending_disable) {
435 event->pending_disable = 0;
436 event->state = PERF_EVENT_STATE_OFF;
437 }
438 event->tstamp_stopped = ctx->time;
439 event->pmu->disable(event);
440 event->oncpu = -1;
441
442 if (!is_software_event(event))
443 cpuctx->active_oncpu--;
444 ctx->nr_active--;
445 if (event->attr.exclusive || !cpuctx->active_oncpu)
446 cpuctx->exclusive = 0;
447}
448
449static void
450group_sched_out(struct perf_event *group_event,
451 struct perf_cpu_context *cpuctx,
452 struct perf_event_context *ctx)
453{
454 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +0200455 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200456
457 event_sched_out(group_event, cpuctx, ctx);
458
459 /*
460 * Schedule out siblings (if any):
461 */
462 list_for_each_entry(event, &group_event->sibling_list, group_entry)
463 event_sched_out(event, cpuctx, ctx);
464
Stephane Eranianfa66f072010-08-26 16:40:01 +0200465 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200466 cpuctx->exclusive = 0;
467}
468
469/*
470 * Cross CPU call to remove a performance event
471 *
472 * We disable the event on the hardware level first. After that we
473 * remove it from the context list.
474 */
475static void __perf_event_remove_from_context(void *info)
476{
477 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
478 struct perf_event *event = info;
479 struct perf_event_context *ctx = event->ctx;
480
481 /*
482 * If this is a task context, we need to check whether it is
483 * the current task context of this cpu. If not it has been
484 * scheduled out before the smp call arrived.
485 */
486 if (ctx->task && cpuctx->task_ctx != ctx)
487 return;
488
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100489 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200490 /*
491 * Protect the list operation against NMI by disabling the
492 * events on a global level.
493 */
494 perf_disable();
495
496 event_sched_out(event, cpuctx, ctx);
497
498 list_del_event(event, ctx);
499
500 if (!ctx->task) {
501 /*
502 * Allow more per task events with respect to the
503 * reservation:
504 */
505 cpuctx->max_pertask =
506 min(perf_max_events - ctx->nr_events,
507 perf_max_events - perf_reserved_percpu);
508 }
509
510 perf_enable();
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100511 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200512}
513
514
515/*
516 * Remove the event from a task's (or a CPU's) list of events.
517 *
518 * Must be called with ctx->mutex held.
519 *
520 * CPU events are removed with a smp call. For task events we only
521 * call when the task is on a CPU.
522 *
523 * If event->ctx is a cloned context, callers must make sure that
524 * every task struct that event->ctx->task could possibly point to
525 * remains valid. This is OK when called from perf_release since
526 * that only calls us on the top-level context, which can't be a clone.
527 * When called from perf_event_exit_task, it's OK because the
528 * context has been detached from its task.
529 */
530static void perf_event_remove_from_context(struct perf_event *event)
531{
532 struct perf_event_context *ctx = event->ctx;
533 struct task_struct *task = ctx->task;
534
535 if (!task) {
536 /*
537 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200538 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200539 */
540 smp_call_function_single(event->cpu,
541 __perf_event_remove_from_context,
542 event, 1);
543 return;
544 }
545
546retry:
547 task_oncpu_function_call(task, __perf_event_remove_from_context,
548 event);
549
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100550 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200551 /*
552 * If the context is active we need to retry the smp call.
553 */
554 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100555 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200556 goto retry;
557 }
558
559 /*
560 * The lock prevents that this context is scheduled in so we
561 * can remove the event safely, if the call above did not
562 * succeed.
563 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100564 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200565 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100566 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200567}
568
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200569/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200570 * Cross CPU call to disable a performance event
571 */
572static void __perf_event_disable(void *info)
573{
574 struct perf_event *event = info;
575 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
576 struct perf_event_context *ctx = event->ctx;
577
578 /*
579 * If this is a per-task event, need to check whether this
580 * event's task is the current task on this cpu.
581 */
582 if (ctx->task && cpuctx->task_ctx != ctx)
583 return;
584
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100585 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200586
587 /*
588 * If the event is on, turn it off.
589 * If it is in error state, leave it in error state.
590 */
591 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
592 update_context_time(ctx);
593 update_group_times(event);
594 if (event == event->group_leader)
595 group_sched_out(event, cpuctx, ctx);
596 else
597 event_sched_out(event, cpuctx, ctx);
598 event->state = PERF_EVENT_STATE_OFF;
599 }
600
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100601 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200602}
603
604/*
605 * Disable a event.
606 *
607 * If event->ctx is a cloned context, callers must make sure that
608 * every task struct that event->ctx->task could possibly point to
609 * remains valid. This condition is satisifed when called through
610 * perf_event_for_each_child or perf_event_for_each because they
611 * hold the top-level event's child_mutex, so any descendant that
612 * goes to exit will block in sync_child_event.
613 * When called from perf_pending_event it's OK because event->ctx
614 * is the current context on this CPU and preemption is disabled,
615 * hence we can't get into perf_event_task_sched_out for this context.
616 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100617void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200618{
619 struct perf_event_context *ctx = event->ctx;
620 struct task_struct *task = ctx->task;
621
622 if (!task) {
623 /*
624 * Disable the event on the cpu that it's on
625 */
626 smp_call_function_single(event->cpu, __perf_event_disable,
627 event, 1);
628 return;
629 }
630
631 retry:
632 task_oncpu_function_call(task, __perf_event_disable, event);
633
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100634 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200635 /*
636 * If the event is still active, we need to retry the cross-call.
637 */
638 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100639 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200640 goto retry;
641 }
642
643 /*
644 * Since we have the lock this context can't be scheduled
645 * in, so we can change the state safely.
646 */
647 if (event->state == PERF_EVENT_STATE_INACTIVE) {
648 update_group_times(event);
649 event->state = PERF_EVENT_STATE_OFF;
650 }
651
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100652 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200653}
654
655static int
656event_sched_in(struct perf_event *event,
657 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100658 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200659{
660 if (event->state <= PERF_EVENT_STATE_OFF)
661 return 0;
662
663 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +0100664 event->oncpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200665 /*
666 * The new state must be visible before we turn it on in the hardware:
667 */
668 smp_wmb();
669
670 if (event->pmu->enable(event)) {
671 event->state = PERF_EVENT_STATE_INACTIVE;
672 event->oncpu = -1;
673 return -EAGAIN;
674 }
675
676 event->tstamp_running += ctx->time - event->tstamp_stopped;
677
678 if (!is_software_event(event))
679 cpuctx->active_oncpu++;
680 ctx->nr_active++;
681
682 if (event->attr.exclusive)
683 cpuctx->exclusive = 1;
684
685 return 0;
686}
687
688static int
689group_sched_in(struct perf_event *group_event,
690 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100691 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200692{
Lin Ming6bde9b62010-04-23 13:56:00 +0800693 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +0200694 struct pmu *pmu = group_event->pmu;
Lin Ming6bde9b62010-04-23 13:56:00 +0800695 bool txn = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200696
697 if (group_event->state == PERF_EVENT_STATE_OFF)
698 return 0;
699
Lin Ming6bde9b62010-04-23 13:56:00 +0800700 /* Check if group transaction availabe */
701 if (pmu->start_txn)
702 txn = true;
703
704 if (txn)
705 pmu->start_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200706
Stephane Eranian90151c32010-05-25 16:23:10 +0200707 if (event_sched_in(group_event, cpuctx, ctx)) {
708 if (txn)
709 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200710 return -EAGAIN;
Stephane Eranian90151c32010-05-25 16:23:10 +0200711 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200712
713 /*
714 * Schedule in siblings as one group (if any):
715 */
716 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Peter Zijlstra6e377382010-02-11 13:21:58 +0100717 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200718 partial_group = event;
719 goto group_error;
720 }
721 }
722
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +0200723 if (!txn || !pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +1000724 return 0;
Lin Ming6bde9b62010-04-23 13:56:00 +0800725
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200726group_error:
727 /*
728 * Groups can be scheduled in as one unit only, so undo any
729 * partial group before returning:
730 */
731 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
732 if (event == partial_group)
733 break;
734 event_sched_out(event, cpuctx, ctx);
735 }
736 event_sched_out(group_event, cpuctx, ctx);
737
Stephane Eranian90151c32010-05-25 16:23:10 +0200738 if (txn)
739 pmu->cancel_txn(pmu);
740
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200741 return -EAGAIN;
742}
743
744/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200745 * Work out whether we can put this event group on the CPU now.
746 */
747static int group_can_go_on(struct perf_event *event,
748 struct perf_cpu_context *cpuctx,
749 int can_add_hw)
750{
751 /*
752 * Groups consisting entirely of software events can always go on.
753 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100754 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200755 return 1;
756 /*
757 * If an exclusive group is already on, no other hardware
758 * events can go on.
759 */
760 if (cpuctx->exclusive)
761 return 0;
762 /*
763 * If this group is exclusive and there are already
764 * events on the CPU, it can't go on.
765 */
766 if (event->attr.exclusive && cpuctx->active_oncpu)
767 return 0;
768 /*
769 * Otherwise, try to add it if all previous groups were able
770 * to go on.
771 */
772 return can_add_hw;
773}
774
775static void add_event_to_ctx(struct perf_event *event,
776 struct perf_event_context *ctx)
777{
778 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200779 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200780 event->tstamp_enabled = ctx->time;
781 event->tstamp_running = ctx->time;
782 event->tstamp_stopped = ctx->time;
783}
784
785/*
786 * Cross CPU call to install and enable a performance event
787 *
788 * Must be called with ctx->mutex held
789 */
790static void __perf_install_in_context(void *info)
791{
792 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
793 struct perf_event *event = info;
794 struct perf_event_context *ctx = event->ctx;
795 struct perf_event *leader = event->group_leader;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200796 int err;
797
798 /*
799 * If this is a task context, we need to check whether it is
800 * the current task context of this cpu. If not it has been
801 * scheduled out before the smp call arrived.
802 * Or possibly this is the right context but it isn't
803 * on this cpu because it had no events.
804 */
805 if (ctx->task && cpuctx->task_ctx != ctx) {
806 if (cpuctx->task_ctx || ctx->task != current)
807 return;
808 cpuctx->task_ctx = ctx;
809 }
810
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100811 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200812 ctx->is_active = 1;
813 update_context_time(ctx);
814
815 /*
816 * Protect the list operation against NMI by disabling the
817 * events on a global level. NOP for non NMI based events.
818 */
819 perf_disable();
820
821 add_event_to_ctx(event, ctx);
822
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100823 if (event->cpu != -1 && event->cpu != smp_processor_id())
824 goto unlock;
825
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200826 /*
827 * Don't put the event on if it is disabled or if
828 * it is in a group and the group isn't on.
829 */
830 if (event->state != PERF_EVENT_STATE_INACTIVE ||
831 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
832 goto unlock;
833
834 /*
835 * An exclusive event can't go on if there are already active
836 * hardware events, and no hardware event can go on if there
837 * is already an exclusive event on.
838 */
839 if (!group_can_go_on(event, cpuctx, 1))
840 err = -EEXIST;
841 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100842 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200843
844 if (err) {
845 /*
846 * This event couldn't go on. If it is in a group
847 * then we have to pull the whole group off.
848 * If the event group is pinned then put it in error state.
849 */
850 if (leader != event)
851 group_sched_out(leader, cpuctx, ctx);
852 if (leader->attr.pinned) {
853 update_group_times(leader);
854 leader->state = PERF_EVENT_STATE_ERROR;
855 }
856 }
857
858 if (!err && !ctx->task && cpuctx->max_pertask)
859 cpuctx->max_pertask--;
860
861 unlock:
862 perf_enable();
863
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100864 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200865}
866
867/*
868 * Attach a performance event to a context
869 *
870 * First we add the event to the list with the hardware enable bit
871 * in event->hw_config cleared.
872 *
873 * If the event is attached to a task which is on a CPU we use a smp
874 * call to enable it in the task context. The task might have been
875 * scheduled away, but we check this in the smp call again.
876 *
877 * Must be called with ctx->mutex held.
878 */
879static void
880perf_install_in_context(struct perf_event_context *ctx,
881 struct perf_event *event,
882 int cpu)
883{
884 struct task_struct *task = ctx->task;
885
886 if (!task) {
887 /*
888 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200889 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200890 */
891 smp_call_function_single(cpu, __perf_install_in_context,
892 event, 1);
893 return;
894 }
895
896retry:
897 task_oncpu_function_call(task, __perf_install_in_context,
898 event);
899
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100900 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200901 /*
902 * we need to retry the smp call.
903 */
904 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100905 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200906 goto retry;
907 }
908
909 /*
910 * The lock prevents that this context is scheduled in so we
911 * can add the event safely, if it the call above did not
912 * succeed.
913 */
914 if (list_empty(&event->group_entry))
915 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100916 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200917}
918
919/*
920 * Put a event into inactive state and update time fields.
921 * Enabling the leader of a group effectively enables all
922 * the group members that aren't explicitly disabled, so we
923 * have to update their ->tstamp_enabled also.
924 * Note: this works for group members as well as group leaders
925 * since the non-leader members' sibling_lists will be empty.
926 */
927static void __perf_event_mark_enabled(struct perf_event *event,
928 struct perf_event_context *ctx)
929{
930 struct perf_event *sub;
931
932 event->state = PERF_EVENT_STATE_INACTIVE;
933 event->tstamp_enabled = ctx->time - event->total_time_enabled;
934 list_for_each_entry(sub, &event->sibling_list, group_entry)
935 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
936 sub->tstamp_enabled =
937 ctx->time - sub->total_time_enabled;
938}
939
940/*
941 * Cross CPU call to enable a performance event
942 */
943static void __perf_event_enable(void *info)
944{
945 struct perf_event *event = info;
946 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
947 struct perf_event_context *ctx = event->ctx;
948 struct perf_event *leader = event->group_leader;
949 int err;
950
951 /*
952 * If this is a per-task event, need to check whether this
953 * event's task is the current task on this cpu.
954 */
955 if (ctx->task && cpuctx->task_ctx != ctx) {
956 if (cpuctx->task_ctx || ctx->task != current)
957 return;
958 cpuctx->task_ctx = ctx;
959 }
960
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100961 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200962 ctx->is_active = 1;
963 update_context_time(ctx);
964
965 if (event->state >= PERF_EVENT_STATE_INACTIVE)
966 goto unlock;
967 __perf_event_mark_enabled(event, ctx);
968
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100969 if (event->cpu != -1 && event->cpu != smp_processor_id())
970 goto unlock;
971
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200972 /*
973 * If the event is in a group and isn't the group leader,
974 * then don't put it on unless the group is on.
975 */
976 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
977 goto unlock;
978
979 if (!group_can_go_on(event, cpuctx, 1)) {
980 err = -EEXIST;
981 } else {
982 perf_disable();
983 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +0100984 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200985 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100986 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200987 perf_enable();
988 }
989
990 if (err) {
991 /*
992 * If this event can't go on and it's part of a
993 * group, then the whole group has to come off.
994 */
995 if (leader != event)
996 group_sched_out(leader, cpuctx, ctx);
997 if (leader->attr.pinned) {
998 update_group_times(leader);
999 leader->state = PERF_EVENT_STATE_ERROR;
1000 }
1001 }
1002
1003 unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001004 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001005}
1006
1007/*
1008 * Enable a event.
1009 *
1010 * If event->ctx is a cloned context, callers must make sure that
1011 * every task struct that event->ctx->task could possibly point to
1012 * remains valid. This condition is satisfied when called through
1013 * perf_event_for_each_child or perf_event_for_each as described
1014 * for perf_event_disable.
1015 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001016void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001017{
1018 struct perf_event_context *ctx = event->ctx;
1019 struct task_struct *task = ctx->task;
1020
1021 if (!task) {
1022 /*
1023 * Enable the event on the cpu that it's on
1024 */
1025 smp_call_function_single(event->cpu, __perf_event_enable,
1026 event, 1);
1027 return;
1028 }
1029
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001030 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001031 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1032 goto out;
1033
1034 /*
1035 * If the event is in error state, clear that first.
1036 * That way, if we see the event in error state below, we
1037 * know that it has gone back into error state, as distinct
1038 * from the task having been scheduled away before the
1039 * cross-call arrived.
1040 */
1041 if (event->state == PERF_EVENT_STATE_ERROR)
1042 event->state = PERF_EVENT_STATE_OFF;
1043
1044 retry:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001045 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001046 task_oncpu_function_call(task, __perf_event_enable, event);
1047
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001048 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001049
1050 /*
1051 * If the context is active and the event is still off,
1052 * we need to retry the cross-call.
1053 */
1054 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1055 goto retry;
1056
1057 /*
1058 * Since we have the lock this context can't be scheduled
1059 * in, so we can change the state safely.
1060 */
1061 if (event->state == PERF_EVENT_STATE_OFF)
1062 __perf_event_mark_enabled(event, ctx);
1063
1064 out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001065 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001066}
1067
1068static int perf_event_refresh(struct perf_event *event, int refresh)
1069{
1070 /*
1071 * not supported on inherited events
1072 */
1073 if (event->attr.inherit)
1074 return -EINVAL;
1075
1076 atomic_add(refresh, &event->event_limit);
1077 perf_event_enable(event);
1078
1079 return 0;
1080}
1081
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001082enum event_type_t {
1083 EVENT_FLEXIBLE = 0x1,
1084 EVENT_PINNED = 0x2,
1085 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1086};
1087
1088static void ctx_sched_out(struct perf_event_context *ctx,
1089 struct perf_cpu_context *cpuctx,
1090 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001091{
1092 struct perf_event *event;
1093
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001094 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001095 ctx->is_active = 0;
1096 if (likely(!ctx->nr_events))
1097 goto out;
1098 update_context_time(ctx);
1099
1100 perf_disable();
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001101 if (!ctx->nr_active)
1102 goto out_enable;
1103
1104 if (event_type & EVENT_PINNED)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001105 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1106 group_sched_out(event, cpuctx, ctx);
1107
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001108 if (event_type & EVENT_FLEXIBLE)
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001109 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001110 group_sched_out(event, cpuctx, ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001111
1112 out_enable:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001113 perf_enable();
1114 out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001115 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001116}
1117
1118/*
1119 * Test whether two contexts are equivalent, i.e. whether they
1120 * have both been cloned from the same version of the same context
1121 * and they both have the same number of enabled events.
1122 * If the number of enabled events is the same, then the set
1123 * of enabled events should be the same, because these are both
1124 * inherited contexts, therefore we can't access individual events
1125 * in them directly with an fd; we can only enable/disable all
1126 * events via prctl, or enable/disable all events in a family
1127 * via ioctl, which will have the same effect on both contexts.
1128 */
1129static int context_equiv(struct perf_event_context *ctx1,
1130 struct perf_event_context *ctx2)
1131{
1132 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1133 && ctx1->parent_gen == ctx2->parent_gen
1134 && !ctx1->pin_count && !ctx2->pin_count;
1135}
1136
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001137static void __perf_event_sync_stat(struct perf_event *event,
1138 struct perf_event *next_event)
1139{
1140 u64 value;
1141
1142 if (!event->attr.inherit_stat)
1143 return;
1144
1145 /*
1146 * Update the event value, we cannot use perf_event_read()
1147 * because we're in the middle of a context switch and have IRQs
1148 * disabled, which upsets smp_call_function_single(), however
1149 * we know the event must be on the current CPU, therefore we
1150 * don't need to use it.
1151 */
1152 switch (event->state) {
1153 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001154 event->pmu->read(event);
1155 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001156
1157 case PERF_EVENT_STATE_INACTIVE:
1158 update_event_times(event);
1159 break;
1160
1161 default:
1162 break;
1163 }
1164
1165 /*
1166 * In order to keep per-task stats reliable we need to flip the event
1167 * values when we flip the contexts.
1168 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001169 value = local64_read(&next_event->count);
1170 value = local64_xchg(&event->count, value);
1171 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001172
1173 swap(event->total_time_enabled, next_event->total_time_enabled);
1174 swap(event->total_time_running, next_event->total_time_running);
1175
1176 /*
1177 * Since we swizzled the values, update the user visible data too.
1178 */
1179 perf_event_update_userpage(event);
1180 perf_event_update_userpage(next_event);
1181}
1182
1183#define list_next_entry(pos, member) \
1184 list_entry(pos->member.next, typeof(*pos), member)
1185
1186static void perf_event_sync_stat(struct perf_event_context *ctx,
1187 struct perf_event_context *next_ctx)
1188{
1189 struct perf_event *event, *next_event;
1190
1191 if (!ctx->nr_stat)
1192 return;
1193
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001194 update_context_time(ctx);
1195
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001196 event = list_first_entry(&ctx->event_list,
1197 struct perf_event, event_entry);
1198
1199 next_event = list_first_entry(&next_ctx->event_list,
1200 struct perf_event, event_entry);
1201
1202 while (&event->event_entry != &ctx->event_list &&
1203 &next_event->event_entry != &next_ctx->event_list) {
1204
1205 __perf_event_sync_stat(event, next_event);
1206
1207 event = list_next_entry(event, event_entry);
1208 next_event = list_next_entry(next_event, event_entry);
1209 }
1210}
1211
1212/*
1213 * Called from scheduler to remove the events of the current task,
1214 * with interrupts disabled.
1215 *
1216 * We stop each event and update the event value in event->count.
1217 *
1218 * This does not protect us against NMI, but disable()
1219 * sets the disabled bit in the control field of event _before_
1220 * accessing the event control register. If a NMI hits, then it will
1221 * not restart the event.
1222 */
1223void perf_event_task_sched_out(struct task_struct *task,
Peter Zijlstra49f47432009-12-27 11:51:52 +01001224 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001225{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001226 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001227 struct perf_event_context *ctx = task->perf_event_ctxp;
1228 struct perf_event_context *next_ctx;
1229 struct perf_event_context *parent;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001230 int do_switch = 1;
1231
Frederic Weisbeckere49a5bd2010-03-22 19:40:03 +01001232 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001233
1234 if (likely(!ctx || !cpuctx->task_ctx))
1235 return;
1236
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001237 rcu_read_lock();
1238 parent = rcu_dereference(ctx->parent_ctx);
1239 next_ctx = next->perf_event_ctxp;
1240 if (parent && next_ctx &&
1241 rcu_dereference(next_ctx->parent_ctx) == parent) {
1242 /*
1243 * Looks like the two contexts are clones, so we might be
1244 * able to optimize the context switch. We lock both
1245 * contexts and check that they are clones under the
1246 * lock (including re-checking that neither has been
1247 * uncloned in the meantime). It doesn't matter which
1248 * order we take the locks because no other cpu could
1249 * be trying to lock both of these tasks.
1250 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001251 raw_spin_lock(&ctx->lock);
1252 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001253 if (context_equiv(ctx, next_ctx)) {
1254 /*
1255 * XXX do we need a memory barrier of sorts
1256 * wrt to rcu_dereference() of perf_event_ctxp
1257 */
1258 task->perf_event_ctxp = next_ctx;
1259 next->perf_event_ctxp = ctx;
1260 ctx->task = next;
1261 next_ctx->task = task;
1262 do_switch = 0;
1263
1264 perf_event_sync_stat(ctx, next_ctx);
1265 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001266 raw_spin_unlock(&next_ctx->lock);
1267 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001268 }
1269 rcu_read_unlock();
1270
1271 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001272 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001273 cpuctx->task_ctx = NULL;
1274 }
1275}
1276
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001277static void task_ctx_sched_out(struct perf_event_context *ctx,
1278 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001279{
1280 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1281
1282 if (!cpuctx->task_ctx)
1283 return;
1284
1285 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1286 return;
1287
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001288 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001289 cpuctx->task_ctx = NULL;
1290}
1291
1292/*
1293 * Called with IRQs disabled
1294 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001295static void __perf_event_task_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001296{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001297 task_ctx_sched_out(ctx, EVENT_ALL);
1298}
1299
1300/*
1301 * Called with IRQs disabled
1302 */
1303static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1304 enum event_type_t event_type)
1305{
1306 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001307}
1308
1309static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001310ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001311 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001312{
1313 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001314
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001315 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1316 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001317 continue;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001318 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001319 continue;
1320
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001321 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001322 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001323
1324 /*
1325 * If this pinned group hasn't been scheduled,
1326 * put it in error state.
1327 */
1328 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1329 update_group_times(event);
1330 event->state = PERF_EVENT_STATE_ERROR;
1331 }
1332 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001333}
1334
1335static void
1336ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001337 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001338{
1339 struct perf_event *event;
1340 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001341
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001342 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1343 /* Ignore events in OFF or ERROR state */
1344 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001345 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001346 /*
1347 * Listen to the 'cpu' scheduling filter constraint
1348 * of events:
1349 */
Peter Zijlstra6e377382010-02-11 13:21:58 +01001350 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001351 continue;
1352
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001353 if (group_can_go_on(event, cpuctx, can_add_hw))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001354 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001355 can_add_hw = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001356 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001357}
1358
1359static void
1360ctx_sched_in(struct perf_event_context *ctx,
1361 struct perf_cpu_context *cpuctx,
1362 enum event_type_t event_type)
1363{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001364 raw_spin_lock(&ctx->lock);
1365 ctx->is_active = 1;
1366 if (likely(!ctx->nr_events))
1367 goto out;
1368
1369 ctx->timestamp = perf_clock();
1370
1371 perf_disable();
1372
1373 /*
1374 * First go through the list and put on any pinned groups
1375 * in order to give them the best chance of going on.
1376 */
1377 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001378 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001379
1380 /* Then walk through the lower prio flexible groups */
1381 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001382 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001383
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001384 perf_enable();
1385 out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001386 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001387}
1388
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001389static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1390 enum event_type_t event_type)
1391{
1392 struct perf_event_context *ctx = &cpuctx->ctx;
1393
1394 ctx_sched_in(ctx, cpuctx, event_type);
1395}
1396
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001397static void task_ctx_sched_in(struct task_struct *task,
1398 enum event_type_t event_type)
1399{
1400 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1401 struct perf_event_context *ctx = task->perf_event_ctxp;
1402
1403 if (likely(!ctx))
1404 return;
1405 if (cpuctx->task_ctx == ctx)
1406 return;
1407 ctx_sched_in(ctx, cpuctx, event_type);
1408 cpuctx->task_ctx = ctx;
1409}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001410/*
1411 * Called from scheduler to add the events of the current task
1412 * with interrupts disabled.
1413 *
1414 * We restore the event value and then enable it.
1415 *
1416 * This does not protect us against NMI, but enable()
1417 * sets the enabled bit in the control field of event _before_
1418 * accessing the event control register. If a NMI hits, then it will
1419 * keep the event running.
1420 */
Peter Zijlstra49f47432009-12-27 11:51:52 +01001421void perf_event_task_sched_in(struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001422{
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001423 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1424 struct perf_event_context *ctx = task->perf_event_ctxp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001425
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001426 if (likely(!ctx))
1427 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001428
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001429 if (cpuctx->task_ctx == ctx)
1430 return;
1431
eranian@google.com9b33fa62010-03-10 22:26:05 -08001432 perf_disable();
1433
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001434 /*
1435 * We want to keep the following priority order:
1436 * cpu pinned (that don't need to move), task pinned,
1437 * cpu flexible, task flexible.
1438 */
1439 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1440
1441 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1442 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1443 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1444
1445 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08001446
1447 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001448}
1449
1450#define MAX_INTERRUPTS (~0ULL)
1451
1452static void perf_log_throttle(struct perf_event *event, int enable);
1453
Peter Zijlstraabd50712010-01-26 18:50:16 +01001454static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1455{
1456 u64 frequency = event->attr.sample_freq;
1457 u64 sec = NSEC_PER_SEC;
1458 u64 divisor, dividend;
1459
1460 int count_fls, nsec_fls, frequency_fls, sec_fls;
1461
1462 count_fls = fls64(count);
1463 nsec_fls = fls64(nsec);
1464 frequency_fls = fls64(frequency);
1465 sec_fls = 30;
1466
1467 /*
1468 * We got @count in @nsec, with a target of sample_freq HZ
1469 * the target period becomes:
1470 *
1471 * @count * 10^9
1472 * period = -------------------
1473 * @nsec * sample_freq
1474 *
1475 */
1476
1477 /*
1478 * Reduce accuracy by one bit such that @a and @b converge
1479 * to a similar magnitude.
1480 */
1481#define REDUCE_FLS(a, b) \
1482do { \
1483 if (a##_fls > b##_fls) { \
1484 a >>= 1; \
1485 a##_fls--; \
1486 } else { \
1487 b >>= 1; \
1488 b##_fls--; \
1489 } \
1490} while (0)
1491
1492 /*
1493 * Reduce accuracy until either term fits in a u64, then proceed with
1494 * the other, so that finally we can do a u64/u64 division.
1495 */
1496 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1497 REDUCE_FLS(nsec, frequency);
1498 REDUCE_FLS(sec, count);
1499 }
1500
1501 if (count_fls + sec_fls > 64) {
1502 divisor = nsec * frequency;
1503
1504 while (count_fls + sec_fls > 64) {
1505 REDUCE_FLS(count, sec);
1506 divisor >>= 1;
1507 }
1508
1509 dividend = count * sec;
1510 } else {
1511 dividend = count * sec;
1512
1513 while (nsec_fls + frequency_fls > 64) {
1514 REDUCE_FLS(nsec, frequency);
1515 dividend >>= 1;
1516 }
1517
1518 divisor = nsec * frequency;
1519 }
1520
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001521 if (!divisor)
1522 return dividend;
1523
Peter Zijlstraabd50712010-01-26 18:50:16 +01001524 return div64_u64(dividend, divisor);
1525}
1526
Stephane Eraniand76a0812010-02-08 17:06:01 +02001527static void perf_event_stop(struct perf_event *event)
1528{
1529 if (!event->pmu->stop)
1530 return event->pmu->disable(event);
1531
1532 return event->pmu->stop(event);
1533}
1534
1535static int perf_event_start(struct perf_event *event)
1536{
1537 if (!event->pmu->start)
1538 return event->pmu->enable(event);
1539
1540 return event->pmu->start(event);
1541}
1542
Peter Zijlstraabd50712010-01-26 18:50:16 +01001543static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001544{
1545 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001546 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001547 s64 delta;
1548
Peter Zijlstraabd50712010-01-26 18:50:16 +01001549 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001550
1551 delta = (s64)(period - hwc->sample_period);
1552 delta = (delta + 7) / 8; /* low pass filter */
1553
1554 sample_period = hwc->sample_period + delta;
1555
1556 if (!sample_period)
1557 sample_period = 1;
1558
1559 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001560
Peter Zijlstrae7850592010-05-21 14:43:08 +02001561 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraabd50712010-01-26 18:50:16 +01001562 perf_disable();
Stephane Eraniand76a0812010-02-08 17:06:01 +02001563 perf_event_stop(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001564 local64_set(&hwc->period_left, 0);
Stephane Eraniand76a0812010-02-08 17:06:01 +02001565 perf_event_start(event);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001566 perf_enable();
1567 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001568}
1569
1570static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1571{
1572 struct perf_event *event;
1573 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001574 u64 interrupts, now;
1575 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001576
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001577 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001578 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001579 if (event->state != PERF_EVENT_STATE_ACTIVE)
1580 continue;
1581
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001582 if (event->cpu != -1 && event->cpu != smp_processor_id())
1583 continue;
1584
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001585 hwc = &event->hw;
1586
1587 interrupts = hwc->interrupts;
1588 hwc->interrupts = 0;
1589
1590 /*
1591 * unthrottle events on the tick
1592 */
1593 if (interrupts == MAX_INTERRUPTS) {
1594 perf_log_throttle(event, 1);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001595 perf_disable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001596 event->pmu->unthrottle(event);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001597 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001598 }
1599
1600 if (!event->attr.freq || !event->attr.sample_freq)
1601 continue;
1602
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001603 perf_disable();
Peter Zijlstraabd50712010-01-26 18:50:16 +01001604 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001605 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001606 delta = now - hwc->freq_count_stamp;
1607 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001608
Peter Zijlstraabd50712010-01-26 18:50:16 +01001609 if (delta > 0)
1610 perf_adjust_period(event, TICK_NSEC, delta);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001611 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001612 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001613 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001614}
1615
1616/*
1617 * Round-robin a context's events:
1618 */
1619static void rotate_ctx(struct perf_event_context *ctx)
1620{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001621 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001622
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001623 /* Rotate the first entry last of non-pinned groups */
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001624 list_rotate_left(&ctx->flexible_groups);
1625
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001626 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001627}
1628
Peter Zijlstra49f47432009-12-27 11:51:52 +01001629void perf_event_task_tick(struct task_struct *curr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001630{
1631 struct perf_cpu_context *cpuctx;
1632 struct perf_event_context *ctx;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001633 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001634
1635 if (!atomic_read(&nr_events))
1636 return;
1637
Peter Zijlstra49f47432009-12-27 11:51:52 +01001638 cpuctx = &__get_cpu_var(perf_cpu_context);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001639 if (cpuctx->ctx.nr_events &&
1640 cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1641 rotate = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001642
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001643 ctx = curr->perf_event_ctxp;
1644 if (ctx && ctx->nr_events && ctx->nr_events != ctx->nr_active)
1645 rotate = 1;
Peter Zijlstra9717e6c2010-01-28 13:57:44 +01001646
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001647 perf_ctx_adjust_freq(&cpuctx->ctx);
1648 if (ctx)
1649 perf_ctx_adjust_freq(ctx);
1650
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001651 if (!rotate)
1652 return;
1653
1654 perf_disable();
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001655 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001656 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001657 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001658
1659 rotate_ctx(&cpuctx->ctx);
1660 if (ctx)
1661 rotate_ctx(ctx);
1662
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001663 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001664 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001665 task_ctx_sched_in(curr, EVENT_FLEXIBLE);
Peter Zijlstra9717e6c2010-01-28 13:57:44 +01001666 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001667}
1668
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001669static int event_enable_on_exec(struct perf_event *event,
1670 struct perf_event_context *ctx)
1671{
1672 if (!event->attr.enable_on_exec)
1673 return 0;
1674
1675 event->attr.enable_on_exec = 0;
1676 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1677 return 0;
1678
1679 __perf_event_mark_enabled(event, ctx);
1680
1681 return 1;
1682}
1683
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001684/*
1685 * Enable all of a task's events that have been marked enable-on-exec.
1686 * This expects task == current.
1687 */
1688static void perf_event_enable_on_exec(struct task_struct *task)
1689{
1690 struct perf_event_context *ctx;
1691 struct perf_event *event;
1692 unsigned long flags;
1693 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001694 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001695
1696 local_irq_save(flags);
1697 ctx = task->perf_event_ctxp;
1698 if (!ctx || !ctx->nr_events)
1699 goto out;
1700
1701 __perf_event_task_sched_out(ctx);
1702
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001703 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001704
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001705 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1706 ret = event_enable_on_exec(event, ctx);
1707 if (ret)
1708 enabled = 1;
1709 }
1710
1711 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1712 ret = event_enable_on_exec(event, ctx);
1713 if (ret)
1714 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001715 }
1716
1717 /*
1718 * Unclone this context if we enabled any event.
1719 */
1720 if (enabled)
1721 unclone_ctx(ctx);
1722
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001723 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001724
Peter Zijlstra49f47432009-12-27 11:51:52 +01001725 perf_event_task_sched_in(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001726 out:
1727 local_irq_restore(flags);
1728}
1729
1730/*
1731 * Cross CPU call to read the hardware event
1732 */
1733static void __perf_event_read(void *info)
1734{
1735 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1736 struct perf_event *event = info;
1737 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001738
1739 /*
1740 * If this is a task context, we need to check whether it is
1741 * the current task context of this cpu. If not it has been
1742 * scheduled out before the smp call arrived. In that case
1743 * event->count would have been updated to a recent sample
1744 * when the event was scheduled out.
1745 */
1746 if (ctx->task && cpuctx->task_ctx != ctx)
1747 return;
1748
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001749 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001750 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001751 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001752 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001753
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001754 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001755}
1756
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001757static inline u64 perf_event_count(struct perf_event *event)
1758{
Peter Zijlstrae7850592010-05-21 14:43:08 +02001759 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001760}
1761
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001762static u64 perf_event_read(struct perf_event *event)
1763{
1764 /*
1765 * If event is enabled and currently active on a CPU, update the
1766 * value in the event structure:
1767 */
1768 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1769 smp_call_function_single(event->oncpu,
1770 __perf_event_read, event, 1);
1771 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001772 struct perf_event_context *ctx = event->ctx;
1773 unsigned long flags;
1774
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001775 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001776 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001777 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001778 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001779 }
1780
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001781 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001782}
1783
1784/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001785 * Callchain support
1786 */
1787
1788struct callchain_cpus_entries {
1789 struct rcu_head rcu_head;
1790 struct perf_callchain_entry *cpu_entries[0];
1791};
1792
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001793static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001794static atomic_t nr_callchain_events;
1795static DEFINE_MUTEX(callchain_mutex);
1796struct callchain_cpus_entries *callchain_cpus_entries;
1797
1798
1799__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1800 struct pt_regs *regs)
1801{
1802}
1803
1804__weak void perf_callchain_user(struct perf_callchain_entry *entry,
1805 struct pt_regs *regs)
1806{
1807}
1808
1809static void release_callchain_buffers_rcu(struct rcu_head *head)
1810{
1811 struct callchain_cpus_entries *entries;
1812 int cpu;
1813
1814 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1815
1816 for_each_possible_cpu(cpu)
1817 kfree(entries->cpu_entries[cpu]);
1818
1819 kfree(entries);
1820}
1821
1822static void release_callchain_buffers(void)
1823{
1824 struct callchain_cpus_entries *entries;
1825
1826 entries = callchain_cpus_entries;
1827 rcu_assign_pointer(callchain_cpus_entries, NULL);
1828 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1829}
1830
1831static int alloc_callchain_buffers(void)
1832{
1833 int cpu;
1834 int size;
1835 struct callchain_cpus_entries *entries;
1836
1837 /*
1838 * We can't use the percpu allocation API for data that can be
1839 * accessed from NMI. Use a temporary manual per cpu allocation
1840 * until that gets sorted out.
1841 */
1842 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1843 num_possible_cpus();
1844
1845 entries = kzalloc(size, GFP_KERNEL);
1846 if (!entries)
1847 return -ENOMEM;
1848
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001849 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001850
1851 for_each_possible_cpu(cpu) {
1852 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1853 cpu_to_node(cpu));
1854 if (!entries->cpu_entries[cpu])
1855 goto fail;
1856 }
1857
1858 rcu_assign_pointer(callchain_cpus_entries, entries);
1859
1860 return 0;
1861
1862fail:
1863 for_each_possible_cpu(cpu)
1864 kfree(entries->cpu_entries[cpu]);
1865 kfree(entries);
1866
1867 return -ENOMEM;
1868}
1869
1870static int get_callchain_buffers(void)
1871{
1872 int err = 0;
1873 int count;
1874
1875 mutex_lock(&callchain_mutex);
1876
1877 count = atomic_inc_return(&nr_callchain_events);
1878 if (WARN_ON_ONCE(count < 1)) {
1879 err = -EINVAL;
1880 goto exit;
1881 }
1882
1883 if (count > 1) {
1884 /* If the allocation failed, give up */
1885 if (!callchain_cpus_entries)
1886 err = -ENOMEM;
1887 goto exit;
1888 }
1889
1890 err = alloc_callchain_buffers();
1891 if (err)
1892 release_callchain_buffers();
1893exit:
1894 mutex_unlock(&callchain_mutex);
1895
1896 return err;
1897}
1898
1899static void put_callchain_buffers(void)
1900{
1901 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1902 release_callchain_buffers();
1903 mutex_unlock(&callchain_mutex);
1904 }
1905}
1906
1907static int get_recursion_context(int *recursion)
1908{
1909 int rctx;
1910
1911 if (in_nmi())
1912 rctx = 3;
1913 else if (in_irq())
1914 rctx = 2;
1915 else if (in_softirq())
1916 rctx = 1;
1917 else
1918 rctx = 0;
1919
1920 if (recursion[rctx])
1921 return -1;
1922
1923 recursion[rctx]++;
1924 barrier();
1925
1926 return rctx;
1927}
1928
1929static inline void put_recursion_context(int *recursion, int rctx)
1930{
1931 barrier();
1932 recursion[rctx]--;
1933}
1934
1935static struct perf_callchain_entry *get_callchain_entry(int *rctx)
1936{
1937 int cpu;
1938 struct callchain_cpus_entries *entries;
1939
1940 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
1941 if (*rctx == -1)
1942 return NULL;
1943
1944 entries = rcu_dereference(callchain_cpus_entries);
1945 if (!entries)
1946 return NULL;
1947
1948 cpu = smp_processor_id();
1949
1950 return &entries->cpu_entries[cpu][*rctx];
1951}
1952
1953static void
1954put_callchain_entry(int rctx)
1955{
1956 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1957}
1958
1959static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1960{
1961 int rctx;
1962 struct perf_callchain_entry *entry;
1963
1964
1965 entry = get_callchain_entry(&rctx);
1966 if (rctx == -1)
1967 return NULL;
1968
1969 if (!entry)
1970 goto exit_put;
1971
1972 entry->nr = 0;
1973
1974 if (!user_mode(regs)) {
1975 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
1976 perf_callchain_kernel(entry, regs);
1977 if (current->mm)
1978 regs = task_pt_regs(current);
1979 else
1980 regs = NULL;
1981 }
1982
1983 if (regs) {
1984 perf_callchain_store(entry, PERF_CONTEXT_USER);
1985 perf_callchain_user(entry, regs);
1986 }
1987
1988exit_put:
1989 put_callchain_entry(rctx);
1990
1991 return entry;
1992}
1993
1994/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001995 * Initialize the perf_event context in a task_struct:
1996 */
1997static void
1998__perf_event_init_context(struct perf_event_context *ctx,
1999 struct task_struct *task)
2000{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002001 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002002 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002003 INIT_LIST_HEAD(&ctx->pinned_groups);
2004 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002005 INIT_LIST_HEAD(&ctx->event_list);
2006 atomic_set(&ctx->refcount, 1);
2007 ctx->task = task;
2008}
2009
2010static struct perf_event_context *find_get_context(pid_t pid, int cpu)
2011{
2012 struct perf_event_context *ctx;
2013 struct perf_cpu_context *cpuctx;
2014 struct task_struct *task;
2015 unsigned long flags;
2016 int err;
2017
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002018 if (pid == -1 && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002019 /* Must be root to operate on a CPU event: */
2020 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2021 return ERR_PTR(-EACCES);
2022
Paul Mackerras0f624e72009-12-15 19:40:32 +11002023 if (cpu < 0 || cpu >= nr_cpumask_bits)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002024 return ERR_PTR(-EINVAL);
2025
2026 /*
2027 * We could be clever and allow to attach a event to an
2028 * offline CPU and activate it when the CPU comes up, but
2029 * that's for later.
2030 */
Rusty Russellf6325e32009-12-17 11:43:08 -06002031 if (!cpu_online(cpu))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002032 return ERR_PTR(-ENODEV);
2033
2034 cpuctx = &per_cpu(perf_cpu_context, cpu);
2035 ctx = &cpuctx->ctx;
2036 get_ctx(ctx);
2037
2038 return ctx;
2039 }
2040
2041 rcu_read_lock();
2042 if (!pid)
2043 task = current;
2044 else
2045 task = find_task_by_vpid(pid);
2046 if (task)
2047 get_task_struct(task);
2048 rcu_read_unlock();
2049
2050 if (!task)
2051 return ERR_PTR(-ESRCH);
2052
2053 /*
2054 * Can't attach events to a dying task.
2055 */
2056 err = -ESRCH;
2057 if (task->flags & PF_EXITING)
2058 goto errout;
2059
2060 /* Reuse ptrace permission checks for now. */
2061 err = -EACCES;
2062 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2063 goto errout;
2064
2065 retry:
2066 ctx = perf_lock_task_context(task, &flags);
2067 if (ctx) {
2068 unclone_ctx(ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002069 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002070 }
2071
2072 if (!ctx) {
Xiao Guangrongaa5452d2009-12-09 11:28:13 +08002073 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002074 err = -ENOMEM;
2075 if (!ctx)
2076 goto errout;
2077 __perf_event_init_context(ctx, task);
2078 get_ctx(ctx);
2079 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
2080 /*
2081 * We raced with some other task; use
2082 * the context they set.
2083 */
2084 kfree(ctx);
2085 goto retry;
2086 }
2087 get_task_struct(task);
2088 }
2089
2090 put_task_struct(task);
2091 return ctx;
2092
2093 errout:
2094 put_task_struct(task);
2095 return ERR_PTR(err);
2096}
2097
Li Zefan6fb29152009-10-15 11:21:42 +08002098static void perf_event_free_filter(struct perf_event *event);
2099
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002100static void free_event_rcu(struct rcu_head *head)
2101{
2102 struct perf_event *event;
2103
2104 event = container_of(head, struct perf_event, rcu_head);
2105 if (event->ns)
2106 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002107 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002108 kfree(event);
2109}
2110
2111static void perf_pending_sync(struct perf_event *event);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002112static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002113
2114static void free_event(struct perf_event *event)
2115{
2116 perf_pending_sync(event);
2117
2118 if (!event->parent) {
2119 atomic_dec(&nr_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002120 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002121 atomic_dec(&nr_mmap_events);
2122 if (event->attr.comm)
2123 atomic_dec(&nr_comm_events);
2124 if (event->attr.task)
2125 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002126 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2127 put_callchain_buffers();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002128 }
2129
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002130 if (event->buffer) {
2131 perf_buffer_put(event->buffer);
2132 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002133 }
2134
2135 if (event->destroy)
2136 event->destroy(event);
2137
2138 put_ctx(event->ctx);
2139 call_rcu(&event->rcu_head, free_event_rcu);
2140}
2141
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002142int perf_event_release_kernel(struct perf_event *event)
2143{
2144 struct perf_event_context *ctx = event->ctx;
2145
Peter Zijlstra050735b2010-05-11 11:51:53 +02002146 /*
2147 * Remove from the PMU, can't get re-enabled since we got
2148 * here because the last ref went.
2149 */
2150 perf_event_disable(event);
2151
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002152 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002153 /*
2154 * There are two ways this annotation is useful:
2155 *
2156 * 1) there is a lock recursion from perf_event_exit_task
2157 * see the comment there.
2158 *
2159 * 2) there is a lock-inversion with mmap_sem through
2160 * perf_event_read_group(), which takes faults while
2161 * holding ctx->mutex, however this is called after
2162 * the last filedesc died, so there is no possibility
2163 * to trigger the AB-BA case.
2164 */
2165 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002166 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002167 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002168 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002169 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002170 mutex_unlock(&ctx->mutex);
2171
2172 mutex_lock(&event->owner->perf_event_mutex);
2173 list_del_init(&event->owner_entry);
2174 mutex_unlock(&event->owner->perf_event_mutex);
2175 put_task_struct(event->owner);
2176
2177 free_event(event);
2178
2179 return 0;
2180}
2181EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2182
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002183/*
2184 * Called when the last reference to the file is gone.
2185 */
2186static int perf_release(struct inode *inode, struct file *file)
2187{
2188 struct perf_event *event = file->private_data;
2189
2190 file->private_data = NULL;
2191
2192 return perf_event_release_kernel(event);
2193}
2194
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002195static int perf_event_read_size(struct perf_event *event)
2196{
2197 int entry = sizeof(u64); /* value */
2198 int size = 0;
2199 int nr = 1;
2200
2201 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2202 size += sizeof(u64);
2203
2204 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2205 size += sizeof(u64);
2206
2207 if (event->attr.read_format & PERF_FORMAT_ID)
2208 entry += sizeof(u64);
2209
2210 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2211 nr += event->group_leader->nr_siblings;
2212 size += sizeof(u64);
2213 }
2214
2215 size += entry * nr;
2216
2217 return size;
2218}
2219
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002220u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002221{
2222 struct perf_event *child;
2223 u64 total = 0;
2224
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002225 *enabled = 0;
2226 *running = 0;
2227
Peter Zijlstra6f105812009-11-20 22:19:56 +01002228 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002229 total += perf_event_read(event);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002230 *enabled += event->total_time_enabled +
2231 atomic64_read(&event->child_total_time_enabled);
2232 *running += event->total_time_running +
2233 atomic64_read(&event->child_total_time_running);
2234
2235 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002236 total += perf_event_read(child);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002237 *enabled += child->total_time_enabled;
2238 *running += child->total_time_running;
2239 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002240 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002241
2242 return total;
2243}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002244EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002245
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002246static int perf_event_read_group(struct perf_event *event,
2247 u64 read_format, char __user *buf)
2248{
2249 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01002250 int n = 0, size = 0, ret = -EFAULT;
2251 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002252 u64 values[5];
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002253 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002254
Peter Zijlstra6f105812009-11-20 22:19:56 +01002255 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002256 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002257
2258 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002259 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2260 values[n++] = enabled;
2261 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2262 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002263 values[n++] = count;
2264 if (read_format & PERF_FORMAT_ID)
2265 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002266
2267 size = n * sizeof(u64);
2268
2269 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01002270 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002271
Peter Zijlstra6f105812009-11-20 22:19:56 +01002272 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002273
2274 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01002275 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002276
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002277 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01002278 if (read_format & PERF_FORMAT_ID)
2279 values[n++] = primary_event_id(sub);
2280
2281 size = n * sizeof(u64);
2282
Stephane Eranian184d3da2009-11-23 21:40:49 -08002283 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002284 ret = -EFAULT;
2285 goto unlock;
2286 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002287
2288 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002289 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002290unlock:
2291 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002292
Peter Zijlstraabf48682009-11-20 22:19:49 +01002293 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002294}
2295
2296static int perf_event_read_one(struct perf_event *event,
2297 u64 read_format, char __user *buf)
2298{
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002299 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002300 u64 values[4];
2301 int n = 0;
2302
Peter Zijlstra59ed446f2009-11-20 22:19:55 +01002303 values[n++] = perf_event_read_value(event, &enabled, &running);
2304 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2305 values[n++] = enabled;
2306 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2307 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002308 if (read_format & PERF_FORMAT_ID)
2309 values[n++] = primary_event_id(event);
2310
2311 if (copy_to_user(buf, values, n * sizeof(u64)))
2312 return -EFAULT;
2313
2314 return n * sizeof(u64);
2315}
2316
2317/*
2318 * Read the performance event - simple non blocking version for now
2319 */
2320static ssize_t
2321perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2322{
2323 u64 read_format = event->attr.read_format;
2324 int ret;
2325
2326 /*
2327 * Return end-of-file for a read on a event that is in
2328 * error state (i.e. because it was pinned but it couldn't be
2329 * scheduled on to the CPU at some point).
2330 */
2331 if (event->state == PERF_EVENT_STATE_ERROR)
2332 return 0;
2333
2334 if (count < perf_event_read_size(event))
2335 return -ENOSPC;
2336
2337 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002338 if (read_format & PERF_FORMAT_GROUP)
2339 ret = perf_event_read_group(event, read_format, buf);
2340 else
2341 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002342
2343 return ret;
2344}
2345
2346static ssize_t
2347perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2348{
2349 struct perf_event *event = file->private_data;
2350
2351 return perf_read_hw(event, buf, count);
2352}
2353
2354static unsigned int perf_poll(struct file *file, poll_table *wait)
2355{
2356 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002357 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002358 unsigned int events = POLL_HUP;
2359
2360 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002361 buffer = rcu_dereference(event->buffer);
2362 if (buffer)
2363 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002364 rcu_read_unlock();
2365
2366 poll_wait(file, &event->waitq, wait);
2367
2368 return events;
2369}
2370
2371static void perf_event_reset(struct perf_event *event)
2372{
2373 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002374 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002375 perf_event_update_userpage(event);
2376}
2377
2378/*
2379 * Holding the top-level event's child_mutex means that any
2380 * descendant process that has inherited this event will block
2381 * in sync_child_event if it goes to exit, thus satisfying the
2382 * task existence requirements of perf_event_enable/disable.
2383 */
2384static void perf_event_for_each_child(struct perf_event *event,
2385 void (*func)(struct perf_event *))
2386{
2387 struct perf_event *child;
2388
2389 WARN_ON_ONCE(event->ctx->parent_ctx);
2390 mutex_lock(&event->child_mutex);
2391 func(event);
2392 list_for_each_entry(child, &event->child_list, child_list)
2393 func(child);
2394 mutex_unlock(&event->child_mutex);
2395}
2396
2397static void perf_event_for_each(struct perf_event *event,
2398 void (*func)(struct perf_event *))
2399{
2400 struct perf_event_context *ctx = event->ctx;
2401 struct perf_event *sibling;
2402
2403 WARN_ON_ONCE(ctx->parent_ctx);
2404 mutex_lock(&ctx->mutex);
2405 event = event->group_leader;
2406
2407 perf_event_for_each_child(event, func);
2408 func(event);
2409 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2410 perf_event_for_each_child(event, func);
2411 mutex_unlock(&ctx->mutex);
2412}
2413
2414static int perf_event_period(struct perf_event *event, u64 __user *arg)
2415{
2416 struct perf_event_context *ctx = event->ctx;
2417 unsigned long size;
2418 int ret = 0;
2419 u64 value;
2420
2421 if (!event->attr.sample_period)
2422 return -EINVAL;
2423
2424 size = copy_from_user(&value, arg, sizeof(value));
2425 if (size != sizeof(value))
2426 return -EFAULT;
2427
2428 if (!value)
2429 return -EINVAL;
2430
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002431 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002432 if (event->attr.freq) {
2433 if (value > sysctl_perf_event_sample_rate) {
2434 ret = -EINVAL;
2435 goto unlock;
2436 }
2437
2438 event->attr.sample_freq = value;
2439 } else {
2440 event->attr.sample_period = value;
2441 event->hw.sample_period = value;
2442 }
2443unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002444 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002445
2446 return ret;
2447}
2448
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002449static const struct file_operations perf_fops;
2450
2451static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2452{
2453 struct file *file;
2454
2455 file = fget_light(fd, fput_needed);
2456 if (!file)
2457 return ERR_PTR(-EBADF);
2458
2459 if (file->f_op != &perf_fops) {
2460 fput_light(file, *fput_needed);
2461 *fput_needed = 0;
2462 return ERR_PTR(-EBADF);
2463 }
2464
2465 return file->private_data;
2466}
2467
2468static int perf_event_set_output(struct perf_event *event,
2469 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08002470static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002471
2472static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2473{
2474 struct perf_event *event = file->private_data;
2475 void (*func)(struct perf_event *);
2476 u32 flags = arg;
2477
2478 switch (cmd) {
2479 case PERF_EVENT_IOC_ENABLE:
2480 func = perf_event_enable;
2481 break;
2482 case PERF_EVENT_IOC_DISABLE:
2483 func = perf_event_disable;
2484 break;
2485 case PERF_EVENT_IOC_RESET:
2486 func = perf_event_reset;
2487 break;
2488
2489 case PERF_EVENT_IOC_REFRESH:
2490 return perf_event_refresh(event, arg);
2491
2492 case PERF_EVENT_IOC_PERIOD:
2493 return perf_event_period(event, (u64 __user *)arg);
2494
2495 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002496 {
2497 struct perf_event *output_event = NULL;
2498 int fput_needed = 0;
2499 int ret;
2500
2501 if (arg != -1) {
2502 output_event = perf_fget_light(arg, &fput_needed);
2503 if (IS_ERR(output_event))
2504 return PTR_ERR(output_event);
2505 }
2506
2507 ret = perf_event_set_output(event, output_event);
2508 if (output_event)
2509 fput_light(output_event->filp, fput_needed);
2510
2511 return ret;
2512 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002513
Li Zefan6fb29152009-10-15 11:21:42 +08002514 case PERF_EVENT_IOC_SET_FILTER:
2515 return perf_event_set_filter(event, (void __user *)arg);
2516
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002517 default:
2518 return -ENOTTY;
2519 }
2520
2521 if (flags & PERF_IOC_FLAG_GROUP)
2522 perf_event_for_each(event, func);
2523 else
2524 perf_event_for_each_child(event, func);
2525
2526 return 0;
2527}
2528
2529int perf_event_task_enable(void)
2530{
2531 struct perf_event *event;
2532
2533 mutex_lock(&current->perf_event_mutex);
2534 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2535 perf_event_for_each_child(event, perf_event_enable);
2536 mutex_unlock(&current->perf_event_mutex);
2537
2538 return 0;
2539}
2540
2541int perf_event_task_disable(void)
2542{
2543 struct perf_event *event;
2544
2545 mutex_lock(&current->perf_event_mutex);
2546 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2547 perf_event_for_each_child(event, perf_event_disable);
2548 mutex_unlock(&current->perf_event_mutex);
2549
2550 return 0;
2551}
2552
2553#ifndef PERF_EVENT_INDEX_OFFSET
2554# define PERF_EVENT_INDEX_OFFSET 0
2555#endif
2556
2557static int perf_event_index(struct perf_event *event)
2558{
2559 if (event->state != PERF_EVENT_STATE_ACTIVE)
2560 return 0;
2561
2562 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2563}
2564
2565/*
2566 * Callers need to ensure there can be no nesting of this function, otherwise
2567 * the seqlock logic goes bad. We can not serialize this because the arch
2568 * code calls this from NMI context.
2569 */
2570void perf_event_update_userpage(struct perf_event *event)
2571{
2572 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002573 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002574
2575 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002576 buffer = rcu_dereference(event->buffer);
2577 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002578 goto unlock;
2579
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002580 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002581
2582 /*
2583 * Disable preemption so as to not let the corresponding user-space
2584 * spin too long if we get preempted.
2585 */
2586 preempt_disable();
2587 ++userpg->lock;
2588 barrier();
2589 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002590 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002591 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02002592 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002593
2594 userpg->time_enabled = event->total_time_enabled +
2595 atomic64_read(&event->child_total_time_enabled);
2596
2597 userpg->time_running = event->total_time_running +
2598 atomic64_read(&event->child_total_time_running);
2599
2600 barrier();
2601 ++userpg->lock;
2602 preempt_enable();
2603unlock:
2604 rcu_read_unlock();
2605}
2606
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002607static unsigned long perf_data_size(struct perf_buffer *buffer);
2608
2609static void
2610perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2611{
2612 long max_size = perf_data_size(buffer);
2613
2614 if (watermark)
2615 buffer->watermark = min(max_size, watermark);
2616
2617 if (!buffer->watermark)
2618 buffer->watermark = max_size / 2;
2619
2620 if (flags & PERF_BUFFER_WRITABLE)
2621 buffer->writable = 1;
2622
2623 atomic_set(&buffer->refcount, 1);
2624}
2625
Peter Zijlstra906010b2009-09-21 16:08:49 +02002626#ifndef CONFIG_PERF_USE_VMALLOC
2627
2628/*
2629 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2630 */
2631
2632static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002633perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002634{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002635 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002636 return NULL;
2637
2638 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002639 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002640
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002641 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002642}
2643
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02002644static void *perf_mmap_alloc_page(int cpu)
2645{
2646 struct page *page;
2647 int node;
2648
2649 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2650 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2651 if (!page)
2652 return NULL;
2653
2654 return page_address(page);
2655}
2656
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002657static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002658perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002659{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002660 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002661 unsigned long size;
2662 int i;
2663
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002664 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002665 size += nr_pages * sizeof(void *);
2666
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002667 buffer = kzalloc(size, GFP_KERNEL);
2668 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002669 goto fail;
2670
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002671 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002672 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002673 goto fail_user_page;
2674
2675 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002676 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002677 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002678 goto fail_data_pages;
2679 }
2680
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002681 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002682
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002683 perf_buffer_init(buffer, watermark, flags);
2684
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002685 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002686
2687fail_data_pages:
2688 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002689 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002690
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002691 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002692
2693fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002694 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002695
2696fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002697 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002698}
2699
2700static void perf_mmap_free_page(unsigned long addr)
2701{
2702 struct page *page = virt_to_page((void *)addr);
2703
2704 page->mapping = NULL;
2705 __free_page(page);
2706}
2707
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002708static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002709{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002710 int i;
2711
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002712 perf_mmap_free_page((unsigned long)buffer->user_page);
2713 for (i = 0; i < buffer->nr_pages; i++)
2714 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2715 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002716}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002717
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002718static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002719{
2720 return 0;
2721}
2722
Peter Zijlstra906010b2009-09-21 16:08:49 +02002723#else
2724
2725/*
2726 * Back perf_mmap() with vmalloc memory.
2727 *
2728 * Required for architectures that have d-cache aliasing issues.
2729 */
2730
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002731static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002732{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002733 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002734}
2735
Peter Zijlstra906010b2009-09-21 16:08:49 +02002736static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002737perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002738{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002739 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02002740 return NULL;
2741
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002742 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002743}
2744
2745static void perf_mmap_unmark_page(void *addr)
2746{
2747 struct page *page = vmalloc_to_page(addr);
2748
2749 page->mapping = NULL;
2750}
2751
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002752static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002753{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002754 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002755 void *base;
2756 int i, nr;
2757
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002758 buffer = container_of(work, struct perf_buffer, work);
2759 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002760
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002761 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002762 for (i = 0; i < nr + 1; i++)
2763 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2764
2765 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002766 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002767}
2768
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002769static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002770{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002771 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002772}
2773
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002774static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002775perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002776{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002777 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002778 unsigned long size;
2779 void *all_buf;
2780
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002781 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002782 size += sizeof(void *);
2783
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002784 buffer = kzalloc(size, GFP_KERNEL);
2785 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002786 goto fail;
2787
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002788 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002789
2790 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2791 if (!all_buf)
2792 goto fail_all_buf;
2793
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002794 buffer->user_page = all_buf;
2795 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2796 buffer->page_order = ilog2(nr_pages);
2797 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002798
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002799 perf_buffer_init(buffer, watermark, flags);
2800
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002801 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002802
2803fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002804 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002805
2806fail:
2807 return NULL;
2808}
2809
2810#endif
2811
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002812static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002813{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002814 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002815}
2816
Peter Zijlstra906010b2009-09-21 16:08:49 +02002817static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2818{
2819 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002820 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002821 int ret = VM_FAULT_SIGBUS;
2822
2823 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2824 if (vmf->pgoff == 0)
2825 ret = 0;
2826 return ret;
2827 }
2828
2829 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002830 buffer = rcu_dereference(event->buffer);
2831 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002832 goto unlock;
2833
2834 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2835 goto unlock;
2836
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002837 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002838 if (!vmf->page)
2839 goto unlock;
2840
2841 get_page(vmf->page);
2842 vmf->page->mapping = vma->vm_file->f_mapping;
2843 vmf->page->index = vmf->pgoff;
2844
2845 ret = 0;
2846unlock:
2847 rcu_read_unlock();
2848
2849 return ret;
2850}
2851
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002852static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002853{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002854 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002855
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002856 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2857 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002858}
2859
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002860static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002861{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002862 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002863
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002864 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002865 buffer = rcu_dereference(event->buffer);
2866 if (buffer) {
2867 if (!atomic_inc_not_zero(&buffer->refcount))
2868 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002869 }
2870 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002871
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002872 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002873}
2874
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002875static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002876{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002877 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002878 return;
2879
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002880 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002881}
2882
2883static void perf_mmap_open(struct vm_area_struct *vma)
2884{
2885 struct perf_event *event = vma->vm_file->private_data;
2886
2887 atomic_inc(&event->mmap_count);
2888}
2889
2890static void perf_mmap_close(struct vm_area_struct *vma)
2891{
2892 struct perf_event *event = vma->vm_file->private_data;
2893
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002894 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002895 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002896 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002897 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002898
Peter Zijlstra906010b2009-09-21 16:08:49 +02002899 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002900 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002901 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002902 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002903
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002904 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002905 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002906 }
2907}
2908
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002909static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002910 .open = perf_mmap_open,
2911 .close = perf_mmap_close,
2912 .fault = perf_mmap_fault,
2913 .page_mkwrite = perf_mmap_fault,
2914};
2915
2916static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2917{
2918 struct perf_event *event = file->private_data;
2919 unsigned long user_locked, user_lock_limit;
2920 struct user_struct *user = current_user();
2921 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002922 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002923 unsigned long vma_size;
2924 unsigned long nr_pages;
2925 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002926 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002927
Peter Zijlstrac7920612010-05-18 10:33:24 +02002928 /*
2929 * Don't allow mmap() of inherited per-task counters. This would
2930 * create a performance issue due to all children writing to the
2931 * same buffer.
2932 */
2933 if (event->cpu == -1 && event->attr.inherit)
2934 return -EINVAL;
2935
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002936 if (!(vma->vm_flags & VM_SHARED))
2937 return -EINVAL;
2938
2939 vma_size = vma->vm_end - vma->vm_start;
2940 nr_pages = (vma_size / PAGE_SIZE) - 1;
2941
2942 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002943 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002944 * can do bitmasks instead of modulo.
2945 */
2946 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2947 return -EINVAL;
2948
2949 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2950 return -EINVAL;
2951
2952 if (vma->vm_pgoff != 0)
2953 return -EINVAL;
2954
2955 WARN_ON_ONCE(event->ctx->parent_ctx);
2956 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002957 if (event->buffer) {
2958 if (event->buffer->nr_pages == nr_pages)
2959 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002960 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002961 ret = -EINVAL;
2962 goto unlock;
2963 }
2964
2965 user_extra = nr_pages + 1;
2966 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2967
2968 /*
2969 * Increase the limit linearly with more CPUs:
2970 */
2971 user_lock_limit *= num_online_cpus();
2972
2973 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2974
2975 extra = 0;
2976 if (user_locked > user_lock_limit)
2977 extra = user_locked - user_lock_limit;
2978
Jiri Slaby78d7d402010-03-05 13:42:54 -08002979 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002980 lock_limit >>= PAGE_SHIFT;
2981 locked = vma->vm_mm->locked_vm + extra;
2982
2983 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2984 !capable(CAP_IPC_LOCK)) {
2985 ret = -EPERM;
2986 goto unlock;
2987 }
2988
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002989 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002990
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002991 if (vma->vm_flags & VM_WRITE)
2992 flags |= PERF_BUFFER_WRITABLE;
2993
2994 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
2995 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002996 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002997 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002998 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002999 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003000 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003001
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003002 atomic_long_add(user_extra, &user->locked_vm);
3003 event->mmap_locked = extra;
3004 event->mmap_user = get_current_user();
3005 vma->vm_mm->locked_vm += event->mmap_locked;
3006
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003007unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003008 if (!ret)
3009 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003010 mutex_unlock(&event->mmap_mutex);
3011
3012 vma->vm_flags |= VM_RESERVED;
3013 vma->vm_ops = &perf_mmap_vmops;
3014
3015 return ret;
3016}
3017
3018static int perf_fasync(int fd, struct file *filp, int on)
3019{
3020 struct inode *inode = filp->f_path.dentry->d_inode;
3021 struct perf_event *event = filp->private_data;
3022 int retval;
3023
3024 mutex_lock(&inode->i_mutex);
3025 retval = fasync_helper(fd, filp, on, &event->fasync);
3026 mutex_unlock(&inode->i_mutex);
3027
3028 if (retval < 0)
3029 return retval;
3030
3031 return 0;
3032}
3033
3034static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003035 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003036 .release = perf_release,
3037 .read = perf_read,
3038 .poll = perf_poll,
3039 .unlocked_ioctl = perf_ioctl,
3040 .compat_ioctl = perf_ioctl,
3041 .mmap = perf_mmap,
3042 .fasync = perf_fasync,
3043};
3044
3045/*
3046 * Perf event wakeup
3047 *
3048 * If there's data, ensure we set the poll() state and publish everything
3049 * to user-space before waking everybody up.
3050 */
3051
3052void perf_event_wakeup(struct perf_event *event)
3053{
3054 wake_up_all(&event->waitq);
3055
3056 if (event->pending_kill) {
3057 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3058 event->pending_kill = 0;
3059 }
3060}
3061
3062/*
3063 * Pending wakeups
3064 *
3065 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
3066 *
3067 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
3068 * single linked list and use cmpxchg() to add entries lockless.
3069 */
3070
3071static void perf_pending_event(struct perf_pending_entry *entry)
3072{
3073 struct perf_event *event = container_of(entry,
3074 struct perf_event, pending);
3075
3076 if (event->pending_disable) {
3077 event->pending_disable = 0;
3078 __perf_event_disable(event);
3079 }
3080
3081 if (event->pending_wakeup) {
3082 event->pending_wakeup = 0;
3083 perf_event_wakeup(event);
3084 }
3085}
3086
3087#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
3088
3089static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
3090 PENDING_TAIL,
3091};
3092
3093static void perf_pending_queue(struct perf_pending_entry *entry,
3094 void (*func)(struct perf_pending_entry *))
3095{
3096 struct perf_pending_entry **head;
3097
3098 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
3099 return;
3100
3101 entry->func = func;
3102
3103 head = &get_cpu_var(perf_pending_head);
3104
3105 do {
3106 entry->next = *head;
3107 } while (cmpxchg(head, entry->next, entry) != entry->next);
3108
3109 set_perf_event_pending();
3110
3111 put_cpu_var(perf_pending_head);
3112}
3113
3114static int __perf_pending_run(void)
3115{
3116 struct perf_pending_entry *list;
3117 int nr = 0;
3118
3119 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
3120 while (list != PENDING_TAIL) {
3121 void (*func)(struct perf_pending_entry *);
3122 struct perf_pending_entry *entry = list;
3123
3124 list = list->next;
3125
3126 func = entry->func;
3127 entry->next = NULL;
3128 /*
3129 * Ensure we observe the unqueue before we issue the wakeup,
3130 * so that we won't be waiting forever.
3131 * -- see perf_not_pending().
3132 */
3133 smp_wmb();
3134
3135 func(entry);
3136 nr++;
3137 }
3138
3139 return nr;
3140}
3141
3142static inline int perf_not_pending(struct perf_event *event)
3143{
3144 /*
3145 * If we flush on whatever cpu we run, there is a chance we don't
3146 * need to wait.
3147 */
3148 get_cpu();
3149 __perf_pending_run();
3150 put_cpu();
3151
3152 /*
3153 * Ensure we see the proper queue state before going to sleep
3154 * so that we do not miss the wakeup. -- see perf_pending_handle()
3155 */
3156 smp_rmb();
3157 return event->pending.next == NULL;
3158}
3159
3160static void perf_pending_sync(struct perf_event *event)
3161{
3162 wait_event(event->waitq, perf_not_pending(event));
3163}
3164
3165void perf_event_do_pending(void)
3166{
3167 __perf_pending_run();
3168}
3169
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003170/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003171 * We assume there is only KVM supporting the callbacks.
3172 * Later on, we might change it to a list if there is
3173 * another virtualization implementation supporting the callbacks.
3174 */
3175struct perf_guest_info_callbacks *perf_guest_cbs;
3176
3177int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3178{
3179 perf_guest_cbs = cbs;
3180 return 0;
3181}
3182EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3183
3184int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3185{
3186 perf_guest_cbs = NULL;
3187 return 0;
3188}
3189EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3190
3191/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003192 * Output
3193 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003194static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003195 unsigned long offset, unsigned long head)
3196{
3197 unsigned long mask;
3198
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003199 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003200 return true;
3201
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003202 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003203
3204 offset = (offset - tail) & mask;
3205 head = (head - tail) & mask;
3206
3207 if ((int)(head - offset) < 0)
3208 return false;
3209
3210 return true;
3211}
3212
3213static void perf_output_wakeup(struct perf_output_handle *handle)
3214{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003215 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003216
3217 if (handle->nmi) {
3218 handle->event->pending_wakeup = 1;
3219 perf_pending_queue(&handle->event->pending,
3220 perf_pending_event);
3221 } else
3222 perf_event_wakeup(handle->event);
3223}
3224
3225/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003226 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003227 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003228 * cannot fully serialize things.
3229 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003230 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003231 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003232 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003233static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003234{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003235 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003236
Peter Zijlstraef607772010-05-18 10:50:41 +02003237 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003238 local_inc(&buffer->nest);
3239 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003240}
3241
Peter Zijlstraef607772010-05-18 10:50:41 +02003242static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003243{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003244 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003245 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003246
3247again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003248 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003249
3250 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003251 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003252 */
3253
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003254 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003255 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003256
3257 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003258 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003259 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003260 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003261 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003262 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003263
Peter Zijlstraef607772010-05-18 10:50:41 +02003264 /*
3265 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003266 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003267 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003268 if (unlikely(head != local_read(&buffer->head))) {
3269 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003270 goto again;
3271 }
3272
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003273 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003274 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003275
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003276 out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003277 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003278}
3279
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003280__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003281 const void *buf, unsigned int len)
3282{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003283 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003284 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003285
3286 memcpy(handle->addr, buf, size);
3287
3288 len -= size;
3289 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003290 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003291 handle->size -= size;
3292 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003293 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003294
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003295 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003296 handle->page &= buffer->nr_pages - 1;
3297 handle->addr = buffer->data_pages[handle->page];
3298 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003299 }
3300 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003301}
3302
3303int perf_output_begin(struct perf_output_handle *handle,
3304 struct perf_event *event, unsigned int size,
3305 int nmi, int sample)
3306{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003307 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003308 unsigned long tail, offset, head;
3309 int have_lost;
3310 struct {
3311 struct perf_event_header header;
3312 u64 id;
3313 u64 lost;
3314 } lost_event;
3315
3316 rcu_read_lock();
3317 /*
3318 * For inherited events we send all the output towards the parent.
3319 */
3320 if (event->parent)
3321 event = event->parent;
3322
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003323 buffer = rcu_dereference(event->buffer);
3324 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003325 goto out;
3326
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003327 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003328 handle->event = event;
3329 handle->nmi = nmi;
3330 handle->sample = sample;
3331
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003332 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02003333 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003334
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003335 have_lost = local_read(&buffer->lost);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003336 if (have_lost)
3337 size += sizeof(lost_event);
3338
Peter Zijlstraef607772010-05-18 10:50:41 +02003339 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003340
3341 do {
3342 /*
3343 * Userspace could choose to issue a mb() before updating the
3344 * tail pointer. So that all reads will be completed before the
3345 * write is issued.
3346 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003347 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003348 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003349 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003350 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003351 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003352 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003353 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003354
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003355 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3356 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003357
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003358 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3359 handle->page &= buffer->nr_pages - 1;
3360 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3361 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003362 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003363 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003364
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003365 if (have_lost) {
3366 lost_event.header.type = PERF_RECORD_LOST;
3367 lost_event.header.misc = 0;
3368 lost_event.header.size = sizeof(lost_event);
3369 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003370 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003371
3372 perf_output_put(handle, lost_event);
3373 }
3374
3375 return 0;
3376
3377fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003378 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02003379 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003380out:
3381 rcu_read_unlock();
3382
3383 return -ENOSPC;
3384}
3385
3386void perf_output_end(struct perf_output_handle *handle)
3387{
3388 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003389 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003390
3391 int wakeup_events = event->attr.wakeup_events;
3392
3393 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003394 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003395 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003396 local_sub(wakeup_events, &buffer->events);
3397 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003398 }
3399 }
3400
Peter Zijlstraef607772010-05-18 10:50:41 +02003401 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402 rcu_read_unlock();
3403}
3404
3405static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3406{
3407 /*
3408 * only top level events have the pid namespace they were created in
3409 */
3410 if (event->parent)
3411 event = event->parent;
3412
3413 return task_tgid_nr_ns(p, event->ns);
3414}
3415
3416static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3417{
3418 /*
3419 * only top level events have the pid namespace they were created in
3420 */
3421 if (event->parent)
3422 event = event->parent;
3423
3424 return task_pid_nr_ns(p, event->ns);
3425}
3426
3427static void perf_output_read_one(struct perf_output_handle *handle,
3428 struct perf_event *event)
3429{
3430 u64 read_format = event->attr.read_format;
3431 u64 values[4];
3432 int n = 0;
3433
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003434 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003435 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3436 values[n++] = event->total_time_enabled +
3437 atomic64_read(&event->child_total_time_enabled);
3438 }
3439 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3440 values[n++] = event->total_time_running +
3441 atomic64_read(&event->child_total_time_running);
3442 }
3443 if (read_format & PERF_FORMAT_ID)
3444 values[n++] = primary_event_id(event);
3445
3446 perf_output_copy(handle, values, n * sizeof(u64));
3447}
3448
3449/*
3450 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3451 */
3452static void perf_output_read_group(struct perf_output_handle *handle,
3453 struct perf_event *event)
3454{
3455 struct perf_event *leader = event->group_leader, *sub;
3456 u64 read_format = event->attr.read_format;
3457 u64 values[5];
3458 int n = 0;
3459
3460 values[n++] = 1 + leader->nr_siblings;
3461
3462 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3463 values[n++] = leader->total_time_enabled;
3464
3465 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3466 values[n++] = leader->total_time_running;
3467
3468 if (leader != event)
3469 leader->pmu->read(leader);
3470
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003471 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003472 if (read_format & PERF_FORMAT_ID)
3473 values[n++] = primary_event_id(leader);
3474
3475 perf_output_copy(handle, values, n * sizeof(u64));
3476
3477 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3478 n = 0;
3479
3480 if (sub != event)
3481 sub->pmu->read(sub);
3482
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003483 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003484 if (read_format & PERF_FORMAT_ID)
3485 values[n++] = primary_event_id(sub);
3486
3487 perf_output_copy(handle, values, n * sizeof(u64));
3488 }
3489}
3490
3491static void perf_output_read(struct perf_output_handle *handle,
3492 struct perf_event *event)
3493{
3494 if (event->attr.read_format & PERF_FORMAT_GROUP)
3495 perf_output_read_group(handle, event);
3496 else
3497 perf_output_read_one(handle, event);
3498}
3499
3500void perf_output_sample(struct perf_output_handle *handle,
3501 struct perf_event_header *header,
3502 struct perf_sample_data *data,
3503 struct perf_event *event)
3504{
3505 u64 sample_type = data->type;
3506
3507 perf_output_put(handle, *header);
3508
3509 if (sample_type & PERF_SAMPLE_IP)
3510 perf_output_put(handle, data->ip);
3511
3512 if (sample_type & PERF_SAMPLE_TID)
3513 perf_output_put(handle, data->tid_entry);
3514
3515 if (sample_type & PERF_SAMPLE_TIME)
3516 perf_output_put(handle, data->time);
3517
3518 if (sample_type & PERF_SAMPLE_ADDR)
3519 perf_output_put(handle, data->addr);
3520
3521 if (sample_type & PERF_SAMPLE_ID)
3522 perf_output_put(handle, data->id);
3523
3524 if (sample_type & PERF_SAMPLE_STREAM_ID)
3525 perf_output_put(handle, data->stream_id);
3526
3527 if (sample_type & PERF_SAMPLE_CPU)
3528 perf_output_put(handle, data->cpu_entry);
3529
3530 if (sample_type & PERF_SAMPLE_PERIOD)
3531 perf_output_put(handle, data->period);
3532
3533 if (sample_type & PERF_SAMPLE_READ)
3534 perf_output_read(handle, event);
3535
3536 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3537 if (data->callchain) {
3538 int size = 1;
3539
3540 if (data->callchain)
3541 size += data->callchain->nr;
3542
3543 size *= sizeof(u64);
3544
3545 perf_output_copy(handle, data->callchain, size);
3546 } else {
3547 u64 nr = 0;
3548 perf_output_put(handle, nr);
3549 }
3550 }
3551
3552 if (sample_type & PERF_SAMPLE_RAW) {
3553 if (data->raw) {
3554 perf_output_put(handle, data->raw->size);
3555 perf_output_copy(handle, data->raw->data,
3556 data->raw->size);
3557 } else {
3558 struct {
3559 u32 size;
3560 u32 data;
3561 } raw = {
3562 .size = sizeof(u32),
3563 .data = 0,
3564 };
3565 perf_output_put(handle, raw);
3566 }
3567 }
3568}
3569
3570void perf_prepare_sample(struct perf_event_header *header,
3571 struct perf_sample_data *data,
3572 struct perf_event *event,
3573 struct pt_regs *regs)
3574{
3575 u64 sample_type = event->attr.sample_type;
3576
3577 data->type = sample_type;
3578
3579 header->type = PERF_RECORD_SAMPLE;
3580 header->size = sizeof(*header);
3581
3582 header->misc = 0;
3583 header->misc |= perf_misc_flags(regs);
3584
3585 if (sample_type & PERF_SAMPLE_IP) {
3586 data->ip = perf_instruction_pointer(regs);
3587
3588 header->size += sizeof(data->ip);
3589 }
3590
3591 if (sample_type & PERF_SAMPLE_TID) {
3592 /* namespace issues */
3593 data->tid_entry.pid = perf_event_pid(event, current);
3594 data->tid_entry.tid = perf_event_tid(event, current);
3595
3596 header->size += sizeof(data->tid_entry);
3597 }
3598
3599 if (sample_type & PERF_SAMPLE_TIME) {
3600 data->time = perf_clock();
3601
3602 header->size += sizeof(data->time);
3603 }
3604
3605 if (sample_type & PERF_SAMPLE_ADDR)
3606 header->size += sizeof(data->addr);
3607
3608 if (sample_type & PERF_SAMPLE_ID) {
3609 data->id = primary_event_id(event);
3610
3611 header->size += sizeof(data->id);
3612 }
3613
3614 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3615 data->stream_id = event->id;
3616
3617 header->size += sizeof(data->stream_id);
3618 }
3619
3620 if (sample_type & PERF_SAMPLE_CPU) {
3621 data->cpu_entry.cpu = raw_smp_processor_id();
3622 data->cpu_entry.reserved = 0;
3623
3624 header->size += sizeof(data->cpu_entry);
3625 }
3626
3627 if (sample_type & PERF_SAMPLE_PERIOD)
3628 header->size += sizeof(data->period);
3629
3630 if (sample_type & PERF_SAMPLE_READ)
3631 header->size += perf_event_read_size(event);
3632
3633 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3634 int size = 1;
3635
3636 data->callchain = perf_callchain(regs);
3637
3638 if (data->callchain)
3639 size += data->callchain->nr;
3640
3641 header->size += size * sizeof(u64);
3642 }
3643
3644 if (sample_type & PERF_SAMPLE_RAW) {
3645 int size = sizeof(u32);
3646
3647 if (data->raw)
3648 size += data->raw->size;
3649 else
3650 size += sizeof(u32);
3651
3652 WARN_ON_ONCE(size & (sizeof(u64)-1));
3653 header->size += size;
3654 }
3655}
3656
3657static void perf_event_output(struct perf_event *event, int nmi,
3658 struct perf_sample_data *data,
3659 struct pt_regs *regs)
3660{
3661 struct perf_output_handle handle;
3662 struct perf_event_header header;
3663
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003664 /* protect the callchain buffers */
3665 rcu_read_lock();
3666
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003667 perf_prepare_sample(&header, data, event, regs);
3668
3669 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003670 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003671
3672 perf_output_sample(&handle, &header, data, event);
3673
3674 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003675
3676exit:
3677 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003678}
3679
3680/*
3681 * read event_id
3682 */
3683
3684struct perf_read_event {
3685 struct perf_event_header header;
3686
3687 u32 pid;
3688 u32 tid;
3689};
3690
3691static void
3692perf_event_read_event(struct perf_event *event,
3693 struct task_struct *task)
3694{
3695 struct perf_output_handle handle;
3696 struct perf_read_event read_event = {
3697 .header = {
3698 .type = PERF_RECORD_READ,
3699 .misc = 0,
3700 .size = sizeof(read_event) + perf_event_read_size(event),
3701 },
3702 .pid = perf_event_pid(event, task),
3703 .tid = perf_event_tid(event, task),
3704 };
3705 int ret;
3706
3707 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3708 if (ret)
3709 return;
3710
3711 perf_output_put(&handle, read_event);
3712 perf_output_read(&handle, event);
3713
3714 perf_output_end(&handle);
3715}
3716
3717/*
3718 * task tracking -- fork/exit
3719 *
Eric B Munson3af9e852010-05-18 15:30:49 +01003720 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003721 */
3722
3723struct perf_task_event {
3724 struct task_struct *task;
3725 struct perf_event_context *task_ctx;
3726
3727 struct {
3728 struct perf_event_header header;
3729
3730 u32 pid;
3731 u32 ppid;
3732 u32 tid;
3733 u32 ptid;
3734 u64 time;
3735 } event_id;
3736};
3737
3738static void perf_event_task_output(struct perf_event *event,
3739 struct perf_task_event *task_event)
3740{
3741 struct perf_output_handle handle;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003742 struct task_struct *task = task_event->task;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003743 int size, ret;
3744
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003745 size = task_event->event_id.header.size;
3746 ret = perf_output_begin(&handle, event, size, 0, 0);
3747
Peter Zijlstraef607772010-05-18 10:50:41 +02003748 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003749 return;
3750
3751 task_event->event_id.pid = perf_event_pid(event, task);
3752 task_event->event_id.ppid = perf_event_pid(event, current);
3753
3754 task_event->event_id.tid = perf_event_tid(event, task);
3755 task_event->event_id.ptid = perf_event_tid(event, current);
3756
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003757 perf_output_put(&handle, task_event->event_id);
3758
3759 perf_output_end(&handle);
3760}
3761
3762static int perf_event_task_match(struct perf_event *event)
3763{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003764 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003765 return 0;
3766
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003767 if (event->cpu != -1 && event->cpu != smp_processor_id())
3768 return 0;
3769
Eric B Munson3af9e852010-05-18 15:30:49 +01003770 if (event->attr.comm || event->attr.mmap ||
3771 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003772 return 1;
3773
3774 return 0;
3775}
3776
3777static void perf_event_task_ctx(struct perf_event_context *ctx,
3778 struct perf_task_event *task_event)
3779{
3780 struct perf_event *event;
3781
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003782 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3783 if (perf_event_task_match(event))
3784 perf_event_task_output(event, task_event);
3785 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003786}
3787
3788static void perf_event_task_event(struct perf_task_event *task_event)
3789{
3790 struct perf_cpu_context *cpuctx;
3791 struct perf_event_context *ctx = task_event->task_ctx;
3792
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01003793 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003794 cpuctx = &get_cpu_var(perf_cpu_context);
3795 perf_event_task_ctx(&cpuctx->ctx, task_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003796 if (!ctx)
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003797 ctx = rcu_dereference(current->perf_event_ctxp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003798 if (ctx)
3799 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003800 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003801 rcu_read_unlock();
3802}
3803
3804static void perf_event_task(struct task_struct *task,
3805 struct perf_event_context *task_ctx,
3806 int new)
3807{
3808 struct perf_task_event task_event;
3809
3810 if (!atomic_read(&nr_comm_events) &&
3811 !atomic_read(&nr_mmap_events) &&
3812 !atomic_read(&nr_task_events))
3813 return;
3814
3815 task_event = (struct perf_task_event){
3816 .task = task,
3817 .task_ctx = task_ctx,
3818 .event_id = {
3819 .header = {
3820 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3821 .misc = 0,
3822 .size = sizeof(task_event.event_id),
3823 },
3824 /* .pid */
3825 /* .ppid */
3826 /* .tid */
3827 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003828 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003829 },
3830 };
3831
3832 perf_event_task_event(&task_event);
3833}
3834
3835void perf_event_fork(struct task_struct *task)
3836{
3837 perf_event_task(task, NULL, 1);
3838}
3839
3840/*
3841 * comm tracking
3842 */
3843
3844struct perf_comm_event {
3845 struct task_struct *task;
3846 char *comm;
3847 int comm_size;
3848
3849 struct {
3850 struct perf_event_header header;
3851
3852 u32 pid;
3853 u32 tid;
3854 } event_id;
3855};
3856
3857static void perf_event_comm_output(struct perf_event *event,
3858 struct perf_comm_event *comm_event)
3859{
3860 struct perf_output_handle handle;
3861 int size = comm_event->event_id.header.size;
3862 int ret = perf_output_begin(&handle, event, size, 0, 0);
3863
3864 if (ret)
3865 return;
3866
3867 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3868 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3869
3870 perf_output_put(&handle, comm_event->event_id);
3871 perf_output_copy(&handle, comm_event->comm,
3872 comm_event->comm_size);
3873 perf_output_end(&handle);
3874}
3875
3876static int perf_event_comm_match(struct perf_event *event)
3877{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003878 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003879 return 0;
3880
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003881 if (event->cpu != -1 && event->cpu != smp_processor_id())
3882 return 0;
3883
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003884 if (event->attr.comm)
3885 return 1;
3886
3887 return 0;
3888}
3889
3890static void perf_event_comm_ctx(struct perf_event_context *ctx,
3891 struct perf_comm_event *comm_event)
3892{
3893 struct perf_event *event;
3894
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003895 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3896 if (perf_event_comm_match(event))
3897 perf_event_comm_output(event, comm_event);
3898 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003899}
3900
3901static void perf_event_comm_event(struct perf_comm_event *comm_event)
3902{
3903 struct perf_cpu_context *cpuctx;
3904 struct perf_event_context *ctx;
3905 unsigned int size;
3906 char comm[TASK_COMM_LEN];
3907
3908 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01003909 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003910 size = ALIGN(strlen(comm)+1, sizeof(u64));
3911
3912 comm_event->comm = comm;
3913 comm_event->comm_size = size;
3914
3915 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3916
Peter Zijlstraf6595f32009-11-20 22:19:47 +01003917 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003918 cpuctx = &get_cpu_var(perf_cpu_context);
3919 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003920 ctx = rcu_dereference(current->perf_event_ctxp);
3921 if (ctx)
3922 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003923 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003924 rcu_read_unlock();
3925}
3926
3927void perf_event_comm(struct task_struct *task)
3928{
3929 struct perf_comm_event comm_event;
3930
3931 if (task->perf_event_ctxp)
3932 perf_event_enable_on_exec(task);
3933
3934 if (!atomic_read(&nr_comm_events))
3935 return;
3936
3937 comm_event = (struct perf_comm_event){
3938 .task = task,
3939 /* .comm */
3940 /* .comm_size */
3941 .event_id = {
3942 .header = {
3943 .type = PERF_RECORD_COMM,
3944 .misc = 0,
3945 /* .size */
3946 },
3947 /* .pid */
3948 /* .tid */
3949 },
3950 };
3951
3952 perf_event_comm_event(&comm_event);
3953}
3954
3955/*
3956 * mmap tracking
3957 */
3958
3959struct perf_mmap_event {
3960 struct vm_area_struct *vma;
3961
3962 const char *file_name;
3963 int file_size;
3964
3965 struct {
3966 struct perf_event_header header;
3967
3968 u32 pid;
3969 u32 tid;
3970 u64 start;
3971 u64 len;
3972 u64 pgoff;
3973 } event_id;
3974};
3975
3976static void perf_event_mmap_output(struct perf_event *event,
3977 struct perf_mmap_event *mmap_event)
3978{
3979 struct perf_output_handle handle;
3980 int size = mmap_event->event_id.header.size;
3981 int ret = perf_output_begin(&handle, event, size, 0, 0);
3982
3983 if (ret)
3984 return;
3985
3986 mmap_event->event_id.pid = perf_event_pid(event, current);
3987 mmap_event->event_id.tid = perf_event_tid(event, current);
3988
3989 perf_output_put(&handle, mmap_event->event_id);
3990 perf_output_copy(&handle, mmap_event->file_name,
3991 mmap_event->file_size);
3992 perf_output_end(&handle);
3993}
3994
3995static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01003996 struct perf_mmap_event *mmap_event,
3997 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003998{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003999 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004000 return 0;
4001
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004002 if (event->cpu != -1 && event->cpu != smp_processor_id())
4003 return 0;
4004
Eric B Munson3af9e852010-05-18 15:30:49 +01004005 if ((!executable && event->attr.mmap_data) ||
4006 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004007 return 1;
4008
4009 return 0;
4010}
4011
4012static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004013 struct perf_mmap_event *mmap_event,
4014 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004015{
4016 struct perf_event *event;
4017
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004018 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004019 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004020 perf_event_mmap_output(event, mmap_event);
4021 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004022}
4023
4024static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4025{
4026 struct perf_cpu_context *cpuctx;
4027 struct perf_event_context *ctx;
4028 struct vm_area_struct *vma = mmap_event->vma;
4029 struct file *file = vma->vm_file;
4030 unsigned int size;
4031 char tmp[16];
4032 char *buf = NULL;
4033 const char *name;
4034
4035 memset(tmp, 0, sizeof(tmp));
4036
4037 if (file) {
4038 /*
4039 * d_path works from the end of the buffer backwards, so we
4040 * need to add enough zero bytes after the string to handle
4041 * the 64bit alignment we do later.
4042 */
4043 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4044 if (!buf) {
4045 name = strncpy(tmp, "//enomem", sizeof(tmp));
4046 goto got_name;
4047 }
4048 name = d_path(&file->f_path, buf, PATH_MAX);
4049 if (IS_ERR(name)) {
4050 name = strncpy(tmp, "//toolong", sizeof(tmp));
4051 goto got_name;
4052 }
4053 } else {
4054 if (arch_vma_name(mmap_event->vma)) {
4055 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4056 sizeof(tmp));
4057 goto got_name;
4058 }
4059
4060 if (!vma->vm_mm) {
4061 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4062 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004063 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4064 vma->vm_end >= vma->vm_mm->brk) {
4065 name = strncpy(tmp, "[heap]", sizeof(tmp));
4066 goto got_name;
4067 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4068 vma->vm_end >= vma->vm_mm->start_stack) {
4069 name = strncpy(tmp, "[stack]", sizeof(tmp));
4070 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004071 }
4072
4073 name = strncpy(tmp, "//anon", sizeof(tmp));
4074 goto got_name;
4075 }
4076
4077got_name:
4078 size = ALIGN(strlen(name)+1, sizeof(u64));
4079
4080 mmap_event->file_name = name;
4081 mmap_event->file_size = size;
4082
4083 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4084
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004085 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004086 cpuctx = &get_cpu_var(perf_cpu_context);
Eric B Munson3af9e852010-05-18 15:30:49 +01004087 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event, vma->vm_flags & VM_EXEC);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004088 ctx = rcu_dereference(current->perf_event_ctxp);
4089 if (ctx)
Eric B Munson3af9e852010-05-18 15:30:49 +01004090 perf_event_mmap_ctx(ctx, mmap_event, vma->vm_flags & VM_EXEC);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004091 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004092 rcu_read_unlock();
4093
4094 kfree(buf);
4095}
4096
Eric B Munson3af9e852010-05-18 15:30:49 +01004097void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004098{
4099 struct perf_mmap_event mmap_event;
4100
4101 if (!atomic_read(&nr_mmap_events))
4102 return;
4103
4104 mmap_event = (struct perf_mmap_event){
4105 .vma = vma,
4106 /* .file_name */
4107 /* .file_size */
4108 .event_id = {
4109 .header = {
4110 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004111 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004112 /* .size */
4113 },
4114 /* .pid */
4115 /* .tid */
4116 .start = vma->vm_start,
4117 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004118 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004119 },
4120 };
4121
4122 perf_event_mmap_event(&mmap_event);
4123}
4124
4125/*
4126 * IRQ throttle logging
4127 */
4128
4129static void perf_log_throttle(struct perf_event *event, int enable)
4130{
4131 struct perf_output_handle handle;
4132 int ret;
4133
4134 struct {
4135 struct perf_event_header header;
4136 u64 time;
4137 u64 id;
4138 u64 stream_id;
4139 } throttle_event = {
4140 .header = {
4141 .type = PERF_RECORD_THROTTLE,
4142 .misc = 0,
4143 .size = sizeof(throttle_event),
4144 },
4145 .time = perf_clock(),
4146 .id = primary_event_id(event),
4147 .stream_id = event->id,
4148 };
4149
4150 if (enable)
4151 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4152
4153 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4154 if (ret)
4155 return;
4156
4157 perf_output_put(&handle, throttle_event);
4158 perf_output_end(&handle);
4159}
4160
4161/*
4162 * Generic event overflow handling, sampling.
4163 */
4164
4165static int __perf_event_overflow(struct perf_event *event, int nmi,
4166 int throttle, struct perf_sample_data *data,
4167 struct pt_regs *regs)
4168{
4169 int events = atomic_read(&event->event_limit);
4170 struct hw_perf_event *hwc = &event->hw;
4171 int ret = 0;
4172
4173 throttle = (throttle && event->pmu->unthrottle != NULL);
4174
4175 if (!throttle) {
4176 hwc->interrupts++;
4177 } else {
4178 if (hwc->interrupts != MAX_INTERRUPTS) {
4179 hwc->interrupts++;
4180 if (HZ * hwc->interrupts >
4181 (u64)sysctl_perf_event_sample_rate) {
4182 hwc->interrupts = MAX_INTERRUPTS;
4183 perf_log_throttle(event, 0);
4184 ret = 1;
4185 }
4186 } else {
4187 /*
4188 * Keep re-disabling events even though on the previous
4189 * pass we disabled it - just in case we raced with a
4190 * sched-in and the event got enabled again:
4191 */
4192 ret = 1;
4193 }
4194 }
4195
4196 if (event->attr.freq) {
4197 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004198 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004199
Peter Zijlstraabd50712010-01-26 18:50:16 +01004200 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004201
Peter Zijlstraabd50712010-01-26 18:50:16 +01004202 if (delta > 0 && delta < 2*TICK_NSEC)
4203 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004204 }
4205
4206 /*
4207 * XXX event_limit might not quite work as expected on inherited
4208 * events
4209 */
4210
4211 event->pending_kill = POLL_IN;
4212 if (events && atomic_dec_and_test(&event->event_limit)) {
4213 ret = 1;
4214 event->pending_kill = POLL_HUP;
4215 if (nmi) {
4216 event->pending_disable = 1;
4217 perf_pending_queue(&event->pending,
4218 perf_pending_event);
4219 } else
4220 perf_event_disable(event);
4221 }
4222
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004223 if (event->overflow_handler)
4224 event->overflow_handler(event, nmi, data, regs);
4225 else
4226 perf_event_output(event, nmi, data, regs);
4227
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004228 return ret;
4229}
4230
4231int perf_event_overflow(struct perf_event *event, int nmi,
4232 struct perf_sample_data *data,
4233 struct pt_regs *regs)
4234{
4235 return __perf_event_overflow(event, nmi, 1, data, regs);
4236}
4237
4238/*
4239 * Generic software event infrastructure
4240 */
4241
4242/*
4243 * We directly increment event->count and keep a second value in
4244 * event->hw.period_left to count intervals. This period event
4245 * is kept in the range [-sample_period, 0] so that we can use the
4246 * sign as trigger.
4247 */
4248
4249static u64 perf_swevent_set_period(struct perf_event *event)
4250{
4251 struct hw_perf_event *hwc = &event->hw;
4252 u64 period = hwc->last_period;
4253 u64 nr, offset;
4254 s64 old, val;
4255
4256 hwc->last_period = hwc->sample_period;
4257
4258again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004259 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004260 if (val < 0)
4261 return 0;
4262
4263 nr = div64_u64(period + val, period);
4264 offset = nr * period;
4265 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004266 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004267 goto again;
4268
4269 return nr;
4270}
4271
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004272static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004273 int nmi, struct perf_sample_data *data,
4274 struct pt_regs *regs)
4275{
4276 struct hw_perf_event *hwc = &event->hw;
4277 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004278
4279 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004280 if (!overflow)
4281 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004282
4283 if (hwc->interrupts == MAX_INTERRUPTS)
4284 return;
4285
4286 for (; overflow; overflow--) {
4287 if (__perf_event_overflow(event, nmi, throttle,
4288 data, regs)) {
4289 /*
4290 * We inhibit the overflow from happening when
4291 * hwc->interrupts == MAX_INTERRUPTS.
4292 */
4293 break;
4294 }
4295 throttle = 1;
4296 }
4297}
4298
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004299static void perf_swevent_add(struct perf_event *event, u64 nr,
4300 int nmi, struct perf_sample_data *data,
4301 struct pt_regs *regs)
4302{
4303 struct hw_perf_event *hwc = &event->hw;
4304
Peter Zijlstrae7850592010-05-21 14:43:08 +02004305 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004306
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004307 if (!regs)
4308 return;
4309
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004310 if (!hwc->sample_period)
4311 return;
4312
4313 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4314 return perf_swevent_overflow(event, 1, nmi, data, regs);
4315
Peter Zijlstrae7850592010-05-21 14:43:08 +02004316 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004317 return;
4318
4319 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004320}
4321
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004322static int perf_exclude_event(struct perf_event *event,
4323 struct pt_regs *regs)
4324{
4325 if (regs) {
4326 if (event->attr.exclude_user && user_mode(regs))
4327 return 1;
4328
4329 if (event->attr.exclude_kernel && !user_mode(regs))
4330 return 1;
4331 }
4332
4333 return 0;
4334}
4335
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004336static int perf_swevent_match(struct perf_event *event,
4337 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004338 u32 event_id,
4339 struct perf_sample_data *data,
4340 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004341{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004342 if (event->attr.type != type)
4343 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004344
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004345 if (event->attr.config != event_id)
4346 return 0;
4347
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004348 if (perf_exclude_event(event, regs))
4349 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004350
4351 return 1;
4352}
4353
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004354static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004355{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004356 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004357
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004358 return hash_64(val, SWEVENT_HLIST_BITS);
4359}
4360
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004361static inline struct hlist_head *
4362__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004363{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004364 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004365
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004366 return &hlist->heads[hash];
4367}
4368
4369/* For the read side: events when they trigger */
4370static inline struct hlist_head *
4371find_swevent_head_rcu(struct perf_cpu_context *ctx, u64 type, u32 event_id)
4372{
4373 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004374
4375 hlist = rcu_dereference(ctx->swevent_hlist);
4376 if (!hlist)
4377 return NULL;
4378
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004379 return __find_swevent_head(hlist, type, event_id);
4380}
4381
4382/* For the event head insertion and removal in the hlist */
4383static inline struct hlist_head *
4384find_swevent_head(struct perf_cpu_context *ctx, struct perf_event *event)
4385{
4386 struct swevent_hlist *hlist;
4387 u32 event_id = event->attr.config;
4388 u64 type = event->attr.type;
4389
4390 /*
4391 * Event scheduling is always serialized against hlist allocation
4392 * and release. Which makes the protected version suitable here.
4393 * The context lock guarantees that.
4394 */
4395 hlist = rcu_dereference_protected(ctx->swevent_hlist,
4396 lockdep_is_held(&event->ctx->lock));
4397 if (!hlist)
4398 return NULL;
4399
4400 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004401}
4402
4403static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4404 u64 nr, int nmi,
4405 struct perf_sample_data *data,
4406 struct pt_regs *regs)
4407{
4408 struct perf_cpu_context *cpuctx;
4409 struct perf_event *event;
4410 struct hlist_node *node;
4411 struct hlist_head *head;
4412
4413 cpuctx = &__get_cpu_var(perf_cpu_context);
4414
4415 rcu_read_lock();
4416
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004417 head = find_swevent_head_rcu(cpuctx, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004418
4419 if (!head)
4420 goto end;
4421
4422 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004423 if (perf_swevent_match(event, type, event_id, data, regs))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004424 perf_swevent_add(event, nr, nmi, data, regs);
4425 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004426end:
4427 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004428}
4429
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004430int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004431{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004432 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004433
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004434 return get_recursion_context(cpuctx->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004435}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004436EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004437
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004438void inline perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004439{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004440 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004441
4442 put_recursion_context(cpuctx->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004443}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004444
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004445void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4446 struct pt_regs *regs, u64 addr)
4447{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004448 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004449 int rctx;
4450
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004451 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004452 rctx = perf_swevent_get_recursion_context();
4453 if (rctx < 0)
4454 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004455
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004456 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004457
4458 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004459
4460 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004461 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004462}
4463
4464static void perf_swevent_read(struct perf_event *event)
4465{
4466}
4467
4468static int perf_swevent_enable(struct perf_event *event)
4469{
4470 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004471 struct perf_cpu_context *cpuctx;
4472 struct hlist_head *head;
4473
4474 cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004475
4476 if (hwc->sample_period) {
4477 hwc->last_period = hwc->sample_period;
4478 perf_swevent_set_period(event);
4479 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004480
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004481 head = find_swevent_head(cpuctx, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004482 if (WARN_ON_ONCE(!head))
4483 return -EINVAL;
4484
4485 hlist_add_head_rcu(&event->hlist_entry, head);
4486
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004487 return 0;
4488}
4489
4490static void perf_swevent_disable(struct perf_event *event)
4491{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004492 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004493}
4494
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004495static void perf_swevent_void(struct perf_event *event)
4496{
4497}
4498
4499static int perf_swevent_int(struct perf_event *event)
4500{
4501 return 0;
4502}
4503
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02004504static struct pmu perf_ops_generic = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004505 .enable = perf_swevent_enable,
4506 .disable = perf_swevent_disable,
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004507 .start = perf_swevent_int,
4508 .stop = perf_swevent_void,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004509 .read = perf_swevent_read,
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004510 .unthrottle = perf_swevent_void, /* hwc->interrupts already reset */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004511};
4512
4513/*
4514 * hrtimer based swevent callback
4515 */
4516
4517static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
4518{
4519 enum hrtimer_restart ret = HRTIMER_RESTART;
4520 struct perf_sample_data data;
4521 struct pt_regs *regs;
4522 struct perf_event *event;
4523 u64 period;
4524
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004525 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004526 event->pmu->read(event);
4527
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004528 perf_sample_data_init(&data, 0);
Xiao Guangrong59d069e2009-12-01 17:30:08 +08004529 data.period = event->hw.last_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004530 regs = get_irq_regs();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004531
Frederic Weisbeckerdf8290b2010-04-09 00:28:14 +02004532 if (regs && !perf_exclude_event(event, regs)) {
Soeren Sandmann54f44072009-10-22 18:34:08 +02004533 if (!(event->attr.exclude_idle && current->pid == 0))
4534 if (perf_event_overflow(event, 0, &data, regs))
4535 ret = HRTIMER_NORESTART;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004536 }
4537
4538 period = max_t(u64, 10000, event->hw.sample_period);
4539 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4540
4541 return ret;
4542}
4543
Soeren Sandmann721a6692009-09-15 14:33:08 +02004544static void perf_swevent_start_hrtimer(struct perf_event *event)
4545{
4546 struct hw_perf_event *hwc = &event->hw;
4547
4548 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4549 hwc->hrtimer.function = perf_swevent_hrtimer;
4550 if (hwc->sample_period) {
4551 u64 period;
4552
4553 if (hwc->remaining) {
4554 if (hwc->remaining < 0)
4555 period = 10000;
4556 else
4557 period = hwc->remaining;
4558 hwc->remaining = 0;
4559 } else {
4560 period = max_t(u64, 10000, hwc->sample_period);
4561 }
4562 __hrtimer_start_range_ns(&hwc->hrtimer,
4563 ns_to_ktime(period), 0,
4564 HRTIMER_MODE_REL, 0);
4565 }
4566}
4567
4568static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4569{
4570 struct hw_perf_event *hwc = &event->hw;
4571
4572 if (hwc->sample_period) {
4573 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4574 hwc->remaining = ktime_to_ns(remaining);
4575
4576 hrtimer_cancel(&hwc->hrtimer);
4577 }
4578}
4579
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004580/*
4581 * Software event: cpu wall time clock
4582 */
4583
4584static void cpu_clock_perf_event_update(struct perf_event *event)
4585{
4586 int cpu = raw_smp_processor_id();
4587 s64 prev;
4588 u64 now;
4589
4590 now = cpu_clock(cpu);
Peter Zijlstrae7850592010-05-21 14:43:08 +02004591 prev = local64_xchg(&event->hw.prev_count, now);
4592 local64_add(now - prev, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004593}
4594
4595static int cpu_clock_perf_event_enable(struct perf_event *event)
4596{
4597 struct hw_perf_event *hwc = &event->hw;
4598 int cpu = raw_smp_processor_id();
4599
Peter Zijlstrae7850592010-05-21 14:43:08 +02004600 local64_set(&hwc->prev_count, cpu_clock(cpu));
Soeren Sandmann721a6692009-09-15 14:33:08 +02004601 perf_swevent_start_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004602
4603 return 0;
4604}
4605
4606static void cpu_clock_perf_event_disable(struct perf_event *event)
4607{
Soeren Sandmann721a6692009-09-15 14:33:08 +02004608 perf_swevent_cancel_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004609 cpu_clock_perf_event_update(event);
4610}
4611
4612static void cpu_clock_perf_event_read(struct perf_event *event)
4613{
4614 cpu_clock_perf_event_update(event);
4615}
4616
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02004617static struct pmu perf_ops_cpu_clock = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004618 .enable = cpu_clock_perf_event_enable,
4619 .disable = cpu_clock_perf_event_disable,
4620 .read = cpu_clock_perf_event_read,
4621};
4622
4623/*
4624 * Software event: task time clock
4625 */
4626
4627static void task_clock_perf_event_update(struct perf_event *event, u64 now)
4628{
4629 u64 prev;
4630 s64 delta;
4631
Peter Zijlstrae7850592010-05-21 14:43:08 +02004632 prev = local64_xchg(&event->hw.prev_count, now);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004633 delta = now - prev;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004634 local64_add(delta, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004635}
4636
4637static int task_clock_perf_event_enable(struct perf_event *event)
4638{
4639 struct hw_perf_event *hwc = &event->hw;
4640 u64 now;
4641
4642 now = event->ctx->time;
4643
Peter Zijlstrae7850592010-05-21 14:43:08 +02004644 local64_set(&hwc->prev_count, now);
Soeren Sandmann721a6692009-09-15 14:33:08 +02004645
4646 perf_swevent_start_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004647
4648 return 0;
4649}
4650
4651static void task_clock_perf_event_disable(struct perf_event *event)
4652{
Soeren Sandmann721a6692009-09-15 14:33:08 +02004653 perf_swevent_cancel_hrtimer(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004654 task_clock_perf_event_update(event, event->ctx->time);
4655
4656}
4657
4658static void task_clock_perf_event_read(struct perf_event *event)
4659{
4660 u64 time;
4661
4662 if (!in_nmi()) {
4663 update_context_time(event->ctx);
4664 time = event->ctx->time;
4665 } else {
4666 u64 now = perf_clock();
4667 u64 delta = now - event->ctx->timestamp;
4668 time = event->ctx->time + delta;
4669 }
4670
4671 task_clock_perf_event_update(event, time);
4672}
4673
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02004674static struct pmu perf_ops_task_clock = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004675 .enable = task_clock_perf_event_enable,
4676 .disable = task_clock_perf_event_disable,
4677 .read = task_clock_perf_event_read,
4678};
4679
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004680/* Deref the hlist from the update side */
4681static inline struct swevent_hlist *
4682swevent_hlist_deref(struct perf_cpu_context *cpuctx)
4683{
4684 return rcu_dereference_protected(cpuctx->swevent_hlist,
4685 lockdep_is_held(&cpuctx->hlist_mutex));
4686}
4687
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004688static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4689{
4690 struct swevent_hlist *hlist;
4691
4692 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4693 kfree(hlist);
4694}
4695
4696static void swevent_hlist_release(struct perf_cpu_context *cpuctx)
4697{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004698 struct swevent_hlist *hlist = swevent_hlist_deref(cpuctx);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004699
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004700 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004701 return;
4702
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004703 rcu_assign_pointer(cpuctx->swevent_hlist, NULL);
4704 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4705}
4706
4707static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4708{
4709 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4710
4711 mutex_lock(&cpuctx->hlist_mutex);
4712
4713 if (!--cpuctx->hlist_refcount)
4714 swevent_hlist_release(cpuctx);
4715
4716 mutex_unlock(&cpuctx->hlist_mutex);
4717}
4718
4719static void swevent_hlist_put(struct perf_event *event)
4720{
4721 int cpu;
4722
4723 if (event->cpu != -1) {
4724 swevent_hlist_put_cpu(event, event->cpu);
4725 return;
4726 }
4727
4728 for_each_possible_cpu(cpu)
4729 swevent_hlist_put_cpu(event, cpu);
4730}
4731
4732static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4733{
4734 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4735 int err = 0;
4736
4737 mutex_lock(&cpuctx->hlist_mutex);
4738
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004739 if (!swevent_hlist_deref(cpuctx) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004740 struct swevent_hlist *hlist;
4741
4742 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4743 if (!hlist) {
4744 err = -ENOMEM;
4745 goto exit;
4746 }
4747 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
4748 }
4749 cpuctx->hlist_refcount++;
4750 exit:
4751 mutex_unlock(&cpuctx->hlist_mutex);
4752
4753 return err;
4754}
4755
4756static int swevent_hlist_get(struct perf_event *event)
4757{
4758 int err;
4759 int cpu, failed_cpu;
4760
4761 if (event->cpu != -1)
4762 return swevent_hlist_get_cpu(event, event->cpu);
4763
4764 get_online_cpus();
4765 for_each_possible_cpu(cpu) {
4766 err = swevent_hlist_get_cpu(event, cpu);
4767 if (err) {
4768 failed_cpu = cpu;
4769 goto fail;
4770 }
4771 }
4772 put_online_cpus();
4773
4774 return 0;
4775 fail:
4776 for_each_possible_cpu(cpu) {
4777 if (cpu == failed_cpu)
4778 break;
4779 swevent_hlist_put_cpu(event, cpu);
4780 }
4781
4782 put_online_cpus();
4783 return err;
4784}
4785
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004786#ifdef CONFIG_EVENT_TRACING
4787
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02004788static struct pmu perf_ops_tracepoint = {
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004789 .enable = perf_trace_enable,
4790 .disable = perf_trace_disable,
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004791 .start = perf_swevent_int,
4792 .stop = perf_swevent_void,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004793 .read = perf_swevent_read,
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004794 .unthrottle = perf_swevent_void,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004795};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004796
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004797static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004798 struct perf_sample_data *data)
4799{
4800 void *record = data->raw->data;
4801
4802 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4803 return 1;
4804 return 0;
4805}
4806
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004807static int perf_tp_event_match(struct perf_event *event,
4808 struct perf_sample_data *data,
4809 struct pt_regs *regs)
4810{
Peter Zijlstra580d6072010-05-20 20:54:31 +02004811 /*
4812 * All tracepoints are from kernel-space.
4813 */
4814 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004815 return 0;
4816
4817 if (!perf_tp_filter_match(event, data))
4818 return 0;
4819
4820 return 1;
4821}
4822
4823void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004824 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004825{
4826 struct perf_sample_data data;
4827 struct perf_event *event;
4828 struct hlist_node *node;
4829
4830 struct perf_raw_record raw = {
4831 .size = entry_size,
4832 .data = record,
4833 };
4834
4835 perf_sample_data_init(&data, addr);
4836 data.raw = &raw;
4837
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004838 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4839 if (perf_tp_event_match(event, &data, regs))
4840 perf_swevent_add(event, count, 1, &data, regs);
4841 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004842
4843 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004844}
4845EXPORT_SYMBOL_GPL(perf_tp_event);
4846
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004847static void tp_perf_event_destroy(struct perf_event *event)
4848{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004849 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004850}
4851
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02004852static struct pmu *tp_perf_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004853{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004854 int err;
4855
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004856 /*
4857 * Raw tracepoint data is a severe data leak, only allow root to
4858 * have these.
4859 */
4860 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4861 perf_paranoid_tracepoint_raw() &&
4862 !capable(CAP_SYS_ADMIN))
4863 return ERR_PTR(-EPERM);
4864
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004865 err = perf_trace_init(event);
4866 if (err)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004867 return NULL;
4868
4869 event->destroy = tp_perf_event_destroy;
4870
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004871 return &perf_ops_tracepoint;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004872}
Li Zefan6fb29152009-10-15 11:21:42 +08004873
4874static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4875{
4876 char *filter_str;
4877 int ret;
4878
4879 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4880 return -EINVAL;
4881
4882 filter_str = strndup_user(arg, PAGE_SIZE);
4883 if (IS_ERR(filter_str))
4884 return PTR_ERR(filter_str);
4885
4886 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4887
4888 kfree(filter_str);
4889 return ret;
4890}
4891
4892static void perf_event_free_filter(struct perf_event *event)
4893{
4894 ftrace_profile_free_filter(event);
4895}
4896
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004897#else
Li Zefan6fb29152009-10-15 11:21:42 +08004898
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02004899static struct pmu *tp_perf_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004900{
4901 return NULL;
4902}
Li Zefan6fb29152009-10-15 11:21:42 +08004903
4904static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4905{
4906 return -ENOENT;
4907}
4908
4909static void perf_event_free_filter(struct perf_event *event)
4910{
4911}
4912
Li Zefan07b139c2009-12-21 14:27:35 +08004913#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004914
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004915#ifdef CONFIG_HAVE_HW_BREAKPOINT
4916static void bp_perf_event_destroy(struct perf_event *event)
4917{
4918 release_bp_slot(event);
4919}
4920
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02004921static struct pmu *bp_perf_event_init(struct perf_event *bp)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004922{
4923 int err;
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01004924
4925 err = register_perf_hw_breakpoint(bp);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004926 if (err)
4927 return ERR_PTR(err);
4928
4929 bp->destroy = bp_perf_event_destroy;
4930
4931 return &perf_ops_bp;
4932}
4933
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004934void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004935{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004936 struct perf_sample_data sample;
4937 struct pt_regs *regs = data;
4938
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004939 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004940
4941 if (!perf_exclude_event(bp, regs))
4942 perf_swevent_add(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004943}
4944#else
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02004945static struct pmu *bp_perf_event_init(struct perf_event *bp)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004946{
4947 return NULL;
4948}
4949
4950void perf_bp_event(struct perf_event *bp, void *regs)
4951{
4952}
4953#endif
4954
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004955atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
4956
4957static void sw_perf_event_destroy(struct perf_event *event)
4958{
4959 u64 event_id = event->attr.config;
4960
4961 WARN_ON(event->parent);
4962
4963 atomic_dec(&perf_swevent_enabled[event_id]);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004964 swevent_hlist_put(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004965}
4966
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02004967static struct pmu *sw_perf_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004968{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02004969 struct pmu *pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004970 u64 event_id = event->attr.config;
4971
4972 /*
4973 * Software events (currently) can't in general distinguish
4974 * between user, kernel and hypervisor events.
4975 * However, context switches and cpu migrations are considered
4976 * to be kernel events, and page faults are never hypervisor
4977 * events.
4978 */
4979 switch (event_id) {
4980 case PERF_COUNT_SW_CPU_CLOCK:
4981 pmu = &perf_ops_cpu_clock;
4982
4983 break;
4984 case PERF_COUNT_SW_TASK_CLOCK:
4985 /*
4986 * If the user instantiates this as a per-cpu event,
4987 * use the cpu_clock event instead.
4988 */
4989 if (event->ctx->task)
4990 pmu = &perf_ops_task_clock;
4991 else
4992 pmu = &perf_ops_cpu_clock;
4993
4994 break;
4995 case PERF_COUNT_SW_PAGE_FAULTS:
4996 case PERF_COUNT_SW_PAGE_FAULTS_MIN:
4997 case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
4998 case PERF_COUNT_SW_CONTEXT_SWITCHES:
4999 case PERF_COUNT_SW_CPU_MIGRATIONS:
Anton Blanchardf7d79862009-10-18 01:09:29 +00005000 case PERF_COUNT_SW_ALIGNMENT_FAULTS:
5001 case PERF_COUNT_SW_EMULATION_FAULTS:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005002 if (!event->parent) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005003 int err;
5004
5005 err = swevent_hlist_get(event);
5006 if (err)
5007 return ERR_PTR(err);
5008
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005009 atomic_inc(&perf_swevent_enabled[event_id]);
5010 event->destroy = sw_perf_event_destroy;
5011 }
5012 pmu = &perf_ops_generic;
5013 break;
5014 }
5015
5016 return pmu;
5017}
5018
5019/*
5020 * Allocate and initialize a event structure
5021 */
5022static struct perf_event *
5023perf_event_alloc(struct perf_event_attr *attr,
5024 int cpu,
5025 struct perf_event_context *ctx,
5026 struct perf_event *group_leader,
5027 struct perf_event *parent_event,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005028 perf_overflow_handler_t overflow_handler,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005029 gfp_t gfpflags)
5030{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005031 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005032 struct perf_event *event;
5033 struct hw_perf_event *hwc;
5034 long err;
5035
5036 event = kzalloc(sizeof(*event), gfpflags);
5037 if (!event)
5038 return ERR_PTR(-ENOMEM);
5039
5040 /*
5041 * Single events are their own group leaders, with an
5042 * empty sibling list:
5043 */
5044 if (!group_leader)
5045 group_leader = event;
5046
5047 mutex_init(&event->child_mutex);
5048 INIT_LIST_HEAD(&event->child_list);
5049
5050 INIT_LIST_HEAD(&event->group_entry);
5051 INIT_LIST_HEAD(&event->event_entry);
5052 INIT_LIST_HEAD(&event->sibling_list);
5053 init_waitqueue_head(&event->waitq);
5054
5055 mutex_init(&event->mmap_mutex);
5056
5057 event->cpu = cpu;
5058 event->attr = *attr;
5059 event->group_leader = group_leader;
5060 event->pmu = NULL;
5061 event->ctx = ctx;
5062 event->oncpu = -1;
5063
5064 event->parent = parent_event;
5065
5066 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5067 event->id = atomic64_inc_return(&perf_event_id);
5068
5069 event->state = PERF_EVENT_STATE_INACTIVE;
5070
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005071 if (!overflow_handler && parent_event)
5072 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005073
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005074 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005075
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005076 if (attr->disabled)
5077 event->state = PERF_EVENT_STATE_OFF;
5078
5079 pmu = NULL;
5080
5081 hwc = &event->hw;
5082 hwc->sample_period = attr->sample_period;
5083 if (attr->freq && attr->sample_freq)
5084 hwc->sample_period = 1;
5085 hwc->last_period = hwc->sample_period;
5086
Peter Zijlstrae7850592010-05-21 14:43:08 +02005087 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005088
5089 /*
5090 * we currently do not support PERF_FORMAT_GROUP on inherited events
5091 */
5092 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5093 goto done;
5094
5095 switch (attr->type) {
5096 case PERF_TYPE_RAW:
5097 case PERF_TYPE_HARDWARE:
5098 case PERF_TYPE_HW_CACHE:
5099 pmu = hw_perf_event_init(event);
5100 break;
5101
5102 case PERF_TYPE_SOFTWARE:
5103 pmu = sw_perf_event_init(event);
5104 break;
5105
5106 case PERF_TYPE_TRACEPOINT:
5107 pmu = tp_perf_event_init(event);
5108 break;
5109
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005110 case PERF_TYPE_BREAKPOINT:
5111 pmu = bp_perf_event_init(event);
5112 break;
5113
5114
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005115 default:
5116 break;
5117 }
5118done:
5119 err = 0;
5120 if (!pmu)
5121 err = -EINVAL;
5122 else if (IS_ERR(pmu))
5123 err = PTR_ERR(pmu);
5124
5125 if (err) {
5126 if (event->ns)
5127 put_pid_ns(event->ns);
5128 kfree(event);
5129 return ERR_PTR(err);
5130 }
5131
5132 event->pmu = pmu;
5133
5134 if (!event->parent) {
5135 atomic_inc(&nr_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01005136 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005137 atomic_inc(&nr_mmap_events);
5138 if (event->attr.comm)
5139 atomic_inc(&nr_comm_events);
5140 if (event->attr.task)
5141 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005142 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5143 err = get_callchain_buffers();
5144 if (err) {
5145 free_event(event);
5146 return ERR_PTR(err);
5147 }
5148 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005149 }
5150
5151 return event;
5152}
5153
5154static int perf_copy_attr(struct perf_event_attr __user *uattr,
5155 struct perf_event_attr *attr)
5156{
5157 u32 size;
5158 int ret;
5159
5160 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5161 return -EFAULT;
5162
5163 /*
5164 * zero the full structure, so that a short copy will be nice.
5165 */
5166 memset(attr, 0, sizeof(*attr));
5167
5168 ret = get_user(size, &uattr->size);
5169 if (ret)
5170 return ret;
5171
5172 if (size > PAGE_SIZE) /* silly large */
5173 goto err_size;
5174
5175 if (!size) /* abi compat */
5176 size = PERF_ATTR_SIZE_VER0;
5177
5178 if (size < PERF_ATTR_SIZE_VER0)
5179 goto err_size;
5180
5181 /*
5182 * If we're handed a bigger struct than we know of,
5183 * ensure all the unknown bits are 0 - i.e. new
5184 * user-space does not rely on any kernel feature
5185 * extensions we dont know about yet.
5186 */
5187 if (size > sizeof(*attr)) {
5188 unsigned char __user *addr;
5189 unsigned char __user *end;
5190 unsigned char val;
5191
5192 addr = (void __user *)uattr + sizeof(*attr);
5193 end = (void __user *)uattr + size;
5194
5195 for (; addr < end; addr++) {
5196 ret = get_user(val, addr);
5197 if (ret)
5198 return ret;
5199 if (val)
5200 goto err_size;
5201 }
5202 size = sizeof(*attr);
5203 }
5204
5205 ret = copy_from_user(attr, uattr, size);
5206 if (ret)
5207 return -EFAULT;
5208
5209 /*
5210 * If the type exists, the corresponding creation will verify
5211 * the attr->config.
5212 */
5213 if (attr->type >= PERF_TYPE_MAX)
5214 return -EINVAL;
5215
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05305216 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005217 return -EINVAL;
5218
5219 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5220 return -EINVAL;
5221
5222 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5223 return -EINVAL;
5224
5225out:
5226 return ret;
5227
5228err_size:
5229 put_user(sizeof(*attr), &uattr->size);
5230 ret = -E2BIG;
5231 goto out;
5232}
5233
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005234static int
5235perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005236{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005237 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005238 int ret = -EINVAL;
5239
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005240 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005241 goto set;
5242
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005243 /* don't allow circular references */
5244 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005245 goto out;
5246
Peter Zijlstra0f139302010-05-20 14:35:15 +02005247 /*
5248 * Don't allow cross-cpu buffers
5249 */
5250 if (output_event->cpu != event->cpu)
5251 goto out;
5252
5253 /*
5254 * If its not a per-cpu buffer, it must be the same task.
5255 */
5256 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5257 goto out;
5258
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005259set:
5260 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005261 /* Can't redirect output if we've got an active mmap() */
5262 if (atomic_read(&event->mmap_count))
5263 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005264
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005265 if (output_event) {
5266 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005267 buffer = perf_buffer_get(output_event);
5268 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005269 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005270 }
5271
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005272 old_buffer = event->buffer;
5273 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005274 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005275unlock:
5276 mutex_unlock(&event->mmap_mutex);
5277
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005278 if (old_buffer)
5279 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005280out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005281 return ret;
5282}
5283
5284/**
5285 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5286 *
5287 * @attr_uptr: event_id type attributes for monitoring/sampling
5288 * @pid: target pid
5289 * @cpu: target cpu
5290 * @group_fd: group leader event fd
5291 */
5292SYSCALL_DEFINE5(perf_event_open,
5293 struct perf_event_attr __user *, attr_uptr,
5294 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5295{
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005296 struct perf_event *event, *group_leader = NULL, *output_event = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005297 struct perf_event_attr attr;
5298 struct perf_event_context *ctx;
5299 struct file *event_file = NULL;
5300 struct file *group_file = NULL;
Al Viroea635c62010-05-26 17:40:29 -04005301 int event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005302 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005303 int err;
5304
5305 /* for future expandability... */
5306 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5307 return -EINVAL;
5308
5309 err = perf_copy_attr(attr_uptr, &attr);
5310 if (err)
5311 return err;
5312
5313 if (!attr.exclude_kernel) {
5314 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5315 return -EACCES;
5316 }
5317
5318 if (attr.freq) {
5319 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5320 return -EINVAL;
5321 }
5322
Al Viroea635c62010-05-26 17:40:29 -04005323 event_fd = get_unused_fd_flags(O_RDWR);
5324 if (event_fd < 0)
5325 return event_fd;
5326
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005327 /*
5328 * Get the target context (task or percpu):
5329 */
5330 ctx = find_get_context(pid, cpu);
Al Viroea635c62010-05-26 17:40:29 -04005331 if (IS_ERR(ctx)) {
5332 err = PTR_ERR(ctx);
5333 goto err_fd;
5334 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005335
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005336 if (group_fd != -1) {
5337 group_leader = perf_fget_light(group_fd, &fput_needed);
5338 if (IS_ERR(group_leader)) {
5339 err = PTR_ERR(group_leader);
5340 goto err_put_context;
5341 }
5342 group_file = group_leader->filp;
5343 if (flags & PERF_FLAG_FD_OUTPUT)
5344 output_event = group_leader;
5345 if (flags & PERF_FLAG_FD_NO_GROUP)
5346 group_leader = NULL;
5347 }
5348
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005349 /*
5350 * Look up the group leader (we will attach this event to it):
5351 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005352 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005353 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005354
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005355 /*
5356 * Do not allow a recursive hierarchy (this new sibling
5357 * becoming part of another group-sibling):
5358 */
5359 if (group_leader->group_leader != group_leader)
5360 goto err_put_context;
5361 /*
5362 * Do not allow to attach to a group in a different
5363 * task or CPU context:
5364 */
5365 if (group_leader->ctx != ctx)
5366 goto err_put_context;
5367 /*
5368 * Only a group leader can be exclusive or pinned
5369 */
5370 if (attr.exclusive || attr.pinned)
5371 goto err_put_context;
5372 }
5373
5374 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005375 NULL, NULL, GFP_KERNEL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005376 if (IS_ERR(event)) {
5377 err = PTR_ERR(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005378 goto err_put_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005379 }
5380
5381 if (output_event) {
5382 err = perf_event_set_output(event, output_event);
5383 if (err)
5384 goto err_free_put_context;
5385 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005386
Al Viroea635c62010-05-26 17:40:29 -04005387 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5388 if (IS_ERR(event_file)) {
5389 err = PTR_ERR(event_file);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005390 goto err_free_put_context;
Al Viroea635c62010-05-26 17:40:29 -04005391 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005392
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005393 event->filp = event_file;
5394 WARN_ON_ONCE(ctx->parent_ctx);
5395 mutex_lock(&ctx->mutex);
5396 perf_install_in_context(ctx, event, cpu);
5397 ++ctx->generation;
5398 mutex_unlock(&ctx->mutex);
5399
5400 event->owner = current;
5401 get_task_struct(current);
5402 mutex_lock(&current->perf_event_mutex);
5403 list_add_tail(&event->owner_entry, &current->perf_event_list);
5404 mutex_unlock(&current->perf_event_mutex);
5405
Peter Zijlstra8a495422010-05-27 15:47:49 +02005406 /*
5407 * Drop the reference on the group_event after placing the
5408 * new event on the sibling_list. This ensures destruction
5409 * of the group leader will find the pointer to itself in
5410 * perf_group_detach().
5411 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005412 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005413 fd_install(event_fd, event_file);
5414 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005415
Al Viroea635c62010-05-26 17:40:29 -04005416err_free_put_context:
5417 free_event(event);
5418err_put_context:
5419 fput_light(group_file, fput_needed);
5420 put_ctx(ctx);
5421err_fd:
5422 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005423 return err;
5424}
5425
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005426/**
5427 * perf_event_create_kernel_counter
5428 *
5429 * @attr: attributes of the counter to create
5430 * @cpu: cpu in which the counter is bound
5431 * @pid: task to profile
5432 */
5433struct perf_event *
5434perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005435 pid_t pid,
5436 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005437{
5438 struct perf_event *event;
5439 struct perf_event_context *ctx;
5440 int err;
5441
5442 /*
5443 * Get the target context (task or percpu):
5444 */
5445
5446 ctx = find_get_context(pid, cpu);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005447 if (IS_ERR(ctx)) {
5448 err = PTR_ERR(ctx);
5449 goto err_exit;
5450 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005451
5452 event = perf_event_alloc(attr, cpu, ctx, NULL,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005453 NULL, overflow_handler, GFP_KERNEL);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005454 if (IS_ERR(event)) {
5455 err = PTR_ERR(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005456 goto err_put_context;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005457 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005458
5459 event->filp = NULL;
5460 WARN_ON_ONCE(ctx->parent_ctx);
5461 mutex_lock(&ctx->mutex);
5462 perf_install_in_context(ctx, event, cpu);
5463 ++ctx->generation;
5464 mutex_unlock(&ctx->mutex);
5465
5466 event->owner = current;
5467 get_task_struct(current);
5468 mutex_lock(&current->perf_event_mutex);
5469 list_add_tail(&event->owner_entry, &current->perf_event_list);
5470 mutex_unlock(&current->perf_event_mutex);
5471
5472 return event;
5473
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005474 err_put_context:
5475 put_ctx(ctx);
5476 err_exit:
5477 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005478}
5479EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5480
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005481/*
5482 * inherit a event from parent task to child task:
5483 */
5484static struct perf_event *
5485inherit_event(struct perf_event *parent_event,
5486 struct task_struct *parent,
5487 struct perf_event_context *parent_ctx,
5488 struct task_struct *child,
5489 struct perf_event *group_leader,
5490 struct perf_event_context *child_ctx)
5491{
5492 struct perf_event *child_event;
5493
5494 /*
5495 * Instead of creating recursive hierarchies of events,
5496 * we link inherited events back to the original parent,
5497 * which has a filp for sure, which we use as the reference
5498 * count:
5499 */
5500 if (parent_event->parent)
5501 parent_event = parent_event->parent;
5502
5503 child_event = perf_event_alloc(&parent_event->attr,
5504 parent_event->cpu, child_ctx,
5505 group_leader, parent_event,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005506 NULL, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005507 if (IS_ERR(child_event))
5508 return child_event;
5509 get_ctx(child_ctx);
5510
5511 /*
5512 * Make the child state follow the state of the parent event,
5513 * not its attr.disabled bit. We hold the parent's mutex,
5514 * so we won't race with perf_event_{en, dis}able_family.
5515 */
5516 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5517 child_event->state = PERF_EVENT_STATE_INACTIVE;
5518 else
5519 child_event->state = PERF_EVENT_STATE_OFF;
5520
Peter Zijlstra75c9f322010-01-29 09:04:26 +01005521 if (parent_event->attr.freq) {
5522 u64 sample_period = parent_event->hw.sample_period;
5523 struct hw_perf_event *hwc = &child_event->hw;
5524
5525 hwc->sample_period = sample_period;
5526 hwc->last_period = sample_period;
5527
Peter Zijlstrae7850592010-05-21 14:43:08 +02005528 local64_set(&hwc->period_left, sample_period);
Peter Zijlstra75c9f322010-01-29 09:04:26 +01005529 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005530
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005531 child_event->overflow_handler = parent_event->overflow_handler;
5532
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005533 /*
5534 * Link it up in the child's context:
5535 */
5536 add_event_to_ctx(child_event, child_ctx);
5537
5538 /*
5539 * Get a reference to the parent filp - we will fput it
5540 * when the child event exits. This is safe to do because
5541 * we are in the parent and we know that the filp still
5542 * exists and has a nonzero count:
5543 */
5544 atomic_long_inc(&parent_event->filp->f_count);
5545
5546 /*
5547 * Link this into the parent event's child list
5548 */
5549 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5550 mutex_lock(&parent_event->child_mutex);
5551 list_add_tail(&child_event->child_list, &parent_event->child_list);
5552 mutex_unlock(&parent_event->child_mutex);
5553
5554 return child_event;
5555}
5556
5557static int inherit_group(struct perf_event *parent_event,
5558 struct task_struct *parent,
5559 struct perf_event_context *parent_ctx,
5560 struct task_struct *child,
5561 struct perf_event_context *child_ctx)
5562{
5563 struct perf_event *leader;
5564 struct perf_event *sub;
5565 struct perf_event *child_ctr;
5566
5567 leader = inherit_event(parent_event, parent, parent_ctx,
5568 child, NULL, child_ctx);
5569 if (IS_ERR(leader))
5570 return PTR_ERR(leader);
5571 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
5572 child_ctr = inherit_event(sub, parent, parent_ctx,
5573 child, leader, child_ctx);
5574 if (IS_ERR(child_ctr))
5575 return PTR_ERR(child_ctr);
5576 }
5577 return 0;
5578}
5579
5580static void sync_child_event(struct perf_event *child_event,
5581 struct task_struct *child)
5582{
5583 struct perf_event *parent_event = child_event->parent;
5584 u64 child_val;
5585
5586 if (child_event->attr.inherit_stat)
5587 perf_event_read_event(child_event, child);
5588
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005589 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005590
5591 /*
5592 * Add back the child's count to the parent's count:
5593 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02005594 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005595 atomic64_add(child_event->total_time_enabled,
5596 &parent_event->child_total_time_enabled);
5597 atomic64_add(child_event->total_time_running,
5598 &parent_event->child_total_time_running);
5599
5600 /*
5601 * Remove this event from the parent's list
5602 */
5603 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5604 mutex_lock(&parent_event->child_mutex);
5605 list_del_init(&child_event->child_list);
5606 mutex_unlock(&parent_event->child_mutex);
5607
5608 /*
5609 * Release the parent event, if this was the last
5610 * reference to it.
5611 */
5612 fput(parent_event->filp);
5613}
5614
5615static void
5616__perf_event_exit_task(struct perf_event *child_event,
5617 struct perf_event_context *child_ctx,
5618 struct task_struct *child)
5619{
5620 struct perf_event *parent_event;
5621
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005622 perf_event_remove_from_context(child_event);
5623
5624 parent_event = child_event->parent;
5625 /*
5626 * It can happen that parent exits first, and has events
5627 * that are still around due to the child reference. These
5628 * events need to be zapped - but otherwise linger.
5629 */
5630 if (parent_event) {
5631 sync_child_event(child_event, child);
5632 free_event(child_event);
5633 }
5634}
5635
5636/*
5637 * When a child task exits, feed back event values to parent events.
5638 */
5639void perf_event_exit_task(struct task_struct *child)
5640{
5641 struct perf_event *child_event, *tmp;
5642 struct perf_event_context *child_ctx;
5643 unsigned long flags;
5644
5645 if (likely(!child->perf_event_ctxp)) {
5646 perf_event_task(child, NULL, 0);
5647 return;
5648 }
5649
5650 local_irq_save(flags);
5651 /*
5652 * We can't reschedule here because interrupts are disabled,
5653 * and either child is current or it is a task that can't be
5654 * scheduled, so we are now safe from rescheduling changing
5655 * our context.
5656 */
5657 child_ctx = child->perf_event_ctxp;
5658 __perf_event_task_sched_out(child_ctx);
5659
5660 /*
5661 * Take the context lock here so that if find_get_context is
5662 * reading child->perf_event_ctxp, we wait until it has
5663 * incremented the context's refcount before we do put_ctx below.
5664 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005665 raw_spin_lock(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005666 child->perf_event_ctxp = NULL;
5667 /*
5668 * If this context is a clone; unclone it so it can't get
5669 * swapped to another process while we're removing all
5670 * the events from it.
5671 */
5672 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01005673 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01005674 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005675
5676 /*
5677 * Report the task dead after unscheduling the events so that we
5678 * won't get any samples after PERF_RECORD_EXIT. We can however still
5679 * get a few PERF_RECORD_READ events.
5680 */
5681 perf_event_task(child, child_ctx, 0);
5682
5683 /*
5684 * We can recurse on the same lock type through:
5685 *
5686 * __perf_event_exit_task()
5687 * sync_child_event()
5688 * fput(parent_event->filp)
5689 * perf_release()
5690 * mutex_lock(&ctx->mutex)
5691 *
5692 * But since its the parent context it won't be the same instance.
5693 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02005694 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005695
5696again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005697 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5698 group_entry)
5699 __perf_event_exit_task(child_event, child_ctx, child);
5700
5701 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005702 group_entry)
5703 __perf_event_exit_task(child_event, child_ctx, child);
5704
5705 /*
5706 * If the last event was a group event, it will have appended all
5707 * its siblings to the list, but we obtained 'tmp' before that which
5708 * will still point to the list head terminating the iteration.
5709 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005710 if (!list_empty(&child_ctx->pinned_groups) ||
5711 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005712 goto again;
5713
5714 mutex_unlock(&child_ctx->mutex);
5715
5716 put_ctx(child_ctx);
5717}
5718
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005719static void perf_free_event(struct perf_event *event,
5720 struct perf_event_context *ctx)
5721{
5722 struct perf_event *parent = event->parent;
5723
5724 if (WARN_ON_ONCE(!parent))
5725 return;
5726
5727 mutex_lock(&parent->child_mutex);
5728 list_del_init(&event->child_list);
5729 mutex_unlock(&parent->child_mutex);
5730
5731 fput(parent->filp);
5732
Peter Zijlstra8a495422010-05-27 15:47:49 +02005733 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005734 list_del_event(event, ctx);
5735 free_event(event);
5736}
5737
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005738/*
5739 * free an unexposed, unused context as created by inheritance by
5740 * init_task below, used by fork() in case of fail.
5741 */
5742void perf_event_free_task(struct task_struct *task)
5743{
5744 struct perf_event_context *ctx = task->perf_event_ctxp;
5745 struct perf_event *event, *tmp;
5746
5747 if (!ctx)
5748 return;
5749
5750 mutex_lock(&ctx->mutex);
5751again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005752 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5753 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005754
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005755 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5756 group_entry)
5757 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005758
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005759 if (!list_empty(&ctx->pinned_groups) ||
5760 !list_empty(&ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005761 goto again;
5762
5763 mutex_unlock(&ctx->mutex);
5764
5765 put_ctx(ctx);
5766}
5767
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005768static int
5769inherit_task_group(struct perf_event *event, struct task_struct *parent,
5770 struct perf_event_context *parent_ctx,
5771 struct task_struct *child,
5772 int *inherited_all)
5773{
5774 int ret;
5775 struct perf_event_context *child_ctx = child->perf_event_ctxp;
5776
5777 if (!event->attr.inherit) {
5778 *inherited_all = 0;
5779 return 0;
5780 }
5781
5782 if (!child_ctx) {
5783 /*
5784 * This is executed from the parent task context, so
5785 * inherit events that have been marked for cloning.
5786 * First allocate and initialize a context for the
5787 * child.
5788 */
5789
5790 child_ctx = kzalloc(sizeof(struct perf_event_context),
5791 GFP_KERNEL);
5792 if (!child_ctx)
5793 return -ENOMEM;
5794
5795 __perf_event_init_context(child_ctx, child);
5796 child->perf_event_ctxp = child_ctx;
5797 get_task_struct(child);
5798 }
5799
5800 ret = inherit_group(event, parent, parent_ctx,
5801 child, child_ctx);
5802
5803 if (ret)
5804 *inherited_all = 0;
5805
5806 return ret;
5807}
5808
5809
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005810/*
5811 * Initialize the perf_event context in task_struct
5812 */
5813int perf_event_init_task(struct task_struct *child)
5814{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005815 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005816 struct perf_event_context *cloned_ctx;
5817 struct perf_event *event;
5818 struct task_struct *parent = current;
5819 int inherited_all = 1;
5820 int ret = 0;
5821
5822 child->perf_event_ctxp = NULL;
5823
5824 mutex_init(&child->perf_event_mutex);
5825 INIT_LIST_HEAD(&child->perf_event_list);
5826
5827 if (likely(!parent->perf_event_ctxp))
5828 return 0;
5829
5830 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005831 * If the parent's context is a clone, pin it so it won't get
5832 * swapped under us.
5833 */
5834 parent_ctx = perf_pin_task_context(parent);
5835
5836 /*
5837 * No need to check if parent_ctx != NULL here; since we saw
5838 * it non-NULL earlier, the only reason for it to become NULL
5839 * is if we exit, and since we're currently in the middle of
5840 * a fork we can't be exiting at the same time.
5841 */
5842
5843 /*
5844 * Lock the parent list. No need to lock the child - not PID
5845 * hashed yet and not running, so nobody can access it.
5846 */
5847 mutex_lock(&parent_ctx->mutex);
5848
5849 /*
5850 * We dont have to disable NMIs - we are only looking at
5851 * the list, not manipulating it:
5852 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005853 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
5854 ret = inherit_task_group(event, parent, parent_ctx, child,
5855 &inherited_all);
5856 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005857 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005858 }
5859
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005860 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
5861 ret = inherit_task_group(event, parent, parent_ctx, child,
5862 &inherited_all);
5863 if (ret)
5864 break;
5865 }
5866
5867 child_ctx = child->perf_event_ctxp;
5868
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01005869 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005870 /*
5871 * Mark the child context as a clone of the parent
5872 * context, or of whatever the parent is a clone of.
5873 * Note that if the parent is a clone, it could get
5874 * uncloned at any point, but that doesn't matter
5875 * because the list of events and the generation
5876 * count can't have changed since we took the mutex.
5877 */
5878 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5879 if (cloned_ctx) {
5880 child_ctx->parent_ctx = cloned_ctx;
5881 child_ctx->parent_gen = parent_ctx->parent_gen;
5882 } else {
5883 child_ctx->parent_ctx = parent_ctx;
5884 child_ctx->parent_gen = parent_ctx->generation;
5885 }
5886 get_ctx(child_ctx->parent_ctx);
5887 }
5888
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005889 mutex_unlock(&parent_ctx->mutex);
5890
5891 perf_unpin_context(parent_ctx);
5892
5893 return ret;
5894}
5895
Paul Mackerras220b1402010-03-10 20:45:52 +11005896static void __init perf_event_init_all_cpus(void)
5897{
5898 int cpu;
5899 struct perf_cpu_context *cpuctx;
5900
5901 for_each_possible_cpu(cpu) {
5902 cpuctx = &per_cpu(perf_cpu_context, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005903 mutex_init(&cpuctx->hlist_mutex);
Paul Mackerras220b1402010-03-10 20:45:52 +11005904 __perf_event_init_context(&cpuctx->ctx, NULL);
5905 }
5906}
5907
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005908static void __cpuinit perf_event_init_cpu(int cpu)
5909{
5910 struct perf_cpu_context *cpuctx;
5911
5912 cpuctx = &per_cpu(perf_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005913
5914 spin_lock(&perf_resource_lock);
5915 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5916 spin_unlock(&perf_resource_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005917
5918 mutex_lock(&cpuctx->hlist_mutex);
5919 if (cpuctx->hlist_refcount > 0) {
5920 struct swevent_hlist *hlist;
5921
5922 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5923 WARN_ON_ONCE(!hlist);
5924 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
5925 }
5926 mutex_unlock(&cpuctx->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005927}
5928
5929#ifdef CONFIG_HOTPLUG_CPU
5930static void __perf_event_exit_cpu(void *info)
5931{
5932 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5933 struct perf_event_context *ctx = &cpuctx->ctx;
5934 struct perf_event *event, *tmp;
5935
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005936 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5937 __perf_event_remove_from_context(event);
5938 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005939 __perf_event_remove_from_context(event);
5940}
5941static void perf_event_exit_cpu(int cpu)
5942{
5943 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5944 struct perf_event_context *ctx = &cpuctx->ctx;
5945
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005946 mutex_lock(&cpuctx->hlist_mutex);
5947 swevent_hlist_release(cpuctx);
5948 mutex_unlock(&cpuctx->hlist_mutex);
5949
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005950 mutex_lock(&ctx->mutex);
5951 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5952 mutex_unlock(&ctx->mutex);
5953}
5954#else
5955static inline void perf_event_exit_cpu(int cpu) { }
5956#endif
5957
5958static int __cpuinit
5959perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5960{
5961 unsigned int cpu = (long)hcpu;
5962
Peter Zijlstra5e116372010-06-11 13:35:08 +02005963 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005964
5965 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02005966 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005967 perf_event_init_cpu(cpu);
5968 break;
5969
Peter Zijlstra5e116372010-06-11 13:35:08 +02005970 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005971 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005972 perf_event_exit_cpu(cpu);
5973 break;
5974
5975 default:
5976 break;
5977 }
5978
5979 return NOTIFY_OK;
5980}
5981
5982/*
5983 * This has to have a higher priority than migration_notifier in sched.c.
5984 */
5985static struct notifier_block __cpuinitdata perf_cpu_nb = {
5986 .notifier_call = perf_cpu_notify,
5987 .priority = 20,
5988};
5989
5990void __init perf_event_init(void)
5991{
Paul Mackerras220b1402010-03-10 20:45:52 +11005992 perf_event_init_all_cpus();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005993 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
5994 (void *)(long)smp_processor_id());
5995 perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
5996 (void *)(long)smp_processor_id());
5997 register_cpu_notifier(&perf_cpu_nb);
5998}
5999
Andi Kleenc9be0a32010-01-05 12:47:58 +01006000static ssize_t perf_show_reserve_percpu(struct sysdev_class *class,
6001 struct sysdev_class_attribute *attr,
6002 char *buf)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006003{
6004 return sprintf(buf, "%d\n", perf_reserved_percpu);
6005}
6006
6007static ssize_t
6008perf_set_reserve_percpu(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01006009 struct sysdev_class_attribute *attr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006010 const char *buf,
6011 size_t count)
6012{
6013 struct perf_cpu_context *cpuctx;
6014 unsigned long val;
6015 int err, cpu, mpt;
6016
6017 err = strict_strtoul(buf, 10, &val);
6018 if (err)
6019 return err;
6020 if (val > perf_max_events)
6021 return -EINVAL;
6022
6023 spin_lock(&perf_resource_lock);
6024 perf_reserved_percpu = val;
6025 for_each_online_cpu(cpu) {
6026 cpuctx = &per_cpu(perf_cpu_context, cpu);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01006027 raw_spin_lock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006028 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
6029 perf_max_events - perf_reserved_percpu);
6030 cpuctx->max_pertask = mpt;
Thomas Gleixnere625cce2009-11-17 18:02:06 +01006031 raw_spin_unlock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006032 }
6033 spin_unlock(&perf_resource_lock);
6034
6035 return count;
6036}
6037
Andi Kleenc9be0a32010-01-05 12:47:58 +01006038static ssize_t perf_show_overcommit(struct sysdev_class *class,
6039 struct sysdev_class_attribute *attr,
6040 char *buf)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006041{
6042 return sprintf(buf, "%d\n", perf_overcommit);
6043}
6044
6045static ssize_t
Andi Kleenc9be0a32010-01-05 12:47:58 +01006046perf_set_overcommit(struct sysdev_class *class,
6047 struct sysdev_class_attribute *attr,
6048 const char *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006049{
6050 unsigned long val;
6051 int err;
6052
6053 err = strict_strtoul(buf, 10, &val);
6054 if (err)
6055 return err;
6056 if (val > 1)
6057 return -EINVAL;
6058
6059 spin_lock(&perf_resource_lock);
6060 perf_overcommit = val;
6061 spin_unlock(&perf_resource_lock);
6062
6063 return count;
6064}
6065
6066static SYSDEV_CLASS_ATTR(
6067 reserve_percpu,
6068 0644,
6069 perf_show_reserve_percpu,
6070 perf_set_reserve_percpu
6071 );
6072
6073static SYSDEV_CLASS_ATTR(
6074 overcommit,
6075 0644,
6076 perf_show_overcommit,
6077 perf_set_overcommit
6078 );
6079
6080static struct attribute *perfclass_attrs[] = {
6081 &attr_reserve_percpu.attr,
6082 &attr_overcommit.attr,
6083 NULL
6084};
6085
6086static struct attribute_group perfclass_attr_group = {
6087 .attrs = perfclass_attrs,
6088 .name = "perf_events",
6089};
6090
6091static int __init perf_event_sysfs_init(void)
6092{
6093 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
6094 &perfclass_attr_group);
6095}
6096device_initcall(perf_event_sysfs_init);