blob: 149ca18371b74d2df9f35e6d006d4e24fa9ce034 [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>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020034
35#include <asm/irq_regs.h>
36
37/*
38 * Each CPU has a list of per CPU events:
39 */
Xiao Guangrongaa5452d2009-12-09 11:28:13 +080040static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020041
42int perf_max_events __read_mostly = 1;
43static int perf_reserved_percpu __read_mostly;
44static int perf_overcommit __read_mostly = 1;
45
46static atomic_t nr_events __read_mostly;
47static atomic_t nr_mmap_events __read_mostly;
48static atomic_t nr_comm_events __read_mostly;
49static atomic_t nr_task_events __read_mostly;
50
51/*
52 * perf event paranoia level:
53 * -1 - not paranoid at all
54 * 0 - disallow raw tracepoint access for unpriv
55 * 1 - disallow cpu events for unpriv
56 * 2 - disallow kernel profiling for unpriv
57 */
58int sysctl_perf_event_paranoid __read_mostly = 1;
59
Ingo Molnarcdd6c482009-09-21 12:02:48 +020060int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
61
62/*
63 * max perf event sample rate
64 */
65int sysctl_perf_event_sample_rate __read_mostly = 100000;
66
67static atomic64_t perf_event_id;
68
69/*
70 * Lock for (sysadmin-configurable) event reservations:
71 */
72static DEFINE_SPINLOCK(perf_resource_lock);
73
Ingo Molnarcdd6c482009-09-21 12:02:48 +020074void __weak hw_perf_disable(void) { barrier(); }
75void __weak hw_perf_enable(void) { barrier(); }
76
Ingo Molnarcdd6c482009-09-21 12:02:48 +020077void __weak perf_event_print_debug(void) { }
78
79static DEFINE_PER_CPU(int, perf_disable_count);
80
Ingo Molnarcdd6c482009-09-21 12:02:48 +020081void perf_disable(void)
82{
Peter Zijlstra32975a42010-03-06 19:49:19 +010083 if (!__get_cpu_var(perf_disable_count)++)
84 hw_perf_disable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +020085}
86
87void perf_enable(void)
88{
Peter Zijlstra32975a42010-03-06 19:49:19 +010089 if (!--__get_cpu_var(perf_disable_count))
Ingo Molnarcdd6c482009-09-21 12:02:48 +020090 hw_perf_enable();
91}
92
93static void get_ctx(struct perf_event_context *ctx)
94{
95 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
96}
97
98static void free_ctx(struct rcu_head *head)
99{
100 struct perf_event_context *ctx;
101
102 ctx = container_of(head, struct perf_event_context, rcu_head);
103 kfree(ctx);
104}
105
106static void put_ctx(struct perf_event_context *ctx)
107{
108 if (atomic_dec_and_test(&ctx->refcount)) {
109 if (ctx->parent_ctx)
110 put_ctx(ctx->parent_ctx);
111 if (ctx->task)
112 put_task_struct(ctx->task);
113 call_rcu(&ctx->rcu_head, free_ctx);
114 }
115}
116
117static void unclone_ctx(struct perf_event_context *ctx)
118{
119 if (ctx->parent_ctx) {
120 put_ctx(ctx->parent_ctx);
121 ctx->parent_ctx = NULL;
122 }
123}
124
125/*
126 * If we inherit events we want to return the parent event id
127 * to userspace.
128 */
129static u64 primary_event_id(struct perf_event *event)
130{
131 u64 id = event->id;
132
133 if (event->parent)
134 id = event->parent->id;
135
136 return id;
137}
138
139/*
140 * Get the perf_event_context for a task and lock it.
141 * This has to cope with with the fact that until it is locked,
142 * the context could get moved to another task.
143 */
144static struct perf_event_context *
145perf_lock_task_context(struct task_struct *task, unsigned long *flags)
146{
147 struct perf_event_context *ctx;
148
149 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200150retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200151 ctx = rcu_dereference(task->perf_event_ctxp);
152 if (ctx) {
153 /*
154 * If this context is a clone of another, it might
155 * get swapped for another underneath us by
156 * perf_event_task_sched_out, though the
157 * rcu_read_lock() protects us from any context
158 * getting freed. Lock the context and check if it
159 * got swapped before we could get the lock, and retry
160 * if so. If we locked the right context, then it
161 * can't get swapped on us any more.
162 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100163 raw_spin_lock_irqsave(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200164 if (ctx != rcu_dereference(task->perf_event_ctxp)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100165 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200166 goto retry;
167 }
168
169 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100170 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200171 ctx = NULL;
172 }
173 }
174 rcu_read_unlock();
175 return ctx;
176}
177
178/*
179 * Get the context for a task and increment its pin_count so it
180 * can't get swapped to another task. This also increments its
181 * reference count so that the context can't get freed.
182 */
183static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
184{
185 struct perf_event_context *ctx;
186 unsigned long flags;
187
188 ctx = perf_lock_task_context(task, &flags);
189 if (ctx) {
190 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100191 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200192 }
193 return ctx;
194}
195
196static void perf_unpin_context(struct perf_event_context *ctx)
197{
198 unsigned long flags;
199
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100200 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200201 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100202 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200203 put_ctx(ctx);
204}
205
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100206static inline u64 perf_clock(void)
207{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200208 return local_clock();
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100209}
210
211/*
212 * Update the record of the current time in a context.
213 */
214static void update_context_time(struct perf_event_context *ctx)
215{
216 u64 now = perf_clock();
217
218 ctx->time += now - ctx->timestamp;
219 ctx->timestamp = now;
220}
221
222/*
223 * Update the total_time_enabled and total_time_running fields for a event.
224 */
225static void update_event_times(struct perf_event *event)
226{
227 struct perf_event_context *ctx = event->ctx;
228 u64 run_end;
229
230 if (event->state < PERF_EVENT_STATE_INACTIVE ||
231 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
232 return;
233
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100234 if (ctx->is_active)
235 run_end = ctx->time;
236 else
237 run_end = event->tstamp_stopped;
238
239 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100240
241 if (event->state == PERF_EVENT_STATE_INACTIVE)
242 run_end = event->tstamp_stopped;
243 else
244 run_end = ctx->time;
245
246 event->total_time_running = run_end - event->tstamp_running;
247}
248
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200249/*
250 * Update total_time_enabled and total_time_running for all events in a group.
251 */
252static void update_group_times(struct perf_event *leader)
253{
254 struct perf_event *event;
255
256 update_event_times(leader);
257 list_for_each_entry(event, &leader->sibling_list, group_entry)
258 update_event_times(event);
259}
260
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100261static struct list_head *
262ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
263{
264 if (event->attr.pinned)
265 return &ctx->pinned_groups;
266 else
267 return &ctx->flexible_groups;
268}
269
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200270/*
271 * Add a event from the lists for its context.
272 * Must be called with ctx->mutex and ctx->lock held.
273 */
274static void
275list_add_event(struct perf_event *event, struct perf_event_context *ctx)
276{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200277 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
278 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200279
280 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200281 * If we're a stand alone event or group leader, we go to the context
282 * list, group events are kept attached to the group so that
283 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200284 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200285 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100286 struct list_head *list;
287
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100288 if (is_software_event(event))
289 event->group_flags |= PERF_GROUP_SOFTWARE;
290
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100291 list = ctx_group_list(event, ctx);
292 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200293 }
294
295 list_add_rcu(&event->event_entry, &ctx->event_list);
296 ctx->nr_events++;
297 if (event->attr.inherit_stat)
298 ctx->nr_stat++;
299}
300
Peter Zijlstra8a495422010-05-27 15:47:49 +0200301static void perf_group_attach(struct perf_event *event)
302{
303 struct perf_event *group_leader = event->group_leader;
304
305 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_GROUP);
306 event->attach_state |= PERF_ATTACH_GROUP;
307
308 if (group_leader == event)
309 return;
310
311 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
312 !is_software_event(event))
313 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
314
315 list_add_tail(&event->group_entry, &group_leader->sibling_list);
316 group_leader->nr_siblings++;
317}
318
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200319/*
320 * Remove a event from the lists for its context.
321 * Must be called with ctx->mutex and ctx->lock held.
322 */
323static void
324list_del_event(struct perf_event *event, struct perf_event_context *ctx)
325{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200326 /*
327 * We can have double detach due to exit/hot-unplug + close.
328 */
329 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200330 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200331
332 event->attach_state &= ~PERF_ATTACH_CONTEXT;
333
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200334 ctx->nr_events--;
335 if (event->attr.inherit_stat)
336 ctx->nr_stat--;
337
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200338 list_del_rcu(&event->event_entry);
339
Peter Zijlstra8a495422010-05-27 15:47:49 +0200340 if (event->group_leader == event)
341 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200342
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200343 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800344
345 /*
346 * If event was in error state, then keep it
347 * that way, otherwise bogus counts will be
348 * returned on read(). The only way to get out
349 * of error state is by explicit re-enabling
350 * of the event
351 */
352 if (event->state > PERF_EVENT_STATE_OFF)
353 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200354}
355
Peter Zijlstra8a495422010-05-27 15:47:49 +0200356static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200357{
358 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200359 struct list_head *list = NULL;
360
361 /*
362 * We can have double detach due to exit/hot-unplug + close.
363 */
364 if (!(event->attach_state & PERF_ATTACH_GROUP))
365 return;
366
367 event->attach_state &= ~PERF_ATTACH_GROUP;
368
369 /*
370 * If this is a sibling, remove it from its group.
371 */
372 if (event->group_leader != event) {
373 list_del_init(&event->group_entry);
374 event->group_leader->nr_siblings--;
375 return;
376 }
377
378 if (!list_empty(&event->group_entry))
379 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100380
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200381 /*
382 * If this was a group event with sibling events then
383 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +0200384 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200385 */
386 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +0200387 if (list)
388 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200389 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100390
391 /* Inherit group flags from the previous leader */
392 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200393 }
394}
395
Stephane Eranianfa66f072010-08-26 16:40:01 +0200396static inline int
397event_filter_match(struct perf_event *event)
398{
399 return event->cpu == -1 || event->cpu == smp_processor_id();
400}
401
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200402static void
403event_sched_out(struct perf_event *event,
404 struct perf_cpu_context *cpuctx,
405 struct perf_event_context *ctx)
406{
Stephane Eranianfa66f072010-08-26 16:40:01 +0200407 u64 delta;
408 /*
409 * An event which could not be activated because of
410 * filter mismatch still needs to have its timings
411 * maintained, otherwise bogus information is return
412 * via read() for time_enabled, time_running:
413 */
414 if (event->state == PERF_EVENT_STATE_INACTIVE
415 && !event_filter_match(event)) {
416 delta = ctx->time - event->tstamp_stopped;
417 event->tstamp_running += delta;
418 event->tstamp_stopped = ctx->time;
419 }
420
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200421 if (event->state != PERF_EVENT_STATE_ACTIVE)
422 return;
423
424 event->state = PERF_EVENT_STATE_INACTIVE;
425 if (event->pending_disable) {
426 event->pending_disable = 0;
427 event->state = PERF_EVENT_STATE_OFF;
428 }
429 event->tstamp_stopped = ctx->time;
430 event->pmu->disable(event);
431 event->oncpu = -1;
432
433 if (!is_software_event(event))
434 cpuctx->active_oncpu--;
435 ctx->nr_active--;
436 if (event->attr.exclusive || !cpuctx->active_oncpu)
437 cpuctx->exclusive = 0;
438}
439
440static void
441group_sched_out(struct perf_event *group_event,
442 struct perf_cpu_context *cpuctx,
443 struct perf_event_context *ctx)
444{
445 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +0200446 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200447
448 event_sched_out(group_event, cpuctx, ctx);
449
450 /*
451 * Schedule out siblings (if any):
452 */
453 list_for_each_entry(event, &group_event->sibling_list, group_entry)
454 event_sched_out(event, cpuctx, ctx);
455
Stephane Eranianfa66f072010-08-26 16:40:01 +0200456 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200457 cpuctx->exclusive = 0;
458}
459
460/*
461 * Cross CPU call to remove a performance event
462 *
463 * We disable the event on the hardware level first. After that we
464 * remove it from the context list.
465 */
466static void __perf_event_remove_from_context(void *info)
467{
468 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
469 struct perf_event *event = info;
470 struct perf_event_context *ctx = event->ctx;
471
472 /*
473 * If this is a task context, we need to check whether it is
474 * the current task context of this cpu. If not it has been
475 * scheduled out before the smp call arrived.
476 */
477 if (ctx->task && cpuctx->task_ctx != ctx)
478 return;
479
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100480 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200481 /*
482 * Protect the list operation against NMI by disabling the
483 * events on a global level.
484 */
485 perf_disable();
486
487 event_sched_out(event, cpuctx, ctx);
488
489 list_del_event(event, ctx);
490
491 if (!ctx->task) {
492 /*
493 * Allow more per task events with respect to the
494 * reservation:
495 */
496 cpuctx->max_pertask =
497 min(perf_max_events - ctx->nr_events,
498 perf_max_events - perf_reserved_percpu);
499 }
500
501 perf_enable();
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100502 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200503}
504
505
506/*
507 * Remove the event from a task's (or a CPU's) list of events.
508 *
509 * Must be called with ctx->mutex held.
510 *
511 * CPU events are removed with a smp call. For task events we only
512 * call when the task is on a CPU.
513 *
514 * If event->ctx is a cloned context, callers must make sure that
515 * every task struct that event->ctx->task could possibly point to
516 * remains valid. This is OK when called from perf_release since
517 * that only calls us on the top-level context, which can't be a clone.
518 * When called from perf_event_exit_task, it's OK because the
519 * context has been detached from its task.
520 */
521static void perf_event_remove_from_context(struct perf_event *event)
522{
523 struct perf_event_context *ctx = event->ctx;
524 struct task_struct *task = ctx->task;
525
526 if (!task) {
527 /*
528 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200529 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200530 */
531 smp_call_function_single(event->cpu,
532 __perf_event_remove_from_context,
533 event, 1);
534 return;
535 }
536
537retry:
538 task_oncpu_function_call(task, __perf_event_remove_from_context,
539 event);
540
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100541 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200542 /*
543 * If the context is active we need to retry the smp call.
544 */
545 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100546 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200547 goto retry;
548 }
549
550 /*
551 * The lock prevents that this context is scheduled in so we
552 * can remove the event safely, if the call above did not
553 * succeed.
554 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100555 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200556 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100557 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200558}
559
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200560/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200561 * Cross CPU call to disable a performance event
562 */
563static void __perf_event_disable(void *info)
564{
565 struct perf_event *event = info;
566 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
567 struct perf_event_context *ctx = event->ctx;
568
569 /*
570 * If this is a per-task event, need to check whether this
571 * event's task is the current task on this cpu.
572 */
573 if (ctx->task && cpuctx->task_ctx != ctx)
574 return;
575
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100576 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200577
578 /*
579 * If the event is on, turn it off.
580 * If it is in error state, leave it in error state.
581 */
582 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
583 update_context_time(ctx);
584 update_group_times(event);
585 if (event == event->group_leader)
586 group_sched_out(event, cpuctx, ctx);
587 else
588 event_sched_out(event, cpuctx, ctx);
589 event->state = PERF_EVENT_STATE_OFF;
590 }
591
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100592 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200593}
594
595/*
596 * Disable a event.
597 *
598 * If event->ctx is a cloned context, callers must make sure that
599 * every task struct that event->ctx->task could possibly point to
600 * remains valid. This condition is satisifed when called through
601 * perf_event_for_each_child or perf_event_for_each because they
602 * hold the top-level event's child_mutex, so any descendant that
603 * goes to exit will block in sync_child_event.
604 * When called from perf_pending_event it's OK because event->ctx
605 * is the current context on this CPU and preemption is disabled,
606 * hence we can't get into perf_event_task_sched_out for this context.
607 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100608void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200609{
610 struct perf_event_context *ctx = event->ctx;
611 struct task_struct *task = ctx->task;
612
613 if (!task) {
614 /*
615 * Disable the event on the cpu that it's on
616 */
617 smp_call_function_single(event->cpu, __perf_event_disable,
618 event, 1);
619 return;
620 }
621
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200622retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200623 task_oncpu_function_call(task, __perf_event_disable, event);
624
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100625 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200626 /*
627 * If the event is still active, we need to retry the cross-call.
628 */
629 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100630 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200631 goto retry;
632 }
633
634 /*
635 * Since we have the lock this context can't be scheduled
636 * in, so we can change the state safely.
637 */
638 if (event->state == PERF_EVENT_STATE_INACTIVE) {
639 update_group_times(event);
640 event->state = PERF_EVENT_STATE_OFF;
641 }
642
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100643 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200644}
645
646static int
647event_sched_in(struct perf_event *event,
648 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100649 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200650{
651 if (event->state <= PERF_EVENT_STATE_OFF)
652 return 0;
653
654 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +0100655 event->oncpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200656 /*
657 * The new state must be visible before we turn it on in the hardware:
658 */
659 smp_wmb();
660
661 if (event->pmu->enable(event)) {
662 event->state = PERF_EVENT_STATE_INACTIVE;
663 event->oncpu = -1;
664 return -EAGAIN;
665 }
666
667 event->tstamp_running += ctx->time - event->tstamp_stopped;
668
669 if (!is_software_event(event))
670 cpuctx->active_oncpu++;
671 ctx->nr_active++;
672
673 if (event->attr.exclusive)
674 cpuctx->exclusive = 1;
675
676 return 0;
677}
678
679static int
680group_sched_in(struct perf_event *group_event,
681 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100682 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200683{
Lin Ming6bde9b62010-04-23 13:56:00 +0800684 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +0200685 struct pmu *pmu = group_event->pmu;
Lin Ming6bde9b62010-04-23 13:56:00 +0800686 bool txn = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200687
688 if (group_event->state == PERF_EVENT_STATE_OFF)
689 return 0;
690
Lin Ming6bde9b62010-04-23 13:56:00 +0800691 /* Check if group transaction availabe */
692 if (pmu->start_txn)
693 txn = true;
694
695 if (txn)
696 pmu->start_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200697
Stephane Eranian90151c352010-05-25 16:23:10 +0200698 if (event_sched_in(group_event, cpuctx, ctx)) {
699 if (txn)
700 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200701 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +0200702 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200703
704 /*
705 * Schedule in siblings as one group (if any):
706 */
707 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Peter Zijlstra6e377382010-02-11 13:21:58 +0100708 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200709 partial_group = event;
710 goto group_error;
711 }
712 }
713
Peter Zijlstra8d2cacb2010-05-25 17:49:05 +0200714 if (!txn || !pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +1000715 return 0;
Lin Ming6bde9b62010-04-23 13:56:00 +0800716
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200717group_error:
718 /*
719 * Groups can be scheduled in as one unit only, so undo any
720 * partial group before returning:
721 */
722 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
723 if (event == partial_group)
724 break;
725 event_sched_out(event, cpuctx, ctx);
726 }
727 event_sched_out(group_event, cpuctx, ctx);
728
Stephane Eranian90151c352010-05-25 16:23:10 +0200729 if (txn)
730 pmu->cancel_txn(pmu);
731
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200732 return -EAGAIN;
733}
734
735/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200736 * Work out whether we can put this event group on the CPU now.
737 */
738static int group_can_go_on(struct perf_event *event,
739 struct perf_cpu_context *cpuctx,
740 int can_add_hw)
741{
742 /*
743 * Groups consisting entirely of software events can always go on.
744 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100745 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200746 return 1;
747 /*
748 * If an exclusive group is already on, no other hardware
749 * events can go on.
750 */
751 if (cpuctx->exclusive)
752 return 0;
753 /*
754 * If this group is exclusive and there are already
755 * events on the CPU, it can't go on.
756 */
757 if (event->attr.exclusive && cpuctx->active_oncpu)
758 return 0;
759 /*
760 * Otherwise, try to add it if all previous groups were able
761 * to go on.
762 */
763 return can_add_hw;
764}
765
766static void add_event_to_ctx(struct perf_event *event,
767 struct perf_event_context *ctx)
768{
769 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200770 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200771 event->tstamp_enabled = ctx->time;
772 event->tstamp_running = ctx->time;
773 event->tstamp_stopped = ctx->time;
774}
775
776/*
777 * Cross CPU call to install and enable a performance event
778 *
779 * Must be called with ctx->mutex held
780 */
781static void __perf_install_in_context(void *info)
782{
783 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
784 struct perf_event *event = info;
785 struct perf_event_context *ctx = event->ctx;
786 struct perf_event *leader = event->group_leader;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200787 int err;
788
789 /*
790 * If this is a task context, we need to check whether it is
791 * the current task context of this cpu. If not it has been
792 * scheduled out before the smp call arrived.
793 * Or possibly this is the right context but it isn't
794 * on this cpu because it had no events.
795 */
796 if (ctx->task && cpuctx->task_ctx != ctx) {
797 if (cpuctx->task_ctx || ctx->task != current)
798 return;
799 cpuctx->task_ctx = ctx;
800 }
801
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100802 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200803 ctx->is_active = 1;
804 update_context_time(ctx);
805
806 /*
807 * Protect the list operation against NMI by disabling the
808 * events on a global level. NOP for non NMI based events.
809 */
810 perf_disable();
811
812 add_event_to_ctx(event, ctx);
813
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100814 if (event->cpu != -1 && event->cpu != smp_processor_id())
815 goto unlock;
816
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200817 /*
818 * Don't put the event on if it is disabled or if
819 * it is in a group and the group isn't on.
820 */
821 if (event->state != PERF_EVENT_STATE_INACTIVE ||
822 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
823 goto unlock;
824
825 /*
826 * An exclusive event can't go on if there are already active
827 * hardware events, and no hardware event can go on if there
828 * is already an exclusive event on.
829 */
830 if (!group_can_go_on(event, cpuctx, 1))
831 err = -EEXIST;
832 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100833 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200834
835 if (err) {
836 /*
837 * This event couldn't go on. If it is in a group
838 * then we have to pull the whole group off.
839 * If the event group is pinned then put it in error state.
840 */
841 if (leader != event)
842 group_sched_out(leader, cpuctx, ctx);
843 if (leader->attr.pinned) {
844 update_group_times(leader);
845 leader->state = PERF_EVENT_STATE_ERROR;
846 }
847 }
848
849 if (!err && !ctx->task && cpuctx->max_pertask)
850 cpuctx->max_pertask--;
851
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200852unlock:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200853 perf_enable();
854
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100855 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200856}
857
858/*
859 * Attach a performance event to a context
860 *
861 * First we add the event to the list with the hardware enable bit
862 * in event->hw_config cleared.
863 *
864 * If the event is attached to a task which is on a CPU we use a smp
865 * call to enable it in the task context. The task might have been
866 * scheduled away, but we check this in the smp call again.
867 *
868 * Must be called with ctx->mutex held.
869 */
870static void
871perf_install_in_context(struct perf_event_context *ctx,
872 struct perf_event *event,
873 int cpu)
874{
875 struct task_struct *task = ctx->task;
876
877 if (!task) {
878 /*
879 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200880 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200881 */
882 smp_call_function_single(cpu, __perf_install_in_context,
883 event, 1);
884 return;
885 }
886
887retry:
888 task_oncpu_function_call(task, __perf_install_in_context,
889 event);
890
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100891 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200892 /*
893 * we need to retry the smp call.
894 */
895 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100896 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200897 goto retry;
898 }
899
900 /*
901 * The lock prevents that this context is scheduled in so we
902 * can add the event safely, if it the call above did not
903 * succeed.
904 */
905 if (list_empty(&event->group_entry))
906 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100907 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200908}
909
910/*
911 * Put a event into inactive state and update time fields.
912 * Enabling the leader of a group effectively enables all
913 * the group members that aren't explicitly disabled, so we
914 * have to update their ->tstamp_enabled also.
915 * Note: this works for group members as well as group leaders
916 * since the non-leader members' sibling_lists will be empty.
917 */
918static void __perf_event_mark_enabled(struct perf_event *event,
919 struct perf_event_context *ctx)
920{
921 struct perf_event *sub;
922
923 event->state = PERF_EVENT_STATE_INACTIVE;
924 event->tstamp_enabled = ctx->time - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200925 list_for_each_entry(sub, &event->sibling_list, group_entry) {
926 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200927 sub->tstamp_enabled =
928 ctx->time - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200929 }
930 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200931}
932
933/*
934 * Cross CPU call to enable a performance event
935 */
936static void __perf_event_enable(void *info)
937{
938 struct perf_event *event = info;
939 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
940 struct perf_event_context *ctx = event->ctx;
941 struct perf_event *leader = event->group_leader;
942 int err;
943
944 /*
945 * If this is a per-task event, need to check whether this
946 * event's task is the current task on this cpu.
947 */
948 if (ctx->task && cpuctx->task_ctx != ctx) {
949 if (cpuctx->task_ctx || ctx->task != current)
950 return;
951 cpuctx->task_ctx = ctx;
952 }
953
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100954 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200955 ctx->is_active = 1;
956 update_context_time(ctx);
957
958 if (event->state >= PERF_EVENT_STATE_INACTIVE)
959 goto unlock;
960 __perf_event_mark_enabled(event, ctx);
961
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100962 if (event->cpu != -1 && event->cpu != smp_processor_id())
963 goto unlock;
964
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200965 /*
966 * If the event is in a group and isn't the group leader,
967 * then don't put it on unless the group is on.
968 */
969 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
970 goto unlock;
971
972 if (!group_can_go_on(event, cpuctx, 1)) {
973 err = -EEXIST;
974 } else {
975 perf_disable();
976 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +0100977 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200978 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100979 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200980 perf_enable();
981 }
982
983 if (err) {
984 /*
985 * If this event can't go on and it's part of a
986 * group, then the whole group has to come off.
987 */
988 if (leader != event)
989 group_sched_out(leader, cpuctx, ctx);
990 if (leader->attr.pinned) {
991 update_group_times(leader);
992 leader->state = PERF_EVENT_STATE_ERROR;
993 }
994 }
995
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200996unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100997 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200998}
999
1000/*
1001 * Enable a event.
1002 *
1003 * If event->ctx is a cloned context, callers must make sure that
1004 * every task struct that event->ctx->task could possibly point to
1005 * remains valid. This condition is satisfied when called through
1006 * perf_event_for_each_child or perf_event_for_each as described
1007 * for perf_event_disable.
1008 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001009void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001010{
1011 struct perf_event_context *ctx = event->ctx;
1012 struct task_struct *task = ctx->task;
1013
1014 if (!task) {
1015 /*
1016 * Enable the event on the cpu that it's on
1017 */
1018 smp_call_function_single(event->cpu, __perf_event_enable,
1019 event, 1);
1020 return;
1021 }
1022
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001023 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001024 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1025 goto out;
1026
1027 /*
1028 * If the event is in error state, clear that first.
1029 * That way, if we see the event in error state below, we
1030 * know that it has gone back into error state, as distinct
1031 * from the task having been scheduled away before the
1032 * cross-call arrived.
1033 */
1034 if (event->state == PERF_EVENT_STATE_ERROR)
1035 event->state = PERF_EVENT_STATE_OFF;
1036
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001037retry:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001038 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001039 task_oncpu_function_call(task, __perf_event_enable, event);
1040
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001041 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001042
1043 /*
1044 * If the context is active and the event is still off,
1045 * we need to retry the cross-call.
1046 */
1047 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1048 goto retry;
1049
1050 /*
1051 * Since we have the lock this context can't be scheduled
1052 * in, so we can change the state safely.
1053 */
1054 if (event->state == PERF_EVENT_STATE_OFF)
1055 __perf_event_mark_enabled(event, ctx);
1056
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001057out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001058 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001059}
1060
1061static int perf_event_refresh(struct perf_event *event, int refresh)
1062{
1063 /*
1064 * not supported on inherited events
1065 */
1066 if (event->attr.inherit)
1067 return -EINVAL;
1068
1069 atomic_add(refresh, &event->event_limit);
1070 perf_event_enable(event);
1071
1072 return 0;
1073}
1074
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001075enum event_type_t {
1076 EVENT_FLEXIBLE = 0x1,
1077 EVENT_PINNED = 0x2,
1078 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1079};
1080
1081static void ctx_sched_out(struct perf_event_context *ctx,
1082 struct perf_cpu_context *cpuctx,
1083 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001084{
1085 struct perf_event *event;
1086
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001087 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001088 ctx->is_active = 0;
1089 if (likely(!ctx->nr_events))
1090 goto out;
1091 update_context_time(ctx);
1092
1093 perf_disable();
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001094 if (!ctx->nr_active)
1095 goto out_enable;
1096
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001097 if (event_type & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001098 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1099 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001100 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001101
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001102 if (event_type & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001103 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001104 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001105 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001106
1107 out_enable:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001108 perf_enable();
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001109out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001110 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001111}
1112
1113/*
1114 * Test whether two contexts are equivalent, i.e. whether they
1115 * have both been cloned from the same version of the same context
1116 * and they both have the same number of enabled events.
1117 * If the number of enabled events is the same, then the set
1118 * of enabled events should be the same, because these are both
1119 * inherited contexts, therefore we can't access individual events
1120 * in them directly with an fd; we can only enable/disable all
1121 * events via prctl, or enable/disable all events in a family
1122 * via ioctl, which will have the same effect on both contexts.
1123 */
1124static int context_equiv(struct perf_event_context *ctx1,
1125 struct perf_event_context *ctx2)
1126{
1127 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1128 && ctx1->parent_gen == ctx2->parent_gen
1129 && !ctx1->pin_count && !ctx2->pin_count;
1130}
1131
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001132static void __perf_event_sync_stat(struct perf_event *event,
1133 struct perf_event *next_event)
1134{
1135 u64 value;
1136
1137 if (!event->attr.inherit_stat)
1138 return;
1139
1140 /*
1141 * Update the event value, we cannot use perf_event_read()
1142 * because we're in the middle of a context switch and have IRQs
1143 * disabled, which upsets smp_call_function_single(), however
1144 * we know the event must be on the current CPU, therefore we
1145 * don't need to use it.
1146 */
1147 switch (event->state) {
1148 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001149 event->pmu->read(event);
1150 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001151
1152 case PERF_EVENT_STATE_INACTIVE:
1153 update_event_times(event);
1154 break;
1155
1156 default:
1157 break;
1158 }
1159
1160 /*
1161 * In order to keep per-task stats reliable we need to flip the event
1162 * values when we flip the contexts.
1163 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001164 value = local64_read(&next_event->count);
1165 value = local64_xchg(&event->count, value);
1166 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001167
1168 swap(event->total_time_enabled, next_event->total_time_enabled);
1169 swap(event->total_time_running, next_event->total_time_running);
1170
1171 /*
1172 * Since we swizzled the values, update the user visible data too.
1173 */
1174 perf_event_update_userpage(event);
1175 perf_event_update_userpage(next_event);
1176}
1177
1178#define list_next_entry(pos, member) \
1179 list_entry(pos->member.next, typeof(*pos), member)
1180
1181static void perf_event_sync_stat(struct perf_event_context *ctx,
1182 struct perf_event_context *next_ctx)
1183{
1184 struct perf_event *event, *next_event;
1185
1186 if (!ctx->nr_stat)
1187 return;
1188
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001189 update_context_time(ctx);
1190
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001191 event = list_first_entry(&ctx->event_list,
1192 struct perf_event, event_entry);
1193
1194 next_event = list_first_entry(&next_ctx->event_list,
1195 struct perf_event, event_entry);
1196
1197 while (&event->event_entry != &ctx->event_list &&
1198 &next_event->event_entry != &next_ctx->event_list) {
1199
1200 __perf_event_sync_stat(event, next_event);
1201
1202 event = list_next_entry(event, event_entry);
1203 next_event = list_next_entry(next_event, event_entry);
1204 }
1205}
1206
1207/*
1208 * Called from scheduler to remove the events of the current task,
1209 * with interrupts disabled.
1210 *
1211 * We stop each event and update the event value in event->count.
1212 *
1213 * This does not protect us against NMI, but disable()
1214 * sets the disabled bit in the control field of event _before_
1215 * accessing the event control register. If a NMI hits, then it will
1216 * not restart the event.
1217 */
1218void perf_event_task_sched_out(struct task_struct *task,
Peter Zijlstra49f47432009-12-27 11:51:52 +01001219 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001220{
Peter Zijlstra49f47432009-12-27 11:51:52 +01001221 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001222 struct perf_event_context *ctx = task->perf_event_ctxp;
1223 struct perf_event_context *next_ctx;
1224 struct perf_event_context *parent;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001225 int do_switch = 1;
1226
Frederic Weisbeckere49a5bd2010-03-22 19:40:03 +01001227 perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001228
1229 if (likely(!ctx || !cpuctx->task_ctx))
1230 return;
1231
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001232 rcu_read_lock();
1233 parent = rcu_dereference(ctx->parent_ctx);
1234 next_ctx = next->perf_event_ctxp;
1235 if (parent && next_ctx &&
1236 rcu_dereference(next_ctx->parent_ctx) == parent) {
1237 /*
1238 * Looks like the two contexts are clones, so we might be
1239 * able to optimize the context switch. We lock both
1240 * contexts and check that they are clones under the
1241 * lock (including re-checking that neither has been
1242 * uncloned in the meantime). It doesn't matter which
1243 * order we take the locks because no other cpu could
1244 * be trying to lock both of these tasks.
1245 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001246 raw_spin_lock(&ctx->lock);
1247 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001248 if (context_equiv(ctx, next_ctx)) {
1249 /*
1250 * XXX do we need a memory barrier of sorts
1251 * wrt to rcu_dereference() of perf_event_ctxp
1252 */
1253 task->perf_event_ctxp = next_ctx;
1254 next->perf_event_ctxp = ctx;
1255 ctx->task = next;
1256 next_ctx->task = task;
1257 do_switch = 0;
1258
1259 perf_event_sync_stat(ctx, next_ctx);
1260 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001261 raw_spin_unlock(&next_ctx->lock);
1262 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001263 }
1264 rcu_read_unlock();
1265
1266 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001267 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001268 cpuctx->task_ctx = NULL;
1269 }
1270}
1271
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001272static void task_ctx_sched_out(struct perf_event_context *ctx,
1273 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001274{
1275 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1276
1277 if (!cpuctx->task_ctx)
1278 return;
1279
1280 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1281 return;
1282
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001283 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001284 cpuctx->task_ctx = NULL;
1285}
1286
1287/*
1288 * Called with IRQs disabled
1289 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001290static void __perf_event_task_sched_out(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001291{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001292 task_ctx_sched_out(ctx, EVENT_ALL);
1293}
1294
1295/*
1296 * Called with IRQs disabled
1297 */
1298static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1299 enum event_type_t event_type)
1300{
1301 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001302}
1303
1304static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001305ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001306 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001307{
1308 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001309
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001310 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1311 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001312 continue;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001313 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001314 continue;
1315
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001316 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001317 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001318
1319 /*
1320 * If this pinned group hasn't been scheduled,
1321 * put it in error state.
1322 */
1323 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1324 update_group_times(event);
1325 event->state = PERF_EVENT_STATE_ERROR;
1326 }
1327 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001328}
1329
1330static void
1331ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001332 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001333{
1334 struct perf_event *event;
1335 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001336
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001337 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1338 /* Ignore events in OFF or ERROR state */
1339 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001340 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001341 /*
1342 * Listen to the 'cpu' scheduling filter constraint
1343 * of events:
1344 */
Peter Zijlstra6e377382010-02-11 13:21:58 +01001345 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001346 continue;
1347
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001348 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01001349 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001350 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001351 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001352 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001353}
1354
1355static void
1356ctx_sched_in(struct perf_event_context *ctx,
1357 struct perf_cpu_context *cpuctx,
1358 enum event_type_t event_type)
1359{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001360 raw_spin_lock(&ctx->lock);
1361 ctx->is_active = 1;
1362 if (likely(!ctx->nr_events))
1363 goto out;
1364
1365 ctx->timestamp = perf_clock();
1366
1367 perf_disable();
1368
1369 /*
1370 * First go through the list and put on any pinned groups
1371 * in order to give them the best chance of going on.
1372 */
1373 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001374 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001375
1376 /* Then walk through the lower prio flexible groups */
1377 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001378 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001379
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001380 perf_enable();
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001381out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001382 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001383}
1384
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001385static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1386 enum event_type_t event_type)
1387{
1388 struct perf_event_context *ctx = &cpuctx->ctx;
1389
1390 ctx_sched_in(ctx, cpuctx, event_type);
1391}
1392
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001393static void task_ctx_sched_in(struct task_struct *task,
1394 enum event_type_t event_type)
1395{
1396 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1397 struct perf_event_context *ctx = task->perf_event_ctxp;
1398
1399 if (likely(!ctx))
1400 return;
1401 if (cpuctx->task_ctx == ctx)
1402 return;
1403 ctx_sched_in(ctx, cpuctx, event_type);
1404 cpuctx->task_ctx = ctx;
1405}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001406/*
1407 * Called from scheduler to add the events of the current task
1408 * with interrupts disabled.
1409 *
1410 * We restore the event value and then enable it.
1411 *
1412 * This does not protect us against NMI, but enable()
1413 * sets the enabled bit in the control field of event _before_
1414 * accessing the event control register. If a NMI hits, then it will
1415 * keep the event running.
1416 */
Peter Zijlstra49f47432009-12-27 11:51:52 +01001417void perf_event_task_sched_in(struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001418{
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001419 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1420 struct perf_event_context *ctx = task->perf_event_ctxp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001421
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001422 if (likely(!ctx))
1423 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001424
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001425 if (cpuctx->task_ctx == ctx)
1426 return;
1427
eranian@google.com9b33fa62010-03-10 22:26:05 -08001428 perf_disable();
1429
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001430 /*
1431 * We want to keep the following priority order:
1432 * cpu pinned (that don't need to move), task pinned,
1433 * cpu flexible, task flexible.
1434 */
1435 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1436
1437 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1438 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1439 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1440
1441 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08001442
1443 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001444}
1445
1446#define MAX_INTERRUPTS (~0ULL)
1447
1448static void perf_log_throttle(struct perf_event *event, int enable);
1449
Peter Zijlstraabd50712010-01-26 18:50:16 +01001450static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1451{
1452 u64 frequency = event->attr.sample_freq;
1453 u64 sec = NSEC_PER_SEC;
1454 u64 divisor, dividend;
1455
1456 int count_fls, nsec_fls, frequency_fls, sec_fls;
1457
1458 count_fls = fls64(count);
1459 nsec_fls = fls64(nsec);
1460 frequency_fls = fls64(frequency);
1461 sec_fls = 30;
1462
1463 /*
1464 * We got @count in @nsec, with a target of sample_freq HZ
1465 * the target period becomes:
1466 *
1467 * @count * 10^9
1468 * period = -------------------
1469 * @nsec * sample_freq
1470 *
1471 */
1472
1473 /*
1474 * Reduce accuracy by one bit such that @a and @b converge
1475 * to a similar magnitude.
1476 */
1477#define REDUCE_FLS(a, b) \
1478do { \
1479 if (a##_fls > b##_fls) { \
1480 a >>= 1; \
1481 a##_fls--; \
1482 } else { \
1483 b >>= 1; \
1484 b##_fls--; \
1485 } \
1486} while (0)
1487
1488 /*
1489 * Reduce accuracy until either term fits in a u64, then proceed with
1490 * the other, so that finally we can do a u64/u64 division.
1491 */
1492 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1493 REDUCE_FLS(nsec, frequency);
1494 REDUCE_FLS(sec, count);
1495 }
1496
1497 if (count_fls + sec_fls > 64) {
1498 divisor = nsec * frequency;
1499
1500 while (count_fls + sec_fls > 64) {
1501 REDUCE_FLS(count, sec);
1502 divisor >>= 1;
1503 }
1504
1505 dividend = count * sec;
1506 } else {
1507 dividend = count * sec;
1508
1509 while (nsec_fls + frequency_fls > 64) {
1510 REDUCE_FLS(nsec, frequency);
1511 dividend >>= 1;
1512 }
1513
1514 divisor = nsec * frequency;
1515 }
1516
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001517 if (!divisor)
1518 return dividend;
1519
Peter Zijlstraabd50712010-01-26 18:50:16 +01001520 return div64_u64(dividend, divisor);
1521}
1522
Stephane Eraniand76a0812010-02-08 17:06:01 +02001523static void perf_event_stop(struct perf_event *event)
1524{
1525 if (!event->pmu->stop)
1526 return event->pmu->disable(event);
1527
1528 return event->pmu->stop(event);
1529}
1530
1531static int perf_event_start(struct perf_event *event)
1532{
1533 if (!event->pmu->start)
1534 return event->pmu->enable(event);
1535
1536 return event->pmu->start(event);
1537}
1538
Peter Zijlstraabd50712010-01-26 18:50:16 +01001539static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001540{
1541 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001542 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001543 s64 delta;
1544
Peter Zijlstraabd50712010-01-26 18:50:16 +01001545 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001546
1547 delta = (s64)(period - hwc->sample_period);
1548 delta = (delta + 7) / 8; /* low pass filter */
1549
1550 sample_period = hwc->sample_period + delta;
1551
1552 if (!sample_period)
1553 sample_period = 1;
1554
1555 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001556
Peter Zijlstrae7850592010-05-21 14:43:08 +02001557 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraabd50712010-01-26 18:50:16 +01001558 perf_disable();
Stephane Eraniand76a0812010-02-08 17:06:01 +02001559 perf_event_stop(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001560 local64_set(&hwc->period_left, 0);
Stephane Eraniand76a0812010-02-08 17:06:01 +02001561 perf_event_start(event);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001562 perf_enable();
1563 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001564}
1565
1566static void perf_ctx_adjust_freq(struct perf_event_context *ctx)
1567{
1568 struct perf_event *event;
1569 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001570 u64 interrupts, now;
1571 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001572
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001573 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001574 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001575 if (event->state != PERF_EVENT_STATE_ACTIVE)
1576 continue;
1577
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001578 if (event->cpu != -1 && event->cpu != smp_processor_id())
1579 continue;
1580
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001581 hwc = &event->hw;
1582
1583 interrupts = hwc->interrupts;
1584 hwc->interrupts = 0;
1585
1586 /*
1587 * unthrottle events on the tick
1588 */
1589 if (interrupts == MAX_INTERRUPTS) {
1590 perf_log_throttle(event, 1);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001591 perf_disable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001592 event->pmu->unthrottle(event);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001593 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001594 }
1595
1596 if (!event->attr.freq || !event->attr.sample_freq)
1597 continue;
1598
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001599 perf_disable();
Peter Zijlstraabd50712010-01-26 18:50:16 +01001600 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001601 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001602 delta = now - hwc->freq_count_stamp;
1603 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001604
Peter Zijlstraabd50712010-01-26 18:50:16 +01001605 if (delta > 0)
1606 perf_adjust_period(event, TICK_NSEC, delta);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001607 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001608 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001609 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001610}
1611
1612/*
1613 * Round-robin a context's events:
1614 */
1615static void rotate_ctx(struct perf_event_context *ctx)
1616{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001617 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001618
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001619 /* Rotate the first entry last of non-pinned groups */
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001620 list_rotate_left(&ctx->flexible_groups);
1621
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001622 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001623}
1624
Peter Zijlstra49f47432009-12-27 11:51:52 +01001625void perf_event_task_tick(struct task_struct *curr)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001626{
1627 struct perf_cpu_context *cpuctx;
1628 struct perf_event_context *ctx;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001629 int rotate = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001630
1631 if (!atomic_read(&nr_events))
1632 return;
1633
Peter Zijlstra49f47432009-12-27 11:51:52 +01001634 cpuctx = &__get_cpu_var(perf_cpu_context);
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001635 if (cpuctx->ctx.nr_events &&
1636 cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1637 rotate = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001638
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001639 ctx = curr->perf_event_ctxp;
1640 if (ctx && ctx->nr_events && ctx->nr_events != ctx->nr_active)
1641 rotate = 1;
Peter Zijlstra9717e6c2010-01-28 13:57:44 +01001642
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001643 perf_ctx_adjust_freq(&cpuctx->ctx);
1644 if (ctx)
1645 perf_ctx_adjust_freq(ctx);
1646
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001647 if (!rotate)
1648 return;
1649
1650 perf_disable();
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001651 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001652 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001653 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001654
1655 rotate_ctx(&cpuctx->ctx);
1656 if (ctx)
1657 rotate_ctx(ctx);
1658
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001659 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001660 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001661 task_ctx_sched_in(curr, EVENT_FLEXIBLE);
Peter Zijlstra9717e6c2010-01-28 13:57:44 +01001662 perf_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001663}
1664
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001665static int event_enable_on_exec(struct perf_event *event,
1666 struct perf_event_context *ctx)
1667{
1668 if (!event->attr.enable_on_exec)
1669 return 0;
1670
1671 event->attr.enable_on_exec = 0;
1672 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1673 return 0;
1674
1675 __perf_event_mark_enabled(event, ctx);
1676
1677 return 1;
1678}
1679
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001680/*
1681 * Enable all of a task's events that have been marked enable-on-exec.
1682 * This expects task == current.
1683 */
1684static void perf_event_enable_on_exec(struct task_struct *task)
1685{
1686 struct perf_event_context *ctx;
1687 struct perf_event *event;
1688 unsigned long flags;
1689 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001690 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001691
1692 local_irq_save(flags);
1693 ctx = task->perf_event_ctxp;
1694 if (!ctx || !ctx->nr_events)
1695 goto out;
1696
1697 __perf_event_task_sched_out(ctx);
1698
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001699 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001700
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001701 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1702 ret = event_enable_on_exec(event, ctx);
1703 if (ret)
1704 enabled = 1;
1705 }
1706
1707 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1708 ret = event_enable_on_exec(event, ctx);
1709 if (ret)
1710 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001711 }
1712
1713 /*
1714 * Unclone this context if we enabled any event.
1715 */
1716 if (enabled)
1717 unclone_ctx(ctx);
1718
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001719 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001720
Peter Zijlstra49f47432009-12-27 11:51:52 +01001721 perf_event_task_sched_in(task);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001722out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001723 local_irq_restore(flags);
1724}
1725
1726/*
1727 * Cross CPU call to read the hardware event
1728 */
1729static void __perf_event_read(void *info)
1730{
1731 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
1732 struct perf_event *event = info;
1733 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001734
1735 /*
1736 * If this is a task context, we need to check whether it is
1737 * the current task context of this cpu. If not it has been
1738 * scheduled out before the smp call arrived. In that case
1739 * event->count would have been updated to a recent sample
1740 * when the event was scheduled out.
1741 */
1742 if (ctx->task && cpuctx->task_ctx != ctx)
1743 return;
1744
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001745 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001746 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001747 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001748 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001749
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001750 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001751}
1752
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001753static inline u64 perf_event_count(struct perf_event *event)
1754{
Peter Zijlstrae7850592010-05-21 14:43:08 +02001755 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001756}
1757
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001758static u64 perf_event_read(struct perf_event *event)
1759{
1760 /*
1761 * If event is enabled and currently active on a CPU, update the
1762 * value in the event structure:
1763 */
1764 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1765 smp_call_function_single(event->oncpu,
1766 __perf_event_read, event, 1);
1767 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001768 struct perf_event_context *ctx = event->ctx;
1769 unsigned long flags;
1770
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001771 raw_spin_lock_irqsave(&ctx->lock, flags);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001772 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001773 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001774 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001775 }
1776
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001777 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001778}
1779
1780/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001781 * Callchain support
1782 */
1783
1784struct callchain_cpus_entries {
1785 struct rcu_head rcu_head;
1786 struct perf_callchain_entry *cpu_entries[0];
1787};
1788
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001789static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001790static atomic_t nr_callchain_events;
1791static DEFINE_MUTEX(callchain_mutex);
1792struct callchain_cpus_entries *callchain_cpus_entries;
1793
1794
1795__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1796 struct pt_regs *regs)
1797{
1798}
1799
1800__weak void perf_callchain_user(struct perf_callchain_entry *entry,
1801 struct pt_regs *regs)
1802{
1803}
1804
1805static void release_callchain_buffers_rcu(struct rcu_head *head)
1806{
1807 struct callchain_cpus_entries *entries;
1808 int cpu;
1809
1810 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1811
1812 for_each_possible_cpu(cpu)
1813 kfree(entries->cpu_entries[cpu]);
1814
1815 kfree(entries);
1816}
1817
1818static void release_callchain_buffers(void)
1819{
1820 struct callchain_cpus_entries *entries;
1821
1822 entries = callchain_cpus_entries;
1823 rcu_assign_pointer(callchain_cpus_entries, NULL);
1824 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1825}
1826
1827static int alloc_callchain_buffers(void)
1828{
1829 int cpu;
1830 int size;
1831 struct callchain_cpus_entries *entries;
1832
1833 /*
1834 * We can't use the percpu allocation API for data that can be
1835 * accessed from NMI. Use a temporary manual per cpu allocation
1836 * until that gets sorted out.
1837 */
1838 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1839 num_possible_cpus();
1840
1841 entries = kzalloc(size, GFP_KERNEL);
1842 if (!entries)
1843 return -ENOMEM;
1844
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001845 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001846
1847 for_each_possible_cpu(cpu) {
1848 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1849 cpu_to_node(cpu));
1850 if (!entries->cpu_entries[cpu])
1851 goto fail;
1852 }
1853
1854 rcu_assign_pointer(callchain_cpus_entries, entries);
1855
1856 return 0;
1857
1858fail:
1859 for_each_possible_cpu(cpu)
1860 kfree(entries->cpu_entries[cpu]);
1861 kfree(entries);
1862
1863 return -ENOMEM;
1864}
1865
1866static int get_callchain_buffers(void)
1867{
1868 int err = 0;
1869 int count;
1870
1871 mutex_lock(&callchain_mutex);
1872
1873 count = atomic_inc_return(&nr_callchain_events);
1874 if (WARN_ON_ONCE(count < 1)) {
1875 err = -EINVAL;
1876 goto exit;
1877 }
1878
1879 if (count > 1) {
1880 /* If the allocation failed, give up */
1881 if (!callchain_cpus_entries)
1882 err = -ENOMEM;
1883 goto exit;
1884 }
1885
1886 err = alloc_callchain_buffers();
1887 if (err)
1888 release_callchain_buffers();
1889exit:
1890 mutex_unlock(&callchain_mutex);
1891
1892 return err;
1893}
1894
1895static void put_callchain_buffers(void)
1896{
1897 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1898 release_callchain_buffers();
1899 mutex_unlock(&callchain_mutex);
1900 }
1901}
1902
1903static int get_recursion_context(int *recursion)
1904{
1905 int rctx;
1906
1907 if (in_nmi())
1908 rctx = 3;
1909 else if (in_irq())
1910 rctx = 2;
1911 else if (in_softirq())
1912 rctx = 1;
1913 else
1914 rctx = 0;
1915
1916 if (recursion[rctx])
1917 return -1;
1918
1919 recursion[rctx]++;
1920 barrier();
1921
1922 return rctx;
1923}
1924
1925static inline void put_recursion_context(int *recursion, int rctx)
1926{
1927 barrier();
1928 recursion[rctx]--;
1929}
1930
1931static struct perf_callchain_entry *get_callchain_entry(int *rctx)
1932{
1933 int cpu;
1934 struct callchain_cpus_entries *entries;
1935
1936 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
1937 if (*rctx == -1)
1938 return NULL;
1939
1940 entries = rcu_dereference(callchain_cpus_entries);
1941 if (!entries)
1942 return NULL;
1943
1944 cpu = smp_processor_id();
1945
1946 return &entries->cpu_entries[cpu][*rctx];
1947}
1948
1949static void
1950put_callchain_entry(int rctx)
1951{
1952 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
1953}
1954
1955static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
1956{
1957 int rctx;
1958 struct perf_callchain_entry *entry;
1959
1960
1961 entry = get_callchain_entry(&rctx);
1962 if (rctx == -1)
1963 return NULL;
1964
1965 if (!entry)
1966 goto exit_put;
1967
1968 entry->nr = 0;
1969
1970 if (!user_mode(regs)) {
1971 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
1972 perf_callchain_kernel(entry, regs);
1973 if (current->mm)
1974 regs = task_pt_regs(current);
1975 else
1976 regs = NULL;
1977 }
1978
1979 if (regs) {
1980 perf_callchain_store(entry, PERF_CONTEXT_USER);
1981 perf_callchain_user(entry, regs);
1982 }
1983
1984exit_put:
1985 put_callchain_entry(rctx);
1986
1987 return entry;
1988}
1989
1990/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001991 * Initialize the perf_event context in a task_struct:
1992 */
1993static void
1994__perf_event_init_context(struct perf_event_context *ctx,
1995 struct task_struct *task)
1996{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001997 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001998 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001999 INIT_LIST_HEAD(&ctx->pinned_groups);
2000 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002001 INIT_LIST_HEAD(&ctx->event_list);
2002 atomic_set(&ctx->refcount, 1);
2003 ctx->task = task;
2004}
2005
2006static struct perf_event_context *find_get_context(pid_t pid, int cpu)
2007{
2008 struct perf_event_context *ctx;
2009 struct perf_cpu_context *cpuctx;
2010 struct task_struct *task;
2011 unsigned long flags;
2012 int err;
2013
Peter Zijlstraf4c41762009-12-16 17:55:54 +01002014 if (pid == -1 && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002015 /* Must be root to operate on a CPU event: */
2016 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2017 return ERR_PTR(-EACCES);
2018
Paul Mackerras0f624e72009-12-15 19:40:32 +11002019 if (cpu < 0 || cpu >= nr_cpumask_bits)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002020 return ERR_PTR(-EINVAL);
2021
2022 /*
2023 * We could be clever and allow to attach a event to an
2024 * offline CPU and activate it when the CPU comes up, but
2025 * that's for later.
2026 */
Rusty Russellf6325e32009-12-17 11:43:08 -06002027 if (!cpu_online(cpu))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002028 return ERR_PTR(-ENODEV);
2029
2030 cpuctx = &per_cpu(perf_cpu_context, cpu);
2031 ctx = &cpuctx->ctx;
2032 get_ctx(ctx);
2033
2034 return ctx;
2035 }
2036
2037 rcu_read_lock();
2038 if (!pid)
2039 task = current;
2040 else
2041 task = find_task_by_vpid(pid);
2042 if (task)
2043 get_task_struct(task);
2044 rcu_read_unlock();
2045
2046 if (!task)
2047 return ERR_PTR(-ESRCH);
2048
2049 /*
2050 * Can't attach events to a dying task.
2051 */
2052 err = -ESRCH;
2053 if (task->flags & PF_EXITING)
2054 goto errout;
2055
2056 /* Reuse ptrace permission checks for now. */
2057 err = -EACCES;
2058 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2059 goto errout;
2060
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002061retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002062 ctx = perf_lock_task_context(task, &flags);
2063 if (ctx) {
2064 unclone_ctx(ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002065 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002066 }
2067
2068 if (!ctx) {
Xiao Guangrongaa5452d2009-12-09 11:28:13 +08002069 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002070 err = -ENOMEM;
2071 if (!ctx)
2072 goto errout;
2073 __perf_event_init_context(ctx, task);
2074 get_ctx(ctx);
2075 if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
2076 /*
2077 * We raced with some other task; use
2078 * the context they set.
2079 */
2080 kfree(ctx);
2081 goto retry;
2082 }
2083 get_task_struct(task);
2084 }
2085
2086 put_task_struct(task);
2087 return ctx;
2088
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002089errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002090 put_task_struct(task);
2091 return ERR_PTR(err);
2092}
2093
Li Zefan6fb29152009-10-15 11:21:42 +08002094static void perf_event_free_filter(struct perf_event *event);
2095
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002096static void free_event_rcu(struct rcu_head *head)
2097{
2098 struct perf_event *event;
2099
2100 event = container_of(head, struct perf_event, rcu_head);
2101 if (event->ns)
2102 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002103 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002104 kfree(event);
2105}
2106
2107static void perf_pending_sync(struct perf_event *event);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002108static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002109
2110static void free_event(struct perf_event *event)
2111{
2112 perf_pending_sync(event);
2113
2114 if (!event->parent) {
2115 atomic_dec(&nr_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002116 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002117 atomic_dec(&nr_mmap_events);
2118 if (event->attr.comm)
2119 atomic_dec(&nr_comm_events);
2120 if (event->attr.task)
2121 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002122 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2123 put_callchain_buffers();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002124 }
2125
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002126 if (event->buffer) {
2127 perf_buffer_put(event->buffer);
2128 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002129 }
2130
2131 if (event->destroy)
2132 event->destroy(event);
2133
2134 put_ctx(event->ctx);
2135 call_rcu(&event->rcu_head, free_event_rcu);
2136}
2137
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002138int perf_event_release_kernel(struct perf_event *event)
2139{
2140 struct perf_event_context *ctx = event->ctx;
2141
Peter Zijlstra050735b2010-05-11 11:51:53 +02002142 /*
2143 * Remove from the PMU, can't get re-enabled since we got
2144 * here because the last ref went.
2145 */
2146 perf_event_disable(event);
2147
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002148 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002149 /*
2150 * There are two ways this annotation is useful:
2151 *
2152 * 1) there is a lock recursion from perf_event_exit_task
2153 * see the comment there.
2154 *
2155 * 2) there is a lock-inversion with mmap_sem through
2156 * perf_event_read_group(), which takes faults while
2157 * holding ctx->mutex, however this is called after
2158 * the last filedesc died, so there is no possibility
2159 * to trigger the AB-BA case.
2160 */
2161 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002162 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002163 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002164 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002165 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002166 mutex_unlock(&ctx->mutex);
2167
2168 mutex_lock(&event->owner->perf_event_mutex);
2169 list_del_init(&event->owner_entry);
2170 mutex_unlock(&event->owner->perf_event_mutex);
2171 put_task_struct(event->owner);
2172
2173 free_event(event);
2174
2175 return 0;
2176}
2177EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2178
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002179/*
2180 * Called when the last reference to the file is gone.
2181 */
2182static int perf_release(struct inode *inode, struct file *file)
2183{
2184 struct perf_event *event = file->private_data;
2185
2186 file->private_data = NULL;
2187
2188 return perf_event_release_kernel(event);
2189}
2190
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002191static int perf_event_read_size(struct perf_event *event)
2192{
2193 int entry = sizeof(u64); /* value */
2194 int size = 0;
2195 int nr = 1;
2196
2197 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2198 size += sizeof(u64);
2199
2200 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2201 size += sizeof(u64);
2202
2203 if (event->attr.read_format & PERF_FORMAT_ID)
2204 entry += sizeof(u64);
2205
2206 if (event->attr.read_format & PERF_FORMAT_GROUP) {
2207 nr += event->group_leader->nr_siblings;
2208 size += sizeof(u64);
2209 }
2210
2211 size += entry * nr;
2212
2213 return size;
2214}
2215
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002216u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002217{
2218 struct perf_event *child;
2219 u64 total = 0;
2220
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002221 *enabled = 0;
2222 *running = 0;
2223
Peter Zijlstra6f105812009-11-20 22:19:56 +01002224 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002225 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002226 *enabled += event->total_time_enabled +
2227 atomic64_read(&event->child_total_time_enabled);
2228 *running += event->total_time_running +
2229 atomic64_read(&event->child_total_time_running);
2230
2231 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002232 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002233 *enabled += child->total_time_enabled;
2234 *running += child->total_time_running;
2235 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002236 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002237
2238 return total;
2239}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002240EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002241
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002242static int perf_event_read_group(struct perf_event *event,
2243 u64 read_format, char __user *buf)
2244{
2245 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01002246 int n = 0, size = 0, ret = -EFAULT;
2247 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002248 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002249 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002250
Peter Zijlstra6f105812009-11-20 22:19:56 +01002251 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002252 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002253
2254 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002255 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2256 values[n++] = enabled;
2257 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2258 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002259 values[n++] = count;
2260 if (read_format & PERF_FORMAT_ID)
2261 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002262
2263 size = n * sizeof(u64);
2264
2265 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01002266 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002267
Peter Zijlstra6f105812009-11-20 22:19:56 +01002268 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002269
2270 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01002271 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002272
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002273 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01002274 if (read_format & PERF_FORMAT_ID)
2275 values[n++] = primary_event_id(sub);
2276
2277 size = n * sizeof(u64);
2278
Stephane Eranian184d3da2009-11-23 21:40:49 -08002279 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002280 ret = -EFAULT;
2281 goto unlock;
2282 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002283
2284 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002285 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002286unlock:
2287 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002288
Peter Zijlstraabf48682009-11-20 22:19:49 +01002289 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002290}
2291
2292static int perf_event_read_one(struct perf_event *event,
2293 u64 read_format, char __user *buf)
2294{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002295 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002296 u64 values[4];
2297 int n = 0;
2298
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002299 values[n++] = perf_event_read_value(event, &enabled, &running);
2300 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2301 values[n++] = enabled;
2302 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2303 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002304 if (read_format & PERF_FORMAT_ID)
2305 values[n++] = primary_event_id(event);
2306
2307 if (copy_to_user(buf, values, n * sizeof(u64)))
2308 return -EFAULT;
2309
2310 return n * sizeof(u64);
2311}
2312
2313/*
2314 * Read the performance event - simple non blocking version for now
2315 */
2316static ssize_t
2317perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2318{
2319 u64 read_format = event->attr.read_format;
2320 int ret;
2321
2322 /*
2323 * Return end-of-file for a read on a event that is in
2324 * error state (i.e. because it was pinned but it couldn't be
2325 * scheduled on to the CPU at some point).
2326 */
2327 if (event->state == PERF_EVENT_STATE_ERROR)
2328 return 0;
2329
2330 if (count < perf_event_read_size(event))
2331 return -ENOSPC;
2332
2333 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002334 if (read_format & PERF_FORMAT_GROUP)
2335 ret = perf_event_read_group(event, read_format, buf);
2336 else
2337 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002338
2339 return ret;
2340}
2341
2342static ssize_t
2343perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2344{
2345 struct perf_event *event = file->private_data;
2346
2347 return perf_read_hw(event, buf, count);
2348}
2349
2350static unsigned int perf_poll(struct file *file, poll_table *wait)
2351{
2352 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002353 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002354 unsigned int events = POLL_HUP;
2355
2356 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002357 buffer = rcu_dereference(event->buffer);
2358 if (buffer)
2359 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002360 rcu_read_unlock();
2361
2362 poll_wait(file, &event->waitq, wait);
2363
2364 return events;
2365}
2366
2367static void perf_event_reset(struct perf_event *event)
2368{
2369 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002370 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002371 perf_event_update_userpage(event);
2372}
2373
2374/*
2375 * Holding the top-level event's child_mutex means that any
2376 * descendant process that has inherited this event will block
2377 * in sync_child_event if it goes to exit, thus satisfying the
2378 * task existence requirements of perf_event_enable/disable.
2379 */
2380static void perf_event_for_each_child(struct perf_event *event,
2381 void (*func)(struct perf_event *))
2382{
2383 struct perf_event *child;
2384
2385 WARN_ON_ONCE(event->ctx->parent_ctx);
2386 mutex_lock(&event->child_mutex);
2387 func(event);
2388 list_for_each_entry(child, &event->child_list, child_list)
2389 func(child);
2390 mutex_unlock(&event->child_mutex);
2391}
2392
2393static void perf_event_for_each(struct perf_event *event,
2394 void (*func)(struct perf_event *))
2395{
2396 struct perf_event_context *ctx = event->ctx;
2397 struct perf_event *sibling;
2398
2399 WARN_ON_ONCE(ctx->parent_ctx);
2400 mutex_lock(&ctx->mutex);
2401 event = event->group_leader;
2402
2403 perf_event_for_each_child(event, func);
2404 func(event);
2405 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2406 perf_event_for_each_child(event, func);
2407 mutex_unlock(&ctx->mutex);
2408}
2409
2410static int perf_event_period(struct perf_event *event, u64 __user *arg)
2411{
2412 struct perf_event_context *ctx = event->ctx;
2413 unsigned long size;
2414 int ret = 0;
2415 u64 value;
2416
2417 if (!event->attr.sample_period)
2418 return -EINVAL;
2419
2420 size = copy_from_user(&value, arg, sizeof(value));
2421 if (size != sizeof(value))
2422 return -EFAULT;
2423
2424 if (!value)
2425 return -EINVAL;
2426
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002427 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002428 if (event->attr.freq) {
2429 if (value > sysctl_perf_event_sample_rate) {
2430 ret = -EINVAL;
2431 goto unlock;
2432 }
2433
2434 event->attr.sample_freq = value;
2435 } else {
2436 event->attr.sample_period = value;
2437 event->hw.sample_period = value;
2438 }
2439unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002440 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002441
2442 return ret;
2443}
2444
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002445static const struct file_operations perf_fops;
2446
2447static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2448{
2449 struct file *file;
2450
2451 file = fget_light(fd, fput_needed);
2452 if (!file)
2453 return ERR_PTR(-EBADF);
2454
2455 if (file->f_op != &perf_fops) {
2456 fput_light(file, *fput_needed);
2457 *fput_needed = 0;
2458 return ERR_PTR(-EBADF);
2459 }
2460
2461 return file->private_data;
2462}
2463
2464static int perf_event_set_output(struct perf_event *event,
2465 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08002466static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002467
2468static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2469{
2470 struct perf_event *event = file->private_data;
2471 void (*func)(struct perf_event *);
2472 u32 flags = arg;
2473
2474 switch (cmd) {
2475 case PERF_EVENT_IOC_ENABLE:
2476 func = perf_event_enable;
2477 break;
2478 case PERF_EVENT_IOC_DISABLE:
2479 func = perf_event_disable;
2480 break;
2481 case PERF_EVENT_IOC_RESET:
2482 func = perf_event_reset;
2483 break;
2484
2485 case PERF_EVENT_IOC_REFRESH:
2486 return perf_event_refresh(event, arg);
2487
2488 case PERF_EVENT_IOC_PERIOD:
2489 return perf_event_period(event, (u64 __user *)arg);
2490
2491 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002492 {
2493 struct perf_event *output_event = NULL;
2494 int fput_needed = 0;
2495 int ret;
2496
2497 if (arg != -1) {
2498 output_event = perf_fget_light(arg, &fput_needed);
2499 if (IS_ERR(output_event))
2500 return PTR_ERR(output_event);
2501 }
2502
2503 ret = perf_event_set_output(event, output_event);
2504 if (output_event)
2505 fput_light(output_event->filp, fput_needed);
2506
2507 return ret;
2508 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002509
Li Zefan6fb29152009-10-15 11:21:42 +08002510 case PERF_EVENT_IOC_SET_FILTER:
2511 return perf_event_set_filter(event, (void __user *)arg);
2512
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002513 default:
2514 return -ENOTTY;
2515 }
2516
2517 if (flags & PERF_IOC_FLAG_GROUP)
2518 perf_event_for_each(event, func);
2519 else
2520 perf_event_for_each_child(event, func);
2521
2522 return 0;
2523}
2524
2525int perf_event_task_enable(void)
2526{
2527 struct perf_event *event;
2528
2529 mutex_lock(&current->perf_event_mutex);
2530 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2531 perf_event_for_each_child(event, perf_event_enable);
2532 mutex_unlock(&current->perf_event_mutex);
2533
2534 return 0;
2535}
2536
2537int perf_event_task_disable(void)
2538{
2539 struct perf_event *event;
2540
2541 mutex_lock(&current->perf_event_mutex);
2542 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2543 perf_event_for_each_child(event, perf_event_disable);
2544 mutex_unlock(&current->perf_event_mutex);
2545
2546 return 0;
2547}
2548
2549#ifndef PERF_EVENT_INDEX_OFFSET
2550# define PERF_EVENT_INDEX_OFFSET 0
2551#endif
2552
2553static int perf_event_index(struct perf_event *event)
2554{
2555 if (event->state != PERF_EVENT_STATE_ACTIVE)
2556 return 0;
2557
2558 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2559}
2560
2561/*
2562 * Callers need to ensure there can be no nesting of this function, otherwise
2563 * the seqlock logic goes bad. We can not serialize this because the arch
2564 * code calls this from NMI context.
2565 */
2566void perf_event_update_userpage(struct perf_event *event)
2567{
2568 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002569 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002570
2571 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002572 buffer = rcu_dereference(event->buffer);
2573 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002574 goto unlock;
2575
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002576 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002577
2578 /*
2579 * Disable preemption so as to not let the corresponding user-space
2580 * spin too long if we get preempted.
2581 */
2582 preempt_disable();
2583 ++userpg->lock;
2584 barrier();
2585 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002586 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002587 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02002588 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002589
2590 userpg->time_enabled = event->total_time_enabled +
2591 atomic64_read(&event->child_total_time_enabled);
2592
2593 userpg->time_running = event->total_time_running +
2594 atomic64_read(&event->child_total_time_running);
2595
2596 barrier();
2597 ++userpg->lock;
2598 preempt_enable();
2599unlock:
2600 rcu_read_unlock();
2601}
2602
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002603static unsigned long perf_data_size(struct perf_buffer *buffer);
2604
2605static void
2606perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2607{
2608 long max_size = perf_data_size(buffer);
2609
2610 if (watermark)
2611 buffer->watermark = min(max_size, watermark);
2612
2613 if (!buffer->watermark)
2614 buffer->watermark = max_size / 2;
2615
2616 if (flags & PERF_BUFFER_WRITABLE)
2617 buffer->writable = 1;
2618
2619 atomic_set(&buffer->refcount, 1);
2620}
2621
Peter Zijlstra906010b2009-09-21 16:08:49 +02002622#ifndef CONFIG_PERF_USE_VMALLOC
2623
2624/*
2625 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2626 */
2627
2628static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002629perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002630{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002631 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002632 return NULL;
2633
2634 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002635 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002636
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002637 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002638}
2639
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02002640static void *perf_mmap_alloc_page(int cpu)
2641{
2642 struct page *page;
2643 int node;
2644
2645 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2646 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2647 if (!page)
2648 return NULL;
2649
2650 return page_address(page);
2651}
2652
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002653static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002654perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002655{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002656 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002657 unsigned long size;
2658 int i;
2659
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002660 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002661 size += nr_pages * sizeof(void *);
2662
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002663 buffer = kzalloc(size, GFP_KERNEL);
2664 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002665 goto fail;
2666
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002667 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002668 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002669 goto fail_user_page;
2670
2671 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002672 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002673 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002674 goto fail_data_pages;
2675 }
2676
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002677 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002678
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002679 perf_buffer_init(buffer, watermark, flags);
2680
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002681 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002682
2683fail_data_pages:
2684 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002685 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002686
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002687 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002688
2689fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002690 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002691
2692fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002693 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002694}
2695
2696static void perf_mmap_free_page(unsigned long addr)
2697{
2698 struct page *page = virt_to_page((void *)addr);
2699
2700 page->mapping = NULL;
2701 __free_page(page);
2702}
2703
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002704static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002705{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002706 int i;
2707
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002708 perf_mmap_free_page((unsigned long)buffer->user_page);
2709 for (i = 0; i < buffer->nr_pages; i++)
2710 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2711 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002712}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002713
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002714static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002715{
2716 return 0;
2717}
2718
Peter Zijlstra906010b2009-09-21 16:08:49 +02002719#else
2720
2721/*
2722 * Back perf_mmap() with vmalloc memory.
2723 *
2724 * Required for architectures that have d-cache aliasing issues.
2725 */
2726
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002727static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002728{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002729 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002730}
2731
Peter Zijlstra906010b2009-09-21 16:08:49 +02002732static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002733perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002734{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002735 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02002736 return NULL;
2737
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002738 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002739}
2740
2741static void perf_mmap_unmark_page(void *addr)
2742{
2743 struct page *page = vmalloc_to_page(addr);
2744
2745 page->mapping = NULL;
2746}
2747
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002748static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002749{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002750 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002751 void *base;
2752 int i, nr;
2753
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002754 buffer = container_of(work, struct perf_buffer, work);
2755 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002756
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002757 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002758 for (i = 0; i < nr + 1; i++)
2759 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2760
2761 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002762 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002763}
2764
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002765static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002766{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002767 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002768}
2769
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002770static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002771perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002772{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002773 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002774 unsigned long size;
2775 void *all_buf;
2776
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002777 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002778 size += sizeof(void *);
2779
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002780 buffer = kzalloc(size, GFP_KERNEL);
2781 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002782 goto fail;
2783
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002784 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002785
2786 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2787 if (!all_buf)
2788 goto fail_all_buf;
2789
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002790 buffer->user_page = all_buf;
2791 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2792 buffer->page_order = ilog2(nr_pages);
2793 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002794
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002795 perf_buffer_init(buffer, watermark, flags);
2796
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002797 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002798
2799fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002800 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002801
2802fail:
2803 return NULL;
2804}
2805
2806#endif
2807
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002808static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002809{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002810 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002811}
2812
Peter Zijlstra906010b2009-09-21 16:08:49 +02002813static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2814{
2815 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002816 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002817 int ret = VM_FAULT_SIGBUS;
2818
2819 if (vmf->flags & FAULT_FLAG_MKWRITE) {
2820 if (vmf->pgoff == 0)
2821 ret = 0;
2822 return ret;
2823 }
2824
2825 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002826 buffer = rcu_dereference(event->buffer);
2827 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002828 goto unlock;
2829
2830 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
2831 goto unlock;
2832
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002833 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002834 if (!vmf->page)
2835 goto unlock;
2836
2837 get_page(vmf->page);
2838 vmf->page->mapping = vma->vm_file->f_mapping;
2839 vmf->page->index = vmf->pgoff;
2840
2841 ret = 0;
2842unlock:
2843 rcu_read_unlock();
2844
2845 return ret;
2846}
2847
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002848static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002849{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002850 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002851
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002852 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
2853 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002854}
2855
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002856static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002857{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002858 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002859
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002860 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002861 buffer = rcu_dereference(event->buffer);
2862 if (buffer) {
2863 if (!atomic_inc_not_zero(&buffer->refcount))
2864 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002865 }
2866 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002867
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002868 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002869}
2870
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002871static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002872{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002873 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002874 return;
2875
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002876 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002877}
2878
2879static void perf_mmap_open(struct vm_area_struct *vma)
2880{
2881 struct perf_event *event = vma->vm_file->private_data;
2882
2883 atomic_inc(&event->mmap_count);
2884}
2885
2886static void perf_mmap_close(struct vm_area_struct *vma)
2887{
2888 struct perf_event *event = vma->vm_file->private_data;
2889
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002890 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002891 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002892 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002893 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002894
Peter Zijlstra906010b2009-09-21 16:08:49 +02002895 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002896 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002897 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002898 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002899
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002900 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002901 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002902 }
2903}
2904
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04002905static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002906 .open = perf_mmap_open,
2907 .close = perf_mmap_close,
2908 .fault = perf_mmap_fault,
2909 .page_mkwrite = perf_mmap_fault,
2910};
2911
2912static int perf_mmap(struct file *file, struct vm_area_struct *vma)
2913{
2914 struct perf_event *event = file->private_data;
2915 unsigned long user_locked, user_lock_limit;
2916 struct user_struct *user = current_user();
2917 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002918 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002919 unsigned long vma_size;
2920 unsigned long nr_pages;
2921 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002922 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002923
Peter Zijlstrac7920612010-05-18 10:33:24 +02002924 /*
2925 * Don't allow mmap() of inherited per-task counters. This would
2926 * create a performance issue due to all children writing to the
2927 * same buffer.
2928 */
2929 if (event->cpu == -1 && event->attr.inherit)
2930 return -EINVAL;
2931
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002932 if (!(vma->vm_flags & VM_SHARED))
2933 return -EINVAL;
2934
2935 vma_size = vma->vm_end - vma->vm_start;
2936 nr_pages = (vma_size / PAGE_SIZE) - 1;
2937
2938 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002939 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002940 * can do bitmasks instead of modulo.
2941 */
2942 if (nr_pages != 0 && !is_power_of_2(nr_pages))
2943 return -EINVAL;
2944
2945 if (vma_size != PAGE_SIZE * (1 + nr_pages))
2946 return -EINVAL;
2947
2948 if (vma->vm_pgoff != 0)
2949 return -EINVAL;
2950
2951 WARN_ON_ONCE(event->ctx->parent_ctx);
2952 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002953 if (event->buffer) {
2954 if (event->buffer->nr_pages == nr_pages)
2955 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002956 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002957 ret = -EINVAL;
2958 goto unlock;
2959 }
2960
2961 user_extra = nr_pages + 1;
2962 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
2963
2964 /*
2965 * Increase the limit linearly with more CPUs:
2966 */
2967 user_lock_limit *= num_online_cpus();
2968
2969 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
2970
2971 extra = 0;
2972 if (user_locked > user_lock_limit)
2973 extra = user_locked - user_lock_limit;
2974
Jiri Slaby78d7d402010-03-05 13:42:54 -08002975 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002976 lock_limit >>= PAGE_SHIFT;
2977 locked = vma->vm_mm->locked_vm + extra;
2978
2979 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
2980 !capable(CAP_IPC_LOCK)) {
2981 ret = -EPERM;
2982 goto unlock;
2983 }
2984
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002985 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002986
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002987 if (vma->vm_flags & VM_WRITE)
2988 flags |= PERF_BUFFER_WRITABLE;
2989
2990 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
2991 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002992 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002993 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002994 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002995 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002996 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002997
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002998 atomic_long_add(user_extra, &user->locked_vm);
2999 event->mmap_locked = extra;
3000 event->mmap_user = get_current_user();
3001 vma->vm_mm->locked_vm += event->mmap_locked;
3002
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003003unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003004 if (!ret)
3005 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003006 mutex_unlock(&event->mmap_mutex);
3007
3008 vma->vm_flags |= VM_RESERVED;
3009 vma->vm_ops = &perf_mmap_vmops;
3010
3011 return ret;
3012}
3013
3014static int perf_fasync(int fd, struct file *filp, int on)
3015{
3016 struct inode *inode = filp->f_path.dentry->d_inode;
3017 struct perf_event *event = filp->private_data;
3018 int retval;
3019
3020 mutex_lock(&inode->i_mutex);
3021 retval = fasync_helper(fd, filp, on, &event->fasync);
3022 mutex_unlock(&inode->i_mutex);
3023
3024 if (retval < 0)
3025 return retval;
3026
3027 return 0;
3028}
3029
3030static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003031 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003032 .release = perf_release,
3033 .read = perf_read,
3034 .poll = perf_poll,
3035 .unlocked_ioctl = perf_ioctl,
3036 .compat_ioctl = perf_ioctl,
3037 .mmap = perf_mmap,
3038 .fasync = perf_fasync,
3039};
3040
3041/*
3042 * Perf event wakeup
3043 *
3044 * If there's data, ensure we set the poll() state and publish everything
3045 * to user-space before waking everybody up.
3046 */
3047
3048void perf_event_wakeup(struct perf_event *event)
3049{
3050 wake_up_all(&event->waitq);
3051
3052 if (event->pending_kill) {
3053 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3054 event->pending_kill = 0;
3055 }
3056}
3057
3058/*
3059 * Pending wakeups
3060 *
3061 * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
3062 *
3063 * The NMI bit means we cannot possibly take locks. Therefore, maintain a
3064 * single linked list and use cmpxchg() to add entries lockless.
3065 */
3066
3067static void perf_pending_event(struct perf_pending_entry *entry)
3068{
3069 struct perf_event *event = container_of(entry,
3070 struct perf_event, pending);
3071
3072 if (event->pending_disable) {
3073 event->pending_disable = 0;
3074 __perf_event_disable(event);
3075 }
3076
3077 if (event->pending_wakeup) {
3078 event->pending_wakeup = 0;
3079 perf_event_wakeup(event);
3080 }
3081}
3082
3083#define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
3084
3085static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
3086 PENDING_TAIL,
3087};
3088
3089static void perf_pending_queue(struct perf_pending_entry *entry,
3090 void (*func)(struct perf_pending_entry *))
3091{
3092 struct perf_pending_entry **head;
3093
3094 if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
3095 return;
3096
3097 entry->func = func;
3098
3099 head = &get_cpu_var(perf_pending_head);
3100
3101 do {
3102 entry->next = *head;
3103 } while (cmpxchg(head, entry->next, entry) != entry->next);
3104
3105 set_perf_event_pending();
3106
3107 put_cpu_var(perf_pending_head);
3108}
3109
3110static int __perf_pending_run(void)
3111{
3112 struct perf_pending_entry *list;
3113 int nr = 0;
3114
3115 list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
3116 while (list != PENDING_TAIL) {
3117 void (*func)(struct perf_pending_entry *);
3118 struct perf_pending_entry *entry = list;
3119
3120 list = list->next;
3121
3122 func = entry->func;
3123 entry->next = NULL;
3124 /*
3125 * Ensure we observe the unqueue before we issue the wakeup,
3126 * so that we won't be waiting forever.
3127 * -- see perf_not_pending().
3128 */
3129 smp_wmb();
3130
3131 func(entry);
3132 nr++;
3133 }
3134
3135 return nr;
3136}
3137
3138static inline int perf_not_pending(struct perf_event *event)
3139{
3140 /*
3141 * If we flush on whatever cpu we run, there is a chance we don't
3142 * need to wait.
3143 */
3144 get_cpu();
3145 __perf_pending_run();
3146 put_cpu();
3147
3148 /*
3149 * Ensure we see the proper queue state before going to sleep
3150 * so that we do not miss the wakeup. -- see perf_pending_handle()
3151 */
3152 smp_rmb();
3153 return event->pending.next == NULL;
3154}
3155
3156static void perf_pending_sync(struct perf_event *event)
3157{
3158 wait_event(event->waitq, perf_not_pending(event));
3159}
3160
3161void perf_event_do_pending(void)
3162{
3163 __perf_pending_run();
3164}
3165
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003166/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003167 * We assume there is only KVM supporting the callbacks.
3168 * Later on, we might change it to a list if there is
3169 * another virtualization implementation supporting the callbacks.
3170 */
3171struct perf_guest_info_callbacks *perf_guest_cbs;
3172
3173int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3174{
3175 perf_guest_cbs = cbs;
3176 return 0;
3177}
3178EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3179
3180int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3181{
3182 perf_guest_cbs = NULL;
3183 return 0;
3184}
3185EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3186
3187/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003188 * Output
3189 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003190static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003191 unsigned long offset, unsigned long head)
3192{
3193 unsigned long mask;
3194
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003195 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003196 return true;
3197
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003198 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003199
3200 offset = (offset - tail) & mask;
3201 head = (head - tail) & mask;
3202
3203 if ((int)(head - offset) < 0)
3204 return false;
3205
3206 return true;
3207}
3208
3209static void perf_output_wakeup(struct perf_output_handle *handle)
3210{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003211 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003212
3213 if (handle->nmi) {
3214 handle->event->pending_wakeup = 1;
3215 perf_pending_queue(&handle->event->pending,
3216 perf_pending_event);
3217 } else
3218 perf_event_wakeup(handle->event);
3219}
3220
3221/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003222 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003223 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003224 * cannot fully serialize things.
3225 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003226 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003227 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003228 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003229static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003230{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003231 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003232
Peter Zijlstraef607772010-05-18 10:50:41 +02003233 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003234 local_inc(&buffer->nest);
3235 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003236}
3237
Peter Zijlstraef607772010-05-18 10:50:41 +02003238static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003239{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003240 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003241 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003242
3243again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003244 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003245
3246 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003247 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003248 */
3249
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003250 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003251 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003252
3253 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003254 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003255 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003256 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003257 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003258 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003259
Peter Zijlstraef607772010-05-18 10:50:41 +02003260 /*
3261 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003262 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003263 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003264 if (unlikely(head != local_read(&buffer->head))) {
3265 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003266 goto again;
3267 }
3268
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003269 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003270 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003271
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003272out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003273 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003274}
3275
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003276__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003277 const void *buf, unsigned int len)
3278{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003279 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003280 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003281
3282 memcpy(handle->addr, buf, size);
3283
3284 len -= size;
3285 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003286 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003287 handle->size -= size;
3288 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003289 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003290
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003291 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003292 handle->page &= buffer->nr_pages - 1;
3293 handle->addr = buffer->data_pages[handle->page];
3294 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003295 }
3296 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003297}
3298
3299int perf_output_begin(struct perf_output_handle *handle,
3300 struct perf_event *event, unsigned int size,
3301 int nmi, int sample)
3302{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003303 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003304 unsigned long tail, offset, head;
3305 int have_lost;
3306 struct {
3307 struct perf_event_header header;
3308 u64 id;
3309 u64 lost;
3310 } lost_event;
3311
3312 rcu_read_lock();
3313 /*
3314 * For inherited events we send all the output towards the parent.
3315 */
3316 if (event->parent)
3317 event = event->parent;
3318
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003319 buffer = rcu_dereference(event->buffer);
3320 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003321 goto out;
3322
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003323 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003324 handle->event = event;
3325 handle->nmi = nmi;
3326 handle->sample = sample;
3327
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003328 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02003329 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003330
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003331 have_lost = local_read(&buffer->lost);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003332 if (have_lost)
3333 size += sizeof(lost_event);
3334
Peter Zijlstraef607772010-05-18 10:50:41 +02003335 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003336
3337 do {
3338 /*
3339 * Userspace could choose to issue a mb() before updating the
3340 * tail pointer. So that all reads will be completed before the
3341 * write is issued.
3342 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003343 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003344 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003345 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003346 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003347 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003348 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003349 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003350
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003351 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3352 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003353
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003354 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3355 handle->page &= buffer->nr_pages - 1;
3356 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3357 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003358 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003359 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003360
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003361 if (have_lost) {
3362 lost_event.header.type = PERF_RECORD_LOST;
3363 lost_event.header.misc = 0;
3364 lost_event.header.size = sizeof(lost_event);
3365 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003366 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003367
3368 perf_output_put(handle, lost_event);
3369 }
3370
3371 return 0;
3372
3373fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003374 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02003375 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003376out:
3377 rcu_read_unlock();
3378
3379 return -ENOSPC;
3380}
3381
3382void perf_output_end(struct perf_output_handle *handle)
3383{
3384 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003385 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003386
3387 int wakeup_events = event->attr.wakeup_events;
3388
3389 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003390 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003391 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003392 local_sub(wakeup_events, &buffer->events);
3393 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003394 }
3395 }
3396
Peter Zijlstraef607772010-05-18 10:50:41 +02003397 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003398 rcu_read_unlock();
3399}
3400
3401static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
3402{
3403 /*
3404 * only top level events have the pid namespace they were created in
3405 */
3406 if (event->parent)
3407 event = event->parent;
3408
3409 return task_tgid_nr_ns(p, event->ns);
3410}
3411
3412static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
3413{
3414 /*
3415 * only top level events have the pid namespace they were created in
3416 */
3417 if (event->parent)
3418 event = event->parent;
3419
3420 return task_pid_nr_ns(p, event->ns);
3421}
3422
3423static void perf_output_read_one(struct perf_output_handle *handle,
3424 struct perf_event *event)
3425{
3426 u64 read_format = event->attr.read_format;
3427 u64 values[4];
3428 int n = 0;
3429
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003430 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003431 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
3432 values[n++] = event->total_time_enabled +
3433 atomic64_read(&event->child_total_time_enabled);
3434 }
3435 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
3436 values[n++] = event->total_time_running +
3437 atomic64_read(&event->child_total_time_running);
3438 }
3439 if (read_format & PERF_FORMAT_ID)
3440 values[n++] = primary_event_id(event);
3441
3442 perf_output_copy(handle, values, n * sizeof(u64));
3443}
3444
3445/*
3446 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3447 */
3448static void perf_output_read_group(struct perf_output_handle *handle,
3449 struct perf_event *event)
3450{
3451 struct perf_event *leader = event->group_leader, *sub;
3452 u64 read_format = event->attr.read_format;
3453 u64 values[5];
3454 int n = 0;
3455
3456 values[n++] = 1 + leader->nr_siblings;
3457
3458 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
3459 values[n++] = leader->total_time_enabled;
3460
3461 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
3462 values[n++] = leader->total_time_running;
3463
3464 if (leader != event)
3465 leader->pmu->read(leader);
3466
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003467 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003468 if (read_format & PERF_FORMAT_ID)
3469 values[n++] = primary_event_id(leader);
3470
3471 perf_output_copy(handle, values, n * sizeof(u64));
3472
3473 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3474 n = 0;
3475
3476 if (sub != event)
3477 sub->pmu->read(sub);
3478
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003479 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003480 if (read_format & PERF_FORMAT_ID)
3481 values[n++] = primary_event_id(sub);
3482
3483 perf_output_copy(handle, values, n * sizeof(u64));
3484 }
3485}
3486
3487static void perf_output_read(struct perf_output_handle *handle,
3488 struct perf_event *event)
3489{
3490 if (event->attr.read_format & PERF_FORMAT_GROUP)
3491 perf_output_read_group(handle, event);
3492 else
3493 perf_output_read_one(handle, event);
3494}
3495
3496void perf_output_sample(struct perf_output_handle *handle,
3497 struct perf_event_header *header,
3498 struct perf_sample_data *data,
3499 struct perf_event *event)
3500{
3501 u64 sample_type = data->type;
3502
3503 perf_output_put(handle, *header);
3504
3505 if (sample_type & PERF_SAMPLE_IP)
3506 perf_output_put(handle, data->ip);
3507
3508 if (sample_type & PERF_SAMPLE_TID)
3509 perf_output_put(handle, data->tid_entry);
3510
3511 if (sample_type & PERF_SAMPLE_TIME)
3512 perf_output_put(handle, data->time);
3513
3514 if (sample_type & PERF_SAMPLE_ADDR)
3515 perf_output_put(handle, data->addr);
3516
3517 if (sample_type & PERF_SAMPLE_ID)
3518 perf_output_put(handle, data->id);
3519
3520 if (sample_type & PERF_SAMPLE_STREAM_ID)
3521 perf_output_put(handle, data->stream_id);
3522
3523 if (sample_type & PERF_SAMPLE_CPU)
3524 perf_output_put(handle, data->cpu_entry);
3525
3526 if (sample_type & PERF_SAMPLE_PERIOD)
3527 perf_output_put(handle, data->period);
3528
3529 if (sample_type & PERF_SAMPLE_READ)
3530 perf_output_read(handle, event);
3531
3532 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3533 if (data->callchain) {
3534 int size = 1;
3535
3536 if (data->callchain)
3537 size += data->callchain->nr;
3538
3539 size *= sizeof(u64);
3540
3541 perf_output_copy(handle, data->callchain, size);
3542 } else {
3543 u64 nr = 0;
3544 perf_output_put(handle, nr);
3545 }
3546 }
3547
3548 if (sample_type & PERF_SAMPLE_RAW) {
3549 if (data->raw) {
3550 perf_output_put(handle, data->raw->size);
3551 perf_output_copy(handle, data->raw->data,
3552 data->raw->size);
3553 } else {
3554 struct {
3555 u32 size;
3556 u32 data;
3557 } raw = {
3558 .size = sizeof(u32),
3559 .data = 0,
3560 };
3561 perf_output_put(handle, raw);
3562 }
3563 }
3564}
3565
3566void perf_prepare_sample(struct perf_event_header *header,
3567 struct perf_sample_data *data,
3568 struct perf_event *event,
3569 struct pt_regs *regs)
3570{
3571 u64 sample_type = event->attr.sample_type;
3572
3573 data->type = sample_type;
3574
3575 header->type = PERF_RECORD_SAMPLE;
3576 header->size = sizeof(*header);
3577
3578 header->misc = 0;
3579 header->misc |= perf_misc_flags(regs);
3580
3581 if (sample_type & PERF_SAMPLE_IP) {
3582 data->ip = perf_instruction_pointer(regs);
3583
3584 header->size += sizeof(data->ip);
3585 }
3586
3587 if (sample_type & PERF_SAMPLE_TID) {
3588 /* namespace issues */
3589 data->tid_entry.pid = perf_event_pid(event, current);
3590 data->tid_entry.tid = perf_event_tid(event, current);
3591
3592 header->size += sizeof(data->tid_entry);
3593 }
3594
3595 if (sample_type & PERF_SAMPLE_TIME) {
3596 data->time = perf_clock();
3597
3598 header->size += sizeof(data->time);
3599 }
3600
3601 if (sample_type & PERF_SAMPLE_ADDR)
3602 header->size += sizeof(data->addr);
3603
3604 if (sample_type & PERF_SAMPLE_ID) {
3605 data->id = primary_event_id(event);
3606
3607 header->size += sizeof(data->id);
3608 }
3609
3610 if (sample_type & PERF_SAMPLE_STREAM_ID) {
3611 data->stream_id = event->id;
3612
3613 header->size += sizeof(data->stream_id);
3614 }
3615
3616 if (sample_type & PERF_SAMPLE_CPU) {
3617 data->cpu_entry.cpu = raw_smp_processor_id();
3618 data->cpu_entry.reserved = 0;
3619
3620 header->size += sizeof(data->cpu_entry);
3621 }
3622
3623 if (sample_type & PERF_SAMPLE_PERIOD)
3624 header->size += sizeof(data->period);
3625
3626 if (sample_type & PERF_SAMPLE_READ)
3627 header->size += perf_event_read_size(event);
3628
3629 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3630 int size = 1;
3631
3632 data->callchain = perf_callchain(regs);
3633
3634 if (data->callchain)
3635 size += data->callchain->nr;
3636
3637 header->size += size * sizeof(u64);
3638 }
3639
3640 if (sample_type & PERF_SAMPLE_RAW) {
3641 int size = sizeof(u32);
3642
3643 if (data->raw)
3644 size += data->raw->size;
3645 else
3646 size += sizeof(u32);
3647
3648 WARN_ON_ONCE(size & (sizeof(u64)-1));
3649 header->size += size;
3650 }
3651}
3652
3653static void perf_event_output(struct perf_event *event, int nmi,
3654 struct perf_sample_data *data,
3655 struct pt_regs *regs)
3656{
3657 struct perf_output_handle handle;
3658 struct perf_event_header header;
3659
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003660 /* protect the callchain buffers */
3661 rcu_read_lock();
3662
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003663 perf_prepare_sample(&header, data, event, regs);
3664
3665 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003666 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003667
3668 perf_output_sample(&handle, &header, data, event);
3669
3670 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003671
3672exit:
3673 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003674}
3675
3676/*
3677 * read event_id
3678 */
3679
3680struct perf_read_event {
3681 struct perf_event_header header;
3682
3683 u32 pid;
3684 u32 tid;
3685};
3686
3687static void
3688perf_event_read_event(struct perf_event *event,
3689 struct task_struct *task)
3690{
3691 struct perf_output_handle handle;
3692 struct perf_read_event read_event = {
3693 .header = {
3694 .type = PERF_RECORD_READ,
3695 .misc = 0,
3696 .size = sizeof(read_event) + perf_event_read_size(event),
3697 },
3698 .pid = perf_event_pid(event, task),
3699 .tid = perf_event_tid(event, task),
3700 };
3701 int ret;
3702
3703 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3704 if (ret)
3705 return;
3706
3707 perf_output_put(&handle, read_event);
3708 perf_output_read(&handle, event);
3709
3710 perf_output_end(&handle);
3711}
3712
3713/*
3714 * task tracking -- fork/exit
3715 *
Eric B Munson3af9e852010-05-18 15:30:49 +01003716 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003717 */
3718
3719struct perf_task_event {
3720 struct task_struct *task;
3721 struct perf_event_context *task_ctx;
3722
3723 struct {
3724 struct perf_event_header header;
3725
3726 u32 pid;
3727 u32 ppid;
3728 u32 tid;
3729 u32 ptid;
3730 u64 time;
3731 } event_id;
3732};
3733
3734static void perf_event_task_output(struct perf_event *event,
3735 struct perf_task_event *task_event)
3736{
3737 struct perf_output_handle handle;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003738 struct task_struct *task = task_event->task;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003739 int size, ret;
3740
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003741 size = task_event->event_id.header.size;
3742 ret = perf_output_begin(&handle, event, size, 0, 0);
3743
Peter Zijlstraef607772010-05-18 10:50:41 +02003744 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003745 return;
3746
3747 task_event->event_id.pid = perf_event_pid(event, task);
3748 task_event->event_id.ppid = perf_event_pid(event, current);
3749
3750 task_event->event_id.tid = perf_event_tid(event, task);
3751 task_event->event_id.ptid = perf_event_tid(event, current);
3752
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003753 perf_output_put(&handle, task_event->event_id);
3754
3755 perf_output_end(&handle);
3756}
3757
3758static int perf_event_task_match(struct perf_event *event)
3759{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003760 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003761 return 0;
3762
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003763 if (event->cpu != -1 && event->cpu != smp_processor_id())
3764 return 0;
3765
Eric B Munson3af9e852010-05-18 15:30:49 +01003766 if (event->attr.comm || event->attr.mmap ||
3767 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003768 return 1;
3769
3770 return 0;
3771}
3772
3773static void perf_event_task_ctx(struct perf_event_context *ctx,
3774 struct perf_task_event *task_event)
3775{
3776 struct perf_event *event;
3777
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003778 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3779 if (perf_event_task_match(event))
3780 perf_event_task_output(event, task_event);
3781 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003782}
3783
3784static void perf_event_task_event(struct perf_task_event *task_event)
3785{
3786 struct perf_cpu_context *cpuctx;
3787 struct perf_event_context *ctx = task_event->task_ctx;
3788
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01003789 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003790 cpuctx = &get_cpu_var(perf_cpu_context);
3791 perf_event_task_ctx(&cpuctx->ctx, task_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003792 if (!ctx)
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003793 ctx = rcu_dereference(current->perf_event_ctxp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003794 if (ctx)
3795 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003796 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003797 rcu_read_unlock();
3798}
3799
3800static void perf_event_task(struct task_struct *task,
3801 struct perf_event_context *task_ctx,
3802 int new)
3803{
3804 struct perf_task_event task_event;
3805
3806 if (!atomic_read(&nr_comm_events) &&
3807 !atomic_read(&nr_mmap_events) &&
3808 !atomic_read(&nr_task_events))
3809 return;
3810
3811 task_event = (struct perf_task_event){
3812 .task = task,
3813 .task_ctx = task_ctx,
3814 .event_id = {
3815 .header = {
3816 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3817 .misc = 0,
3818 .size = sizeof(task_event.event_id),
3819 },
3820 /* .pid */
3821 /* .ppid */
3822 /* .tid */
3823 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003824 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003825 },
3826 };
3827
3828 perf_event_task_event(&task_event);
3829}
3830
3831void perf_event_fork(struct task_struct *task)
3832{
3833 perf_event_task(task, NULL, 1);
3834}
3835
3836/*
3837 * comm tracking
3838 */
3839
3840struct perf_comm_event {
3841 struct task_struct *task;
3842 char *comm;
3843 int comm_size;
3844
3845 struct {
3846 struct perf_event_header header;
3847
3848 u32 pid;
3849 u32 tid;
3850 } event_id;
3851};
3852
3853static void perf_event_comm_output(struct perf_event *event,
3854 struct perf_comm_event *comm_event)
3855{
3856 struct perf_output_handle handle;
3857 int size = comm_event->event_id.header.size;
3858 int ret = perf_output_begin(&handle, event, size, 0, 0);
3859
3860 if (ret)
3861 return;
3862
3863 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
3864 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
3865
3866 perf_output_put(&handle, comm_event->event_id);
3867 perf_output_copy(&handle, comm_event->comm,
3868 comm_event->comm_size);
3869 perf_output_end(&handle);
3870}
3871
3872static int perf_event_comm_match(struct perf_event *event)
3873{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003874 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003875 return 0;
3876
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003877 if (event->cpu != -1 && event->cpu != smp_processor_id())
3878 return 0;
3879
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003880 if (event->attr.comm)
3881 return 1;
3882
3883 return 0;
3884}
3885
3886static void perf_event_comm_ctx(struct perf_event_context *ctx,
3887 struct perf_comm_event *comm_event)
3888{
3889 struct perf_event *event;
3890
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003891 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3892 if (perf_event_comm_match(event))
3893 perf_event_comm_output(event, comm_event);
3894 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003895}
3896
3897static void perf_event_comm_event(struct perf_comm_event *comm_event)
3898{
3899 struct perf_cpu_context *cpuctx;
3900 struct perf_event_context *ctx;
3901 unsigned int size;
3902 char comm[TASK_COMM_LEN];
3903
3904 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01003905 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003906 size = ALIGN(strlen(comm)+1, sizeof(u64));
3907
3908 comm_event->comm = comm;
3909 comm_event->comm_size = size;
3910
3911 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
3912
Peter Zijlstraf6595f32009-11-20 22:19:47 +01003913 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003914 cpuctx = &get_cpu_var(perf_cpu_context);
3915 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003916 ctx = rcu_dereference(current->perf_event_ctxp);
3917 if (ctx)
3918 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003919 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003920 rcu_read_unlock();
3921}
3922
3923void perf_event_comm(struct task_struct *task)
3924{
3925 struct perf_comm_event comm_event;
3926
3927 if (task->perf_event_ctxp)
3928 perf_event_enable_on_exec(task);
3929
3930 if (!atomic_read(&nr_comm_events))
3931 return;
3932
3933 comm_event = (struct perf_comm_event){
3934 .task = task,
3935 /* .comm */
3936 /* .comm_size */
3937 .event_id = {
3938 .header = {
3939 .type = PERF_RECORD_COMM,
3940 .misc = 0,
3941 /* .size */
3942 },
3943 /* .pid */
3944 /* .tid */
3945 },
3946 };
3947
3948 perf_event_comm_event(&comm_event);
3949}
3950
3951/*
3952 * mmap tracking
3953 */
3954
3955struct perf_mmap_event {
3956 struct vm_area_struct *vma;
3957
3958 const char *file_name;
3959 int file_size;
3960
3961 struct {
3962 struct perf_event_header header;
3963
3964 u32 pid;
3965 u32 tid;
3966 u64 start;
3967 u64 len;
3968 u64 pgoff;
3969 } event_id;
3970};
3971
3972static void perf_event_mmap_output(struct perf_event *event,
3973 struct perf_mmap_event *mmap_event)
3974{
3975 struct perf_output_handle handle;
3976 int size = mmap_event->event_id.header.size;
3977 int ret = perf_output_begin(&handle, event, size, 0, 0);
3978
3979 if (ret)
3980 return;
3981
3982 mmap_event->event_id.pid = perf_event_pid(event, current);
3983 mmap_event->event_id.tid = perf_event_tid(event, current);
3984
3985 perf_output_put(&handle, mmap_event->event_id);
3986 perf_output_copy(&handle, mmap_event->file_name,
3987 mmap_event->file_size);
3988 perf_output_end(&handle);
3989}
3990
3991static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01003992 struct perf_mmap_event *mmap_event,
3993 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003994{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003995 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003996 return 0;
3997
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003998 if (event->cpu != -1 && event->cpu != smp_processor_id())
3999 return 0;
4000
Eric B Munson3af9e852010-05-18 15:30:49 +01004001 if ((!executable && event->attr.mmap_data) ||
4002 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004003 return 1;
4004
4005 return 0;
4006}
4007
4008static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004009 struct perf_mmap_event *mmap_event,
4010 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004011{
4012 struct perf_event *event;
4013
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004014 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004015 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004016 perf_event_mmap_output(event, mmap_event);
4017 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004018}
4019
4020static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4021{
4022 struct perf_cpu_context *cpuctx;
4023 struct perf_event_context *ctx;
4024 struct vm_area_struct *vma = mmap_event->vma;
4025 struct file *file = vma->vm_file;
4026 unsigned int size;
4027 char tmp[16];
4028 char *buf = NULL;
4029 const char *name;
4030
4031 memset(tmp, 0, sizeof(tmp));
4032
4033 if (file) {
4034 /*
4035 * d_path works from the end of the buffer backwards, so we
4036 * need to add enough zero bytes after the string to handle
4037 * the 64bit alignment we do later.
4038 */
4039 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4040 if (!buf) {
4041 name = strncpy(tmp, "//enomem", sizeof(tmp));
4042 goto got_name;
4043 }
4044 name = d_path(&file->f_path, buf, PATH_MAX);
4045 if (IS_ERR(name)) {
4046 name = strncpy(tmp, "//toolong", sizeof(tmp));
4047 goto got_name;
4048 }
4049 } else {
4050 if (arch_vma_name(mmap_event->vma)) {
4051 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4052 sizeof(tmp));
4053 goto got_name;
4054 }
4055
4056 if (!vma->vm_mm) {
4057 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4058 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004059 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4060 vma->vm_end >= vma->vm_mm->brk) {
4061 name = strncpy(tmp, "[heap]", sizeof(tmp));
4062 goto got_name;
4063 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4064 vma->vm_end >= vma->vm_mm->start_stack) {
4065 name = strncpy(tmp, "[stack]", sizeof(tmp));
4066 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004067 }
4068
4069 name = strncpy(tmp, "//anon", sizeof(tmp));
4070 goto got_name;
4071 }
4072
4073got_name:
4074 size = ALIGN(strlen(name)+1, sizeof(u64));
4075
4076 mmap_event->file_name = name;
4077 mmap_event->file_size = size;
4078
4079 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4080
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004081 rcu_read_lock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004082 cpuctx = &get_cpu_var(perf_cpu_context);
Eric B Munson3af9e852010-05-18 15:30:49 +01004083 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event, vma->vm_flags & VM_EXEC);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004084 ctx = rcu_dereference(current->perf_event_ctxp);
4085 if (ctx)
Eric B Munson3af9e852010-05-18 15:30:49 +01004086 perf_event_mmap_ctx(ctx, mmap_event, vma->vm_flags & VM_EXEC);
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004087 put_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004088 rcu_read_unlock();
4089
4090 kfree(buf);
4091}
4092
Eric B Munson3af9e852010-05-18 15:30:49 +01004093void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004094{
4095 struct perf_mmap_event mmap_event;
4096
4097 if (!atomic_read(&nr_mmap_events))
4098 return;
4099
4100 mmap_event = (struct perf_mmap_event){
4101 .vma = vma,
4102 /* .file_name */
4103 /* .file_size */
4104 .event_id = {
4105 .header = {
4106 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004107 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004108 /* .size */
4109 },
4110 /* .pid */
4111 /* .tid */
4112 .start = vma->vm_start,
4113 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004114 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004115 },
4116 };
4117
4118 perf_event_mmap_event(&mmap_event);
4119}
4120
4121/*
4122 * IRQ throttle logging
4123 */
4124
4125static void perf_log_throttle(struct perf_event *event, int enable)
4126{
4127 struct perf_output_handle handle;
4128 int ret;
4129
4130 struct {
4131 struct perf_event_header header;
4132 u64 time;
4133 u64 id;
4134 u64 stream_id;
4135 } throttle_event = {
4136 .header = {
4137 .type = PERF_RECORD_THROTTLE,
4138 .misc = 0,
4139 .size = sizeof(throttle_event),
4140 },
4141 .time = perf_clock(),
4142 .id = primary_event_id(event),
4143 .stream_id = event->id,
4144 };
4145
4146 if (enable)
4147 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4148
4149 ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
4150 if (ret)
4151 return;
4152
4153 perf_output_put(&handle, throttle_event);
4154 perf_output_end(&handle);
4155}
4156
4157/*
4158 * Generic event overflow handling, sampling.
4159 */
4160
4161static int __perf_event_overflow(struct perf_event *event, int nmi,
4162 int throttle, struct perf_sample_data *data,
4163 struct pt_regs *regs)
4164{
4165 int events = atomic_read(&event->event_limit);
4166 struct hw_perf_event *hwc = &event->hw;
4167 int ret = 0;
4168
4169 throttle = (throttle && event->pmu->unthrottle != NULL);
4170
4171 if (!throttle) {
4172 hwc->interrupts++;
4173 } else {
4174 if (hwc->interrupts != MAX_INTERRUPTS) {
4175 hwc->interrupts++;
4176 if (HZ * hwc->interrupts >
4177 (u64)sysctl_perf_event_sample_rate) {
4178 hwc->interrupts = MAX_INTERRUPTS;
4179 perf_log_throttle(event, 0);
4180 ret = 1;
4181 }
4182 } else {
4183 /*
4184 * Keep re-disabling events even though on the previous
4185 * pass we disabled it - just in case we raced with a
4186 * sched-in and the event got enabled again:
4187 */
4188 ret = 1;
4189 }
4190 }
4191
4192 if (event->attr.freq) {
4193 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004194 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004195
Peter Zijlstraabd50712010-01-26 18:50:16 +01004196 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004197
Peter Zijlstraabd50712010-01-26 18:50:16 +01004198 if (delta > 0 && delta < 2*TICK_NSEC)
4199 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004200 }
4201
4202 /*
4203 * XXX event_limit might not quite work as expected on inherited
4204 * events
4205 */
4206
4207 event->pending_kill = POLL_IN;
4208 if (events && atomic_dec_and_test(&event->event_limit)) {
4209 ret = 1;
4210 event->pending_kill = POLL_HUP;
4211 if (nmi) {
4212 event->pending_disable = 1;
4213 perf_pending_queue(&event->pending,
4214 perf_pending_event);
4215 } else
4216 perf_event_disable(event);
4217 }
4218
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004219 if (event->overflow_handler)
4220 event->overflow_handler(event, nmi, data, regs);
4221 else
4222 perf_event_output(event, nmi, data, regs);
4223
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004224 return ret;
4225}
4226
4227int perf_event_overflow(struct perf_event *event, int nmi,
4228 struct perf_sample_data *data,
4229 struct pt_regs *regs)
4230{
4231 return __perf_event_overflow(event, nmi, 1, data, regs);
4232}
4233
4234/*
4235 * Generic software event infrastructure
4236 */
4237
4238/*
4239 * We directly increment event->count and keep a second value in
4240 * event->hw.period_left to count intervals. This period event
4241 * is kept in the range [-sample_period, 0] so that we can use the
4242 * sign as trigger.
4243 */
4244
4245static u64 perf_swevent_set_period(struct perf_event *event)
4246{
4247 struct hw_perf_event *hwc = &event->hw;
4248 u64 period = hwc->last_period;
4249 u64 nr, offset;
4250 s64 old, val;
4251
4252 hwc->last_period = hwc->sample_period;
4253
4254again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004255 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004256 if (val < 0)
4257 return 0;
4258
4259 nr = div64_u64(period + val, period);
4260 offset = nr * period;
4261 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004262 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004263 goto again;
4264
4265 return nr;
4266}
4267
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004268static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004269 int nmi, struct perf_sample_data *data,
4270 struct pt_regs *regs)
4271{
4272 struct hw_perf_event *hwc = &event->hw;
4273 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004274
4275 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004276 if (!overflow)
4277 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004278
4279 if (hwc->interrupts == MAX_INTERRUPTS)
4280 return;
4281
4282 for (; overflow; overflow--) {
4283 if (__perf_event_overflow(event, nmi, throttle,
4284 data, regs)) {
4285 /*
4286 * We inhibit the overflow from happening when
4287 * hwc->interrupts == MAX_INTERRUPTS.
4288 */
4289 break;
4290 }
4291 throttle = 1;
4292 }
4293}
4294
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004295static void perf_swevent_add(struct perf_event *event, u64 nr,
4296 int nmi, struct perf_sample_data *data,
4297 struct pt_regs *regs)
4298{
4299 struct hw_perf_event *hwc = &event->hw;
4300
Peter Zijlstrae7850592010-05-21 14:43:08 +02004301 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004302
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004303 if (!regs)
4304 return;
4305
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004306 if (!hwc->sample_period)
4307 return;
4308
4309 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4310 return perf_swevent_overflow(event, 1, nmi, data, regs);
4311
Peter Zijlstrae7850592010-05-21 14:43:08 +02004312 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004313 return;
4314
4315 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004316}
4317
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004318static int perf_exclude_event(struct perf_event *event,
4319 struct pt_regs *regs)
4320{
4321 if (regs) {
4322 if (event->attr.exclude_user && user_mode(regs))
4323 return 1;
4324
4325 if (event->attr.exclude_kernel && !user_mode(regs))
4326 return 1;
4327 }
4328
4329 return 0;
4330}
4331
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004332static int perf_swevent_match(struct perf_event *event,
4333 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004334 u32 event_id,
4335 struct perf_sample_data *data,
4336 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004337{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004338 if (event->attr.type != type)
4339 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004340
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004341 if (event->attr.config != event_id)
4342 return 0;
4343
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004344 if (perf_exclude_event(event, regs))
4345 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004346
4347 return 1;
4348}
4349
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004350static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004351{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004352 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004353
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004354 return hash_64(val, SWEVENT_HLIST_BITS);
4355}
4356
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004357static inline struct hlist_head *
4358__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004359{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004360 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004361
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004362 return &hlist->heads[hash];
4363}
4364
4365/* For the read side: events when they trigger */
4366static inline struct hlist_head *
4367find_swevent_head_rcu(struct perf_cpu_context *ctx, u64 type, u32 event_id)
4368{
4369 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004370
4371 hlist = rcu_dereference(ctx->swevent_hlist);
4372 if (!hlist)
4373 return NULL;
4374
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004375 return __find_swevent_head(hlist, type, event_id);
4376}
4377
4378/* For the event head insertion and removal in the hlist */
4379static inline struct hlist_head *
4380find_swevent_head(struct perf_cpu_context *ctx, struct perf_event *event)
4381{
4382 struct swevent_hlist *hlist;
4383 u32 event_id = event->attr.config;
4384 u64 type = event->attr.type;
4385
4386 /*
4387 * Event scheduling is always serialized against hlist allocation
4388 * and release. Which makes the protected version suitable here.
4389 * The context lock guarantees that.
4390 */
4391 hlist = rcu_dereference_protected(ctx->swevent_hlist,
4392 lockdep_is_held(&event->ctx->lock));
4393 if (!hlist)
4394 return NULL;
4395
4396 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004397}
4398
4399static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4400 u64 nr, int nmi,
4401 struct perf_sample_data *data,
4402 struct pt_regs *regs)
4403{
4404 struct perf_cpu_context *cpuctx;
4405 struct perf_event *event;
4406 struct hlist_node *node;
4407 struct hlist_head *head;
4408
4409 cpuctx = &__get_cpu_var(perf_cpu_context);
4410
4411 rcu_read_lock();
4412
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004413 head = find_swevent_head_rcu(cpuctx, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004414
4415 if (!head)
4416 goto end;
4417
4418 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004419 if (perf_swevent_match(event, type, event_id, data, regs))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004420 perf_swevent_add(event, nr, nmi, data, regs);
4421 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004422end:
4423 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004424}
4425
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004426int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004427{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004428 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004429
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004430 return get_recursion_context(cpuctx->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004431}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004432EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004433
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004434void inline perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004435{
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004436 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004437
4438 put_recursion_context(cpuctx->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004439}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004440
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004441void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4442 struct pt_regs *regs, u64 addr)
4443{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004444 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004445 int rctx;
4446
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004447 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004448 rctx = perf_swevent_get_recursion_context();
4449 if (rctx < 0)
4450 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004451
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004452 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004453
4454 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004455
4456 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004457 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004458}
4459
4460static void perf_swevent_read(struct perf_event *event)
4461{
4462}
4463
4464static int perf_swevent_enable(struct perf_event *event)
4465{
4466 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004467 struct perf_cpu_context *cpuctx;
4468 struct hlist_head *head;
4469
4470 cpuctx = &__get_cpu_var(perf_cpu_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004471
4472 if (hwc->sample_period) {
4473 hwc->last_period = hwc->sample_period;
4474 perf_swevent_set_period(event);
4475 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004476
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004477 head = find_swevent_head(cpuctx, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004478 if (WARN_ON_ONCE(!head))
4479 return -EINVAL;
4480
4481 hlist_add_head_rcu(&event->hlist_entry, head);
4482
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004483 return 0;
4484}
4485
4486static void perf_swevent_disable(struct perf_event *event)
4487{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004488 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004489}
4490
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004491static void perf_swevent_void(struct perf_event *event)
4492{
4493}
4494
4495static int perf_swevent_int(struct perf_event *event)
4496{
4497 return 0;
4498}
4499
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004500/* Deref the hlist from the update side */
4501static inline struct swevent_hlist *
4502swevent_hlist_deref(struct perf_cpu_context *cpuctx)
4503{
4504 return rcu_dereference_protected(cpuctx->swevent_hlist,
4505 lockdep_is_held(&cpuctx->hlist_mutex));
4506}
4507
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004508static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4509{
4510 struct swevent_hlist *hlist;
4511
4512 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4513 kfree(hlist);
4514}
4515
4516static void swevent_hlist_release(struct perf_cpu_context *cpuctx)
4517{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004518 struct swevent_hlist *hlist = swevent_hlist_deref(cpuctx);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004519
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004520 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004521 return;
4522
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004523 rcu_assign_pointer(cpuctx->swevent_hlist, NULL);
4524 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4525}
4526
4527static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4528{
4529 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4530
4531 mutex_lock(&cpuctx->hlist_mutex);
4532
4533 if (!--cpuctx->hlist_refcount)
4534 swevent_hlist_release(cpuctx);
4535
4536 mutex_unlock(&cpuctx->hlist_mutex);
4537}
4538
4539static void swevent_hlist_put(struct perf_event *event)
4540{
4541 int cpu;
4542
4543 if (event->cpu != -1) {
4544 swevent_hlist_put_cpu(event, event->cpu);
4545 return;
4546 }
4547
4548 for_each_possible_cpu(cpu)
4549 swevent_hlist_put_cpu(event, cpu);
4550}
4551
4552static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4553{
4554 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
4555 int err = 0;
4556
4557 mutex_lock(&cpuctx->hlist_mutex);
4558
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004559 if (!swevent_hlist_deref(cpuctx) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004560 struct swevent_hlist *hlist;
4561
4562 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4563 if (!hlist) {
4564 err = -ENOMEM;
4565 goto exit;
4566 }
4567 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
4568 }
4569 cpuctx->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004570exit:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004571 mutex_unlock(&cpuctx->hlist_mutex);
4572
4573 return err;
4574}
4575
4576static int swevent_hlist_get(struct perf_event *event)
4577{
4578 int err;
4579 int cpu, failed_cpu;
4580
4581 if (event->cpu != -1)
4582 return swevent_hlist_get_cpu(event, event->cpu);
4583
4584 get_online_cpus();
4585 for_each_possible_cpu(cpu) {
4586 err = swevent_hlist_get_cpu(event, cpu);
4587 if (err) {
4588 failed_cpu = cpu;
4589 goto fail;
4590 }
4591 }
4592 put_online_cpus();
4593
4594 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004595fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004596 for_each_possible_cpu(cpu) {
4597 if (cpu == failed_cpu)
4598 break;
4599 swevent_hlist_put_cpu(event, cpu);
4600 }
4601
4602 put_online_cpus();
4603 return err;
4604}
4605
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004606atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004607
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004608static void sw_perf_event_destroy(struct perf_event *event)
4609{
4610 u64 event_id = event->attr.config;
4611
4612 WARN_ON(event->parent);
4613
4614 atomic_dec(&perf_swevent_enabled[event_id]);
4615 swevent_hlist_put(event);
4616}
4617
4618static int perf_swevent_init(struct perf_event *event)
4619{
4620 int event_id = event->attr.config;
4621
4622 if (event->attr.type != PERF_TYPE_SOFTWARE)
4623 return -ENOENT;
4624
4625 switch (event_id) {
4626 case PERF_COUNT_SW_CPU_CLOCK:
4627 case PERF_COUNT_SW_TASK_CLOCK:
4628 return -ENOENT;
4629
4630 default:
4631 break;
4632 }
4633
4634 if (event_id > PERF_COUNT_SW_MAX)
4635 return -ENOENT;
4636
4637 if (!event->parent) {
4638 int err;
4639
4640 err = swevent_hlist_get(event);
4641 if (err)
4642 return err;
4643
4644 atomic_inc(&perf_swevent_enabled[event_id]);
4645 event->destroy = sw_perf_event_destroy;
4646 }
4647
4648 return 0;
4649}
4650
4651static struct pmu perf_swevent = {
4652 .event_init = perf_swevent_init,
4653 .enable = perf_swevent_enable,
4654 .disable = perf_swevent_disable,
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004655 .start = perf_swevent_int,
4656 .stop = perf_swevent_void,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004657 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004658 .unthrottle = perf_swevent_void, /* hwc->interrupts already reset */
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004659};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004660
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004661#ifdef CONFIG_EVENT_TRACING
4662
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004663static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004664 struct perf_sample_data *data)
4665{
4666 void *record = data->raw->data;
4667
4668 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4669 return 1;
4670 return 0;
4671}
4672
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004673static int perf_tp_event_match(struct perf_event *event,
4674 struct perf_sample_data *data,
4675 struct pt_regs *regs)
4676{
Peter Zijlstra580d6072010-05-20 20:54:31 +02004677 /*
4678 * All tracepoints are from kernel-space.
4679 */
4680 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004681 return 0;
4682
4683 if (!perf_tp_filter_match(event, data))
4684 return 0;
4685
4686 return 1;
4687}
4688
4689void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004690 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004691{
4692 struct perf_sample_data data;
4693 struct perf_event *event;
4694 struct hlist_node *node;
4695
4696 struct perf_raw_record raw = {
4697 .size = entry_size,
4698 .data = record,
4699 };
4700
4701 perf_sample_data_init(&data, addr);
4702 data.raw = &raw;
4703
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004704 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4705 if (perf_tp_event_match(event, &data, regs))
4706 perf_swevent_add(event, count, 1, &data, regs);
4707 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004708
4709 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004710}
4711EXPORT_SYMBOL_GPL(perf_tp_event);
4712
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004713static void tp_perf_event_destroy(struct perf_event *event)
4714{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004715 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004716}
4717
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004718static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004719{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004720 int err;
4721
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004722 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4723 return -ENOENT;
4724
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004725 /*
4726 * Raw tracepoint data is a severe data leak, only allow root to
4727 * have these.
4728 */
4729 if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
4730 perf_paranoid_tracepoint_raw() &&
4731 !capable(CAP_SYS_ADMIN))
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004732 return -EPERM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004733
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004734 err = perf_trace_init(event);
4735 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004736 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004737
4738 event->destroy = tp_perf_event_destroy;
4739
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004740 return 0;
4741}
4742
4743static struct pmu perf_tracepoint = {
4744 .event_init = perf_tp_event_init,
4745 .enable = perf_trace_enable,
4746 .disable = perf_trace_disable,
4747 .start = perf_swevent_int,
4748 .stop = perf_swevent_void,
4749 .read = perf_swevent_read,
4750 .unthrottle = perf_swevent_void,
4751};
4752
4753static inline void perf_tp_register(void)
4754{
4755 perf_pmu_register(&perf_tracepoint);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004756}
Li Zefan6fb29152009-10-15 11:21:42 +08004757
4758static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4759{
4760 char *filter_str;
4761 int ret;
4762
4763 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4764 return -EINVAL;
4765
4766 filter_str = strndup_user(arg, PAGE_SIZE);
4767 if (IS_ERR(filter_str))
4768 return PTR_ERR(filter_str);
4769
4770 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4771
4772 kfree(filter_str);
4773 return ret;
4774}
4775
4776static void perf_event_free_filter(struct perf_event *event)
4777{
4778 ftrace_profile_free_filter(event);
4779}
4780
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004781#else
Li Zefan6fb29152009-10-15 11:21:42 +08004782
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004783static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004784{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004785}
Li Zefan6fb29152009-10-15 11:21:42 +08004786
4787static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4788{
4789 return -ENOENT;
4790}
4791
4792static void perf_event_free_filter(struct perf_event *event)
4793{
4794}
4795
Li Zefan07b139c2009-12-21 14:27:35 +08004796#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004797
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004798#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004799void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004800{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004801 struct perf_sample_data sample;
4802 struct pt_regs *regs = data;
4803
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004804 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004805
4806 if (!perf_exclude_event(bp, regs))
4807 perf_swevent_add(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004808}
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02004809#endif
4810
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004811/*
4812 * hrtimer based swevent callback
4813 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004814
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004815static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004816{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004817 enum hrtimer_restart ret = HRTIMER_RESTART;
4818 struct perf_sample_data data;
4819 struct pt_regs *regs;
4820 struct perf_event *event;
4821 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004822
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004823 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
4824 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004825
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004826 perf_sample_data_init(&data, 0);
4827 data.period = event->hw.last_period;
4828 regs = get_irq_regs();
4829
4830 if (regs && !perf_exclude_event(event, regs)) {
4831 if (!(event->attr.exclude_idle && current->pid == 0))
4832 if (perf_event_overflow(event, 0, &data, regs))
4833 ret = HRTIMER_NORESTART;
4834 }
4835
4836 period = max_t(u64, 10000, event->hw.sample_period);
4837 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
4838
4839 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004840}
4841
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004842static void perf_swevent_start_hrtimer(struct perf_event *event)
4843{
4844 struct hw_perf_event *hwc = &event->hw;
4845
4846 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
4847 hwc->hrtimer.function = perf_swevent_hrtimer;
4848 if (hwc->sample_period) {
4849 u64 period;
4850
4851 if (hwc->remaining) {
4852 if (hwc->remaining < 0)
4853 period = 10000;
4854 else
4855 period = hwc->remaining;
4856 hwc->remaining = 0;
4857 } else {
4858 period = max_t(u64, 10000, hwc->sample_period);
4859 }
4860 __hrtimer_start_range_ns(&hwc->hrtimer,
4861 ns_to_ktime(period), 0,
4862 HRTIMER_MODE_REL, 0);
4863 }
4864}
4865
4866static void perf_swevent_cancel_hrtimer(struct perf_event *event)
4867{
4868 struct hw_perf_event *hwc = &event->hw;
4869
4870 if (hwc->sample_period) {
4871 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
4872 hwc->remaining = ktime_to_ns(remaining);
4873
4874 hrtimer_cancel(&hwc->hrtimer);
4875 }
4876}
4877
4878/*
4879 * Software event: cpu wall time clock
4880 */
4881
4882static void cpu_clock_event_update(struct perf_event *event)
4883{
4884 int cpu = raw_smp_processor_id();
4885 s64 prev;
4886 u64 now;
4887
4888 now = cpu_clock(cpu);
4889 prev = local64_xchg(&event->hw.prev_count, now);
4890 local64_add(now - prev, &event->count);
4891}
4892
4893static int cpu_clock_event_enable(struct perf_event *event)
4894{
4895 struct hw_perf_event *hwc = &event->hw;
4896 int cpu = raw_smp_processor_id();
4897
4898 local64_set(&hwc->prev_count, cpu_clock(cpu));
4899 perf_swevent_start_hrtimer(event);
4900
4901 return 0;
4902}
4903
4904static void cpu_clock_event_disable(struct perf_event *event)
4905{
4906 perf_swevent_cancel_hrtimer(event);
4907 cpu_clock_event_update(event);
4908}
4909
4910static void cpu_clock_event_read(struct perf_event *event)
4911{
4912 cpu_clock_event_update(event);
4913}
4914
4915static int cpu_clock_event_init(struct perf_event *event)
4916{
4917 if (event->attr.type != PERF_TYPE_SOFTWARE)
4918 return -ENOENT;
4919
4920 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
4921 return -ENOENT;
4922
4923 return 0;
4924}
4925
4926static struct pmu perf_cpu_clock = {
4927 .event_init = cpu_clock_event_init,
4928 .enable = cpu_clock_event_enable,
4929 .disable = cpu_clock_event_disable,
4930 .read = cpu_clock_event_read,
4931};
4932
4933/*
4934 * Software event: task time clock
4935 */
4936
4937static void task_clock_event_update(struct perf_event *event, u64 now)
4938{
4939 u64 prev;
4940 s64 delta;
4941
4942 prev = local64_xchg(&event->hw.prev_count, now);
4943 delta = now - prev;
4944 local64_add(delta, &event->count);
4945}
4946
4947static int task_clock_event_enable(struct perf_event *event)
4948{
4949 struct hw_perf_event *hwc = &event->hw;
4950 u64 now;
4951
4952 now = event->ctx->time;
4953
4954 local64_set(&hwc->prev_count, now);
4955
4956 perf_swevent_start_hrtimer(event);
4957
4958 return 0;
4959}
4960
4961static void task_clock_event_disable(struct perf_event *event)
4962{
4963 perf_swevent_cancel_hrtimer(event);
4964 task_clock_event_update(event, event->ctx->time);
4965
4966}
4967
4968static void task_clock_event_read(struct perf_event *event)
4969{
4970 u64 time;
4971
4972 if (!in_nmi()) {
4973 update_context_time(event->ctx);
4974 time = event->ctx->time;
4975 } else {
4976 u64 now = perf_clock();
4977 u64 delta = now - event->ctx->timestamp;
4978 time = event->ctx->time + delta;
4979 }
4980
4981 task_clock_event_update(event, time);
4982}
4983
4984static int task_clock_event_init(struct perf_event *event)
4985{
4986 if (event->attr.type != PERF_TYPE_SOFTWARE)
4987 return -ENOENT;
4988
4989 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
4990 return -ENOENT;
4991
4992 return 0;
4993}
4994
4995static struct pmu perf_task_clock = {
4996 .event_init = task_clock_event_init,
4997 .enable = task_clock_event_enable,
4998 .disable = task_clock_event_disable,
4999 .read = task_clock_event_read,
5000};
5001
5002static LIST_HEAD(pmus);
5003static DEFINE_MUTEX(pmus_lock);
5004static struct srcu_struct pmus_srcu;
5005
5006int perf_pmu_register(struct pmu *pmu)
5007{
5008 mutex_lock(&pmus_lock);
5009 list_add_rcu(&pmu->entry, &pmus);
5010 mutex_unlock(&pmus_lock);
5011
5012 return 0;
5013}
5014
5015void perf_pmu_unregister(struct pmu *pmu)
5016{
5017 mutex_lock(&pmus_lock);
5018 list_del_rcu(&pmu->entry);
5019 mutex_unlock(&pmus_lock);
5020
5021 synchronize_srcu(&pmus_srcu);
5022}
5023
5024struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005025{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005026 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005027 int idx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005028
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005029 idx = srcu_read_lock(&pmus_srcu);
5030 list_for_each_entry_rcu(pmu, &pmus, entry) {
5031 int ret = pmu->event_init(event);
5032 if (!ret)
5033 break;
5034 if (ret != -ENOENT) {
5035 pmu = ERR_PTR(ret);
5036 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005037 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005038 }
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005039 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005040
5041 return pmu;
5042}
5043
5044/*
5045 * Allocate and initialize a event structure
5046 */
5047static struct perf_event *
5048perf_event_alloc(struct perf_event_attr *attr,
5049 int cpu,
5050 struct perf_event_context *ctx,
5051 struct perf_event *group_leader,
5052 struct perf_event *parent_event,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005053 perf_overflow_handler_t overflow_handler,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005054 gfp_t gfpflags)
5055{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005056 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005057 struct perf_event *event;
5058 struct hw_perf_event *hwc;
5059 long err;
5060
5061 event = kzalloc(sizeof(*event), gfpflags);
5062 if (!event)
5063 return ERR_PTR(-ENOMEM);
5064
5065 /*
5066 * Single events are their own group leaders, with an
5067 * empty sibling list:
5068 */
5069 if (!group_leader)
5070 group_leader = event;
5071
5072 mutex_init(&event->child_mutex);
5073 INIT_LIST_HEAD(&event->child_list);
5074
5075 INIT_LIST_HEAD(&event->group_entry);
5076 INIT_LIST_HEAD(&event->event_entry);
5077 INIT_LIST_HEAD(&event->sibling_list);
5078 init_waitqueue_head(&event->waitq);
5079
5080 mutex_init(&event->mmap_mutex);
5081
5082 event->cpu = cpu;
5083 event->attr = *attr;
5084 event->group_leader = group_leader;
5085 event->pmu = NULL;
5086 event->ctx = ctx;
5087 event->oncpu = -1;
5088
5089 event->parent = parent_event;
5090
5091 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5092 event->id = atomic64_inc_return(&perf_event_id);
5093
5094 event->state = PERF_EVENT_STATE_INACTIVE;
5095
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005096 if (!overflow_handler && parent_event)
5097 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005098
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005099 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005100
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005101 if (attr->disabled)
5102 event->state = PERF_EVENT_STATE_OFF;
5103
5104 pmu = NULL;
5105
5106 hwc = &event->hw;
5107 hwc->sample_period = attr->sample_period;
5108 if (attr->freq && attr->sample_freq)
5109 hwc->sample_period = 1;
5110 hwc->last_period = hwc->sample_period;
5111
Peter Zijlstrae7850592010-05-21 14:43:08 +02005112 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005113
5114 /*
5115 * we currently do not support PERF_FORMAT_GROUP on inherited events
5116 */
5117 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5118 goto done;
5119
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005120 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005121
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005122done:
5123 err = 0;
5124 if (!pmu)
5125 err = -EINVAL;
5126 else if (IS_ERR(pmu))
5127 err = PTR_ERR(pmu);
5128
5129 if (err) {
5130 if (event->ns)
5131 put_pid_ns(event->ns);
5132 kfree(event);
5133 return ERR_PTR(err);
5134 }
5135
5136 event->pmu = pmu;
5137
5138 if (!event->parent) {
5139 atomic_inc(&nr_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01005140 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005141 atomic_inc(&nr_mmap_events);
5142 if (event->attr.comm)
5143 atomic_inc(&nr_comm_events);
5144 if (event->attr.task)
5145 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005146 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5147 err = get_callchain_buffers();
5148 if (err) {
5149 free_event(event);
5150 return ERR_PTR(err);
5151 }
5152 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005153 }
5154
5155 return event;
5156}
5157
5158static int perf_copy_attr(struct perf_event_attr __user *uattr,
5159 struct perf_event_attr *attr)
5160{
5161 u32 size;
5162 int ret;
5163
5164 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5165 return -EFAULT;
5166
5167 /*
5168 * zero the full structure, so that a short copy will be nice.
5169 */
5170 memset(attr, 0, sizeof(*attr));
5171
5172 ret = get_user(size, &uattr->size);
5173 if (ret)
5174 return ret;
5175
5176 if (size > PAGE_SIZE) /* silly large */
5177 goto err_size;
5178
5179 if (!size) /* abi compat */
5180 size = PERF_ATTR_SIZE_VER0;
5181
5182 if (size < PERF_ATTR_SIZE_VER0)
5183 goto err_size;
5184
5185 /*
5186 * If we're handed a bigger struct than we know of,
5187 * ensure all the unknown bits are 0 - i.e. new
5188 * user-space does not rely on any kernel feature
5189 * extensions we dont know about yet.
5190 */
5191 if (size > sizeof(*attr)) {
5192 unsigned char __user *addr;
5193 unsigned char __user *end;
5194 unsigned char val;
5195
5196 addr = (void __user *)uattr + sizeof(*attr);
5197 end = (void __user *)uattr + size;
5198
5199 for (; addr < end; addr++) {
5200 ret = get_user(val, addr);
5201 if (ret)
5202 return ret;
5203 if (val)
5204 goto err_size;
5205 }
5206 size = sizeof(*attr);
5207 }
5208
5209 ret = copy_from_user(attr, uattr, size);
5210 if (ret)
5211 return -EFAULT;
5212
5213 /*
5214 * If the type exists, the corresponding creation will verify
5215 * the attr->config.
5216 */
5217 if (attr->type >= PERF_TYPE_MAX)
5218 return -EINVAL;
5219
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05305220 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005221 return -EINVAL;
5222
5223 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5224 return -EINVAL;
5225
5226 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5227 return -EINVAL;
5228
5229out:
5230 return ret;
5231
5232err_size:
5233 put_user(sizeof(*attr), &uattr->size);
5234 ret = -E2BIG;
5235 goto out;
5236}
5237
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005238static int
5239perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005240{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005241 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005242 int ret = -EINVAL;
5243
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005244 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005245 goto set;
5246
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005247 /* don't allow circular references */
5248 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005249 goto out;
5250
Peter Zijlstra0f139302010-05-20 14:35:15 +02005251 /*
5252 * Don't allow cross-cpu buffers
5253 */
5254 if (output_event->cpu != event->cpu)
5255 goto out;
5256
5257 /*
5258 * If its not a per-cpu buffer, it must be the same task.
5259 */
5260 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5261 goto out;
5262
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005263set:
5264 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005265 /* Can't redirect output if we've got an active mmap() */
5266 if (atomic_read(&event->mmap_count))
5267 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005268
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005269 if (output_event) {
5270 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005271 buffer = perf_buffer_get(output_event);
5272 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005273 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005274 }
5275
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005276 old_buffer = event->buffer;
5277 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005278 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005279unlock:
5280 mutex_unlock(&event->mmap_mutex);
5281
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005282 if (old_buffer)
5283 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005284out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005285 return ret;
5286}
5287
5288/**
5289 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5290 *
5291 * @attr_uptr: event_id type attributes for monitoring/sampling
5292 * @pid: target pid
5293 * @cpu: target cpu
5294 * @group_fd: group leader event fd
5295 */
5296SYSCALL_DEFINE5(perf_event_open,
5297 struct perf_event_attr __user *, attr_uptr,
5298 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5299{
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005300 struct perf_event *event, *group_leader = NULL, *output_event = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005301 struct perf_event_attr attr;
5302 struct perf_event_context *ctx;
5303 struct file *event_file = NULL;
5304 struct file *group_file = NULL;
Al Viroea635c62010-05-26 17:40:29 -04005305 int event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005306 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005307 int err;
5308
5309 /* for future expandability... */
5310 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5311 return -EINVAL;
5312
5313 err = perf_copy_attr(attr_uptr, &attr);
5314 if (err)
5315 return err;
5316
5317 if (!attr.exclude_kernel) {
5318 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5319 return -EACCES;
5320 }
5321
5322 if (attr.freq) {
5323 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5324 return -EINVAL;
5325 }
5326
Al Viroea635c62010-05-26 17:40:29 -04005327 event_fd = get_unused_fd_flags(O_RDWR);
5328 if (event_fd < 0)
5329 return event_fd;
5330
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005331 /*
5332 * Get the target context (task or percpu):
5333 */
5334 ctx = find_get_context(pid, cpu);
Al Viroea635c62010-05-26 17:40:29 -04005335 if (IS_ERR(ctx)) {
5336 err = PTR_ERR(ctx);
5337 goto err_fd;
5338 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005339
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005340 if (group_fd != -1) {
5341 group_leader = perf_fget_light(group_fd, &fput_needed);
5342 if (IS_ERR(group_leader)) {
5343 err = PTR_ERR(group_leader);
5344 goto err_put_context;
5345 }
5346 group_file = group_leader->filp;
5347 if (flags & PERF_FLAG_FD_OUTPUT)
5348 output_event = group_leader;
5349 if (flags & PERF_FLAG_FD_NO_GROUP)
5350 group_leader = NULL;
5351 }
5352
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005353 /*
5354 * Look up the group leader (we will attach this event to it):
5355 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005356 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005357 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005358
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005359 /*
5360 * Do not allow a recursive hierarchy (this new sibling
5361 * becoming part of another group-sibling):
5362 */
5363 if (group_leader->group_leader != group_leader)
5364 goto err_put_context;
5365 /*
5366 * Do not allow to attach to a group in a different
5367 * task or CPU context:
5368 */
5369 if (group_leader->ctx != ctx)
5370 goto err_put_context;
5371 /*
5372 * Only a group leader can be exclusive or pinned
5373 */
5374 if (attr.exclusive || attr.pinned)
5375 goto err_put_context;
5376 }
5377
5378 event = perf_event_alloc(&attr, cpu, ctx, group_leader,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005379 NULL, NULL, GFP_KERNEL);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005380 if (IS_ERR(event)) {
5381 err = PTR_ERR(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005382 goto err_put_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005383 }
5384
5385 if (output_event) {
5386 err = perf_event_set_output(event, output_event);
5387 if (err)
5388 goto err_free_put_context;
5389 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005390
Al Viroea635c62010-05-26 17:40:29 -04005391 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5392 if (IS_ERR(event_file)) {
5393 err = PTR_ERR(event_file);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005394 goto err_free_put_context;
Al Viroea635c62010-05-26 17:40:29 -04005395 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005396
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005397 event->filp = event_file;
5398 WARN_ON_ONCE(ctx->parent_ctx);
5399 mutex_lock(&ctx->mutex);
5400 perf_install_in_context(ctx, event, cpu);
5401 ++ctx->generation;
5402 mutex_unlock(&ctx->mutex);
5403
5404 event->owner = current;
5405 get_task_struct(current);
5406 mutex_lock(&current->perf_event_mutex);
5407 list_add_tail(&event->owner_entry, &current->perf_event_list);
5408 mutex_unlock(&current->perf_event_mutex);
5409
Peter Zijlstra8a495422010-05-27 15:47:49 +02005410 /*
5411 * Drop the reference on the group_event after placing the
5412 * new event on the sibling_list. This ensures destruction
5413 * of the group leader will find the pointer to itself in
5414 * perf_group_detach().
5415 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005416 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005417 fd_install(event_fd, event_file);
5418 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005419
Al Viroea635c62010-05-26 17:40:29 -04005420err_free_put_context:
5421 free_event(event);
5422err_put_context:
5423 fput_light(group_file, fput_needed);
5424 put_ctx(ctx);
5425err_fd:
5426 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005427 return err;
5428}
5429
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005430/**
5431 * perf_event_create_kernel_counter
5432 *
5433 * @attr: attributes of the counter to create
5434 * @cpu: cpu in which the counter is bound
5435 * @pid: task to profile
5436 */
5437struct perf_event *
5438perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005439 pid_t pid,
5440 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005441{
5442 struct perf_event *event;
5443 struct perf_event_context *ctx;
5444 int err;
5445
5446 /*
5447 * Get the target context (task or percpu):
5448 */
5449
5450 ctx = find_get_context(pid, cpu);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005451 if (IS_ERR(ctx)) {
5452 err = PTR_ERR(ctx);
5453 goto err_exit;
5454 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005455
5456 event = perf_event_alloc(attr, cpu, ctx, NULL,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005457 NULL, overflow_handler, GFP_KERNEL);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005458 if (IS_ERR(event)) {
5459 err = PTR_ERR(event);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005460 goto err_put_context;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005461 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005462
5463 event->filp = NULL;
5464 WARN_ON_ONCE(ctx->parent_ctx);
5465 mutex_lock(&ctx->mutex);
5466 perf_install_in_context(ctx, event, cpu);
5467 ++ctx->generation;
5468 mutex_unlock(&ctx->mutex);
5469
5470 event->owner = current;
5471 get_task_struct(current);
5472 mutex_lock(&current->perf_event_mutex);
5473 list_add_tail(&event->owner_entry, &current->perf_event_list);
5474 mutex_unlock(&current->perf_event_mutex);
5475
5476 return event;
5477
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005478 err_put_context:
5479 put_ctx(ctx);
5480 err_exit:
5481 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005482}
5483EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5484
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005485/*
5486 * inherit a event from parent task to child task:
5487 */
5488static struct perf_event *
5489inherit_event(struct perf_event *parent_event,
5490 struct task_struct *parent,
5491 struct perf_event_context *parent_ctx,
5492 struct task_struct *child,
5493 struct perf_event *group_leader,
5494 struct perf_event_context *child_ctx)
5495{
5496 struct perf_event *child_event;
5497
5498 /*
5499 * Instead of creating recursive hierarchies of events,
5500 * we link inherited events back to the original parent,
5501 * which has a filp for sure, which we use as the reference
5502 * count:
5503 */
5504 if (parent_event->parent)
5505 parent_event = parent_event->parent;
5506
5507 child_event = perf_event_alloc(&parent_event->attr,
5508 parent_event->cpu, child_ctx,
5509 group_leader, parent_event,
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005510 NULL, GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005511 if (IS_ERR(child_event))
5512 return child_event;
5513 get_ctx(child_ctx);
5514
5515 /*
5516 * Make the child state follow the state of the parent event,
5517 * not its attr.disabled bit. We hold the parent's mutex,
5518 * so we won't race with perf_event_{en, dis}able_family.
5519 */
5520 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
5521 child_event->state = PERF_EVENT_STATE_INACTIVE;
5522 else
5523 child_event->state = PERF_EVENT_STATE_OFF;
5524
Peter Zijlstra75c9f322010-01-29 09:04:26 +01005525 if (parent_event->attr.freq) {
5526 u64 sample_period = parent_event->hw.sample_period;
5527 struct hw_perf_event *hwc = &child_event->hw;
5528
5529 hwc->sample_period = sample_period;
5530 hwc->last_period = sample_period;
5531
Peter Zijlstrae7850592010-05-21 14:43:08 +02005532 local64_set(&hwc->period_left, sample_period);
Peter Zijlstra75c9f322010-01-29 09:04:26 +01005533 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005534
Peter Zijlstra453f19e2009-11-20 22:19:43 +01005535 child_event->overflow_handler = parent_event->overflow_handler;
5536
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005537 /*
5538 * Link it up in the child's context:
5539 */
5540 add_event_to_ctx(child_event, child_ctx);
5541
5542 /*
5543 * Get a reference to the parent filp - we will fput it
5544 * when the child event exits. This is safe to do because
5545 * we are in the parent and we know that the filp still
5546 * exists and has a nonzero count:
5547 */
5548 atomic_long_inc(&parent_event->filp->f_count);
5549
5550 /*
5551 * Link this into the parent event's child list
5552 */
5553 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5554 mutex_lock(&parent_event->child_mutex);
5555 list_add_tail(&child_event->child_list, &parent_event->child_list);
5556 mutex_unlock(&parent_event->child_mutex);
5557
5558 return child_event;
5559}
5560
5561static int inherit_group(struct perf_event *parent_event,
5562 struct task_struct *parent,
5563 struct perf_event_context *parent_ctx,
5564 struct task_struct *child,
5565 struct perf_event_context *child_ctx)
5566{
5567 struct perf_event *leader;
5568 struct perf_event *sub;
5569 struct perf_event *child_ctr;
5570
5571 leader = inherit_event(parent_event, parent, parent_ctx,
5572 child, NULL, child_ctx);
5573 if (IS_ERR(leader))
5574 return PTR_ERR(leader);
5575 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
5576 child_ctr = inherit_event(sub, parent, parent_ctx,
5577 child, leader, child_ctx);
5578 if (IS_ERR(child_ctr))
5579 return PTR_ERR(child_ctr);
5580 }
5581 return 0;
5582}
5583
5584static void sync_child_event(struct perf_event *child_event,
5585 struct task_struct *child)
5586{
5587 struct perf_event *parent_event = child_event->parent;
5588 u64 child_val;
5589
5590 if (child_event->attr.inherit_stat)
5591 perf_event_read_event(child_event, child);
5592
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005593 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005594
5595 /*
5596 * Add back the child's count to the parent's count:
5597 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02005598 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005599 atomic64_add(child_event->total_time_enabled,
5600 &parent_event->child_total_time_enabled);
5601 atomic64_add(child_event->total_time_running,
5602 &parent_event->child_total_time_running);
5603
5604 /*
5605 * Remove this event from the parent's list
5606 */
5607 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5608 mutex_lock(&parent_event->child_mutex);
5609 list_del_init(&child_event->child_list);
5610 mutex_unlock(&parent_event->child_mutex);
5611
5612 /*
5613 * Release the parent event, if this was the last
5614 * reference to it.
5615 */
5616 fput(parent_event->filp);
5617}
5618
5619static void
5620__perf_event_exit_task(struct perf_event *child_event,
5621 struct perf_event_context *child_ctx,
5622 struct task_struct *child)
5623{
5624 struct perf_event *parent_event;
5625
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005626 perf_event_remove_from_context(child_event);
5627
5628 parent_event = child_event->parent;
5629 /*
5630 * It can happen that parent exits first, and has events
5631 * that are still around due to the child reference. These
5632 * events need to be zapped - but otherwise linger.
5633 */
5634 if (parent_event) {
5635 sync_child_event(child_event, child);
5636 free_event(child_event);
5637 }
5638}
5639
5640/*
5641 * When a child task exits, feed back event values to parent events.
5642 */
5643void perf_event_exit_task(struct task_struct *child)
5644{
5645 struct perf_event *child_event, *tmp;
5646 struct perf_event_context *child_ctx;
5647 unsigned long flags;
5648
5649 if (likely(!child->perf_event_ctxp)) {
5650 perf_event_task(child, NULL, 0);
5651 return;
5652 }
5653
5654 local_irq_save(flags);
5655 /*
5656 * We can't reschedule here because interrupts are disabled,
5657 * and either child is current or it is a task that can't be
5658 * scheduled, so we are now safe from rescheduling changing
5659 * our context.
5660 */
5661 child_ctx = child->perf_event_ctxp;
5662 __perf_event_task_sched_out(child_ctx);
5663
5664 /*
5665 * Take the context lock here so that if find_get_context is
5666 * reading child->perf_event_ctxp, we wait until it has
5667 * incremented the context's refcount before we do put_ctx below.
5668 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005669 raw_spin_lock(&child_ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005670 child->perf_event_ctxp = NULL;
5671 /*
5672 * If this context is a clone; unclone it so it can't get
5673 * swapped to another process while we're removing all
5674 * the events from it.
5675 */
5676 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01005677 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01005678 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005679
5680 /*
5681 * Report the task dead after unscheduling the events so that we
5682 * won't get any samples after PERF_RECORD_EXIT. We can however still
5683 * get a few PERF_RECORD_READ events.
5684 */
5685 perf_event_task(child, child_ctx, 0);
5686
5687 /*
5688 * We can recurse on the same lock type through:
5689 *
5690 * __perf_event_exit_task()
5691 * sync_child_event()
5692 * fput(parent_event->filp)
5693 * perf_release()
5694 * mutex_lock(&ctx->mutex)
5695 *
5696 * But since its the parent context it won't be the same instance.
5697 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02005698 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005699
5700again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005701 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
5702 group_entry)
5703 __perf_event_exit_task(child_event, child_ctx, child);
5704
5705 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005706 group_entry)
5707 __perf_event_exit_task(child_event, child_ctx, child);
5708
5709 /*
5710 * If the last event was a group event, it will have appended all
5711 * its siblings to the list, but we obtained 'tmp' before that which
5712 * will still point to the list head terminating the iteration.
5713 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005714 if (!list_empty(&child_ctx->pinned_groups) ||
5715 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005716 goto again;
5717
5718 mutex_unlock(&child_ctx->mutex);
5719
5720 put_ctx(child_ctx);
5721}
5722
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005723static void perf_free_event(struct perf_event *event,
5724 struct perf_event_context *ctx)
5725{
5726 struct perf_event *parent = event->parent;
5727
5728 if (WARN_ON_ONCE(!parent))
5729 return;
5730
5731 mutex_lock(&parent->child_mutex);
5732 list_del_init(&event->child_list);
5733 mutex_unlock(&parent->child_mutex);
5734
5735 fput(parent->filp);
5736
Peter Zijlstra8a495422010-05-27 15:47:49 +02005737 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005738 list_del_event(event, ctx);
5739 free_event(event);
5740}
5741
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005742/*
5743 * free an unexposed, unused context as created by inheritance by
5744 * init_task below, used by fork() in case of fail.
5745 */
5746void perf_event_free_task(struct task_struct *task)
5747{
5748 struct perf_event_context *ctx = task->perf_event_ctxp;
5749 struct perf_event *event, *tmp;
5750
5751 if (!ctx)
5752 return;
5753
5754 mutex_lock(&ctx->mutex);
5755again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005756 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5757 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005758
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005759 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
5760 group_entry)
5761 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005762
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005763 if (!list_empty(&ctx->pinned_groups) ||
5764 !list_empty(&ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005765 goto again;
5766
5767 mutex_unlock(&ctx->mutex);
5768
5769 put_ctx(ctx);
5770}
5771
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005772static int
5773inherit_task_group(struct perf_event *event, struct task_struct *parent,
5774 struct perf_event_context *parent_ctx,
5775 struct task_struct *child,
5776 int *inherited_all)
5777{
5778 int ret;
5779 struct perf_event_context *child_ctx = child->perf_event_ctxp;
5780
5781 if (!event->attr.inherit) {
5782 *inherited_all = 0;
5783 return 0;
5784 }
5785
5786 if (!child_ctx) {
5787 /*
5788 * This is executed from the parent task context, so
5789 * inherit events that have been marked for cloning.
5790 * First allocate and initialize a context for the
5791 * child.
5792 */
5793
5794 child_ctx = kzalloc(sizeof(struct perf_event_context),
5795 GFP_KERNEL);
5796 if (!child_ctx)
5797 return -ENOMEM;
5798
5799 __perf_event_init_context(child_ctx, child);
5800 child->perf_event_ctxp = child_ctx;
5801 get_task_struct(child);
5802 }
5803
5804 ret = inherit_group(event, parent, parent_ctx,
5805 child, child_ctx);
5806
5807 if (ret)
5808 *inherited_all = 0;
5809
5810 return ret;
5811}
5812
5813
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005814/*
5815 * Initialize the perf_event context in task_struct
5816 */
5817int perf_event_init_task(struct task_struct *child)
5818{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005819 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005820 struct perf_event_context *cloned_ctx;
5821 struct perf_event *event;
5822 struct task_struct *parent = current;
5823 int inherited_all = 1;
5824 int ret = 0;
5825
5826 child->perf_event_ctxp = NULL;
5827
5828 mutex_init(&child->perf_event_mutex);
5829 INIT_LIST_HEAD(&child->perf_event_list);
5830
5831 if (likely(!parent->perf_event_ctxp))
5832 return 0;
5833
5834 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005835 * If the parent's context is a clone, pin it so it won't get
5836 * swapped under us.
5837 */
5838 parent_ctx = perf_pin_task_context(parent);
5839
5840 /*
5841 * No need to check if parent_ctx != NULL here; since we saw
5842 * it non-NULL earlier, the only reason for it to become NULL
5843 * is if we exit, and since we're currently in the middle of
5844 * a fork we can't be exiting at the same time.
5845 */
5846
5847 /*
5848 * Lock the parent list. No need to lock the child - not PID
5849 * hashed yet and not running, so nobody can access it.
5850 */
5851 mutex_lock(&parent_ctx->mutex);
5852
5853 /*
5854 * We dont have to disable NMIs - we are only looking at
5855 * the list, not manipulating it:
5856 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005857 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
5858 ret = inherit_task_group(event, parent, parent_ctx, child,
5859 &inherited_all);
5860 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005861 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005862 }
5863
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005864 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
5865 ret = inherit_task_group(event, parent, parent_ctx, child,
5866 &inherited_all);
5867 if (ret)
5868 break;
5869 }
5870
5871 child_ctx = child->perf_event_ctxp;
5872
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01005873 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005874 /*
5875 * Mark the child context as a clone of the parent
5876 * context, or of whatever the parent is a clone of.
5877 * Note that if the parent is a clone, it could get
5878 * uncloned at any point, but that doesn't matter
5879 * because the list of events and the generation
5880 * count can't have changed since we took the mutex.
5881 */
5882 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
5883 if (cloned_ctx) {
5884 child_ctx->parent_ctx = cloned_ctx;
5885 child_ctx->parent_gen = parent_ctx->parent_gen;
5886 } else {
5887 child_ctx->parent_ctx = parent_ctx;
5888 child_ctx->parent_gen = parent_ctx->generation;
5889 }
5890 get_ctx(child_ctx->parent_ctx);
5891 }
5892
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005893 mutex_unlock(&parent_ctx->mutex);
5894
5895 perf_unpin_context(parent_ctx);
5896
5897 return ret;
5898}
5899
Paul Mackerras220b1402010-03-10 20:45:52 +11005900static void __init perf_event_init_all_cpus(void)
5901{
5902 int cpu;
5903 struct perf_cpu_context *cpuctx;
5904
5905 for_each_possible_cpu(cpu) {
5906 cpuctx = &per_cpu(perf_cpu_context, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005907 mutex_init(&cpuctx->hlist_mutex);
Paul Mackerras220b1402010-03-10 20:45:52 +11005908 __perf_event_init_context(&cpuctx->ctx, NULL);
5909 }
5910}
5911
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005912static void __cpuinit perf_event_init_cpu(int cpu)
5913{
5914 struct perf_cpu_context *cpuctx;
5915
5916 cpuctx = &per_cpu(perf_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005917
5918 spin_lock(&perf_resource_lock);
5919 cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
5920 spin_unlock(&perf_resource_lock);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005921
5922 mutex_lock(&cpuctx->hlist_mutex);
5923 if (cpuctx->hlist_refcount > 0) {
5924 struct swevent_hlist *hlist;
5925
5926 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
5927 WARN_ON_ONCE(!hlist);
5928 rcu_assign_pointer(cpuctx->swevent_hlist, hlist);
5929 }
5930 mutex_unlock(&cpuctx->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005931}
5932
5933#ifdef CONFIG_HOTPLUG_CPU
5934static void __perf_event_exit_cpu(void *info)
5935{
5936 struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
5937 struct perf_event_context *ctx = &cpuctx->ctx;
5938 struct perf_event *event, *tmp;
5939
Frederic Weisbecker889ff012010-01-09 20:04:47 +01005940 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
5941 __perf_event_remove_from_context(event);
5942 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005943 __perf_event_remove_from_context(event);
5944}
5945static void perf_event_exit_cpu(int cpu)
5946{
5947 struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
5948 struct perf_event_context *ctx = &cpuctx->ctx;
5949
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005950 mutex_lock(&cpuctx->hlist_mutex);
5951 swevent_hlist_release(cpuctx);
5952 mutex_unlock(&cpuctx->hlist_mutex);
5953
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005954 mutex_lock(&ctx->mutex);
5955 smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
5956 mutex_unlock(&ctx->mutex);
5957}
5958#else
5959static inline void perf_event_exit_cpu(int cpu) { }
5960#endif
5961
5962static int __cpuinit
5963perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
5964{
5965 unsigned int cpu = (long)hcpu;
5966
Peter Zijlstra5e116372010-06-11 13:35:08 +02005967 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005968
5969 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02005970 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005971 perf_event_init_cpu(cpu);
5972 break;
5973
Peter Zijlstra5e116372010-06-11 13:35:08 +02005974 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005975 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005976 perf_event_exit_cpu(cpu);
5977 break;
5978
5979 default:
5980 break;
5981 }
5982
5983 return NOTIFY_OK;
5984}
5985
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005986void __init perf_event_init(void)
5987{
Paul Mackerras220b1402010-03-10 20:45:52 +11005988 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005989 init_srcu_struct(&pmus_srcu);
5990 perf_pmu_register(&perf_swevent);
5991 perf_pmu_register(&perf_cpu_clock);
5992 perf_pmu_register(&perf_task_clock);
5993 perf_tp_register();
5994 perf_cpu_notifier(perf_cpu_notify);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005995}
5996
Andi Kleenc9be0a32010-01-05 12:47:58 +01005997static ssize_t perf_show_reserve_percpu(struct sysdev_class *class,
5998 struct sysdev_class_attribute *attr,
5999 char *buf)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006000{
6001 return sprintf(buf, "%d\n", perf_reserved_percpu);
6002}
6003
6004static ssize_t
6005perf_set_reserve_percpu(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01006006 struct sysdev_class_attribute *attr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006007 const char *buf,
6008 size_t count)
6009{
6010 struct perf_cpu_context *cpuctx;
6011 unsigned long val;
6012 int err, cpu, mpt;
6013
6014 err = strict_strtoul(buf, 10, &val);
6015 if (err)
6016 return err;
6017 if (val > perf_max_events)
6018 return -EINVAL;
6019
6020 spin_lock(&perf_resource_lock);
6021 perf_reserved_percpu = val;
6022 for_each_online_cpu(cpu) {
6023 cpuctx = &per_cpu(perf_cpu_context, cpu);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006024 raw_spin_lock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006025 mpt = min(perf_max_events - cpuctx->ctx.nr_events,
6026 perf_max_events - perf_reserved_percpu);
6027 cpuctx->max_pertask = mpt;
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006028 raw_spin_unlock_irq(&cpuctx->ctx.lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006029 }
6030 spin_unlock(&perf_resource_lock);
6031
6032 return count;
6033}
6034
Andi Kleenc9be0a32010-01-05 12:47:58 +01006035static ssize_t perf_show_overcommit(struct sysdev_class *class,
6036 struct sysdev_class_attribute *attr,
6037 char *buf)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006038{
6039 return sprintf(buf, "%d\n", perf_overcommit);
6040}
6041
6042static ssize_t
Andi Kleenc9be0a32010-01-05 12:47:58 +01006043perf_set_overcommit(struct sysdev_class *class,
6044 struct sysdev_class_attribute *attr,
6045 const char *buf, size_t count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006046{
6047 unsigned long val;
6048 int err;
6049
6050 err = strict_strtoul(buf, 10, &val);
6051 if (err)
6052 return err;
6053 if (val > 1)
6054 return -EINVAL;
6055
6056 spin_lock(&perf_resource_lock);
6057 perf_overcommit = val;
6058 spin_unlock(&perf_resource_lock);
6059
6060 return count;
6061}
6062
6063static SYSDEV_CLASS_ATTR(
6064 reserve_percpu,
6065 0644,
6066 perf_show_reserve_percpu,
6067 perf_set_reserve_percpu
6068 );
6069
6070static SYSDEV_CLASS_ATTR(
6071 overcommit,
6072 0644,
6073 perf_show_overcommit,
6074 perf_set_overcommit
6075 );
6076
6077static struct attribute *perfclass_attrs[] = {
6078 &attr_reserve_percpu.attr,
6079 &attr_overcommit.attr,
6080 NULL
6081};
6082
6083static struct attribute_group perfclass_attr_group = {
6084 .attrs = perfclass_attrs,
6085 .name = "perf_events",
6086};
6087
6088static int __init perf_event_sysfs_init(void)
6089{
6090 return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
6091 &perfclass_attr_group);
6092}
6093device_initcall(perf_event_sysfs_init);