blob: 656222fcf767e4442acaf291a9bca566f9332d00 [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>
Peter Zijlstra2e80a822010-11-17 23:17:36 +010016#include <linux/idr.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/file.h>
18#include <linux/poll.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Frederic Weisbecker76e1d902010-04-05 15:35:57 +020020#include <linux/hash.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020021#include <linux/sysfs.h>
22#include <linux/dcache.h>
23#include <linux/percpu.h>
24#include <linux/ptrace.h>
Peter Zijlstrac2774432010-12-08 15:29:02 +010025#include <linux/reboot.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020026#include <linux/vmstat.h>
Peter Zijlstraabe43402010-11-17 23:17:37 +010027#include <linux/device.h>
Peter Zijlstra906010b2009-09-21 16:08:49 +020028#include <linux/vmalloc.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020029#include <linux/hardirq.h>
30#include <linux/rculist.h>
31#include <linux/uaccess.h>
32#include <linux/syscalls.h>
33#include <linux/anon_inodes.h>
34#include <linux/kernel_stat.h>
35#include <linux/perf_event.h>
Li Zefan6fb29152009-10-15 11:21:42 +080036#include <linux/ftrace_event.h>
Jason Wessel3c502e72010-11-04 17:33:01 -050037#include <linux/hw_breakpoint.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020038
39#include <asm/irq_regs.h>
40
Stephane Eranian0b3fcf12011-01-03 18:20:01 +020041enum event_type_t {
42 EVENT_FLEXIBLE = 0x1,
43 EVENT_PINNED = 0x2,
44 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
45};
46
Peter Zijlstra82cd6de2010-10-14 17:57:23 +020047atomic_t perf_task_events __read_mostly;
Ingo Molnarcdd6c482009-09-21 12:02:48 +020048static atomic_t nr_mmap_events __read_mostly;
49static atomic_t nr_comm_events __read_mostly;
50static atomic_t nr_task_events __read_mostly;
51
Peter Zijlstra108b02c2010-09-06 14:32:03 +020052static LIST_HEAD(pmus);
53static DEFINE_MUTEX(pmus_lock);
54static struct srcu_struct pmus_srcu;
55
Ingo Molnarcdd6c482009-09-21 12:02:48 +020056/*
57 * perf event paranoia level:
58 * -1 - not paranoid at all
59 * 0 - disallow raw tracepoint access for unpriv
60 * 1 - disallow cpu events for unpriv
61 * 2 - disallow kernel profiling for unpriv
62 */
63int sysctl_perf_event_paranoid __read_mostly = 1;
64
Ingo Molnarcdd6c482009-09-21 12:02:48 +020065int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
66
67/*
68 * max perf event sample rate
69 */
70int sysctl_perf_event_sample_rate __read_mostly = 100000;
71
72static atomic64_t perf_event_id;
73
Stephane Eranian0b3fcf12011-01-03 18:20:01 +020074static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
75 enum event_type_t event_type);
76
77static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
78 enum event_type_t event_type);
79
Ingo Molnarcdd6c482009-09-21 12:02:48 +020080void __weak perf_event_print_debug(void) { }
81
Matt Fleming84c79912010-10-03 21:41:13 +010082extern __weak const char *perf_pmu_name(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020083{
Matt Fleming84c79912010-10-03 21:41:13 +010084 return "pmu";
Ingo Molnarcdd6c482009-09-21 12:02:48 +020085}
86
Stephane Eranian0b3fcf12011-01-03 18:20:01 +020087static inline u64 perf_clock(void)
88{
89 return local_clock();
90}
91
Peter Zijlstra33696fc2010-06-14 08:49:00 +020092void perf_pmu_disable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +020093{
Peter Zijlstra33696fc2010-06-14 08:49:00 +020094 int *count = this_cpu_ptr(pmu->pmu_disable_count);
95 if (!(*count)++)
96 pmu->pmu_disable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +020097}
98
Peter Zijlstra33696fc2010-06-14 08:49:00 +020099void perf_pmu_enable(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200100{
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200101 int *count = this_cpu_ptr(pmu->pmu_disable_count);
102 if (!--(*count))
103 pmu->pmu_enable(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200104}
105
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200106static DEFINE_PER_CPU(struct list_head, rotation_list);
107
108/*
109 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
110 * because they're strictly cpu affine and rotate_start is called with IRQs
111 * disabled, while rotate_context is called from IRQ context.
112 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200113static void perf_pmu_rotate_start(struct pmu *pmu)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200114{
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200115 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200116 struct list_head *head = &__get_cpu_var(rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200117
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200118 WARN_ON(!irqs_disabled());
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200119
Peter Zijlstrae9d2b062010-09-17 11:28:50 +0200120 if (list_empty(&cpuctx->rotation_list))
121 list_add(&cpuctx->rotation_list, head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200122}
123
124static void get_ctx(struct perf_event_context *ctx)
125{
126 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
127}
128
129static void free_ctx(struct rcu_head *head)
130{
131 struct perf_event_context *ctx;
132
133 ctx = container_of(head, struct perf_event_context, rcu_head);
134 kfree(ctx);
135}
136
137static void put_ctx(struct perf_event_context *ctx)
138{
139 if (atomic_dec_and_test(&ctx->refcount)) {
140 if (ctx->parent_ctx)
141 put_ctx(ctx->parent_ctx);
142 if (ctx->task)
143 put_task_struct(ctx->task);
144 call_rcu(&ctx->rcu_head, free_ctx);
145 }
146}
147
148static void unclone_ctx(struct perf_event_context *ctx)
149{
150 if (ctx->parent_ctx) {
151 put_ctx(ctx->parent_ctx);
152 ctx->parent_ctx = NULL;
153 }
154}
155
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200156static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
157{
158 /*
159 * only top level events have the pid namespace they were created in
160 */
161 if (event->parent)
162 event = event->parent;
163
164 return task_tgid_nr_ns(p, event->ns);
165}
166
167static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
168{
169 /*
170 * only top level events have the pid namespace they were created in
171 */
172 if (event->parent)
173 event = event->parent;
174
175 return task_pid_nr_ns(p, event->ns);
176}
177
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200178/*
179 * If we inherit events we want to return the parent event id
180 * to userspace.
181 */
182static u64 primary_event_id(struct perf_event *event)
183{
184 u64 id = event->id;
185
186 if (event->parent)
187 id = event->parent->id;
188
189 return id;
190}
191
192/*
193 * Get the perf_event_context for a task and lock it.
194 * This has to cope with with the fact that until it is locked,
195 * the context could get moved to another task.
196 */
197static struct perf_event_context *
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200198perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200199{
200 struct perf_event_context *ctx;
201
202 rcu_read_lock();
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200203retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200204 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200205 if (ctx) {
206 /*
207 * If this context is a clone of another, it might
208 * get swapped for another underneath us by
209 * perf_event_task_sched_out, though the
210 * rcu_read_lock() protects us from any context
211 * getting freed. Lock the context and check if it
212 * got swapped before we could get the lock, and retry
213 * if so. If we locked the right context, then it
214 * can't get swapped on us any more.
215 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100216 raw_spin_lock_irqsave(&ctx->lock, *flags);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200217 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100218 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200219 goto retry;
220 }
221
222 if (!atomic_inc_not_zero(&ctx->refcount)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100223 raw_spin_unlock_irqrestore(&ctx->lock, *flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200224 ctx = NULL;
225 }
226 }
227 rcu_read_unlock();
228 return ctx;
229}
230
231/*
232 * Get the context for a task and increment its pin_count so it
233 * can't get swapped to another task. This also increments its
234 * reference count so that the context can't get freed.
235 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200236static struct perf_event_context *
237perf_pin_task_context(struct task_struct *task, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200238{
239 struct perf_event_context *ctx;
240 unsigned long flags;
241
Peter Zijlstra8dc85d52010-09-02 16:50:03 +0200242 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200243 if (ctx) {
244 ++ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100245 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200246 }
247 return ctx;
248}
249
250static void perf_unpin_context(struct perf_event_context *ctx)
251{
252 unsigned long flags;
253
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100254 raw_spin_lock_irqsave(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200255 --ctx->pin_count;
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100256 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200257 put_ctx(ctx);
258}
259
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100260/*
261 * Update the record of the current time in a context.
262 */
263static void update_context_time(struct perf_event_context *ctx)
264{
265 u64 now = perf_clock();
266
267 ctx->time += now - ctx->timestamp;
268 ctx->timestamp = now;
269}
270
Stephane Eranian41587552011-01-03 18:20:01 +0200271static u64 perf_event_time(struct perf_event *event)
272{
273 struct perf_event_context *ctx = event->ctx;
274 return ctx ? ctx->time : 0;
275}
276
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100277/*
278 * Update the total_time_enabled and total_time_running fields for a event.
279 */
280static void update_event_times(struct perf_event *event)
281{
282 struct perf_event_context *ctx = event->ctx;
283 u64 run_end;
284
285 if (event->state < PERF_EVENT_STATE_INACTIVE ||
286 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
287 return;
288
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100289 if (ctx->is_active)
Stephane Eranian41587552011-01-03 18:20:01 +0200290 run_end = perf_event_time(event);
Peter Zijlstraacd1d7c2009-11-23 15:00:36 +0100291 else
292 run_end = event->tstamp_stopped;
293
294 event->total_time_enabled = run_end - event->tstamp_enabled;
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100295
296 if (event->state == PERF_EVENT_STATE_INACTIVE)
297 run_end = event->tstamp_stopped;
298 else
Stephane Eranian41587552011-01-03 18:20:01 +0200299 run_end = perf_event_time(event);
Peter Zijlstraf67218c2009-11-23 11:37:27 +0100300
301 event->total_time_running = run_end - event->tstamp_running;
302}
303
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200304/*
305 * Update total_time_enabled and total_time_running for all events in a group.
306 */
307static void update_group_times(struct perf_event *leader)
308{
309 struct perf_event *event;
310
311 update_event_times(leader);
312 list_for_each_entry(event, &leader->sibling_list, group_entry)
313 update_event_times(event);
314}
315
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100316static struct list_head *
317ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
318{
319 if (event->attr.pinned)
320 return &ctx->pinned_groups;
321 else
322 return &ctx->flexible_groups;
323}
324
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200325/*
326 * Add a event from the lists for its context.
327 * Must be called with ctx->mutex and ctx->lock held.
328 */
329static void
330list_add_event(struct perf_event *event, struct perf_event_context *ctx)
331{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200332 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
333 event->attach_state |= PERF_ATTACH_CONTEXT;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200334
335 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +0200336 * If we're a stand alone event or group leader, we go to the context
337 * list, group events are kept attached to the group so that
338 * perf_group_detach can, at all times, locate all siblings.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200339 */
Peter Zijlstra8a495422010-05-27 15:47:49 +0200340 if (event->group_leader == event) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100341 struct list_head *list;
342
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100343 if (is_software_event(event))
344 event->group_flags |= PERF_GROUP_SOFTWARE;
345
Frederic Weisbecker889ff012010-01-09 20:04:47 +0100346 list = ctx_group_list(event, ctx);
347 list_add_tail(&event->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200348 }
349
350 list_add_rcu(&event->event_entry, &ctx->event_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +0200351 if (!ctx->nr_events)
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200352 perf_pmu_rotate_start(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200353 ctx->nr_events++;
354 if (event->attr.inherit_stat)
355 ctx->nr_stat++;
356}
357
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200358/*
359 * Called at perf_event creation and when events are attached/detached from a
360 * group.
361 */
362static void perf_event__read_size(struct perf_event *event)
363{
364 int entry = sizeof(u64); /* value */
365 int size = 0;
366 int nr = 1;
367
368 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
369 size += sizeof(u64);
370
371 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
372 size += sizeof(u64);
373
374 if (event->attr.read_format & PERF_FORMAT_ID)
375 entry += sizeof(u64);
376
377 if (event->attr.read_format & PERF_FORMAT_GROUP) {
378 nr += event->group_leader->nr_siblings;
379 size += sizeof(u64);
380 }
381
382 size += entry * nr;
383 event->read_size = size;
384}
385
386static void perf_event__header_size(struct perf_event *event)
387{
388 struct perf_sample_data *data;
389 u64 sample_type = event->attr.sample_type;
390 u16 size = 0;
391
392 perf_event__read_size(event);
393
394 if (sample_type & PERF_SAMPLE_IP)
395 size += sizeof(data->ip);
396
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200397 if (sample_type & PERF_SAMPLE_ADDR)
398 size += sizeof(data->addr);
399
400 if (sample_type & PERF_SAMPLE_PERIOD)
401 size += sizeof(data->period);
402
403 if (sample_type & PERF_SAMPLE_READ)
404 size += event->read_size;
405
406 event->header_size = size;
407}
408
409static void perf_event__id_header_size(struct perf_event *event)
410{
411 struct perf_sample_data *data;
412 u64 sample_type = event->attr.sample_type;
413 u16 size = 0;
414
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200415 if (sample_type & PERF_SAMPLE_TID)
416 size += sizeof(data->tid_entry);
417
418 if (sample_type & PERF_SAMPLE_TIME)
419 size += sizeof(data->time);
420
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200421 if (sample_type & PERF_SAMPLE_ID)
422 size += sizeof(data->id);
423
424 if (sample_type & PERF_SAMPLE_STREAM_ID)
425 size += sizeof(data->stream_id);
426
427 if (sample_type & PERF_SAMPLE_CPU)
428 size += sizeof(data->cpu_entry);
429
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -0200430 event->id_header_size = size;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200431}
432
Peter Zijlstra8a495422010-05-27 15:47:49 +0200433static void perf_group_attach(struct perf_event *event)
434{
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200435 struct perf_event *group_leader = event->group_leader, *pos;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200436
Peter Zijlstra74c33372010-10-15 11:40:29 +0200437 /*
438 * We can have double attach due to group movement in perf_event_open.
439 */
440 if (event->attach_state & PERF_ATTACH_GROUP)
441 return;
442
Peter Zijlstra8a495422010-05-27 15:47:49 +0200443 event->attach_state |= PERF_ATTACH_GROUP;
444
445 if (group_leader == event)
446 return;
447
448 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
449 !is_software_event(event))
450 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
451
452 list_add_tail(&event->group_entry, &group_leader->sibling_list);
453 group_leader->nr_siblings++;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200454
455 perf_event__header_size(group_leader);
456
457 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
458 perf_event__header_size(pos);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200459}
460
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200461/*
462 * Remove a event from the lists for its context.
463 * Must be called with ctx->mutex and ctx->lock held.
464 */
465static void
466list_del_event(struct perf_event *event, struct perf_event_context *ctx)
467{
Peter Zijlstra8a495422010-05-27 15:47:49 +0200468 /*
469 * We can have double detach due to exit/hot-unplug + close.
470 */
471 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200472 return;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200473
474 event->attach_state &= ~PERF_ATTACH_CONTEXT;
475
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200476 ctx->nr_events--;
477 if (event->attr.inherit_stat)
478 ctx->nr_stat--;
479
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200480 list_del_rcu(&event->event_entry);
481
Peter Zijlstra8a495422010-05-27 15:47:49 +0200482 if (event->group_leader == event)
483 list_del_init(&event->group_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200484
Peter Zijlstra96c21a42010-05-11 16:19:10 +0200485 update_group_times(event);
Stephane Eranianb2e74a22009-11-26 09:24:30 -0800486
487 /*
488 * If event was in error state, then keep it
489 * that way, otherwise bogus counts will be
490 * returned on read(). The only way to get out
491 * of error state is by explicit re-enabling
492 * of the event
493 */
494 if (event->state > PERF_EVENT_STATE_OFF)
495 event->state = PERF_EVENT_STATE_OFF;
Peter Zijlstra050735b2010-05-11 11:51:53 +0200496}
497
Peter Zijlstra8a495422010-05-27 15:47:49 +0200498static void perf_group_detach(struct perf_event *event)
Peter Zijlstra050735b2010-05-11 11:51:53 +0200499{
500 struct perf_event *sibling, *tmp;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200501 struct list_head *list = NULL;
502
503 /*
504 * We can have double detach due to exit/hot-unplug + close.
505 */
506 if (!(event->attach_state & PERF_ATTACH_GROUP))
507 return;
508
509 event->attach_state &= ~PERF_ATTACH_GROUP;
510
511 /*
512 * If this is a sibling, remove it from its group.
513 */
514 if (event->group_leader != event) {
515 list_del_init(&event->group_entry);
516 event->group_leader->nr_siblings--;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200517 goto out;
Peter Zijlstra8a495422010-05-27 15:47:49 +0200518 }
519
520 if (!list_empty(&event->group_entry))
521 list = &event->group_entry;
Peter Zijlstra2e2af502009-11-23 11:37:25 +0100522
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200523 /*
524 * If this was a group event with sibling events then
525 * upgrade the siblings to singleton events by adding them
Peter Zijlstra8a495422010-05-27 15:47:49 +0200526 * to whatever list we are on.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200527 */
528 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
Peter Zijlstra8a495422010-05-27 15:47:49 +0200529 if (list)
530 list_move_tail(&sibling->group_entry, list);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200531 sibling->group_leader = sibling;
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100532
533 /* Inherit group flags from the previous leader */
534 sibling->group_flags = event->group_flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200535 }
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -0200536
537out:
538 perf_event__header_size(event->group_leader);
539
540 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
541 perf_event__header_size(tmp);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200542}
543
Stephane Eranianfa66f072010-08-26 16:40:01 +0200544static inline int
545event_filter_match(struct perf_event *event)
546{
547 return event->cpu == -1 || event->cpu == smp_processor_id();
548}
549
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200550static void
551event_sched_out(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200552 struct perf_cpu_context *cpuctx,
553 struct perf_event_context *ctx)
554{
Stephane Eranian41587552011-01-03 18:20:01 +0200555 u64 tstamp = perf_event_time(event);
Stephane Eranianfa66f072010-08-26 16:40:01 +0200556 u64 delta;
557 /*
558 * An event which could not be activated because of
559 * filter mismatch still needs to have its timings
560 * maintained, otherwise bogus information is return
561 * via read() for time_enabled, time_running:
562 */
563 if (event->state == PERF_EVENT_STATE_INACTIVE
564 && !event_filter_match(event)) {
565 delta = ctx->time - event->tstamp_stopped;
566 event->tstamp_running += delta;
Stephane Eranian41587552011-01-03 18:20:01 +0200567 event->tstamp_stopped = tstamp;
Stephane Eranianfa66f072010-08-26 16:40:01 +0200568 }
569
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200570 if (event->state != PERF_EVENT_STATE_ACTIVE)
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200571 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200572
573 event->state = PERF_EVENT_STATE_INACTIVE;
574 if (event->pending_disable) {
575 event->pending_disable = 0;
576 event->state = PERF_EVENT_STATE_OFF;
577 }
Stephane Eranian41587552011-01-03 18:20:01 +0200578 event->tstamp_stopped = tstamp;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200579 event->pmu->del(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200580 event->oncpu = -1;
581
582 if (!is_software_event(event))
583 cpuctx->active_oncpu--;
584 ctx->nr_active--;
585 if (event->attr.exclusive || !cpuctx->active_oncpu)
586 cpuctx->exclusive = 0;
587}
588
589static void
590group_sched_out(struct perf_event *group_event,
591 struct perf_cpu_context *cpuctx,
592 struct perf_event_context *ctx)
593{
594 struct perf_event *event;
Stephane Eranianfa66f072010-08-26 16:40:01 +0200595 int state = group_event->state;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200596
597 event_sched_out(group_event, cpuctx, ctx);
598
599 /*
600 * Schedule out siblings (if any):
601 */
602 list_for_each_entry(event, &group_event->sibling_list, group_entry)
603 event_sched_out(event, cpuctx, ctx);
604
Stephane Eranianfa66f072010-08-26 16:40:01 +0200605 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200606 cpuctx->exclusive = 0;
607}
608
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200609static inline struct perf_cpu_context *
610__get_cpu_context(struct perf_event_context *ctx)
611{
612 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
613}
614
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200615/*
616 * Cross CPU call to remove a performance event
617 *
618 * We disable the event on the hardware level first. After that we
619 * remove it from the context list.
620 */
621static void __perf_event_remove_from_context(void *info)
622{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200623 struct perf_event *event = info;
624 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200625 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200626
627 /*
628 * If this is a task context, we need to check whether it is
629 * the current task context of this cpu. If not it has been
630 * scheduled out before the smp call arrived.
631 */
632 if (ctx->task && cpuctx->task_ctx != ctx)
633 return;
634
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100635 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200636
637 event_sched_out(event, cpuctx, ctx);
638
639 list_del_event(event, ctx);
640
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100641 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200642}
643
644
645/*
646 * Remove the event from a task's (or a CPU's) list of events.
647 *
648 * Must be called with ctx->mutex held.
649 *
650 * CPU events are removed with a smp call. For task events we only
651 * call when the task is on a CPU.
652 *
653 * If event->ctx is a cloned context, callers must make sure that
654 * every task struct that event->ctx->task could possibly point to
655 * remains valid. This is OK when called from perf_release since
656 * that only calls us on the top-level context, which can't be a clone.
657 * When called from perf_event_exit_task, it's OK because the
658 * context has been detached from its task.
659 */
660static void perf_event_remove_from_context(struct perf_event *event)
661{
662 struct perf_event_context *ctx = event->ctx;
663 struct task_struct *task = ctx->task;
664
665 if (!task) {
666 /*
667 * Per cpu events are removed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200668 * the removal is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200669 */
670 smp_call_function_single(event->cpu,
671 __perf_event_remove_from_context,
672 event, 1);
673 return;
674 }
675
676retry:
677 task_oncpu_function_call(task, __perf_event_remove_from_context,
678 event);
679
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100680 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200681 /*
682 * If the context is active we need to retry the smp call.
683 */
684 if (ctx->nr_active && !list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100685 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200686 goto retry;
687 }
688
689 /*
690 * The lock prevents that this context is scheduled in so we
691 * can remove the event safely, if the call above did not
692 * succeed.
693 */
Peter Zijlstra6c2bfcb2009-11-23 11:37:24 +0100694 if (!list_empty(&event->group_entry))
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200695 list_del_event(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100696 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200697}
698
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200699/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200700 * Cross CPU call to disable a performance event
701 */
702static void __perf_event_disable(void *info)
703{
704 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200705 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200706 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200707
708 /*
709 * If this is a per-task event, need to check whether this
710 * event's task is the current task on this cpu.
711 */
712 if (ctx->task && cpuctx->task_ctx != ctx)
713 return;
714
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100715 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200716
717 /*
718 * If the event is on, turn it off.
719 * If it is in error state, leave it in error state.
720 */
721 if (event->state >= PERF_EVENT_STATE_INACTIVE) {
722 update_context_time(ctx);
723 update_group_times(event);
724 if (event == event->group_leader)
725 group_sched_out(event, cpuctx, ctx);
726 else
727 event_sched_out(event, cpuctx, ctx);
728 event->state = PERF_EVENT_STATE_OFF;
729 }
730
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100731 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200732}
733
734/*
735 * Disable a event.
736 *
737 * If event->ctx is a cloned context, callers must make sure that
738 * every task struct that event->ctx->task could possibly point to
739 * remains valid. This condition is satisifed when called through
740 * perf_event_for_each_child or perf_event_for_each because they
741 * hold the top-level event's child_mutex, so any descendant that
742 * goes to exit will block in sync_child_event.
743 * When called from perf_pending_event it's OK because event->ctx
744 * is the current context on this CPU and preemption is disabled,
745 * hence we can't get into perf_event_task_sched_out for this context.
746 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +0100747void perf_event_disable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200748{
749 struct perf_event_context *ctx = event->ctx;
750 struct task_struct *task = ctx->task;
751
752 if (!task) {
753 /*
754 * Disable the event on the cpu that it's on
755 */
756 smp_call_function_single(event->cpu, __perf_event_disable,
757 event, 1);
758 return;
759 }
760
Peter Zijlstra9ed60602010-06-11 17:36:35 +0200761retry:
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200762 task_oncpu_function_call(task, __perf_event_disable, event);
763
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100764 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200765 /*
766 * If the event is still active, we need to retry the cross-call.
767 */
768 if (event->state == PERF_EVENT_STATE_ACTIVE) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100769 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200770 goto retry;
771 }
772
773 /*
774 * Since we have the lock this context can't be scheduled
775 * in, so we can change the state safely.
776 */
777 if (event->state == PERF_EVENT_STATE_INACTIVE) {
778 update_group_times(event);
779 event->state = PERF_EVENT_STATE_OFF;
780 }
781
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100782 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200783}
784
Peter Zijlstra4fe757d2011-02-15 22:26:07 +0100785#define MAX_INTERRUPTS (~0ULL)
786
787static void perf_log_throttle(struct perf_event *event, int enable);
788
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200789static int
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200790event_sched_in(struct perf_event *event,
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200791 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100792 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200793{
Stephane Eranian41587552011-01-03 18:20:01 +0200794 u64 tstamp = perf_event_time(event);
795
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200796 if (event->state <= PERF_EVENT_STATE_OFF)
797 return 0;
798
799 event->state = PERF_EVENT_STATE_ACTIVE;
Peter Zijlstra6e377382010-02-11 13:21:58 +0100800 event->oncpu = smp_processor_id();
Peter Zijlstra4fe757d2011-02-15 22:26:07 +0100801
802 /*
803 * Unthrottle events, since we scheduled we might have missed several
804 * ticks already, also for a heavily scheduling task there is little
805 * guarantee it'll get a tick in a timely manner.
806 */
807 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
808 perf_log_throttle(event, 1);
809 event->hw.interrupts = 0;
810 }
811
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200812 /*
813 * The new state must be visible before we turn it on in the hardware:
814 */
815 smp_wmb();
816
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200817 if (event->pmu->add(event, PERF_EF_START)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200818 event->state = PERF_EVENT_STATE_INACTIVE;
819 event->oncpu = -1;
820 return -EAGAIN;
821 }
822
Stephane Eranian41587552011-01-03 18:20:01 +0200823 event->tstamp_running += tstamp - event->tstamp_stopped;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200824
Stephane Eranian41587552011-01-03 18:20:01 +0200825 event->shadow_ctx_time = tstamp - ctx->timestamp;
Stephane Eranianeed01522010-10-26 16:08:01 +0200826
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200827 if (!is_software_event(event))
828 cpuctx->active_oncpu++;
829 ctx->nr_active++;
830
831 if (event->attr.exclusive)
832 cpuctx->exclusive = 1;
833
834 return 0;
835}
836
837static int
838group_sched_in(struct perf_event *group_event,
839 struct perf_cpu_context *cpuctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +0100840 struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200841{
Lin Ming6bde9b62010-04-23 13:56:00 +0800842 struct perf_event *event, *partial_group = NULL;
Peter Zijlstra51b0fe32010-06-11 13:35:57 +0200843 struct pmu *pmu = group_event->pmu;
Stephane Eraniand7842da2010-10-20 15:25:01 +0200844 u64 now = ctx->time;
845 bool simulate = false;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200846
847 if (group_event->state == PERF_EVENT_STATE_OFF)
848 return 0;
849
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200850 pmu->start_txn(pmu);
Lin Ming6bde9b62010-04-23 13:56:00 +0800851
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200852 if (event_sched_in(group_event, cpuctx, ctx)) {
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200853 pmu->cancel_txn(pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200854 return -EAGAIN;
Stephane Eranian90151c32010-05-25 16:23:10 +0200855 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200856
857 /*
858 * Schedule in siblings as one group (if any):
859 */
860 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200861 if (event_sched_in(event, cpuctx, ctx)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200862 partial_group = event;
863 goto group_error;
864 }
865 }
866
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200867 if (!pmu->commit_txn(pmu))
Paul Mackerras6e851582010-05-08 20:58:00 +1000868 return 0;
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200869
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200870group_error:
871 /*
872 * Groups can be scheduled in as one unit only, so undo any
873 * partial group before returning:
Stephane Eraniand7842da2010-10-20 15:25:01 +0200874 * The events up to the failed event are scheduled out normally,
875 * tstamp_stopped will be updated.
876 *
877 * The failed events and the remaining siblings need to have
878 * their timings updated as if they had gone thru event_sched_in()
879 * and event_sched_out(). This is required to get consistent timings
880 * across the group. This also takes care of the case where the group
881 * could never be scheduled by ensuring tstamp_stopped is set to mark
882 * the time the event was actually stopped, such that time delta
883 * calculation in update_event_times() is correct.
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200884 */
885 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
886 if (event == partial_group)
Stephane Eraniand7842da2010-10-20 15:25:01 +0200887 simulate = true;
888
889 if (simulate) {
890 event->tstamp_running += now - event->tstamp_stopped;
891 event->tstamp_stopped = now;
892 } else {
893 event_sched_out(event, cpuctx, ctx);
894 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200895 }
Stephane Eranian9ffcfa62010-10-20 15:25:01 +0200896 event_sched_out(group_event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200897
Peter Zijlstraad5133b2010-06-15 12:22:39 +0200898 pmu->cancel_txn(pmu);
Stephane Eranian90151c32010-05-25 16:23:10 +0200899
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200900 return -EAGAIN;
901}
902
903/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200904 * Work out whether we can put this event group on the CPU now.
905 */
906static int group_can_go_on(struct perf_event *event,
907 struct perf_cpu_context *cpuctx,
908 int can_add_hw)
909{
910 /*
911 * Groups consisting entirely of software events can always go on.
912 */
Frederic Weisbeckerd6f962b2010-01-10 01:25:51 +0100913 if (event->group_flags & PERF_GROUP_SOFTWARE)
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200914 return 1;
915 /*
916 * If an exclusive group is already on, no other hardware
917 * events can go on.
918 */
919 if (cpuctx->exclusive)
920 return 0;
921 /*
922 * If this group is exclusive and there are already
923 * events on the CPU, it can't go on.
924 */
925 if (event->attr.exclusive && cpuctx->active_oncpu)
926 return 0;
927 /*
928 * Otherwise, try to add it if all previous groups were able
929 * to go on.
930 */
931 return can_add_hw;
932}
933
934static void add_event_to_ctx(struct perf_event *event,
935 struct perf_event_context *ctx)
936{
Stephane Eranian41587552011-01-03 18:20:01 +0200937 u64 tstamp = perf_event_time(event);
938
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200939 list_add_event(event, ctx);
Peter Zijlstra8a495422010-05-27 15:47:49 +0200940 perf_group_attach(event);
Stephane Eranian41587552011-01-03 18:20:01 +0200941 event->tstamp_enabled = tstamp;
942 event->tstamp_running = tstamp;
943 event->tstamp_stopped = tstamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200944}
945
946/*
947 * Cross CPU call to install and enable a performance event
948 *
949 * Must be called with ctx->mutex held
950 */
951static void __perf_install_in_context(void *info)
952{
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200953 struct perf_event *event = info;
954 struct perf_event_context *ctx = event->ctx;
955 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +0200956 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200957 int err;
958
959 /*
960 * If this is a task context, we need to check whether it is
961 * the current task context of this cpu. If not it has been
962 * scheduled out before the smp call arrived.
963 * Or possibly this is the right context but it isn't
964 * on this cpu because it had no events.
965 */
966 if (ctx->task && cpuctx->task_ctx != ctx) {
967 if (cpuctx->task_ctx || ctx->task != current)
968 return;
969 cpuctx->task_ctx = ctx;
970 }
971
Thomas Gleixnere625cce2009-11-17 18:02:06 +0100972 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200973 ctx->is_active = 1;
974 update_context_time(ctx);
975
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200976 add_event_to_ctx(event, ctx);
977
Stephane Eranian5632ab12011-01-03 18:20:01 +0200978 if (!event_filter_match(event))
Peter Zijlstraf4c41762009-12-16 17:55:54 +0100979 goto unlock;
980
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200981 /*
982 * Don't put the event on if it is disabled or if
983 * it is in a group and the group isn't on.
984 */
985 if (event->state != PERF_EVENT_STATE_INACTIVE ||
986 (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
987 goto unlock;
988
989 /*
990 * An exclusive event can't go on if there are already active
991 * hardware events, and no hardware event can go on if there
992 * is already an exclusive event on.
993 */
994 if (!group_can_go_on(event, cpuctx, 1))
995 err = -EEXIST;
996 else
Peter Zijlstra6e377382010-02-11 13:21:58 +0100997 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200998
999 if (err) {
1000 /*
1001 * This event couldn't go on. If it is in a group
1002 * then we have to pull the whole group off.
1003 * If the event group is pinned then put it in error state.
1004 */
1005 if (leader != event)
1006 group_sched_out(leader, cpuctx, ctx);
1007 if (leader->attr.pinned) {
1008 update_group_times(leader);
1009 leader->state = PERF_EVENT_STATE_ERROR;
1010 }
1011 }
1012
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001013unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001014 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001015}
1016
1017/*
1018 * Attach a performance event to a context
1019 *
1020 * First we add the event to the list with the hardware enable bit
1021 * in event->hw_config cleared.
1022 *
1023 * If the event is attached to a task which is on a CPU we use a smp
1024 * call to enable it in the task context. The task might have been
1025 * scheduled away, but we check this in the smp call again.
1026 *
1027 * Must be called with ctx->mutex held.
1028 */
1029static void
1030perf_install_in_context(struct perf_event_context *ctx,
1031 struct perf_event *event,
1032 int cpu)
1033{
1034 struct task_struct *task = ctx->task;
1035
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02001036 event->ctx = ctx;
1037
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001038 if (!task) {
1039 /*
1040 * Per cpu events are installed via an smp call and
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001041 * the install is always successful.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001042 */
1043 smp_call_function_single(cpu, __perf_install_in_context,
1044 event, 1);
1045 return;
1046 }
1047
1048retry:
1049 task_oncpu_function_call(task, __perf_install_in_context,
1050 event);
1051
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001052 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001053 /*
1054 * we need to retry the smp call.
1055 */
1056 if (ctx->is_active && list_empty(&event->group_entry)) {
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001057 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001058 goto retry;
1059 }
1060
1061 /*
1062 * The lock prevents that this context is scheduled in so we
1063 * can add the event safely, if it the call above did not
1064 * succeed.
1065 */
1066 if (list_empty(&event->group_entry))
1067 add_event_to_ctx(event, ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001068 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001069}
1070
1071/*
1072 * Put a event into inactive state and update time fields.
1073 * Enabling the leader of a group effectively enables all
1074 * the group members that aren't explicitly disabled, so we
1075 * have to update their ->tstamp_enabled also.
1076 * Note: this works for group members as well as group leaders
1077 * since the non-leader members' sibling_lists will be empty.
1078 */
1079static void __perf_event_mark_enabled(struct perf_event *event,
1080 struct perf_event_context *ctx)
1081{
1082 struct perf_event *sub;
Stephane Eranian41587552011-01-03 18:20:01 +02001083 u64 tstamp = perf_event_time(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001084
1085 event->state = PERF_EVENT_STATE_INACTIVE;
Stephane Eranian41587552011-01-03 18:20:01 +02001086 event->tstamp_enabled = tstamp - event->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001087 list_for_each_entry(sub, &event->sibling_list, group_entry) {
Stephane Eranian41587552011-01-03 18:20:01 +02001088 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
1089 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001090 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001091}
1092
1093/*
1094 * Cross CPU call to enable a performance event
1095 */
1096static void __perf_event_enable(void *info)
1097{
1098 struct perf_event *event = info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001099 struct perf_event_context *ctx = event->ctx;
1100 struct perf_event *leader = event->group_leader;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001101 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001102 int err;
1103
1104 /*
1105 * If this is a per-task event, need to check whether this
1106 * event's task is the current task on this cpu.
1107 */
1108 if (ctx->task && cpuctx->task_ctx != ctx) {
1109 if (cpuctx->task_ctx || ctx->task != current)
1110 return;
1111 cpuctx->task_ctx = ctx;
1112 }
1113
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001114 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001115 ctx->is_active = 1;
1116 update_context_time(ctx);
1117
1118 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1119 goto unlock;
1120 __perf_event_mark_enabled(event, ctx);
1121
Stephane Eranian5632ab12011-01-03 18:20:01 +02001122 if (!event_filter_match(event))
Peter Zijlstraf4c41762009-12-16 17:55:54 +01001123 goto unlock;
1124
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001125 /*
1126 * If the event is in a group and isn't the group leader,
1127 * then don't put it on unless the group is on.
1128 */
1129 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
1130 goto unlock;
1131
1132 if (!group_can_go_on(event, cpuctx, 1)) {
1133 err = -EEXIST;
1134 } else {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001135 if (event == leader)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001136 err = group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001137 else
Peter Zijlstra6e377382010-02-11 13:21:58 +01001138 err = event_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001139 }
1140
1141 if (err) {
1142 /*
1143 * If this event can't go on and it's part of a
1144 * group, then the whole group has to come off.
1145 */
1146 if (leader != event)
1147 group_sched_out(leader, cpuctx, ctx);
1148 if (leader->attr.pinned) {
1149 update_group_times(leader);
1150 leader->state = PERF_EVENT_STATE_ERROR;
1151 }
1152 }
1153
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001154unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001155 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001156}
1157
1158/*
1159 * Enable a event.
1160 *
1161 * If event->ctx is a cloned context, callers must make sure that
1162 * every task struct that event->ctx->task could possibly point to
1163 * remains valid. This condition is satisfied when called through
1164 * perf_event_for_each_child or perf_event_for_each as described
1165 * for perf_event_disable.
1166 */
Frederic Weisbecker44234ad2009-12-09 09:25:48 +01001167void perf_event_enable(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001168{
1169 struct perf_event_context *ctx = event->ctx;
1170 struct task_struct *task = ctx->task;
1171
1172 if (!task) {
1173 /*
1174 * Enable the event on the cpu that it's on
1175 */
1176 smp_call_function_single(event->cpu, __perf_event_enable,
1177 event, 1);
1178 return;
1179 }
1180
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001181 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001182 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1183 goto out;
1184
1185 /*
1186 * If the event is in error state, clear that first.
1187 * That way, if we see the event in error state below, we
1188 * know that it has gone back into error state, as distinct
1189 * from the task having been scheduled away before the
1190 * cross-call arrived.
1191 */
1192 if (event->state == PERF_EVENT_STATE_ERROR)
1193 event->state = PERF_EVENT_STATE_OFF;
1194
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001195retry:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001196 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001197 task_oncpu_function_call(task, __perf_event_enable, event);
1198
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001199 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001200
1201 /*
1202 * If the context is active and the event is still off,
1203 * we need to retry the cross-call.
1204 */
1205 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
1206 goto retry;
1207
1208 /*
1209 * Since we have the lock this context can't be scheduled
1210 * in, so we can change the state safely.
1211 */
1212 if (event->state == PERF_EVENT_STATE_OFF)
1213 __perf_event_mark_enabled(event, ctx);
1214
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001215out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001216 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001217}
1218
1219static int perf_event_refresh(struct perf_event *event, int refresh)
1220{
1221 /*
1222 * not supported on inherited events
1223 */
Franck Bui-Huu2e939d12010-11-23 16:21:44 +01001224 if (event->attr.inherit || !is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001225 return -EINVAL;
1226
1227 atomic_add(refresh, &event->event_limit);
1228 perf_event_enable(event);
1229
1230 return 0;
1231}
1232
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001233static void ctx_sched_out(struct perf_event_context *ctx,
1234 struct perf_cpu_context *cpuctx,
1235 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001236{
1237 struct perf_event *event;
1238
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001239 raw_spin_lock(&ctx->lock);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001240 perf_pmu_disable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001241 ctx->is_active = 0;
1242 if (likely(!ctx->nr_events))
1243 goto out;
1244 update_context_time(ctx);
1245
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001246 if (!ctx->nr_active)
Peter Zijlstra24cd7f52010-06-11 17:32:03 +02001247 goto out;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001248
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001249 if (event_type & EVENT_PINNED) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001250 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
1251 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001252 }
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001253
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001254 if (event_type & EVENT_FLEXIBLE) {
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001255 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001256 group_sched_out(event, cpuctx, ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001257 }
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001258out:
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001259 perf_pmu_enable(ctx->pmu);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001260 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001261}
1262
1263/*
1264 * Test whether two contexts are equivalent, i.e. whether they
1265 * have both been cloned from the same version of the same context
1266 * and they both have the same number of enabled events.
1267 * If the number of enabled events is the same, then the set
1268 * of enabled events should be the same, because these are both
1269 * inherited contexts, therefore we can't access individual events
1270 * in them directly with an fd; we can only enable/disable all
1271 * events via prctl, or enable/disable all events in a family
1272 * via ioctl, which will have the same effect on both contexts.
1273 */
1274static int context_equiv(struct perf_event_context *ctx1,
1275 struct perf_event_context *ctx2)
1276{
1277 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
1278 && ctx1->parent_gen == ctx2->parent_gen
1279 && !ctx1->pin_count && !ctx2->pin_count;
1280}
1281
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001282static void __perf_event_sync_stat(struct perf_event *event,
1283 struct perf_event *next_event)
1284{
1285 u64 value;
1286
1287 if (!event->attr.inherit_stat)
1288 return;
1289
1290 /*
1291 * Update the event value, we cannot use perf_event_read()
1292 * because we're in the middle of a context switch and have IRQs
1293 * disabled, which upsets smp_call_function_single(), however
1294 * we know the event must be on the current CPU, therefore we
1295 * don't need to use it.
1296 */
1297 switch (event->state) {
1298 case PERF_EVENT_STATE_ACTIVE:
Peter Zijlstra3dbebf12009-11-20 22:19:52 +01001299 event->pmu->read(event);
1300 /* fall-through */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001301
1302 case PERF_EVENT_STATE_INACTIVE:
1303 update_event_times(event);
1304 break;
1305
1306 default:
1307 break;
1308 }
1309
1310 /*
1311 * In order to keep per-task stats reliable we need to flip the event
1312 * values when we flip the contexts.
1313 */
Peter Zijlstrae7850592010-05-21 14:43:08 +02001314 value = local64_read(&next_event->count);
1315 value = local64_xchg(&event->count, value);
1316 local64_set(&next_event->count, value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001317
1318 swap(event->total_time_enabled, next_event->total_time_enabled);
1319 swap(event->total_time_running, next_event->total_time_running);
1320
1321 /*
1322 * Since we swizzled the values, update the user visible data too.
1323 */
1324 perf_event_update_userpage(event);
1325 perf_event_update_userpage(next_event);
1326}
1327
1328#define list_next_entry(pos, member) \
1329 list_entry(pos->member.next, typeof(*pos), member)
1330
1331static void perf_event_sync_stat(struct perf_event_context *ctx,
1332 struct perf_event_context *next_ctx)
1333{
1334 struct perf_event *event, *next_event;
1335
1336 if (!ctx->nr_stat)
1337 return;
1338
Peter Zijlstra02ffdbc2009-11-20 22:19:50 +01001339 update_context_time(ctx);
1340
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001341 event = list_first_entry(&ctx->event_list,
1342 struct perf_event, event_entry);
1343
1344 next_event = list_first_entry(&next_ctx->event_list,
1345 struct perf_event, event_entry);
1346
1347 while (&event->event_entry != &ctx->event_list &&
1348 &next_event->event_entry != &next_ctx->event_list) {
1349
1350 __perf_event_sync_stat(event, next_event);
1351
1352 event = list_next_entry(event, event_entry);
1353 next_event = list_next_entry(next_event, event_entry);
1354 }
1355}
1356
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001357void perf_event_context_sched_out(struct task_struct *task, int ctxn,
1358 struct task_struct *next)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001359{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001360 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001361 struct perf_event_context *next_ctx;
1362 struct perf_event_context *parent;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001363 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001364 int do_switch = 1;
1365
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001366 if (likely(!ctx))
1367 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001368
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001369 cpuctx = __get_cpu_context(ctx);
1370 if (!cpuctx->task_ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001371 return;
1372
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001373 rcu_read_lock();
1374 parent = rcu_dereference(ctx->parent_ctx);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001375 next_ctx = next->perf_event_ctxp[ctxn];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001376 if (parent && next_ctx &&
1377 rcu_dereference(next_ctx->parent_ctx) == parent) {
1378 /*
1379 * Looks like the two contexts are clones, so we might be
1380 * able to optimize the context switch. We lock both
1381 * contexts and check that they are clones under the
1382 * lock (including re-checking that neither has been
1383 * uncloned in the meantime). It doesn't matter which
1384 * order we take the locks because no other cpu could
1385 * be trying to lock both of these tasks.
1386 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001387 raw_spin_lock(&ctx->lock);
1388 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001389 if (context_equiv(ctx, next_ctx)) {
1390 /*
1391 * XXX do we need a memory barrier of sorts
1392 * wrt to rcu_dereference() of perf_event_ctxp
1393 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001394 task->perf_event_ctxp[ctxn] = next_ctx;
1395 next->perf_event_ctxp[ctxn] = ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001396 ctx->task = next;
1397 next_ctx->task = task;
1398 do_switch = 0;
1399
1400 perf_event_sync_stat(ctx, next_ctx);
1401 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001402 raw_spin_unlock(&next_ctx->lock);
1403 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001404 }
1405 rcu_read_unlock();
1406
1407 if (do_switch) {
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001408 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001409 cpuctx->task_ctx = NULL;
1410 }
1411}
1412
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001413#define for_each_task_context_nr(ctxn) \
1414 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
1415
1416/*
1417 * Called from scheduler to remove the events of the current task,
1418 * with interrupts disabled.
1419 *
1420 * We stop each event and update the event value in event->count.
1421 *
1422 * This does not protect us against NMI, but disable()
1423 * sets the disabled bit in the control field of event _before_
1424 * accessing the event control register. If a NMI hits, then it will
1425 * not restart the event.
1426 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001427void __perf_event_task_sched_out(struct task_struct *task,
1428 struct task_struct *next)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001429{
1430 int ctxn;
1431
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001432 for_each_task_context_nr(ctxn)
1433 perf_event_context_sched_out(task, ctxn, next);
1434}
1435
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001436static void task_ctx_sched_out(struct perf_event_context *ctx,
1437 enum event_type_t event_type)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001438{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001439 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001440
1441 if (!cpuctx->task_ctx)
1442 return;
1443
1444 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
1445 return;
1446
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001447 ctx_sched_out(ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001448 cpuctx->task_ctx = NULL;
1449}
1450
1451/*
1452 * Called with IRQs disabled
1453 */
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001454static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
1455 enum event_type_t event_type)
1456{
1457 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001458}
1459
1460static void
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001461ctx_pinned_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001462 struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001463{
1464 struct perf_event *event;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001465
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001466 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1467 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001468 continue;
Stephane Eranian5632ab12011-01-03 18:20:01 +02001469 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001470 continue;
1471
Xiao Guangrong8c9ed8e2009-09-25 13:51:17 +08001472 if (group_can_go_on(event, cpuctx, 1))
Peter Zijlstra6e377382010-02-11 13:21:58 +01001473 group_sched_in(event, cpuctx, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001474
1475 /*
1476 * If this pinned group hasn't been scheduled,
1477 * put it in error state.
1478 */
1479 if (event->state == PERF_EVENT_STATE_INACTIVE) {
1480 update_group_times(event);
1481 event->state = PERF_EVENT_STATE_ERROR;
1482 }
1483 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001484}
1485
1486static void
1487ctx_flexible_sched_in(struct perf_event_context *ctx,
Peter Zijlstra6e377382010-02-11 13:21:58 +01001488 struct perf_cpu_context *cpuctx)
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001489{
1490 struct perf_event *event;
1491 int can_add_hw = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001492
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001493 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1494 /* Ignore events in OFF or ERROR state */
1495 if (event->state <= PERF_EVENT_STATE_OFF)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001496 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001497 /*
1498 * Listen to the 'cpu' scheduling filter constraint
1499 * of events:
1500 */
Stephane Eranian5632ab12011-01-03 18:20:01 +02001501 if (!event_filter_match(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001502 continue;
1503
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001504 if (group_can_go_on(event, cpuctx, can_add_hw)) {
Peter Zijlstra6e377382010-02-11 13:21:58 +01001505 if (group_sched_in(event, cpuctx, ctx))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001506 can_add_hw = 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001507 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001508 }
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001509}
1510
1511static void
1512ctx_sched_in(struct perf_event_context *ctx,
1513 struct perf_cpu_context *cpuctx,
1514 enum event_type_t event_type)
1515{
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001516 raw_spin_lock(&ctx->lock);
1517 ctx->is_active = 1;
1518 if (likely(!ctx->nr_events))
1519 goto out;
1520
1521 ctx->timestamp = perf_clock();
1522
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001523 /*
1524 * First go through the list and put on any pinned groups
1525 * in order to give them the best chance of going on.
1526 */
1527 if (event_type & EVENT_PINNED)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001528 ctx_pinned_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001529
1530 /* Then walk through the lower prio flexible groups */
1531 if (event_type & EVENT_FLEXIBLE)
Peter Zijlstra6e377382010-02-11 13:21:58 +01001532 ctx_flexible_sched_in(ctx, cpuctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001533
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001534out:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001535 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001536}
1537
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001538static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
1539 enum event_type_t event_type)
1540{
1541 struct perf_event_context *ctx = &cpuctx->ctx;
1542
1543 ctx_sched_in(ctx, cpuctx, event_type);
1544}
1545
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001546static void task_ctx_sched_in(struct perf_event_context *ctx,
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001547 enum event_type_t event_type)
1548{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001549 struct perf_cpu_context *cpuctx;
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001550
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001551 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001552 if (cpuctx->task_ctx == ctx)
1553 return;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001554
Frederic Weisbecker5b0311e2010-01-17 11:59:13 +01001555 ctx_sched_in(ctx, cpuctx, event_type);
1556 cpuctx->task_ctx = ctx;
1557}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001558
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001559void perf_event_context_sched_in(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001560{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001561 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001562
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001563 cpuctx = __get_cpu_context(ctx);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001564 if (cpuctx->task_ctx == ctx)
1565 return;
1566
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001567 perf_pmu_disable(ctx->pmu);
Frederic Weisbecker329c0e02010-01-17 12:56:05 +01001568 /*
1569 * We want to keep the following priority order:
1570 * cpu pinned (that don't need to move), task pinned,
1571 * cpu flexible, task flexible.
1572 */
1573 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
1574
1575 ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
1576 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
1577 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
1578
1579 cpuctx->task_ctx = ctx;
eranian@google.com9b33fa62010-03-10 22:26:05 -08001580
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001581 /*
1582 * Since these rotations are per-cpu, we need to ensure the
1583 * cpu-context we got scheduled on is actually rotating.
1584 */
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001585 perf_pmu_rotate_start(ctx->pmu);
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001586 perf_pmu_enable(ctx->pmu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001587}
1588
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001589/*
1590 * Called from scheduler to add the events of the current task
1591 * with interrupts disabled.
1592 *
1593 * We restore the event value and then enable it.
1594 *
1595 * This does not protect us against NMI, but enable()
1596 * sets the enabled bit in the control field of event _before_
1597 * accessing the event control register. If a NMI hits, then it will
1598 * keep the event running.
1599 */
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02001600void __perf_event_task_sched_in(struct task_struct *task)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001601{
1602 struct perf_event_context *ctx;
1603 int ctxn;
1604
1605 for_each_task_context_nr(ctxn) {
1606 ctx = task->perf_event_ctxp[ctxn];
1607 if (likely(!ctx))
1608 continue;
1609
1610 perf_event_context_sched_in(ctx);
1611 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001612}
1613
Peter Zijlstraabd50712010-01-26 18:50:16 +01001614static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
1615{
1616 u64 frequency = event->attr.sample_freq;
1617 u64 sec = NSEC_PER_SEC;
1618 u64 divisor, dividend;
1619
1620 int count_fls, nsec_fls, frequency_fls, sec_fls;
1621
1622 count_fls = fls64(count);
1623 nsec_fls = fls64(nsec);
1624 frequency_fls = fls64(frequency);
1625 sec_fls = 30;
1626
1627 /*
1628 * We got @count in @nsec, with a target of sample_freq HZ
1629 * the target period becomes:
1630 *
1631 * @count * 10^9
1632 * period = -------------------
1633 * @nsec * sample_freq
1634 *
1635 */
1636
1637 /*
1638 * Reduce accuracy by one bit such that @a and @b converge
1639 * to a similar magnitude.
1640 */
1641#define REDUCE_FLS(a, b) \
1642do { \
1643 if (a##_fls > b##_fls) { \
1644 a >>= 1; \
1645 a##_fls--; \
1646 } else { \
1647 b >>= 1; \
1648 b##_fls--; \
1649 } \
1650} while (0)
1651
1652 /*
1653 * Reduce accuracy until either term fits in a u64, then proceed with
1654 * the other, so that finally we can do a u64/u64 division.
1655 */
1656 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
1657 REDUCE_FLS(nsec, frequency);
1658 REDUCE_FLS(sec, count);
1659 }
1660
1661 if (count_fls + sec_fls > 64) {
1662 divisor = nsec * frequency;
1663
1664 while (count_fls + sec_fls > 64) {
1665 REDUCE_FLS(count, sec);
1666 divisor >>= 1;
1667 }
1668
1669 dividend = count * sec;
1670 } else {
1671 dividend = count * sec;
1672
1673 while (nsec_fls + frequency_fls > 64) {
1674 REDUCE_FLS(nsec, frequency);
1675 dividend >>= 1;
1676 }
1677
1678 divisor = nsec * frequency;
1679 }
1680
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001681 if (!divisor)
1682 return dividend;
1683
Peter Zijlstraabd50712010-01-26 18:50:16 +01001684 return div64_u64(dividend, divisor);
1685}
1686
1687static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001688{
1689 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstraf6ab91a2010-06-04 15:18:01 +02001690 s64 period, sample_period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001691 s64 delta;
1692
Peter Zijlstraabd50712010-01-26 18:50:16 +01001693 period = perf_calculate_period(event, nsec, count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001694
1695 delta = (s64)(period - hwc->sample_period);
1696 delta = (delta + 7) / 8; /* low pass filter */
1697
1698 sample_period = hwc->sample_period + delta;
1699
1700 if (!sample_period)
1701 sample_period = 1;
1702
1703 hwc->sample_period = sample_period;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001704
Peter Zijlstrae7850592010-05-21 14:43:08 +02001705 if (local64_read(&hwc->period_left) > 8*sample_period) {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001706 event->pmu->stop(event, PERF_EF_UPDATE);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001707 local64_set(&hwc->period_left, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001708 event->pmu->start(event, PERF_EF_RELOAD);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001709 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001710}
1711
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001712static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001713{
1714 struct perf_event *event;
1715 struct hw_perf_event *hwc;
Peter Zijlstraabd50712010-01-26 18:50:16 +01001716 u64 interrupts, now;
1717 s64 delta;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001718
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001719 raw_spin_lock(&ctx->lock);
Paul Mackerras03541f82009-10-14 16:58:03 +11001720 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001721 if (event->state != PERF_EVENT_STATE_ACTIVE)
1722 continue;
1723
Stephane Eranian5632ab12011-01-03 18:20:01 +02001724 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01001725 continue;
1726
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001727 hwc = &event->hw;
1728
1729 interrupts = hwc->interrupts;
1730 hwc->interrupts = 0;
1731
1732 /*
1733 * unthrottle events on the tick
1734 */
1735 if (interrupts == MAX_INTERRUPTS) {
1736 perf_log_throttle(event, 1);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02001737 event->pmu->start(event, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001738 }
1739
1740 if (!event->attr.freq || !event->attr.sample_freq)
1741 continue;
1742
Peter Zijlstraabd50712010-01-26 18:50:16 +01001743 event->pmu->read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02001744 now = local64_read(&event->count);
Peter Zijlstraabd50712010-01-26 18:50:16 +01001745 delta = now - hwc->freq_count_stamp;
1746 hwc->freq_count_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001747
Peter Zijlstraabd50712010-01-26 18:50:16 +01001748 if (delta > 0)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001749 perf_adjust_period(event, period, delta);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001750 }
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001751 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001752}
1753
1754/*
1755 * Round-robin a context's events:
1756 */
1757static void rotate_ctx(struct perf_event_context *ctx)
1758{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001759 raw_spin_lock(&ctx->lock);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001760
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01001761 /*
1762 * Rotate the first entry last of non-pinned groups. Rotation might be
1763 * disabled by the inheritance code.
1764 */
1765 if (!ctx->rotate_disable)
1766 list_rotate_left(&ctx->flexible_groups);
Frederic Weisbeckere2864172010-01-09 21:05:28 +01001767
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001768 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001769}
1770
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001771/*
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001772 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
1773 * because they're strictly cpu affine and rotate_start is called with IRQs
1774 * disabled, while rotate_context is called from IRQ context.
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001775 */
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001776static void perf_rotate_context(struct perf_cpu_context *cpuctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001777{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001778 u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001779 struct perf_event_context *ctx = NULL;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001780 int rotate = 0, remove = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001781
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001782 if (cpuctx->ctx.nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001783 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001784 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
1785 rotate = 1;
1786 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001787
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001788 ctx = cpuctx->task_ctx;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001789 if (ctx && ctx->nr_events) {
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001790 remove = 0;
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001791 if (ctx->nr_events != ctx->nr_active)
1792 rotate = 1;
1793 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001794
Peter Zijlstra1b9a6442010-09-07 18:32:22 +02001795 perf_pmu_disable(cpuctx->ctx.pmu);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001796 perf_ctx_adjust_freq(&cpuctx->ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001797 if (ctx)
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001798 perf_ctx_adjust_freq(ctx, interval);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001799
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001800 if (!rotate)
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001801 goto done;
Peter Zijlstrad4944a02010-03-08 13:51:20 +01001802
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001803 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001804 if (ctx)
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001805 task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001806
1807 rotate_ctx(&cpuctx->ctx);
1808 if (ctx)
1809 rotate_ctx(ctx);
1810
Frederic Weisbecker7defb0f2010-01-17 12:15:31 +01001811 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001812 if (ctx)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001813 task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001814
1815done:
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001816 if (remove)
1817 list_del_init(&cpuctx->rotation_list);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02001818
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02001819 perf_pmu_enable(cpuctx->ctx.pmu);
1820}
1821
1822void perf_event_task_tick(void)
1823{
1824 struct list_head *head = &__get_cpu_var(rotation_list);
1825 struct perf_cpu_context *cpuctx, *tmp;
1826
1827 WARN_ON(!irqs_disabled());
1828
1829 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
1830 if (cpuctx->jiffies_interval == 1 ||
1831 !(jiffies % cpuctx->jiffies_interval))
1832 perf_rotate_context(cpuctx);
1833 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001834}
1835
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001836static int event_enable_on_exec(struct perf_event *event,
1837 struct perf_event_context *ctx)
1838{
1839 if (!event->attr.enable_on_exec)
1840 return 0;
1841
1842 event->attr.enable_on_exec = 0;
1843 if (event->state >= PERF_EVENT_STATE_INACTIVE)
1844 return 0;
1845
1846 __perf_event_mark_enabled(event, ctx);
1847
1848 return 1;
1849}
1850
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001851/*
1852 * Enable all of a task's events that have been marked enable-on-exec.
1853 * This expects task == current.
1854 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001855static void perf_event_enable_on_exec(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001856{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001857 struct perf_event *event;
1858 unsigned long flags;
1859 int enabled = 0;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001860 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001861
1862 local_irq_save(flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001863 if (!ctx || !ctx->nr_events)
1864 goto out;
1865
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001866 task_ctx_sched_out(ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001867
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001868 raw_spin_lock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001869
Frederic Weisbecker889ff012010-01-09 20:04:47 +01001870 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
1871 ret = event_enable_on_exec(event, ctx);
1872 if (ret)
1873 enabled = 1;
1874 }
1875
1876 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
1877 ret = event_enable_on_exec(event, ctx);
1878 if (ret)
1879 enabled = 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001880 }
1881
1882 /*
1883 * Unclone this context if we enabled any event.
1884 */
1885 if (enabled)
1886 unclone_ctx(ctx);
1887
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001888 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001889
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02001890 perf_event_context_sched_in(ctx);
Peter Zijlstra9ed60602010-06-11 17:36:35 +02001891out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001892 local_irq_restore(flags);
1893}
1894
1895/*
1896 * Cross CPU call to read the hardware event
1897 */
1898static void __perf_event_read(void *info)
1899{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001900 struct perf_event *event = info;
1901 struct perf_event_context *ctx = event->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02001902 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001903
1904 /*
1905 * If this is a task context, we need to check whether it is
1906 * the current task context of this cpu. If not it has been
1907 * scheduled out before the smp call arrived. In that case
1908 * event->count would have been updated to a recent sample
1909 * when the event was scheduled out.
1910 */
1911 if (ctx->task && cpuctx->task_ctx != ctx)
1912 return;
1913
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001914 raw_spin_lock(&ctx->lock);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01001915 if (ctx->is_active)
1916 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001917 update_event_times(event);
Peter Zijlstra542e72f2011-01-26 15:38:35 +01001918 if (event->state == PERF_EVENT_STATE_ACTIVE)
1919 event->pmu->read(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001920 raw_spin_unlock(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001921}
1922
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001923static inline u64 perf_event_count(struct perf_event *event)
1924{
Peter Zijlstrae7850592010-05-21 14:43:08 +02001925 return local64_read(&event->count) + atomic64_read(&event->child_count);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001926}
1927
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001928static u64 perf_event_read(struct perf_event *event)
1929{
1930 /*
1931 * If event is enabled and currently active on a CPU, update the
1932 * value in the event structure:
1933 */
1934 if (event->state == PERF_EVENT_STATE_ACTIVE) {
1935 smp_call_function_single(event->oncpu,
1936 __perf_event_read, event, 1);
1937 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
Peter Zijlstra2b8988c2009-11-20 22:19:54 +01001938 struct perf_event_context *ctx = event->ctx;
1939 unsigned long flags;
1940
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001941 raw_spin_lock_irqsave(&ctx->lock, flags);
Stephane Eranianc530ccd2010-10-15 15:26:01 +02001942 /*
1943 * may read while context is not active
1944 * (e.g., thread is blocked), in that case
1945 * we cannot update context time
1946 */
1947 if (ctx->is_active)
1948 update_context_time(ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001949 update_event_times(event);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01001950 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001951 }
1952
Peter Zijlstrab5e58792010-05-21 14:43:12 +02001953 return perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001954}
1955
1956/*
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001957 * Callchain support
1958 */
1959
1960struct callchain_cpus_entries {
1961 struct rcu_head rcu_head;
1962 struct perf_callchain_entry *cpu_entries[0];
1963};
1964
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02001965static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02001966static atomic_t nr_callchain_events;
1967static DEFINE_MUTEX(callchain_mutex);
1968struct callchain_cpus_entries *callchain_cpus_entries;
1969
1970
1971__weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
1972 struct pt_regs *regs)
1973{
1974}
1975
1976__weak void perf_callchain_user(struct perf_callchain_entry *entry,
1977 struct pt_regs *regs)
1978{
1979}
1980
1981static void release_callchain_buffers_rcu(struct rcu_head *head)
1982{
1983 struct callchain_cpus_entries *entries;
1984 int cpu;
1985
1986 entries = container_of(head, struct callchain_cpus_entries, rcu_head);
1987
1988 for_each_possible_cpu(cpu)
1989 kfree(entries->cpu_entries[cpu]);
1990
1991 kfree(entries);
1992}
1993
1994static void release_callchain_buffers(void)
1995{
1996 struct callchain_cpus_entries *entries;
1997
1998 entries = callchain_cpus_entries;
1999 rcu_assign_pointer(callchain_cpus_entries, NULL);
2000 call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
2001}
2002
2003static int alloc_callchain_buffers(void)
2004{
2005 int cpu;
2006 int size;
2007 struct callchain_cpus_entries *entries;
2008
2009 /*
2010 * We can't use the percpu allocation API for data that can be
2011 * accessed from NMI. Use a temporary manual per cpu allocation
2012 * until that gets sorted out.
2013 */
Eric Dumazet88d4f0d2011-01-25 19:40:51 +01002014 size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002015
2016 entries = kzalloc(size, GFP_KERNEL);
2017 if (!entries)
2018 return -ENOMEM;
2019
Frederic Weisbecker7ae07ea2010-08-14 20:45:13 +02002020 size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002021
2022 for_each_possible_cpu(cpu) {
2023 entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
2024 cpu_to_node(cpu));
2025 if (!entries->cpu_entries[cpu])
2026 goto fail;
2027 }
2028
2029 rcu_assign_pointer(callchain_cpus_entries, entries);
2030
2031 return 0;
2032
2033fail:
2034 for_each_possible_cpu(cpu)
2035 kfree(entries->cpu_entries[cpu]);
2036 kfree(entries);
2037
2038 return -ENOMEM;
2039}
2040
2041static int get_callchain_buffers(void)
2042{
2043 int err = 0;
2044 int count;
2045
2046 mutex_lock(&callchain_mutex);
2047
2048 count = atomic_inc_return(&nr_callchain_events);
2049 if (WARN_ON_ONCE(count < 1)) {
2050 err = -EINVAL;
2051 goto exit;
2052 }
2053
2054 if (count > 1) {
2055 /* If the allocation failed, give up */
2056 if (!callchain_cpus_entries)
2057 err = -ENOMEM;
2058 goto exit;
2059 }
2060
2061 err = alloc_callchain_buffers();
2062 if (err)
2063 release_callchain_buffers();
2064exit:
2065 mutex_unlock(&callchain_mutex);
2066
2067 return err;
2068}
2069
2070static void put_callchain_buffers(void)
2071{
2072 if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
2073 release_callchain_buffers();
2074 mutex_unlock(&callchain_mutex);
2075 }
2076}
2077
2078static int get_recursion_context(int *recursion)
2079{
2080 int rctx;
2081
2082 if (in_nmi())
2083 rctx = 3;
2084 else if (in_irq())
2085 rctx = 2;
2086 else if (in_softirq())
2087 rctx = 1;
2088 else
2089 rctx = 0;
2090
2091 if (recursion[rctx])
2092 return -1;
2093
2094 recursion[rctx]++;
2095 barrier();
2096
2097 return rctx;
2098}
2099
2100static inline void put_recursion_context(int *recursion, int rctx)
2101{
2102 barrier();
2103 recursion[rctx]--;
2104}
2105
2106static struct perf_callchain_entry *get_callchain_entry(int *rctx)
2107{
2108 int cpu;
2109 struct callchain_cpus_entries *entries;
2110
2111 *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
2112 if (*rctx == -1)
2113 return NULL;
2114
2115 entries = rcu_dereference(callchain_cpus_entries);
2116 if (!entries)
2117 return NULL;
2118
2119 cpu = smp_processor_id();
2120
2121 return &entries->cpu_entries[cpu][*rctx];
2122}
2123
2124static void
2125put_callchain_entry(int rctx)
2126{
2127 put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
2128}
2129
2130static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
2131{
2132 int rctx;
2133 struct perf_callchain_entry *entry;
2134
2135
2136 entry = get_callchain_entry(&rctx);
2137 if (rctx == -1)
2138 return NULL;
2139
2140 if (!entry)
2141 goto exit_put;
2142
2143 entry->nr = 0;
2144
2145 if (!user_mode(regs)) {
2146 perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
2147 perf_callchain_kernel(entry, regs);
2148 if (current->mm)
2149 regs = task_pt_regs(current);
2150 else
2151 regs = NULL;
2152 }
2153
2154 if (regs) {
2155 perf_callchain_store(entry, PERF_CONTEXT_USER);
2156 perf_callchain_user(entry, regs);
2157 }
2158
2159exit_put:
2160 put_callchain_entry(rctx);
2161
2162 return entry;
2163}
2164
2165/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002166 * Initialize the perf_event context in a task_struct:
2167 */
Peter Zijlstraeb184472010-09-07 15:55:13 +02002168static void __perf_event_init_context(struct perf_event_context *ctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002169{
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002170 raw_spin_lock_init(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002171 mutex_init(&ctx->mutex);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01002172 INIT_LIST_HEAD(&ctx->pinned_groups);
2173 INIT_LIST_HEAD(&ctx->flexible_groups);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002174 INIT_LIST_HEAD(&ctx->event_list);
2175 atomic_set(&ctx->refcount, 1);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002176}
2177
Peter Zijlstraeb184472010-09-07 15:55:13 +02002178static struct perf_event_context *
2179alloc_perf_context(struct pmu *pmu, struct task_struct *task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002180{
2181 struct perf_event_context *ctx;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002182
2183 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
2184 if (!ctx)
2185 return NULL;
2186
2187 __perf_event_init_context(ctx);
2188 if (task) {
2189 ctx->task = task;
2190 get_task_struct(task);
2191 }
2192 ctx->pmu = pmu;
2193
2194 return ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002195}
2196
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002197static struct task_struct *
2198find_lively_task_by_vpid(pid_t vpid)
2199{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002200 struct task_struct *task;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002201 int err;
2202
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002203 rcu_read_lock();
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002204 if (!vpid)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002205 task = current;
2206 else
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002207 task = find_task_by_vpid(vpid);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002208 if (task)
2209 get_task_struct(task);
2210 rcu_read_unlock();
2211
2212 if (!task)
2213 return ERR_PTR(-ESRCH);
2214
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002215 /* Reuse ptrace permission checks for now. */
2216 err = -EACCES;
2217 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2218 goto errout;
2219
Matt Helsley2ebd4ff2010-09-13 13:01:19 -07002220 return task;
2221errout:
2222 put_task_struct(task);
2223 return ERR_PTR(err);
2224
2225}
2226
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002227static struct perf_event_context *
Matt Helsley38a81da2010-09-13 13:01:20 -07002228find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002229{
2230 struct perf_event_context *ctx;
2231 struct perf_cpu_context *cpuctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002232 unsigned long flags;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002233 int ctxn, err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002234
Oleg Nesterov22a4ec72011-01-18 17:10:08 +01002235 if (!task) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002236 /* Must be root to operate on a CPU event: */
2237 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
2238 return ERR_PTR(-EACCES);
2239
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002240 /*
2241 * We could be clever and allow to attach a event to an
2242 * offline CPU and activate it when the CPU comes up, but
2243 * that's for later.
2244 */
2245 if (!cpu_online(cpu))
2246 return ERR_PTR(-ENODEV);
2247
Peter Zijlstra108b02c2010-09-06 14:32:03 +02002248 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002249 ctx = &cpuctx->ctx;
2250 get_ctx(ctx);
2251
2252 return ctx;
2253 }
2254
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002255 err = -EINVAL;
2256 ctxn = pmu->task_ctx_nr;
2257 if (ctxn < 0)
2258 goto errout;
2259
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002260retry:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02002261 ctx = perf_lock_task_context(task, ctxn, &flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002262 if (ctx) {
2263 unclone_ctx(ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002264 raw_spin_unlock_irqrestore(&ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002265 }
2266
2267 if (!ctx) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002268 ctx = alloc_perf_context(pmu, task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002269 err = -ENOMEM;
2270 if (!ctx)
2271 goto errout;
Peter Zijlstraeb184472010-09-07 15:55:13 +02002272
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002273 get_ctx(ctx);
Peter Zijlstraeb184472010-09-07 15:55:13 +02002274
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002275 err = 0;
2276 mutex_lock(&task->perf_event_mutex);
2277 /*
2278 * If it has already passed perf_event_exit_task().
2279 * we must see PF_EXITING, it takes this mutex too.
2280 */
2281 if (task->flags & PF_EXITING)
2282 err = -ESRCH;
2283 else if (task->perf_event_ctxp[ctxn])
2284 err = -EAGAIN;
2285 else
2286 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
2287 mutex_unlock(&task->perf_event_mutex);
2288
2289 if (unlikely(err)) {
Peter Zijlstraeb184472010-09-07 15:55:13 +02002290 put_task_struct(task);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002291 kfree(ctx);
Oleg Nesterovdbe08d82011-01-19 19:22:07 +01002292
2293 if (err == -EAGAIN)
2294 goto retry;
2295 goto errout;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002296 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002297 }
2298
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002299 return ctx;
2300
Peter Zijlstra9ed60602010-06-11 17:36:35 +02002301errout:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002302 return ERR_PTR(err);
2303}
2304
Li Zefan6fb29152009-10-15 11:21:42 +08002305static void perf_event_free_filter(struct perf_event *event);
2306
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002307static void free_event_rcu(struct rcu_head *head)
2308{
2309 struct perf_event *event;
2310
2311 event = container_of(head, struct perf_event, rcu_head);
2312 if (event->ns)
2313 put_pid_ns(event->ns);
Li Zefan6fb29152009-10-15 11:21:42 +08002314 perf_event_free_filter(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002315 kfree(event);
2316}
2317
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002318static void perf_buffer_put(struct perf_buffer *buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002319
2320static void free_event(struct perf_event *event)
2321{
Peter Zijlstrae360adb2010-10-14 14:01:34 +08002322 irq_work_sync(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002323
2324 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02002325 if (event->attach_state & PERF_ATTACH_TASK)
2326 jump_label_dec(&perf_task_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01002327 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002328 atomic_dec(&nr_mmap_events);
2329 if (event->attr.comm)
2330 atomic_dec(&nr_comm_events);
2331 if (event->attr.task)
2332 atomic_dec(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02002333 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
2334 put_callchain_buffers();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002335 }
2336
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002337 if (event->buffer) {
2338 perf_buffer_put(event->buffer);
2339 event->buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002340 }
2341
2342 if (event->destroy)
2343 event->destroy(event);
2344
Peter Zijlstra0c67b402010-09-13 11:15:58 +02002345 if (event->ctx)
2346 put_ctx(event->ctx);
2347
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002348 call_rcu(&event->rcu_head, free_event_rcu);
2349}
2350
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002351int perf_event_release_kernel(struct perf_event *event)
2352{
2353 struct perf_event_context *ctx = event->ctx;
2354
Peter Zijlstra050735b2010-05-11 11:51:53 +02002355 /*
2356 * Remove from the PMU, can't get re-enabled since we got
2357 * here because the last ref went.
2358 */
2359 perf_event_disable(event);
2360
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002361 WARN_ON_ONCE(ctx->parent_ctx);
Peter Zijlstraa0507c82010-05-06 15:42:53 +02002362 /*
2363 * There are two ways this annotation is useful:
2364 *
2365 * 1) there is a lock recursion from perf_event_exit_task
2366 * see the comment there.
2367 *
2368 * 2) there is a lock-inversion with mmap_sem through
2369 * perf_event_read_group(), which takes faults while
2370 * holding ctx->mutex, however this is called after
2371 * the last filedesc died, so there is no possibility
2372 * to trigger the AB-BA case.
2373 */
2374 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002375 raw_spin_lock_irq(&ctx->lock);
Peter Zijlstra8a495422010-05-27 15:47:49 +02002376 perf_group_detach(event);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002377 list_del_event(event, ctx);
Peter Zijlstra050735b2010-05-11 11:51:53 +02002378 raw_spin_unlock_irq(&ctx->lock);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002379 mutex_unlock(&ctx->mutex);
2380
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002381 free_event(event);
2382
2383 return 0;
2384}
2385EXPORT_SYMBOL_GPL(perf_event_release_kernel);
2386
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002387/*
2388 * Called when the last reference to the file is gone.
2389 */
2390static int perf_release(struct inode *inode, struct file *file)
2391{
2392 struct perf_event *event = file->private_data;
Peter Zijlstra88821352010-11-09 19:01:43 +01002393 struct task_struct *owner;
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002394
2395 file->private_data = NULL;
2396
Peter Zijlstra88821352010-11-09 19:01:43 +01002397 rcu_read_lock();
2398 owner = ACCESS_ONCE(event->owner);
2399 /*
2400 * Matches the smp_wmb() in perf_event_exit_task(). If we observe
2401 * !owner it means the list deletion is complete and we can indeed
2402 * free this event, otherwise we need to serialize on
2403 * owner->perf_event_mutex.
2404 */
2405 smp_read_barrier_depends();
2406 if (owner) {
2407 /*
2408 * Since delayed_put_task_struct() also drops the last
2409 * task reference we can safely take a new reference
2410 * while holding the rcu_read_lock().
2411 */
2412 get_task_struct(owner);
2413 }
2414 rcu_read_unlock();
2415
2416 if (owner) {
2417 mutex_lock(&owner->perf_event_mutex);
2418 /*
2419 * We have to re-check the event->owner field, if it is cleared
2420 * we raced with perf_event_exit_task(), acquiring the mutex
2421 * ensured they're done, and we can proceed with freeing the
2422 * event.
2423 */
2424 if (event->owner)
2425 list_del_init(&event->owner_entry);
2426 mutex_unlock(&owner->perf_event_mutex);
2427 put_task_struct(owner);
2428 }
2429
Peter Zijlstraa66a3052009-11-23 11:37:23 +01002430 return perf_event_release_kernel(event);
2431}
2432
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002433u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002434{
2435 struct perf_event *child;
2436 u64 total = 0;
2437
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002438 *enabled = 0;
2439 *running = 0;
2440
Peter Zijlstra6f105812009-11-20 22:19:56 +01002441 mutex_lock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002442 total += perf_event_read(event);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002443 *enabled += event->total_time_enabled +
2444 atomic64_read(&event->child_total_time_enabled);
2445 *running += event->total_time_running +
2446 atomic64_read(&event->child_total_time_running);
2447
2448 list_for_each_entry(child, &event->child_list, child_list) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002449 total += perf_event_read(child);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002450 *enabled += child->total_time_enabled;
2451 *running += child->total_time_running;
2452 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002453 mutex_unlock(&event->child_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002454
2455 return total;
2456}
Arjan van de Venfb0459d2009-09-25 12:25:56 +02002457EXPORT_SYMBOL_GPL(perf_event_read_value);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002458
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002459static int perf_event_read_group(struct perf_event *event,
2460 u64 read_format, char __user *buf)
2461{
2462 struct perf_event *leader = event->group_leader, *sub;
Peter Zijlstra6f105812009-11-20 22:19:56 +01002463 int n = 0, size = 0, ret = -EFAULT;
2464 struct perf_event_context *ctx = leader->ctx;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002465 u64 values[5];
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002466 u64 count, enabled, running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002467
Peter Zijlstra6f105812009-11-20 22:19:56 +01002468 mutex_lock(&ctx->mutex);
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002469 count = perf_event_read_value(leader, &enabled, &running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002470
2471 values[n++] = 1 + leader->nr_siblings;
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002472 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2473 values[n++] = enabled;
2474 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2475 values[n++] = running;
Peter Zijlstraabf48682009-11-20 22:19:49 +01002476 values[n++] = count;
2477 if (read_format & PERF_FORMAT_ID)
2478 values[n++] = primary_event_id(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002479
2480 size = n * sizeof(u64);
2481
2482 if (copy_to_user(buf, values, size))
Peter Zijlstra6f105812009-11-20 22:19:56 +01002483 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002484
Peter Zijlstra6f105812009-11-20 22:19:56 +01002485 ret = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002486
2487 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
Peter Zijlstraabf48682009-11-20 22:19:49 +01002488 n = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002489
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002490 values[n++] = perf_event_read_value(sub, &enabled, &running);
Peter Zijlstraabf48682009-11-20 22:19:49 +01002491 if (read_format & PERF_FORMAT_ID)
2492 values[n++] = primary_event_id(sub);
2493
2494 size = n * sizeof(u64);
2495
Stephane Eranian184d3da2009-11-23 21:40:49 -08002496 if (copy_to_user(buf + ret, values, size)) {
Peter Zijlstra6f105812009-11-20 22:19:56 +01002497 ret = -EFAULT;
2498 goto unlock;
2499 }
Peter Zijlstraabf48682009-11-20 22:19:49 +01002500
2501 ret += size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002502 }
Peter Zijlstra6f105812009-11-20 22:19:56 +01002503unlock:
2504 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002505
Peter Zijlstraabf48682009-11-20 22:19:49 +01002506 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002507}
2508
2509static int perf_event_read_one(struct perf_event *event,
2510 u64 read_format, char __user *buf)
2511{
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002512 u64 enabled, running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002513 u64 values[4];
2514 int n = 0;
2515
Peter Zijlstra59ed4462009-11-20 22:19:55 +01002516 values[n++] = perf_event_read_value(event, &enabled, &running);
2517 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
2518 values[n++] = enabled;
2519 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
2520 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002521 if (read_format & PERF_FORMAT_ID)
2522 values[n++] = primary_event_id(event);
2523
2524 if (copy_to_user(buf, values, n * sizeof(u64)))
2525 return -EFAULT;
2526
2527 return n * sizeof(u64);
2528}
2529
2530/*
2531 * Read the performance event - simple non blocking version for now
2532 */
2533static ssize_t
2534perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
2535{
2536 u64 read_format = event->attr.read_format;
2537 int ret;
2538
2539 /*
2540 * Return end-of-file for a read on a event that is in
2541 * error state (i.e. because it was pinned but it couldn't be
2542 * scheduled on to the CPU at some point).
2543 */
2544 if (event->state == PERF_EVENT_STATE_ERROR)
2545 return 0;
2546
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02002547 if (count < event->read_size)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002548 return -ENOSPC;
2549
2550 WARN_ON_ONCE(event->ctx->parent_ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002551 if (read_format & PERF_FORMAT_GROUP)
2552 ret = perf_event_read_group(event, read_format, buf);
2553 else
2554 ret = perf_event_read_one(event, read_format, buf);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002555
2556 return ret;
2557}
2558
2559static ssize_t
2560perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
2561{
2562 struct perf_event *event = file->private_data;
2563
2564 return perf_read_hw(event, buf, count);
2565}
2566
2567static unsigned int perf_poll(struct file *file, poll_table *wait)
2568{
2569 struct perf_event *event = file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002570 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002571 unsigned int events = POLL_HUP;
2572
2573 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002574 buffer = rcu_dereference(event->buffer);
2575 if (buffer)
2576 events = atomic_xchg(&buffer->poll, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002577 rcu_read_unlock();
2578
2579 poll_wait(file, &event->waitq, wait);
2580
2581 return events;
2582}
2583
2584static void perf_event_reset(struct perf_event *event)
2585{
2586 (void)perf_event_read(event);
Peter Zijlstrae7850592010-05-21 14:43:08 +02002587 local64_set(&event->count, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002588 perf_event_update_userpage(event);
2589}
2590
2591/*
2592 * Holding the top-level event's child_mutex means that any
2593 * descendant process that has inherited this event will block
2594 * in sync_child_event if it goes to exit, thus satisfying the
2595 * task existence requirements of perf_event_enable/disable.
2596 */
2597static void perf_event_for_each_child(struct perf_event *event,
2598 void (*func)(struct perf_event *))
2599{
2600 struct perf_event *child;
2601
2602 WARN_ON_ONCE(event->ctx->parent_ctx);
2603 mutex_lock(&event->child_mutex);
2604 func(event);
2605 list_for_each_entry(child, &event->child_list, child_list)
2606 func(child);
2607 mutex_unlock(&event->child_mutex);
2608}
2609
2610static void perf_event_for_each(struct perf_event *event,
2611 void (*func)(struct perf_event *))
2612{
2613 struct perf_event_context *ctx = event->ctx;
2614 struct perf_event *sibling;
2615
2616 WARN_ON_ONCE(ctx->parent_ctx);
2617 mutex_lock(&ctx->mutex);
2618 event = event->group_leader;
2619
2620 perf_event_for_each_child(event, func);
2621 func(event);
2622 list_for_each_entry(sibling, &event->sibling_list, group_entry)
2623 perf_event_for_each_child(event, func);
2624 mutex_unlock(&ctx->mutex);
2625}
2626
2627static int perf_event_period(struct perf_event *event, u64 __user *arg)
2628{
2629 struct perf_event_context *ctx = event->ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002630 int ret = 0;
2631 u64 value;
2632
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01002633 if (!is_sampling_event(event))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002634 return -EINVAL;
2635
John Blackwoodad0cf342010-09-28 18:03:11 -04002636 if (copy_from_user(&value, arg, sizeof(value)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002637 return -EFAULT;
2638
2639 if (!value)
2640 return -EINVAL;
2641
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002642 raw_spin_lock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002643 if (event->attr.freq) {
2644 if (value > sysctl_perf_event_sample_rate) {
2645 ret = -EINVAL;
2646 goto unlock;
2647 }
2648
2649 event->attr.sample_freq = value;
2650 } else {
2651 event->attr.sample_period = value;
2652 event->hw.sample_period = value;
2653 }
2654unlock:
Thomas Gleixnere625cce2009-11-17 18:02:06 +01002655 raw_spin_unlock_irq(&ctx->lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002656
2657 return ret;
2658}
2659
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002660static const struct file_operations perf_fops;
2661
2662static struct perf_event *perf_fget_light(int fd, int *fput_needed)
2663{
2664 struct file *file;
2665
2666 file = fget_light(fd, fput_needed);
2667 if (!file)
2668 return ERR_PTR(-EBADF);
2669
2670 if (file->f_op != &perf_fops) {
2671 fput_light(file, *fput_needed);
2672 *fput_needed = 0;
2673 return ERR_PTR(-EBADF);
2674 }
2675
2676 return file->private_data;
2677}
2678
2679static int perf_event_set_output(struct perf_event *event,
2680 struct perf_event *output_event);
Li Zefan6fb29152009-10-15 11:21:42 +08002681static int perf_event_set_filter(struct perf_event *event, void __user *arg);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002682
2683static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2684{
2685 struct perf_event *event = file->private_data;
2686 void (*func)(struct perf_event *);
2687 u32 flags = arg;
2688
2689 switch (cmd) {
2690 case PERF_EVENT_IOC_ENABLE:
2691 func = perf_event_enable;
2692 break;
2693 case PERF_EVENT_IOC_DISABLE:
2694 func = perf_event_disable;
2695 break;
2696 case PERF_EVENT_IOC_RESET:
2697 func = perf_event_reset;
2698 break;
2699
2700 case PERF_EVENT_IOC_REFRESH:
2701 return perf_event_refresh(event, arg);
2702
2703 case PERF_EVENT_IOC_PERIOD:
2704 return perf_event_period(event, (u64 __user *)arg);
2705
2706 case PERF_EVENT_IOC_SET_OUTPUT:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02002707 {
2708 struct perf_event *output_event = NULL;
2709 int fput_needed = 0;
2710 int ret;
2711
2712 if (arg != -1) {
2713 output_event = perf_fget_light(arg, &fput_needed);
2714 if (IS_ERR(output_event))
2715 return PTR_ERR(output_event);
2716 }
2717
2718 ret = perf_event_set_output(event, output_event);
2719 if (output_event)
2720 fput_light(output_event->filp, fput_needed);
2721
2722 return ret;
2723 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002724
Li Zefan6fb29152009-10-15 11:21:42 +08002725 case PERF_EVENT_IOC_SET_FILTER:
2726 return perf_event_set_filter(event, (void __user *)arg);
2727
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002728 default:
2729 return -ENOTTY;
2730 }
2731
2732 if (flags & PERF_IOC_FLAG_GROUP)
2733 perf_event_for_each(event, func);
2734 else
2735 perf_event_for_each_child(event, func);
2736
2737 return 0;
2738}
2739
2740int perf_event_task_enable(void)
2741{
2742 struct perf_event *event;
2743
2744 mutex_lock(&current->perf_event_mutex);
2745 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2746 perf_event_for_each_child(event, perf_event_enable);
2747 mutex_unlock(&current->perf_event_mutex);
2748
2749 return 0;
2750}
2751
2752int perf_event_task_disable(void)
2753{
2754 struct perf_event *event;
2755
2756 mutex_lock(&current->perf_event_mutex);
2757 list_for_each_entry(event, &current->perf_event_list, owner_entry)
2758 perf_event_for_each_child(event, perf_event_disable);
2759 mutex_unlock(&current->perf_event_mutex);
2760
2761 return 0;
2762}
2763
2764#ifndef PERF_EVENT_INDEX_OFFSET
2765# define PERF_EVENT_INDEX_OFFSET 0
2766#endif
2767
2768static int perf_event_index(struct perf_event *event)
2769{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02002770 if (event->hw.state & PERF_HES_STOPPED)
2771 return 0;
2772
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002773 if (event->state != PERF_EVENT_STATE_ACTIVE)
2774 return 0;
2775
2776 return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
2777}
2778
2779/*
2780 * Callers need to ensure there can be no nesting of this function, otherwise
2781 * the seqlock logic goes bad. We can not serialize this because the arch
2782 * code calls this from NMI context.
2783 */
2784void perf_event_update_userpage(struct perf_event *event)
2785{
2786 struct perf_event_mmap_page *userpg;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002787 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002788
2789 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002790 buffer = rcu_dereference(event->buffer);
2791 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002792 goto unlock;
2793
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002794 userpg = buffer->user_page;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002795
2796 /*
2797 * Disable preemption so as to not let the corresponding user-space
2798 * spin too long if we get preempted.
2799 */
2800 preempt_disable();
2801 ++userpg->lock;
2802 barrier();
2803 userpg->index = perf_event_index(event);
Peter Zijlstrab5e58792010-05-21 14:43:12 +02002804 userpg->offset = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002805 if (event->state == PERF_EVENT_STATE_ACTIVE)
Peter Zijlstrae7850592010-05-21 14:43:08 +02002806 userpg->offset -= local64_read(&event->hw.prev_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002807
2808 userpg->time_enabled = event->total_time_enabled +
2809 atomic64_read(&event->child_total_time_enabled);
2810
2811 userpg->time_running = event->total_time_running +
2812 atomic64_read(&event->child_total_time_running);
2813
2814 barrier();
2815 ++userpg->lock;
2816 preempt_enable();
2817unlock:
2818 rcu_read_unlock();
2819}
2820
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002821static unsigned long perf_data_size(struct perf_buffer *buffer);
2822
2823static void
2824perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
2825{
2826 long max_size = perf_data_size(buffer);
2827
2828 if (watermark)
2829 buffer->watermark = min(max_size, watermark);
2830
2831 if (!buffer->watermark)
2832 buffer->watermark = max_size / 2;
2833
2834 if (flags & PERF_BUFFER_WRITABLE)
2835 buffer->writable = 1;
2836
2837 atomic_set(&buffer->refcount, 1);
2838}
2839
Peter Zijlstra906010b2009-09-21 16:08:49 +02002840#ifndef CONFIG_PERF_USE_VMALLOC
2841
2842/*
2843 * Back perf_mmap() with regular GFP_KERNEL-0 pages.
2844 */
2845
2846static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002847perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002848{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002849 if (pgoff > buffer->nr_pages)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002850 return NULL;
2851
2852 if (pgoff == 0)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002853 return virt_to_page(buffer->user_page);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002854
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002855 return virt_to_page(buffer->data_pages[pgoff - 1]);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002856}
2857
Peter Zijlstraa19d35c2010-05-17 18:48:00 +02002858static void *perf_mmap_alloc_page(int cpu)
2859{
2860 struct page *page;
2861 int node;
2862
2863 node = (cpu == -1) ? cpu : cpu_to_node(cpu);
2864 page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
2865 if (!page)
2866 return NULL;
2867
2868 return page_address(page);
2869}
2870
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002871static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002872perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002873{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002874 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002875 unsigned long size;
2876 int i;
2877
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002878 size = sizeof(struct perf_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002879 size += nr_pages * sizeof(void *);
2880
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002881 buffer = kzalloc(size, GFP_KERNEL);
2882 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002883 goto fail;
2884
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002885 buffer->user_page = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002886 if (!buffer->user_page)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002887 goto fail_user_page;
2888
2889 for (i = 0; i < nr_pages; i++) {
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002890 buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002891 if (!buffer->data_pages[i])
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002892 goto fail_data_pages;
2893 }
2894
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002895 buffer->nr_pages = nr_pages;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002896
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002897 perf_buffer_init(buffer, watermark, flags);
2898
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002899 return buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002900
2901fail_data_pages:
2902 for (i--; i >= 0; i--)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002903 free_page((unsigned long)buffer->data_pages[i]);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002904
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002905 free_page((unsigned long)buffer->user_page);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002906
2907fail_user_page:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002908 kfree(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002909
2910fail:
Peter Zijlstra906010b2009-09-21 16:08:49 +02002911 return NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002912}
2913
2914static void perf_mmap_free_page(unsigned long addr)
2915{
2916 struct page *page = virt_to_page((void *)addr);
2917
2918 page->mapping = NULL;
2919 __free_page(page);
2920}
2921
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002922static void perf_buffer_free(struct perf_buffer *buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002923{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002924 int i;
2925
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002926 perf_mmap_free_page((unsigned long)buffer->user_page);
2927 for (i = 0; i < buffer->nr_pages; i++)
2928 perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
2929 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002930}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02002931
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002932static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002933{
2934 return 0;
2935}
2936
Peter Zijlstra906010b2009-09-21 16:08:49 +02002937#else
2938
2939/*
2940 * Back perf_mmap() with vmalloc memory.
2941 *
2942 * Required for architectures that have d-cache aliasing issues.
2943 */
2944
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002945static inline int page_order(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002946{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002947 return buffer->page_order;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02002948}
2949
Peter Zijlstra906010b2009-09-21 16:08:49 +02002950static struct page *
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002951perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002952{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002953 if (pgoff > (1UL << page_order(buffer)))
Peter Zijlstra906010b2009-09-21 16:08:49 +02002954 return NULL;
2955
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002956 return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002957}
2958
2959static void perf_mmap_unmark_page(void *addr)
2960{
2961 struct page *page = vmalloc_to_page(addr);
2962
2963 page->mapping = NULL;
2964}
2965
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002966static void perf_buffer_free_work(struct work_struct *work)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002967{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002968 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002969 void *base;
2970 int i, nr;
2971
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002972 buffer = container_of(work, struct perf_buffer, work);
2973 nr = 1 << page_order(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002974
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002975 base = buffer->user_page;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002976 for (i = 0; i < nr + 1; i++)
2977 perf_mmap_unmark_page(base + (i * PAGE_SIZE));
2978
2979 vfree(base);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002980 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002981}
2982
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002983static void perf_buffer_free(struct perf_buffer *buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002984{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002985 schedule_work(&buffer->work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002986}
2987
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002988static struct perf_buffer *
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02002989perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
Peter Zijlstra906010b2009-09-21 16:08:49 +02002990{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002991 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02002992 unsigned long size;
2993 void *all_buf;
2994
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002995 size = sizeof(struct perf_buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02002996 size += sizeof(void *);
2997
Peter Zijlstraca5135e2010-05-28 19:33:23 +02002998 buffer = kzalloc(size, GFP_KERNEL);
2999 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003000 goto fail;
3001
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003002 INIT_WORK(&buffer->work, perf_buffer_free_work);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003003
3004 all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
3005 if (!all_buf)
3006 goto fail_all_buf;
3007
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003008 buffer->user_page = all_buf;
3009 buffer->data_pages[0] = all_buf + PAGE_SIZE;
3010 buffer->page_order = ilog2(nr_pages);
3011 buffer->nr_pages = 1;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003012
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003013 perf_buffer_init(buffer, watermark, flags);
3014
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003015 return buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003016
3017fail_all_buf:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003018 kfree(buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003019
3020fail:
3021 return NULL;
3022}
3023
3024#endif
3025
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003026static unsigned long perf_data_size(struct perf_buffer *buffer)
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003027{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003028 return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003029}
3030
Peter Zijlstra906010b2009-09-21 16:08:49 +02003031static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
3032{
3033 struct perf_event *event = vma->vm_file->private_data;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003034 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003035 int ret = VM_FAULT_SIGBUS;
3036
3037 if (vmf->flags & FAULT_FLAG_MKWRITE) {
3038 if (vmf->pgoff == 0)
3039 ret = 0;
3040 return ret;
3041 }
3042
3043 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003044 buffer = rcu_dereference(event->buffer);
3045 if (!buffer)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003046 goto unlock;
3047
3048 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
3049 goto unlock;
3050
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003051 vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003052 if (!vmf->page)
3053 goto unlock;
3054
3055 get_page(vmf->page);
3056 vmf->page->mapping = vma->vm_file->f_mapping;
3057 vmf->page->index = vmf->pgoff;
3058
3059 ret = 0;
3060unlock:
3061 rcu_read_unlock();
3062
3063 return ret;
3064}
3065
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003066static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
Peter Zijlstra906010b2009-09-21 16:08:49 +02003067{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003068 struct perf_buffer *buffer;
Peter Zijlstra906010b2009-09-21 16:08:49 +02003069
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003070 buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
3071 perf_buffer_free(buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003072}
3073
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003074static struct perf_buffer *perf_buffer_get(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003075{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003076 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003077
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003078 rcu_read_lock();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003079 buffer = rcu_dereference(event->buffer);
3080 if (buffer) {
3081 if (!atomic_inc_not_zero(&buffer->refcount))
3082 buffer = NULL;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003083 }
3084 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003085
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003086 return buffer;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003087}
3088
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003089static void perf_buffer_put(struct perf_buffer *buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003090{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003091 if (!atomic_dec_and_test(&buffer->refcount))
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003092 return;
3093
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003094 call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003095}
3096
3097static void perf_mmap_open(struct vm_area_struct *vma)
3098{
3099 struct perf_event *event = vma->vm_file->private_data;
3100
3101 atomic_inc(&event->mmap_count);
3102}
3103
3104static void perf_mmap_close(struct vm_area_struct *vma)
3105{
3106 struct perf_event *event = vma->vm_file->private_data;
3107
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003108 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003109 unsigned long size = perf_data_size(event->buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003110 struct user_struct *user = event->mmap_user;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003111 struct perf_buffer *buffer = event->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003112
Peter Zijlstra906010b2009-09-21 16:08:49 +02003113 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003114 vma->vm_mm->locked_vm -= event->mmap_locked;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003115 rcu_assign_pointer(event->buffer, NULL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003116 mutex_unlock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003117
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003118 perf_buffer_put(buffer);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003119 free_uid(user);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003120 }
3121}
3122
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003123static const struct vm_operations_struct perf_mmap_vmops = {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003124 .open = perf_mmap_open,
3125 .close = perf_mmap_close,
3126 .fault = perf_mmap_fault,
3127 .page_mkwrite = perf_mmap_fault,
3128};
3129
3130static int perf_mmap(struct file *file, struct vm_area_struct *vma)
3131{
3132 struct perf_event *event = file->private_data;
3133 unsigned long user_locked, user_lock_limit;
3134 struct user_struct *user = current_user();
3135 unsigned long locked, lock_limit;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003136 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003137 unsigned long vma_size;
3138 unsigned long nr_pages;
3139 long user_extra, extra;
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003140 int ret = 0, flags = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003141
Peter Zijlstrac7920612010-05-18 10:33:24 +02003142 /*
3143 * Don't allow mmap() of inherited per-task counters. This would
3144 * create a performance issue due to all children writing to the
3145 * same buffer.
3146 */
3147 if (event->cpu == -1 && event->attr.inherit)
3148 return -EINVAL;
3149
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003150 if (!(vma->vm_flags & VM_SHARED))
3151 return -EINVAL;
3152
3153 vma_size = vma->vm_end - vma->vm_start;
3154 nr_pages = (vma_size / PAGE_SIZE) - 1;
3155
3156 /*
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003157 * If we have buffer pages ensure they're a power-of-two number, so we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003158 * can do bitmasks instead of modulo.
3159 */
3160 if (nr_pages != 0 && !is_power_of_2(nr_pages))
3161 return -EINVAL;
3162
3163 if (vma_size != PAGE_SIZE * (1 + nr_pages))
3164 return -EINVAL;
3165
3166 if (vma->vm_pgoff != 0)
3167 return -EINVAL;
3168
3169 WARN_ON_ONCE(event->ctx->parent_ctx);
3170 mutex_lock(&event->mmap_mutex);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003171 if (event->buffer) {
3172 if (event->buffer->nr_pages == nr_pages)
3173 atomic_inc(&event->buffer->refcount);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003174 else
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003175 ret = -EINVAL;
3176 goto unlock;
3177 }
3178
3179 user_extra = nr_pages + 1;
3180 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
3181
3182 /*
3183 * Increase the limit linearly with more CPUs:
3184 */
3185 user_lock_limit *= num_online_cpus();
3186
3187 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
3188
3189 extra = 0;
3190 if (user_locked > user_lock_limit)
3191 extra = user_locked - user_lock_limit;
3192
Jiri Slaby78d7d402010-03-05 13:42:54 -08003193 lock_limit = rlimit(RLIMIT_MEMLOCK);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003194 lock_limit >>= PAGE_SHIFT;
3195 locked = vma->vm_mm->locked_vm + extra;
3196
3197 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
3198 !capable(CAP_IPC_LOCK)) {
3199 ret = -EPERM;
3200 goto unlock;
3201 }
3202
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003203 WARN_ON(event->buffer);
Peter Zijlstra906010b2009-09-21 16:08:49 +02003204
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003205 if (vma->vm_flags & VM_WRITE)
3206 flags |= PERF_BUFFER_WRITABLE;
3207
3208 buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
3209 event->cpu, flags);
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003210 if (!buffer) {
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003211 ret = -ENOMEM;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003212 goto unlock;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003213 }
Peter Zijlstrad57e34f2010-05-28 19:41:35 +02003214 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003215
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003216 atomic_long_add(user_extra, &user->locked_vm);
3217 event->mmap_locked = extra;
3218 event->mmap_user = get_current_user();
3219 vma->vm_mm->locked_vm += event->mmap_locked;
3220
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003221unlock:
Peter Zijlstraac9721f2010-05-27 12:54:41 +02003222 if (!ret)
3223 atomic_inc(&event->mmap_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003224 mutex_unlock(&event->mmap_mutex);
3225
3226 vma->vm_flags |= VM_RESERVED;
3227 vma->vm_ops = &perf_mmap_vmops;
3228
3229 return ret;
3230}
3231
3232static int perf_fasync(int fd, struct file *filp, int on)
3233{
3234 struct inode *inode = filp->f_path.dentry->d_inode;
3235 struct perf_event *event = filp->private_data;
3236 int retval;
3237
3238 mutex_lock(&inode->i_mutex);
3239 retval = fasync_helper(fd, filp, on, &event->fasync);
3240 mutex_unlock(&inode->i_mutex);
3241
3242 if (retval < 0)
3243 return retval;
3244
3245 return 0;
3246}
3247
3248static const struct file_operations perf_fops = {
Arnd Bergmann3326c1c2010-03-23 19:09:33 +01003249 .llseek = no_llseek,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003250 .release = perf_release,
3251 .read = perf_read,
3252 .poll = perf_poll,
3253 .unlocked_ioctl = perf_ioctl,
3254 .compat_ioctl = perf_ioctl,
3255 .mmap = perf_mmap,
3256 .fasync = perf_fasync,
3257};
3258
3259/*
3260 * Perf event wakeup
3261 *
3262 * If there's data, ensure we set the poll() state and publish everything
3263 * to user-space before waking everybody up.
3264 */
3265
3266void perf_event_wakeup(struct perf_event *event)
3267{
3268 wake_up_all(&event->waitq);
3269
3270 if (event->pending_kill) {
3271 kill_fasync(&event->fasync, SIGIO, event->pending_kill);
3272 event->pending_kill = 0;
3273 }
3274}
3275
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003276static void perf_pending_event(struct irq_work *entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003277{
3278 struct perf_event *event = container_of(entry,
3279 struct perf_event, pending);
3280
3281 if (event->pending_disable) {
3282 event->pending_disable = 0;
3283 __perf_event_disable(event);
3284 }
3285
3286 if (event->pending_wakeup) {
3287 event->pending_wakeup = 0;
3288 perf_event_wakeup(event);
3289 }
3290}
3291
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003292/*
Zhang, Yanmin39447b32010-04-19 13:32:41 +08003293 * We assume there is only KVM supporting the callbacks.
3294 * Later on, we might change it to a list if there is
3295 * another virtualization implementation supporting the callbacks.
3296 */
3297struct perf_guest_info_callbacks *perf_guest_cbs;
3298
3299int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3300{
3301 perf_guest_cbs = cbs;
3302 return 0;
3303}
3304EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
3305
3306int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
3307{
3308 perf_guest_cbs = NULL;
3309 return 0;
3310}
3311EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
3312
3313/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003314 * Output
3315 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003316static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003317 unsigned long offset, unsigned long head)
3318{
3319 unsigned long mask;
3320
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003321 if (!buffer->writable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003322 return true;
3323
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003324 mask = perf_data_size(buffer) - 1;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003325
3326 offset = (offset - tail) & mask;
3327 head = (head - tail) & mask;
3328
3329 if ((int)(head - offset) < 0)
3330 return false;
3331
3332 return true;
3333}
3334
3335static void perf_output_wakeup(struct perf_output_handle *handle)
3336{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003337 atomic_set(&handle->buffer->poll, POLL_IN);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003338
3339 if (handle->nmi) {
3340 handle->event->pending_wakeup = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003341 irq_work_queue(&handle->event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003342 } else
3343 perf_event_wakeup(handle->event);
3344}
3345
3346/*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003347 * We need to ensure a later event_id doesn't publish a head when a former
Peter Zijlstraef607772010-05-18 10:50:41 +02003348 * event isn't done writing. However since we need to deal with NMIs we
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003349 * cannot fully serialize things.
3350 *
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003351 * We only publish the head (and generate a wakeup) when the outer-most
Peter Zijlstraef607772010-05-18 10:50:41 +02003352 * event completes.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003353 */
Peter Zijlstraef607772010-05-18 10:50:41 +02003354static void perf_output_get_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003355{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003356 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003357
Peter Zijlstraef607772010-05-18 10:50:41 +02003358 preempt_disable();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003359 local_inc(&buffer->nest);
3360 handle->wakeup = local_read(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003361}
3362
Peter Zijlstraef607772010-05-18 10:50:41 +02003363static void perf_output_put_handle(struct perf_output_handle *handle)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003364{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003365 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003366 unsigned long head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003367
3368again:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003369 head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003370
3371 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003372 * IRQ/NMI can happen here, which means we can miss a head update.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003373 */
3374
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003375 if (!local_dec_and_test(&buffer->nest))
Frederic Weisbeckeracd35a42010-05-20 21:28:34 +02003376 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003377
3378 /*
Peter Zijlstraef607772010-05-18 10:50:41 +02003379 * Publish the known good head. Rely on the full barrier implied
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003380 * by atomic_dec_and_test() order the buffer->head read and this
Peter Zijlstraef607772010-05-18 10:50:41 +02003381 * write.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003382 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003383 buffer->user_page->data_head = head;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003384
Peter Zijlstraef607772010-05-18 10:50:41 +02003385 /*
3386 * Now check if we missed an update, rely on the (compiler)
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003387 * barrier in atomic_dec_and_test() to re-read buffer->head.
Peter Zijlstraef607772010-05-18 10:50:41 +02003388 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003389 if (unlikely(head != local_read(&buffer->head))) {
3390 local_inc(&buffer->nest);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003391 goto again;
3392 }
3393
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003394 if (handle->wakeup != local_read(&buffer->wakeup))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003395 perf_output_wakeup(handle);
Peter Zijlstraef607772010-05-18 10:50:41 +02003396
Peter Zijlstra9ed60602010-06-11 17:36:35 +02003397out:
Peter Zijlstraef607772010-05-18 10:50:41 +02003398 preempt_enable();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003399}
3400
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003401__always_inline void perf_output_copy(struct perf_output_handle *handle,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003402 const void *buf, unsigned int len)
3403{
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003404 do {
Peter Zijlstraa94ffaa2010-05-20 19:50:07 +02003405 unsigned long size = min_t(unsigned long, handle->size, len);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003406
3407 memcpy(handle->addr, buf, size);
3408
3409 len -= size;
3410 handle->addr += size;
Frederic Weisbecker74048f82010-05-27 21:34:58 +02003411 buf += size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003412 handle->size -= size;
3413 if (!handle->size) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003414 struct perf_buffer *buffer = handle->buffer;
Peter Zijlstra3cafa9f2010-05-20 19:07:56 +02003415
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003416 handle->page++;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003417 handle->page &= buffer->nr_pages - 1;
3418 handle->addr = buffer->data_pages[handle->page];
3419 handle->size = PAGE_SIZE << page_order(buffer);
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003420 }
3421 } while (len);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003422}
3423
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003424static void __perf_event_header__init_id(struct perf_event_header *header,
3425 struct perf_sample_data *data,
3426 struct perf_event *event)
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02003427{
3428 u64 sample_type = event->attr.sample_type;
3429
3430 data->type = sample_type;
3431 header->size += event->id_header_size;
3432
3433 if (sample_type & PERF_SAMPLE_TID) {
3434 /* namespace issues */
3435 data->tid_entry.pid = perf_event_pid(event, current);
3436 data->tid_entry.tid = perf_event_tid(event, current);
3437 }
3438
3439 if (sample_type & PERF_SAMPLE_TIME)
3440 data->time = perf_clock();
3441
3442 if (sample_type & PERF_SAMPLE_ID)
3443 data->id = primary_event_id(event);
3444
3445 if (sample_type & PERF_SAMPLE_STREAM_ID)
3446 data->stream_id = event->id;
3447
3448 if (sample_type & PERF_SAMPLE_CPU) {
3449 data->cpu_entry.cpu = raw_smp_processor_id();
3450 data->cpu_entry.reserved = 0;
3451 }
3452}
3453
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003454static void perf_event_header__init_id(struct perf_event_header *header,
3455 struct perf_sample_data *data,
3456 struct perf_event *event)
3457{
3458 if (event->attr.sample_id_all)
3459 __perf_event_header__init_id(header, data, event);
3460}
3461
3462static void __perf_event__output_id_sample(struct perf_output_handle *handle,
3463 struct perf_sample_data *data)
3464{
3465 u64 sample_type = data->type;
3466
3467 if (sample_type & PERF_SAMPLE_TID)
3468 perf_output_put(handle, data->tid_entry);
3469
3470 if (sample_type & PERF_SAMPLE_TIME)
3471 perf_output_put(handle, data->time);
3472
3473 if (sample_type & PERF_SAMPLE_ID)
3474 perf_output_put(handle, data->id);
3475
3476 if (sample_type & PERF_SAMPLE_STREAM_ID)
3477 perf_output_put(handle, data->stream_id);
3478
3479 if (sample_type & PERF_SAMPLE_CPU)
3480 perf_output_put(handle, data->cpu_entry);
3481}
3482
3483static void perf_event__output_id_sample(struct perf_event *event,
3484 struct perf_output_handle *handle,
3485 struct perf_sample_data *sample)
3486{
3487 if (event->attr.sample_id_all)
3488 __perf_event__output_id_sample(handle, sample);
3489}
3490
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003491int perf_output_begin(struct perf_output_handle *handle,
3492 struct perf_event *event, unsigned int size,
3493 int nmi, int sample)
3494{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003495 struct perf_buffer *buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003496 unsigned long tail, offset, head;
3497 int have_lost;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003498 struct perf_sample_data sample_data;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003499 struct {
3500 struct perf_event_header header;
3501 u64 id;
3502 u64 lost;
3503 } lost_event;
3504
3505 rcu_read_lock();
3506 /*
3507 * For inherited events we send all the output towards the parent.
3508 */
3509 if (event->parent)
3510 event = event->parent;
3511
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003512 buffer = rcu_dereference(event->buffer);
3513 if (!buffer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003514 goto out;
3515
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003516 handle->buffer = buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003517 handle->event = event;
3518 handle->nmi = nmi;
3519 handle->sample = sample;
3520
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003521 if (!buffer->nr_pages)
Stephane Eranian00d1d0b2010-05-17 12:46:01 +02003522 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003523
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003524 have_lost = local_read(&buffer->lost);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003525 if (have_lost) {
3526 lost_event.header.size = sizeof(lost_event);
3527 perf_event_header__init_id(&lost_event.header, &sample_data,
3528 event);
3529 size += lost_event.header.size;
3530 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003531
Peter Zijlstraef607772010-05-18 10:50:41 +02003532 perf_output_get_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003533
3534 do {
3535 /*
3536 * Userspace could choose to issue a mb() before updating the
3537 * tail pointer. So that all reads will be completed before the
3538 * write is issued.
3539 */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003540 tail = ACCESS_ONCE(buffer->user_page->data_tail);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003541 smp_rmb();
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003542 offset = head = local_read(&buffer->head);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003543 head += size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003544 if (unlikely(!perf_output_space(buffer, tail, offset, head)))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003545 goto fail;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003546 } while (local_cmpxchg(&buffer->head, offset, head) != offset);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003547
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003548 if (head - local_read(&buffer->wakeup) > buffer->watermark)
3549 local_add(buffer->watermark, &buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003550
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003551 handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
3552 handle->page &= buffer->nr_pages - 1;
3553 handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
3554 handle->addr = buffer->data_pages[handle->page];
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003555 handle->addr += handle->size;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003556 handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
Peter Zijlstra5d967a82010-05-20 16:46:39 +02003557
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003558 if (have_lost) {
3559 lost_event.header.type = PERF_RECORD_LOST;
3560 lost_event.header.misc = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003561 lost_event.id = event->id;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003562 lost_event.lost = local_xchg(&buffer->lost, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003563
3564 perf_output_put(handle, lost_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003565 perf_event__output_id_sample(event, handle, &sample_data);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003566 }
3567
3568 return 0;
3569
3570fail:
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003571 local_inc(&buffer->lost);
Peter Zijlstraef607772010-05-18 10:50:41 +02003572 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003573out:
3574 rcu_read_unlock();
3575
3576 return -ENOSPC;
3577}
3578
3579void perf_output_end(struct perf_output_handle *handle)
3580{
3581 struct perf_event *event = handle->event;
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003582 struct perf_buffer *buffer = handle->buffer;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003583
3584 int wakeup_events = event->attr.wakeup_events;
3585
3586 if (handle->sample && wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003587 int events = local_inc_return(&buffer->events);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003588 if (events >= wakeup_events) {
Peter Zijlstraca5135e2010-05-28 19:33:23 +02003589 local_sub(wakeup_events, &buffer->events);
3590 local_inc(&buffer->wakeup);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003591 }
3592 }
3593
Peter Zijlstraef607772010-05-18 10:50:41 +02003594 perf_output_put_handle(handle);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003595 rcu_read_unlock();
3596}
3597
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003598static void perf_output_read_one(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02003599 struct perf_event *event,
3600 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003601{
3602 u64 read_format = event->attr.read_format;
3603 u64 values[4];
3604 int n = 0;
3605
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003606 values[n++] = perf_event_count(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003607 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
Stephane Eranianeed01522010-10-26 16:08:01 +02003608 values[n++] = enabled +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003609 atomic64_read(&event->child_total_time_enabled);
3610 }
3611 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
Stephane Eranianeed01522010-10-26 16:08:01 +02003612 values[n++] = running +
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003613 atomic64_read(&event->child_total_time_running);
3614 }
3615 if (read_format & PERF_FORMAT_ID)
3616 values[n++] = primary_event_id(event);
3617
3618 perf_output_copy(handle, values, n * sizeof(u64));
3619}
3620
3621/*
3622 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3623 */
3624static void perf_output_read_group(struct perf_output_handle *handle,
Stephane Eranianeed01522010-10-26 16:08:01 +02003625 struct perf_event *event,
3626 u64 enabled, u64 running)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003627{
3628 struct perf_event *leader = event->group_leader, *sub;
3629 u64 read_format = event->attr.read_format;
3630 u64 values[5];
3631 int n = 0;
3632
3633 values[n++] = 1 + leader->nr_siblings;
3634
3635 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
Stephane Eranianeed01522010-10-26 16:08:01 +02003636 values[n++] = enabled;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003637
3638 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
Stephane Eranianeed01522010-10-26 16:08:01 +02003639 values[n++] = running;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003640
3641 if (leader != event)
3642 leader->pmu->read(leader);
3643
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003644 values[n++] = perf_event_count(leader);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003645 if (read_format & PERF_FORMAT_ID)
3646 values[n++] = primary_event_id(leader);
3647
3648 perf_output_copy(handle, values, n * sizeof(u64));
3649
3650 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3651 n = 0;
3652
3653 if (sub != event)
3654 sub->pmu->read(sub);
3655
Peter Zijlstrab5e58792010-05-21 14:43:12 +02003656 values[n++] = perf_event_count(sub);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003657 if (read_format & PERF_FORMAT_ID)
3658 values[n++] = primary_event_id(sub);
3659
3660 perf_output_copy(handle, values, n * sizeof(u64));
3661 }
3662}
3663
Stephane Eranianeed01522010-10-26 16:08:01 +02003664#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
3665 PERF_FORMAT_TOTAL_TIME_RUNNING)
3666
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003667static void perf_output_read(struct perf_output_handle *handle,
3668 struct perf_event *event)
3669{
Stephane Eranianeed01522010-10-26 16:08:01 +02003670 u64 enabled = 0, running = 0, now, ctx_time;
3671 u64 read_format = event->attr.read_format;
3672
3673 /*
3674 * compute total_time_enabled, total_time_running
3675 * based on snapshot values taken when the event
3676 * was last scheduled in.
3677 *
3678 * we cannot simply called update_context_time()
3679 * because of locking issue as we are called in
3680 * NMI context
3681 */
3682 if (read_format & PERF_FORMAT_TOTAL_TIMES) {
3683 now = perf_clock();
3684 ctx_time = event->shadow_ctx_time + now;
3685 enabled = ctx_time - event->tstamp_enabled;
3686 running = ctx_time - event->tstamp_running;
3687 }
3688
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003689 if (event->attr.read_format & PERF_FORMAT_GROUP)
Stephane Eranianeed01522010-10-26 16:08:01 +02003690 perf_output_read_group(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003691 else
Stephane Eranianeed01522010-10-26 16:08:01 +02003692 perf_output_read_one(handle, event, enabled, running);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003693}
3694
3695void perf_output_sample(struct perf_output_handle *handle,
3696 struct perf_event_header *header,
3697 struct perf_sample_data *data,
3698 struct perf_event *event)
3699{
3700 u64 sample_type = data->type;
3701
3702 perf_output_put(handle, *header);
3703
3704 if (sample_type & PERF_SAMPLE_IP)
3705 perf_output_put(handle, data->ip);
3706
3707 if (sample_type & PERF_SAMPLE_TID)
3708 perf_output_put(handle, data->tid_entry);
3709
3710 if (sample_type & PERF_SAMPLE_TIME)
3711 perf_output_put(handle, data->time);
3712
3713 if (sample_type & PERF_SAMPLE_ADDR)
3714 perf_output_put(handle, data->addr);
3715
3716 if (sample_type & PERF_SAMPLE_ID)
3717 perf_output_put(handle, data->id);
3718
3719 if (sample_type & PERF_SAMPLE_STREAM_ID)
3720 perf_output_put(handle, data->stream_id);
3721
3722 if (sample_type & PERF_SAMPLE_CPU)
3723 perf_output_put(handle, data->cpu_entry);
3724
3725 if (sample_type & PERF_SAMPLE_PERIOD)
3726 perf_output_put(handle, data->period);
3727
3728 if (sample_type & PERF_SAMPLE_READ)
3729 perf_output_read(handle, event);
3730
3731 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3732 if (data->callchain) {
3733 int size = 1;
3734
3735 if (data->callchain)
3736 size += data->callchain->nr;
3737
3738 size *= sizeof(u64);
3739
3740 perf_output_copy(handle, data->callchain, size);
3741 } else {
3742 u64 nr = 0;
3743 perf_output_put(handle, nr);
3744 }
3745 }
3746
3747 if (sample_type & PERF_SAMPLE_RAW) {
3748 if (data->raw) {
3749 perf_output_put(handle, data->raw->size);
3750 perf_output_copy(handle, data->raw->data,
3751 data->raw->size);
3752 } else {
3753 struct {
3754 u32 size;
3755 u32 data;
3756 } raw = {
3757 .size = sizeof(u32),
3758 .data = 0,
3759 };
3760 perf_output_put(handle, raw);
3761 }
3762 }
3763}
3764
3765void perf_prepare_sample(struct perf_event_header *header,
3766 struct perf_sample_data *data,
3767 struct perf_event *event,
3768 struct pt_regs *regs)
3769{
3770 u64 sample_type = event->attr.sample_type;
3771
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003772 header->type = PERF_RECORD_SAMPLE;
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003773 header->size = sizeof(*header) + event->header_size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003774
3775 header->misc = 0;
3776 header->misc |= perf_misc_flags(regs);
3777
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003778 __perf_event_header__init_id(header, data, event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02003779
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003780 if (sample_type & PERF_SAMPLE_IP)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003781 data->ip = perf_instruction_pointer(regs);
3782
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003783 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
3784 int size = 1;
3785
3786 data->callchain = perf_callchain(regs);
3787
3788 if (data->callchain)
3789 size += data->callchain->nr;
3790
3791 header->size += size * sizeof(u64);
3792 }
3793
3794 if (sample_type & PERF_SAMPLE_RAW) {
3795 int size = sizeof(u32);
3796
3797 if (data->raw)
3798 size += data->raw->size;
3799 else
3800 size += sizeof(u32);
3801
3802 WARN_ON_ONCE(size & (sizeof(u64)-1));
3803 header->size += size;
3804 }
3805}
3806
3807static void perf_event_output(struct perf_event *event, int nmi,
3808 struct perf_sample_data *data,
3809 struct pt_regs *regs)
3810{
3811 struct perf_output_handle handle;
3812 struct perf_event_header header;
3813
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003814 /* protect the callchain buffers */
3815 rcu_read_lock();
3816
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003817 perf_prepare_sample(&header, data, event, regs);
3818
3819 if (perf_output_begin(&handle, event, header.size, nmi, 1))
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003820 goto exit;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003821
3822 perf_output_sample(&handle, &header, data, event);
3823
3824 perf_output_end(&handle);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02003825
3826exit:
3827 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003828}
3829
3830/*
3831 * read event_id
3832 */
3833
3834struct perf_read_event {
3835 struct perf_event_header header;
3836
3837 u32 pid;
3838 u32 tid;
3839};
3840
3841static void
3842perf_event_read_event(struct perf_event *event,
3843 struct task_struct *task)
3844{
3845 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003846 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003847 struct perf_read_event read_event = {
3848 .header = {
3849 .type = PERF_RECORD_READ,
3850 .misc = 0,
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02003851 .size = sizeof(read_event) + event->read_size,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003852 },
3853 .pid = perf_event_pid(event, task),
3854 .tid = perf_event_tid(event, task),
3855 };
3856 int ret;
3857
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003858 perf_event_header__init_id(&read_event.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003859 ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
3860 if (ret)
3861 return;
3862
3863 perf_output_put(&handle, read_event);
3864 perf_output_read(&handle, event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003865 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003866
3867 perf_output_end(&handle);
3868}
3869
3870/*
3871 * task tracking -- fork/exit
3872 *
Eric B Munson3af9e852010-05-18 15:30:49 +01003873 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003874 */
3875
3876struct perf_task_event {
3877 struct task_struct *task;
3878 struct perf_event_context *task_ctx;
3879
3880 struct {
3881 struct perf_event_header header;
3882
3883 u32 pid;
3884 u32 ppid;
3885 u32 tid;
3886 u32 ptid;
3887 u64 time;
3888 } event_id;
3889};
3890
3891static void perf_event_task_output(struct perf_event *event,
3892 struct perf_task_event *task_event)
3893{
3894 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003895 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003896 struct task_struct *task = task_event->task;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003897 int ret, size = task_event->event_id.header.size;
Mike Galbraith8bb39f92010-03-26 11:11:33 +01003898
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003899 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003900
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003901 ret = perf_output_begin(&handle, event,
3902 task_event->event_id.header.size, 0, 0);
Peter Zijlstraef607772010-05-18 10:50:41 +02003903 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003904 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003905
3906 task_event->event_id.pid = perf_event_pid(event, task);
3907 task_event->event_id.ppid = perf_event_pid(event, current);
3908
3909 task_event->event_id.tid = perf_event_tid(event, task);
3910 task_event->event_id.ptid = perf_event_tid(event, current);
3911
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003912 perf_output_put(&handle, task_event->event_id);
3913
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003914 perf_event__output_id_sample(event, &handle, &sample);
3915
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003916 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02003917out:
3918 task_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003919}
3920
3921static int perf_event_task_match(struct perf_event *event)
3922{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01003923 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01003924 return 0;
3925
Stephane Eranian5632ab12011-01-03 18:20:01 +02003926 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01003927 return 0;
3928
Eric B Munson3af9e852010-05-18 15:30:49 +01003929 if (event->attr.comm || event->attr.mmap ||
3930 event->attr.mmap_data || event->attr.task)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003931 return 1;
3932
3933 return 0;
3934}
3935
3936static void perf_event_task_ctx(struct perf_event_context *ctx,
3937 struct perf_task_event *task_event)
3938{
3939 struct perf_event *event;
3940
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003941 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
3942 if (perf_event_task_match(event))
3943 perf_event_task_output(event, task_event);
3944 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003945}
3946
3947static void perf_event_task_event(struct perf_task_event *task_event)
3948{
3949 struct perf_cpu_context *cpuctx;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003950 struct perf_event_context *ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003951 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003952 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003953
Peter Zijlstrad6ff86c2009-11-20 22:19:46 +01003954 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003955 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02003956 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01003957 if (cpuctx->active_pmu != pmu)
3958 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003959 perf_event_task_ctx(&cpuctx->ctx, task_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003960
3961 ctx = task_event->task_ctx;
3962 if (!ctx) {
3963 ctxn = pmu->task_ctx_nr;
3964 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02003965 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02003966 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
3967 }
3968 if (ctx)
3969 perf_event_task_ctx(ctx, task_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02003970next:
3971 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02003972 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02003973 rcu_read_unlock();
3974}
3975
3976static void perf_event_task(struct task_struct *task,
3977 struct perf_event_context *task_ctx,
3978 int new)
3979{
3980 struct perf_task_event task_event;
3981
3982 if (!atomic_read(&nr_comm_events) &&
3983 !atomic_read(&nr_mmap_events) &&
3984 !atomic_read(&nr_task_events))
3985 return;
3986
3987 task_event = (struct perf_task_event){
3988 .task = task,
3989 .task_ctx = task_ctx,
3990 .event_id = {
3991 .header = {
3992 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
3993 .misc = 0,
3994 .size = sizeof(task_event.event_id),
3995 },
3996 /* .pid */
3997 /* .ppid */
3998 /* .tid */
3999 /* .ptid */
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004000 .time = perf_clock(),
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004001 },
4002 };
4003
4004 perf_event_task_event(&task_event);
4005}
4006
4007void perf_event_fork(struct task_struct *task)
4008{
4009 perf_event_task(task, NULL, 1);
4010}
4011
4012/*
4013 * comm tracking
4014 */
4015
4016struct perf_comm_event {
4017 struct task_struct *task;
4018 char *comm;
4019 int comm_size;
4020
4021 struct {
4022 struct perf_event_header header;
4023
4024 u32 pid;
4025 u32 tid;
4026 } event_id;
4027};
4028
4029static void perf_event_comm_output(struct perf_event *event,
4030 struct perf_comm_event *comm_event)
4031{
4032 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004033 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004034 int size = comm_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004035 int ret;
4036
4037 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
4038 ret = perf_output_begin(&handle, event,
4039 comm_event->event_id.header.size, 0, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004040
4041 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004042 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004043
4044 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
4045 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
4046
4047 perf_output_put(&handle, comm_event->event_id);
4048 perf_output_copy(&handle, comm_event->comm,
4049 comm_event->comm_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004050
4051 perf_event__output_id_sample(event, &handle, &sample);
4052
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004053 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004054out:
4055 comm_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004056}
4057
4058static int perf_event_comm_match(struct perf_event *event)
4059{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004060 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004061 return 0;
4062
Stephane Eranian5632ab12011-01-03 18:20:01 +02004063 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004064 return 0;
4065
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004066 if (event->attr.comm)
4067 return 1;
4068
4069 return 0;
4070}
4071
4072static void perf_event_comm_ctx(struct perf_event_context *ctx,
4073 struct perf_comm_event *comm_event)
4074{
4075 struct perf_event *event;
4076
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004077 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
4078 if (perf_event_comm_match(event))
4079 perf_event_comm_output(event, comm_event);
4080 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004081}
4082
4083static void perf_event_comm_event(struct perf_comm_event *comm_event)
4084{
4085 struct perf_cpu_context *cpuctx;
4086 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004087 char comm[TASK_COMM_LEN];
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004088 unsigned int size;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004089 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004090 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004091
4092 memset(comm, 0, sizeof(comm));
Márton Németh96b02d72009-11-21 23:10:15 +01004093 strlcpy(comm, comm_event->task->comm, sizeof(comm));
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004094 size = ALIGN(strlen(comm)+1, sizeof(u64));
4095
4096 comm_event->comm = comm;
4097 comm_event->comm_size = size;
4098
4099 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
Peter Zijlstraf6595f32009-11-20 22:19:47 +01004100 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004101 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004102 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004103 if (cpuctx->active_pmu != pmu)
4104 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004105 perf_event_comm_ctx(&cpuctx->ctx, comm_event);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004106
4107 ctxn = pmu->task_ctx_nr;
4108 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004109 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004110
4111 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4112 if (ctx)
4113 perf_event_comm_ctx(ctx, comm_event);
Peter Zijlstra41945f62010-09-16 19:17:24 +02004114next:
4115 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004116 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004117 rcu_read_unlock();
4118}
4119
4120void perf_event_comm(struct task_struct *task)
4121{
4122 struct perf_comm_event comm_event;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004123 struct perf_event_context *ctx;
4124 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004125
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004126 for_each_task_context_nr(ctxn) {
4127 ctx = task->perf_event_ctxp[ctxn];
4128 if (!ctx)
4129 continue;
4130
4131 perf_event_enable_on_exec(ctx);
4132 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004133
4134 if (!atomic_read(&nr_comm_events))
4135 return;
4136
4137 comm_event = (struct perf_comm_event){
4138 .task = task,
4139 /* .comm */
4140 /* .comm_size */
4141 .event_id = {
4142 .header = {
4143 .type = PERF_RECORD_COMM,
4144 .misc = 0,
4145 /* .size */
4146 },
4147 /* .pid */
4148 /* .tid */
4149 },
4150 };
4151
4152 perf_event_comm_event(&comm_event);
4153}
4154
4155/*
4156 * mmap tracking
4157 */
4158
4159struct perf_mmap_event {
4160 struct vm_area_struct *vma;
4161
4162 const char *file_name;
4163 int file_size;
4164
4165 struct {
4166 struct perf_event_header header;
4167
4168 u32 pid;
4169 u32 tid;
4170 u64 start;
4171 u64 len;
4172 u64 pgoff;
4173 } event_id;
4174};
4175
4176static void perf_event_mmap_output(struct perf_event *event,
4177 struct perf_mmap_event *mmap_event)
4178{
4179 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004180 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004181 int size = mmap_event->event_id.header.size;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004182 int ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004183
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004184 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
4185 ret = perf_output_begin(&handle, event,
4186 mmap_event->event_id.header.size, 0, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004187 if (ret)
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004188 goto out;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004189
4190 mmap_event->event_id.pid = perf_event_pid(event, current);
4191 mmap_event->event_id.tid = perf_event_tid(event, current);
4192
4193 perf_output_put(&handle, mmap_event->event_id);
4194 perf_output_copy(&handle, mmap_event->file_name,
4195 mmap_event->file_size);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004196
4197 perf_event__output_id_sample(event, &handle, &sample);
4198
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004199 perf_output_end(&handle);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004200out:
4201 mmap_event->event_id.header.size = size;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004202}
4203
4204static int perf_event_mmap_match(struct perf_event *event,
Eric B Munson3af9e852010-05-18 15:30:49 +01004205 struct perf_mmap_event *mmap_event,
4206 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004207{
Peter Zijlstra6f93d0a2010-02-14 11:12:04 +01004208 if (event->state < PERF_EVENT_STATE_INACTIVE)
Peter Zijlstra22e19082010-01-18 09:12:32 +01004209 return 0;
4210
Stephane Eranian5632ab12011-01-03 18:20:01 +02004211 if (!event_filter_match(event))
Peter Zijlstra5d27c232009-12-17 13:16:32 +01004212 return 0;
4213
Eric B Munson3af9e852010-05-18 15:30:49 +01004214 if ((!executable && event->attr.mmap_data) ||
4215 (executable && event->attr.mmap))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004216 return 1;
4217
4218 return 0;
4219}
4220
4221static void perf_event_mmap_ctx(struct perf_event_context *ctx,
Eric B Munson3af9e852010-05-18 15:30:49 +01004222 struct perf_mmap_event *mmap_event,
4223 int executable)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004224{
4225 struct perf_event *event;
4226
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004227 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
Eric B Munson3af9e852010-05-18 15:30:49 +01004228 if (perf_event_mmap_match(event, mmap_event, executable))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004229 perf_event_mmap_output(event, mmap_event);
4230 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004231}
4232
4233static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
4234{
4235 struct perf_cpu_context *cpuctx;
4236 struct perf_event_context *ctx;
4237 struct vm_area_struct *vma = mmap_event->vma;
4238 struct file *file = vma->vm_file;
4239 unsigned int size;
4240 char tmp[16];
4241 char *buf = NULL;
4242 const char *name;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004243 struct pmu *pmu;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004244 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004245
4246 memset(tmp, 0, sizeof(tmp));
4247
4248 if (file) {
4249 /*
4250 * d_path works from the end of the buffer backwards, so we
4251 * need to add enough zero bytes after the string to handle
4252 * the 64bit alignment we do later.
4253 */
4254 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
4255 if (!buf) {
4256 name = strncpy(tmp, "//enomem", sizeof(tmp));
4257 goto got_name;
4258 }
4259 name = d_path(&file->f_path, buf, PATH_MAX);
4260 if (IS_ERR(name)) {
4261 name = strncpy(tmp, "//toolong", sizeof(tmp));
4262 goto got_name;
4263 }
4264 } else {
4265 if (arch_vma_name(mmap_event->vma)) {
4266 name = strncpy(tmp, arch_vma_name(mmap_event->vma),
4267 sizeof(tmp));
4268 goto got_name;
4269 }
4270
4271 if (!vma->vm_mm) {
4272 name = strncpy(tmp, "[vdso]", sizeof(tmp));
4273 goto got_name;
Eric B Munson3af9e852010-05-18 15:30:49 +01004274 } else if (vma->vm_start <= vma->vm_mm->start_brk &&
4275 vma->vm_end >= vma->vm_mm->brk) {
4276 name = strncpy(tmp, "[heap]", sizeof(tmp));
4277 goto got_name;
4278 } else if (vma->vm_start <= vma->vm_mm->start_stack &&
4279 vma->vm_end >= vma->vm_mm->start_stack) {
4280 name = strncpy(tmp, "[stack]", sizeof(tmp));
4281 goto got_name;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004282 }
4283
4284 name = strncpy(tmp, "//anon", sizeof(tmp));
4285 goto got_name;
4286 }
4287
4288got_name:
4289 size = ALIGN(strlen(name)+1, sizeof(u64));
4290
4291 mmap_event->file_name = name;
4292 mmap_event->file_size = size;
4293
4294 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
4295
Peter Zijlstraf6d9dd22009-11-20 22:19:48 +01004296 rcu_read_lock();
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004297 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra41945f62010-09-16 19:17:24 +02004298 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra51676952010-12-07 14:18:20 +01004299 if (cpuctx->active_pmu != pmu)
4300 goto next;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004301 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
4302 vma->vm_flags & VM_EXEC);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004303
4304 ctxn = pmu->task_ctx_nr;
4305 if (ctxn < 0)
Peter Zijlstra41945f62010-09-16 19:17:24 +02004306 goto next;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02004307
4308 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
4309 if (ctx) {
4310 perf_event_mmap_ctx(ctx, mmap_event,
4311 vma->vm_flags & VM_EXEC);
4312 }
Peter Zijlstra41945f62010-09-16 19:17:24 +02004313next:
4314 put_cpu_ptr(pmu->pmu_cpu_context);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02004315 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004316 rcu_read_unlock();
4317
4318 kfree(buf);
4319}
4320
Eric B Munson3af9e852010-05-18 15:30:49 +01004321void perf_event_mmap(struct vm_area_struct *vma)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004322{
4323 struct perf_mmap_event mmap_event;
4324
4325 if (!atomic_read(&nr_mmap_events))
4326 return;
4327
4328 mmap_event = (struct perf_mmap_event){
4329 .vma = vma,
4330 /* .file_name */
4331 /* .file_size */
4332 .event_id = {
4333 .header = {
4334 .type = PERF_RECORD_MMAP,
Zhang, Yanmin39447b32010-04-19 13:32:41 +08004335 .misc = PERF_RECORD_MISC_USER,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004336 /* .size */
4337 },
4338 /* .pid */
4339 /* .tid */
4340 .start = vma->vm_start,
4341 .len = vma->vm_end - vma->vm_start,
Peter Zijlstra3a0304e2010-02-26 10:33:41 +01004342 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004343 },
4344 };
4345
4346 perf_event_mmap_event(&mmap_event);
4347}
4348
4349/*
4350 * IRQ throttle logging
4351 */
4352
4353static void perf_log_throttle(struct perf_event *event, int enable)
4354{
4355 struct perf_output_handle handle;
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004356 struct perf_sample_data sample;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004357 int ret;
4358
4359 struct {
4360 struct perf_event_header header;
4361 u64 time;
4362 u64 id;
4363 u64 stream_id;
4364 } throttle_event = {
4365 .header = {
4366 .type = PERF_RECORD_THROTTLE,
4367 .misc = 0,
4368 .size = sizeof(throttle_event),
4369 },
4370 .time = perf_clock(),
4371 .id = primary_event_id(event),
4372 .stream_id = event->id,
4373 };
4374
4375 if (enable)
4376 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
4377
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004378 perf_event_header__init_id(&throttle_event.header, &sample, event);
4379
4380 ret = perf_output_begin(&handle, event,
4381 throttle_event.header.size, 1, 0);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004382 if (ret)
4383 return;
4384
4385 perf_output_put(&handle, throttle_event);
Arnaldo Carvalho de Meloc980d102010-12-04 23:02:20 -02004386 perf_event__output_id_sample(event, &handle, &sample);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004387 perf_output_end(&handle);
4388}
4389
4390/*
4391 * Generic event overflow handling, sampling.
4392 */
4393
4394static int __perf_event_overflow(struct perf_event *event, int nmi,
4395 int throttle, struct perf_sample_data *data,
4396 struct pt_regs *regs)
4397{
4398 int events = atomic_read(&event->event_limit);
4399 struct hw_perf_event *hwc = &event->hw;
4400 int ret = 0;
4401
Peter Zijlstra96398822010-11-24 18:55:29 +01004402 /*
4403 * Non-sampling counters might still use the PMI to fold short
4404 * hardware counters, ignore those.
4405 */
4406 if (unlikely(!is_sampling_event(event)))
4407 return 0;
4408
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004409 if (!throttle) {
4410 hwc->interrupts++;
4411 } else {
4412 if (hwc->interrupts != MAX_INTERRUPTS) {
4413 hwc->interrupts++;
4414 if (HZ * hwc->interrupts >
4415 (u64)sysctl_perf_event_sample_rate) {
4416 hwc->interrupts = MAX_INTERRUPTS;
4417 perf_log_throttle(event, 0);
4418 ret = 1;
4419 }
4420 } else {
4421 /*
4422 * Keep re-disabling events even though on the previous
4423 * pass we disabled it - just in case we raced with a
4424 * sched-in and the event got enabled again:
4425 */
4426 ret = 1;
4427 }
4428 }
4429
4430 if (event->attr.freq) {
4431 u64 now = perf_clock();
Peter Zijlstraabd50712010-01-26 18:50:16 +01004432 s64 delta = now - hwc->freq_time_stamp;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004433
Peter Zijlstraabd50712010-01-26 18:50:16 +01004434 hwc->freq_time_stamp = now;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004435
Peter Zijlstraabd50712010-01-26 18:50:16 +01004436 if (delta > 0 && delta < 2*TICK_NSEC)
4437 perf_adjust_period(event, delta, hwc->last_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004438 }
4439
4440 /*
4441 * XXX event_limit might not quite work as expected on inherited
4442 * events
4443 */
4444
4445 event->pending_kill = POLL_IN;
4446 if (events && atomic_dec_and_test(&event->event_limit)) {
4447 ret = 1;
4448 event->pending_kill = POLL_HUP;
4449 if (nmi) {
4450 event->pending_disable = 1;
Peter Zijlstrae360adb2010-10-14 14:01:34 +08004451 irq_work_queue(&event->pending);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004452 } else
4453 perf_event_disable(event);
4454 }
4455
Peter Zijlstra453f19e2009-11-20 22:19:43 +01004456 if (event->overflow_handler)
4457 event->overflow_handler(event, nmi, data, regs);
4458 else
4459 perf_event_output(event, nmi, data, regs);
4460
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004461 return ret;
4462}
4463
4464int perf_event_overflow(struct perf_event *event, int nmi,
4465 struct perf_sample_data *data,
4466 struct pt_regs *regs)
4467{
4468 return __perf_event_overflow(event, nmi, 1, data, regs);
4469}
4470
4471/*
4472 * Generic software event infrastructure
4473 */
4474
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004475struct swevent_htable {
4476 struct swevent_hlist *swevent_hlist;
4477 struct mutex hlist_mutex;
4478 int hlist_refcount;
4479
4480 /* Recursion avoidance in each contexts */
4481 int recursion[PERF_NR_CONTEXTS];
4482};
4483
4484static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
4485
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004486/*
4487 * We directly increment event->count and keep a second value in
4488 * event->hw.period_left to count intervals. This period event
4489 * is kept in the range [-sample_period, 0] so that we can use the
4490 * sign as trigger.
4491 */
4492
4493static u64 perf_swevent_set_period(struct perf_event *event)
4494{
4495 struct hw_perf_event *hwc = &event->hw;
4496 u64 period = hwc->last_period;
4497 u64 nr, offset;
4498 s64 old, val;
4499
4500 hwc->last_period = hwc->sample_period;
4501
4502again:
Peter Zijlstrae7850592010-05-21 14:43:08 +02004503 old = val = local64_read(&hwc->period_left);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004504 if (val < 0)
4505 return 0;
4506
4507 nr = div64_u64(period + val, period);
4508 offset = nr * period;
4509 val -= offset;
Peter Zijlstrae7850592010-05-21 14:43:08 +02004510 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004511 goto again;
4512
4513 return nr;
4514}
4515
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004516static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004517 int nmi, struct perf_sample_data *data,
4518 struct pt_regs *regs)
4519{
4520 struct hw_perf_event *hwc = &event->hw;
4521 int throttle = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004522
4523 data->period = event->hw.last_period;
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004524 if (!overflow)
4525 overflow = perf_swevent_set_period(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004526
4527 if (hwc->interrupts == MAX_INTERRUPTS)
4528 return;
4529
4530 for (; overflow; overflow--) {
4531 if (__perf_event_overflow(event, nmi, throttle,
4532 data, regs)) {
4533 /*
4534 * We inhibit the overflow from happening when
4535 * hwc->interrupts == MAX_INTERRUPTS.
4536 */
4537 break;
4538 }
4539 throttle = 1;
4540 }
4541}
4542
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004543static void perf_swevent_event(struct perf_event *event, u64 nr,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004544 int nmi, struct perf_sample_data *data,
4545 struct pt_regs *regs)
4546{
4547 struct hw_perf_event *hwc = &event->hw;
4548
Peter Zijlstrae7850592010-05-21 14:43:08 +02004549 local64_add(nr, &event->count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004550
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004551 if (!regs)
4552 return;
4553
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01004554 if (!is_sampling_event(event))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004555 return;
4556
4557 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
4558 return perf_swevent_overflow(event, 1, nmi, data, regs);
4559
Peter Zijlstrae7850592010-05-21 14:43:08 +02004560 if (local64_add_negative(nr, &hwc->period_left))
Peter Zijlstra0cff7842009-11-20 22:19:44 +01004561 return;
4562
4563 perf_swevent_overflow(event, 0, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004564}
4565
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004566static int perf_exclude_event(struct perf_event *event,
4567 struct pt_regs *regs)
4568{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004569 if (event->hw.state & PERF_HES_STOPPED)
4570 return 0;
4571
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004572 if (regs) {
4573 if (event->attr.exclude_user && user_mode(regs))
4574 return 1;
4575
4576 if (event->attr.exclude_kernel && !user_mode(regs))
4577 return 1;
4578 }
4579
4580 return 0;
4581}
4582
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004583static int perf_swevent_match(struct perf_event *event,
4584 enum perf_type_id type,
Li Zefan6fb29152009-10-15 11:21:42 +08004585 u32 event_id,
4586 struct perf_sample_data *data,
4587 struct pt_regs *regs)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004588{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004589 if (event->attr.type != type)
4590 return 0;
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004591
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004592 if (event->attr.config != event_id)
4593 return 0;
4594
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01004595 if (perf_exclude_event(event, regs))
4596 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004597
4598 return 1;
4599}
4600
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004601static inline u64 swevent_hash(u64 type, u32 event_id)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004602{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004603 u64 val = event_id | (type << 32);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004604
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004605 return hash_64(val, SWEVENT_HLIST_BITS);
4606}
4607
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004608static inline struct hlist_head *
4609__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004610{
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004611 u64 hash = swevent_hash(type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004612
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004613 return &hlist->heads[hash];
4614}
4615
4616/* For the read side: events when they trigger */
4617static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004618find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004619{
4620 struct swevent_hlist *hlist;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004621
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004622 hlist = rcu_dereference(swhash->swevent_hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004623 if (!hlist)
4624 return NULL;
4625
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004626 return __find_swevent_head(hlist, type, event_id);
4627}
4628
4629/* For the event head insertion and removal in the hlist */
4630static inline struct hlist_head *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004631find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004632{
4633 struct swevent_hlist *hlist;
4634 u32 event_id = event->attr.config;
4635 u64 type = event->attr.type;
4636
4637 /*
4638 * Event scheduling is always serialized against hlist allocation
4639 * and release. Which makes the protected version suitable here.
4640 * The context lock guarantees that.
4641 */
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004642 hlist = rcu_dereference_protected(swhash->swevent_hlist,
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004643 lockdep_is_held(&event->ctx->lock));
4644 if (!hlist)
4645 return NULL;
4646
4647 return __find_swevent_head(hlist, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004648}
4649
4650static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
4651 u64 nr, int nmi,
4652 struct perf_sample_data *data,
4653 struct pt_regs *regs)
4654{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004655 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004656 struct perf_event *event;
4657 struct hlist_node *node;
4658 struct hlist_head *head;
4659
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004660 rcu_read_lock();
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004661 head = find_swevent_head_rcu(swhash, type, event_id);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004662 if (!head)
4663 goto end;
4664
4665 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
Li Zefan6fb29152009-10-15 11:21:42 +08004666 if (perf_swevent_match(event, type, event_id, data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004667 perf_swevent_event(event, nr, nmi, data, regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004668 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004669end:
4670 rcu_read_unlock();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004671}
4672
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004673int perf_swevent_get_recursion_context(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004674{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004675 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004676
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004677 return get_recursion_context(swhash->recursion);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004678}
Ingo Molnar645e8cc2009-11-22 12:20:19 +01004679EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004680
Jesper Juhlfa9f90b2010-11-28 21:39:34 +01004681inline void perf_swevent_put_recursion_context(int rctx)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004682{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004683 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02004684
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004685 put_recursion_context(swhash->recursion, rctx);
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +01004686}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004687
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004688void __perf_sw_event(u32 event_id, u64 nr, int nmi,
4689 struct pt_regs *regs, u64 addr)
4690{
Ingo Molnara4234bf2009-11-23 10:57:59 +01004691 struct perf_sample_data data;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004692 int rctx;
4693
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004694 preempt_disable_notrace();
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004695 rctx = perf_swevent_get_recursion_context();
4696 if (rctx < 0)
4697 return;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004698
Peter Zijlstradc1d6282010-03-03 15:55:04 +01004699 perf_sample_data_init(&data, addr);
Ingo Molnara4234bf2009-11-23 10:57:59 +01004700
4701 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
Peter Zijlstra4ed7c922009-11-23 11:37:29 +01004702
4703 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004704 preempt_enable_notrace();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004705}
4706
4707static void perf_swevent_read(struct perf_event *event)
4708{
4709}
4710
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004711static int perf_swevent_add(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004712{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004713 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004714 struct hw_perf_event *hwc = &event->hw;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004715 struct hlist_head *head;
4716
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01004717 if (is_sampling_event(event)) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004718 hwc->last_period = hwc->sample_period;
4719 perf_swevent_set_period(event);
4720 }
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004721
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004722 hwc->state = !(flags & PERF_EF_START);
4723
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004724 head = find_swevent_head(swhash, event);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004725 if (WARN_ON_ONCE(!head))
4726 return -EINVAL;
4727
4728 hlist_add_head_rcu(&event->hlist_entry, head);
4729
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004730 return 0;
4731}
4732
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004733static void perf_swevent_del(struct perf_event *event, int flags)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004734{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004735 hlist_del_rcu(&event->hlist_entry);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004736}
4737
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004738static void perf_swevent_start(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004739{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004740 event->hw.state = 0;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004741}
4742
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004743static void perf_swevent_stop(struct perf_event *event, int flags)
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004744{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004745 event->hw.state = PERF_HES_STOPPED;
Peter Zijlstrac6df8d52010-06-03 11:21:20 +02004746}
4747
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004748/* Deref the hlist from the update side */
4749static inline struct swevent_hlist *
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004750swevent_hlist_deref(struct swevent_htable *swhash)
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004751{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004752 return rcu_dereference_protected(swhash->swevent_hlist,
4753 lockdep_is_held(&swhash->hlist_mutex));
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004754}
4755
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004756static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
4757{
4758 struct swevent_hlist *hlist;
4759
4760 hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
4761 kfree(hlist);
4762}
4763
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004764static void swevent_hlist_release(struct swevent_htable *swhash)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004765{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004766 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004767
Frederic Weisbecker49f135e2010-05-20 10:17:46 +02004768 if (!hlist)
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004769 return;
4770
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004771 rcu_assign_pointer(swhash->swevent_hlist, NULL);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004772 call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
4773}
4774
4775static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
4776{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004777 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004778
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004779 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004780
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004781 if (!--swhash->hlist_refcount)
4782 swevent_hlist_release(swhash);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004783
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004784 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004785}
4786
4787static void swevent_hlist_put(struct perf_event *event)
4788{
4789 int cpu;
4790
4791 if (event->cpu != -1) {
4792 swevent_hlist_put_cpu(event, event->cpu);
4793 return;
4794 }
4795
4796 for_each_possible_cpu(cpu)
4797 swevent_hlist_put_cpu(event, cpu);
4798}
4799
4800static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
4801{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004802 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004803 int err = 0;
4804
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004805 mutex_lock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004806
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004807 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004808 struct swevent_hlist *hlist;
4809
4810 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
4811 if (!hlist) {
4812 err = -ENOMEM;
4813 goto exit;
4814 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004815 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004816 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004817 swhash->hlist_refcount++;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004818exit:
Peter Zijlstrab28ab832010-09-06 14:48:15 +02004819 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004820
4821 return err;
4822}
4823
4824static int swevent_hlist_get(struct perf_event *event)
4825{
4826 int err;
4827 int cpu, failed_cpu;
4828
4829 if (event->cpu != -1)
4830 return swevent_hlist_get_cpu(event, event->cpu);
4831
4832 get_online_cpus();
4833 for_each_possible_cpu(cpu) {
4834 err = swevent_hlist_get_cpu(event, cpu);
4835 if (err) {
4836 failed_cpu = cpu;
4837 goto fail;
4838 }
4839 }
4840 put_online_cpus();
4841
4842 return 0;
Peter Zijlstra9ed60602010-06-11 17:36:35 +02004843fail:
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004844 for_each_possible_cpu(cpu) {
4845 if (cpu == failed_cpu)
4846 break;
4847 swevent_hlist_put_cpu(event, cpu);
4848 }
4849
4850 put_online_cpus();
4851 return err;
4852}
4853
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004854atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004855
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004856static void sw_perf_event_destroy(struct perf_event *event)
4857{
4858 u64 event_id = event->attr.config;
4859
4860 WARN_ON(event->parent);
4861
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02004862 jump_label_dec(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004863 swevent_hlist_put(event);
4864}
4865
4866static int perf_swevent_init(struct perf_event *event)
4867{
4868 int event_id = event->attr.config;
4869
4870 if (event->attr.type != PERF_TYPE_SOFTWARE)
4871 return -ENOENT;
4872
4873 switch (event_id) {
4874 case PERF_COUNT_SW_CPU_CLOCK:
4875 case PERF_COUNT_SW_TASK_CLOCK:
4876 return -ENOENT;
4877
4878 default:
4879 break;
4880 }
4881
Dan Carpenterce677832010-10-24 21:50:42 +02004882 if (event_id >= PERF_COUNT_SW_MAX)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004883 return -ENOENT;
4884
4885 if (!event->parent) {
4886 int err;
4887
4888 err = swevent_hlist_get(event);
4889 if (err)
4890 return err;
4891
Peter Zijlstra7e54a5a2010-10-14 22:32:45 +02004892 jump_label_inc(&perf_swevent_enabled[event_id]);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004893 event->destroy = sw_perf_event_destroy;
4894 }
4895
4896 return 0;
4897}
4898
4899static struct pmu perf_swevent = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004900 .task_ctx_nr = perf_sw_context,
4901
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004902 .event_init = perf_swevent_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004903 .add = perf_swevent_add,
4904 .del = perf_swevent_del,
4905 .start = perf_swevent_start,
4906 .stop = perf_swevent_stop,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004907 .read = perf_swevent_read,
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004908};
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004909
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004910#ifdef CONFIG_EVENT_TRACING
4911
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004912static int perf_tp_filter_match(struct perf_event *event,
Frederic Weisbecker95476b62010-04-14 23:42:18 +02004913 struct perf_sample_data *data)
4914{
4915 void *record = data->raw->data;
4916
4917 if (likely(!event->filter) || filter_match_preds(event->filter, record))
4918 return 1;
4919 return 0;
4920}
4921
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004922static int perf_tp_event_match(struct perf_event *event,
4923 struct perf_sample_data *data,
4924 struct pt_regs *regs)
4925{
Peter Zijlstra580d6072010-05-20 20:54:31 +02004926 /*
4927 * All tracepoints are from kernel-space.
4928 */
4929 if (event->attr.exclude_kernel)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004930 return 0;
4931
4932 if (!perf_tp_filter_match(event, data))
4933 return 0;
4934
4935 return 1;
4936}
4937
4938void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004939 struct pt_regs *regs, struct hlist_head *head, int rctx)
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004940{
4941 struct perf_sample_data data;
4942 struct perf_event *event;
4943 struct hlist_node *node;
4944
4945 struct perf_raw_record raw = {
4946 .size = entry_size,
4947 .data = record,
4948 };
4949
4950 perf_sample_data_init(&data, addr);
4951 data.raw = &raw;
4952
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004953 hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
4954 if (perf_tp_event_match(event, &data, regs))
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004955 perf_swevent_event(event, count, 1, &data, regs);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004956 }
Peter Zijlstraecc55f82010-05-21 15:11:34 +02004957
4958 perf_swevent_put_recursion_context(rctx);
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004959}
4960EXPORT_SYMBOL_GPL(perf_tp_event);
4961
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004962static void tp_perf_event_destroy(struct perf_event *event)
4963{
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004964 perf_trace_destroy(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004965}
4966
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004967static int perf_tp_event_init(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004968{
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02004969 int err;
4970
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004971 if (event->attr.type != PERF_TYPE_TRACEPOINT)
4972 return -ENOENT;
4973
Peter Zijlstra1c024eca2010-05-19 14:02:22 +02004974 err = perf_trace_init(event);
4975 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004976 return err;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004977
4978 event->destroy = tp_perf_event_destroy;
4979
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004980 return 0;
4981}
4982
4983static struct pmu perf_tracepoint = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02004984 .task_ctx_nr = perf_sw_context,
4985
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004986 .event_init = perf_tp_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02004987 .add = perf_trace_add,
4988 .del = perf_trace_del,
4989 .start = perf_swevent_start,
4990 .stop = perf_swevent_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004991 .read = perf_swevent_read,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02004992};
4993
4994static inline void perf_tp_register(void)
4995{
Peter Zijlstra2e80a822010-11-17 23:17:36 +01004996 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02004997}
Li Zefan6fb29152009-10-15 11:21:42 +08004998
4999static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5000{
5001 char *filter_str;
5002 int ret;
5003
5004 if (event->attr.type != PERF_TYPE_TRACEPOINT)
5005 return -EINVAL;
5006
5007 filter_str = strndup_user(arg, PAGE_SIZE);
5008 if (IS_ERR(filter_str))
5009 return PTR_ERR(filter_str);
5010
5011 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
5012
5013 kfree(filter_str);
5014 return ret;
5015}
5016
5017static void perf_event_free_filter(struct perf_event *event)
5018{
5019 ftrace_profile_free_filter(event);
5020}
5021
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005022#else
Li Zefan6fb29152009-10-15 11:21:42 +08005023
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005024static inline void perf_tp_register(void)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005025{
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005026}
Li Zefan6fb29152009-10-15 11:21:42 +08005027
5028static int perf_event_set_filter(struct perf_event *event, void __user *arg)
5029{
5030 return -ENOENT;
5031}
5032
5033static void perf_event_free_filter(struct perf_event *event)
5034{
5035}
5036
Li Zefan07b139c2009-12-21 14:27:35 +08005037#endif /* CONFIG_EVENT_TRACING */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005038
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005039#ifdef CONFIG_HAVE_HW_BREAKPOINT
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005040void perf_bp_event(struct perf_event *bp, void *data)
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005041{
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005042 struct perf_sample_data sample;
5043 struct pt_regs *regs = data;
5044
Peter Zijlstradc1d6282010-03-03 15:55:04 +01005045 perf_sample_data_init(&sample, bp->attr.bp_addr);
Frederic Weisbeckerf5ffe022009-11-23 15:42:34 +01005046
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005047 if (!bp->hw.state && !perf_exclude_event(bp, regs))
5048 perf_swevent_event(bp, 1, 1, &sample, regs);
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +02005049}
5050#endif
5051
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005052/*
5053 * hrtimer based swevent callback
5054 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005055
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005056static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005057{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005058 enum hrtimer_restart ret = HRTIMER_RESTART;
5059 struct perf_sample_data data;
5060 struct pt_regs *regs;
5061 struct perf_event *event;
5062 u64 period;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005063
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005064 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
5065 event->pmu->read(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005066
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005067 perf_sample_data_init(&data, 0);
5068 data.period = event->hw.last_period;
5069 regs = get_irq_regs();
5070
5071 if (regs && !perf_exclude_event(event, regs)) {
5072 if (!(event->attr.exclude_idle && current->pid == 0))
5073 if (perf_event_overflow(event, 0, &data, regs))
5074 ret = HRTIMER_NORESTART;
5075 }
5076
5077 period = max_t(u64, 10000, event->hw.sample_period);
5078 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
5079
5080 return ret;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005081}
5082
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005083static void perf_swevent_start_hrtimer(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005084{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005085 struct hw_perf_event *hwc = &event->hw;
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005086 s64 period;
5087
5088 if (!is_sampling_event(event))
5089 return;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005090
5091 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
5092 hwc->hrtimer.function = perf_swevent_hrtimer;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005093
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005094 period = local64_read(&hwc->period_left);
5095 if (period) {
5096 if (period < 0)
5097 period = 10000;
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005098
Franck Bui-Huu5d508e82010-11-23 16:21:45 +01005099 local64_set(&hwc->period_left, 0);
5100 } else {
5101 period = max_t(u64, 10000, hwc->sample_period);
5102 }
5103 __hrtimer_start_range_ns(&hwc->hrtimer,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005104 ns_to_ktime(period), 0,
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02005105 HRTIMER_MODE_REL_PINNED, 0);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005106}
5107
5108static void perf_swevent_cancel_hrtimer(struct perf_event *event)
5109{
5110 struct hw_perf_event *hwc = &event->hw;
5111
Franck Bui-Huu6c7e5502010-11-23 16:21:43 +01005112 if (is_sampling_event(event)) {
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005113 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
Peter Zijlstrafa407f32010-06-24 12:35:12 +02005114 local64_set(&hwc->period_left, ktime_to_ns(remaining));
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005115
5116 hrtimer_cancel(&hwc->hrtimer);
5117 }
5118}
5119
5120/*
5121 * Software event: cpu wall time clock
5122 */
5123
5124static void cpu_clock_event_update(struct perf_event *event)
5125{
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005126 s64 prev;
5127 u64 now;
5128
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005129 now = local_clock();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005130 prev = local64_xchg(&event->hw.prev_count, now);
5131 local64_add(now - prev, &event->count);
5132}
5133
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005134static void cpu_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005135{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005136 local64_set(&event->hw.prev_count, local_clock());
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005137 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005138}
5139
5140static void cpu_clock_event_stop(struct perf_event *event, int flags)
5141{
5142 perf_swevent_cancel_hrtimer(event);
5143 cpu_clock_event_update(event);
5144}
5145
5146static int cpu_clock_event_add(struct perf_event *event, int flags)
5147{
5148 if (flags & PERF_EF_START)
5149 cpu_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005150
5151 return 0;
5152}
5153
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005154static void cpu_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005155{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005156 cpu_clock_event_stop(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005157}
5158
5159static void cpu_clock_event_read(struct perf_event *event)
5160{
5161 cpu_clock_event_update(event);
5162}
5163
5164static int cpu_clock_event_init(struct perf_event *event)
5165{
5166 if (event->attr.type != PERF_TYPE_SOFTWARE)
5167 return -ENOENT;
5168
5169 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
5170 return -ENOENT;
5171
5172 return 0;
5173}
5174
5175static struct pmu perf_cpu_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005176 .task_ctx_nr = perf_sw_context,
5177
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005178 .event_init = cpu_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005179 .add = cpu_clock_event_add,
5180 .del = cpu_clock_event_del,
5181 .start = cpu_clock_event_start,
5182 .stop = cpu_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005183 .read = cpu_clock_event_read,
5184};
5185
5186/*
5187 * Software event: task time clock
5188 */
5189
5190static void task_clock_event_update(struct perf_event *event, u64 now)
5191{
5192 u64 prev;
5193 s64 delta;
5194
5195 prev = local64_xchg(&event->hw.prev_count, now);
5196 delta = now - prev;
5197 local64_add(delta, &event->count);
5198}
5199
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005200static void task_clock_event_start(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005201{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005202 local64_set(&event->hw.prev_count, event->ctx->time);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005203 perf_swevent_start_hrtimer(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005204}
5205
5206static void task_clock_event_stop(struct perf_event *event, int flags)
5207{
5208 perf_swevent_cancel_hrtimer(event);
5209 task_clock_event_update(event, event->ctx->time);
5210}
5211
5212static int task_clock_event_add(struct perf_event *event, int flags)
5213{
5214 if (flags & PERF_EF_START)
5215 task_clock_event_start(event, flags);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005216
5217 return 0;
5218}
5219
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005220static void task_clock_event_del(struct perf_event *event, int flags)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005221{
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005222 task_clock_event_stop(event, PERF_EF_UPDATE);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005223}
5224
5225static void task_clock_event_read(struct perf_event *event)
5226{
5227 u64 time;
5228
5229 if (!in_nmi()) {
5230 update_context_time(event->ctx);
5231 time = event->ctx->time;
5232 } else {
5233 u64 now = perf_clock();
5234 u64 delta = now - event->ctx->timestamp;
5235 time = event->ctx->time + delta;
5236 }
5237
5238 task_clock_event_update(event, time);
5239}
5240
5241static int task_clock_event_init(struct perf_event *event)
5242{
5243 if (event->attr.type != PERF_TYPE_SOFTWARE)
5244 return -ENOENT;
5245
5246 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
5247 return -ENOENT;
5248
5249 return 0;
5250}
5251
5252static struct pmu perf_task_clock = {
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005253 .task_ctx_nr = perf_sw_context,
5254
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005255 .event_init = task_clock_event_init,
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +02005256 .add = task_clock_event_add,
5257 .del = task_clock_event_del,
5258 .start = task_clock_event_start,
5259 .stop = task_clock_event_stop,
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005260 .read = task_clock_event_read,
5261};
5262
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005263static void perf_pmu_nop_void(struct pmu *pmu)
5264{
5265}
5266
5267static int perf_pmu_nop_int(struct pmu *pmu)
5268{
5269 return 0;
5270}
5271
5272static void perf_pmu_start_txn(struct pmu *pmu)
5273{
5274 perf_pmu_disable(pmu);
5275}
5276
5277static int perf_pmu_commit_txn(struct pmu *pmu)
5278{
5279 perf_pmu_enable(pmu);
5280 return 0;
5281}
5282
5283static void perf_pmu_cancel_txn(struct pmu *pmu)
5284{
5285 perf_pmu_enable(pmu);
5286}
5287
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005288/*
5289 * Ensures all contexts with the same task_ctx_nr have the same
5290 * pmu_cpu_context too.
5291 */
5292static void *find_pmu_context(int ctxn)
5293{
5294 struct pmu *pmu;
5295
5296 if (ctxn < 0)
5297 return NULL;
5298
5299 list_for_each_entry(pmu, &pmus, entry) {
5300 if (pmu->task_ctx_nr == ctxn)
5301 return pmu->pmu_cpu_context;
5302 }
5303
5304 return NULL;
5305}
5306
Peter Zijlstra51676952010-12-07 14:18:20 +01005307static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005308{
Peter Zijlstra51676952010-12-07 14:18:20 +01005309 int cpu;
5310
5311 for_each_possible_cpu(cpu) {
5312 struct perf_cpu_context *cpuctx;
5313
5314 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
5315
5316 if (cpuctx->active_pmu == old_pmu)
5317 cpuctx->active_pmu = pmu;
5318 }
5319}
5320
5321static void free_pmu_context(struct pmu *pmu)
5322{
5323 struct pmu *i;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005324
5325 mutex_lock(&pmus_lock);
5326 /*
5327 * Like a real lame refcount.
5328 */
Peter Zijlstra51676952010-12-07 14:18:20 +01005329 list_for_each_entry(i, &pmus, entry) {
5330 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
5331 update_pmu_context(i, pmu);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005332 goto out;
Peter Zijlstra51676952010-12-07 14:18:20 +01005333 }
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005334 }
5335
Peter Zijlstra51676952010-12-07 14:18:20 +01005336 free_percpu(pmu->pmu_cpu_context);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005337out:
5338 mutex_unlock(&pmus_lock);
5339}
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005340static struct idr pmu_idr;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005341
Peter Zijlstraabe43402010-11-17 23:17:37 +01005342static ssize_t
5343type_show(struct device *dev, struct device_attribute *attr, char *page)
5344{
5345 struct pmu *pmu = dev_get_drvdata(dev);
5346
5347 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
5348}
5349
5350static struct device_attribute pmu_dev_attrs[] = {
5351 __ATTR_RO(type),
5352 __ATTR_NULL,
5353};
5354
5355static int pmu_bus_running;
5356static struct bus_type pmu_bus = {
5357 .name = "event_source",
5358 .dev_attrs = pmu_dev_attrs,
5359};
5360
5361static void pmu_dev_release(struct device *dev)
5362{
5363 kfree(dev);
5364}
5365
5366static int pmu_dev_alloc(struct pmu *pmu)
5367{
5368 int ret = -ENOMEM;
5369
5370 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
5371 if (!pmu->dev)
5372 goto out;
5373
5374 device_initialize(pmu->dev);
5375 ret = dev_set_name(pmu->dev, "%s", pmu->name);
5376 if (ret)
5377 goto free_dev;
5378
5379 dev_set_drvdata(pmu->dev, pmu);
5380 pmu->dev->bus = &pmu_bus;
5381 pmu->dev->release = pmu_dev_release;
5382 ret = device_add(pmu->dev);
5383 if (ret)
5384 goto free_dev;
5385
5386out:
5387 return ret;
5388
5389free_dev:
5390 put_device(pmu->dev);
5391 goto out;
5392}
5393
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005394static struct lock_class_key cpuctx_mutex;
5395
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005396int perf_pmu_register(struct pmu *pmu, char *name, int type)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005397{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005398 int cpu, ret;
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005399
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005400 mutex_lock(&pmus_lock);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005401 ret = -ENOMEM;
5402 pmu->pmu_disable_count = alloc_percpu(int);
5403 if (!pmu->pmu_disable_count)
5404 goto unlock;
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005405
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005406 pmu->type = -1;
5407 if (!name)
5408 goto skip_type;
5409 pmu->name = name;
5410
5411 if (type < 0) {
5412 int err = idr_pre_get(&pmu_idr, GFP_KERNEL);
5413 if (!err)
5414 goto free_pdc;
5415
5416 err = idr_get_new_above(&pmu_idr, pmu, PERF_TYPE_MAX, &type);
5417 if (err) {
5418 ret = err;
5419 goto free_pdc;
5420 }
5421 }
5422 pmu->type = type;
5423
Peter Zijlstraabe43402010-11-17 23:17:37 +01005424 if (pmu_bus_running) {
5425 ret = pmu_dev_alloc(pmu);
5426 if (ret)
5427 goto free_idr;
5428 }
5429
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005430skip_type:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005431 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
5432 if (pmu->pmu_cpu_context)
5433 goto got_cpu_context;
5434
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005435 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
5436 if (!pmu->pmu_cpu_context)
Peter Zijlstraabe43402010-11-17 23:17:37 +01005437 goto free_dev;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005438
5439 for_each_possible_cpu(cpu) {
5440 struct perf_cpu_context *cpuctx;
5441
5442 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
Peter Zijlstraeb184472010-09-07 15:55:13 +02005443 __perf_event_init_context(&cpuctx->ctx);
Peter Zijlstra547e9fd2011-01-19 12:51:39 +01005444 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005445 cpuctx->ctx.type = cpu_context;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005446 cpuctx->ctx.pmu = pmu;
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02005447 cpuctx->jiffies_interval = 1;
5448 INIT_LIST_HEAD(&cpuctx->rotation_list);
Peter Zijlstra51676952010-12-07 14:18:20 +01005449 cpuctx->active_pmu = pmu;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005450 }
5451
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02005452got_cpu_context:
Peter Zijlstraad5133b2010-06-15 12:22:39 +02005453 if (!pmu->start_txn) {
5454 if (pmu->pmu_enable) {
5455 /*
5456 * If we have pmu_enable/pmu_disable calls, install
5457 * transaction stubs that use that to try and batch
5458 * hardware accesses.
5459 */
5460 pmu->start_txn = perf_pmu_start_txn;
5461 pmu->commit_txn = perf_pmu_commit_txn;
5462 pmu->cancel_txn = perf_pmu_cancel_txn;
5463 } else {
5464 pmu->start_txn = perf_pmu_nop_void;
5465 pmu->commit_txn = perf_pmu_nop_int;
5466 pmu->cancel_txn = perf_pmu_nop_void;
5467 }
5468 }
5469
5470 if (!pmu->pmu_enable) {
5471 pmu->pmu_enable = perf_pmu_nop_void;
5472 pmu->pmu_disable = perf_pmu_nop_void;
5473 }
5474
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005475 list_add_rcu(&pmu->entry, &pmus);
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005476 ret = 0;
5477unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005478 mutex_unlock(&pmus_lock);
5479
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005480 return ret;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005481
Peter Zijlstraabe43402010-11-17 23:17:37 +01005482free_dev:
5483 device_del(pmu->dev);
5484 put_device(pmu->dev);
5485
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005486free_idr:
5487 if (pmu->type >= PERF_TYPE_MAX)
5488 idr_remove(&pmu_idr, pmu->type);
5489
Peter Zijlstra108b02c2010-09-06 14:32:03 +02005490free_pdc:
5491 free_percpu(pmu->pmu_disable_count);
5492 goto unlock;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005493}
5494
5495void perf_pmu_unregister(struct pmu *pmu)
5496{
5497 mutex_lock(&pmus_lock);
5498 list_del_rcu(&pmu->entry);
5499 mutex_unlock(&pmus_lock);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005500
5501 /*
Peter Zijlstracde8e882010-09-13 11:06:55 +02005502 * We dereference the pmu list under both SRCU and regular RCU, so
5503 * synchronize against both of those.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005504 */
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005505 synchronize_srcu(&pmus_srcu);
Peter Zijlstracde8e882010-09-13 11:06:55 +02005506 synchronize_rcu();
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005507
Peter Zijlstra33696fc2010-06-14 08:49:00 +02005508 free_percpu(pmu->pmu_disable_count);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005509 if (pmu->type >= PERF_TYPE_MAX)
5510 idr_remove(&pmu_idr, pmu->type);
Peter Zijlstraabe43402010-11-17 23:17:37 +01005511 device_del(pmu->dev);
5512 put_device(pmu->dev);
Peter Zijlstra51676952010-12-07 14:18:20 +01005513 free_pmu_context(pmu);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005514}
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005515
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005516struct pmu *perf_init_event(struct perf_event *event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005517{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005518 struct pmu *pmu = NULL;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005519 int idx;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005520
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005521 idx = srcu_read_lock(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01005522
5523 rcu_read_lock();
5524 pmu = idr_find(&pmu_idr, event->attr.type);
5525 rcu_read_unlock();
5526 if (pmu)
5527 goto unlock;
5528
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005529 list_for_each_entry_rcu(pmu, &pmus, entry) {
5530 int ret = pmu->event_init(event);
5531 if (!ret)
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005532 goto unlock;
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02005533
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005534 if (ret != -ENOENT) {
5535 pmu = ERR_PTR(ret);
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005536 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005537 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005538 }
Peter Zijlstrae5f4d332010-09-10 17:38:06 +02005539 pmu = ERR_PTR(-ENOENT);
5540unlock:
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005541 srcu_read_unlock(&pmus_srcu, idx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005542
5543 return pmu;
5544}
5545
5546/*
5547 * Allocate and initialize a event structure
5548 */
5549static struct perf_event *
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005550perf_event_alloc(struct perf_event_attr *attr, int cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005551 struct task_struct *task,
5552 struct perf_event *group_leader,
5553 struct perf_event *parent_event,
5554 perf_overflow_handler_t overflow_handler)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005555{
Peter Zijlstra51b0fe32010-06-11 13:35:57 +02005556 struct pmu *pmu;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005557 struct perf_event *event;
5558 struct hw_perf_event *hwc;
5559 long err;
5560
Oleg Nesterov66832eb2011-01-18 17:10:32 +01005561 if ((unsigned)cpu >= nr_cpu_ids) {
5562 if (!task || cpu != -1)
5563 return ERR_PTR(-EINVAL);
5564 }
5565
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005566 event = kzalloc(sizeof(*event), GFP_KERNEL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005567 if (!event)
5568 return ERR_PTR(-ENOMEM);
5569
5570 /*
5571 * Single events are their own group leaders, with an
5572 * empty sibling list:
5573 */
5574 if (!group_leader)
5575 group_leader = event;
5576
5577 mutex_init(&event->child_mutex);
5578 INIT_LIST_HEAD(&event->child_list);
5579
5580 INIT_LIST_HEAD(&event->group_entry);
5581 INIT_LIST_HEAD(&event->event_entry);
5582 INIT_LIST_HEAD(&event->sibling_list);
5583 init_waitqueue_head(&event->waitq);
Peter Zijlstrae360adb2010-10-14 14:01:34 +08005584 init_irq_work(&event->pending, perf_pending_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005585
5586 mutex_init(&event->mmap_mutex);
5587
5588 event->cpu = cpu;
5589 event->attr = *attr;
5590 event->group_leader = group_leader;
5591 event->pmu = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005592 event->oncpu = -1;
5593
5594 event->parent = parent_event;
5595
5596 event->ns = get_pid_ns(current->nsproxy->pid_ns);
5597 event->id = atomic64_inc_return(&perf_event_id);
5598
5599 event->state = PERF_EVENT_STATE_INACTIVE;
5600
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005601 if (task) {
5602 event->attach_state = PERF_ATTACH_TASK;
5603#ifdef CONFIG_HAVE_HW_BREAKPOINT
5604 /*
5605 * hw_breakpoint is a bit difficult here..
5606 */
5607 if (attr->type == PERF_TYPE_BREAKPOINT)
5608 event->hw.bp_target = task;
5609#endif
5610 }
5611
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005612 if (!overflow_handler && parent_event)
5613 overflow_handler = parent_event->overflow_handler;
Oleg Nesterov66832eb2011-01-18 17:10:32 +01005614
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01005615 event->overflow_handler = overflow_handler;
Frederic Weisbecker97eaf532009-10-18 15:33:50 +02005616
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005617 if (attr->disabled)
5618 event->state = PERF_EVENT_STATE_OFF;
5619
5620 pmu = NULL;
5621
5622 hwc = &event->hw;
5623 hwc->sample_period = attr->sample_period;
5624 if (attr->freq && attr->sample_freq)
5625 hwc->sample_period = 1;
5626 hwc->last_period = hwc->sample_period;
5627
Peter Zijlstrae7850592010-05-21 14:43:08 +02005628 local64_set(&hwc->period_left, hwc->sample_period);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005629
5630 /*
5631 * we currently do not support PERF_FORMAT_GROUP on inherited events
5632 */
5633 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
5634 goto done;
5635
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02005636 pmu = perf_init_event(event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005637
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005638done:
5639 err = 0;
5640 if (!pmu)
5641 err = -EINVAL;
5642 else if (IS_ERR(pmu))
5643 err = PTR_ERR(pmu);
5644
5645 if (err) {
5646 if (event->ns)
5647 put_pid_ns(event->ns);
5648 kfree(event);
5649 return ERR_PTR(err);
5650 }
5651
5652 event->pmu = pmu;
5653
5654 if (!event->parent) {
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02005655 if (event->attach_state & PERF_ATTACH_TASK)
5656 jump_label_inc(&perf_task_events);
Eric B Munson3af9e852010-05-18 15:30:49 +01005657 if (event->attr.mmap || event->attr.mmap_data)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005658 atomic_inc(&nr_mmap_events);
5659 if (event->attr.comm)
5660 atomic_inc(&nr_comm_events);
5661 if (event->attr.task)
5662 atomic_inc(&nr_task_events);
Frederic Weisbecker927c7a92010-07-01 16:20:36 +02005663 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
5664 err = get_callchain_buffers();
5665 if (err) {
5666 free_event(event);
5667 return ERR_PTR(err);
5668 }
5669 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005670 }
5671
5672 return event;
5673}
5674
5675static int perf_copy_attr(struct perf_event_attr __user *uattr,
5676 struct perf_event_attr *attr)
5677{
5678 u32 size;
5679 int ret;
5680
5681 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
5682 return -EFAULT;
5683
5684 /*
5685 * zero the full structure, so that a short copy will be nice.
5686 */
5687 memset(attr, 0, sizeof(*attr));
5688
5689 ret = get_user(size, &uattr->size);
5690 if (ret)
5691 return ret;
5692
5693 if (size > PAGE_SIZE) /* silly large */
5694 goto err_size;
5695
5696 if (!size) /* abi compat */
5697 size = PERF_ATTR_SIZE_VER0;
5698
5699 if (size < PERF_ATTR_SIZE_VER0)
5700 goto err_size;
5701
5702 /*
5703 * If we're handed a bigger struct than we know of,
5704 * ensure all the unknown bits are 0 - i.e. new
5705 * user-space does not rely on any kernel feature
5706 * extensions we dont know about yet.
5707 */
5708 if (size > sizeof(*attr)) {
5709 unsigned char __user *addr;
5710 unsigned char __user *end;
5711 unsigned char val;
5712
5713 addr = (void __user *)uattr + sizeof(*attr);
5714 end = (void __user *)uattr + size;
5715
5716 for (; addr < end; addr++) {
5717 ret = get_user(val, addr);
5718 if (ret)
5719 return ret;
5720 if (val)
5721 goto err_size;
5722 }
5723 size = sizeof(*attr);
5724 }
5725
5726 ret = copy_from_user(attr, uattr, size);
5727 if (ret)
5728 return -EFAULT;
5729
5730 /*
5731 * If the type exists, the corresponding creation will verify
5732 * the attr->config.
5733 */
5734 if (attr->type >= PERF_TYPE_MAX)
5735 return -EINVAL;
5736
Mahesh Salgaonkarcd757642010-01-30 10:25:18 +05305737 if (attr->__reserved_1)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005738 return -EINVAL;
5739
5740 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
5741 return -EINVAL;
5742
5743 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
5744 return -EINVAL;
5745
5746out:
5747 return ret;
5748
5749err_size:
5750 put_user(sizeof(*attr), &uattr->size);
5751 ret = -E2BIG;
5752 goto out;
5753}
5754
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005755static int
5756perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005757{
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005758 struct perf_buffer *buffer = NULL, *old_buffer = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005759 int ret = -EINVAL;
5760
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005761 if (!output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005762 goto set;
5763
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005764 /* don't allow circular references */
5765 if (event == output_event)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005766 goto out;
5767
Peter Zijlstra0f139302010-05-20 14:35:15 +02005768 /*
5769 * Don't allow cross-cpu buffers
5770 */
5771 if (output_event->cpu != event->cpu)
5772 goto out;
5773
5774 /*
5775 * If its not a per-cpu buffer, it must be the same task.
5776 */
5777 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
5778 goto out;
5779
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005780set:
5781 mutex_lock(&event->mmap_mutex);
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005782 /* Can't redirect output if we've got an active mmap() */
5783 if (atomic_read(&event->mmap_count))
5784 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005785
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005786 if (output_event) {
5787 /* get the buffer we want to redirect to */
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005788 buffer = perf_buffer_get(output_event);
5789 if (!buffer)
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005790 goto unlock;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005791 }
5792
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005793 old_buffer = event->buffer;
5794 rcu_assign_pointer(event->buffer, buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005795 ret = 0;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005796unlock:
5797 mutex_unlock(&event->mmap_mutex);
5798
Peter Zijlstraca5135e2010-05-28 19:33:23 +02005799 if (old_buffer)
5800 perf_buffer_put(old_buffer);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005801out:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005802 return ret;
5803}
5804
5805/**
5806 * sys_perf_event_open - open a performance event, associate it to a task/cpu
5807 *
5808 * @attr_uptr: event_id type attributes for monitoring/sampling
5809 * @pid: target pid
5810 * @cpu: target cpu
5811 * @group_fd: group leader event fd
5812 */
5813SYSCALL_DEFINE5(perf_event_open,
5814 struct perf_event_attr __user *, attr_uptr,
5815 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
5816{
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005817 struct perf_event *group_leader = NULL, *output_event = NULL;
5818 struct perf_event *event, *sibling;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005819 struct perf_event_attr attr;
5820 struct perf_event_context *ctx;
5821 struct file *event_file = NULL;
5822 struct file *group_file = NULL;
Matt Helsley38a81da2010-09-13 13:01:20 -07005823 struct task_struct *task = NULL;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005824 struct pmu *pmu;
Al Viroea635c62010-05-26 17:40:29 -04005825 int event_fd;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005826 int move_group = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005827 int fput_needed = 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005828 int err;
5829
5830 /* for future expandability... */
5831 if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
5832 return -EINVAL;
5833
5834 err = perf_copy_attr(attr_uptr, &attr);
5835 if (err)
5836 return err;
5837
5838 if (!attr.exclude_kernel) {
5839 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
5840 return -EACCES;
5841 }
5842
5843 if (attr.freq) {
5844 if (attr.sample_freq > sysctl_perf_event_sample_rate)
5845 return -EINVAL;
5846 }
5847
Al Viroea635c62010-05-26 17:40:29 -04005848 event_fd = get_unused_fd_flags(O_RDWR);
5849 if (event_fd < 0)
5850 return event_fd;
5851
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005852 if (group_fd != -1) {
5853 group_leader = perf_fget_light(group_fd, &fput_needed);
5854 if (IS_ERR(group_leader)) {
5855 err = PTR_ERR(group_leader);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005856 goto err_fd;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005857 }
5858 group_file = group_leader->filp;
5859 if (flags & PERF_FLAG_FD_OUTPUT)
5860 output_event = group_leader;
5861 if (flags & PERF_FLAG_FD_NO_GROUP)
5862 group_leader = NULL;
5863 }
5864
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005865 if (pid != -1) {
5866 task = find_lively_task_by_vpid(pid);
5867 if (IS_ERR(task)) {
5868 err = PTR_ERR(task);
5869 goto err_group_fd;
5870 }
5871 }
5872
Peter Zijlstrad580ff82010-10-14 17:43:23 +02005873 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005874 if (IS_ERR(event)) {
5875 err = PTR_ERR(event);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005876 goto err_task;
Stephane Eraniand14b12d2010-09-17 11:28:47 +02005877 }
5878
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005879 /*
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005880 * Special case software events and allow them to be part of
5881 * any hardware group.
5882 */
5883 pmu = event->pmu;
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005884
5885 if (group_leader &&
5886 (is_software_event(event) != is_software_event(group_leader))) {
5887 if (is_software_event(event)) {
5888 /*
5889 * If event and group_leader are not both a software
5890 * event, and event is, then group leader is not.
5891 *
5892 * Allow the addition of software events to !software
5893 * groups, this is safe because software events never
5894 * fail to schedule.
5895 */
5896 pmu = group_leader->pmu;
5897 } else if (is_software_event(group_leader) &&
5898 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
5899 /*
5900 * In case the group is a pure software group, and we
5901 * try to add a hardware event, move the whole group to
5902 * the hardware context.
5903 */
5904 move_group = 1;
5905 }
5906 }
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005907
5908 /*
5909 * Get the target context (task or percpu):
5910 */
Matt Helsley38a81da2010-09-13 13:01:20 -07005911 ctx = find_get_context(pmu, task, cpu);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005912 if (IS_ERR(ctx)) {
5913 err = PTR_ERR(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02005914 goto err_alloc;
Peter Zijlstra89a1e182010-09-07 17:34:50 +02005915 }
5916
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005917 /*
5918 * Look up the group leader (we will attach this event to it):
5919 */
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005920 if (group_leader) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005921 err = -EINVAL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005922
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005923 /*
5924 * Do not allow a recursive hierarchy (this new sibling
5925 * becoming part of another group-sibling):
5926 */
5927 if (group_leader->group_leader != group_leader)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005928 goto err_context;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005929 /*
5930 * Do not allow to attach to a group in a different
5931 * task or CPU context:
5932 */
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005933 if (move_group) {
5934 if (group_leader->ctx->type != ctx->type)
5935 goto err_context;
5936 } else {
5937 if (group_leader->ctx != ctx)
5938 goto err_context;
5939 }
5940
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005941 /*
5942 * Only a group leader can be exclusive or pinned
5943 */
5944 if (attr.exclusive || attr.pinned)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005945 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005946 }
5947
5948 if (output_event) {
5949 err = perf_event_set_output(event, output_event);
5950 if (err)
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005951 goto err_context;
Peter Zijlstraac9721f2010-05-27 12:54:41 +02005952 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005953
Al Viroea635c62010-05-26 17:40:29 -04005954 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
5955 if (IS_ERR(event_file)) {
5956 err = PTR_ERR(event_file);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02005957 goto err_context;
Al Viroea635c62010-05-26 17:40:29 -04005958 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005959
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005960 if (move_group) {
5961 struct perf_event_context *gctx = group_leader->ctx;
5962
5963 mutex_lock(&gctx->mutex);
5964 perf_event_remove_from_context(group_leader);
5965 list_for_each_entry(sibling, &group_leader->sibling_list,
5966 group_entry) {
5967 perf_event_remove_from_context(sibling);
5968 put_ctx(gctx);
5969 }
5970 mutex_unlock(&gctx->mutex);
5971 put_ctx(gctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005972 }
5973
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005974 event->filp = event_file;
5975 WARN_ON_ONCE(ctx->parent_ctx);
5976 mutex_lock(&ctx->mutex);
Peter Zijlstrab04243e2010-09-17 11:28:48 +02005977
5978 if (move_group) {
5979 perf_install_in_context(ctx, group_leader, cpu);
5980 get_ctx(ctx);
5981 list_for_each_entry(sibling, &group_leader->sibling_list,
5982 group_entry) {
5983 perf_install_in_context(ctx, sibling, cpu);
5984 get_ctx(ctx);
5985 }
5986 }
5987
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005988 perf_install_in_context(ctx, event, cpu);
5989 ++ctx->generation;
5990 mutex_unlock(&ctx->mutex);
5991
5992 event->owner = current;
Peter Zijlstra88821352010-11-09 19:01:43 +01005993
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005994 mutex_lock(&current->perf_event_mutex);
5995 list_add_tail(&event->owner_entry, &current->perf_event_list);
5996 mutex_unlock(&current->perf_event_mutex);
5997
Peter Zijlstra8a495422010-05-27 15:47:49 +02005998 /*
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02005999 * Precalculate sample_data sizes
6000 */
6001 perf_event__header_size(event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006002 perf_event__id_header_size(event);
Arnaldo Carvalho de Meloc320c7b2010-10-20 12:50:11 -02006003
6004 /*
Peter Zijlstra8a495422010-05-27 15:47:49 +02006005 * Drop the reference on the group_event after placing the
6006 * new event on the sibling_list. This ensures destruction
6007 * of the group leader will find the pointer to itself in
6008 * perf_group_detach().
6009 */
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006010 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006011 fd_install(event_fd, event_file);
6012 return event_fd;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006013
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006014err_context:
Al Viroea635c62010-05-26 17:40:29 -04006015 put_ctx(ctx);
Peter Zijlstrac6be5a52010-10-14 16:59:46 +02006016err_alloc:
6017 free_event(event);
Peter Zijlstrae7d0bc02010-10-14 16:54:51 +02006018err_task:
6019 if (task)
6020 put_task_struct(task);
Peter Zijlstra89a1e182010-09-07 17:34:50 +02006021err_group_fd:
6022 fput_light(group_file, fput_needed);
Al Viroea635c62010-05-26 17:40:29 -04006023err_fd:
6024 put_unused_fd(event_fd);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006025 return err;
6026}
6027
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006028/**
6029 * perf_event_create_kernel_counter
6030 *
6031 * @attr: attributes of the counter to create
6032 * @cpu: cpu in which the counter is bound
Matt Helsley38a81da2010-09-13 13:01:20 -07006033 * @task: task to profile (NULL for percpu)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006034 */
6035struct perf_event *
6036perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
Matt Helsley38a81da2010-09-13 13:01:20 -07006037 struct task_struct *task,
Frederic Weisbeckerb326e952009-12-05 09:44:31 +01006038 perf_overflow_handler_t overflow_handler)
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006039{
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006040 struct perf_event_context *ctx;
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006041 struct perf_event *event;
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006042 int err;
6043
6044 /*
6045 * Get the target context (task or percpu):
6046 */
6047
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006048 event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006049 if (IS_ERR(event)) {
6050 err = PTR_ERR(event);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006051 goto err;
6052 }
6053
Matt Helsley38a81da2010-09-13 13:01:20 -07006054 ctx = find_get_context(event->pmu, task, cpu);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006055 if (IS_ERR(ctx)) {
6056 err = PTR_ERR(ctx);
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006057 goto err_free;
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006058 }
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006059
6060 event->filp = NULL;
6061 WARN_ON_ONCE(ctx->parent_ctx);
6062 mutex_lock(&ctx->mutex);
6063 perf_install_in_context(ctx, event, cpu);
6064 ++ctx->generation;
6065 mutex_unlock(&ctx->mutex);
6066
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006067 return event;
6068
Peter Zijlstrac3f00c72010-08-18 14:37:15 +02006069err_free:
6070 free_event(event);
6071err:
Frederic Weisbeckerc6567f62009-11-26 05:35:41 +01006072 return ERR_PTR(err);
Arjan van de Venfb0459d2009-09-25 12:25:56 +02006073}
6074EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
6075
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006076static void sync_child_event(struct perf_event *child_event,
6077 struct task_struct *child)
6078{
6079 struct perf_event *parent_event = child_event->parent;
6080 u64 child_val;
6081
6082 if (child_event->attr.inherit_stat)
6083 perf_event_read_event(child_event, child);
6084
Peter Zijlstrab5e58792010-05-21 14:43:12 +02006085 child_val = perf_event_count(child_event);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006086
6087 /*
6088 * Add back the child's count to the parent's count:
6089 */
Peter Zijlstraa6e6dea2010-05-21 14:27:58 +02006090 atomic64_add(child_val, &parent_event->child_count);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006091 atomic64_add(child_event->total_time_enabled,
6092 &parent_event->child_total_time_enabled);
6093 atomic64_add(child_event->total_time_running,
6094 &parent_event->child_total_time_running);
6095
6096 /*
6097 * Remove this event from the parent's list
6098 */
6099 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6100 mutex_lock(&parent_event->child_mutex);
6101 list_del_init(&child_event->child_list);
6102 mutex_unlock(&parent_event->child_mutex);
6103
6104 /*
6105 * Release the parent event, if this was the last
6106 * reference to it.
6107 */
6108 fput(parent_event->filp);
6109}
6110
6111static void
6112__perf_event_exit_task(struct perf_event *child_event,
6113 struct perf_event_context *child_ctx,
6114 struct task_struct *child)
6115{
6116 struct perf_event *parent_event;
6117
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006118 perf_event_remove_from_context(child_event);
6119
6120 parent_event = child_event->parent;
6121 /*
6122 * It can happen that parent exits first, and has events
6123 * that are still around due to the child reference. These
6124 * events need to be zapped - but otherwise linger.
6125 */
6126 if (parent_event) {
6127 sync_child_event(child_event, child);
6128 free_event(child_event);
6129 }
6130}
6131
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006132static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006133{
6134 struct perf_event *child_event, *tmp;
6135 struct perf_event_context *child_ctx;
6136 unsigned long flags;
6137
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006138 if (likely(!child->perf_event_ctxp[ctxn])) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006139 perf_event_task(child, NULL, 0);
6140 return;
6141 }
6142
6143 local_irq_save(flags);
6144 /*
6145 * We can't reschedule here because interrupts are disabled,
6146 * and either child is current or it is a task that can't be
6147 * scheduled, so we are now safe from rescheduling changing
6148 * our context.
6149 */
Oleg Nesterov806839b2011-01-21 18:45:47 +01006150 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
Peter Zijlstra82cd6de2010-10-14 17:57:23 +02006151 task_ctx_sched_out(child_ctx, EVENT_ALL);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006152
6153 /*
6154 * Take the context lock here so that if find_get_context is
6155 * reading child->perf_event_ctxp, we wait until it has
6156 * incremented the context's refcount before we do put_ctx below.
6157 */
Thomas Gleixnere625cce2009-11-17 18:02:06 +01006158 raw_spin_lock(&child_ctx->lock);
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006159 child->perf_event_ctxp[ctxn] = NULL;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006160 /*
6161 * If this context is a clone; unclone it so it can't get
6162 * swapped to another process while we're removing all
6163 * the events from it.
6164 */
6165 unclone_ctx(child_ctx);
Peter Zijlstra5e942bb2009-11-23 11:37:26 +01006166 update_context_time(child_ctx);
Thomas Gleixnere625cce2009-11-17 18:02:06 +01006167 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006168
6169 /*
6170 * Report the task dead after unscheduling the events so that we
6171 * won't get any samples after PERF_RECORD_EXIT. We can however still
6172 * get a few PERF_RECORD_READ events.
6173 */
6174 perf_event_task(child, child_ctx, 0);
6175
6176 /*
6177 * We can recurse on the same lock type through:
6178 *
6179 * __perf_event_exit_task()
6180 * sync_child_event()
6181 * fput(parent_event->filp)
6182 * perf_release()
6183 * mutex_lock(&ctx->mutex)
6184 *
6185 * But since its the parent context it won't be the same instance.
6186 */
Peter Zijlstraa0507c82010-05-06 15:42:53 +02006187 mutex_lock(&child_ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006188
6189again:
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006190 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
6191 group_entry)
6192 __perf_event_exit_task(child_event, child_ctx, child);
6193
6194 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006195 group_entry)
6196 __perf_event_exit_task(child_event, child_ctx, child);
6197
6198 /*
6199 * If the last event was a group event, it will have appended all
6200 * its siblings to the list, but we obtained 'tmp' before that which
6201 * will still point to the list head terminating the iteration.
6202 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006203 if (!list_empty(&child_ctx->pinned_groups) ||
6204 !list_empty(&child_ctx->flexible_groups))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006205 goto again;
6206
6207 mutex_unlock(&child_ctx->mutex);
6208
6209 put_ctx(child_ctx);
6210}
6211
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006212/*
6213 * When a child task exits, feed back event values to parent events.
6214 */
6215void perf_event_exit_task(struct task_struct *child)
6216{
Peter Zijlstra88821352010-11-09 19:01:43 +01006217 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006218 int ctxn;
6219
Peter Zijlstra88821352010-11-09 19:01:43 +01006220 mutex_lock(&child->perf_event_mutex);
6221 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
6222 owner_entry) {
6223 list_del_init(&event->owner_entry);
6224
6225 /*
6226 * Ensure the list deletion is visible before we clear
6227 * the owner, closes a race against perf_release() where
6228 * we need to serialize on the owner->perf_event_mutex.
6229 */
6230 smp_wmb();
6231 event->owner = NULL;
6232 }
6233 mutex_unlock(&child->perf_event_mutex);
6234
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006235 for_each_task_context_nr(ctxn)
6236 perf_event_exit_task_context(child, ctxn);
6237}
6238
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006239static void perf_free_event(struct perf_event *event,
6240 struct perf_event_context *ctx)
6241{
6242 struct perf_event *parent = event->parent;
6243
6244 if (WARN_ON_ONCE(!parent))
6245 return;
6246
6247 mutex_lock(&parent->child_mutex);
6248 list_del_init(&event->child_list);
6249 mutex_unlock(&parent->child_mutex);
6250
6251 fput(parent->filp);
6252
Peter Zijlstra8a495422010-05-27 15:47:49 +02006253 perf_group_detach(event);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006254 list_del_event(event, ctx);
6255 free_event(event);
6256}
6257
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006258/*
6259 * free an unexposed, unused context as created by inheritance by
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006260 * perf_event_init_task below, used by fork() in case of fail.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006261 */
6262void perf_event_free_task(struct task_struct *task)
6263{
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006264 struct perf_event_context *ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006265 struct perf_event *event, *tmp;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006266 int ctxn;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006267
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006268 for_each_task_context_nr(ctxn) {
6269 ctx = task->perf_event_ctxp[ctxn];
6270 if (!ctx)
6271 continue;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006272
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006273 mutex_lock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006274again:
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006275 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
6276 group_entry)
6277 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006278
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006279 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
6280 group_entry)
6281 perf_free_event(event, ctx);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006282
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006283 if (!list_empty(&ctx->pinned_groups) ||
6284 !list_empty(&ctx->flexible_groups))
6285 goto again;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006286
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006287 mutex_unlock(&ctx->mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006288
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006289 put_ctx(ctx);
6290 }
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006291}
6292
Peter Zijlstra4e231c72010-09-09 21:01:59 +02006293void perf_event_delayed_put(struct task_struct *task)
6294{
6295 int ctxn;
6296
6297 for_each_task_context_nr(ctxn)
6298 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
6299}
6300
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006301/*
6302 * inherit a event from parent task to child task:
6303 */
6304static struct perf_event *
6305inherit_event(struct perf_event *parent_event,
6306 struct task_struct *parent,
6307 struct perf_event_context *parent_ctx,
6308 struct task_struct *child,
6309 struct perf_event *group_leader,
6310 struct perf_event_context *child_ctx)
6311{
6312 struct perf_event *child_event;
Peter Zijlstracee010e2010-09-10 12:51:54 +02006313 unsigned long flags;
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006314
6315 /*
6316 * Instead of creating recursive hierarchies of events,
6317 * we link inherited events back to the original parent,
6318 * which has a filp for sure, which we use as the reference
6319 * count:
6320 */
6321 if (parent_event->parent)
6322 parent_event = parent_event->parent;
6323
6324 child_event = perf_event_alloc(&parent_event->attr,
6325 parent_event->cpu,
Peter Zijlstrad580ff82010-10-14 17:43:23 +02006326 child,
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006327 group_leader, parent_event,
6328 NULL);
6329 if (IS_ERR(child_event))
6330 return child_event;
6331 get_ctx(child_ctx);
6332
6333 /*
6334 * Make the child state follow the state of the parent event,
6335 * not its attr.disabled bit. We hold the parent's mutex,
6336 * so we won't race with perf_event_{en, dis}able_family.
6337 */
6338 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
6339 child_event->state = PERF_EVENT_STATE_INACTIVE;
6340 else
6341 child_event->state = PERF_EVENT_STATE_OFF;
6342
6343 if (parent_event->attr.freq) {
6344 u64 sample_period = parent_event->hw.sample_period;
6345 struct hw_perf_event *hwc = &child_event->hw;
6346
6347 hwc->sample_period = sample_period;
6348 hwc->last_period = sample_period;
6349
6350 local64_set(&hwc->period_left, sample_period);
6351 }
6352
6353 child_event->ctx = child_ctx;
6354 child_event->overflow_handler = parent_event->overflow_handler;
6355
6356 /*
Thomas Gleixner614b6782010-12-03 16:24:32 -02006357 * Precalculate sample_data sizes
6358 */
6359 perf_event__header_size(child_event);
Arnaldo Carvalho de Melo6844c092010-12-03 16:36:35 -02006360 perf_event__id_header_size(child_event);
Thomas Gleixner614b6782010-12-03 16:24:32 -02006361
6362 /*
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006363 * Link it up in the child's context:
6364 */
Peter Zijlstracee010e2010-09-10 12:51:54 +02006365 raw_spin_lock_irqsave(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006366 add_event_to_ctx(child_event, child_ctx);
Peter Zijlstracee010e2010-09-10 12:51:54 +02006367 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
Peter Zijlstra97dee4f2010-09-07 15:35:33 +02006368
6369 /*
6370 * Get a reference to the parent filp - we will fput it
6371 * when the child event exits. This is safe to do because
6372 * we are in the parent and we know that the filp still
6373 * exists and has a nonzero count:
6374 */
6375 atomic_long_inc(&parent_event->filp->f_count);
6376
6377 /*
6378 * Link this into the parent event's child list
6379 */
6380 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
6381 mutex_lock(&parent_event->child_mutex);
6382 list_add_tail(&child_event->child_list, &parent_event->child_list);
6383 mutex_unlock(&parent_event->child_mutex);
6384
6385 return child_event;
6386}
6387
6388static int inherit_group(struct perf_event *parent_event,
6389 struct task_struct *parent,
6390 struct perf_event_context *parent_ctx,
6391 struct task_struct *child,
6392 struct perf_event_context *child_ctx)
6393{
6394 struct perf_event *leader;
6395 struct perf_event *sub;
6396 struct perf_event *child_ctr;
6397
6398 leader = inherit_event(parent_event, parent, parent_ctx,
6399 child, NULL, child_ctx);
6400 if (IS_ERR(leader))
6401 return PTR_ERR(leader);
6402 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
6403 child_ctr = inherit_event(sub, parent, parent_ctx,
6404 child, leader, child_ctx);
6405 if (IS_ERR(child_ctr))
6406 return PTR_ERR(child_ctr);
6407 }
6408 return 0;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006409}
6410
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006411static int
6412inherit_task_group(struct perf_event *event, struct task_struct *parent,
6413 struct perf_event_context *parent_ctx,
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006414 struct task_struct *child, int ctxn,
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006415 int *inherited_all)
6416{
6417 int ret;
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006418 struct perf_event_context *child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006419
6420 if (!event->attr.inherit) {
6421 *inherited_all = 0;
6422 return 0;
6423 }
6424
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006425 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006426 if (!child_ctx) {
6427 /*
6428 * This is executed from the parent task context, so
6429 * inherit events that have been marked for cloning.
6430 * First allocate and initialize a context for the
6431 * child.
6432 */
6433
Peter Zijlstraeb184472010-09-07 15:55:13 +02006434 child_ctx = alloc_perf_context(event->pmu, child);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006435 if (!child_ctx)
6436 return -ENOMEM;
6437
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006438 child->perf_event_ctxp[ctxn] = child_ctx;
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006439 }
6440
6441 ret = inherit_group(event, parent, parent_ctx,
6442 child, child_ctx);
6443
6444 if (ret)
6445 *inherited_all = 0;
6446
6447 return ret;
6448}
6449
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006450/*
6451 * Initialize the perf_event context in task_struct
6452 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006453int perf_event_init_context(struct task_struct *child, int ctxn)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006454{
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006455 struct perf_event_context *child_ctx, *parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006456 struct perf_event_context *cloned_ctx;
6457 struct perf_event *event;
6458 struct task_struct *parent = current;
6459 int inherited_all = 1;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006460 unsigned long flags;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006461 int ret = 0;
6462
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006463 if (likely(!parent->perf_event_ctxp[ctxn]))
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006464 return 0;
6465
6466 /*
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006467 * If the parent's context is a clone, pin it so it won't get
6468 * swapped under us.
6469 */
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006470 parent_ctx = perf_pin_task_context(parent, ctxn);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006471
6472 /*
6473 * No need to check if parent_ctx != NULL here; since we saw
6474 * it non-NULL earlier, the only reason for it to become NULL
6475 * is if we exit, and since we're currently in the middle of
6476 * a fork we can't be exiting at the same time.
6477 */
6478
6479 /*
6480 * Lock the parent list. No need to lock the child - not PID
6481 * hashed yet and not running, so nobody can access it.
6482 */
6483 mutex_lock(&parent_ctx->mutex);
6484
6485 /*
6486 * We dont have to disable NMIs - we are only looking at
6487 * the list, not manipulating it:
6488 */
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006489 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006490 ret = inherit_task_group(event, parent, parent_ctx,
6491 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006492 if (ret)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006493 break;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006494 }
6495
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006496 /*
6497 * We can't hold ctx->lock when iterating the ->flexible_group list due
6498 * to allocations, but we need to prevent rotation because
6499 * rotate_ctx() will change the list from interrupt context.
6500 */
6501 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6502 parent_ctx->rotate_disable = 1;
6503 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
6504
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006505 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006506 ret = inherit_task_group(event, parent, parent_ctx,
6507 child, ctxn, &inherited_all);
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006508 if (ret)
6509 break;
6510 }
6511
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006512 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
6513 parent_ctx->rotate_disable = 0;
Thomas Gleixnerdddd3372010-11-24 10:05:55 +01006514
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006515 child_ctx = child->perf_event_ctxp[ctxn];
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006516
Peter Zijlstra05cbaa22009-12-30 16:00:35 +01006517 if (child_ctx && inherited_all) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006518 /*
6519 * Mark the child context as a clone of the parent
6520 * context, or of whatever the parent is a clone of.
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01006521 *
6522 * Note that if the parent is a clone, the holding of
6523 * parent_ctx->lock avoids it from being uncloned.
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006524 */
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01006525 cloned_ctx = parent_ctx->parent_ctx;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006526 if (cloned_ctx) {
6527 child_ctx->parent_ctx = cloned_ctx;
6528 child_ctx->parent_gen = parent_ctx->parent_gen;
6529 } else {
6530 child_ctx->parent_ctx = parent_ctx;
6531 child_ctx->parent_gen = parent_ctx->generation;
6532 }
6533 get_ctx(child_ctx->parent_ctx);
6534 }
6535
Peter Zijlstrac5ed5142011-01-17 13:45:37 +01006536 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006537 mutex_unlock(&parent_ctx->mutex);
6538
6539 perf_unpin_context(parent_ctx);
6540
6541 return ret;
6542}
6543
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006544/*
6545 * Initialize the perf_event context in task_struct
6546 */
6547int perf_event_init_task(struct task_struct *child)
6548{
6549 int ctxn, ret;
6550
Oleg Nesterov8550d7c2011-01-19 19:22:28 +01006551 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
6552 mutex_init(&child->perf_event_mutex);
6553 INIT_LIST_HEAD(&child->perf_event_list);
6554
Peter Zijlstra8dc85d52010-09-02 16:50:03 +02006555 for_each_task_context_nr(ctxn) {
6556 ret = perf_event_init_context(child, ctxn);
6557 if (ret)
6558 return ret;
6559 }
6560
6561 return 0;
6562}
6563
Paul Mackerras220b1402010-03-10 20:45:52 +11006564static void __init perf_event_init_all_cpus(void)
6565{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006566 struct swevent_htable *swhash;
Paul Mackerras220b1402010-03-10 20:45:52 +11006567 int cpu;
Paul Mackerras220b1402010-03-10 20:45:52 +11006568
6569 for_each_possible_cpu(cpu) {
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006570 swhash = &per_cpu(swevent_htable, cpu);
6571 mutex_init(&swhash->hlist_mutex);
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006572 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
Paul Mackerras220b1402010-03-10 20:45:52 +11006573 }
6574}
6575
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006576static void __cpuinit perf_event_init_cpu(int cpu)
6577{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006578 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006579
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006580 mutex_lock(&swhash->hlist_mutex);
6581 if (swhash->hlist_refcount > 0) {
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006582 struct swevent_hlist *hlist;
6583
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006584 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
6585 WARN_ON(!hlist);
6586 rcu_assign_pointer(swhash->swevent_hlist, hlist);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006587 }
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006588 mutex_unlock(&swhash->hlist_mutex);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006589}
6590
Peter Zijlstrac2774432010-12-08 15:29:02 +01006591#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006592static void perf_pmu_rotate_stop(struct pmu *pmu)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006593{
Peter Zijlstrae9d2b062010-09-17 11:28:50 +02006594 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
6595
6596 WARN_ON(!irqs_disabled());
6597
6598 list_del_init(&cpuctx->rotation_list);
6599}
6600
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006601static void __perf_event_exit_context(void *__info)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006602{
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006603 struct perf_event_context *ctx = __info;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006604 struct perf_event *event, *tmp;
6605
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006606 perf_pmu_rotate_stop(ctx->pmu);
Peter Zijlstrab5ab4cd2010-09-06 16:32:21 +02006607
Frederic Weisbecker889ff012010-01-09 20:04:47 +01006608 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
6609 __perf_event_remove_from_context(event);
6610 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006611 __perf_event_remove_from_context(event);
6612}
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006613
6614static void perf_event_exit_cpu_context(int cpu)
6615{
6616 struct perf_event_context *ctx;
6617 struct pmu *pmu;
6618 int idx;
6619
6620 idx = srcu_read_lock(&pmus_srcu);
6621 list_for_each_entry_rcu(pmu, &pmus, entry) {
Peter Zijlstra917bdd12010-09-17 11:28:49 +02006622 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006623
6624 mutex_lock(&ctx->mutex);
6625 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
6626 mutex_unlock(&ctx->mutex);
6627 }
6628 srcu_read_unlock(&pmus_srcu, idx);
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006629}
6630
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006631static void perf_event_exit_cpu(int cpu)
6632{
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006633 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006634
Peter Zijlstrab28ab832010-09-06 14:48:15 +02006635 mutex_lock(&swhash->hlist_mutex);
6636 swevent_hlist_release(swhash);
6637 mutex_unlock(&swhash->hlist_mutex);
Frederic Weisbecker76e1d902010-04-05 15:35:57 +02006638
Peter Zijlstra108b02c2010-09-06 14:32:03 +02006639 perf_event_exit_cpu_context(cpu);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006640}
6641#else
6642static inline void perf_event_exit_cpu(int cpu) { }
6643#endif
6644
Peter Zijlstrac2774432010-12-08 15:29:02 +01006645static int
6646perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
6647{
6648 int cpu;
6649
6650 for_each_online_cpu(cpu)
6651 perf_event_exit_cpu(cpu);
6652
6653 return NOTIFY_OK;
6654}
6655
6656/*
6657 * Run the perf reboot notifier at the very last possible moment so that
6658 * the generic watchdog code runs as long as possible.
6659 */
6660static struct notifier_block perf_reboot_notifier = {
6661 .notifier_call = perf_reboot,
6662 .priority = INT_MIN,
6663};
6664
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006665static int __cpuinit
6666perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
6667{
6668 unsigned int cpu = (long)hcpu;
6669
Peter Zijlstra5e116372010-06-11 13:35:08 +02006670 switch (action & ~CPU_TASKS_FROZEN) {
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006671
6672 case CPU_UP_PREPARE:
Peter Zijlstra5e116372010-06-11 13:35:08 +02006673 case CPU_DOWN_FAILED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006674 perf_event_init_cpu(cpu);
6675 break;
6676
Peter Zijlstra5e116372010-06-11 13:35:08 +02006677 case CPU_UP_CANCELED:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006678 case CPU_DOWN_PREPARE:
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006679 perf_event_exit_cpu(cpu);
6680 break;
6681
6682 default:
6683 break;
6684 }
6685
6686 return NOTIFY_OK;
6687}
6688
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006689void __init perf_event_init(void)
6690{
Jason Wessel3c502e72010-11-04 17:33:01 -05006691 int ret;
6692
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006693 idr_init(&pmu_idr);
6694
Paul Mackerras220b1402010-03-10 20:45:52 +11006695 perf_event_init_all_cpus();
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006696 init_srcu_struct(&pmus_srcu);
Peter Zijlstra2e80a822010-11-17 23:17:36 +01006697 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
6698 perf_pmu_register(&perf_cpu_clock, NULL, -1);
6699 perf_pmu_register(&perf_task_clock, NULL, -1);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +02006700 perf_tp_register();
6701 perf_cpu_notifier(perf_cpu_notify);
Peter Zijlstrac2774432010-12-08 15:29:02 +01006702 register_reboot_notifier(&perf_reboot_notifier);
Jason Wessel3c502e72010-11-04 17:33:01 -05006703
6704 ret = init_hw_breakpoint();
6705 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
Ingo Molnarcdd6c482009-09-21 12:02:48 +02006706}
Peter Zijlstraabe43402010-11-17 23:17:37 +01006707
6708static int __init perf_event_sysfs_init(void)
6709{
6710 struct pmu *pmu;
6711 int ret;
6712
6713 mutex_lock(&pmus_lock);
6714
6715 ret = bus_register(&pmu_bus);
6716 if (ret)
6717 goto unlock;
6718
6719 list_for_each_entry(pmu, &pmus, entry) {
6720 if (!pmu->name || pmu->type < 0)
6721 continue;
6722
6723 ret = pmu_dev_alloc(pmu);
6724 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
6725 }
6726 pmu_bus_running = 1;
6727 ret = 0;
6728
6729unlock:
6730 mutex_unlock(&pmus_lock);
6731
6732 return ret;
6733}
6734device_initcall(perf_event_sysfs_init);