blob: a3d568fbacc60b8103830c2910c0f0302d2037c5 [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>
Peter Zijlstrac2774432010-12-08 15:29:02 +010024#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020025#include <linux/vmstat.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020026#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020027#include <linux/hardirq.h>
28#include <linux/rculist.h>
29#include <linux/uaccess.h>
30#include <linux/syscalls.h>
31#include <linux/anon_inodes.h>
32#include <linux/kernel_stat.h>
33#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080034#include <linux/ftrace_event.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050035#include <linux/hw_breakpoint.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020036
37#include <asm/irq_regs.h>
38
Peter Zijlstra82cd6de2010-10-14 17:57:23 +020039atomic_t perf_task_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020040static atomic_t nr_mmap_events __read_mostly;
41static atomic_t nr_comm_events __read_mostly;
42static atomic_t nr_task_events __read_mostly;
43
Peter Zijlstra108b02c2010-09-06 14:32:03 +020044static LIST_HEAD(pmus);
45static DEFINE_MUTEX(pmus_lock);
46static struct srcu_struct pmus_srcu;
47
Ingo Molnarcdd6c482009-09-21 12:02:48 +020048/*
49 * perf event paranoia level:
50 * -1 - not paranoid at all
51 * 0 - disallow raw tracepoint access for unpriv
52 * 1 - disallow cpu events for unpriv
53 * 2 - disallow kernel profiling for unpriv
54 */
55int sysctl_perf_event_paranoid __read_mostly = 1;
56
Ingo Molnarcdd6c482009-09-21 12:02:48 +020057int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
58
59/*
60 * max perf event sample rate
61 */
62int sysctl_perf_event_sample_rate __read_mostly = 100000;
63
64static atomic64_t perf_event_id;
65
Ingo Molnarcdd6c482009-09-21 12:02:48 +020066void __weak perf_event_print_debug(void) { }
67
Matt Fleming84c79912010-10-03 21:41:13 +010068extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020069{
Matt Fleming84c79912010-10-03 21:41:13 +010070 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +020071}
72
Peter Zijlstra33696fc2010-06-14 08:49:00 +020073void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020074{
Peter Zijlstra33696fc2010-06-14 08:49:00 +020075 int *count = this_cpu_ptr(pmu->pmu_disable_count);
76 if (!(*count)++)
77 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020078}
79
Peter Zijlstra33696fc2010-06-14 08:49:00 +020080void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020081{
Peter Zijlstra33696fc2010-06-14 08:49:00 +020082 int *count = this_cpu_ptr(pmu->pmu_disable_count);
83 if (!--(*count))
84 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020085}
86
Peter Zijlstrae9d2b062010-09-17 11:28:50 +020087static DEFINE_PER_CPU(struct list_head, rotation_list);
88
89/*
90 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
91 * because they're strictly cpu affine and rotate_start is called with IRQs
92 * disabled, while rotate_context is called from IRQ context.
93 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +020094static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +020095{
Peter Zijlstra108b02c2010-09-06 14:32:03 +020096 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +020097 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +020098
Peter Zijlstrae9d2b062010-09-17 11:28:50 +020099 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200100
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200101 if (list_empty(&cpuctx->rotation_list))
102 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200103}
104
105static void get_ctx(struct perf_event_context *ctx)
106{
107 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
108}
109
110static void free_ctx(struct rcu_head *head)
111{
112 struct perf_event_context *ctx;
113
114 ctx = container_of(head, struct perf_event_context, rcu_head);
115 kfree(ctx);
116}
117
118static void put_ctx(struct perf_event_context *ctx)
119{
120 if (atomic_dec_and_test(&ctx->refcount)) {
121 if (ctx->parent_ctx)
122 put_ctx(ctx->parent_ctx);
123 if (ctx->task)
124 put_task_struct(ctx->task);
125 call_rcu(&ctx->rcu_head, free_ctx);
126 }
127}
128
129static void unclone_ctx(struct perf_event_context *ctx)
130{
131 if (ctx->parent_ctx) {
132 put_ctx(ctx->parent_ctx);
133 ctx->parent_ctx = NULL;
134 }
135}
136
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200137static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
138{
139 /*
140 * only top level events have the pid namespace they were created in
141 */
142 if (event->parent)
143 event = event->parent;
144
145 return task_tgid_nr_ns(p, event->ns);
146}
147
148static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
149{
150 /*
151 * only top level events have the pid namespace they were created in
152 */
153 if (event->parent)
154 event = event->parent;
155
156 return task_pid_nr_ns(p, event->ns);
157}
158
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200159/*
160 * If we inherit events we want to return the parent event id
161 * to userspace.
162 */
163static u64 primary_event_id(struct perf_event *event)
164{
165 u64 id = event->id;
166
167 if (event->parent)
168 id = event->parent->id;
169
170 return id;
171}
172
173/*
174 * Get the perf_event_context for a task and lock it.
175 * This has to cope with with the fact that until it is locked,
176 * the context could get moved to another task.
177 */
178static struct perf_event_context *
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200179perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200180{
181 struct perf_event_context *ctx;
182
183 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200184retry:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200185 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200186 if (ctx) {
187 /*
188 * If this context is a clone of another, it might
189 * get swapped for another underneath us by
190 * perf_event_task_sched_out, though the
191 * rcu_read_lock() protects us from any context
192 * getting freed. Lock the context and check if it
193 * got swapped before we could get the lock, and retry
194 * if so. If we locked the right context, then it
195 * can't get swapped on us any more.
196 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100197 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200198 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100199 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200200 goto retry;
201 }
202
203 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100204 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200205 ctx = NULL;
206 }
207 }
208 rcu_read_unlock();
209 return ctx;
210}
211
212/*
213 * Get the context for a task and increment its pin_count so it
214 * can't get swapped to another task. This also increments its
215 * reference count so that the context can't get freed.
216 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200217static struct perf_event_context *
218perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200219{
220 struct perf_event_context *ctx;
221 unsigned long flags;
222
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +0200223 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200224 if (ctx) {
225 ++ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100226 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200227 }
228 return ctx;
229}
230
231static void perf_unpin_context(struct perf_event_context *ctx)
232{
233 unsigned long flags;
234
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100235 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200236 --ctx->pin_count;
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100237 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200238 put_ctx(ctx);
239}
240
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100241static inline u64 perf_clock(void)
242{
Peter Zijlstrac6763292010-05-25 10:48:51 +0200243 return local_clock();
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100244}
245
246/*
247 * Update the record of the current time in a context.
248 */
249static void update_context_time(struct perf_event_context *ctx)
250{
251 u64 now = perf_clock();
252
253 ctx->time += now - ctx->timestamp;
254 ctx->timestamp = now;
255}
256
257/*
258 * Update the total_time_enabled and total_time_running fields for a event.
259 */
260static void update_event_times(struct perf_event *event)
261{
262 struct perf_event_context *ctx = event->ctx;
263 u64 run_end;
264
265 if (event->state < PERF_EVENT_STATE_INACTIVE ||
266 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
267 return;
268
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100269 if (ctx->is_active)
270 run_end = ctx->time;
271 else
272 run_end = event->tstamp_stopped;
273
274 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100275
276 if (event->state == PERF_EVENT_STATE_INACTIVE)
277 run_end = event->tstamp_stopped;
278 else
279 run_end = ctx->time;
280
281 event->total_time_running = run_end - event->tstamp_running;
282}
283
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200284/*
285 * Update total_time_enabled and total_time_running for all events in a group.
286 */
287static void update_group_times(struct perf_event *leader)
288{
289 struct perf_event *event;
290
291 update_event_times(leader);
292 list_for_each_entry(event, &leader->sibling_list, group_entry)
293 update_event_times(event);
294}
295
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100296static struct list_head *
297ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
298{
299 if (event->attr.pinned)
300 return &ctx->pinned_groups;
301 else
302 return &ctx->flexible_groups;
303}
304
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200305/*
306 * Add a event from the lists for its context.
307 * Must be called with ctx->mutex and ctx->lock held.
308 */
309static void
310list_add_event(struct perf_event *event, struct perf_event_context *ctx)
311{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200312 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
313 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200314
315 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200316 * If we're a stand alone event or group leader, we go to the context
317 * list, group events are kept attached to the group so that
318 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200319 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200320 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100321 struct list_head *list;
322
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100323 if (is_software_event(event))
324 event->group_flags |= PERF_GROUP_SOFTWARE;
325
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100326 list = ctx_group_list(event, ctx);
327 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200328 }
329
330 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200331 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200332 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200333 ctx->nr_events++;
334 if (event->attr.inherit_stat)
335 ctx->nr_stat++;
336}
337
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200338/*
339 * Called at perf_event creation and when events are attached/detached from a
340 * group.
341 */
342static void perf_event__read_size(struct perf_event *event)
343{
344 int entry = sizeof(u64); /* value */
345 int size = 0;
346 int nr = 1;
347
348 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
349 size += sizeof(u64);
350
351 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
352 size += sizeof(u64);
353
354 if (event->attr.read_format & PERF_FORMAT_ID)
355 entry += sizeof(u64);
356
357 if (event->attr.read_format & PERF_FORMAT_GROUP) {
358 nr += event->group_leader->nr_siblings;
359 size += sizeof(u64);
360 }
361
362 size += entry * nr;
363 event->read_size = size;
364}
365
366static void perf_event__header_size(struct perf_event *event)
367{
368 struct perf_sample_data *data;
369 u64 sample_type = event->attr.sample_type;
370 u16 size = 0;
371
372 perf_event__read_size(event);
373
374 if (sample_type & PERF_SAMPLE_IP)
375 size += sizeof(data->ip);
376
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200377 if (sample_type & PERF_SAMPLE_ADDR)
378 size += sizeof(data->addr);
379
380 if (sample_type & PERF_SAMPLE_PERIOD)
381 size += sizeof(data->period);
382
383 if (sample_type & PERF_SAMPLE_READ)
384 size += event->read_size;
385
386 event->header_size = size;
387}
388
389static void perf_event__id_header_size(struct perf_event *event)
390{
391 struct perf_sample_data *data;
392 u64 sample_type = event->attr.sample_type;
393 u16 size = 0;
394
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200395 if (sample_type & PERF_SAMPLE_TID)
396 size += sizeof(data->tid_entry);
397
398 if (sample_type & PERF_SAMPLE_TIME)
399 size += sizeof(data->time);
400
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200401 if (sample_type & PERF_SAMPLE_ID)
402 size += sizeof(data->id);
403
404 if (sample_type & PERF_SAMPLE_STREAM_ID)
405 size += sizeof(data->stream_id);
406
407 if (sample_type & PERF_SAMPLE_CPU)
408 size += sizeof(data->cpu_entry);
409
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200410 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200411}
412
Peter Zijlstra8a495422010-05-27 15:47:49 +0200413static void perf_group_attach(struct perf_event *event)
414{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200415 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200416
Peter Zijlstra74c33372010-10-15 11:40:29 +0200417 /*
418 * We can have double attach due to group movement in perf_event_open.
419 */
420 if (event->attach_state & PERF_ATTACH_GROUP)
421 return;
422
Peter Zijlstra8a495422010-05-27 15:47:49 +0200423 event->attach_state |= PERF_ATTACH_GROUP;
424
425 if (group_leader == event)
426 return;
427
428 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
429 !is_software_event(event))
430 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
431
432 list_add_tail(&event->group_entry, &group_leader->sibling_list);
433 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200434
435 perf_event__header_size(group_leader);
436
437 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
438 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200439}
440
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200441/*
442 * Remove a event from the lists for its context.
443 * Must be called with ctx->mutex and ctx->lock held.
444 */
445static void
446list_del_event(struct perf_event *event, struct perf_event_context *ctx)
447{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200448 /*
449 * We can have double detach due to exit/hot-unplug + close.
450 */
451 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200452 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200453
454 event->attach_state &= ~PERF_ATTACH_CONTEXT;
455
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200456 ctx->nr_events--;
457 if (event->attr.inherit_stat)
458 ctx->nr_stat--;
459
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200460 list_del_rcu(&event->event_entry);
461
Peter Zijlstra8a495422010-05-27 15:47:49 +0200462 if (event->group_leader == event)
463 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200464
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200465 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800466
467 /*
468 * If event was in error state, then keep it
469 * that way, otherwise bogus counts will be
470 * returned on read(). The only way to get out
471 * of error state is by explicit re-enabling
472 * of the event
473 */
474 if (event->state > PERF_EVENT_STATE_OFF)
475 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200476}
477
Peter Zijlstra8a495422010-05-27 15:47:49 +0200478static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200479{
480 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200481 struct list_head *list = NULL;
482
483 /*
484 * We can have double detach due to exit/hot-unplug + close.
485 */
486 if (!(event->attach_state & PERF_ATTACH_GROUP))
487 return;
488
489 event->attach_state &= ~PERF_ATTACH_GROUP;
490
491 /*
492 * If this is a sibling, remove it from its group.
493 */
494 if (event->group_leader != event) {
495 list_del_init(&event->group_entry);
496 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200497 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200498 }
499
500 if (!list_empty(&event->group_entry))
501 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100502
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200503 /*
504 * If this was a group event with sibling events then
505 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +0200506 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200507 */
508 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +0200509 if (list)
510 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200511 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100512
513 /* Inherit group flags from the previous leader */
514 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200515 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200516
517out:
518 perf_event__header_size(event->group_leader);
519
520 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
521 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200522}
523
Stephane Eranianfa66f072010-08-26 16:40:01 +0200524static inline int
525event_filter_match(struct perf_event *event)
526{
527 return event->cpu == -1 || event->cpu == smp_processor_id();
528}
529
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200530static void
531event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200532 struct perf_cpu_context *cpuctx,
533 struct perf_event_context *ctx)
534{
Stephane Eranianfa66f072010-08-26 16:40:01 +0200535 u64 delta;
536 /*
537 * An event which could not be activated because of
538 * filter mismatch still needs to have its timings
539 * maintained, otherwise bogus information is return
540 * via read() for time_enabled, time_running:
541 */
542 if (event->state == PERF_EVENT_STATE_INACTIVE
543 && !event_filter_match(event)) {
544 delta = ctx->time - event->tstamp_stopped;
545 event->tstamp_running += delta;
546 event->tstamp_stopped = ctx->time;
547 }
548
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200549 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200550 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200551
552 event->state = PERF_EVENT_STATE_INACTIVE;
553 if (event->pending_disable) {
554 event->pending_disable = 0;
555 event->state = PERF_EVENT_STATE_OFF;
556 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200557 event->tstamp_stopped = ctx->time;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200558 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200559 event->oncpu = -1;
560
561 if (!is_software_event(event))
562 cpuctx->active_oncpu--;
563 ctx->nr_active--;
564 if (event->attr.exclusive || !cpuctx->active_oncpu)
565 cpuctx->exclusive = 0;
566}
567
568static void
569group_sched_out(struct perf_event *group_event,
570 struct perf_cpu_context *cpuctx,
571 struct perf_event_context *ctx)
572{
573 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +0200574 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200575
576 event_sched_out(group_event, cpuctx, ctx);
577
578 /*
579 * Schedule out siblings (if any):
580 */
581 list_for_each_entry(event, &group_event->sibling_list, group_entry)
582 event_sched_out(event, cpuctx, ctx);
583
Stephane Eranianfa66f072010-08-26 16:40:01 +0200584 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200585 cpuctx->exclusive = 0;
586}
587
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200588static inline struct perf_cpu_context *
589__get_cpu_context(struct perf_event_context *ctx)
590{
591 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
592}
593
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200594/*
595 * Cross CPU call to remove a performance event
596 *
597 * We disable the event on the hardware level first. After that we
598 * remove it from the context list.
599 */
600static void __perf_event_remove_from_context(void *info)
601{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200602 struct perf_event *event = info;
603 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200604 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200605
606 /*
607 * If this is a task context, we need to check whether it is
608 * the current task context of this cpu. If not it has been
609 * scheduled out before the smp call arrived.
610 */
611 if (ctx->task && cpuctx->task_ctx != ctx)
612 return;
613
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100614 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200615
616 event_sched_out(event, cpuctx, ctx);
617
618 list_del_event(event, ctx);
619
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100620 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200621}
622
623
624/*
625 * Remove the event from a task's (or a CPU's) list of events.
626 *
627 * Must be called with ctx->mutex held.
628 *
629 * CPU events are removed with a smp call. For task events we only
630 * call when the task is on a CPU.
631 *
632 * If event->ctx is a cloned context, callers must make sure that
633 * every task struct that event->ctx->task could possibly point to
634 * remains valid. This is OK when called from perf_release since
635 * that only calls us on the top-level context, which can't be a clone.
636 * When called from perf_event_exit_task, it's OK because the
637 * context has been detached from its task.
638 */
639static void perf_event_remove_from_context(struct perf_event *event)
640{
641 struct perf_event_context *ctx = event->ctx;
642 struct task_struct *task = ctx->task;
643
644 if (!task) {
645 /*
646 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200647 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200648 */
649 smp_call_function_single(event->cpu,
650 __perf_event_remove_from_context,
651 event, 1);
652 return;
653 }
654
655retry:
656 task_oncpu_function_call(task, __perf_event_remove_from_context,
657 event);
658
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100659 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200660 /*
661 * If the context is active we need to retry the smp call.
662 */
663 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100664 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200665 goto retry;
666 }
667
668 /*
669 * The lock prevents that this context is scheduled in so we
670 * can remove the event safely, if the call above did not
671 * succeed.
672 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100673 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200674 list_del_event(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100675 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200676}
677
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200678/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200679 * Cross CPU call to disable a performance event
680 */
681static void __perf_event_disable(void *info)
682{
683 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200684 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200685 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200686
687 /*
688 * If this is a per-task event, need to check whether this
689 * event's task is the current task on this cpu.
690 */
691 if (ctx->task && cpuctx->task_ctx != ctx)
692 return;
693
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100694 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200695
696 /*
697 * If the event is on, turn it off.
698 * If it is in error state, leave it in error state.
699 */
700 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
701 update_context_time(ctx);
702 update_group_times(event);
703 if (event == event->group_leader)
704 group_sched_out(event, cpuctx, ctx);
705 else
706 event_sched_out(event, cpuctx, ctx);
707 event->state = PERF_EVENT_STATE_OFF;
708 }
709
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100710 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200711}
712
713/*
714 * Disable a event.
715 *
716 * If event->ctx is a cloned context, callers must make sure that
717 * every task struct that event->ctx->task could possibly point to
718 * remains valid. This condition is satisifed when called through
719 * perf_event_for_each_child or perf_event_for_each because they
720 * hold the top-level event's child_mutex, so any descendant that
721 * goes to exit will block in sync_child_event.
722 * When called from perf_pending_event it's OK because event->ctx
723 * is the current context on this CPU and preemption is disabled,
724 * hence we can't get into perf_event_task_sched_out for this context.
725 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100726void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200727{
728 struct perf_event_context *ctx = event->ctx;
729 struct task_struct *task = ctx->task;
730
731 if (!task) {
732 /*
733 * Disable the event on the cpu that it's on
734 */
735 smp_call_function_single(event->cpu, __perf_event_disable,
736 event, 1);
737 return;
738 }
739
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200740retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200741 task_oncpu_function_call(task, __perf_event_disable, event);
742
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100743 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200744 /*
745 * If the event is still active, we need to retry the cross-call.
746 */
747 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100748 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200749 goto retry;
750 }
751
752 /*
753 * Since we have the lock this context can't be scheduled
754 * in, so we can change the state safely.
755 */
756 if (event->state == PERF_EVENT_STATE_INACTIVE) {
757 update_group_times(event);
758 event->state = PERF_EVENT_STATE_OFF;
759 }
760
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100761 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200762}
763
764static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200765event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200766 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100767 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200768{
769 if (event->state <= PERF_EVENT_STATE_OFF)
770 return 0;
771
772 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +0100773 event->oncpu = smp_processor_id();
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200774 /*
775 * The new state must be visible before we turn it on in the hardware:
776 */
777 smp_wmb();
778
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200779 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200780 event->state = PERF_EVENT_STATE_INACTIVE;
781 event->oncpu = -1;
782 return -EAGAIN;
783 }
784
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200785 event->tstamp_running += ctx->time - event->tstamp_stopped;
786
Stephane Eranianeed01522010-10-26 16:08:01 +0200787 event->shadow_ctx_time = ctx->time - ctx->timestamp;
788
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200789 if (!is_software_event(event))
790 cpuctx->active_oncpu++;
791 ctx->nr_active++;
792
793 if (event->attr.exclusive)
794 cpuctx->exclusive = 1;
795
796 return 0;
797}
798
799static int
800group_sched_in(struct perf_event *group_event,
801 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100802 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200803{
Lin Ming6bde9b62010-04-23 13:56:00 +0800804 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +0200805 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +0200806 u64 now = ctx->time;
807 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200808
809 if (group_event->state == PERF_EVENT_STATE_OFF)
810 return 0;
811
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200812 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +0800813
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200814 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200815 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200816 return -EAGAIN;
Stephane Eranian90151c352010-05-25 16:23:10 +0200817 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200818
819 /*
820 * Schedule in siblings as one group (if any):
821 */
822 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200823 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200824 partial_group = event;
825 goto group_error;
826 }
827 }
828
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200829 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +1000830 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200831
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200832group_error:
833 /*
834 * Groups can be scheduled in as one unit only, so undo any
835 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +0200836 * The events up to the failed event are scheduled out normally,
837 * tstamp_stopped will be updated.
838 *
839 * The failed events and the remaining siblings need to have
840 * their timings updated as if they had gone thru event_sched_in()
841 * and event_sched_out(). This is required to get consistent timings
842 * across the group. This also takes care of the case where the group
843 * could never be scheduled by ensuring tstamp_stopped is set to mark
844 * the time the event was actually stopped, such that time delta
845 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200846 */
847 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
848 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +0200849 simulate = true;
850
851 if (simulate) {
852 event->tstamp_running += now - event->tstamp_stopped;
853 event->tstamp_stopped = now;
854 } else {
855 event_sched_out(event, cpuctx, ctx);
856 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200857 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200858 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200859
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200860 pmu->cancel_txn(pmu);
Stephane Eranian90151c352010-05-25 16:23:10 +0200861
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200862 return -EAGAIN;
863}
864
865/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200866 * Work out whether we can put this event group on the CPU now.
867 */
868static int group_can_go_on(struct perf_event *event,
869 struct perf_cpu_context *cpuctx,
870 int can_add_hw)
871{
872 /*
873 * Groups consisting entirely of software events can always go on.
874 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100875 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200876 return 1;
877 /*
878 * If an exclusive group is already on, no other hardware
879 * events can go on.
880 */
881 if (cpuctx->exclusive)
882 return 0;
883 /*
884 * If this group is exclusive and there are already
885 * events on the CPU, it can't go on.
886 */
887 if (event->attr.exclusive && cpuctx->active_oncpu)
888 return 0;
889 /*
890 * Otherwise, try to add it if all previous groups were able
891 * to go on.
892 */
893 return can_add_hw;
894}
895
896static void add_event_to_ctx(struct perf_event *event,
897 struct perf_event_context *ctx)
898{
899 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200900 perf_group_attach(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200901 event->tstamp_enabled = ctx->time;
902 event->tstamp_running = ctx->time;
903 event->tstamp_stopped = ctx->time;
904}
905
906/*
907 * Cross CPU call to install and enable a performance event
908 *
909 * Must be called with ctx->mutex held
910 */
911static void __perf_install_in_context(void *info)
912{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200913 struct perf_event *event = info;
914 struct perf_event_context *ctx = event->ctx;
915 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200916 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200917 int err;
918
919 /*
920 * If this is a task context, we need to check whether it is
921 * the current task context of this cpu. If not it has been
922 * scheduled out before the smp call arrived.
923 * Or possibly this is the right context but it isn't
924 * on this cpu because it had no events.
925 */
926 if (ctx->task && cpuctx->task_ctx != ctx) {
927 if (cpuctx->task_ctx || ctx->task != current)
928 return;
929 cpuctx->task_ctx = ctx;
930 }
931
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100932 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200933 ctx->is_active = 1;
934 update_context_time(ctx);
935
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200936 add_event_to_ctx(event, ctx);
937
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100938 if (event->cpu != -1 && event->cpu != smp_processor_id())
939 goto unlock;
940
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200941 /*
942 * Don't put the event on if it is disabled or if
943 * it is in a group and the group isn't on.
944 */
945 if (event->state != PERF_EVENT_STATE_INACTIVE ||
946 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
947 goto unlock;
948
949 /*
950 * An exclusive event can't go on if there are already active
951 * hardware events, and no hardware event can go on if there
952 * is already an exclusive event on.
953 */
954 if (!group_can_go_on(event, cpuctx, 1))
955 err = -EEXIST;
956 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100957 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200958
959 if (err) {
960 /*
961 * This event couldn't go on. If it is in a group
962 * then we have to pull the whole group off.
963 * If the event group is pinned then put it in error state.
964 */
965 if (leader != event)
966 group_sched_out(leader, cpuctx, ctx);
967 if (leader->attr.pinned) {
968 update_group_times(leader);
969 leader->state = PERF_EVENT_STATE_ERROR;
970 }
971 }
972
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200973unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +0100974 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200975}
976
977/*
978 * Attach a performance event to a context
979 *
980 * First we add the event to the list with the hardware enable bit
981 * in event->hw_config cleared.
982 *
983 * If the event is attached to a task which is on a CPU we use a smp
984 * call to enable it in the task context. The task might have been
985 * scheduled away, but we check this in the smp call again.
986 *
987 * Must be called with ctx->mutex held.
988 */
989static void
990perf_install_in_context(struct perf_event_context *ctx,
991 struct perf_event *event,
992 int cpu)
993{
994 struct task_struct *task = ctx->task;
995
Peter Zijlstrac3f00c72010-08-18 14:37:15 +0200996 event->ctx = ctx;
997
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200998 if (!task) {
999 /*
1000 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001001 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001002 */
1003 smp_call_function_single(cpu, __perf_install_in_context,
1004 event, 1);
1005 return;
1006 }
1007
1008retry:
1009 task_oncpu_function_call(task, __perf_install_in_context,
1010 event);
1011
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001012 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001013 /*
1014 * we need to retry the smp call.
1015 */
1016 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001017 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001018 goto retry;
1019 }
1020
1021 /*
1022 * The lock prevents that this context is scheduled in so we
1023 * can add the event safely, if it the call above did not
1024 * succeed.
1025 */
1026 if (list_empty(&event->group_entry))
1027 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001028 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001029}
1030
1031/*
1032 * Put a event into inactive state and update time fields.
1033 * Enabling the leader of a group effectively enables all
1034 * the group members that aren't explicitly disabled, so we
1035 * have to update their ->tstamp_enabled also.
1036 * Note: this works for group members as well as group leaders
1037 * since the non-leader members' sibling_lists will be empty.
1038 */
1039static void __perf_event_mark_enabled(struct perf_event *event,
1040 struct perf_event_context *ctx)
1041{
1042 struct perf_event *sub;
1043
1044 event->state = PERF_EVENT_STATE_INACTIVE;
1045 event->tstamp_enabled = ctx->time - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001046 list_for_each_entry(sub, &event->sibling_list, group_entry) {
1047 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001048 sub->tstamp_enabled =
1049 ctx->time - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001050 }
1051 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001052}
1053
1054/*
1055 * Cross CPU call to enable a performance event
1056 */
1057static void __perf_event_enable(void *info)
1058{
1059 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001060 struct perf_event_context *ctx = event->ctx;
1061 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001062 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001063 int err;
1064
1065 /*
1066 * If this is a per-task event, need to check whether this
1067 * event's task is the current task on this cpu.
1068 */
1069 if (ctx->task && cpuctx->task_ctx != ctx) {
1070 if (cpuctx->task_ctx || ctx->task != current)
1071 return;
1072 cpuctx->task_ctx = ctx;
1073 }
1074
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001075 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001076 ctx->is_active = 1;
1077 update_context_time(ctx);
1078
1079 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1080 goto unlock;
1081 __perf_event_mark_enabled(event, ctx);
1082
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001083 if (event->cpu != -1 && event->cpu != smp_processor_id())
1084 goto unlock;
1085
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001086 /*
1087 * If the event is in a group and isn't the group leader,
1088 * then don't put it on unless the group is on.
1089 */
1090 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1091 goto unlock;
1092
1093 if (!group_can_go_on(event, cpuctx, 1)) {
1094 err = -EEXIST;
1095 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001096 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001097 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001098 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001099 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001100 }
1101
1102 if (err) {
1103 /*
1104 * If this event can't go on and it's part of a
1105 * group, then the whole group has to come off.
1106 */
1107 if (leader != event)
1108 group_sched_out(leader, cpuctx, ctx);
1109 if (leader->attr.pinned) {
1110 update_group_times(leader);
1111 leader->state = PERF_EVENT_STATE_ERROR;
1112 }
1113 }
1114
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001115unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001116 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001117}
1118
1119/*
1120 * Enable a event.
1121 *
1122 * If event->ctx is a cloned context, callers must make sure that
1123 * every task struct that event->ctx->task could possibly point to
1124 * remains valid. This condition is satisfied when called through
1125 * perf_event_for_each_child or perf_event_for_each as described
1126 * for perf_event_disable.
1127 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001128void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001129{
1130 struct perf_event_context *ctx = event->ctx;
1131 struct task_struct *task = ctx->task;
1132
1133 if (!task) {
1134 /*
1135 * Enable the event on the cpu that it's on
1136 */
1137 smp_call_function_single(event->cpu, __perf_event_enable,
1138 event, 1);
1139 return;
1140 }
1141
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001142 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001143 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1144 goto out;
1145
1146 /*
1147 * If the event is in error state, clear that first.
1148 * That way, if we see the event in error state below, we
1149 * know that it has gone back into error state, as distinct
1150 * from the task having been scheduled away before the
1151 * cross-call arrived.
1152 */
1153 if (event->state == PERF_EVENT_STATE_ERROR)
1154 event->state = PERF_EVENT_STATE_OFF;
1155
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001156retry:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001157 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001158 task_oncpu_function_call(task, __perf_event_enable, event);
1159
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001160 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001161
1162 /*
1163 * If the context is active and the event is still off,
1164 * we need to retry the cross-call.
1165 */
1166 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1167 goto retry;
1168
1169 /*
1170 * Since we have the lock this context can't be scheduled
1171 * in, so we can change the state safely.
1172 */
1173 if (event->state == PERF_EVENT_STATE_OFF)
1174 __perf_event_mark_enabled(event, ctx);
1175
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001176out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001177 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001178}
1179
1180static int perf_event_refresh(struct perf_event *event, int refresh)
1181{
1182 /*
1183 * not supported on inherited events
1184 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01001185 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001186 return -EINVAL;
1187
1188 atomic_add(refresh, &event->event_limit);
1189 perf_event_enable(event);
1190
1191 return 0;
1192}
1193
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001194enum event_type_t {
1195 EVENT_FLEXIBLE = 0x1,
1196 EVENT_PINNED = 0x2,
1197 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
1198};
1199
1200static void ctx_sched_out(struct perf_event_context *ctx,
1201 struct perf_cpu_context *cpuctx,
1202 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001203{
1204 struct perf_event *event;
1205
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001206 raw_spin_lock(&ctx->lock);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001207 perf_pmu_disable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001208 ctx->is_active = 0;
1209 if (likely(!ctx->nr_events))
1210 goto out;
1211 update_context_time(ctx);
1212
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001213 if (!ctx->nr_active)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001214 goto out;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001215
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001216 if (event_type & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001217 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1218 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001219 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001220
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001221 if (event_type & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001222 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001223 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001224 }
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001225out:
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001226 perf_pmu_enable(ctx->pmu);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001227 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001228}
1229
1230/*
1231 * Test whether two contexts are equivalent, i.e. whether they
1232 * have both been cloned from the same version of the same context
1233 * and they both have the same number of enabled events.
1234 * If the number of enabled events is the same, then the set
1235 * of enabled events should be the same, because these are both
1236 * inherited contexts, therefore we can't access individual events
1237 * in them directly with an fd; we can only enable/disable all
1238 * events via prctl, or enable/disable all events in a family
1239 * via ioctl, which will have the same effect on both contexts.
1240 */
1241static int context_equiv(struct perf_event_context *ctx1,
1242 struct perf_event_context *ctx2)
1243{
1244 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1245 && ctx1->parent_gen == ctx2->parent_gen
1246 && !ctx1->pin_count && !ctx2->pin_count;
1247}
1248
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001249static void __perf_event_sync_stat(struct perf_event *event,
1250 struct perf_event *next_event)
1251{
1252 u64 value;
1253
1254 if (!event->attr.inherit_stat)
1255 return;
1256
1257 /*
1258 * Update the event value, we cannot use perf_event_read()
1259 * because we're in the middle of a context switch and have IRQs
1260 * disabled, which upsets smp_call_function_single(), however
1261 * we know the event must be on the current CPU, therefore we
1262 * don't need to use it.
1263 */
1264 switch (event->state) {
1265 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001266 event->pmu->read(event);
1267 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001268
1269 case PERF_EVENT_STATE_INACTIVE:
1270 update_event_times(event);
1271 break;
1272
1273 default:
1274 break;
1275 }
1276
1277 /*
1278 * In order to keep per-task stats reliable we need to flip the event
1279 * values when we flip the contexts.
1280 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001281 value = local64_read(&next_event->count);
1282 value = local64_xchg(&event->count, value);
1283 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001284
1285 swap(event->total_time_enabled, next_event->total_time_enabled);
1286 swap(event->total_time_running, next_event->total_time_running);
1287
1288 /*
1289 * Since we swizzled the values, update the user visible data too.
1290 */
1291 perf_event_update_userpage(event);
1292 perf_event_update_userpage(next_event);
1293}
1294
1295#define list_next_entry(pos, member) \
1296 list_entry(pos->member.next, typeof(*pos), member)
1297
1298static void perf_event_sync_stat(struct perf_event_context *ctx,
1299 struct perf_event_context *next_ctx)
1300{
1301 struct perf_event *event, *next_event;
1302
1303 if (!ctx->nr_stat)
1304 return;
1305
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001306 update_context_time(ctx);
1307
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001308 event = list_first_entry(&ctx->event_list,
1309 struct perf_event, event_entry);
1310
1311 next_event = list_first_entry(&next_ctx->event_list,
1312 struct perf_event, event_entry);
1313
1314 while (&event->event_entry != &ctx->event_list &&
1315 &next_event->event_entry != &next_ctx->event_list) {
1316
1317 __perf_event_sync_stat(event, next_event);
1318
1319 event = list_next_entry(event, event_entry);
1320 next_event = list_next_entry(next_event, event_entry);
1321 }
1322}
1323
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001324void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1325 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001326{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001327 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001328 struct perf_event_context *next_ctx;
1329 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001330 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001331 int do_switch = 1;
1332
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001333 if (likely(!ctx))
1334 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001335
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001336 cpuctx = __get_cpu_context(ctx);
1337 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001338 return;
1339
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001340 rcu_read_lock();
1341 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001342 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001343 if (parent && next_ctx &&
1344 rcu_dereference(next_ctx->parent_ctx) == parent) {
1345 /*
1346 * Looks like the two contexts are clones, so we might be
1347 * able to optimize the context switch. We lock both
1348 * contexts and check that they are clones under the
1349 * lock (including re-checking that neither has been
1350 * uncloned in the meantime). It doesn't matter which
1351 * order we take the locks because no other cpu could
1352 * be trying to lock both of these tasks.
1353 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001354 raw_spin_lock(&ctx->lock);
1355 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001356 if (context_equiv(ctx, next_ctx)) {
1357 /*
1358 * XXX do we need a memory barrier of sorts
1359 * wrt to rcu_dereference() of perf_event_ctxp
1360 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001361 task->perf_event_ctxp[ctxn] = next_ctx;
1362 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001363 ctx->task = next;
1364 next_ctx->task = task;
1365 do_switch = 0;
1366
1367 perf_event_sync_stat(ctx, next_ctx);
1368 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001369 raw_spin_unlock(&next_ctx->lock);
1370 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001371 }
1372 rcu_read_unlock();
1373
1374 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001375 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001376 cpuctx->task_ctx = NULL;
1377 }
1378}
1379
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001380#define for_each_task_context_nr(ctxn) \
1381 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1382
1383/*
1384 * Called from scheduler to remove the events of the current task,
1385 * with interrupts disabled.
1386 *
1387 * We stop each event and update the event value in event->count.
1388 *
1389 * This does not protect us against NMI, but disable()
1390 * sets the disabled bit in the control field of event _before_
1391 * accessing the event control register. If a NMI hits, then it will
1392 * not restart the event.
1393 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001394void __perf_event_task_sched_out(struct task_struct *task,
1395 struct task_struct *next)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001396{
1397 int ctxn;
1398
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001399 for_each_task_context_nr(ctxn)
1400 perf_event_context_sched_out(task, ctxn, next);
1401}
1402
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001403static void task_ctx_sched_out(struct perf_event_context *ctx,
1404 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001405{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001406 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001407
1408 if (!cpuctx->task_ctx)
1409 return;
1410
1411 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1412 return;
1413
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001414 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001415 cpuctx->task_ctx = NULL;
1416}
1417
1418/*
1419 * Called with IRQs disabled
1420 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001421static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1422 enum event_type_t event_type)
1423{
1424 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001425}
1426
1427static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001428ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001429 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001430{
1431 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001432
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001433 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1434 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001435 continue;
Peter Zijlstra6e377382010-02-11 13:21:58 +01001436 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001437 continue;
1438
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001439 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001440 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001441
1442 /*
1443 * If this pinned group hasn't been scheduled,
1444 * put it in error state.
1445 */
1446 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1447 update_group_times(event);
1448 event->state = PERF_EVENT_STATE_ERROR;
1449 }
1450 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001451}
1452
1453static void
1454ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001455 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001456{
1457 struct perf_event *event;
1458 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001459
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001460 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1461 /* Ignore events in OFF or ERROR state */
1462 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001463 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001464 /*
1465 * Listen to the 'cpu' scheduling filter constraint
1466 * of events:
1467 */
Peter Zijlstra6e377382010-02-11 13:21:58 +01001468 if (event->cpu != -1 && event->cpu != smp_processor_id())
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001469 continue;
1470
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001471 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01001472 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001473 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001474 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001475 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001476}
1477
1478static void
1479ctx_sched_in(struct perf_event_context *ctx,
1480 struct perf_cpu_context *cpuctx,
1481 enum event_type_t event_type)
1482{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001483 raw_spin_lock(&ctx->lock);
1484 ctx->is_active = 1;
1485 if (likely(!ctx->nr_events))
1486 goto out;
1487
1488 ctx->timestamp = perf_clock();
1489
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001490 /*
1491 * First go through the list and put on any pinned groups
1492 * in order to give them the best chance of going on.
1493 */
1494 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001495 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001496
1497 /* Then walk through the lower prio flexible groups */
1498 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001499 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001500
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001501out:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001502 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001503}
1504
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001505static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1506 enum event_type_t event_type)
1507{
1508 struct perf_event_context *ctx = &cpuctx->ctx;
1509
1510 ctx_sched_in(ctx, cpuctx, event_type);
1511}
1512
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001513static void task_ctx_sched_in(struct perf_event_context *ctx,
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001514 enum event_type_t event_type)
1515{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001516 struct perf_cpu_context *cpuctx;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001517
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001518 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001519 if (cpuctx->task_ctx == ctx)
1520 return;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001521
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001522 ctx_sched_in(ctx, cpuctx, event_type);
1523 cpuctx->task_ctx = ctx;
1524}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001525
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001526void perf_event_context_sched_in(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001527{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001528 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001529
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001530 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001531 if (cpuctx->task_ctx == ctx)
1532 return;
1533
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001534 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001535 /*
1536 * We want to keep the following priority order:
1537 * cpu pinned (that don't need to move), task pinned,
1538 * cpu flexible, task flexible.
1539 */
1540 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1541
1542 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1543 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1544 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1545
1546 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08001547
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001548 /*
1549 * Since these rotations are per-cpu, we need to ensure the
1550 * cpu-context we got scheduled on is actually rotating.
1551 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001552 perf_pmu_rotate_start(ctx->pmu);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001553 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001554}
1555
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001556/*
1557 * Called from scheduler to add the events of the current task
1558 * with interrupts disabled.
1559 *
1560 * We restore the event value and then enable it.
1561 *
1562 * This does not protect us against NMI, but enable()
1563 * sets the enabled bit in the control field of event _before_
1564 * accessing the event control register. If a NMI hits, then it will
1565 * keep the event running.
1566 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001567void __perf_event_task_sched_in(struct task_struct *task)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001568{
1569 struct perf_event_context *ctx;
1570 int ctxn;
1571
1572 for_each_task_context_nr(ctxn) {
1573 ctx = task->perf_event_ctxp[ctxn];
1574 if (likely(!ctx))
1575 continue;
1576
1577 perf_event_context_sched_in(ctx);
1578 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001579}
1580
1581#define MAX_INTERRUPTS (~0ULL)
1582
1583static void perf_log_throttle(struct perf_event *event, int enable);
1584
Peter Zijlstraabd50712010-01-26 18:50:16 +01001585static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1586{
1587 u64 frequency = event->attr.sample_freq;
1588 u64 sec = NSEC_PER_SEC;
1589 u64 divisor, dividend;
1590
1591 int count_fls, nsec_fls, frequency_fls, sec_fls;
1592
1593 count_fls = fls64(count);
1594 nsec_fls = fls64(nsec);
1595 frequency_fls = fls64(frequency);
1596 sec_fls = 30;
1597
1598 /*
1599 * We got @count in @nsec, with a target of sample_freq HZ
1600 * the target period becomes:
1601 *
1602 * @count * 10^9
1603 * period = -------------------
1604 * @nsec * sample_freq
1605 *
1606 */
1607
1608 /*
1609 * Reduce accuracy by one bit such that @a and @b converge
1610 * to a similar magnitude.
1611 */
1612#define REDUCE_FLS(a, b) \
1613do { \
1614 if (a##_fls > b##_fls) { \
1615 a >>= 1; \
1616 a##_fls--; \
1617 } else { \
1618 b >>= 1; \
1619 b##_fls--; \
1620 } \
1621} while (0)
1622
1623 /*
1624 * Reduce accuracy until either term fits in a u64, then proceed with
1625 * the other, so that finally we can do a u64/u64 division.
1626 */
1627 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1628 REDUCE_FLS(nsec, frequency);
1629 REDUCE_FLS(sec, count);
1630 }
1631
1632 if (count_fls + sec_fls > 64) {
1633 divisor = nsec * frequency;
1634
1635 while (count_fls + sec_fls > 64) {
1636 REDUCE_FLS(count, sec);
1637 divisor >>= 1;
1638 }
1639
1640 dividend = count * sec;
1641 } else {
1642 dividend = count * sec;
1643
1644 while (nsec_fls + frequency_fls > 64) {
1645 REDUCE_FLS(nsec, frequency);
1646 dividend >>= 1;
1647 }
1648
1649 divisor = nsec * frequency;
1650 }
1651
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02001652 if (!divisor)
1653 return dividend;
1654
Peter Zijlstraabd50712010-01-26 18:50:16 +01001655 return div64_u64(dividend, divisor);
1656}
1657
1658static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001659{
1660 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91ad2010-06-04 15:18:01 +02001661 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001662 s64 delta;
1663
Peter Zijlstraabd50712010-01-26 18:50:16 +01001664 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001665
1666 delta = (s64)(period - hwc->sample_period);
1667 delta = (delta + 7) / 8; /* low pass filter */
1668
1669 sample_period = hwc->sample_period + delta;
1670
1671 if (!sample_period)
1672 sample_period = 1;
1673
1674 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001675
Peter Zijlstrae7850592010-05-21 14:43:08 +02001676 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001677 event->pmu->stop(event, PERF_EF_UPDATE);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001678 local64_set(&hwc->period_left, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001679 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001680 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001681}
1682
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001683static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001684{
1685 struct perf_event *event;
1686 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001687 u64 interrupts, now;
1688 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001689
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001690 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001691 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001692 if (event->state != PERF_EVENT_STATE_ACTIVE)
1693 continue;
1694
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001695 if (event->cpu != -1 && event->cpu != smp_processor_id())
1696 continue;
1697
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001698 hwc = &event->hw;
1699
1700 interrupts = hwc->interrupts;
1701 hwc->interrupts = 0;
1702
1703 /*
1704 * unthrottle events on the tick
1705 */
1706 if (interrupts == MAX_INTERRUPTS) {
1707 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001708 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001709 }
1710
1711 if (!event->attr.freq || !event->attr.sample_freq)
1712 continue;
1713
Peter Zijlstraabd50712010-01-26 18:50:16 +01001714 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001715 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001716 delta = now - hwc->freq_count_stamp;
1717 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001718
Peter Zijlstraabd50712010-01-26 18:50:16 +01001719 if (delta > 0)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001720 perf_adjust_period(event, period, delta);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001721 }
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001722 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001723}
1724
1725/*
1726 * Round-robin a context's events:
1727 */
1728static void rotate_ctx(struct perf_event_context *ctx)
1729{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001730 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001731
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01001732 /*
1733 * Rotate the first entry last of non-pinned groups. Rotation might be
1734 * disabled by the inheritance code.
1735 */
1736 if (!ctx->rotate_disable)
1737 list_rotate_left(&ctx->flexible_groups);
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001738
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001739 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001740}
1741
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001742/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001743 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1744 * because they're strictly cpu affine and rotate_start is called with IRQs
1745 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001746 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001747static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001748{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001749 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001750 struct perf_event_context *ctx = NULL;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001751 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001752
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001753 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001754 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001755 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1756 rotate = 1;
1757 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001758
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001759 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001760 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001761 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001762 if (ctx->nr_events != ctx->nr_active)
1763 rotate = 1;
1764 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001765
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001766 perf_pmu_disable(cpuctx->ctx.pmu);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001767 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001768 if (ctx)
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001769 perf_ctx_adjust_freq(ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001770
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001771 if (!rotate)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001772 goto done;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001773
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001774 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001775 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001776 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001777
1778 rotate_ctx(&cpuctx->ctx);
1779 if (ctx)
1780 rotate_ctx(ctx);
1781
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001782 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001783 if (ctx)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001784 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001785
1786done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001787 if (remove)
1788 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001789
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001790 perf_pmu_enable(cpuctx->ctx.pmu);
1791}
1792
1793void perf_event_task_tick(void)
1794{
1795 struct list_head *head = &__get_cpu_var(rotation_list);
1796 struct perf_cpu_context *cpuctx, *tmp;
1797
1798 WARN_ON(!irqs_disabled());
1799
1800 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
1801 if (cpuctx->jiffies_interval == 1 ||
1802 !(jiffies % cpuctx->jiffies_interval))
1803 perf_rotate_context(cpuctx);
1804 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001805}
1806
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001807static int event_enable_on_exec(struct perf_event *event,
1808 struct perf_event_context *ctx)
1809{
1810 if (!event->attr.enable_on_exec)
1811 return 0;
1812
1813 event->attr.enable_on_exec = 0;
1814 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1815 return 0;
1816
1817 __perf_event_mark_enabled(event, ctx);
1818
1819 return 1;
1820}
1821
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001822/*
1823 * Enable all of a task's events that have been marked enable-on-exec.
1824 * This expects task == current.
1825 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001826static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001827{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001828 struct perf_event *event;
1829 unsigned long flags;
1830 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001831 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001832
1833 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001834 if (!ctx || !ctx->nr_events)
1835 goto out;
1836
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001837 task_ctx_sched_out(ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001838
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001839 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001840
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001841 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1842 ret = event_enable_on_exec(event, ctx);
1843 if (ret)
1844 enabled = 1;
1845 }
1846
1847 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1848 ret = event_enable_on_exec(event, ctx);
1849 if (ret)
1850 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001851 }
1852
1853 /*
1854 * Unclone this context if we enabled any event.
1855 */
1856 if (enabled)
1857 unclone_ctx(ctx);
1858
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001859 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001860
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02001861 perf_event_context_sched_in(ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001862out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001863 local_irq_restore(flags);
1864}
1865
1866/*
1867 * Cross CPU call to read the hardware event
1868 */
1869static void __perf_event_read(void *info)
1870{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001871 struct perf_event *event = info;
1872 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001873 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001874
1875 /*
1876 * If this is a task context, we need to check whether it is
1877 * the current task context of this cpu. If not it has been
1878 * scheduled out before the smp call arrived. In that case
1879 * event->count would have been updated to a recent sample
1880 * when the event was scheduled out.
1881 */
1882 if (ctx->task && cpuctx->task_ctx != ctx)
1883 return;
1884
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001885 raw_spin_lock(&ctx->lock);
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001886 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001887 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001888 raw_spin_unlock(&ctx->lock);
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001889
Peter Zijlstra58e5ad12009-11-20 22:19:53 +01001890 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001891}
1892
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001893static inline u64 perf_event_count(struct perf_event *event)
1894{
Peter Zijlstrae7850592010-05-21 14:43:08 +02001895 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001896}
1897
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001898static u64 perf_event_read(struct perf_event *event)
1899{
1900 /*
1901 * If event is enabled and currently active on a CPU, update the
1902 * value in the event structure:
1903 */
1904 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1905 smp_call_function_single(event->oncpu,
1906 __perf_event_read, event, 1);
1907 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001908 struct perf_event_context *ctx = event->ctx;
1909 unsigned long flags;
1910
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001911 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02001912 /*
1913 * may read while context is not active
1914 * (e.g., thread is blocked), in that case
1915 * we cannot update context time
1916 */
1917 if (ctx->is_active)
1918 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001919 update_event_times(event);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01001920 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001921 }
1922
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001923 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001924}
1925
1926/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001927 * Callchain support
1928 */
1929
1930struct callchain_cpus_entries {
1931 struct rcu_head rcu_head;
1932 struct perf_callchain_entry *cpu_entries[0];
1933};
1934
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001935static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001936static atomic_t nr_callchain_events;
1937static DEFINE_MUTEX(callchain_mutex);
1938struct callchain_cpus_entries *callchain_cpus_entries;
1939
1940
1941__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1942 struct pt_regs *regs)
1943{
1944}
1945
1946__weak void perf_callchain_user(struct perf_callchain_entry *entry,
1947 struct pt_regs *regs)
1948{
1949}
1950
1951static void release_callchain_buffers_rcu(struct rcu_head *head)
1952{
1953 struct callchain_cpus_entries *entries;
1954 int cpu;
1955
1956 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1957
1958 for_each_possible_cpu(cpu)
1959 kfree(entries->cpu_entries[cpu]);
1960
1961 kfree(entries);
1962}
1963
1964static void release_callchain_buffers(void)
1965{
1966 struct callchain_cpus_entries *entries;
1967
1968 entries = callchain_cpus_entries;
1969 rcu_assign_pointer(callchain_cpus_entries, NULL);
1970 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
1971}
1972
1973static int alloc_callchain_buffers(void)
1974{
1975 int cpu;
1976 int size;
1977 struct callchain_cpus_entries *entries;
1978
1979 /*
1980 * We can't use the percpu allocation API for data that can be
1981 * accessed from NMI. Use a temporary manual per cpu allocation
1982 * until that gets sorted out.
1983 */
1984 size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
1985 num_possible_cpus();
1986
1987 entries = kzalloc(size, GFP_KERNEL);
1988 if (!entries)
1989 return -ENOMEM;
1990
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001991 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001992
1993 for_each_possible_cpu(cpu) {
1994 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
1995 cpu_to_node(cpu));
1996 if (!entries->cpu_entries[cpu])
1997 goto fail;
1998 }
1999
2000 rcu_assign_pointer(callchain_cpus_entries, entries);
2001
2002 return 0;
2003
2004fail:
2005 for_each_possible_cpu(cpu)
2006 kfree(entries->cpu_entries[cpu]);
2007 kfree(entries);
2008
2009 return -ENOMEM;
2010}
2011
2012static int get_callchain_buffers(void)
2013{
2014 int err = 0;
2015 int count;
2016
2017 mutex_lock(&callchain_mutex);
2018
2019 count = atomic_inc_return(&nr_callchain_events);
2020 if (WARN_ON_ONCE(count < 1)) {
2021 err = -EINVAL;
2022 goto exit;
2023 }
2024
2025 if (count > 1) {
2026 /* If the allocation failed, give up */
2027 if (!callchain_cpus_entries)
2028 err = -ENOMEM;
2029 goto exit;
2030 }
2031
2032 err = alloc_callchain_buffers();
2033 if (err)
2034 release_callchain_buffers();
2035exit:
2036 mutex_unlock(&callchain_mutex);
2037
2038 return err;
2039}
2040
2041static void put_callchain_buffers(void)
2042{
2043 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2044 release_callchain_buffers();
2045 mutex_unlock(&callchain_mutex);
2046 }
2047}
2048
2049static int get_recursion_context(int *recursion)
2050{
2051 int rctx;
2052
2053 if (in_nmi())
2054 rctx = 3;
2055 else if (in_irq())
2056 rctx = 2;
2057 else if (in_softirq())
2058 rctx = 1;
2059 else
2060 rctx = 0;
2061
2062 if (recursion[rctx])
2063 return -1;
2064
2065 recursion[rctx]++;
2066 barrier();
2067
2068 return rctx;
2069}
2070
2071static inline void put_recursion_context(int *recursion, int rctx)
2072{
2073 barrier();
2074 recursion[rctx]--;
2075}
2076
2077static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2078{
2079 int cpu;
2080 struct callchain_cpus_entries *entries;
2081
2082 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2083 if (*rctx == -1)
2084 return NULL;
2085
2086 entries = rcu_dereference(callchain_cpus_entries);
2087 if (!entries)
2088 return NULL;
2089
2090 cpu = smp_processor_id();
2091
2092 return &entries->cpu_entries[cpu][*rctx];
2093}
2094
2095static void
2096put_callchain_entry(int rctx)
2097{
2098 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2099}
2100
2101static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2102{
2103 int rctx;
2104 struct perf_callchain_entry *entry;
2105
2106
2107 entry = get_callchain_entry(&rctx);
2108 if (rctx == -1)
2109 return NULL;
2110
2111 if (!entry)
2112 goto exit_put;
2113
2114 entry->nr = 0;
2115
2116 if (!user_mode(regs)) {
2117 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2118 perf_callchain_kernel(entry, regs);
2119 if (current->mm)
2120 regs = task_pt_regs(current);
2121 else
2122 regs = NULL;
2123 }
2124
2125 if (regs) {
2126 perf_callchain_store(entry, PERF_CONTEXT_USER);
2127 perf_callchain_user(entry, regs);
2128 }
2129
2130exit_put:
2131 put_callchain_entry(rctx);
2132
2133 return entry;
2134}
2135
2136/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002137 * Initialize the perf_event context in a task_struct:
2138 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002139static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002140{
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002141 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002142 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002143 INIT_LIST_HEAD(&ctx->pinned_groups);
2144 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002145 INIT_LIST_HEAD(&ctx->event_list);
2146 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002147}
2148
Peter Zijlstraeb184472010-09-07 15:55:13 +02002149static struct perf_event_context *
2150alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002151{
2152 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002153
2154 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2155 if (!ctx)
2156 return NULL;
2157
2158 __perf_event_init_context(ctx);
2159 if (task) {
2160 ctx->task = task;
2161 get_task_struct(task);
2162 }
2163 ctx->pmu = pmu;
2164
2165 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002166}
2167
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002168static struct task_struct *
2169find_lively_task_by_vpid(pid_t vpid)
2170{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002171 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002172 int err;
2173
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002174 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002175 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002176 task = current;
2177 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002178 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002179 if (task)
2180 get_task_struct(task);
2181 rcu_read_unlock();
2182
2183 if (!task)
2184 return ERR_PTR(-ESRCH);
2185
2186 /*
2187 * Can't attach events to a dying task.
2188 */
2189 err = -ESRCH;
2190 if (task->flags & PF_EXITING)
2191 goto errout;
2192
2193 /* Reuse ptrace permission checks for now. */
2194 err = -EACCES;
2195 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2196 goto errout;
2197
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002198 return task;
2199errout:
2200 put_task_struct(task);
2201 return ERR_PTR(err);
2202
2203}
2204
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002205static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002206find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002207{
2208 struct perf_event_context *ctx;
2209 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002210 unsigned long flags;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002211 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002212
Matt Helsley38a81da2010-09-13 13:01:20 -07002213 if (!task && cpu != -1) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002214 /* Must be root to operate on a CPU event: */
2215 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2216 return ERR_PTR(-EACCES);
2217
2218 if (cpu < 0 || cpu >= nr_cpumask_bits)
2219 return ERR_PTR(-EINVAL);
2220
2221 /*
2222 * We could be clever and allow to attach a event to an
2223 * offline CPU and activate it when the CPU comes up, but
2224 * that's for later.
2225 */
2226 if (!cpu_online(cpu))
2227 return ERR_PTR(-ENODEV);
2228
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002229 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002230 ctx = &cpuctx->ctx;
2231 get_ctx(ctx);
2232
2233 return ctx;
2234 }
2235
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002236 err = -EINVAL;
2237 ctxn = pmu->task_ctx_nr;
2238 if (ctxn < 0)
2239 goto errout;
2240
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002241retry:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002242 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002243 if (ctx) {
2244 unclone_ctx(ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002245 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002246 }
2247
2248 if (!ctx) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002249 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002250 err = -ENOMEM;
2251 if (!ctx)
2252 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002253
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002254 get_ctx(ctx);
Peter Zijlstraeb184472010-09-07 15:55:13 +02002255
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02002256 if (cmpxchg(&task->perf_event_ctxp[ctxn], NULL, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002257 /*
2258 * We raced with some other task; use
2259 * the context they set.
2260 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002261 put_task_struct(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002262 kfree(ctx);
2263 goto retry;
2264 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002265 }
2266
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002267 return ctx;
2268
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002269errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002270 return ERR_PTR(err);
2271}
2272
Li Zefan6fb29152009-10-15 11:21:42 +08002273static void perf_event_free_filter(struct perf_event *event);
2274
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002275static void free_event_rcu(struct rcu_head *head)
2276{
2277 struct perf_event *event;
2278
2279 event = container_of(head, struct perf_event, rcu_head);
2280 if (event->ns)
2281 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002282 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002283 kfree(event);
2284}
2285
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002286static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002287
2288static void free_event(struct perf_event *event)
2289{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08002290 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002291
2292 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002293 if (event->attach_state & PERF_ATTACH_TASK)
2294 jump_label_dec(&perf_task_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002295 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002296 atomic_dec(&nr_mmap_events);
2297 if (event->attr.comm)
2298 atomic_dec(&nr_comm_events);
2299 if (event->attr.task)
2300 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002301 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2302 put_callchain_buffers();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002303 }
2304
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002305 if (event->buffer) {
2306 perf_buffer_put(event->buffer);
2307 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002308 }
2309
2310 if (event->destroy)
2311 event->destroy(event);
2312
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002313 if (event->ctx)
2314 put_ctx(event->ctx);
2315
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002316 call_rcu(&event->rcu_head, free_event_rcu);
2317}
2318
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002319int perf_event_release_kernel(struct perf_event *event)
2320{
2321 struct perf_event_context *ctx = event->ctx;
2322
Peter Zijlstra050735b2010-05-11 11:51:53 +02002323 /*
2324 * Remove from the PMU, can't get re-enabled since we got
2325 * here because the last ref went.
2326 */
2327 perf_event_disable(event);
2328
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002329 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002330 /*
2331 * There are two ways this annotation is useful:
2332 *
2333 * 1) there is a lock recursion from perf_event_exit_task
2334 * see the comment there.
2335 *
2336 * 2) there is a lock-inversion with mmap_sem through
2337 * perf_event_read_group(), which takes faults while
2338 * holding ctx->mutex, however this is called after
2339 * the last filedesc died, so there is no possibility
2340 * to trigger the AB-BA case.
2341 */
2342 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002343 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002344 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002345 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002346 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002347 mutex_unlock(&ctx->mutex);
2348
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002349 free_event(event);
2350
2351 return 0;
2352}
2353EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2354
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002355/*
2356 * Called when the last reference to the file is gone.
2357 */
2358static int perf_release(struct inode *inode, struct file *file)
2359{
2360 struct perf_event *event = file->private_data;
Peter Zijlstra88821352010-11-09 19:01:43 +01002361 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002362
2363 file->private_data = NULL;
2364
Peter Zijlstra88821352010-11-09 19:01:43 +01002365 rcu_read_lock();
2366 owner = ACCESS_ONCE(event->owner);
2367 /*
2368 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2369 * !owner it means the list deletion is complete and we can indeed
2370 * free this event, otherwise we need to serialize on
2371 * owner->perf_event_mutex.
2372 */
2373 smp_read_barrier_depends();
2374 if (owner) {
2375 /*
2376 * Since delayed_put_task_struct() also drops the last
2377 * task reference we can safely take a new reference
2378 * while holding the rcu_read_lock().
2379 */
2380 get_task_struct(owner);
2381 }
2382 rcu_read_unlock();
2383
2384 if (owner) {
2385 mutex_lock(&owner->perf_event_mutex);
2386 /*
2387 * We have to re-check the event->owner field, if it is cleared
2388 * we raced with perf_event_exit_task(), acquiring the mutex
2389 * ensured they're done, and we can proceed with freeing the
2390 * event.
2391 */
2392 if (event->owner)
2393 list_del_init(&event->owner_entry);
2394 mutex_unlock(&owner->perf_event_mutex);
2395 put_task_struct(owner);
2396 }
2397
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002398 return perf_event_release_kernel(event);
2399}
2400
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002401u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002402{
2403 struct perf_event *child;
2404 u64 total = 0;
2405
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002406 *enabled = 0;
2407 *running = 0;
2408
Peter Zijlstra6f105812009-11-20 22:19:56 +01002409 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002410 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002411 *enabled += event->total_time_enabled +
2412 atomic64_read(&event->child_total_time_enabled);
2413 *running += event->total_time_running +
2414 atomic64_read(&event->child_total_time_running);
2415
2416 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002417 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002418 *enabled += child->total_time_enabled;
2419 *running += child->total_time_running;
2420 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002421 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002422
2423 return total;
2424}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002425EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002426
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002427static int perf_event_read_group(struct perf_event *event,
2428 u64 read_format, char __user *buf)
2429{
2430 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01002431 int n = 0, size = 0, ret = -EFAULT;
2432 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002433 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002434 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002435
Peter Zijlstra6f105812009-11-20 22:19:56 +01002436 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002437 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002438
2439 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002440 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2441 values[n++] = enabled;
2442 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2443 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002444 values[n++] = count;
2445 if (read_format & PERF_FORMAT_ID)
2446 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002447
2448 size = n * sizeof(u64);
2449
2450 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01002451 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002452
Peter Zijlstra6f105812009-11-20 22:19:56 +01002453 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002454
2455 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01002456 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002457
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002458 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01002459 if (read_format & PERF_FORMAT_ID)
2460 values[n++] = primary_event_id(sub);
2461
2462 size = n * sizeof(u64);
2463
Stephane Eranian184d3da2009-11-23 21:40:49 -08002464 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002465 ret = -EFAULT;
2466 goto unlock;
2467 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002468
2469 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002470 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002471unlock:
2472 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002473
Peter Zijlstraabf48682009-11-20 22:19:49 +01002474 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002475}
2476
2477static int perf_event_read_one(struct perf_event *event,
2478 u64 read_format, char __user *buf)
2479{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002480 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002481 u64 values[4];
2482 int n = 0;
2483
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002484 values[n++] = perf_event_read_value(event, &enabled, &running);
2485 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2486 values[n++] = enabled;
2487 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2488 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002489 if (read_format & PERF_FORMAT_ID)
2490 values[n++] = primary_event_id(event);
2491
2492 if (copy_to_user(buf, values, n * sizeof(u64)))
2493 return -EFAULT;
2494
2495 return n * sizeof(u64);
2496}
2497
2498/*
2499 * Read the performance event - simple non blocking version for now
2500 */
2501static ssize_t
2502perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2503{
2504 u64 read_format = event->attr.read_format;
2505 int ret;
2506
2507 /*
2508 * Return end-of-file for a read on a event that is in
2509 * error state (i.e. because it was pinned but it couldn't be
2510 * scheduled on to the CPU at some point).
2511 */
2512 if (event->state == PERF_EVENT_STATE_ERROR)
2513 return 0;
2514
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02002515 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002516 return -ENOSPC;
2517
2518 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002519 if (read_format & PERF_FORMAT_GROUP)
2520 ret = perf_event_read_group(event, read_format, buf);
2521 else
2522 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002523
2524 return ret;
2525}
2526
2527static ssize_t
2528perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2529{
2530 struct perf_event *event = file->private_data;
2531
2532 return perf_read_hw(event, buf, count);
2533}
2534
2535static unsigned int perf_poll(struct file *file, poll_table *wait)
2536{
2537 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002538 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002539 unsigned int events = POLL_HUP;
2540
2541 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002542 buffer = rcu_dereference(event->buffer);
2543 if (buffer)
2544 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002545 rcu_read_unlock();
2546
2547 poll_wait(file, &event->waitq, wait);
2548
2549 return events;
2550}
2551
2552static void perf_event_reset(struct perf_event *event)
2553{
2554 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002555 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002556 perf_event_update_userpage(event);
2557}
2558
2559/*
2560 * Holding the top-level event's child_mutex means that any
2561 * descendant process that has inherited this event will block
2562 * in sync_child_event if it goes to exit, thus satisfying the
2563 * task existence requirements of perf_event_enable/disable.
2564 */
2565static void perf_event_for_each_child(struct perf_event *event,
2566 void (*func)(struct perf_event *))
2567{
2568 struct perf_event *child;
2569
2570 WARN_ON_ONCE(event->ctx->parent_ctx);
2571 mutex_lock(&event->child_mutex);
2572 func(event);
2573 list_for_each_entry(child, &event->child_list, child_list)
2574 func(child);
2575 mutex_unlock(&event->child_mutex);
2576}
2577
2578static void perf_event_for_each(struct perf_event *event,
2579 void (*func)(struct perf_event *))
2580{
2581 struct perf_event_context *ctx = event->ctx;
2582 struct perf_event *sibling;
2583
2584 WARN_ON_ONCE(ctx->parent_ctx);
2585 mutex_lock(&ctx->mutex);
2586 event = event->group_leader;
2587
2588 perf_event_for_each_child(event, func);
2589 func(event);
2590 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2591 perf_event_for_each_child(event, func);
2592 mutex_unlock(&ctx->mutex);
2593}
2594
2595static int perf_event_period(struct perf_event *event, u64 __user *arg)
2596{
2597 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002598 int ret = 0;
2599 u64 value;
2600
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01002601 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002602 return -EINVAL;
2603
John Blackwoodad0cf342010-09-28 18:03:11 -04002604 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002605 return -EFAULT;
2606
2607 if (!value)
2608 return -EINVAL;
2609
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002610 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002611 if (event->attr.freq) {
2612 if (value > sysctl_perf_event_sample_rate) {
2613 ret = -EINVAL;
2614 goto unlock;
2615 }
2616
2617 event->attr.sample_freq = value;
2618 } else {
2619 event->attr.sample_period = value;
2620 event->hw.sample_period = value;
2621 }
2622unlock:
Thomas Gleixnere625cce12009-11-17 18:02:06 +01002623 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002624
2625 return ret;
2626}
2627
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002628static const struct file_operations perf_fops;
2629
2630static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2631{
2632 struct file *file;
2633
2634 file = fget_light(fd, fput_needed);
2635 if (!file)
2636 return ERR_PTR(-EBADF);
2637
2638 if (file->f_op != &perf_fops) {
2639 fput_light(file, *fput_needed);
2640 *fput_needed = 0;
2641 return ERR_PTR(-EBADF);
2642 }
2643
2644 return file->private_data;
2645}
2646
2647static int perf_event_set_output(struct perf_event *event,
2648 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08002649static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002650
2651static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2652{
2653 struct perf_event *event = file->private_data;
2654 void (*func)(struct perf_event *);
2655 u32 flags = arg;
2656
2657 switch (cmd) {
2658 case PERF_EVENT_IOC_ENABLE:
2659 func = perf_event_enable;
2660 break;
2661 case PERF_EVENT_IOC_DISABLE:
2662 func = perf_event_disable;
2663 break;
2664 case PERF_EVENT_IOC_RESET:
2665 func = perf_event_reset;
2666 break;
2667
2668 case PERF_EVENT_IOC_REFRESH:
2669 return perf_event_refresh(event, arg);
2670
2671 case PERF_EVENT_IOC_PERIOD:
2672 return perf_event_period(event, (u64 __user *)arg);
2673
2674 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002675 {
2676 struct perf_event *output_event = NULL;
2677 int fput_needed = 0;
2678 int ret;
2679
2680 if (arg != -1) {
2681 output_event = perf_fget_light(arg, &fput_needed);
2682 if (IS_ERR(output_event))
2683 return PTR_ERR(output_event);
2684 }
2685
2686 ret = perf_event_set_output(event, output_event);
2687 if (output_event)
2688 fput_light(output_event->filp, fput_needed);
2689
2690 return ret;
2691 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002692
Li Zefan6fb29152009-10-15 11:21:42 +08002693 case PERF_EVENT_IOC_SET_FILTER:
2694 return perf_event_set_filter(event, (void __user *)arg);
2695
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002696 default:
2697 return -ENOTTY;
2698 }
2699
2700 if (flags & PERF_IOC_FLAG_GROUP)
2701 perf_event_for_each(event, func);
2702 else
2703 perf_event_for_each_child(event, func);
2704
2705 return 0;
2706}
2707
2708int perf_event_task_enable(void)
2709{
2710 struct perf_event *event;
2711
2712 mutex_lock(&current->perf_event_mutex);
2713 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2714 perf_event_for_each_child(event, perf_event_enable);
2715 mutex_unlock(&current->perf_event_mutex);
2716
2717 return 0;
2718}
2719
2720int perf_event_task_disable(void)
2721{
2722 struct perf_event *event;
2723
2724 mutex_lock(&current->perf_event_mutex);
2725 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2726 perf_event_for_each_child(event, perf_event_disable);
2727 mutex_unlock(&current->perf_event_mutex);
2728
2729 return 0;
2730}
2731
2732#ifndef PERF_EVENT_INDEX_OFFSET
2733# define PERF_EVENT_INDEX_OFFSET 0
2734#endif
2735
2736static int perf_event_index(struct perf_event *event)
2737{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002738 if (event->hw.state & PERF_HES_STOPPED)
2739 return 0;
2740
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002741 if (event->state != PERF_EVENT_STATE_ACTIVE)
2742 return 0;
2743
2744 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2745}
2746
2747/*
2748 * Callers need to ensure there can be no nesting of this function, otherwise
2749 * the seqlock logic goes bad. We can not serialize this because the arch
2750 * code calls this from NMI context.
2751 */
2752void perf_event_update_userpage(struct perf_event *event)
2753{
2754 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002755 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002756
2757 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002758 buffer = rcu_dereference(event->buffer);
2759 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002760 goto unlock;
2761
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002762 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002763
2764 /*
2765 * Disable preemption so as to not let the corresponding user-space
2766 * spin too long if we get preempted.
2767 */
2768 preempt_disable();
2769 ++userpg->lock;
2770 barrier();
2771 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002772 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002773 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02002774 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002775
2776 userpg->time_enabled = event->total_time_enabled +
2777 atomic64_read(&event->child_total_time_enabled);
2778
2779 userpg->time_running = event->total_time_running +
2780 atomic64_read(&event->child_total_time_running);
2781
2782 barrier();
2783 ++userpg->lock;
2784 preempt_enable();
2785unlock:
2786 rcu_read_unlock();
2787}
2788
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002789static unsigned long perf_data_size(struct perf_buffer *buffer);
2790
2791static void
2792perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2793{
2794 long max_size = perf_data_size(buffer);
2795
2796 if (watermark)
2797 buffer->watermark = min(max_size, watermark);
2798
2799 if (!buffer->watermark)
2800 buffer->watermark = max_size / 2;
2801
2802 if (flags & PERF_BUFFER_WRITABLE)
2803 buffer->writable = 1;
2804
2805 atomic_set(&buffer->refcount, 1);
2806}
2807
Peter Zijlstra906010b2009-09-21 16:08:49 +02002808#ifndef CONFIG_PERF_USE_VMALLOC
2809
2810/*
2811 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2812 */
2813
2814static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002815perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002816{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002817 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002818 return NULL;
2819
2820 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002821 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002822
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002823 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002824}
2825
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02002826static void *perf_mmap_alloc_page(int cpu)
2827{
2828 struct page *page;
2829 int node;
2830
2831 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2832 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2833 if (!page)
2834 return NULL;
2835
2836 return page_address(page);
2837}
2838
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002839static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002840perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002841{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002842 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002843 unsigned long size;
2844 int i;
2845
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002846 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002847 size += nr_pages * sizeof(void *);
2848
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002849 buffer = kzalloc(size, GFP_KERNEL);
2850 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002851 goto fail;
2852
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002853 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002854 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002855 goto fail_user_page;
2856
2857 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002858 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002859 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002860 goto fail_data_pages;
2861 }
2862
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002863 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002864
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002865 perf_buffer_init(buffer, watermark, flags);
2866
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002867 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002868
2869fail_data_pages:
2870 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002871 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002872
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002873 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002874
2875fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002876 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002877
2878fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002879 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002880}
2881
2882static void perf_mmap_free_page(unsigned long addr)
2883{
2884 struct page *page = virt_to_page((void *)addr);
2885
2886 page->mapping = NULL;
2887 __free_page(page);
2888}
2889
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002890static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002891{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002892 int i;
2893
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002894 perf_mmap_free_page((unsigned long)buffer->user_page);
2895 for (i = 0; i < buffer->nr_pages; i++)
2896 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2897 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002898}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002899
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002900static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002901{
2902 return 0;
2903}
2904
Peter Zijlstra906010b2009-09-21 16:08:49 +02002905#else
2906
2907/*
2908 * Back perf_mmap() with vmalloc memory.
2909 *
2910 * Required for architectures that have d-cache aliasing issues.
2911 */
2912
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002913static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002914{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002915 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002916}
2917
Peter Zijlstra906010b2009-09-21 16:08:49 +02002918static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002919perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002920{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002921 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02002922 return NULL;
2923
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002924 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002925}
2926
2927static void perf_mmap_unmark_page(void *addr)
2928{
2929 struct page *page = vmalloc_to_page(addr);
2930
2931 page->mapping = NULL;
2932}
2933
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002934static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002935{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002936 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002937 void *base;
2938 int i, nr;
2939
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002940 buffer = container_of(work, struct perf_buffer, work);
2941 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002942
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002943 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002944 for (i = 0; i < nr + 1; i++)
2945 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2946
2947 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002948 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002949}
2950
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002951static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002952{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002953 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002954}
2955
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002956static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002957perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002958{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002959 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002960 unsigned long size;
2961 void *all_buf;
2962
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002963 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002964 size += sizeof(void *);
2965
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002966 buffer = kzalloc(size, GFP_KERNEL);
2967 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002968 goto fail;
2969
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002970 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002971
2972 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
2973 if (!all_buf)
2974 goto fail_all_buf;
2975
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002976 buffer->user_page = all_buf;
2977 buffer->data_pages[0] = all_buf + PAGE_SIZE;
2978 buffer->page_order = ilog2(nr_pages);
2979 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002980
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002981 perf_buffer_init(buffer, watermark, flags);
2982
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002983 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002984
2985fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002986 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002987
2988fail:
2989 return NULL;
2990}
2991
2992#endif
2993
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002994static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002995{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002996 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002997}
2998
Peter Zijlstra906010b2009-09-21 16:08:49 +02002999static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3000{
3001 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003002 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003003 int ret = VM_FAULT_SIGBUS;
3004
3005 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3006 if (vmf->pgoff == 0)
3007 ret = 0;
3008 return ret;
3009 }
3010
3011 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003012 buffer = rcu_dereference(event->buffer);
3013 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003014 goto unlock;
3015
3016 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3017 goto unlock;
3018
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003019 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003020 if (!vmf->page)
3021 goto unlock;
3022
3023 get_page(vmf->page);
3024 vmf->page->mapping = vma->vm_file->f_mapping;
3025 vmf->page->index = vmf->pgoff;
3026
3027 ret = 0;
3028unlock:
3029 rcu_read_unlock();
3030
3031 return ret;
3032}
3033
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003034static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003035{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003036 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003037
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003038 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3039 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003040}
3041
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003042static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003043{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003044 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003045
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003046 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003047 buffer = rcu_dereference(event->buffer);
3048 if (buffer) {
3049 if (!atomic_inc_not_zero(&buffer->refcount))
3050 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003051 }
3052 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003053
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003054 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003055}
3056
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003057static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003058{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003059 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003060 return;
3061
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003062 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003063}
3064
3065static void perf_mmap_open(struct vm_area_struct *vma)
3066{
3067 struct perf_event *event = vma->vm_file->private_data;
3068
3069 atomic_inc(&event->mmap_count);
3070}
3071
3072static void perf_mmap_close(struct vm_area_struct *vma)
3073{
3074 struct perf_event *event = vma->vm_file->private_data;
3075
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003076 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003077 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003078 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003079 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003080
Peter Zijlstra906010b2009-09-21 16:08:49 +02003081 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003082 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003083 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003084 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003085
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003086 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003087 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003088 }
3089}
3090
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003091static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003092 .open = perf_mmap_open,
3093 .close = perf_mmap_close,
3094 .fault = perf_mmap_fault,
3095 .page_mkwrite = perf_mmap_fault,
3096};
3097
3098static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3099{
3100 struct perf_event *event = file->private_data;
3101 unsigned long user_locked, user_lock_limit;
3102 struct user_struct *user = current_user();
3103 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003104 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003105 unsigned long vma_size;
3106 unsigned long nr_pages;
3107 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003108 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003109
Peter Zijlstrac7920612010-05-18 10:33:24 +02003110 /*
3111 * Don't allow mmap() of inherited per-task counters. This would
3112 * create a performance issue due to all children writing to the
3113 * same buffer.
3114 */
3115 if (event->cpu == -1 && event->attr.inherit)
3116 return -EINVAL;
3117
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003118 if (!(vma->vm_flags & VM_SHARED))
3119 return -EINVAL;
3120
3121 vma_size = vma->vm_end - vma->vm_start;
3122 nr_pages = (vma_size / PAGE_SIZE) - 1;
3123
3124 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003125 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003126 * can do bitmasks instead of modulo.
3127 */
3128 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3129 return -EINVAL;
3130
3131 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3132 return -EINVAL;
3133
3134 if (vma->vm_pgoff != 0)
3135 return -EINVAL;
3136
3137 WARN_ON_ONCE(event->ctx->parent_ctx);
3138 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003139 if (event->buffer) {
3140 if (event->buffer->nr_pages == nr_pages)
3141 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003142 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003143 ret = -EINVAL;
3144 goto unlock;
3145 }
3146
3147 user_extra = nr_pages + 1;
3148 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3149
3150 /*
3151 * Increase the limit linearly with more CPUs:
3152 */
3153 user_lock_limit *= num_online_cpus();
3154
3155 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3156
3157 extra = 0;
3158 if (user_locked > user_lock_limit)
3159 extra = user_locked - user_lock_limit;
3160
Jiri Slaby78d7d402010-03-05 13:42:54 -08003161 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003162 lock_limit >>= PAGE_SHIFT;
3163 locked = vma->vm_mm->locked_vm + extra;
3164
3165 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3166 !capable(CAP_IPC_LOCK)) {
3167 ret = -EPERM;
3168 goto unlock;
3169 }
3170
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003171 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003172
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003173 if (vma->vm_flags & VM_WRITE)
3174 flags |= PERF_BUFFER_WRITABLE;
3175
3176 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3177 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003178 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003179 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003180 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003181 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003182 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003183
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003184 atomic_long_add(user_extra, &user->locked_vm);
3185 event->mmap_locked = extra;
3186 event->mmap_user = get_current_user();
3187 vma->vm_mm->locked_vm += event->mmap_locked;
3188
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003189unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003190 if (!ret)
3191 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003192 mutex_unlock(&event->mmap_mutex);
3193
3194 vma->vm_flags |= VM_RESERVED;
3195 vma->vm_ops = &perf_mmap_vmops;
3196
3197 return ret;
3198}
3199
3200static int perf_fasync(int fd, struct file *filp, int on)
3201{
3202 struct inode *inode = filp->f_path.dentry->d_inode;
3203 struct perf_event *event = filp->private_data;
3204 int retval;
3205
3206 mutex_lock(&inode->i_mutex);
3207 retval = fasync_helper(fd, filp, on, &event->fasync);
3208 mutex_unlock(&inode->i_mutex);
3209
3210 if (retval < 0)
3211 return retval;
3212
3213 return 0;
3214}
3215
3216static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003217 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003218 .release = perf_release,
3219 .read = perf_read,
3220 .poll = perf_poll,
3221 .unlocked_ioctl = perf_ioctl,
3222 .compat_ioctl = perf_ioctl,
3223 .mmap = perf_mmap,
3224 .fasync = perf_fasync,
3225};
3226
3227/*
3228 * Perf event wakeup
3229 *
3230 * If there's data, ensure we set the poll() state and publish everything
3231 * to user-space before waking everybody up.
3232 */
3233
3234void perf_event_wakeup(struct perf_event *event)
3235{
3236 wake_up_all(&event->waitq);
3237
3238 if (event->pending_kill) {
3239 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3240 event->pending_kill = 0;
3241 }
3242}
3243
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003244static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003245{
3246 struct perf_event *event = container_of(entry,
3247 struct perf_event, pending);
3248
3249 if (event->pending_disable) {
3250 event->pending_disable = 0;
3251 __perf_event_disable(event);
3252 }
3253
3254 if (event->pending_wakeup) {
3255 event->pending_wakeup = 0;
3256 perf_event_wakeup(event);
3257 }
3258}
3259
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003260/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003261 * We assume there is only KVM supporting the callbacks.
3262 * Later on, we might change it to a list if there is
3263 * another virtualization implementation supporting the callbacks.
3264 */
3265struct perf_guest_info_callbacks *perf_guest_cbs;
3266
3267int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3268{
3269 perf_guest_cbs = cbs;
3270 return 0;
3271}
3272EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3273
3274int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3275{
3276 perf_guest_cbs = NULL;
3277 return 0;
3278}
3279EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3280
3281/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003282 * Output
3283 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003284static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003285 unsigned long offset, unsigned long head)
3286{
3287 unsigned long mask;
3288
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003289 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003290 return true;
3291
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003292 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003293
3294 offset = (offset - tail) & mask;
3295 head = (head - tail) & mask;
3296
3297 if ((int)(head - offset) < 0)
3298 return false;
3299
3300 return true;
3301}
3302
3303static void perf_output_wakeup(struct perf_output_handle *handle)
3304{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003305 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003306
3307 if (handle->nmi) {
3308 handle->event->pending_wakeup = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003309 irq_work_queue(&handle->event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003310 } else
3311 perf_event_wakeup(handle->event);
3312}
3313
3314/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003315 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003316 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003317 * cannot fully serialize things.
3318 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003319 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003320 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003321 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003322static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003323{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003324 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003325
Peter Zijlstraef607772010-05-18 10:50:41 +02003326 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003327 local_inc(&buffer->nest);
3328 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003329}
3330
Peter Zijlstraef607772010-05-18 10:50:41 +02003331static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003332{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003333 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003334 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003335
3336again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003337 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003338
3339 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003340 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003341 */
3342
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003343 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003344 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003345
3346 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003347 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003348 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003349 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003350 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003351 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003352
Peter Zijlstraef607772010-05-18 10:50:41 +02003353 /*
3354 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003355 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003356 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003357 if (unlikely(head != local_read(&buffer->head))) {
3358 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003359 goto again;
3360 }
3361
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003362 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003363 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003364
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003365out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003366 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003367}
3368
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003369__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003370 const void *buf, unsigned int len)
3371{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003372 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003373 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003374
3375 memcpy(handle->addr, buf, size);
3376
3377 len -= size;
3378 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003379 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003380 handle->size -= size;
3381 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003382 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003383
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003384 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003385 handle->page &= buffer->nr_pages - 1;
3386 handle->addr = buffer->data_pages[handle->page];
3387 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003388 }
3389 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003390}
3391
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003392static void __perf_event_header__init_id(struct perf_event_header *header,
3393 struct perf_sample_data *data,
3394 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02003395{
3396 u64 sample_type = event->attr.sample_type;
3397
3398 data->type = sample_type;
3399 header->size += event->id_header_size;
3400
3401 if (sample_type & PERF_SAMPLE_TID) {
3402 /* namespace issues */
3403 data->tid_entry.pid = perf_event_pid(event, current);
3404 data->tid_entry.tid = perf_event_tid(event, current);
3405 }
3406
3407 if (sample_type & PERF_SAMPLE_TIME)
3408 data->time = perf_clock();
3409
3410 if (sample_type & PERF_SAMPLE_ID)
3411 data->id = primary_event_id(event);
3412
3413 if (sample_type & PERF_SAMPLE_STREAM_ID)
3414 data->stream_id = event->id;
3415
3416 if (sample_type & PERF_SAMPLE_CPU) {
3417 data->cpu_entry.cpu = raw_smp_processor_id();
3418 data->cpu_entry.reserved = 0;
3419 }
3420}
3421
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003422static void perf_event_header__init_id(struct perf_event_header *header,
3423 struct perf_sample_data *data,
3424 struct perf_event *event)
3425{
3426 if (event->attr.sample_id_all)
3427 __perf_event_header__init_id(header, data, event);
3428}
3429
3430static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3431 struct perf_sample_data *data)
3432{
3433 u64 sample_type = data->type;
3434
3435 if (sample_type & PERF_SAMPLE_TID)
3436 perf_output_put(handle, data->tid_entry);
3437
3438 if (sample_type & PERF_SAMPLE_TIME)
3439 perf_output_put(handle, data->time);
3440
3441 if (sample_type & PERF_SAMPLE_ID)
3442 perf_output_put(handle, data->id);
3443
3444 if (sample_type & PERF_SAMPLE_STREAM_ID)
3445 perf_output_put(handle, data->stream_id);
3446
3447 if (sample_type & PERF_SAMPLE_CPU)
3448 perf_output_put(handle, data->cpu_entry);
3449}
3450
3451static void perf_event__output_id_sample(struct perf_event *event,
3452 struct perf_output_handle *handle,
3453 struct perf_sample_data *sample)
3454{
3455 if (event->attr.sample_id_all)
3456 __perf_event__output_id_sample(handle, sample);
3457}
3458
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003459int perf_output_begin(struct perf_output_handle *handle,
3460 struct perf_event *event, unsigned int size,
3461 int nmi, int sample)
3462{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003463 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003464 unsigned long tail, offset, head;
3465 int have_lost;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003466 struct perf_sample_data sample_data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003467 struct {
3468 struct perf_event_header header;
3469 u64 id;
3470 u64 lost;
3471 } lost_event;
3472
3473 rcu_read_lock();
3474 /*
3475 * For inherited events we send all the output towards the parent.
3476 */
3477 if (event->parent)
3478 event = event->parent;
3479
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003480 buffer = rcu_dereference(event->buffer);
3481 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003482 goto out;
3483
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003484 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003485 handle->event = event;
3486 handle->nmi = nmi;
3487 handle->sample = sample;
3488
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003489 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02003490 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003491
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003492 have_lost = local_read(&buffer->lost);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003493 if (have_lost) {
3494 lost_event.header.size = sizeof(lost_event);
3495 perf_event_header__init_id(&lost_event.header, &sample_data,
3496 event);
3497 size += lost_event.header.size;
3498 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003499
Peter Zijlstraef607772010-05-18 10:50:41 +02003500 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003501
3502 do {
3503 /*
3504 * Userspace could choose to issue a mb() before updating the
3505 * tail pointer. So that all reads will be completed before the
3506 * write is issued.
3507 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003508 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003509 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003510 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003511 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003512 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003513 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003514 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003515
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003516 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3517 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003518
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003519 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3520 handle->page &= buffer->nr_pages - 1;
3521 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3522 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003523 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003524 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003525
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003526 if (have_lost) {
3527 lost_event.header.type = PERF_RECORD_LOST;
3528 lost_event.header.misc = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003529 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003530 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003531
3532 perf_output_put(handle, lost_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003533 perf_event__output_id_sample(event, handle, &sample_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003534 }
3535
3536 return 0;
3537
3538fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003539 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02003540 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003541out:
3542 rcu_read_unlock();
3543
3544 return -ENOSPC;
3545}
3546
3547void perf_output_end(struct perf_output_handle *handle)
3548{
3549 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003550 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003551
3552 int wakeup_events = event->attr.wakeup_events;
3553
3554 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003555 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003556 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003557 local_sub(wakeup_events, &buffer->events);
3558 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003559 }
3560 }
3561
Peter Zijlstraef607772010-05-18 10:50:41 +02003562 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003563 rcu_read_unlock();
3564}
3565
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003566static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02003567 struct perf_event *event,
3568 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003569{
3570 u64 read_format = event->attr.read_format;
3571 u64 values[4];
3572 int n = 0;
3573
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003574 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003575 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02003576 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003577 atomic64_read(&event->child_total_time_enabled);
3578 }
3579 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02003580 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003581 atomic64_read(&event->child_total_time_running);
3582 }
3583 if (read_format & PERF_FORMAT_ID)
3584 values[n++] = primary_event_id(event);
3585
3586 perf_output_copy(handle, values, n * sizeof(u64));
3587}
3588
3589/*
3590 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3591 */
3592static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02003593 struct perf_event *event,
3594 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003595{
3596 struct perf_event *leader = event->group_leader, *sub;
3597 u64 read_format = event->attr.read_format;
3598 u64 values[5];
3599 int n = 0;
3600
3601 values[n++] = 1 + leader->nr_siblings;
3602
3603 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02003604 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003605
3606 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02003607 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003608
3609 if (leader != event)
3610 leader->pmu->read(leader);
3611
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003612 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003613 if (read_format & PERF_FORMAT_ID)
3614 values[n++] = primary_event_id(leader);
3615
3616 perf_output_copy(handle, values, n * sizeof(u64));
3617
3618 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3619 n = 0;
3620
3621 if (sub != event)
3622 sub->pmu->read(sub);
3623
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003624 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003625 if (read_format & PERF_FORMAT_ID)
3626 values[n++] = primary_event_id(sub);
3627
3628 perf_output_copy(handle, values, n * sizeof(u64));
3629 }
3630}
3631
Stephane Eranianeed01522010-10-26 16:08:01 +02003632#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3633 PERF_FORMAT_TOTAL_TIME_RUNNING)
3634
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003635static void perf_output_read(struct perf_output_handle *handle,
3636 struct perf_event *event)
3637{
Stephane Eranianeed01522010-10-26 16:08:01 +02003638 u64 enabled = 0, running = 0, now, ctx_time;
3639 u64 read_format = event->attr.read_format;
3640
3641 /*
3642 * compute total_time_enabled, total_time_running
3643 * based on snapshot values taken when the event
3644 * was last scheduled in.
3645 *
3646 * we cannot simply called update_context_time()
3647 * because of locking issue as we are called in
3648 * NMI context
3649 */
3650 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
3651 now = perf_clock();
3652 ctx_time = event->shadow_ctx_time + now;
3653 enabled = ctx_time - event->tstamp_enabled;
3654 running = ctx_time - event->tstamp_running;
3655 }
3656
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003657 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02003658 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003659 else
Stephane Eranianeed01522010-10-26 16:08:01 +02003660 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003661}
3662
3663void perf_output_sample(struct perf_output_handle *handle,
3664 struct perf_event_header *header,
3665 struct perf_sample_data *data,
3666 struct perf_event *event)
3667{
3668 u64 sample_type = data->type;
3669
3670 perf_output_put(handle, *header);
3671
3672 if (sample_type & PERF_SAMPLE_IP)
3673 perf_output_put(handle, data->ip);
3674
3675 if (sample_type & PERF_SAMPLE_TID)
3676 perf_output_put(handle, data->tid_entry);
3677
3678 if (sample_type & PERF_SAMPLE_TIME)
3679 perf_output_put(handle, data->time);
3680
3681 if (sample_type & PERF_SAMPLE_ADDR)
3682 perf_output_put(handle, data->addr);
3683
3684 if (sample_type & PERF_SAMPLE_ID)
3685 perf_output_put(handle, data->id);
3686
3687 if (sample_type & PERF_SAMPLE_STREAM_ID)
3688 perf_output_put(handle, data->stream_id);
3689
3690 if (sample_type & PERF_SAMPLE_CPU)
3691 perf_output_put(handle, data->cpu_entry);
3692
3693 if (sample_type & PERF_SAMPLE_PERIOD)
3694 perf_output_put(handle, data->period);
3695
3696 if (sample_type & PERF_SAMPLE_READ)
3697 perf_output_read(handle, event);
3698
3699 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3700 if (data->callchain) {
3701 int size = 1;
3702
3703 if (data->callchain)
3704 size += data->callchain->nr;
3705
3706 size *= sizeof(u64);
3707
3708 perf_output_copy(handle, data->callchain, size);
3709 } else {
3710 u64 nr = 0;
3711 perf_output_put(handle, nr);
3712 }
3713 }
3714
3715 if (sample_type & PERF_SAMPLE_RAW) {
3716 if (data->raw) {
3717 perf_output_put(handle, data->raw->size);
3718 perf_output_copy(handle, data->raw->data,
3719 data->raw->size);
3720 } else {
3721 struct {
3722 u32 size;
3723 u32 data;
3724 } raw = {
3725 .size = sizeof(u32),
3726 .data = 0,
3727 };
3728 perf_output_put(handle, raw);
3729 }
3730 }
3731}
3732
3733void perf_prepare_sample(struct perf_event_header *header,
3734 struct perf_sample_data *data,
3735 struct perf_event *event,
3736 struct pt_regs *regs)
3737{
3738 u64 sample_type = event->attr.sample_type;
3739
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003740 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003741 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003742
3743 header->misc = 0;
3744 header->misc |= perf_misc_flags(regs);
3745
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003746 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02003747
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003748 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003749 data->ip = perf_instruction_pointer(regs);
3750
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003751 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3752 int size = 1;
3753
3754 data->callchain = perf_callchain(regs);
3755
3756 if (data->callchain)
3757 size += data->callchain->nr;
3758
3759 header->size += size * sizeof(u64);
3760 }
3761
3762 if (sample_type & PERF_SAMPLE_RAW) {
3763 int size = sizeof(u32);
3764
3765 if (data->raw)
3766 size += data->raw->size;
3767 else
3768 size += sizeof(u32);
3769
3770 WARN_ON_ONCE(size & (sizeof(u64)-1));
3771 header->size += size;
3772 }
3773}
3774
3775static void perf_event_output(struct perf_event *event, int nmi,
3776 struct perf_sample_data *data,
3777 struct pt_regs *regs)
3778{
3779 struct perf_output_handle handle;
3780 struct perf_event_header header;
3781
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003782 /* protect the callchain buffers */
3783 rcu_read_lock();
3784
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003785 perf_prepare_sample(&header, data, event, regs);
3786
3787 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003788 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003789
3790 perf_output_sample(&handle, &header, data, event);
3791
3792 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003793
3794exit:
3795 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003796}
3797
3798/*
3799 * read event_id
3800 */
3801
3802struct perf_read_event {
3803 struct perf_event_header header;
3804
3805 u32 pid;
3806 u32 tid;
3807};
3808
3809static void
3810perf_event_read_event(struct perf_event *event,
3811 struct task_struct *task)
3812{
3813 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003814 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003815 struct perf_read_event read_event = {
3816 .header = {
3817 .type = PERF_RECORD_READ,
3818 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003819 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003820 },
3821 .pid = perf_event_pid(event, task),
3822 .tid = perf_event_tid(event, task),
3823 };
3824 int ret;
3825
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003826 perf_event_header__init_id(&read_event.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003827 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3828 if (ret)
3829 return;
3830
3831 perf_output_put(&handle, read_event);
3832 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003833 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003834
3835 perf_output_end(&handle);
3836}
3837
3838/*
3839 * task tracking -- fork/exit
3840 *
Eric B Munson3af9e852010-05-18 15:30:49 +01003841 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003842 */
3843
3844struct perf_task_event {
3845 struct task_struct *task;
3846 struct perf_event_context *task_ctx;
3847
3848 struct {
3849 struct perf_event_header header;
3850
3851 u32 pid;
3852 u32 ppid;
3853 u32 tid;
3854 u32 ptid;
3855 u64 time;
3856 } event_id;
3857};
3858
3859static void perf_event_task_output(struct perf_event *event,
3860 struct perf_task_event *task_event)
3861{
3862 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003863 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003864 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003865 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003866
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003867 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003868
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003869 ret = perf_output_begin(&handle, event,
3870 task_event->event_id.header.size, 0, 0);
Peter Zijlstraef607772010-05-18 10:50:41 +02003871 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003872 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003873
3874 task_event->event_id.pid = perf_event_pid(event, task);
3875 task_event->event_id.ppid = perf_event_pid(event, current);
3876
3877 task_event->event_id.tid = perf_event_tid(event, task);
3878 task_event->event_id.ptid = perf_event_tid(event, current);
3879
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003880 perf_output_put(&handle, task_event->event_id);
3881
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003882 perf_event__output_id_sample(event, &handle, &sample);
3883
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003884 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003885out:
3886 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003887}
3888
3889static int perf_event_task_match(struct perf_event *event)
3890{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003891 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003892 return 0;
3893
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003894 if (event->cpu != -1 && event->cpu != smp_processor_id())
3895 return 0;
3896
Eric B Munson3af9e852010-05-18 15:30:49 +01003897 if (event->attr.comm || event->attr.mmap ||
3898 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003899 return 1;
3900
3901 return 0;
3902}
3903
3904static void perf_event_task_ctx(struct perf_event_context *ctx,
3905 struct perf_task_event *task_event)
3906{
3907 struct perf_event *event;
3908
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003909 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3910 if (perf_event_task_match(event))
3911 perf_event_task_output(event, task_event);
3912 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003913}
3914
3915static void perf_event_task_event(struct perf_task_event *task_event)
3916{
3917 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003918 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003919 struct pmu *pmu;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003920 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003921
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01003922 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003923 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02003924 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01003925 if (cpuctx->active_pmu != pmu)
3926 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003927 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003928
3929 ctx = task_event->task_ctx;
3930 if (!ctx) {
3931 ctxn = pmu->task_ctx_nr;
3932 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02003933 goto next;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02003934 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3935 }
3936 if (ctx)
3937 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02003938next:
3939 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003940 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003941 rcu_read_unlock();
3942}
3943
3944static void perf_event_task(struct task_struct *task,
3945 struct perf_event_context *task_ctx,
3946 int new)
3947{
3948 struct perf_task_event task_event;
3949
3950 if (!atomic_read(&nr_comm_events) &&
3951 !atomic_read(&nr_mmap_events) &&
3952 !atomic_read(&nr_task_events))
3953 return;
3954
3955 task_event = (struct perf_task_event){
3956 .task = task,
3957 .task_ctx = task_ctx,
3958 .event_id = {
3959 .header = {
3960 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3961 .misc = 0,
3962 .size = sizeof(task_event.event_id),
3963 },
3964 /* .pid */
3965 /* .ppid */
3966 /* .tid */
3967 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003968 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003969 },
3970 };
3971
3972 perf_event_task_event(&task_event);
3973}
3974
3975void perf_event_fork(struct task_struct *task)
3976{
3977 perf_event_task(task, NULL, 1);
3978}
3979
3980/*
3981 * comm tracking
3982 */
3983
3984struct perf_comm_event {
3985 struct task_struct *task;
3986 char *comm;
3987 int comm_size;
3988
3989 struct {
3990 struct perf_event_header header;
3991
3992 u32 pid;
3993 u32 tid;
3994 } event_id;
3995};
3996
3997static void perf_event_comm_output(struct perf_event *event,
3998 struct perf_comm_event *comm_event)
3999{
4000 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004001 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004002 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004003 int ret;
4004
4005 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4006 ret = perf_output_begin(&handle, event,
4007 comm_event->event_id.header.size, 0, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004008
4009 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004010 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004011
4012 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4013 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4014
4015 perf_output_put(&handle, comm_event->event_id);
4016 perf_output_copy(&handle, comm_event->comm,
4017 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004018
4019 perf_event__output_id_sample(event, &handle, &sample);
4020
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004021 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004022out:
4023 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004024}
4025
4026static int perf_event_comm_match(struct perf_event *event)
4027{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004028 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004029 return 0;
4030
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004031 if (event->cpu != -1 && event->cpu != smp_processor_id())
4032 return 0;
4033
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004034 if (event->attr.comm)
4035 return 1;
4036
4037 return 0;
4038}
4039
4040static void perf_event_comm_ctx(struct perf_event_context *ctx,
4041 struct perf_comm_event *comm_event)
4042{
4043 struct perf_event *event;
4044
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004045 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4046 if (perf_event_comm_match(event))
4047 perf_event_comm_output(event, comm_event);
4048 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004049}
4050
4051static void perf_event_comm_event(struct perf_comm_event *comm_event)
4052{
4053 struct perf_cpu_context *cpuctx;
4054 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004055 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004056 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004057 struct pmu *pmu;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004058 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004059
4060 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01004061 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004062 size = ALIGN(strlen(comm)+1, sizeof(u64));
4063
4064 comm_event->comm = comm;
4065 comm_event->comm_size = size;
4066
4067 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstraf6595f32009-11-20 22:19:47 +01004068 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004069 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004070 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004071 if (cpuctx->active_pmu != pmu)
4072 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004073 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004074
4075 ctxn = pmu->task_ctx_nr;
4076 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004077 goto next;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004078
4079 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4080 if (ctx)
4081 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004082next:
4083 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004084 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004085 rcu_read_unlock();
4086}
4087
4088void perf_event_comm(struct task_struct *task)
4089{
4090 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004091 struct perf_event_context *ctx;
4092 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004093
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004094 for_each_task_context_nr(ctxn) {
4095 ctx = task->perf_event_ctxp[ctxn];
4096 if (!ctx)
4097 continue;
4098
4099 perf_event_enable_on_exec(ctx);
4100 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004101
4102 if (!atomic_read(&nr_comm_events))
4103 return;
4104
4105 comm_event = (struct perf_comm_event){
4106 .task = task,
4107 /* .comm */
4108 /* .comm_size */
4109 .event_id = {
4110 .header = {
4111 .type = PERF_RECORD_COMM,
4112 .misc = 0,
4113 /* .size */
4114 },
4115 /* .pid */
4116 /* .tid */
4117 },
4118 };
4119
4120 perf_event_comm_event(&comm_event);
4121}
4122
4123/*
4124 * mmap tracking
4125 */
4126
4127struct perf_mmap_event {
4128 struct vm_area_struct *vma;
4129
4130 const char *file_name;
4131 int file_size;
4132
4133 struct {
4134 struct perf_event_header header;
4135
4136 u32 pid;
4137 u32 tid;
4138 u64 start;
4139 u64 len;
4140 u64 pgoff;
4141 } event_id;
4142};
4143
4144static void perf_event_mmap_output(struct perf_event *event,
4145 struct perf_mmap_event *mmap_event)
4146{
4147 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004148 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004149 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004150 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004151
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004152 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4153 ret = perf_output_begin(&handle, event,
4154 mmap_event->event_id.header.size, 0, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004155 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004156 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004157
4158 mmap_event->event_id.pid = perf_event_pid(event, current);
4159 mmap_event->event_id.tid = perf_event_tid(event, current);
4160
4161 perf_output_put(&handle, mmap_event->event_id);
4162 perf_output_copy(&handle, mmap_event->file_name,
4163 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004164
4165 perf_event__output_id_sample(event, &handle, &sample);
4166
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004167 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004168out:
4169 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004170}
4171
4172static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01004173 struct perf_mmap_event *mmap_event,
4174 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004175{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004176 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004177 return 0;
4178
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004179 if (event->cpu != -1 && event->cpu != smp_processor_id())
4180 return 0;
4181
Eric B Munson3af9e852010-05-18 15:30:49 +01004182 if ((!executable && event->attr.mmap_data) ||
4183 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004184 return 1;
4185
4186 return 0;
4187}
4188
4189static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004190 struct perf_mmap_event *mmap_event,
4191 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004192{
4193 struct perf_event *event;
4194
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004195 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004196 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004197 perf_event_mmap_output(event, mmap_event);
4198 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004199}
4200
4201static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4202{
4203 struct perf_cpu_context *cpuctx;
4204 struct perf_event_context *ctx;
4205 struct vm_area_struct *vma = mmap_event->vma;
4206 struct file *file = vma->vm_file;
4207 unsigned int size;
4208 char tmp[16];
4209 char *buf = NULL;
4210 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004211 struct pmu *pmu;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004212 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004213
4214 memset(tmp, 0, sizeof(tmp));
4215
4216 if (file) {
4217 /*
4218 * d_path works from the end of the buffer backwards, so we
4219 * need to add enough zero bytes after the string to handle
4220 * the 64bit alignment we do later.
4221 */
4222 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4223 if (!buf) {
4224 name = strncpy(tmp, "//enomem", sizeof(tmp));
4225 goto got_name;
4226 }
4227 name = d_path(&file->f_path, buf, PATH_MAX);
4228 if (IS_ERR(name)) {
4229 name = strncpy(tmp, "//toolong", sizeof(tmp));
4230 goto got_name;
4231 }
4232 } else {
4233 if (arch_vma_name(mmap_event->vma)) {
4234 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4235 sizeof(tmp));
4236 goto got_name;
4237 }
4238
4239 if (!vma->vm_mm) {
4240 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4241 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004242 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4243 vma->vm_end >= vma->vm_mm->brk) {
4244 name = strncpy(tmp, "[heap]", sizeof(tmp));
4245 goto got_name;
4246 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4247 vma->vm_end >= vma->vm_mm->start_stack) {
4248 name = strncpy(tmp, "[stack]", sizeof(tmp));
4249 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004250 }
4251
4252 name = strncpy(tmp, "//anon", sizeof(tmp));
4253 goto got_name;
4254 }
4255
4256got_name:
4257 size = ALIGN(strlen(name)+1, sizeof(u64));
4258
4259 mmap_event->file_name = name;
4260 mmap_event->file_size = size;
4261
4262 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4263
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004264 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004265 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004266 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004267 if (cpuctx->active_pmu != pmu)
4268 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004269 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4270 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004271
4272 ctxn = pmu->task_ctx_nr;
4273 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004274 goto next;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02004275
4276 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4277 if (ctx) {
4278 perf_event_mmap_ctx(ctx, mmap_event,
4279 vma->vm_flags & VM_EXEC);
4280 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004281next:
4282 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004283 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004284 rcu_read_unlock();
4285
4286 kfree(buf);
4287}
4288
Eric B Munson3af9e852010-05-18 15:30:49 +01004289void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004290{
4291 struct perf_mmap_event mmap_event;
4292
4293 if (!atomic_read(&nr_mmap_events))
4294 return;
4295
4296 mmap_event = (struct perf_mmap_event){
4297 .vma = vma,
4298 /* .file_name */
4299 /* .file_size */
4300 .event_id = {
4301 .header = {
4302 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004303 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004304 /* .size */
4305 },
4306 /* .pid */
4307 /* .tid */
4308 .start = vma->vm_start,
4309 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004310 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004311 },
4312 };
4313
4314 perf_event_mmap_event(&mmap_event);
4315}
4316
4317/*
4318 * IRQ throttle logging
4319 */
4320
4321static void perf_log_throttle(struct perf_event *event, int enable)
4322{
4323 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004324 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004325 int ret;
4326
4327 struct {
4328 struct perf_event_header header;
4329 u64 time;
4330 u64 id;
4331 u64 stream_id;
4332 } throttle_event = {
4333 .header = {
4334 .type = PERF_RECORD_THROTTLE,
4335 .misc = 0,
4336 .size = sizeof(throttle_event),
4337 },
4338 .time = perf_clock(),
4339 .id = primary_event_id(event),
4340 .stream_id = event->id,
4341 };
4342
4343 if (enable)
4344 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4345
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004346 perf_event_header__init_id(&throttle_event.header, &sample, event);
4347
4348 ret = perf_output_begin(&handle, event,
4349 throttle_event.header.size, 1, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004350 if (ret)
4351 return;
4352
4353 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004354 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004355 perf_output_end(&handle);
4356}
4357
4358/*
4359 * Generic event overflow handling, sampling.
4360 */
4361
4362static int __perf_event_overflow(struct perf_event *event, int nmi,
4363 int throttle, struct perf_sample_data *data,
4364 struct pt_regs *regs)
4365{
4366 int events = atomic_read(&event->event_limit);
4367 struct hw_perf_event *hwc = &event->hw;
4368 int ret = 0;
4369
Peter Zijlstra96398822010-11-24 18:55:29 +01004370 /*
4371 * Non-sampling counters might still use the PMI to fold short
4372 * hardware counters, ignore those.
4373 */
4374 if (unlikely(!is_sampling_event(event)))
4375 return 0;
4376
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004377 if (!throttle) {
4378 hwc->interrupts++;
4379 } else {
4380 if (hwc->interrupts != MAX_INTERRUPTS) {
4381 hwc->interrupts++;
4382 if (HZ * hwc->interrupts >
4383 (u64)sysctl_perf_event_sample_rate) {
4384 hwc->interrupts = MAX_INTERRUPTS;
4385 perf_log_throttle(event, 0);
4386 ret = 1;
4387 }
4388 } else {
4389 /*
4390 * Keep re-disabling events even though on the previous
4391 * pass we disabled it - just in case we raced with a
4392 * sched-in and the event got enabled again:
4393 */
4394 ret = 1;
4395 }
4396 }
4397
4398 if (event->attr.freq) {
4399 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004400 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004401
Peter Zijlstraabd50712010-01-26 18:50:16 +01004402 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004403
Peter Zijlstraabd50712010-01-26 18:50:16 +01004404 if (delta > 0 && delta < 2*TICK_NSEC)
4405 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004406 }
4407
4408 /*
4409 * XXX event_limit might not quite work as expected on inherited
4410 * events
4411 */
4412
4413 event->pending_kill = POLL_IN;
4414 if (events && atomic_dec_and_test(&event->event_limit)) {
4415 ret = 1;
4416 event->pending_kill = POLL_HUP;
4417 if (nmi) {
4418 event->pending_disable = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004419 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004420 } else
4421 perf_event_disable(event);
4422 }
4423
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004424 if (event->overflow_handler)
4425 event->overflow_handler(event, nmi, data, regs);
4426 else
4427 perf_event_output(event, nmi, data, regs);
4428
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004429 return ret;
4430}
4431
4432int perf_event_overflow(struct perf_event *event, int nmi,
4433 struct perf_sample_data *data,
4434 struct pt_regs *regs)
4435{
4436 return __perf_event_overflow(event, nmi, 1, data, regs);
4437}
4438
4439/*
4440 * Generic software event infrastructure
4441 */
4442
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004443struct swevent_htable {
4444 struct swevent_hlist *swevent_hlist;
4445 struct mutex hlist_mutex;
4446 int hlist_refcount;
4447
4448 /* Recursion avoidance in each contexts */
4449 int recursion[PERF_NR_CONTEXTS];
4450};
4451
4452static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4453
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004454/*
4455 * We directly increment event->count and keep a second value in
4456 * event->hw.period_left to count intervals. This period event
4457 * is kept in the range [-sample_period, 0] so that we can use the
4458 * sign as trigger.
4459 */
4460
4461static u64 perf_swevent_set_period(struct perf_event *event)
4462{
4463 struct hw_perf_event *hwc = &event->hw;
4464 u64 period = hwc->last_period;
4465 u64 nr, offset;
4466 s64 old, val;
4467
4468 hwc->last_period = hwc->sample_period;
4469
4470again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004471 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004472 if (val < 0)
4473 return 0;
4474
4475 nr = div64_u64(period + val, period);
4476 offset = nr * period;
4477 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004478 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004479 goto again;
4480
4481 return nr;
4482}
4483
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004484static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004485 int nmi, struct perf_sample_data *data,
4486 struct pt_regs *regs)
4487{
4488 struct hw_perf_event *hwc = &event->hw;
4489 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004490
4491 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004492 if (!overflow)
4493 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004494
4495 if (hwc->interrupts == MAX_INTERRUPTS)
4496 return;
4497
4498 for (; overflow; overflow--) {
4499 if (__perf_event_overflow(event, nmi, throttle,
4500 data, regs)) {
4501 /*
4502 * We inhibit the overflow from happening when
4503 * hwc->interrupts == MAX_INTERRUPTS.
4504 */
4505 break;
4506 }
4507 throttle = 1;
4508 }
4509}
4510
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004511static void perf_swevent_event(struct perf_event *event, u64 nr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004512 int nmi, struct perf_sample_data *data,
4513 struct pt_regs *regs)
4514{
4515 struct hw_perf_event *hwc = &event->hw;
4516
Peter Zijlstrae7850592010-05-21 14:43:08 +02004517 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004518
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004519 if (!regs)
4520 return;
4521
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01004522 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004523 return;
4524
4525 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4526 return perf_swevent_overflow(event, 1, nmi, data, regs);
4527
Peter Zijlstrae7850592010-05-21 14:43:08 +02004528 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004529 return;
4530
4531 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004532}
4533
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004534static int perf_exclude_event(struct perf_event *event,
4535 struct pt_regs *regs)
4536{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004537 if (event->hw.state & PERF_HES_STOPPED)
4538 return 0;
4539
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004540 if (regs) {
4541 if (event->attr.exclude_user && user_mode(regs))
4542 return 1;
4543
4544 if (event->attr.exclude_kernel && !user_mode(regs))
4545 return 1;
4546 }
4547
4548 return 0;
4549}
4550
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004551static int perf_swevent_match(struct perf_event *event,
4552 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004553 u32 event_id,
4554 struct perf_sample_data *data,
4555 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004556{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004557 if (event->attr.type != type)
4558 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004559
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004560 if (event->attr.config != event_id)
4561 return 0;
4562
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004563 if (perf_exclude_event(event, regs))
4564 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004565
4566 return 1;
4567}
4568
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004569static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004570{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004571 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004572
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004573 return hash_64(val, SWEVENT_HLIST_BITS);
4574}
4575
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004576static inline struct hlist_head *
4577__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004578{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004579 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004580
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004581 return &hlist->heads[hash];
4582}
4583
4584/* For the read side: events when they trigger */
4585static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004586find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004587{
4588 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004589
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004590 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004591 if (!hlist)
4592 return NULL;
4593
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004594 return __find_swevent_head(hlist, type, event_id);
4595}
4596
4597/* For the event head insertion and removal in the hlist */
4598static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004599find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004600{
4601 struct swevent_hlist *hlist;
4602 u32 event_id = event->attr.config;
4603 u64 type = event->attr.type;
4604
4605 /*
4606 * Event scheduling is always serialized against hlist allocation
4607 * and release. Which makes the protected version suitable here.
4608 * The context lock guarantees that.
4609 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004610 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004611 lockdep_is_held(&event->ctx->lock));
4612 if (!hlist)
4613 return NULL;
4614
4615 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004616}
4617
4618static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4619 u64 nr, int nmi,
4620 struct perf_sample_data *data,
4621 struct pt_regs *regs)
4622{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004623 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004624 struct perf_event *event;
4625 struct hlist_node *node;
4626 struct hlist_head *head;
4627
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004628 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004629 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004630 if (!head)
4631 goto end;
4632
4633 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004634 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004635 perf_swevent_event(event, nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004636 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004637end:
4638 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004639}
4640
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004641int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004642{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004643 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004644
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004645 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004646}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004647EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004648
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004649void inline perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004650{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004651 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004652
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004653 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004654}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004655
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004656void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4657 struct pt_regs *regs, u64 addr)
4658{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004659 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004660 int rctx;
4661
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004662 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004663 rctx = perf_swevent_get_recursion_context();
4664 if (rctx < 0)
4665 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004666
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004667 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004668
4669 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004670
4671 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004672 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004673}
4674
4675static void perf_swevent_read(struct perf_event *event)
4676{
4677}
4678
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004679static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004680{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004681 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004682 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004683 struct hlist_head *head;
4684
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01004685 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004686 hwc->last_period = hwc->sample_period;
4687 perf_swevent_set_period(event);
4688 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004689
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004690 hwc->state = !(flags & PERF_EF_START);
4691
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004692 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004693 if (WARN_ON_ONCE(!head))
4694 return -EINVAL;
4695
4696 hlist_add_head_rcu(&event->hlist_entry, head);
4697
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004698 return 0;
4699}
4700
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004701static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004702{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004703 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004704}
4705
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004706static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004707{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004708 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004709}
4710
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004711static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004712{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004713 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004714}
4715
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004716/* Deref the hlist from the update side */
4717static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004718swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004719{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004720 return rcu_dereference_protected(swhash->swevent_hlist,
4721 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004722}
4723
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004724static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4725{
4726 struct swevent_hlist *hlist;
4727
4728 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4729 kfree(hlist);
4730}
4731
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004732static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004733{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004734 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004735
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004736 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004737 return;
4738
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004739 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004740 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4741}
4742
4743static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4744{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004745 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004746
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004747 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004748
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004749 if (!--swhash->hlist_refcount)
4750 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004751
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004752 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004753}
4754
4755static void swevent_hlist_put(struct perf_event *event)
4756{
4757 int cpu;
4758
4759 if (event->cpu != -1) {
4760 swevent_hlist_put_cpu(event, event->cpu);
4761 return;
4762 }
4763
4764 for_each_possible_cpu(cpu)
4765 swevent_hlist_put_cpu(event, cpu);
4766}
4767
4768static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4769{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004770 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004771 int err = 0;
4772
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004773 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004774
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004775 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004776 struct swevent_hlist *hlist;
4777
4778 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4779 if (!hlist) {
4780 err = -ENOMEM;
4781 goto exit;
4782 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004783 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004784 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004785 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004786exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004787 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004788
4789 return err;
4790}
4791
4792static int swevent_hlist_get(struct perf_event *event)
4793{
4794 int err;
4795 int cpu, failed_cpu;
4796
4797 if (event->cpu != -1)
4798 return swevent_hlist_get_cpu(event, event->cpu);
4799
4800 get_online_cpus();
4801 for_each_possible_cpu(cpu) {
4802 err = swevent_hlist_get_cpu(event, cpu);
4803 if (err) {
4804 failed_cpu = cpu;
4805 goto fail;
4806 }
4807 }
4808 put_online_cpus();
4809
4810 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004811fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004812 for_each_possible_cpu(cpu) {
4813 if (cpu == failed_cpu)
4814 break;
4815 swevent_hlist_put_cpu(event, cpu);
4816 }
4817
4818 put_online_cpus();
4819 return err;
4820}
4821
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004822atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004823
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004824static void sw_perf_event_destroy(struct perf_event *event)
4825{
4826 u64 event_id = event->attr.config;
4827
4828 WARN_ON(event->parent);
4829
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02004830 jump_label_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004831 swevent_hlist_put(event);
4832}
4833
4834static int perf_swevent_init(struct perf_event *event)
4835{
4836 int event_id = event->attr.config;
4837
4838 if (event->attr.type != PERF_TYPE_SOFTWARE)
4839 return -ENOENT;
4840
4841 switch (event_id) {
4842 case PERF_COUNT_SW_CPU_CLOCK:
4843 case PERF_COUNT_SW_TASK_CLOCK:
4844 return -ENOENT;
4845
4846 default:
4847 break;
4848 }
4849
Dan Carpenterce677832010-10-24 21:50:42 +02004850 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004851 return -ENOENT;
4852
4853 if (!event->parent) {
4854 int err;
4855
4856 err = swevent_hlist_get(event);
4857 if (err)
4858 return err;
4859
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02004860 jump_label_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004861 event->destroy = sw_perf_event_destroy;
4862 }
4863
4864 return 0;
4865}
4866
4867static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004868 .task_ctx_nr = perf_sw_context,
4869
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004870 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004871 .add = perf_swevent_add,
4872 .del = perf_swevent_del,
4873 .start = perf_swevent_start,
4874 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004875 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004876};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004877
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004878#ifdef CONFIG_EVENT_TRACING
4879
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004880static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004881 struct perf_sample_data *data)
4882{
4883 void *record = data->raw->data;
4884
4885 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4886 return 1;
4887 return 0;
4888}
4889
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004890static int perf_tp_event_match(struct perf_event *event,
4891 struct perf_sample_data *data,
4892 struct pt_regs *regs)
4893{
Peter Zijlstra580d6072010-05-20 20:54:31 +02004894 /*
4895 * All tracepoints are from kernel-space.
4896 */
4897 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004898 return 0;
4899
4900 if (!perf_tp_filter_match(event, data))
4901 return 0;
4902
4903 return 1;
4904}
4905
4906void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004907 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004908{
4909 struct perf_sample_data data;
4910 struct perf_event *event;
4911 struct hlist_node *node;
4912
4913 struct perf_raw_record raw = {
4914 .size = entry_size,
4915 .data = record,
4916 };
4917
4918 perf_sample_data_init(&data, addr);
4919 data.raw = &raw;
4920
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004921 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4922 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004923 perf_swevent_event(event, count, 1, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004924 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004925
4926 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004927}
4928EXPORT_SYMBOL_GPL(perf_tp_event);
4929
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004930static void tp_perf_event_destroy(struct perf_event *event)
4931{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004932 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004933}
4934
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004935static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004936{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004937 int err;
4938
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004939 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4940 return -ENOENT;
4941
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004942 err = perf_trace_init(event);
4943 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004944 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004945
4946 event->destroy = tp_perf_event_destroy;
4947
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004948 return 0;
4949}
4950
4951static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004952 .task_ctx_nr = perf_sw_context,
4953
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004954 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004955 .add = perf_trace_add,
4956 .del = perf_trace_del,
4957 .start = perf_swevent_start,
4958 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004959 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004960};
4961
4962static inline void perf_tp_register(void)
4963{
4964 perf_pmu_register(&perf_tracepoint);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004965}
Li Zefan6fb29152009-10-15 11:21:42 +08004966
4967static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4968{
4969 char *filter_str;
4970 int ret;
4971
4972 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4973 return -EINVAL;
4974
4975 filter_str = strndup_user(arg, PAGE_SIZE);
4976 if (IS_ERR(filter_str))
4977 return PTR_ERR(filter_str);
4978
4979 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
4980
4981 kfree(filter_str);
4982 return ret;
4983}
4984
4985static void perf_event_free_filter(struct perf_event *event)
4986{
4987 ftrace_profile_free_filter(event);
4988}
4989
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004990#else
Li Zefan6fb29152009-10-15 11:21:42 +08004991
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004992static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004993{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004994}
Li Zefan6fb29152009-10-15 11:21:42 +08004995
4996static int perf_event_set_filter(struct perf_event *event, void __user *arg)
4997{
4998 return -ENOENT;
4999}
5000
5001static void perf_event_free_filter(struct perf_event *event)
5002{
5003}
5004
Li Zefan07b139c2009-12-21 14:27:35 +08005005#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005006
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005007#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005008void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005009{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005010 struct perf_sample_data sample;
5011 struct pt_regs *regs = data;
5012
Peter Zijlstradc1d6282010-03-03 15:55:04 +01005013 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005014
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005015 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5016 perf_swevent_event(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005017}
5018#endif
5019
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005020/*
5021 * hrtimer based swevent callback
5022 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005023
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005024static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005025{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005026 enum hrtimer_restart ret = HRTIMER_RESTART;
5027 struct perf_sample_data data;
5028 struct pt_regs *regs;
5029 struct perf_event *event;
5030 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005031
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005032 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5033 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005034
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005035 perf_sample_data_init(&data, 0);
5036 data.period = event->hw.last_period;
5037 regs = get_irq_regs();
5038
5039 if (regs && !perf_exclude_event(event, regs)) {
5040 if (!(event->attr.exclude_idle && current->pid == 0))
5041 if (perf_event_overflow(event, 0, &data, regs))
5042 ret = HRTIMER_NORESTART;
5043 }
5044
5045 period = max_t(u64, 10000, event->hw.sample_period);
5046 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5047
5048 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005049}
5050
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005051static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005052{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005053 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005054 s64 period;
5055
5056 if (!is_sampling_event(event))
5057 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005058
5059 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5060 hwc->hrtimer.function = perf_swevent_hrtimer;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005061
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005062 period = local64_read(&hwc->period_left);
5063 if (period) {
5064 if (period < 0)
5065 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005066
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005067 local64_set(&hwc->period_left, 0);
5068 } else {
5069 period = max_t(u64, 10000, hwc->sample_period);
5070 }
5071 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005072 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02005073 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005074}
5075
5076static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5077{
5078 struct hw_perf_event *hwc = &event->hw;
5079
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005080 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005081 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005082 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005083
5084 hrtimer_cancel(&hwc->hrtimer);
5085 }
5086}
5087
5088/*
5089 * Software event: cpu wall time clock
5090 */
5091
5092static void cpu_clock_event_update(struct perf_event *event)
5093{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005094 s64 prev;
5095 u64 now;
5096
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005097 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005098 prev = local64_xchg(&event->hw.prev_count, now);
5099 local64_add(now - prev, &event->count);
5100}
5101
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005102static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005103{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005104 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005105 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005106}
5107
5108static void cpu_clock_event_stop(struct perf_event *event, int flags)
5109{
5110 perf_swevent_cancel_hrtimer(event);
5111 cpu_clock_event_update(event);
5112}
5113
5114static int cpu_clock_event_add(struct perf_event *event, int flags)
5115{
5116 if (flags & PERF_EF_START)
5117 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005118
5119 return 0;
5120}
5121
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005122static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005123{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005124 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005125}
5126
5127static void cpu_clock_event_read(struct perf_event *event)
5128{
5129 cpu_clock_event_update(event);
5130}
5131
5132static int cpu_clock_event_init(struct perf_event *event)
5133{
5134 if (event->attr.type != PERF_TYPE_SOFTWARE)
5135 return -ENOENT;
5136
5137 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5138 return -ENOENT;
5139
5140 return 0;
5141}
5142
5143static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005144 .task_ctx_nr = perf_sw_context,
5145
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005146 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005147 .add = cpu_clock_event_add,
5148 .del = cpu_clock_event_del,
5149 .start = cpu_clock_event_start,
5150 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005151 .read = cpu_clock_event_read,
5152};
5153
5154/*
5155 * Software event: task time clock
5156 */
5157
5158static void task_clock_event_update(struct perf_event *event, u64 now)
5159{
5160 u64 prev;
5161 s64 delta;
5162
5163 prev = local64_xchg(&event->hw.prev_count, now);
5164 delta = now - prev;
5165 local64_add(delta, &event->count);
5166}
5167
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005168static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005169{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005170 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005171 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005172}
5173
5174static void task_clock_event_stop(struct perf_event *event, int flags)
5175{
5176 perf_swevent_cancel_hrtimer(event);
5177 task_clock_event_update(event, event->ctx->time);
5178}
5179
5180static int task_clock_event_add(struct perf_event *event, int flags)
5181{
5182 if (flags & PERF_EF_START)
5183 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005184
5185 return 0;
5186}
5187
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005188static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005189{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005190 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005191}
5192
5193static void task_clock_event_read(struct perf_event *event)
5194{
5195 u64 time;
5196
5197 if (!in_nmi()) {
5198 update_context_time(event->ctx);
5199 time = event->ctx->time;
5200 } else {
5201 u64 now = perf_clock();
5202 u64 delta = now - event->ctx->timestamp;
5203 time = event->ctx->time + delta;
5204 }
5205
5206 task_clock_event_update(event, time);
5207}
5208
5209static int task_clock_event_init(struct perf_event *event)
5210{
5211 if (event->attr.type != PERF_TYPE_SOFTWARE)
5212 return -ENOENT;
5213
5214 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5215 return -ENOENT;
5216
5217 return 0;
5218}
5219
5220static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005221 .task_ctx_nr = perf_sw_context,
5222
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005223 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005224 .add = task_clock_event_add,
5225 .del = task_clock_event_del,
5226 .start = task_clock_event_start,
5227 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005228 .read = task_clock_event_read,
5229};
5230
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005231static void perf_pmu_nop_void(struct pmu *pmu)
5232{
5233}
5234
5235static int perf_pmu_nop_int(struct pmu *pmu)
5236{
5237 return 0;
5238}
5239
5240static void perf_pmu_start_txn(struct pmu *pmu)
5241{
5242 perf_pmu_disable(pmu);
5243}
5244
5245static int perf_pmu_commit_txn(struct pmu *pmu)
5246{
5247 perf_pmu_enable(pmu);
5248 return 0;
5249}
5250
5251static void perf_pmu_cancel_txn(struct pmu *pmu)
5252{
5253 perf_pmu_enable(pmu);
5254}
5255
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005256/*
5257 * Ensures all contexts with the same task_ctx_nr have the same
5258 * pmu_cpu_context too.
5259 */
5260static void *find_pmu_context(int ctxn)
5261{
5262 struct pmu *pmu;
5263
5264 if (ctxn < 0)
5265 return NULL;
5266
5267 list_for_each_entry(pmu, &pmus, entry) {
5268 if (pmu->task_ctx_nr == ctxn)
5269 return pmu->pmu_cpu_context;
5270 }
5271
5272 return NULL;
5273}
5274
Peter Zijlstra51676952010-12-07 14:18:20 +01005275static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005276{
Peter Zijlstra51676952010-12-07 14:18:20 +01005277 int cpu;
5278
5279 for_each_possible_cpu(cpu) {
5280 struct perf_cpu_context *cpuctx;
5281
5282 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5283
5284 if (cpuctx->active_pmu == old_pmu)
5285 cpuctx->active_pmu = pmu;
5286 }
5287}
5288
5289static void free_pmu_context(struct pmu *pmu)
5290{
5291 struct pmu *i;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005292
5293 mutex_lock(&pmus_lock);
5294 /*
5295 * Like a real lame refcount.
5296 */
Peter Zijlstra51676952010-12-07 14:18:20 +01005297 list_for_each_entry(i, &pmus, entry) {
5298 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5299 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005300 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01005301 }
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005302 }
5303
Peter Zijlstra51676952010-12-07 14:18:20 +01005304 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005305out:
5306 mutex_unlock(&pmus_lock);
5307}
5308
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005309int perf_pmu_register(struct pmu *pmu)
5310{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005311 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005312
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005313 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005314 ret = -ENOMEM;
5315 pmu->pmu_disable_count = alloc_percpu(int);
5316 if (!pmu->pmu_disable_count)
5317 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005318
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005319 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5320 if (pmu->pmu_cpu_context)
5321 goto got_cpu_context;
5322
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005323 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5324 if (!pmu->pmu_cpu_context)
5325 goto free_pdc;
5326
5327 for_each_possible_cpu(cpu) {
5328 struct perf_cpu_context *cpuctx;
5329
5330 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02005331 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005332 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005333 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02005334 cpuctx->jiffies_interval = 1;
5335 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra51676952010-12-07 14:18:20 +01005336 cpuctx->active_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005337 }
5338
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005339got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005340 if (!pmu->start_txn) {
5341 if (pmu->pmu_enable) {
5342 /*
5343 * If we have pmu_enable/pmu_disable calls, install
5344 * transaction stubs that use that to try and batch
5345 * hardware accesses.
5346 */
5347 pmu->start_txn = perf_pmu_start_txn;
5348 pmu->commit_txn = perf_pmu_commit_txn;
5349 pmu->cancel_txn = perf_pmu_cancel_txn;
5350 } else {
5351 pmu->start_txn = perf_pmu_nop_void;
5352 pmu->commit_txn = perf_pmu_nop_int;
5353 pmu->cancel_txn = perf_pmu_nop_void;
5354 }
5355 }
5356
5357 if (!pmu->pmu_enable) {
5358 pmu->pmu_enable = perf_pmu_nop_void;
5359 pmu->pmu_disable = perf_pmu_nop_void;
5360 }
5361
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005362 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005363 ret = 0;
5364unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005365 mutex_unlock(&pmus_lock);
5366
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005367 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005368
5369free_pdc:
5370 free_percpu(pmu->pmu_disable_count);
5371 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005372}
5373
5374void perf_pmu_unregister(struct pmu *pmu)
5375{
5376 mutex_lock(&pmus_lock);
5377 list_del_rcu(&pmu->entry);
5378 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005379
5380 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02005381 * We dereference the pmu list under both SRCU and regular RCU, so
5382 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005383 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005384 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02005385 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005386
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005387 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra51676952010-12-07 14:18:20 +01005388 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005389}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005390
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005391struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005392{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005393 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005394 int idx;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005395
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005396 idx = srcu_read_lock(&pmus_srcu);
5397 list_for_each_entry_rcu(pmu, &pmus, entry) {
5398 int ret = pmu->event_init(event);
5399 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005400 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005401
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005402 if (ret != -ENOENT) {
5403 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005404 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005405 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005406 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005407 pmu = ERR_PTR(-ENOENT);
5408unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005409 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005410
5411 return pmu;
5412}
5413
5414/*
5415 * Allocate and initialize a event structure
5416 */
5417static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005418perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005419 struct task_struct *task,
5420 struct perf_event *group_leader,
5421 struct perf_event *parent_event,
5422 perf_overflow_handler_t overflow_handler)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005423{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005424 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005425 struct perf_event *event;
5426 struct hw_perf_event *hwc;
5427 long err;
5428
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005429 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005430 if (!event)
5431 return ERR_PTR(-ENOMEM);
5432
5433 /*
5434 * Single events are their own group leaders, with an
5435 * empty sibling list:
5436 */
5437 if (!group_leader)
5438 group_leader = event;
5439
5440 mutex_init(&event->child_mutex);
5441 INIT_LIST_HEAD(&event->child_list);
5442
5443 INIT_LIST_HEAD(&event->group_entry);
5444 INIT_LIST_HEAD(&event->event_entry);
5445 INIT_LIST_HEAD(&event->sibling_list);
5446 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005447 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005448
5449 mutex_init(&event->mmap_mutex);
5450
5451 event->cpu = cpu;
5452 event->attr = *attr;
5453 event->group_leader = group_leader;
5454 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005455 event->oncpu = -1;
5456
5457 event->parent = parent_event;
5458
5459 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5460 event->id = atomic64_inc_return(&perf_event_id);
5461
5462 event->state = PERF_EVENT_STATE_INACTIVE;
5463
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005464 if (task) {
5465 event->attach_state = PERF_ATTACH_TASK;
5466#ifdef CONFIG_HAVE_HW_BREAKPOINT
5467 /*
5468 * hw_breakpoint is a bit difficult here..
5469 */
5470 if (attr->type == PERF_TYPE_BREAKPOINT)
5471 event->hw.bp_target = task;
5472#endif
5473 }
5474
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005475 if (!overflow_handler && parent_event)
5476 overflow_handler = parent_event->overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005477
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005478 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005479
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005480 if (attr->disabled)
5481 event->state = PERF_EVENT_STATE_OFF;
5482
5483 pmu = NULL;
5484
5485 hwc = &event->hw;
5486 hwc->sample_period = attr->sample_period;
5487 if (attr->freq && attr->sample_freq)
5488 hwc->sample_period = 1;
5489 hwc->last_period = hwc->sample_period;
5490
Peter Zijlstrae7850592010-05-21 14:43:08 +02005491 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005492
5493 /*
5494 * we currently do not support PERF_FORMAT_GROUP on inherited events
5495 */
5496 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5497 goto done;
5498
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005499 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005500
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005501done:
5502 err = 0;
5503 if (!pmu)
5504 err = -EINVAL;
5505 else if (IS_ERR(pmu))
5506 err = PTR_ERR(pmu);
5507
5508 if (err) {
5509 if (event->ns)
5510 put_pid_ns(event->ns);
5511 kfree(event);
5512 return ERR_PTR(err);
5513 }
5514
5515 event->pmu = pmu;
5516
5517 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02005518 if (event->attach_state & PERF_ATTACH_TASK)
5519 jump_label_inc(&perf_task_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01005520 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005521 atomic_inc(&nr_mmap_events);
5522 if (event->attr.comm)
5523 atomic_inc(&nr_comm_events);
5524 if (event->attr.task)
5525 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005526 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5527 err = get_callchain_buffers();
5528 if (err) {
5529 free_event(event);
5530 return ERR_PTR(err);
5531 }
5532 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005533 }
5534
5535 return event;
5536}
5537
5538static int perf_copy_attr(struct perf_event_attr __user *uattr,
5539 struct perf_event_attr *attr)
5540{
5541 u32 size;
5542 int ret;
5543
5544 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5545 return -EFAULT;
5546
5547 /*
5548 * zero the full structure, so that a short copy will be nice.
5549 */
5550 memset(attr, 0, sizeof(*attr));
5551
5552 ret = get_user(size, &uattr->size);
5553 if (ret)
5554 return ret;
5555
5556 if (size > PAGE_SIZE) /* silly large */
5557 goto err_size;
5558
5559 if (!size) /* abi compat */
5560 size = PERF_ATTR_SIZE_VER0;
5561
5562 if (size < PERF_ATTR_SIZE_VER0)
5563 goto err_size;
5564
5565 /*
5566 * If we're handed a bigger struct than we know of,
5567 * ensure all the unknown bits are 0 - i.e. new
5568 * user-space does not rely on any kernel feature
5569 * extensions we dont know about yet.
5570 */
5571 if (size > sizeof(*attr)) {
5572 unsigned char __user *addr;
5573 unsigned char __user *end;
5574 unsigned char val;
5575
5576 addr = (void __user *)uattr + sizeof(*attr);
5577 end = (void __user *)uattr + size;
5578
5579 for (; addr < end; addr++) {
5580 ret = get_user(val, addr);
5581 if (ret)
5582 return ret;
5583 if (val)
5584 goto err_size;
5585 }
5586 size = sizeof(*attr);
5587 }
5588
5589 ret = copy_from_user(attr, uattr, size);
5590 if (ret)
5591 return -EFAULT;
5592
5593 /*
5594 * If the type exists, the corresponding creation will verify
5595 * the attr->config.
5596 */
5597 if (attr->type >= PERF_TYPE_MAX)
5598 return -EINVAL;
5599
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05305600 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005601 return -EINVAL;
5602
5603 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5604 return -EINVAL;
5605
5606 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5607 return -EINVAL;
5608
5609out:
5610 return ret;
5611
5612err_size:
5613 put_user(sizeof(*attr), &uattr->size);
5614 ret = -E2BIG;
5615 goto out;
5616}
5617
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005618static int
5619perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005620{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005621 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005622 int ret = -EINVAL;
5623
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005624 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005625 goto set;
5626
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005627 /* don't allow circular references */
5628 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005629 goto out;
5630
Peter Zijlstra0f139302010-05-20 14:35:15 +02005631 /*
5632 * Don't allow cross-cpu buffers
5633 */
5634 if (output_event->cpu != event->cpu)
5635 goto out;
5636
5637 /*
5638 * If its not a per-cpu buffer, it must be the same task.
5639 */
5640 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5641 goto out;
5642
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005643set:
5644 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005645 /* Can't redirect output if we've got an active mmap() */
5646 if (atomic_read(&event->mmap_count))
5647 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005648
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005649 if (output_event) {
5650 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005651 buffer = perf_buffer_get(output_event);
5652 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005653 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005654 }
5655
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005656 old_buffer = event->buffer;
5657 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005658 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005659unlock:
5660 mutex_unlock(&event->mmap_mutex);
5661
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005662 if (old_buffer)
5663 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005664out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005665 return ret;
5666}
5667
5668/**
5669 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5670 *
5671 * @attr_uptr: event_id type attributes for monitoring/sampling
5672 * @pid: target pid
5673 * @cpu: target cpu
5674 * @group_fd: group leader event fd
5675 */
5676SYSCALL_DEFINE5(perf_event_open,
5677 struct perf_event_attr __user *, attr_uptr,
5678 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5679{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005680 struct perf_event *group_leader = NULL, *output_event = NULL;
5681 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005682 struct perf_event_attr attr;
5683 struct perf_event_context *ctx;
5684 struct file *event_file = NULL;
5685 struct file *group_file = NULL;
Matt Helsley38a81da2010-09-13 13:01:20 -07005686 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005687 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04005688 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005689 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005690 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005691 int err;
5692
5693 /* for future expandability... */
5694 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5695 return -EINVAL;
5696
5697 err = perf_copy_attr(attr_uptr, &attr);
5698 if (err)
5699 return err;
5700
5701 if (!attr.exclude_kernel) {
5702 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5703 return -EACCES;
5704 }
5705
5706 if (attr.freq) {
5707 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5708 return -EINVAL;
5709 }
5710
Al Viroea635c62010-05-26 17:40:29 -04005711 event_fd = get_unused_fd_flags(O_RDWR);
5712 if (event_fd < 0)
5713 return event_fd;
5714
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005715 if (group_fd != -1) {
5716 group_leader = perf_fget_light(group_fd, &fput_needed);
5717 if (IS_ERR(group_leader)) {
5718 err = PTR_ERR(group_leader);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005719 goto err_fd;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005720 }
5721 group_file = group_leader->filp;
5722 if (flags & PERF_FLAG_FD_OUTPUT)
5723 output_event = group_leader;
5724 if (flags & PERF_FLAG_FD_NO_GROUP)
5725 group_leader = NULL;
5726 }
5727
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005728 if (pid != -1) {
5729 task = find_lively_task_by_vpid(pid);
5730 if (IS_ERR(task)) {
5731 err = PTR_ERR(task);
5732 goto err_group_fd;
5733 }
5734 }
5735
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005736 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005737 if (IS_ERR(event)) {
5738 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005739 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005740 }
5741
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005742 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005743 * Special case software events and allow them to be part of
5744 * any hardware group.
5745 */
5746 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005747
5748 if (group_leader &&
5749 (is_software_event(event) != is_software_event(group_leader))) {
5750 if (is_software_event(event)) {
5751 /*
5752 * If event and group_leader are not both a software
5753 * event, and event is, then group leader is not.
5754 *
5755 * Allow the addition of software events to !software
5756 * groups, this is safe because software events never
5757 * fail to schedule.
5758 */
5759 pmu = group_leader->pmu;
5760 } else if (is_software_event(group_leader) &&
5761 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
5762 /*
5763 * In case the group is a pure software group, and we
5764 * try to add a hardware event, move the whole group to
5765 * the hardware context.
5766 */
5767 move_group = 1;
5768 }
5769 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005770
5771 /*
5772 * Get the target context (task or percpu):
5773 */
Matt Helsley38a81da2010-09-13 13:01:20 -07005774 ctx = find_get_context(pmu, task, cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005775 if (IS_ERR(ctx)) {
5776 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005777 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005778 }
5779
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005780 /*
5781 * Look up the group leader (we will attach this event to it):
5782 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005783 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005784 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005785
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005786 /*
5787 * Do not allow a recursive hierarchy (this new sibling
5788 * becoming part of another group-sibling):
5789 */
5790 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005791 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005792 /*
5793 * Do not allow to attach to a group in a different
5794 * task or CPU context:
5795 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005796 if (move_group) {
5797 if (group_leader->ctx->type != ctx->type)
5798 goto err_context;
5799 } else {
5800 if (group_leader->ctx != ctx)
5801 goto err_context;
5802 }
5803
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005804 /*
5805 * Only a group leader can be exclusive or pinned
5806 */
5807 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005808 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005809 }
5810
5811 if (output_event) {
5812 err = perf_event_set_output(event, output_event);
5813 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005814 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005815 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005816
Al Viroea635c62010-05-26 17:40:29 -04005817 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5818 if (IS_ERR(event_file)) {
5819 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005820 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04005821 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005822
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005823 if (move_group) {
5824 struct perf_event_context *gctx = group_leader->ctx;
5825
5826 mutex_lock(&gctx->mutex);
5827 perf_event_remove_from_context(group_leader);
5828 list_for_each_entry(sibling, &group_leader->sibling_list,
5829 group_entry) {
5830 perf_event_remove_from_context(sibling);
5831 put_ctx(gctx);
5832 }
5833 mutex_unlock(&gctx->mutex);
5834 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005835 }
5836
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005837 event->filp = event_file;
5838 WARN_ON_ONCE(ctx->parent_ctx);
5839 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005840
5841 if (move_group) {
5842 perf_install_in_context(ctx, group_leader, cpu);
5843 get_ctx(ctx);
5844 list_for_each_entry(sibling, &group_leader->sibling_list,
5845 group_entry) {
5846 perf_install_in_context(ctx, sibling, cpu);
5847 get_ctx(ctx);
5848 }
5849 }
5850
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005851 perf_install_in_context(ctx, event, cpu);
5852 ++ctx->generation;
5853 mutex_unlock(&ctx->mutex);
5854
5855 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01005856
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005857 mutex_lock(&current->perf_event_mutex);
5858 list_add_tail(&event->owner_entry, &current->perf_event_list);
5859 mutex_unlock(&current->perf_event_mutex);
5860
Peter Zijlstra8a495422010-05-27 15:47:49 +02005861 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005862 * Precalculate sample_data sizes
5863 */
5864 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02005865 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005866
5867 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02005868 * Drop the reference on the group_event after placing the
5869 * new event on the sibling_list. This ensures destruction
5870 * of the group leader will find the pointer to itself in
5871 * perf_group_detach().
5872 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005873 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005874 fd_install(event_fd, event_file);
5875 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005876
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005877err_context:
Al Viroea635c62010-05-26 17:40:29 -04005878 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005879err_alloc:
5880 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02005881err_task:
5882 if (task)
5883 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005884err_group_fd:
5885 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04005886err_fd:
5887 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005888 return err;
5889}
5890
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005891/**
5892 * perf_event_create_kernel_counter
5893 *
5894 * @attr: attributes of the counter to create
5895 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07005896 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005897 */
5898struct perf_event *
5899perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07005900 struct task_struct *task,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005901 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005902{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005903 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005904 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005905 int err;
5906
5907 /*
5908 * Get the target context (task or percpu):
5909 */
5910
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005911 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005912 if (IS_ERR(event)) {
5913 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005914 goto err;
5915 }
5916
Matt Helsley38a81da2010-09-13 13:01:20 -07005917 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005918 if (IS_ERR(ctx)) {
5919 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005920 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005921 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005922
5923 event->filp = NULL;
5924 WARN_ON_ONCE(ctx->parent_ctx);
5925 mutex_lock(&ctx->mutex);
5926 perf_install_in_context(ctx, event, cpu);
5927 ++ctx->generation;
5928 mutex_unlock(&ctx->mutex);
5929
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005930 return event;
5931
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005932err_free:
5933 free_event(event);
5934err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01005935 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02005936}
5937EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
5938
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005939static void sync_child_event(struct perf_event *child_event,
5940 struct task_struct *child)
5941{
5942 struct perf_event *parent_event = child_event->parent;
5943 u64 child_val;
5944
5945 if (child_event->attr.inherit_stat)
5946 perf_event_read_event(child_event, child);
5947
Peter Zijlstrab5e58792010-05-21 14:43:12 +02005948 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005949
5950 /*
5951 * Add back the child's count to the parent's count:
5952 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02005953 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005954 atomic64_add(child_event->total_time_enabled,
5955 &parent_event->child_total_time_enabled);
5956 atomic64_add(child_event->total_time_running,
5957 &parent_event->child_total_time_running);
5958
5959 /*
5960 * Remove this event from the parent's list
5961 */
5962 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
5963 mutex_lock(&parent_event->child_mutex);
5964 list_del_init(&child_event->child_list);
5965 mutex_unlock(&parent_event->child_mutex);
5966
5967 /*
5968 * Release the parent event, if this was the last
5969 * reference to it.
5970 */
5971 fput(parent_event->filp);
5972}
5973
5974static void
5975__perf_event_exit_task(struct perf_event *child_event,
5976 struct perf_event_context *child_ctx,
5977 struct task_struct *child)
5978{
5979 struct perf_event *parent_event;
5980
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005981 perf_event_remove_from_context(child_event);
5982
5983 parent_event = child_event->parent;
5984 /*
5985 * It can happen that parent exits first, and has events
5986 * that are still around due to the child reference. These
5987 * events need to be zapped - but otherwise linger.
5988 */
5989 if (parent_event) {
5990 sync_child_event(child_event, child);
5991 free_event(child_event);
5992 }
5993}
5994
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02005995static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005996{
5997 struct perf_event *child_event, *tmp;
5998 struct perf_event_context *child_ctx;
5999 unsigned long flags;
6000
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006001 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006002 perf_event_task(child, NULL, 0);
6003 return;
6004 }
6005
6006 local_irq_save(flags);
6007 /*
6008 * We can't reschedule here because interrupts are disabled,
6009 * and either child is current or it is a task that can't be
6010 * scheduled, so we are now safe from rescheduling changing
6011 * our context.
6012 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006013 child_ctx = child->perf_event_ctxp[ctxn];
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02006014 task_ctx_sched_out(child_ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006015
6016 /*
6017 * Take the context lock here so that if find_get_context is
6018 * reading child->perf_event_ctxp, we wait until it has
6019 * incremented the context's refcount before we do put_ctx below.
6020 */
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006021 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006022 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006023 /*
6024 * If this context is a clone; unclone it so it can't get
6025 * swapped to another process while we're removing all
6026 * the events from it.
6027 */
6028 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01006029 update_context_time(child_ctx);
Thomas Gleixnere625cce12009-11-17 18:02:06 +01006030 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006031
6032 /*
6033 * Report the task dead after unscheduling the events so that we
6034 * won't get any samples after PERF_RECORD_EXIT. We can however still
6035 * get a few PERF_RECORD_READ events.
6036 */
6037 perf_event_task(child, child_ctx, 0);
6038
6039 /*
6040 * We can recurse on the same lock type through:
6041 *
6042 * __perf_event_exit_task()
6043 * sync_child_event()
6044 * fput(parent_event->filp)
6045 * perf_release()
6046 * mutex_lock(&ctx->mutex)
6047 *
6048 * But since its the parent context it won't be the same instance.
6049 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02006050 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006051
6052again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006053 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6054 group_entry)
6055 __perf_event_exit_task(child_event, child_ctx, child);
6056
6057 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006058 group_entry)
6059 __perf_event_exit_task(child_event, child_ctx, child);
6060
6061 /*
6062 * If the last event was a group event, it will have appended all
6063 * its siblings to the list, but we obtained 'tmp' before that which
6064 * will still point to the list head terminating the iteration.
6065 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006066 if (!list_empty(&child_ctx->pinned_groups) ||
6067 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006068 goto again;
6069
6070 mutex_unlock(&child_ctx->mutex);
6071
6072 put_ctx(child_ctx);
6073}
6074
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006075/*
6076 * When a child task exits, feed back event values to parent events.
6077 */
6078void perf_event_exit_task(struct task_struct *child)
6079{
Peter Zijlstra88821352010-11-09 19:01:43 +01006080 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006081 int ctxn;
6082
Peter Zijlstra88821352010-11-09 19:01:43 +01006083 mutex_lock(&child->perf_event_mutex);
6084 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6085 owner_entry) {
6086 list_del_init(&event->owner_entry);
6087
6088 /*
6089 * Ensure the list deletion is visible before we clear
6090 * the owner, closes a race against perf_release() where
6091 * we need to serialize on the owner->perf_event_mutex.
6092 */
6093 smp_wmb();
6094 event->owner = NULL;
6095 }
6096 mutex_unlock(&child->perf_event_mutex);
6097
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006098 for_each_task_context_nr(ctxn)
6099 perf_event_exit_task_context(child, ctxn);
6100}
6101
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006102static void perf_free_event(struct perf_event *event,
6103 struct perf_event_context *ctx)
6104{
6105 struct perf_event *parent = event->parent;
6106
6107 if (WARN_ON_ONCE(!parent))
6108 return;
6109
6110 mutex_lock(&parent->child_mutex);
6111 list_del_init(&event->child_list);
6112 mutex_unlock(&parent->child_mutex);
6113
6114 fput(parent->filp);
6115
Peter Zijlstra8a495422010-05-27 15:47:49 +02006116 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006117 list_del_event(event, ctx);
6118 free_event(event);
6119}
6120
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006121/*
6122 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006123 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006124 */
6125void perf_event_free_task(struct task_struct *task)
6126{
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006127 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006128 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006129 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006130
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006131 for_each_task_context_nr(ctxn) {
6132 ctx = task->perf_event_ctxp[ctxn];
6133 if (!ctx)
6134 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006135
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006136 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006137again:
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006138 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6139 group_entry)
6140 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006141
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006142 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6143 group_entry)
6144 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006145
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006146 if (!list_empty(&ctx->pinned_groups) ||
6147 !list_empty(&ctx->flexible_groups))
6148 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006149
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006150 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006151
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006152 put_ctx(ctx);
6153 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006154}
6155
Peter Zijlstra4e231c72010-09-09 21:01:59 +02006156void perf_event_delayed_put(struct task_struct *task)
6157{
6158 int ctxn;
6159
6160 for_each_task_context_nr(ctxn)
6161 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6162}
6163
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006164/*
6165 * inherit a event from parent task to child task:
6166 */
6167static struct perf_event *
6168inherit_event(struct perf_event *parent_event,
6169 struct task_struct *parent,
6170 struct perf_event_context *parent_ctx,
6171 struct task_struct *child,
6172 struct perf_event *group_leader,
6173 struct perf_event_context *child_ctx)
6174{
6175 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02006176 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006177
6178 /*
6179 * Instead of creating recursive hierarchies of events,
6180 * we link inherited events back to the original parent,
6181 * which has a filp for sure, which we use as the reference
6182 * count:
6183 */
6184 if (parent_event->parent)
6185 parent_event = parent_event->parent;
6186
6187 child_event = perf_event_alloc(&parent_event->attr,
6188 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006189 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006190 group_leader, parent_event,
6191 NULL);
6192 if (IS_ERR(child_event))
6193 return child_event;
6194 get_ctx(child_ctx);
6195
6196 /*
6197 * Make the child state follow the state of the parent event,
6198 * not its attr.disabled bit. We hold the parent's mutex,
6199 * so we won't race with perf_event_{en, dis}able_family.
6200 */
6201 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6202 child_event->state = PERF_EVENT_STATE_INACTIVE;
6203 else
6204 child_event->state = PERF_EVENT_STATE_OFF;
6205
6206 if (parent_event->attr.freq) {
6207 u64 sample_period = parent_event->hw.sample_period;
6208 struct hw_perf_event *hwc = &child_event->hw;
6209
6210 hwc->sample_period = sample_period;
6211 hwc->last_period = sample_period;
6212
6213 local64_set(&hwc->period_left, sample_period);
6214 }
6215
6216 child_event->ctx = child_ctx;
6217 child_event->overflow_handler = parent_event->overflow_handler;
6218
6219 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02006220 * Precalculate sample_data sizes
6221 */
6222 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006223 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02006224
6225 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006226 * Link it up in the child's context:
6227 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02006228 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006229 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02006230 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006231
6232 /*
6233 * Get a reference to the parent filp - we will fput it
6234 * when the child event exits. This is safe to do because
6235 * we are in the parent and we know that the filp still
6236 * exists and has a nonzero count:
6237 */
6238 atomic_long_inc(&parent_event->filp->f_count);
6239
6240 /*
6241 * Link this into the parent event's child list
6242 */
6243 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6244 mutex_lock(&parent_event->child_mutex);
6245 list_add_tail(&child_event->child_list, &parent_event->child_list);
6246 mutex_unlock(&parent_event->child_mutex);
6247
6248 return child_event;
6249}
6250
6251static int inherit_group(struct perf_event *parent_event,
6252 struct task_struct *parent,
6253 struct perf_event_context *parent_ctx,
6254 struct task_struct *child,
6255 struct perf_event_context *child_ctx)
6256{
6257 struct perf_event *leader;
6258 struct perf_event *sub;
6259 struct perf_event *child_ctr;
6260
6261 leader = inherit_event(parent_event, parent, parent_ctx,
6262 child, NULL, child_ctx);
6263 if (IS_ERR(leader))
6264 return PTR_ERR(leader);
6265 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6266 child_ctr = inherit_event(sub, parent, parent_ctx,
6267 child, leader, child_ctx);
6268 if (IS_ERR(child_ctr))
6269 return PTR_ERR(child_ctr);
6270 }
6271 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006272}
6273
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006274static int
6275inherit_task_group(struct perf_event *event, struct task_struct *parent,
6276 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006277 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006278 int *inherited_all)
6279{
6280 int ret;
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006281 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006282
6283 if (!event->attr.inherit) {
6284 *inherited_all = 0;
6285 return 0;
6286 }
6287
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006288 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006289 if (!child_ctx) {
6290 /*
6291 * This is executed from the parent task context, so
6292 * inherit events that have been marked for cloning.
6293 * First allocate and initialize a context for the
6294 * child.
6295 */
6296
Peter Zijlstraeb184472010-09-07 15:55:13 +02006297 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006298 if (!child_ctx)
6299 return -ENOMEM;
6300
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006301 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006302 }
6303
6304 ret = inherit_group(event, parent, parent_ctx,
6305 child, child_ctx);
6306
6307 if (ret)
6308 *inherited_all = 0;
6309
6310 return ret;
6311}
6312
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006313/*
6314 * Initialize the perf_event context in task_struct
6315 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006316int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006317{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006318 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006319 struct perf_event_context *cloned_ctx;
6320 struct perf_event *event;
6321 struct task_struct *parent = current;
6322 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006323 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006324 int ret = 0;
6325
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006326 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006327
6328 mutex_init(&child->perf_event_mutex);
6329 INIT_LIST_HEAD(&child->perf_event_list);
6330
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006331 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006332 return 0;
6333
6334 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006335 * If the parent's context is a clone, pin it so it won't get
6336 * swapped under us.
6337 */
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006338 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006339
6340 /*
6341 * No need to check if parent_ctx != NULL here; since we saw
6342 * it non-NULL earlier, the only reason for it to become NULL
6343 * is if we exit, and since we're currently in the middle of
6344 * a fork we can't be exiting at the same time.
6345 */
6346
6347 /*
6348 * Lock the parent list. No need to lock the child - not PID
6349 * hashed yet and not running, so nobody can access it.
6350 */
6351 mutex_lock(&parent_ctx->mutex);
6352
6353 /*
6354 * We dont have to disable NMIs - we are only looking at
6355 * the list, not manipulating it:
6356 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006357 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006358 ret = inherit_task_group(event, parent, parent_ctx,
6359 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006360 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006361 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006362 }
6363
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006364 /*
6365 * We can't hold ctx->lock when iterating the ->flexible_group list due
6366 * to allocations, but we need to prevent rotation because
6367 * rotate_ctx() will change the list from interrupt context.
6368 */
6369 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6370 parent_ctx->rotate_disable = 1;
6371 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6372
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006373 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006374 ret = inherit_task_group(event, parent, parent_ctx,
6375 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006376 if (ret)
6377 break;
6378 }
6379
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006380 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6381 parent_ctx->rotate_disable = 0;
6382 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6383
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006384 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006385
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01006386 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006387 /*
6388 * Mark the child context as a clone of the parent
6389 * context, or of whatever the parent is a clone of.
6390 * Note that if the parent is a clone, it could get
6391 * uncloned at any point, but that doesn't matter
6392 * because the list of events and the generation
6393 * count can't have changed since we took the mutex.
6394 */
6395 cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
6396 if (cloned_ctx) {
6397 child_ctx->parent_ctx = cloned_ctx;
6398 child_ctx->parent_gen = parent_ctx->parent_gen;
6399 } else {
6400 child_ctx->parent_ctx = parent_ctx;
6401 child_ctx->parent_gen = parent_ctx->generation;
6402 }
6403 get_ctx(child_ctx->parent_ctx);
6404 }
6405
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006406 mutex_unlock(&parent_ctx->mutex);
6407
6408 perf_unpin_context(parent_ctx);
6409
6410 return ret;
6411}
6412
Peter Zijlstra8dc85d5472010-09-02 16:50:03 +02006413/*
6414 * Initialize the perf_event context in task_struct
6415 */
6416int perf_event_init_task(struct task_struct *child)
6417{
6418 int ctxn, ret;
6419
6420 for_each_task_context_nr(ctxn) {
6421 ret = perf_event_init_context(child, ctxn);
6422 if (ret)
6423 return ret;
6424 }
6425
6426 return 0;
6427}
6428
Paul Mackerras220b1402010-03-10 20:45:52 +11006429static void __init perf_event_init_all_cpus(void)
6430{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006431 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11006432 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11006433
6434 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006435 swhash = &per_cpu(swevent_htable, cpu);
6436 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006437 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11006438 }
6439}
6440
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006441static void __cpuinit perf_event_init_cpu(int cpu)
6442{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006443 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006444
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006445 mutex_lock(&swhash->hlist_mutex);
6446 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006447 struct swevent_hlist *hlist;
6448
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006449 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6450 WARN_ON(!hlist);
6451 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006452 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006453 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006454}
6455
Peter Zijlstrac2774432010-12-08 15:29:02 +01006456#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006457static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006458{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006459 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6460
6461 WARN_ON(!irqs_disabled());
6462
6463 list_del_init(&cpuctx->rotation_list);
6464}
6465
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006466static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006467{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006468 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006469 struct perf_event *event, *tmp;
6470
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006471 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006472
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006473 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6474 __perf_event_remove_from_context(event);
6475 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006476 __perf_event_remove_from_context(event);
6477}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006478
6479static void perf_event_exit_cpu_context(int cpu)
6480{
6481 struct perf_event_context *ctx;
6482 struct pmu *pmu;
6483 int idx;
6484
6485 idx = srcu_read_lock(&pmus_srcu);
6486 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02006487 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006488
6489 mutex_lock(&ctx->mutex);
6490 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6491 mutex_unlock(&ctx->mutex);
6492 }
6493 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006494}
6495
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006496static void perf_event_exit_cpu(int cpu)
6497{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006498 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006499
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006500 mutex_lock(&swhash->hlist_mutex);
6501 swevent_hlist_release(swhash);
6502 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006503
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006504 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006505}
6506#else
6507static inline void perf_event_exit_cpu(int cpu) { }
6508#endif
6509
Peter Zijlstrac2774432010-12-08 15:29:02 +01006510static int
6511perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
6512{
6513 int cpu;
6514
6515 for_each_online_cpu(cpu)
6516 perf_event_exit_cpu(cpu);
6517
6518 return NOTIFY_OK;
6519}
6520
6521/*
6522 * Run the perf reboot notifier at the very last possible moment so that
6523 * the generic watchdog code runs as long as possible.
6524 */
6525static struct notifier_block perf_reboot_notifier = {
6526 .notifier_call = perf_reboot,
6527 .priority = INT_MIN,
6528};
6529
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006530static int __cpuinit
6531perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6532{
6533 unsigned int cpu = (long)hcpu;
6534
Peter Zijlstra5e116372010-06-11 13:35:08 +02006535 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006536
6537 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02006538 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006539 perf_event_init_cpu(cpu);
6540 break;
6541
Peter Zijlstra5e116372010-06-11 13:35:08 +02006542 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006543 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006544 perf_event_exit_cpu(cpu);
6545 break;
6546
6547 default:
6548 break;
6549 }
6550
6551 return NOTIFY_OK;
6552}
6553
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006554void __init perf_event_init(void)
6555{
Jason Wessel3c502e72010-11-04 17:33:01 -05006556 int ret;
6557
Paul Mackerras220b1402010-03-10 20:45:52 +11006558 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006559 init_srcu_struct(&pmus_srcu);
6560 perf_pmu_register(&perf_swevent);
6561 perf_pmu_register(&perf_cpu_clock);
6562 perf_pmu_register(&perf_task_clock);
6563 perf_tp_register();
6564 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01006565 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05006566
6567 ret = init_hw_breakpoint();
6568 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006569}