perf/core: Fix pmu::filter_match for SW-led groups
[deliverable/linux.git] / kernel / events / core.c
CommitLineData
0793a61d 1/*
57c0c15b 2 * Performance events core code:
0793a61d 3 *
98144511 4 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
e7e7ee2e 5 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
90eec103 6 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra
d36b6910 7 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
7b732a75 8 *
57c0c15b 9 * For licensing details see kernel-base/COPYING
0793a61d
TG
10 */
11
12#include <linux/fs.h>
b9cacc7b 13#include <linux/mm.h>
0793a61d
TG
14#include <linux/cpu.h>
15#include <linux/smp.h>
2e80a82a 16#include <linux/idr.h>
04289bb9 17#include <linux/file.h>
0793a61d 18#include <linux/poll.h>
5a0e3ad6 19#include <linux/slab.h>
76e1d904 20#include <linux/hash.h>
12351ef8 21#include <linux/tick.h>
0793a61d 22#include <linux/sysfs.h>
22a4f650 23#include <linux/dcache.h>
0793a61d 24#include <linux/percpu.h>
22a4f650 25#include <linux/ptrace.h>
c277443c 26#include <linux/reboot.h>
b9cacc7b 27#include <linux/vmstat.h>
abe43400 28#include <linux/device.h>
6e5fdeed 29#include <linux/export.h>
906010b2 30#include <linux/vmalloc.h>
b9cacc7b
PZ
31#include <linux/hardirq.h>
32#include <linux/rculist.h>
0793a61d
TG
33#include <linux/uaccess.h>
34#include <linux/syscalls.h>
35#include <linux/anon_inodes.h>
aa9c4c0f 36#include <linux/kernel_stat.h>
39bed6cb 37#include <linux/cgroup.h>
cdd6c482 38#include <linux/perf_event.h>
af658dca 39#include <linux/trace_events.h>
3c502e7a 40#include <linux/hw_breakpoint.h>
c5ebcedb 41#include <linux/mm_types.h>
c464c76e 42#include <linux/module.h>
f972eb63 43#include <linux/mman.h>
b3f20785 44#include <linux/compat.h>
2541517c
AS
45#include <linux/bpf.h>
46#include <linux/filter.h>
375637bc
AS
47#include <linux/namei.h>
48#include <linux/parser.h>
0793a61d 49
76369139
FW
50#include "internal.h"
51
4e193bd4
TB
52#include <asm/irq_regs.h>
53
272325c4
PZ
54typedef int (*remote_function_f)(void *);
55
fe4b04fa 56struct remote_function_call {
e7e7ee2e 57 struct task_struct *p;
272325c4 58 remote_function_f func;
e7e7ee2e
IM
59 void *info;
60 int ret;
fe4b04fa
PZ
61};
62
63static void remote_function(void *data)
64{
65 struct remote_function_call *tfc = data;
66 struct task_struct *p = tfc->p;
67
68 if (p) {
0da4cf3e
PZ
69 /* -EAGAIN */
70 if (task_cpu(p) != smp_processor_id())
71 return;
72
73 /*
74 * Now that we're on right CPU with IRQs disabled, we can test
75 * if we hit the right task without races.
76 */
77
78 tfc->ret = -ESRCH; /* No such (running) process */
79 if (p != current)
fe4b04fa
PZ
80 return;
81 }
82
83 tfc->ret = tfc->func(tfc->info);
84}
85
86/**
87 * task_function_call - call a function on the cpu on which a task runs
88 * @p: the task to evaluate
89 * @func: the function to be called
90 * @info: the function call argument
91 *
92 * Calls the function @func when the task is currently running. This might
93 * be on the current CPU, which just calls the function directly
94 *
95 * returns: @func return value, or
96 * -ESRCH - when the process isn't running
97 * -EAGAIN - when the process moved away
98 */
99static int
272325c4 100task_function_call(struct task_struct *p, remote_function_f func, void *info)
fe4b04fa
PZ
101{
102 struct remote_function_call data = {
e7e7ee2e
IM
103 .p = p,
104 .func = func,
105 .info = info,
0da4cf3e 106 .ret = -EAGAIN,
fe4b04fa 107 };
0da4cf3e 108 int ret;
fe4b04fa 109
0da4cf3e
PZ
110 do {
111 ret = smp_call_function_single(task_cpu(p), remote_function, &data, 1);
112 if (!ret)
113 ret = data.ret;
114 } while (ret == -EAGAIN);
fe4b04fa 115
0da4cf3e 116 return ret;
fe4b04fa
PZ
117}
118
119/**
120 * cpu_function_call - call a function on the cpu
121 * @func: the function to be called
122 * @info: the function call argument
123 *
124 * Calls the function @func on the remote cpu.
125 *
126 * returns: @func return value or -ENXIO when the cpu is offline
127 */
272325c4 128static int cpu_function_call(int cpu, remote_function_f func, void *info)
fe4b04fa
PZ
129{
130 struct remote_function_call data = {
e7e7ee2e
IM
131 .p = NULL,
132 .func = func,
133 .info = info,
134 .ret = -ENXIO, /* No such CPU */
fe4b04fa
PZ
135 };
136
137 smp_call_function_single(cpu, remote_function, &data, 1);
138
139 return data.ret;
140}
141
fae3fde6
PZ
142static inline struct perf_cpu_context *
143__get_cpu_context(struct perf_event_context *ctx)
144{
145 return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
146}
147
148static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
149 struct perf_event_context *ctx)
0017960f 150{
fae3fde6
PZ
151 raw_spin_lock(&cpuctx->ctx.lock);
152 if (ctx)
153 raw_spin_lock(&ctx->lock);
154}
155
156static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
157 struct perf_event_context *ctx)
158{
159 if (ctx)
160 raw_spin_unlock(&ctx->lock);
161 raw_spin_unlock(&cpuctx->ctx.lock);
162}
163
63b6da39
PZ
164#define TASK_TOMBSTONE ((void *)-1L)
165
166static bool is_kernel_event(struct perf_event *event)
167{
f47c02c0 168 return READ_ONCE(event->owner) == TASK_TOMBSTONE;
63b6da39
PZ
169}
170
39a43640
PZ
171/*
172 * On task ctx scheduling...
173 *
174 * When !ctx->nr_events a task context will not be scheduled. This means
175 * we can disable the scheduler hooks (for performance) without leaving
176 * pending task ctx state.
177 *
178 * This however results in two special cases:
179 *
180 * - removing the last event from a task ctx; this is relatively straight
181 * forward and is done in __perf_remove_from_context.
182 *
183 * - adding the first event to a task ctx; this is tricky because we cannot
184 * rely on ctx->is_active and therefore cannot use event_function_call().
185 * See perf_install_in_context().
186 *
39a43640
PZ
187 * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set.
188 */
189
fae3fde6
PZ
190typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *,
191 struct perf_event_context *, void *);
192
193struct event_function_struct {
194 struct perf_event *event;
195 event_f func;
196 void *data;
197};
198
199static int event_function(void *info)
200{
201 struct event_function_struct *efs = info;
202 struct perf_event *event = efs->event;
0017960f 203 struct perf_event_context *ctx = event->ctx;
fae3fde6
PZ
204 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
205 struct perf_event_context *task_ctx = cpuctx->task_ctx;
63b6da39 206 int ret = 0;
fae3fde6
PZ
207
208 WARN_ON_ONCE(!irqs_disabled());
209
63b6da39 210 perf_ctx_lock(cpuctx, task_ctx);
fae3fde6
PZ
211 /*
212 * Since we do the IPI call without holding ctx->lock things can have
213 * changed, double check we hit the task we set out to hit.
fae3fde6
PZ
214 */
215 if (ctx->task) {
63b6da39 216 if (ctx->task != current) {
0da4cf3e 217 ret = -ESRCH;
63b6da39
PZ
218 goto unlock;
219 }
fae3fde6 220
fae3fde6
PZ
221 /*
222 * We only use event_function_call() on established contexts,
223 * and event_function() is only ever called when active (or
224 * rather, we'll have bailed in task_function_call() or the
225 * above ctx->task != current test), therefore we must have
226 * ctx->is_active here.
227 */
228 WARN_ON_ONCE(!ctx->is_active);
229 /*
230 * And since we have ctx->is_active, cpuctx->task_ctx must
231 * match.
232 */
63b6da39
PZ
233 WARN_ON_ONCE(task_ctx != ctx);
234 } else {
235 WARN_ON_ONCE(&cpuctx->ctx != ctx);
fae3fde6 236 }
63b6da39 237
fae3fde6 238 efs->func(event, cpuctx, ctx, efs->data);
63b6da39 239unlock:
fae3fde6
PZ
240 perf_ctx_unlock(cpuctx, task_ctx);
241
63b6da39 242 return ret;
fae3fde6
PZ
243}
244
245static void event_function_local(struct perf_event *event, event_f func, void *data)
246{
247 struct event_function_struct efs = {
248 .event = event,
249 .func = func,
250 .data = data,
251 };
252
253 int ret = event_function(&efs);
254 WARN_ON_ONCE(ret);
255}
256
257static void event_function_call(struct perf_event *event, event_f func, void *data)
0017960f
PZ
258{
259 struct perf_event_context *ctx = event->ctx;
63b6da39 260 struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */
fae3fde6
PZ
261 struct event_function_struct efs = {
262 .event = event,
263 .func = func,
264 .data = data,
265 };
0017960f 266
c97f4736
PZ
267 if (!event->parent) {
268 /*
269 * If this is a !child event, we must hold ctx::mutex to
270 * stabilize the the event->ctx relation. See
271 * perf_event_ctx_lock().
272 */
273 lockdep_assert_held(&ctx->mutex);
274 }
0017960f
PZ
275
276 if (!task) {
fae3fde6 277 cpu_function_call(event->cpu, event_function, &efs);
0017960f
PZ
278 return;
279 }
280
63b6da39
PZ
281 if (task == TASK_TOMBSTONE)
282 return;
283
a096309b 284again:
fae3fde6 285 if (!task_function_call(task, event_function, &efs))
0017960f
PZ
286 return;
287
288 raw_spin_lock_irq(&ctx->lock);
63b6da39
PZ
289 /*
290 * Reload the task pointer, it might have been changed by
291 * a concurrent perf_event_context_sched_out().
292 */
293 task = ctx->task;
a096309b
PZ
294 if (task == TASK_TOMBSTONE) {
295 raw_spin_unlock_irq(&ctx->lock);
296 return;
0017960f 297 }
a096309b
PZ
298 if (ctx->is_active) {
299 raw_spin_unlock_irq(&ctx->lock);
300 goto again;
301 }
302 func(event, NULL, ctx, data);
0017960f
PZ
303 raw_spin_unlock_irq(&ctx->lock);
304}
305
e5d1367f
SE
306#define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
307 PERF_FLAG_FD_OUTPUT |\
a21b0b35
YD
308 PERF_FLAG_PID_CGROUP |\
309 PERF_FLAG_FD_CLOEXEC)
e5d1367f 310
bce38cd5
SE
311/*
312 * branch priv levels that need permission checks
313 */
314#define PERF_SAMPLE_BRANCH_PERM_PLM \
315 (PERF_SAMPLE_BRANCH_KERNEL |\
316 PERF_SAMPLE_BRANCH_HV)
317
0b3fcf17
SE
318enum event_type_t {
319 EVENT_FLEXIBLE = 0x1,
320 EVENT_PINNED = 0x2,
3cbaa590 321 EVENT_TIME = 0x4,
0b3fcf17
SE
322 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
323};
324
e5d1367f
SE
325/*
326 * perf_sched_events : >0 events exist
327 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
328 */
9107c89e
PZ
329
330static void perf_sched_delayed(struct work_struct *work);
331DEFINE_STATIC_KEY_FALSE(perf_sched_events);
332static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed);
333static DEFINE_MUTEX(perf_sched_mutex);
334static atomic_t perf_sched_count;
335
e5d1367f 336static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
ba532500 337static DEFINE_PER_CPU(int, perf_sched_cb_usages);
e5d1367f 338
cdd6c482
IM
339static atomic_t nr_mmap_events __read_mostly;
340static atomic_t nr_comm_events __read_mostly;
341static atomic_t nr_task_events __read_mostly;
948b26b6 342static atomic_t nr_freq_events __read_mostly;
45ac1403 343static atomic_t nr_switch_events __read_mostly;
9ee318a7 344
108b02cf
PZ
345static LIST_HEAD(pmus);
346static DEFINE_MUTEX(pmus_lock);
347static struct srcu_struct pmus_srcu;
348
0764771d 349/*
cdd6c482 350 * perf event paranoia level:
0fbdea19
IM
351 * -1 - not paranoid at all
352 * 0 - disallow raw tracepoint access for unpriv
cdd6c482 353 * 1 - disallow cpu events for unpriv
0fbdea19 354 * 2 - disallow kernel profiling for unpriv
0764771d 355 */
0161028b 356int sysctl_perf_event_paranoid __read_mostly = 2;
0764771d 357
20443384
FW
358/* Minimum for 512 kiB + 1 user control page */
359int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
df58ab24
PZ
360
361/*
cdd6c482 362 * max perf event sample rate
df58ab24 363 */
14c63f17
DH
364#define DEFAULT_MAX_SAMPLE_RATE 100000
365#define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
366#define DEFAULT_CPU_TIME_MAX_PERCENT 25
367
368int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
369
370static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
371static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
372
d9494cb4
PZ
373static int perf_sample_allowed_ns __read_mostly =
374 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
14c63f17 375
18ab2cd3 376static void update_perf_cpu_limits(void)
14c63f17
DH
377{
378 u64 tmp = perf_sample_period_ns;
379
380 tmp *= sysctl_perf_cpu_time_max_percent;
91a612ee
PZ
381 tmp = div_u64(tmp, 100);
382 if (!tmp)
383 tmp = 1;
384
385 WRITE_ONCE(perf_sample_allowed_ns, tmp);
14c63f17 386}
163ec435 387
9e630205
SE
388static int perf_rotate_context(struct perf_cpu_context *cpuctx);
389
163ec435
PZ
390int perf_proc_update_handler(struct ctl_table *table, int write,
391 void __user *buffer, size_t *lenp,
392 loff_t *ppos)
393{
723478c8 394 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
163ec435
PZ
395
396 if (ret || !write)
397 return ret;
398
399 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
14c63f17
DH
400 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
401 update_perf_cpu_limits();
402
403 return 0;
404}
405
406int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
407
408int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
409 void __user *buffer, size_t *lenp,
410 loff_t *ppos)
411{
412 int ret = proc_dointvec(table, write, buffer, lenp, ppos);
413
414 if (ret || !write)
415 return ret;
416
b303e7c1
PZ
417 if (sysctl_perf_cpu_time_max_percent == 100 ||
418 sysctl_perf_cpu_time_max_percent == 0) {
91a612ee
PZ
419 printk(KERN_WARNING
420 "perf: Dynamic interrupt throttling disabled, can hang your system!\n");
421 WRITE_ONCE(perf_sample_allowed_ns, 0);
422 } else {
423 update_perf_cpu_limits();
424 }
163ec435
PZ
425
426 return 0;
427}
1ccd1549 428
14c63f17
DH
429/*
430 * perf samples are done in some very critical code paths (NMIs).
431 * If they take too much CPU time, the system can lock up and not
432 * get any real work done. This will drop the sample rate when
433 * we detect that events are taking too long.
434 */
435#define NR_ACCUMULATED_SAMPLES 128
d9494cb4 436static DEFINE_PER_CPU(u64, running_sample_length);
14c63f17 437
91a612ee
PZ
438static u64 __report_avg;
439static u64 __report_allowed;
440
6a02ad66 441static void perf_duration_warn(struct irq_work *w)
14c63f17 442{
6a02ad66 443 printk_ratelimited(KERN_WARNING
91a612ee
PZ
444 "perf: interrupt took too long (%lld > %lld), lowering "
445 "kernel.perf_event_max_sample_rate to %d\n",
446 __report_avg, __report_allowed,
447 sysctl_perf_event_sample_rate);
6a02ad66
PZ
448}
449
450static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
451
452void perf_sample_event_took(u64 sample_len_ns)
453{
91a612ee
PZ
454 u64 max_len = READ_ONCE(perf_sample_allowed_ns);
455 u64 running_len;
456 u64 avg_len;
457 u32 max;
14c63f17 458
91a612ee 459 if (max_len == 0)
14c63f17
DH
460 return;
461
91a612ee
PZ
462 /* Decay the counter by 1 average sample. */
463 running_len = __this_cpu_read(running_sample_length);
464 running_len -= running_len/NR_ACCUMULATED_SAMPLES;
465 running_len += sample_len_ns;
466 __this_cpu_write(running_sample_length, running_len);
14c63f17
DH
467
468 /*
91a612ee
PZ
469 * Note: this will be biased artifically low until we have
470 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
14c63f17
DH
471 * from having to maintain a count.
472 */
91a612ee
PZ
473 avg_len = running_len/NR_ACCUMULATED_SAMPLES;
474 if (avg_len <= max_len)
14c63f17
DH
475 return;
476
91a612ee
PZ
477 __report_avg = avg_len;
478 __report_allowed = max_len;
14c63f17 479
91a612ee
PZ
480 /*
481 * Compute a throttle threshold 25% below the current duration.
482 */
483 avg_len += avg_len / 4;
484 max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent;
485 if (avg_len < max)
486 max /= (u32)avg_len;
487 else
488 max = 1;
14c63f17 489
91a612ee
PZ
490 WRITE_ONCE(perf_sample_allowed_ns, avg_len);
491 WRITE_ONCE(max_samples_per_tick, max);
492
493 sysctl_perf_event_sample_rate = max * HZ;
494 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
6a02ad66 495
cd578abb 496 if (!irq_work_queue(&perf_duration_work)) {
91a612ee 497 early_printk("perf: interrupt took too long (%lld > %lld), lowering "
cd578abb 498 "kernel.perf_event_max_sample_rate to %d\n",
91a612ee 499 __report_avg, __report_allowed,
cd578abb
PZ
500 sysctl_perf_event_sample_rate);
501 }
14c63f17
DH
502}
503
cdd6c482 504static atomic64_t perf_event_id;
a96bbc16 505
0b3fcf17
SE
506static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
507 enum event_type_t event_type);
508
509static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
e5d1367f
SE
510 enum event_type_t event_type,
511 struct task_struct *task);
512
513static void update_context_time(struct perf_event_context *ctx);
514static u64 perf_event_time(struct perf_event *event);
0b3fcf17 515
cdd6c482 516void __weak perf_event_print_debug(void) { }
0793a61d 517
84c79910 518extern __weak const char *perf_pmu_name(void)
0793a61d 519{
84c79910 520 return "pmu";
0793a61d
TG
521}
522
0b3fcf17
SE
523static inline u64 perf_clock(void)
524{
525 return local_clock();
526}
527
34f43927
PZ
528static inline u64 perf_event_clock(struct perf_event *event)
529{
530 return event->clock();
531}
532
e5d1367f
SE
533#ifdef CONFIG_CGROUP_PERF
534
e5d1367f
SE
535static inline bool
536perf_cgroup_match(struct perf_event *event)
537{
538 struct perf_event_context *ctx = event->ctx;
539 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
540
ef824fa1
TH
541 /* @event doesn't care about cgroup */
542 if (!event->cgrp)
543 return true;
544
545 /* wants specific cgroup scope but @cpuctx isn't associated with any */
546 if (!cpuctx->cgrp)
547 return false;
548
549 /*
550 * Cgroup scoping is recursive. An event enabled for a cgroup is
551 * also enabled for all its descendant cgroups. If @cpuctx's
552 * cgroup is a descendant of @event's (the test covers identity
553 * case), it's a match.
554 */
555 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
556 event->cgrp->css.cgroup);
e5d1367f
SE
557}
558
e5d1367f
SE
559static inline void perf_detach_cgroup(struct perf_event *event)
560{
4e2ba650 561 css_put(&event->cgrp->css);
e5d1367f
SE
562 event->cgrp = NULL;
563}
564
565static inline int is_cgroup_event(struct perf_event *event)
566{
567 return event->cgrp != NULL;
568}
569
570static inline u64 perf_cgroup_event_time(struct perf_event *event)
571{
572 struct perf_cgroup_info *t;
573
574 t = per_cpu_ptr(event->cgrp->info, event->cpu);
575 return t->time;
576}
577
578static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
579{
580 struct perf_cgroup_info *info;
581 u64 now;
582
583 now = perf_clock();
584
585 info = this_cpu_ptr(cgrp->info);
586
587 info->time += now - info->timestamp;
588 info->timestamp = now;
589}
590
591static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
592{
593 struct perf_cgroup *cgrp_out = cpuctx->cgrp;
594 if (cgrp_out)
595 __update_cgrp_time(cgrp_out);
596}
597
598static inline void update_cgrp_time_from_event(struct perf_event *event)
599{
3f7cce3c
SE
600 struct perf_cgroup *cgrp;
601
e5d1367f 602 /*
3f7cce3c
SE
603 * ensure we access cgroup data only when needed and
604 * when we know the cgroup is pinned (css_get)
e5d1367f 605 */
3f7cce3c 606 if (!is_cgroup_event(event))
e5d1367f
SE
607 return;
608
614e4c4e 609 cgrp = perf_cgroup_from_task(current, event->ctx);
3f7cce3c
SE
610 /*
611 * Do not update time when cgroup is not active
612 */
613 if (cgrp == event->cgrp)
614 __update_cgrp_time(event->cgrp);
e5d1367f
SE
615}
616
617static inline void
3f7cce3c
SE
618perf_cgroup_set_timestamp(struct task_struct *task,
619 struct perf_event_context *ctx)
e5d1367f
SE
620{
621 struct perf_cgroup *cgrp;
622 struct perf_cgroup_info *info;
623
3f7cce3c
SE
624 /*
625 * ctx->lock held by caller
626 * ensure we do not access cgroup data
627 * unless we have the cgroup pinned (css_get)
628 */
629 if (!task || !ctx->nr_cgroups)
e5d1367f
SE
630 return;
631
614e4c4e 632 cgrp = perf_cgroup_from_task(task, ctx);
e5d1367f 633 info = this_cpu_ptr(cgrp->info);
3f7cce3c 634 info->timestamp = ctx->timestamp;
e5d1367f
SE
635}
636
637#define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
638#define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
639
640/*
641 * reschedule events based on the cgroup constraint of task.
642 *
643 * mode SWOUT : schedule out everything
644 * mode SWIN : schedule in based on cgroup for next
645 */
18ab2cd3 646static void perf_cgroup_switch(struct task_struct *task, int mode)
e5d1367f
SE
647{
648 struct perf_cpu_context *cpuctx;
649 struct pmu *pmu;
650 unsigned long flags;
651
652 /*
653 * disable interrupts to avoid geting nr_cgroup
654 * changes via __perf_event_disable(). Also
655 * avoids preemption.
656 */
657 local_irq_save(flags);
658
659 /*
660 * we reschedule only in the presence of cgroup
661 * constrained events.
662 */
e5d1367f
SE
663
664 list_for_each_entry_rcu(pmu, &pmus, entry) {
e5d1367f 665 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
95cf59ea
PZ
666 if (cpuctx->unique_pmu != pmu)
667 continue; /* ensure we process each cpuctx once */
e5d1367f 668
e5d1367f
SE
669 /*
670 * perf_cgroup_events says at least one
671 * context on this CPU has cgroup events.
672 *
673 * ctx->nr_cgroups reports the number of cgroup
674 * events for a context.
675 */
676 if (cpuctx->ctx.nr_cgroups > 0) {
facc4307
PZ
677 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
678 perf_pmu_disable(cpuctx->ctx.pmu);
e5d1367f
SE
679
680 if (mode & PERF_CGROUP_SWOUT) {
681 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
682 /*
683 * must not be done before ctxswout due
684 * to event_filter_match() in event_sched_out()
685 */
686 cpuctx->cgrp = NULL;
687 }
688
689 if (mode & PERF_CGROUP_SWIN) {
e566b76e 690 WARN_ON_ONCE(cpuctx->cgrp);
95cf59ea
PZ
691 /*
692 * set cgrp before ctxsw in to allow
693 * event_filter_match() to not have to pass
694 * task around
614e4c4e
SE
695 * we pass the cpuctx->ctx to perf_cgroup_from_task()
696 * because cgorup events are only per-cpu
e5d1367f 697 */
614e4c4e 698 cpuctx->cgrp = perf_cgroup_from_task(task, &cpuctx->ctx);
e5d1367f
SE
699 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
700 }
facc4307
PZ
701 perf_pmu_enable(cpuctx->ctx.pmu);
702 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
e5d1367f 703 }
e5d1367f
SE
704 }
705
e5d1367f
SE
706 local_irq_restore(flags);
707}
708
a8d757ef
SE
709static inline void perf_cgroup_sched_out(struct task_struct *task,
710 struct task_struct *next)
e5d1367f 711{
a8d757ef
SE
712 struct perf_cgroup *cgrp1;
713 struct perf_cgroup *cgrp2 = NULL;
714
ddaaf4e2 715 rcu_read_lock();
a8d757ef
SE
716 /*
717 * we come here when we know perf_cgroup_events > 0
614e4c4e
SE
718 * we do not need to pass the ctx here because we know
719 * we are holding the rcu lock
a8d757ef 720 */
614e4c4e 721 cgrp1 = perf_cgroup_from_task(task, NULL);
70a01657 722 cgrp2 = perf_cgroup_from_task(next, NULL);
a8d757ef
SE
723
724 /*
725 * only schedule out current cgroup events if we know
726 * that we are switching to a different cgroup. Otherwise,
727 * do no touch the cgroup events.
728 */
729 if (cgrp1 != cgrp2)
730 perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
ddaaf4e2
SE
731
732 rcu_read_unlock();
e5d1367f
SE
733}
734
a8d757ef
SE
735static inline void perf_cgroup_sched_in(struct task_struct *prev,
736 struct task_struct *task)
e5d1367f 737{
a8d757ef
SE
738 struct perf_cgroup *cgrp1;
739 struct perf_cgroup *cgrp2 = NULL;
740
ddaaf4e2 741 rcu_read_lock();
a8d757ef
SE
742 /*
743 * we come here when we know perf_cgroup_events > 0
614e4c4e
SE
744 * we do not need to pass the ctx here because we know
745 * we are holding the rcu lock
a8d757ef 746 */
614e4c4e 747 cgrp1 = perf_cgroup_from_task(task, NULL);
614e4c4e 748 cgrp2 = perf_cgroup_from_task(prev, NULL);
a8d757ef
SE
749
750 /*
751 * only need to schedule in cgroup events if we are changing
752 * cgroup during ctxsw. Cgroup events were not scheduled
753 * out of ctxsw out if that was not the case.
754 */
755 if (cgrp1 != cgrp2)
756 perf_cgroup_switch(task, PERF_CGROUP_SWIN);
ddaaf4e2
SE
757
758 rcu_read_unlock();
e5d1367f
SE
759}
760
761static inline int perf_cgroup_connect(int fd, struct perf_event *event,
762 struct perf_event_attr *attr,
763 struct perf_event *group_leader)
764{
765 struct perf_cgroup *cgrp;
766 struct cgroup_subsys_state *css;
2903ff01
AV
767 struct fd f = fdget(fd);
768 int ret = 0;
e5d1367f 769
2903ff01 770 if (!f.file)
e5d1367f
SE
771 return -EBADF;
772
b583043e 773 css = css_tryget_online_from_dir(f.file->f_path.dentry,
ec903c0c 774 &perf_event_cgrp_subsys);
3db272c0
LZ
775 if (IS_ERR(css)) {
776 ret = PTR_ERR(css);
777 goto out;
778 }
e5d1367f
SE
779
780 cgrp = container_of(css, struct perf_cgroup, css);
781 event->cgrp = cgrp;
782
783 /*
784 * all events in a group must monitor
785 * the same cgroup because a task belongs
786 * to only one perf cgroup at a time
787 */
788 if (group_leader && group_leader->cgrp != cgrp) {
789 perf_detach_cgroup(event);
790 ret = -EINVAL;
e5d1367f 791 }
3db272c0 792out:
2903ff01 793 fdput(f);
e5d1367f
SE
794 return ret;
795}
796
797static inline void
798perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
799{
800 struct perf_cgroup_info *t;
801 t = per_cpu_ptr(event->cgrp->info, event->cpu);
802 event->shadow_ctx_time = now - t->timestamp;
803}
804
805static inline void
806perf_cgroup_defer_enabled(struct perf_event *event)
807{
808 /*
809 * when the current task's perf cgroup does not match
810 * the event's, we need to remember to call the
811 * perf_mark_enable() function the first time a task with
812 * a matching perf cgroup is scheduled in.
813 */
814 if (is_cgroup_event(event) && !perf_cgroup_match(event))
815 event->cgrp_defer_enabled = 1;
816}
817
818static inline void
819perf_cgroup_mark_enabled(struct perf_event *event,
820 struct perf_event_context *ctx)
821{
822 struct perf_event *sub;
823 u64 tstamp = perf_event_time(event);
824
825 if (!event->cgrp_defer_enabled)
826 return;
827
828 event->cgrp_defer_enabled = 0;
829
830 event->tstamp_enabled = tstamp - event->total_time_enabled;
831 list_for_each_entry(sub, &event->sibling_list, group_entry) {
832 if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
833 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
834 sub->cgrp_defer_enabled = 0;
835 }
836 }
837}
838#else /* !CONFIG_CGROUP_PERF */
839
840static inline bool
841perf_cgroup_match(struct perf_event *event)
842{
843 return true;
844}
845
846static inline void perf_detach_cgroup(struct perf_event *event)
847{}
848
849static inline int is_cgroup_event(struct perf_event *event)
850{
851 return 0;
852}
853
854static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
855{
856 return 0;
857}
858
859static inline void update_cgrp_time_from_event(struct perf_event *event)
860{
861}
862
863static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
864{
865}
866
a8d757ef
SE
867static inline void perf_cgroup_sched_out(struct task_struct *task,
868 struct task_struct *next)
e5d1367f
SE
869{
870}
871
a8d757ef
SE
872static inline void perf_cgroup_sched_in(struct task_struct *prev,
873 struct task_struct *task)
e5d1367f
SE
874{
875}
876
877static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
878 struct perf_event_attr *attr,
879 struct perf_event *group_leader)
880{
881 return -EINVAL;
882}
883
884static inline void
3f7cce3c
SE
885perf_cgroup_set_timestamp(struct task_struct *task,
886 struct perf_event_context *ctx)
e5d1367f
SE
887{
888}
889
890void
891perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
892{
893}
894
895static inline void
896perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
897{
898}
899
900static inline u64 perf_cgroup_event_time(struct perf_event *event)
901{
902 return 0;
903}
904
905static inline void
906perf_cgroup_defer_enabled(struct perf_event *event)
907{
908}
909
910static inline void
911perf_cgroup_mark_enabled(struct perf_event *event,
912 struct perf_event_context *ctx)
913{
914}
915#endif
916
9e630205
SE
917/*
918 * set default to be dependent on timer tick just
919 * like original code
920 */
921#define PERF_CPU_HRTIMER (1000 / HZ)
922/*
923 * function must be called with interrupts disbled
924 */
272325c4 925static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr)
9e630205
SE
926{
927 struct perf_cpu_context *cpuctx;
9e630205
SE
928 int rotations = 0;
929
930 WARN_ON(!irqs_disabled());
931
932 cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
9e630205
SE
933 rotations = perf_rotate_context(cpuctx);
934
4cfafd30
PZ
935 raw_spin_lock(&cpuctx->hrtimer_lock);
936 if (rotations)
9e630205 937 hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
4cfafd30
PZ
938 else
939 cpuctx->hrtimer_active = 0;
940 raw_spin_unlock(&cpuctx->hrtimer_lock);
9e630205 941
4cfafd30 942 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART;
9e630205
SE
943}
944
272325c4 945static void __perf_mux_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
9e630205 946{
272325c4 947 struct hrtimer *timer = &cpuctx->hrtimer;
9e630205 948 struct pmu *pmu = cpuctx->ctx.pmu;
272325c4 949 u64 interval;
9e630205
SE
950
951 /* no multiplexing needed for SW PMU */
952 if (pmu->task_ctx_nr == perf_sw_context)
953 return;
954
62b85639
SE
955 /*
956 * check default is sane, if not set then force to
957 * default interval (1/tick)
958 */
272325c4
PZ
959 interval = pmu->hrtimer_interval_ms;
960 if (interval < 1)
961 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
62b85639 962
272325c4 963 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval);
9e630205 964
4cfafd30
PZ
965 raw_spin_lock_init(&cpuctx->hrtimer_lock);
966 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED);
272325c4 967 timer->function = perf_mux_hrtimer_handler;
9e630205
SE
968}
969
272325c4 970static int perf_mux_hrtimer_restart(struct perf_cpu_context *cpuctx)
9e630205 971{
272325c4 972 struct hrtimer *timer = &cpuctx->hrtimer;
9e630205 973 struct pmu *pmu = cpuctx->ctx.pmu;
4cfafd30 974 unsigned long flags;
9e630205
SE
975
976 /* not for SW PMU */
977 if (pmu->task_ctx_nr == perf_sw_context)
272325c4 978 return 0;
9e630205 979
4cfafd30
PZ
980 raw_spin_lock_irqsave(&cpuctx->hrtimer_lock, flags);
981 if (!cpuctx->hrtimer_active) {
982 cpuctx->hrtimer_active = 1;
983 hrtimer_forward_now(timer, cpuctx->hrtimer_interval);
984 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
985 }
986 raw_spin_unlock_irqrestore(&cpuctx->hrtimer_lock, flags);
9e630205 987
272325c4 988 return 0;
9e630205
SE
989}
990
33696fc0 991void perf_pmu_disable(struct pmu *pmu)
9e35ad38 992{
33696fc0
PZ
993 int *count = this_cpu_ptr(pmu->pmu_disable_count);
994 if (!(*count)++)
995 pmu->pmu_disable(pmu);
9e35ad38 996}
9e35ad38 997
33696fc0 998void perf_pmu_enable(struct pmu *pmu)
9e35ad38 999{
33696fc0
PZ
1000 int *count = this_cpu_ptr(pmu->pmu_disable_count);
1001 if (!--(*count))
1002 pmu->pmu_enable(pmu);
9e35ad38 1003}
9e35ad38 1004
2fde4f94 1005static DEFINE_PER_CPU(struct list_head, active_ctx_list);
e9d2b064
PZ
1006
1007/*
2fde4f94
MR
1008 * perf_event_ctx_activate(), perf_event_ctx_deactivate(), and
1009 * perf_event_task_tick() are fully serialized because they're strictly cpu
1010 * affine and perf_event_ctx{activate,deactivate} are called with IRQs
1011 * disabled, while perf_event_task_tick is called from IRQ context.
e9d2b064 1012 */
2fde4f94 1013static void perf_event_ctx_activate(struct perf_event_context *ctx)
9e35ad38 1014{
2fde4f94 1015 struct list_head *head = this_cpu_ptr(&active_ctx_list);
b5ab4cd5 1016
e9d2b064 1017 WARN_ON(!irqs_disabled());
b5ab4cd5 1018
2fde4f94
MR
1019 WARN_ON(!list_empty(&ctx->active_ctx_list));
1020
1021 list_add(&ctx->active_ctx_list, head);
1022}
1023
1024static void perf_event_ctx_deactivate(struct perf_event_context *ctx)
1025{
1026 WARN_ON(!irqs_disabled());
1027
1028 WARN_ON(list_empty(&ctx->active_ctx_list));
1029
1030 list_del_init(&ctx->active_ctx_list);
9e35ad38 1031}
9e35ad38 1032
cdd6c482 1033static void get_ctx(struct perf_event_context *ctx)
a63eaf34 1034{
e5289d4a 1035 WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
a63eaf34
PM
1036}
1037
4af57ef2
YZ
1038static void free_ctx(struct rcu_head *head)
1039{
1040 struct perf_event_context *ctx;
1041
1042 ctx = container_of(head, struct perf_event_context, rcu_head);
1043 kfree(ctx->task_ctx_data);
1044 kfree(ctx);
1045}
1046
cdd6c482 1047static void put_ctx(struct perf_event_context *ctx)
a63eaf34 1048{
564c2b21
PM
1049 if (atomic_dec_and_test(&ctx->refcount)) {
1050 if (ctx->parent_ctx)
1051 put_ctx(ctx->parent_ctx);
63b6da39 1052 if (ctx->task && ctx->task != TASK_TOMBSTONE)
c93f7669 1053 put_task_struct(ctx->task);
4af57ef2 1054 call_rcu(&ctx->rcu_head, free_ctx);
564c2b21 1055 }
a63eaf34
PM
1056}
1057
f63a8daa
PZ
1058/*
1059 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and
1060 * perf_pmu_migrate_context() we need some magic.
1061 *
1062 * Those places that change perf_event::ctx will hold both
1063 * perf_event_ctx::mutex of the 'old' and 'new' ctx value.
1064 *
8b10c5e2
PZ
1065 * Lock ordering is by mutex address. There are two other sites where
1066 * perf_event_context::mutex nests and those are:
1067 *
1068 * - perf_event_exit_task_context() [ child , 0 ]
8ba289b8
PZ
1069 * perf_event_exit_event()
1070 * put_event() [ parent, 1 ]
8b10c5e2
PZ
1071 *
1072 * - perf_event_init_context() [ parent, 0 ]
1073 * inherit_task_group()
1074 * inherit_group()
1075 * inherit_event()
1076 * perf_event_alloc()
1077 * perf_init_event()
1078 * perf_try_init_event() [ child , 1 ]
1079 *
1080 * While it appears there is an obvious deadlock here -- the parent and child
1081 * nesting levels are inverted between the two. This is in fact safe because
1082 * life-time rules separate them. That is an exiting task cannot fork, and a
1083 * spawning task cannot (yet) exit.
1084 *
1085 * But remember that that these are parent<->child context relations, and
1086 * migration does not affect children, therefore these two orderings should not
1087 * interact.
f63a8daa
PZ
1088 *
1089 * The change in perf_event::ctx does not affect children (as claimed above)
1090 * because the sys_perf_event_open() case will install a new event and break
1091 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only
1092 * concerned with cpuctx and that doesn't have children.
1093 *
1094 * The places that change perf_event::ctx will issue:
1095 *
1096 * perf_remove_from_context();
1097 * synchronize_rcu();
1098 * perf_install_in_context();
1099 *
1100 * to affect the change. The remove_from_context() + synchronize_rcu() should
1101 * quiesce the event, after which we can install it in the new location. This
1102 * means that only external vectors (perf_fops, prctl) can perturb the event
1103 * while in transit. Therefore all such accessors should also acquire
1104 * perf_event_context::mutex to serialize against this.
1105 *
1106 * However; because event->ctx can change while we're waiting to acquire
1107 * ctx->mutex we must be careful and use the below perf_event_ctx_lock()
1108 * function.
1109 *
1110 * Lock order:
79c9ce57 1111 * cred_guard_mutex
f63a8daa
PZ
1112 * task_struct::perf_event_mutex
1113 * perf_event_context::mutex
f63a8daa 1114 * perf_event::child_mutex;
07c4a776 1115 * perf_event_context::lock
f63a8daa
PZ
1116 * perf_event::mmap_mutex
1117 * mmap_sem
1118 */
a83fe28e
PZ
1119static struct perf_event_context *
1120perf_event_ctx_lock_nested(struct perf_event *event, int nesting)
f63a8daa
PZ
1121{
1122 struct perf_event_context *ctx;
1123
1124again:
1125 rcu_read_lock();
1126 ctx = ACCESS_ONCE(event->ctx);
1127 if (!atomic_inc_not_zero(&ctx->refcount)) {
1128 rcu_read_unlock();
1129 goto again;
1130 }
1131 rcu_read_unlock();
1132
a83fe28e 1133 mutex_lock_nested(&ctx->mutex, nesting);
f63a8daa
PZ
1134 if (event->ctx != ctx) {
1135 mutex_unlock(&ctx->mutex);
1136 put_ctx(ctx);
1137 goto again;
1138 }
1139
1140 return ctx;
1141}
1142
a83fe28e
PZ
1143static inline struct perf_event_context *
1144perf_event_ctx_lock(struct perf_event *event)
1145{
1146 return perf_event_ctx_lock_nested(event, 0);
1147}
1148
f63a8daa
PZ
1149static void perf_event_ctx_unlock(struct perf_event *event,
1150 struct perf_event_context *ctx)
1151{
1152 mutex_unlock(&ctx->mutex);
1153 put_ctx(ctx);
1154}
1155
211de6eb
PZ
1156/*
1157 * This must be done under the ctx->lock, such as to serialize against
1158 * context_equiv(), therefore we cannot call put_ctx() since that might end up
1159 * calling scheduler related locks and ctx->lock nests inside those.
1160 */
1161static __must_check struct perf_event_context *
1162unclone_ctx(struct perf_event_context *ctx)
71a851b4 1163{
211de6eb
PZ
1164 struct perf_event_context *parent_ctx = ctx->parent_ctx;
1165
1166 lockdep_assert_held(&ctx->lock);
1167
1168 if (parent_ctx)
71a851b4 1169 ctx->parent_ctx = NULL;
5a3126d4 1170 ctx->generation++;
211de6eb
PZ
1171
1172 return parent_ctx;
71a851b4
PZ
1173}
1174
6844c09d
ACM
1175static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
1176{
1177 /*
1178 * only top level events have the pid namespace they were created in
1179 */
1180 if (event->parent)
1181 event = event->parent;
1182
1183 return task_tgid_nr_ns(p, event->ns);
1184}
1185
1186static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
1187{
1188 /*
1189 * only top level events have the pid namespace they were created in
1190 */
1191 if (event->parent)
1192 event = event->parent;
1193
1194 return task_pid_nr_ns(p, event->ns);
1195}
1196
7f453c24 1197/*
cdd6c482 1198 * If we inherit events we want to return the parent event id
7f453c24
PZ
1199 * to userspace.
1200 */
cdd6c482 1201static u64 primary_event_id(struct perf_event *event)
7f453c24 1202{
cdd6c482 1203 u64 id = event->id;
7f453c24 1204
cdd6c482
IM
1205 if (event->parent)
1206 id = event->parent->id;
7f453c24
PZ
1207
1208 return id;
1209}
1210
25346b93 1211/*
cdd6c482 1212 * Get the perf_event_context for a task and lock it.
63b6da39 1213 *
25346b93
PM
1214 * This has to cope with with the fact that until it is locked,
1215 * the context could get moved to another task.
1216 */
cdd6c482 1217static struct perf_event_context *
8dc85d54 1218perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
25346b93 1219{
cdd6c482 1220 struct perf_event_context *ctx;
25346b93 1221
9ed6060d 1222retry:
058ebd0e
PZ
1223 /*
1224 * One of the few rules of preemptible RCU is that one cannot do
1225 * rcu_read_unlock() while holding a scheduler (or nested) lock when
2fd59077 1226 * part of the read side critical section was irqs-enabled -- see
058ebd0e
PZ
1227 * rcu_read_unlock_special().
1228 *
1229 * Since ctx->lock nests under rq->lock we must ensure the entire read
2fd59077 1230 * side critical section has interrupts disabled.
058ebd0e 1231 */
2fd59077 1232 local_irq_save(*flags);
058ebd0e 1233 rcu_read_lock();
8dc85d54 1234 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
25346b93
PM
1235 if (ctx) {
1236 /*
1237 * If this context is a clone of another, it might
1238 * get swapped for another underneath us by
cdd6c482 1239 * perf_event_task_sched_out, though the
25346b93
PM
1240 * rcu_read_lock() protects us from any context
1241 * getting freed. Lock the context and check if it
1242 * got swapped before we could get the lock, and retry
1243 * if so. If we locked the right context, then it
1244 * can't get swapped on us any more.
1245 */
2fd59077 1246 raw_spin_lock(&ctx->lock);
8dc85d54 1247 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
2fd59077 1248 raw_spin_unlock(&ctx->lock);
058ebd0e 1249 rcu_read_unlock();
2fd59077 1250 local_irq_restore(*flags);
25346b93
PM
1251 goto retry;
1252 }
b49a9e7e 1253
63b6da39
PZ
1254 if (ctx->task == TASK_TOMBSTONE ||
1255 !atomic_inc_not_zero(&ctx->refcount)) {
2fd59077 1256 raw_spin_unlock(&ctx->lock);
b49a9e7e 1257 ctx = NULL;
828b6f0e
PZ
1258 } else {
1259 WARN_ON_ONCE(ctx->task != task);
b49a9e7e 1260 }
25346b93
PM
1261 }
1262 rcu_read_unlock();
2fd59077
PM
1263 if (!ctx)
1264 local_irq_restore(*flags);
25346b93
PM
1265 return ctx;
1266}
1267
1268/*
1269 * Get the context for a task and increment its pin_count so it
1270 * can't get swapped to another task. This also increments its
1271 * reference count so that the context can't get freed.
1272 */
8dc85d54
PZ
1273static struct perf_event_context *
1274perf_pin_task_context(struct task_struct *task, int ctxn)
25346b93 1275{
cdd6c482 1276 struct perf_event_context *ctx;
25346b93
PM
1277 unsigned long flags;
1278
8dc85d54 1279 ctx = perf_lock_task_context(task, ctxn, &flags);
25346b93
PM
1280 if (ctx) {
1281 ++ctx->pin_count;
e625cce1 1282 raw_spin_unlock_irqrestore(&ctx->lock, flags);
25346b93
PM
1283 }
1284 return ctx;
1285}
1286
cdd6c482 1287static void perf_unpin_context(struct perf_event_context *ctx)
25346b93
PM
1288{
1289 unsigned long flags;
1290
e625cce1 1291 raw_spin_lock_irqsave(&ctx->lock, flags);
25346b93 1292 --ctx->pin_count;
e625cce1 1293 raw_spin_unlock_irqrestore(&ctx->lock, flags);
25346b93
PM
1294}
1295
f67218c3
PZ
1296/*
1297 * Update the record of the current time in a context.
1298 */
1299static void update_context_time(struct perf_event_context *ctx)
1300{
1301 u64 now = perf_clock();
1302
1303 ctx->time += now - ctx->timestamp;
1304 ctx->timestamp = now;
1305}
1306
4158755d
SE
1307static u64 perf_event_time(struct perf_event *event)
1308{
1309 struct perf_event_context *ctx = event->ctx;
e5d1367f
SE
1310
1311 if (is_cgroup_event(event))
1312 return perf_cgroup_event_time(event);
1313
4158755d
SE
1314 return ctx ? ctx->time : 0;
1315}
1316
f67218c3
PZ
1317/*
1318 * Update the total_time_enabled and total_time_running fields for a event.
1319 */
1320static void update_event_times(struct perf_event *event)
1321{
1322 struct perf_event_context *ctx = event->ctx;
1323 u64 run_end;
1324
3cbaa590
PZ
1325 lockdep_assert_held(&ctx->lock);
1326
f67218c3
PZ
1327 if (event->state < PERF_EVENT_STATE_INACTIVE ||
1328 event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
1329 return;
3cbaa590 1330
e5d1367f
SE
1331 /*
1332 * in cgroup mode, time_enabled represents
1333 * the time the event was enabled AND active
1334 * tasks were in the monitored cgroup. This is
1335 * independent of the activity of the context as
1336 * there may be a mix of cgroup and non-cgroup events.
1337 *
1338 * That is why we treat cgroup events differently
1339 * here.
1340 */
1341 if (is_cgroup_event(event))
46cd6a7f 1342 run_end = perf_cgroup_event_time(event);
e5d1367f
SE
1343 else if (ctx->is_active)
1344 run_end = ctx->time;
acd1d7c1
PZ
1345 else
1346 run_end = event->tstamp_stopped;
1347
1348 event->total_time_enabled = run_end - event->tstamp_enabled;
f67218c3
PZ
1349
1350 if (event->state == PERF_EVENT_STATE_INACTIVE)
1351 run_end = event->tstamp_stopped;
1352 else
4158755d 1353 run_end = perf_event_time(event);
f67218c3
PZ
1354
1355 event->total_time_running = run_end - event->tstamp_running;
e5d1367f 1356
f67218c3
PZ
1357}
1358
96c21a46
PZ
1359/*
1360 * Update total_time_enabled and total_time_running for all events in a group.
1361 */
1362static void update_group_times(struct perf_event *leader)
1363{
1364 struct perf_event *event;
1365
1366 update_event_times(leader);
1367 list_for_each_entry(event, &leader->sibling_list, group_entry)
1368 update_event_times(event);
1369}
1370
889ff015
FW
1371static struct list_head *
1372ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
1373{
1374 if (event->attr.pinned)
1375 return &ctx->pinned_groups;
1376 else
1377 return &ctx->flexible_groups;
1378}
1379
fccc714b 1380/*
cdd6c482 1381 * Add a event from the lists for its context.
fccc714b
PZ
1382 * Must be called with ctx->mutex and ctx->lock held.
1383 */
04289bb9 1384static void
cdd6c482 1385list_add_event(struct perf_event *event, struct perf_event_context *ctx)
04289bb9 1386{
c994d613
PZ
1387 lockdep_assert_held(&ctx->lock);
1388
8a49542c
PZ
1389 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
1390 event->attach_state |= PERF_ATTACH_CONTEXT;
04289bb9
IM
1391
1392 /*
8a49542c
PZ
1393 * If we're a stand alone event or group leader, we go to the context
1394 * list, group events are kept attached to the group so that
1395 * perf_group_detach can, at all times, locate all siblings.
04289bb9 1396 */
8a49542c 1397 if (event->group_leader == event) {
889ff015
FW
1398 struct list_head *list;
1399
d6f962b5
FW
1400 if (is_software_event(event))
1401 event->group_flags |= PERF_GROUP_SOFTWARE;
1402
889ff015
FW
1403 list = ctx_group_list(event, ctx);
1404 list_add_tail(&event->group_entry, list);
5c148194 1405 }
592903cd 1406
08309379 1407 if (is_cgroup_event(event))
e5d1367f 1408 ctx->nr_cgroups++;
e5d1367f 1409
cdd6c482
IM
1410 list_add_rcu(&event->event_entry, &ctx->event_list);
1411 ctx->nr_events++;
1412 if (event->attr.inherit_stat)
bfbd3381 1413 ctx->nr_stat++;
5a3126d4
PZ
1414
1415 ctx->generation++;
04289bb9
IM
1416}
1417
0231bb53
JO
1418/*
1419 * Initialize event state based on the perf_event_attr::disabled.
1420 */
1421static inline void perf_event__state_init(struct perf_event *event)
1422{
1423 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
1424 PERF_EVENT_STATE_INACTIVE;
1425}
1426
a723968c 1427static void __perf_event_read_size(struct perf_event *event, int nr_siblings)
c320c7b7
ACM
1428{
1429 int entry = sizeof(u64); /* value */
1430 int size = 0;
1431 int nr = 1;
1432
1433 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1434 size += sizeof(u64);
1435
1436 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1437 size += sizeof(u64);
1438
1439 if (event->attr.read_format & PERF_FORMAT_ID)
1440 entry += sizeof(u64);
1441
1442 if (event->attr.read_format & PERF_FORMAT_GROUP) {
a723968c 1443 nr += nr_siblings;
c320c7b7
ACM
1444 size += sizeof(u64);
1445 }
1446
1447 size += entry * nr;
1448 event->read_size = size;
1449}
1450
a723968c 1451static void __perf_event_header_size(struct perf_event *event, u64 sample_type)
c320c7b7
ACM
1452{
1453 struct perf_sample_data *data;
c320c7b7
ACM
1454 u16 size = 0;
1455
c320c7b7
ACM
1456 if (sample_type & PERF_SAMPLE_IP)
1457 size += sizeof(data->ip);
1458
6844c09d
ACM
1459 if (sample_type & PERF_SAMPLE_ADDR)
1460 size += sizeof(data->addr);
1461
1462 if (sample_type & PERF_SAMPLE_PERIOD)
1463 size += sizeof(data->period);
1464
c3feedf2
AK
1465 if (sample_type & PERF_SAMPLE_WEIGHT)
1466 size += sizeof(data->weight);
1467
6844c09d
ACM
1468 if (sample_type & PERF_SAMPLE_READ)
1469 size += event->read_size;
1470
d6be9ad6
SE
1471 if (sample_type & PERF_SAMPLE_DATA_SRC)
1472 size += sizeof(data->data_src.val);
1473
fdfbbd07
AK
1474 if (sample_type & PERF_SAMPLE_TRANSACTION)
1475 size += sizeof(data->txn);
1476
6844c09d
ACM
1477 event->header_size = size;
1478}
1479
a723968c
PZ
1480/*
1481 * Called at perf_event creation and when events are attached/detached from a
1482 * group.
1483 */
1484static void perf_event__header_size(struct perf_event *event)
1485{
1486 __perf_event_read_size(event,
1487 event->group_leader->nr_siblings);
1488 __perf_event_header_size(event, event->attr.sample_type);
1489}
1490
6844c09d
ACM
1491static void perf_event__id_header_size(struct perf_event *event)
1492{
1493 struct perf_sample_data *data;
1494 u64 sample_type = event->attr.sample_type;
1495 u16 size = 0;
1496
c320c7b7
ACM
1497 if (sample_type & PERF_SAMPLE_TID)
1498 size += sizeof(data->tid_entry);
1499
1500 if (sample_type & PERF_SAMPLE_TIME)
1501 size += sizeof(data->time);
1502
ff3d527c
AH
1503 if (sample_type & PERF_SAMPLE_IDENTIFIER)
1504 size += sizeof(data->id);
1505
c320c7b7
ACM
1506 if (sample_type & PERF_SAMPLE_ID)
1507 size += sizeof(data->id);
1508
1509 if (sample_type & PERF_SAMPLE_STREAM_ID)
1510 size += sizeof(data->stream_id);
1511
1512 if (sample_type & PERF_SAMPLE_CPU)
1513 size += sizeof(data->cpu_entry);
1514
6844c09d 1515 event->id_header_size = size;
c320c7b7
ACM
1516}
1517
a723968c
PZ
1518static bool perf_event_validate_size(struct perf_event *event)
1519{
1520 /*
1521 * The values computed here will be over-written when we actually
1522 * attach the event.
1523 */
1524 __perf_event_read_size(event, event->group_leader->nr_siblings + 1);
1525 __perf_event_header_size(event, event->attr.sample_type & ~PERF_SAMPLE_READ);
1526 perf_event__id_header_size(event);
1527
1528 /*
1529 * Sum the lot; should not exceed the 64k limit we have on records.
1530 * Conservative limit to allow for callchains and other variable fields.
1531 */
1532 if (event->read_size + event->header_size +
1533 event->id_header_size + sizeof(struct perf_event_header) >= 16*1024)
1534 return false;
1535
1536 return true;
1537}
1538
8a49542c
PZ
1539static void perf_group_attach(struct perf_event *event)
1540{
c320c7b7 1541 struct perf_event *group_leader = event->group_leader, *pos;
8a49542c 1542
74c3337c
PZ
1543 /*
1544 * We can have double attach due to group movement in perf_event_open.
1545 */
1546 if (event->attach_state & PERF_ATTACH_GROUP)
1547 return;
1548
8a49542c
PZ
1549 event->attach_state |= PERF_ATTACH_GROUP;
1550
1551 if (group_leader == event)
1552 return;
1553
652884fe
PZ
1554 WARN_ON_ONCE(group_leader->ctx != event->ctx);
1555
8a49542c
PZ
1556 if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
1557 !is_software_event(event))
1558 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
1559
1560 list_add_tail(&event->group_entry, &group_leader->sibling_list);
1561 group_leader->nr_siblings++;
c320c7b7
ACM
1562
1563 perf_event__header_size(group_leader);
1564
1565 list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
1566 perf_event__header_size(pos);
8a49542c
PZ
1567}
1568
a63eaf34 1569/*
cdd6c482 1570 * Remove a event from the lists for its context.
fccc714b 1571 * Must be called with ctx->mutex and ctx->lock held.
a63eaf34 1572 */
04289bb9 1573static void
cdd6c482 1574list_del_event(struct perf_event *event, struct perf_event_context *ctx)
04289bb9 1575{
68cacd29 1576 struct perf_cpu_context *cpuctx;
652884fe
PZ
1577
1578 WARN_ON_ONCE(event->ctx != ctx);
1579 lockdep_assert_held(&ctx->lock);
1580
8a49542c
PZ
1581 /*
1582 * We can have double detach due to exit/hot-unplug + close.
1583 */
1584 if (!(event->attach_state & PERF_ATTACH_CONTEXT))
a63eaf34 1585 return;
8a49542c
PZ
1586
1587 event->attach_state &= ~PERF_ATTACH_CONTEXT;
1588
68cacd29 1589 if (is_cgroup_event(event)) {
e5d1367f 1590 ctx->nr_cgroups--;
70a01657
PZ
1591 /*
1592 * Because cgroup events are always per-cpu events, this will
1593 * always be called from the right CPU.
1594 */
68cacd29
SE
1595 cpuctx = __get_cpu_context(ctx);
1596 /*
70a01657
PZ
1597 * If there are no more cgroup events then clear cgrp to avoid
1598 * stale pointer in update_cgrp_time_from_cpuctx().
68cacd29
SE
1599 */
1600 if (!ctx->nr_cgroups)
1601 cpuctx->cgrp = NULL;
1602 }
e5d1367f 1603
cdd6c482
IM
1604 ctx->nr_events--;
1605 if (event->attr.inherit_stat)
bfbd3381 1606 ctx->nr_stat--;
8bc20959 1607
cdd6c482 1608 list_del_rcu(&event->event_entry);
04289bb9 1609
8a49542c
PZ
1610 if (event->group_leader == event)
1611 list_del_init(&event->group_entry);
5c148194 1612
96c21a46 1613 update_group_times(event);
b2e74a26
SE
1614
1615 /*
1616 * If event was in error state, then keep it
1617 * that way, otherwise bogus counts will be
1618 * returned on read(). The only way to get out
1619 * of error state is by explicit re-enabling
1620 * of the event
1621 */
1622 if (event->state > PERF_EVENT_STATE_OFF)
1623 event->state = PERF_EVENT_STATE_OFF;
5a3126d4
PZ
1624
1625 ctx->generation++;
050735b0
PZ
1626}
1627
8a49542c 1628static void perf_group_detach(struct perf_event *event)
050735b0
PZ
1629{
1630 struct perf_event *sibling, *tmp;
8a49542c
PZ
1631 struct list_head *list = NULL;
1632
1633 /*
1634 * We can have double detach due to exit/hot-unplug + close.
1635 */
1636 if (!(event->attach_state & PERF_ATTACH_GROUP))
1637 return;
1638
1639 event->attach_state &= ~PERF_ATTACH_GROUP;
1640
1641 /*
1642 * If this is a sibling, remove it from its group.
1643 */
1644 if (event->group_leader != event) {
1645 list_del_init(&event->group_entry);
1646 event->group_leader->nr_siblings--;
c320c7b7 1647 goto out;
8a49542c
PZ
1648 }
1649
1650 if (!list_empty(&event->group_entry))
1651 list = &event->group_entry;
2e2af50b 1652
04289bb9 1653 /*
cdd6c482
IM
1654 * If this was a group event with sibling events then
1655 * upgrade the siblings to singleton events by adding them
8a49542c 1656 * to whatever list we are on.
04289bb9 1657 */
cdd6c482 1658 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
8a49542c
PZ
1659 if (list)
1660 list_move_tail(&sibling->group_entry, list);
04289bb9 1661 sibling->group_leader = sibling;
d6f962b5
FW
1662
1663 /* Inherit group flags from the previous leader */
1664 sibling->group_flags = event->group_flags;
652884fe
PZ
1665
1666 WARN_ON_ONCE(sibling->ctx != event->ctx);
04289bb9 1667 }
c320c7b7
ACM
1668
1669out:
1670 perf_event__header_size(event->group_leader);
1671
1672 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
1673 perf_event__header_size(tmp);
04289bb9
IM
1674}
1675
fadfe7be
JO
1676static bool is_orphaned_event(struct perf_event *event)
1677{
a69b0ca4 1678 return event->state == PERF_EVENT_STATE_DEAD;
fadfe7be
JO
1679}
1680
2c81a647 1681static inline int __pmu_filter_match(struct perf_event *event)
66eb579e
MR
1682{
1683 struct pmu *pmu = event->pmu;
1684 return pmu->filter_match ? pmu->filter_match(event) : 1;
1685}
1686
2c81a647
MR
1687/*
1688 * Check whether we should attempt to schedule an event group based on
1689 * PMU-specific filtering. An event group can consist of HW and SW events,
1690 * potentially with a SW leader, so we must check all the filters, to
1691 * determine whether a group is schedulable:
1692 */
1693static inline int pmu_filter_match(struct perf_event *event)
1694{
1695 struct perf_event *child;
1696
1697 if (!__pmu_filter_match(event))
1698 return 0;
1699
1700 list_for_each_entry(child, &event->sibling_list, group_entry) {
1701 if (!__pmu_filter_match(child))
1702 return 0;
1703 }
1704
1705 return 1;
1706}
1707
fa66f07a
SE
1708static inline int
1709event_filter_match(struct perf_event *event)
1710{
e5d1367f 1711 return (event->cpu == -1 || event->cpu == smp_processor_id())
66eb579e 1712 && perf_cgroup_match(event) && pmu_filter_match(event);
fa66f07a
SE
1713}
1714
9ffcfa6f
SE
1715static void
1716event_sched_out(struct perf_event *event,
3b6f9e5c 1717 struct perf_cpu_context *cpuctx,
cdd6c482 1718 struct perf_event_context *ctx)
3b6f9e5c 1719{
4158755d 1720 u64 tstamp = perf_event_time(event);
fa66f07a 1721 u64 delta;
652884fe
PZ
1722
1723 WARN_ON_ONCE(event->ctx != ctx);
1724 lockdep_assert_held(&ctx->lock);
1725
fa66f07a
SE
1726 /*
1727 * An event which could not be activated because of
1728 * filter mismatch still needs to have its timings
1729 * maintained, otherwise bogus information is return
1730 * via read() for time_enabled, time_running:
1731 */
1732 if (event->state == PERF_EVENT_STATE_INACTIVE
1733 && !event_filter_match(event)) {
e5d1367f 1734 delta = tstamp - event->tstamp_stopped;
fa66f07a 1735 event->tstamp_running += delta;
4158755d 1736 event->tstamp_stopped = tstamp;
fa66f07a
SE
1737 }
1738
cdd6c482 1739 if (event->state != PERF_EVENT_STATE_ACTIVE)
9ffcfa6f 1740 return;
3b6f9e5c 1741
44377277
AS
1742 perf_pmu_disable(event->pmu);
1743
28a967c3
PZ
1744 event->tstamp_stopped = tstamp;
1745 event->pmu->del(event, 0);
1746 event->oncpu = -1;
cdd6c482
IM
1747 event->state = PERF_EVENT_STATE_INACTIVE;
1748 if (event->pending_disable) {
1749 event->pending_disable = 0;
1750 event->state = PERF_EVENT_STATE_OFF;
970892a9 1751 }
3b6f9e5c 1752
cdd6c482 1753 if (!is_software_event(event))
3b6f9e5c 1754 cpuctx->active_oncpu--;
2fde4f94
MR
1755 if (!--ctx->nr_active)
1756 perf_event_ctx_deactivate(ctx);
0f5a2601
PZ
1757 if (event->attr.freq && event->attr.sample_freq)
1758 ctx->nr_freq--;
cdd6c482 1759 if (event->attr.exclusive || !cpuctx->active_oncpu)
3b6f9e5c 1760 cpuctx->exclusive = 0;
44377277
AS
1761
1762 perf_pmu_enable(event->pmu);
3b6f9e5c
PM
1763}
1764
d859e29f 1765static void
cdd6c482 1766group_sched_out(struct perf_event *group_event,
d859e29f 1767 struct perf_cpu_context *cpuctx,
cdd6c482 1768 struct perf_event_context *ctx)
d859e29f 1769{
cdd6c482 1770 struct perf_event *event;
fa66f07a 1771 int state = group_event->state;
d859e29f 1772
cdd6c482 1773 event_sched_out(group_event, cpuctx, ctx);
d859e29f
PM
1774
1775 /*
1776 * Schedule out siblings (if any):
1777 */
cdd6c482
IM
1778 list_for_each_entry(event, &group_event->sibling_list, group_entry)
1779 event_sched_out(event, cpuctx, ctx);
d859e29f 1780
fa66f07a 1781 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
d859e29f
PM
1782 cpuctx->exclusive = 0;
1783}
1784
45a0e07a 1785#define DETACH_GROUP 0x01UL
0017960f 1786
0793a61d 1787/*
cdd6c482 1788 * Cross CPU call to remove a performance event
0793a61d 1789 *
cdd6c482 1790 * We disable the event on the hardware level first. After that we
0793a61d
TG
1791 * remove it from the context list.
1792 */
fae3fde6
PZ
1793static void
1794__perf_remove_from_context(struct perf_event *event,
1795 struct perf_cpu_context *cpuctx,
1796 struct perf_event_context *ctx,
1797 void *info)
0793a61d 1798{
45a0e07a 1799 unsigned long flags = (unsigned long)info;
0793a61d 1800
cdd6c482 1801 event_sched_out(event, cpuctx, ctx);
45a0e07a 1802 if (flags & DETACH_GROUP)
46ce0fe9 1803 perf_group_detach(event);
cdd6c482 1804 list_del_event(event, ctx);
39a43640
PZ
1805
1806 if (!ctx->nr_events && ctx->is_active) {
64ce3126 1807 ctx->is_active = 0;
39a43640
PZ
1808 if (ctx->task) {
1809 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
1810 cpuctx->task_ctx = NULL;
1811 }
64ce3126 1812 }
0793a61d
TG
1813}
1814
0793a61d 1815/*
cdd6c482 1816 * Remove the event from a task's (or a CPU's) list of events.
0793a61d 1817 *
cdd6c482
IM
1818 * If event->ctx is a cloned context, callers must make sure that
1819 * every task struct that event->ctx->task could possibly point to
c93f7669
PM
1820 * remains valid. This is OK when called from perf_release since
1821 * that only calls us on the top-level context, which can't be a clone.
cdd6c482 1822 * When called from perf_event_exit_task, it's OK because the
c93f7669 1823 * context has been detached from its task.
0793a61d 1824 */
45a0e07a 1825static void perf_remove_from_context(struct perf_event *event, unsigned long flags)
0793a61d 1826{
fae3fde6 1827 lockdep_assert_held(&event->ctx->mutex);
0793a61d 1828
45a0e07a 1829 event_function_call(event, __perf_remove_from_context, (void *)flags);
0793a61d
TG
1830}
1831
d859e29f 1832/*
cdd6c482 1833 * Cross CPU call to disable a performance event
d859e29f 1834 */
fae3fde6
PZ
1835static void __perf_event_disable(struct perf_event *event,
1836 struct perf_cpu_context *cpuctx,
1837 struct perf_event_context *ctx,
1838 void *info)
7b648018 1839{
fae3fde6
PZ
1840 if (event->state < PERF_EVENT_STATE_INACTIVE)
1841 return;
7b648018 1842
fae3fde6
PZ
1843 update_context_time(ctx);
1844 update_cgrp_time_from_event(event);
1845 update_group_times(event);
1846 if (event == event->group_leader)
1847 group_sched_out(event, cpuctx, ctx);
1848 else
1849 event_sched_out(event, cpuctx, ctx);
1850 event->state = PERF_EVENT_STATE_OFF;
7b648018
PZ
1851}
1852
d859e29f 1853/*
cdd6c482 1854 * Disable a event.
c93f7669 1855 *
cdd6c482
IM
1856 * If event->ctx is a cloned context, callers must make sure that
1857 * every task struct that event->ctx->task could possibly point to
c93f7669 1858 * remains valid. This condition is satisifed when called through
cdd6c482
IM
1859 * perf_event_for_each_child or perf_event_for_each because they
1860 * hold the top-level event's child_mutex, so any descendant that
8ba289b8
PZ
1861 * goes to exit will block in perf_event_exit_event().
1862 *
cdd6c482 1863 * When called from perf_pending_event it's OK because event->ctx
c93f7669 1864 * is the current context on this CPU and preemption is disabled,
cdd6c482 1865 * hence we can't get into perf_event_task_sched_out for this context.
d859e29f 1866 */
f63a8daa 1867static void _perf_event_disable(struct perf_event *event)
d859e29f 1868{
cdd6c482 1869 struct perf_event_context *ctx = event->ctx;
d859e29f 1870
e625cce1 1871 raw_spin_lock_irq(&ctx->lock);
7b648018 1872 if (event->state <= PERF_EVENT_STATE_OFF) {
e625cce1 1873 raw_spin_unlock_irq(&ctx->lock);
7b648018 1874 return;
53cfbf59 1875 }
e625cce1 1876 raw_spin_unlock_irq(&ctx->lock);
7b648018 1877
fae3fde6
PZ
1878 event_function_call(event, __perf_event_disable, NULL);
1879}
1880
1881void perf_event_disable_local(struct perf_event *event)
1882{
1883 event_function_local(event, __perf_event_disable, NULL);
d859e29f 1884}
f63a8daa
PZ
1885
1886/*
1887 * Strictly speaking kernel users cannot create groups and therefore this
1888 * interface does not need the perf_event_ctx_lock() magic.
1889 */
1890void perf_event_disable(struct perf_event *event)
1891{
1892 struct perf_event_context *ctx;
1893
1894 ctx = perf_event_ctx_lock(event);
1895 _perf_event_disable(event);
1896 perf_event_ctx_unlock(event, ctx);
1897}
dcfce4a0 1898EXPORT_SYMBOL_GPL(perf_event_disable);
d859e29f 1899
e5d1367f
SE
1900static void perf_set_shadow_time(struct perf_event *event,
1901 struct perf_event_context *ctx,
1902 u64 tstamp)
1903{
1904 /*
1905 * use the correct time source for the time snapshot
1906 *
1907 * We could get by without this by leveraging the
1908 * fact that to get to this function, the caller
1909 * has most likely already called update_context_time()
1910 * and update_cgrp_time_xx() and thus both timestamp
1911 * are identical (or very close). Given that tstamp is,
1912 * already adjusted for cgroup, we could say that:
1913 * tstamp - ctx->timestamp
1914 * is equivalent to
1915 * tstamp - cgrp->timestamp.
1916 *
1917 * Then, in perf_output_read(), the calculation would
1918 * work with no changes because:
1919 * - event is guaranteed scheduled in
1920 * - no scheduled out in between
1921 * - thus the timestamp would be the same
1922 *
1923 * But this is a bit hairy.
1924 *
1925 * So instead, we have an explicit cgroup call to remain
1926 * within the time time source all along. We believe it
1927 * is cleaner and simpler to understand.
1928 */
1929 if (is_cgroup_event(event))
1930 perf_cgroup_set_shadow_time(event, tstamp);
1931 else
1932 event->shadow_ctx_time = tstamp - ctx->timestamp;
1933}
1934
4fe757dd
PZ
1935#define MAX_INTERRUPTS (~0ULL)
1936
1937static void perf_log_throttle(struct perf_event *event, int enable);
ec0d7729 1938static void perf_log_itrace_start(struct perf_event *event);
4fe757dd 1939
235c7fc7 1940static int
9ffcfa6f 1941event_sched_in(struct perf_event *event,
235c7fc7 1942 struct perf_cpu_context *cpuctx,
6e37738a 1943 struct perf_event_context *ctx)
235c7fc7 1944{
4158755d 1945 u64 tstamp = perf_event_time(event);
44377277 1946 int ret = 0;
4158755d 1947
63342411
PZ
1948 lockdep_assert_held(&ctx->lock);
1949
cdd6c482 1950 if (event->state <= PERF_EVENT_STATE_OFF)
235c7fc7
IM
1951 return 0;
1952
95ff4ca2
AS
1953 WRITE_ONCE(event->oncpu, smp_processor_id());
1954 /*
1955 * Order event::oncpu write to happen before the ACTIVE state
1956 * is visible.
1957 */
1958 smp_wmb();
1959 WRITE_ONCE(event->state, PERF_EVENT_STATE_ACTIVE);
4fe757dd
PZ
1960
1961 /*
1962 * Unthrottle events, since we scheduled we might have missed several
1963 * ticks already, also for a heavily scheduling task there is little
1964 * guarantee it'll get a tick in a timely manner.
1965 */
1966 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
1967 perf_log_throttle(event, 1);
1968 event->hw.interrupts = 0;
1969 }
1970
235c7fc7
IM
1971 /*
1972 * The new state must be visible before we turn it on in the hardware:
1973 */
1974 smp_wmb();
1975
44377277
AS
1976 perf_pmu_disable(event->pmu);
1977
72f669c0
SL
1978 perf_set_shadow_time(event, ctx, tstamp);
1979
ec0d7729
AS
1980 perf_log_itrace_start(event);
1981
a4eaf7f1 1982 if (event->pmu->add(event, PERF_EF_START)) {
cdd6c482
IM
1983 event->state = PERF_EVENT_STATE_INACTIVE;
1984 event->oncpu = -1;
44377277
AS
1985 ret = -EAGAIN;
1986 goto out;
235c7fc7
IM
1987 }
1988
00a2916f
PZ
1989 event->tstamp_running += tstamp - event->tstamp_stopped;
1990
cdd6c482 1991 if (!is_software_event(event))
3b6f9e5c 1992 cpuctx->active_oncpu++;
2fde4f94
MR
1993 if (!ctx->nr_active++)
1994 perf_event_ctx_activate(ctx);
0f5a2601
PZ
1995 if (event->attr.freq && event->attr.sample_freq)
1996 ctx->nr_freq++;
235c7fc7 1997
cdd6c482 1998 if (event->attr.exclusive)
3b6f9e5c
PM
1999 cpuctx->exclusive = 1;
2000
44377277
AS
2001out:
2002 perf_pmu_enable(event->pmu);
2003
2004 return ret;
235c7fc7
IM
2005}
2006
6751b71e 2007static int
cdd6c482 2008group_sched_in(struct perf_event *group_event,
6751b71e 2009 struct perf_cpu_context *cpuctx,
6e37738a 2010 struct perf_event_context *ctx)
6751b71e 2011{
6bde9b6c 2012 struct perf_event *event, *partial_group = NULL;
4a234593 2013 struct pmu *pmu = ctx->pmu;
d7842da4
SE
2014 u64 now = ctx->time;
2015 bool simulate = false;
6751b71e 2016
cdd6c482 2017 if (group_event->state == PERF_EVENT_STATE_OFF)
6751b71e
PM
2018 return 0;
2019
fbbe0701 2020 pmu->start_txn(pmu, PERF_PMU_TXN_ADD);
6bde9b6c 2021
9ffcfa6f 2022 if (event_sched_in(group_event, cpuctx, ctx)) {
ad5133b7 2023 pmu->cancel_txn(pmu);
272325c4 2024 perf_mux_hrtimer_restart(cpuctx);
6751b71e 2025 return -EAGAIN;
90151c35 2026 }
6751b71e
PM
2027
2028 /*
2029 * Schedule in siblings as one group (if any):
2030 */
cdd6c482 2031 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
9ffcfa6f 2032 if (event_sched_in(event, cpuctx, ctx)) {
cdd6c482 2033 partial_group = event;
6751b71e
PM
2034 goto group_error;
2035 }
2036 }
2037
9ffcfa6f 2038 if (!pmu->commit_txn(pmu))
6e85158c 2039 return 0;
9ffcfa6f 2040
6751b71e
PM
2041group_error:
2042 /*
2043 * Groups can be scheduled in as one unit only, so undo any
2044 * partial group before returning:
d7842da4
SE
2045 * The events up to the failed event are scheduled out normally,
2046 * tstamp_stopped will be updated.
2047 *
2048 * The failed events and the remaining siblings need to have
2049 * their timings updated as if they had gone thru event_sched_in()
2050 * and event_sched_out(). This is required to get consistent timings
2051 * across the group. This also takes care of the case where the group
2052 * could never be scheduled by ensuring tstamp_stopped is set to mark
2053 * the time the event was actually stopped, such that time delta
2054 * calculation in update_event_times() is correct.
6751b71e 2055 */
cdd6c482
IM
2056 list_for_each_entry(event, &group_event->sibling_list, group_entry) {
2057 if (event == partial_group)
d7842da4
SE
2058 simulate = true;
2059
2060 if (simulate) {
2061 event->tstamp_running += now - event->tstamp_stopped;
2062 event->tstamp_stopped = now;
2063 } else {
2064 event_sched_out(event, cpuctx, ctx);
2065 }
6751b71e 2066 }
9ffcfa6f 2067 event_sched_out(group_event, cpuctx, ctx);
6751b71e 2068
ad5133b7 2069 pmu->cancel_txn(pmu);
90151c35 2070
272325c4 2071 perf_mux_hrtimer_restart(cpuctx);
9e630205 2072
6751b71e
PM
2073 return -EAGAIN;
2074}
2075
3b6f9e5c 2076/*
cdd6c482 2077 * Work out whether we can put this event group on the CPU now.
3b6f9e5c 2078 */
cdd6c482 2079static int group_can_go_on(struct perf_event *event,
3b6f9e5c
PM
2080 struct perf_cpu_context *cpuctx,
2081 int can_add_hw)
2082{
2083 /*
cdd6c482 2084 * Groups consisting entirely of software events can always go on.
3b6f9e5c 2085 */
d6f962b5 2086 if (event->group_flags & PERF_GROUP_SOFTWARE)
3b6f9e5c
PM
2087 return 1;
2088 /*
2089 * If an exclusive group is already on, no other hardware
cdd6c482 2090 * events can go on.
3b6f9e5c
PM
2091 */
2092 if (cpuctx->exclusive)
2093 return 0;
2094 /*
2095 * If this group is exclusive and there are already
cdd6c482 2096 * events on the CPU, it can't go on.
3b6f9e5c 2097 */
cdd6c482 2098 if (event->attr.exclusive && cpuctx->active_oncpu)
3b6f9e5c
PM
2099 return 0;
2100 /*
2101 * Otherwise, try to add it if all previous groups were able
2102 * to go on.
2103 */
2104 return can_add_hw;
2105}
2106
cdd6c482
IM
2107static void add_event_to_ctx(struct perf_event *event,
2108 struct perf_event_context *ctx)
53cfbf59 2109{
4158755d
SE
2110 u64 tstamp = perf_event_time(event);
2111
cdd6c482 2112 list_add_event(event, ctx);
8a49542c 2113 perf_group_attach(event);
4158755d
SE
2114 event->tstamp_enabled = tstamp;
2115 event->tstamp_running = tstamp;
2116 event->tstamp_stopped = tstamp;
53cfbf59
PM
2117}
2118
bd2afa49
PZ
2119static void ctx_sched_out(struct perf_event_context *ctx,
2120 struct perf_cpu_context *cpuctx,
2121 enum event_type_t event_type);
2c29ef0f
PZ
2122static void
2123ctx_sched_in(struct perf_event_context *ctx,
2124 struct perf_cpu_context *cpuctx,
2125 enum event_type_t event_type,
2126 struct task_struct *task);
fe4b04fa 2127
bd2afa49
PZ
2128static void task_ctx_sched_out(struct perf_cpu_context *cpuctx,
2129 struct perf_event_context *ctx)
2130{
2131 if (!cpuctx->task_ctx)
2132 return;
2133
2134 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
2135 return;
2136
2137 ctx_sched_out(ctx, cpuctx, EVENT_ALL);
2138}
2139
dce5855b
PZ
2140static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
2141 struct perf_event_context *ctx,
2142 struct task_struct *task)
2143{
2144 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
2145 if (ctx)
2146 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
2147 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
2148 if (ctx)
2149 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
2150}
2151
3e349507
PZ
2152static void ctx_resched(struct perf_cpu_context *cpuctx,
2153 struct perf_event_context *task_ctx)
0017960f 2154{
3e349507
PZ
2155 perf_pmu_disable(cpuctx->ctx.pmu);
2156 if (task_ctx)
2157 task_ctx_sched_out(cpuctx, task_ctx);
2158 cpu_ctx_sched_out(cpuctx, EVENT_ALL);
2159 perf_event_sched_in(cpuctx, task_ctx, current);
2160 perf_pmu_enable(cpuctx->ctx.pmu);
0017960f
PZ
2161}
2162
0793a61d 2163/*
cdd6c482 2164 * Cross CPU call to install and enable a performance event
682076ae 2165 *
a096309b
PZ
2166 * Very similar to remote_function() + event_function() but cannot assume that
2167 * things like ctx->is_active and cpuctx->task_ctx are set.
0793a61d 2168 */
fe4b04fa 2169static int __perf_install_in_context(void *info)
0793a61d 2170{
a096309b
PZ
2171 struct perf_event *event = info;
2172 struct perf_event_context *ctx = event->ctx;
108b02cf 2173 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
2c29ef0f 2174 struct perf_event_context *task_ctx = cpuctx->task_ctx;
a096309b
PZ
2175 bool activate = true;
2176 int ret = 0;
0793a61d 2177
63b6da39 2178 raw_spin_lock(&cpuctx->ctx.lock);
39a43640 2179 if (ctx->task) {
b58f6b0d
PZ
2180 raw_spin_lock(&ctx->lock);
2181 task_ctx = ctx;
a096309b
PZ
2182
2183 /* If we're on the wrong CPU, try again */
2184 if (task_cpu(ctx->task) != smp_processor_id()) {
2185 ret = -ESRCH;
63b6da39 2186 goto unlock;
a096309b 2187 }
b58f6b0d 2188
39a43640 2189 /*
a096309b
PZ
2190 * If we're on the right CPU, see if the task we target is
2191 * current, if not we don't have to activate the ctx, a future
2192 * context switch will do that for us.
39a43640 2193 */
a096309b
PZ
2194 if (ctx->task != current)
2195 activate = false;
2196 else
2197 WARN_ON_ONCE(cpuctx->task_ctx && cpuctx->task_ctx != ctx);
2198
63b6da39
PZ
2199 } else if (task_ctx) {
2200 raw_spin_lock(&task_ctx->lock);
2c29ef0f 2201 }
b58f6b0d 2202
a096309b
PZ
2203 if (activate) {
2204 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2205 add_event_to_ctx(event, ctx);
2206 ctx_resched(cpuctx, task_ctx);
2207 } else {
2208 add_event_to_ctx(event, ctx);
2209 }
2210
63b6da39 2211unlock:
2c29ef0f 2212 perf_ctx_unlock(cpuctx, task_ctx);
fe4b04fa 2213
a096309b 2214 return ret;
0793a61d
TG
2215}
2216
2217/*
a096309b
PZ
2218 * Attach a performance event to a context.
2219 *
2220 * Very similar to event_function_call, see comment there.
0793a61d
TG
2221 */
2222static void
cdd6c482
IM
2223perf_install_in_context(struct perf_event_context *ctx,
2224 struct perf_event *event,
0793a61d
TG
2225 int cpu)
2226{
a096309b 2227 struct task_struct *task = READ_ONCE(ctx->task);
39a43640 2228
fe4b04fa
PZ
2229 lockdep_assert_held(&ctx->mutex);
2230
c3f00c70 2231 event->ctx = ctx;
0cda4c02
YZ
2232 if (event->cpu != -1)
2233 event->cpu = cpu;
c3f00c70 2234
a096309b
PZ
2235 if (!task) {
2236 cpu_function_call(cpu, __perf_install_in_context, event);
2237 return;
2238 }
2239
2240 /*
2241 * Should not happen, we validate the ctx is still alive before calling.
2242 */
2243 if (WARN_ON_ONCE(task == TASK_TOMBSTONE))
2244 return;
2245
39a43640
PZ
2246 /*
2247 * Installing events is tricky because we cannot rely on ctx->is_active
2248 * to be set in case this is the nr_events 0 -> 1 transition.
39a43640 2249 */
a096309b 2250again:
63b6da39 2251 /*
a096309b
PZ
2252 * Cannot use task_function_call() because we need to run on the task's
2253 * CPU regardless of whether its current or not.
63b6da39 2254 */
a096309b
PZ
2255 if (!cpu_function_call(task_cpu(task), __perf_install_in_context, event))
2256 return;
2257
2258 raw_spin_lock_irq(&ctx->lock);
2259 task = ctx->task;
84c4e620 2260 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) {
a096309b
PZ
2261 /*
2262 * Cannot happen because we already checked above (which also
2263 * cannot happen), and we hold ctx->mutex, which serializes us
2264 * against perf_event_exit_task_context().
2265 */
63b6da39
PZ
2266 raw_spin_unlock_irq(&ctx->lock);
2267 return;
2268 }
39a43640 2269 raw_spin_unlock_irq(&ctx->lock);
39a43640 2270 /*
a096309b
PZ
2271 * Since !ctx->is_active doesn't mean anything, we must IPI
2272 * unconditionally.
39a43640 2273 */
a096309b 2274 goto again;
0793a61d
TG
2275}
2276
fa289bec 2277/*
cdd6c482 2278 * Put a event into inactive state and update time fields.
fa289bec
PM
2279 * Enabling the leader of a group effectively enables all
2280 * the group members that aren't explicitly disabled, so we
2281 * have to update their ->tstamp_enabled also.
2282 * Note: this works for group members as well as group leaders
2283 * since the non-leader members' sibling_lists will be empty.
2284 */
1d9b482e 2285static void __perf_event_mark_enabled(struct perf_event *event)
fa289bec 2286{
cdd6c482 2287 struct perf_event *sub;
4158755d 2288 u64 tstamp = perf_event_time(event);
fa289bec 2289
cdd6c482 2290 event->state = PERF_EVENT_STATE_INACTIVE;
4158755d 2291 event->tstamp_enabled = tstamp - event->total_time_enabled;
9ed6060d 2292 list_for_each_entry(sub, &event->sibling_list, group_entry) {
4158755d
SE
2293 if (sub->state >= PERF_EVENT_STATE_INACTIVE)
2294 sub->tstamp_enabled = tstamp - sub->total_time_enabled;
9ed6060d 2295 }
fa289bec
PM
2296}
2297
d859e29f 2298/*
cdd6c482 2299 * Cross CPU call to enable a performance event
d859e29f 2300 */
fae3fde6
PZ
2301static void __perf_event_enable(struct perf_event *event,
2302 struct perf_cpu_context *cpuctx,
2303 struct perf_event_context *ctx,
2304 void *info)
04289bb9 2305{
cdd6c482 2306 struct perf_event *leader = event->group_leader;
fae3fde6 2307 struct perf_event_context *task_ctx;
04289bb9 2308
6e801e01
PZ
2309 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2310 event->state <= PERF_EVENT_STATE_ERROR)
fae3fde6 2311 return;
3cbed429 2312
bd2afa49
PZ
2313 if (ctx->is_active)
2314 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
2315
1d9b482e 2316 __perf_event_mark_enabled(event);
04289bb9 2317
fae3fde6
PZ
2318 if (!ctx->is_active)
2319 return;
2320
e5d1367f 2321 if (!event_filter_match(event)) {
bd2afa49 2322 if (is_cgroup_event(event))
e5d1367f 2323 perf_cgroup_defer_enabled(event);
bd2afa49 2324 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
fae3fde6 2325 return;
e5d1367f 2326 }
f4c4176f 2327
04289bb9 2328 /*
cdd6c482 2329 * If the event is in a group and isn't the group leader,
d859e29f 2330 * then don't put it on unless the group is on.
04289bb9 2331 */
bd2afa49
PZ
2332 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) {
2333 ctx_sched_in(ctx, cpuctx, EVENT_TIME, current);
fae3fde6 2334 return;
bd2afa49 2335 }
fe4b04fa 2336
fae3fde6
PZ
2337 task_ctx = cpuctx->task_ctx;
2338 if (ctx->task)
2339 WARN_ON_ONCE(task_ctx != ctx);
d859e29f 2340
fae3fde6 2341 ctx_resched(cpuctx, task_ctx);
7b648018
PZ
2342}
2343
d859e29f 2344/*
cdd6c482 2345 * Enable a event.
c93f7669 2346 *
cdd6c482
IM
2347 * If event->ctx is a cloned context, callers must make sure that
2348 * every task struct that event->ctx->task could possibly point to
c93f7669 2349 * remains valid. This condition is satisfied when called through
cdd6c482
IM
2350 * perf_event_for_each_child or perf_event_for_each as described
2351 * for perf_event_disable.
d859e29f 2352 */
f63a8daa 2353static void _perf_event_enable(struct perf_event *event)
d859e29f 2354{
cdd6c482 2355 struct perf_event_context *ctx = event->ctx;
d859e29f 2356
7b648018 2357 raw_spin_lock_irq(&ctx->lock);
6e801e01
PZ
2358 if (event->state >= PERF_EVENT_STATE_INACTIVE ||
2359 event->state < PERF_EVENT_STATE_ERROR) {
7b648018 2360 raw_spin_unlock_irq(&ctx->lock);
d859e29f
PM
2361 return;
2362 }
2363
d859e29f 2364 /*
cdd6c482 2365 * If the event is in error state, clear that first.
7b648018
PZ
2366 *
2367 * That way, if we see the event in error state below, we know that it
2368 * has gone back into error state, as distinct from the task having
2369 * been scheduled away before the cross-call arrived.
d859e29f 2370 */
cdd6c482
IM
2371 if (event->state == PERF_EVENT_STATE_ERROR)
2372 event->state = PERF_EVENT_STATE_OFF;
e625cce1 2373 raw_spin_unlock_irq(&ctx->lock);
fe4b04fa 2374
fae3fde6 2375 event_function_call(event, __perf_event_enable, NULL);
d859e29f 2376}
f63a8daa
PZ
2377
2378/*
2379 * See perf_event_disable();
2380 */
2381void perf_event_enable(struct perf_event *event)
2382{
2383 struct perf_event_context *ctx;
2384
2385 ctx = perf_event_ctx_lock(event);
2386 _perf_event_enable(event);
2387 perf_event_ctx_unlock(event, ctx);
2388}
dcfce4a0 2389EXPORT_SYMBOL_GPL(perf_event_enable);
d859e29f 2390
375637bc
AS
2391struct stop_event_data {
2392 struct perf_event *event;
2393 unsigned int restart;
2394};
2395
95ff4ca2
AS
2396static int __perf_event_stop(void *info)
2397{
375637bc
AS
2398 struct stop_event_data *sd = info;
2399 struct perf_event *event = sd->event;
95ff4ca2 2400
375637bc 2401 /* if it's already INACTIVE, do nothing */
95ff4ca2
AS
2402 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2403 return 0;
2404
2405 /* matches smp_wmb() in event_sched_in() */
2406 smp_rmb();
2407
2408 /*
2409 * There is a window with interrupts enabled before we get here,
2410 * so we need to check again lest we try to stop another CPU's event.
2411 */
2412 if (READ_ONCE(event->oncpu) != smp_processor_id())
2413 return -EAGAIN;
2414
2415 event->pmu->stop(event, PERF_EF_UPDATE);
2416
375637bc
AS
2417 /*
2418 * May race with the actual stop (through perf_pmu_output_stop()),
2419 * but it is only used for events with AUX ring buffer, and such
2420 * events will refuse to restart because of rb::aux_mmap_count==0,
2421 * see comments in perf_aux_output_begin().
2422 *
2423 * Since this is happening on a event-local CPU, no trace is lost
2424 * while restarting.
2425 */
2426 if (sd->restart)
2427 event->pmu->start(event, PERF_EF_START);
2428
95ff4ca2
AS
2429 return 0;
2430}
2431
375637bc
AS
2432static int perf_event_restart(struct perf_event *event)
2433{
2434 struct stop_event_data sd = {
2435 .event = event,
2436 .restart = 1,
2437 };
2438 int ret = 0;
2439
2440 do {
2441 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE)
2442 return 0;
2443
2444 /* matches smp_wmb() in event_sched_in() */
2445 smp_rmb();
2446
2447 /*
2448 * We only want to restart ACTIVE events, so if the event goes
2449 * inactive here (event->oncpu==-1), there's nothing more to do;
2450 * fall through with ret==-ENXIO.
2451 */
2452 ret = cpu_function_call(READ_ONCE(event->oncpu),
2453 __perf_event_stop, &sd);
2454 } while (ret == -EAGAIN);
2455
2456 return ret;
2457}
2458
2459/*
2460 * In order to contain the amount of racy and tricky in the address filter
2461 * configuration management, it is a two part process:
2462 *
2463 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below,
2464 * we update the addresses of corresponding vmas in
2465 * event::addr_filters_offs array and bump the event::addr_filters_gen;
2466 * (p2) when an event is scheduled in (pmu::add), it calls
2467 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync()
2468 * if the generation has changed since the previous call.
2469 *
2470 * If (p1) happens while the event is active, we restart it to force (p2).
2471 *
2472 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on
2473 * pre-existing mappings, called once when new filters arrive via SET_FILTER
2474 * ioctl;
2475 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly
2476 * registered mapping, called for every new mmap(), with mm::mmap_sem down
2477 * for reading;
2478 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process
2479 * of exec.
2480 */
2481void perf_event_addr_filters_sync(struct perf_event *event)
2482{
2483 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
2484
2485 if (!has_addr_filter(event))
2486 return;
2487
2488 raw_spin_lock(&ifh->lock);
2489 if (event->addr_filters_gen != event->hw.addr_filters_gen) {
2490 event->pmu->addr_filters_sync(event);
2491 event->hw.addr_filters_gen = event->addr_filters_gen;
2492 }
2493 raw_spin_unlock(&ifh->lock);
2494}
2495EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync);
2496
f63a8daa 2497static int _perf_event_refresh(struct perf_event *event, int refresh)
79f14641 2498{
2023b359 2499 /*
cdd6c482 2500 * not supported on inherited events
2023b359 2501 */
2e939d1d 2502 if (event->attr.inherit || !is_sampling_event(event))
2023b359
PZ
2503 return -EINVAL;
2504
cdd6c482 2505 atomic_add(refresh, &event->event_limit);
f63a8daa 2506 _perf_event_enable(event);
2023b359
PZ
2507
2508 return 0;
79f14641 2509}
f63a8daa
PZ
2510
2511/*
2512 * See perf_event_disable()
2513 */
2514int perf_event_refresh(struct perf_event *event, int refresh)
2515{
2516 struct perf_event_context *ctx;
2517 int ret;
2518
2519 ctx = perf_event_ctx_lock(event);
2520 ret = _perf_event_refresh(event, refresh);
2521 perf_event_ctx_unlock(event, ctx);
2522
2523 return ret;
2524}
26ca5c11 2525EXPORT_SYMBOL_GPL(perf_event_refresh);
79f14641 2526
5b0311e1
FW
2527static void ctx_sched_out(struct perf_event_context *ctx,
2528 struct perf_cpu_context *cpuctx,
2529 enum event_type_t event_type)
235c7fc7 2530{
db24d33e 2531 int is_active = ctx->is_active;
c994d613 2532 struct perf_event *event;
235c7fc7 2533
c994d613 2534 lockdep_assert_held(&ctx->lock);
235c7fc7 2535
39a43640
PZ
2536 if (likely(!ctx->nr_events)) {
2537 /*
2538 * See __perf_remove_from_context().
2539 */
2540 WARN_ON_ONCE(ctx->is_active);
2541 if (ctx->task)
2542 WARN_ON_ONCE(cpuctx->task_ctx);
facc4307 2543 return;
39a43640
PZ
2544 }
2545
db24d33e 2546 ctx->is_active &= ~event_type;
3cbaa590
PZ
2547 if (!(ctx->is_active & EVENT_ALL))
2548 ctx->is_active = 0;
2549
63e30d3e
PZ
2550 if (ctx->task) {
2551 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2552 if (!ctx->is_active)
2553 cpuctx->task_ctx = NULL;
2554 }
facc4307 2555
8fdc6539
PZ
2556 /*
2557 * Always update time if it was set; not only when it changes.
2558 * Otherwise we can 'forget' to update time for any but the last
2559 * context we sched out. For example:
2560 *
2561 * ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2562 * ctx_sched_out(.event_type = EVENT_PINNED)
2563 *
2564 * would only update time for the pinned events.
2565 */
3cbaa590
PZ
2566 if (is_active & EVENT_TIME) {
2567 /* update (and stop) ctx time */
2568 update_context_time(ctx);
2569 update_cgrp_time_from_cpuctx(cpuctx);
2570 }
2571
8fdc6539
PZ
2572 is_active ^= ctx->is_active; /* changed bits */
2573
3cbaa590 2574 if (!ctx->nr_active || !(is_active & EVENT_ALL))
facc4307 2575 return;
5b0311e1 2576
075e0b00 2577 perf_pmu_disable(ctx->pmu);
3cbaa590 2578 if (is_active & EVENT_PINNED) {
889ff015
FW
2579 list_for_each_entry(event, &ctx->pinned_groups, group_entry)
2580 group_sched_out(event, cpuctx, ctx);
9ed6060d 2581 }
889ff015 2582
3cbaa590 2583 if (is_active & EVENT_FLEXIBLE) {
889ff015 2584 list_for_each_entry(event, &ctx->flexible_groups, group_entry)
8c9ed8e1 2585 group_sched_out(event, cpuctx, ctx);
9ed6060d 2586 }
1b9a644f 2587 perf_pmu_enable(ctx->pmu);
235c7fc7
IM
2588}
2589
564c2b21 2590/*
5a3126d4
PZ
2591 * Test whether two contexts are equivalent, i.e. whether they have both been
2592 * cloned from the same version of the same context.
2593 *
2594 * Equivalence is measured using a generation number in the context that is
2595 * incremented on each modification to it; see unclone_ctx(), list_add_event()
2596 * and list_del_event().
564c2b21 2597 */
cdd6c482
IM
2598static int context_equiv(struct perf_event_context *ctx1,
2599 struct perf_event_context *ctx2)
564c2b21 2600{
211de6eb
PZ
2601 lockdep_assert_held(&ctx1->lock);
2602 lockdep_assert_held(&ctx2->lock);
2603
5a3126d4
PZ
2604 /* Pinning disables the swap optimization */
2605 if (ctx1->pin_count || ctx2->pin_count)
2606 return 0;
2607
2608 /* If ctx1 is the parent of ctx2 */
2609 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
2610 return 1;
2611
2612 /* If ctx2 is the parent of ctx1 */
2613 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
2614 return 1;
2615
2616 /*
2617 * If ctx1 and ctx2 have the same parent; we flatten the parent
2618 * hierarchy, see perf_event_init_context().
2619 */
2620 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
2621 ctx1->parent_gen == ctx2->parent_gen)
2622 return 1;
2623
2624 /* Unmatched */
2625 return 0;
564c2b21
PM
2626}
2627
cdd6c482
IM
2628static void __perf_event_sync_stat(struct perf_event *event,
2629 struct perf_event *next_event)
bfbd3381
PZ
2630{
2631 u64 value;
2632
cdd6c482 2633 if (!event->attr.inherit_stat)
bfbd3381
PZ
2634 return;
2635
2636 /*
cdd6c482 2637 * Update the event value, we cannot use perf_event_read()
bfbd3381
PZ
2638 * because we're in the middle of a context switch and have IRQs
2639 * disabled, which upsets smp_call_function_single(), however
cdd6c482 2640 * we know the event must be on the current CPU, therefore we
bfbd3381
PZ
2641 * don't need to use it.
2642 */
cdd6c482
IM
2643 switch (event->state) {
2644 case PERF_EVENT_STATE_ACTIVE:
3dbebf15
PZ
2645 event->pmu->read(event);
2646 /* fall-through */
bfbd3381 2647
cdd6c482
IM
2648 case PERF_EVENT_STATE_INACTIVE:
2649 update_event_times(event);
bfbd3381
PZ
2650 break;
2651
2652 default:
2653 break;
2654 }
2655
2656 /*
cdd6c482 2657 * In order to keep per-task stats reliable we need to flip the event
bfbd3381
PZ
2658 * values when we flip the contexts.
2659 */
e7850595
PZ
2660 value = local64_read(&next_event->count);
2661 value = local64_xchg(&event->count, value);
2662 local64_set(&next_event->count, value);
bfbd3381 2663
cdd6c482
IM
2664 swap(event->total_time_enabled, next_event->total_time_enabled);
2665 swap(event->total_time_running, next_event->total_time_running);
19d2e755 2666
bfbd3381 2667 /*
19d2e755 2668 * Since we swizzled the values, update the user visible data too.
bfbd3381 2669 */
cdd6c482
IM
2670 perf_event_update_userpage(event);
2671 perf_event_update_userpage(next_event);
bfbd3381
PZ
2672}
2673
cdd6c482
IM
2674static void perf_event_sync_stat(struct perf_event_context *ctx,
2675 struct perf_event_context *next_ctx)
bfbd3381 2676{
cdd6c482 2677 struct perf_event *event, *next_event;
bfbd3381
PZ
2678
2679 if (!ctx->nr_stat)
2680 return;
2681
02ffdbc8
PZ
2682 update_context_time(ctx);
2683
cdd6c482
IM
2684 event = list_first_entry(&ctx->event_list,
2685 struct perf_event, event_entry);
bfbd3381 2686
cdd6c482
IM
2687 next_event = list_first_entry(&next_ctx->event_list,
2688 struct perf_event, event_entry);
bfbd3381 2689
cdd6c482
IM
2690 while (&event->event_entry != &ctx->event_list &&
2691 &next_event->event_entry != &next_ctx->event_list) {
bfbd3381 2692
cdd6c482 2693 __perf_event_sync_stat(event, next_event);
bfbd3381 2694
cdd6c482
IM
2695 event = list_next_entry(event, event_entry);
2696 next_event = list_next_entry(next_event, event_entry);
bfbd3381
PZ
2697 }
2698}
2699
fe4b04fa
PZ
2700static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
2701 struct task_struct *next)
0793a61d 2702{
8dc85d54 2703 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
cdd6c482 2704 struct perf_event_context *next_ctx;
5a3126d4 2705 struct perf_event_context *parent, *next_parent;
108b02cf 2706 struct perf_cpu_context *cpuctx;
c93f7669 2707 int do_switch = 1;
0793a61d 2708
108b02cf
PZ
2709 if (likely(!ctx))
2710 return;
10989fb2 2711
108b02cf
PZ
2712 cpuctx = __get_cpu_context(ctx);
2713 if (!cpuctx->task_ctx)
0793a61d
TG
2714 return;
2715
c93f7669 2716 rcu_read_lock();
8dc85d54 2717 next_ctx = next->perf_event_ctxp[ctxn];
5a3126d4
PZ
2718 if (!next_ctx)
2719 goto unlock;
2720
2721 parent = rcu_dereference(ctx->parent_ctx);
2722 next_parent = rcu_dereference(next_ctx->parent_ctx);
2723
2724 /* If neither context have a parent context; they cannot be clones. */
802c8a61 2725 if (!parent && !next_parent)
5a3126d4
PZ
2726 goto unlock;
2727
2728 if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
c93f7669
PM
2729 /*
2730 * Looks like the two contexts are clones, so we might be
2731 * able to optimize the context switch. We lock both
2732 * contexts and check that they are clones under the
2733 * lock (including re-checking that neither has been
2734 * uncloned in the meantime). It doesn't matter which
2735 * order we take the locks because no other cpu could
2736 * be trying to lock both of these tasks.
2737 */
e625cce1
TG
2738 raw_spin_lock(&ctx->lock);
2739 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
c93f7669 2740 if (context_equiv(ctx, next_ctx)) {
63b6da39
PZ
2741 WRITE_ONCE(ctx->task, next);
2742 WRITE_ONCE(next_ctx->task, task);
5a158c3c
YZ
2743
2744 swap(ctx->task_ctx_data, next_ctx->task_ctx_data);
2745
63b6da39
PZ
2746 /*
2747 * RCU_INIT_POINTER here is safe because we've not
2748 * modified the ctx and the above modification of
2749 * ctx->task and ctx->task_ctx_data are immaterial
2750 * since those values are always verified under
2751 * ctx->lock which we're now holding.
2752 */
2753 RCU_INIT_POINTER(task->perf_event_ctxp[ctxn], next_ctx);
2754 RCU_INIT_POINTER(next->perf_event_ctxp[ctxn], ctx);
2755
c93f7669 2756 do_switch = 0;
bfbd3381 2757
cdd6c482 2758 perf_event_sync_stat(ctx, next_ctx);
c93f7669 2759 }
e625cce1
TG
2760 raw_spin_unlock(&next_ctx->lock);
2761 raw_spin_unlock(&ctx->lock);
564c2b21 2762 }
5a3126d4 2763unlock:
c93f7669 2764 rcu_read_unlock();
564c2b21 2765
c93f7669 2766 if (do_switch) {
facc4307 2767 raw_spin_lock(&ctx->lock);
8833d0e2 2768 task_ctx_sched_out(cpuctx, ctx);
facc4307 2769 raw_spin_unlock(&ctx->lock);
c93f7669 2770 }
0793a61d
TG
2771}
2772
ba532500
YZ
2773void perf_sched_cb_dec(struct pmu *pmu)
2774{
2775 this_cpu_dec(perf_sched_cb_usages);
2776}
2777
2778void perf_sched_cb_inc(struct pmu *pmu)
2779{
2780 this_cpu_inc(perf_sched_cb_usages);
2781}
2782
2783/*
2784 * This function provides the context switch callback to the lower code
2785 * layer. It is invoked ONLY when the context switch callback is enabled.
2786 */
2787static void perf_pmu_sched_task(struct task_struct *prev,
2788 struct task_struct *next,
2789 bool sched_in)
2790{
2791 struct perf_cpu_context *cpuctx;
2792 struct pmu *pmu;
2793 unsigned long flags;
2794
2795 if (prev == next)
2796 return;
2797
2798 local_irq_save(flags);
2799
2800 rcu_read_lock();
2801
2802 list_for_each_entry_rcu(pmu, &pmus, entry) {
2803 if (pmu->sched_task) {
2804 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
2805
2806 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
2807
2808 perf_pmu_disable(pmu);
2809
2810 pmu->sched_task(cpuctx->task_ctx, sched_in);
2811
2812 perf_pmu_enable(pmu);
2813
2814 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
2815 }
2816 }
2817
2818 rcu_read_unlock();
2819
2820 local_irq_restore(flags);
2821}
2822
45ac1403
AH
2823static void perf_event_switch(struct task_struct *task,
2824 struct task_struct *next_prev, bool sched_in);
2825
8dc85d54
PZ
2826#define for_each_task_context_nr(ctxn) \
2827 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
2828
2829/*
2830 * Called from scheduler to remove the events of the current task,
2831 * with interrupts disabled.
2832 *
2833 * We stop each event and update the event value in event->count.
2834 *
2835 * This does not protect us against NMI, but disable()
2836 * sets the disabled bit in the control field of event _before_
2837 * accessing the event control register. If a NMI hits, then it will
2838 * not restart the event.
2839 */
ab0cce56
JO
2840void __perf_event_task_sched_out(struct task_struct *task,
2841 struct task_struct *next)
8dc85d54
PZ
2842{
2843 int ctxn;
2844
ba532500
YZ
2845 if (__this_cpu_read(perf_sched_cb_usages))
2846 perf_pmu_sched_task(task, next, false);
2847
45ac1403
AH
2848 if (atomic_read(&nr_switch_events))
2849 perf_event_switch(task, next, false);
2850
8dc85d54
PZ
2851 for_each_task_context_nr(ctxn)
2852 perf_event_context_sched_out(task, ctxn, next);
e5d1367f
SE
2853
2854 /*
2855 * if cgroup events exist on this CPU, then we need
2856 * to check if we have to switch out PMU state.
2857 * cgroup event are system-wide mode only
2858 */
4a32fea9 2859 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
a8d757ef 2860 perf_cgroup_sched_out(task, next);
8dc85d54
PZ
2861}
2862
5b0311e1
FW
2863/*
2864 * Called with IRQs disabled
2865 */
2866static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
2867 enum event_type_t event_type)
2868{
2869 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
04289bb9
IM
2870}
2871
235c7fc7 2872static void
5b0311e1 2873ctx_pinned_sched_in(struct perf_event_context *ctx,
6e37738a 2874 struct perf_cpu_context *cpuctx)
0793a61d 2875{
cdd6c482 2876 struct perf_event *event;
0793a61d 2877
889ff015
FW
2878 list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
2879 if (event->state <= PERF_EVENT_STATE_OFF)
3b6f9e5c 2880 continue;
5632ab12 2881 if (!event_filter_match(event))
3b6f9e5c
PM
2882 continue;
2883
e5d1367f
SE
2884 /* may need to reset tstamp_enabled */
2885 if (is_cgroup_event(event))
2886 perf_cgroup_mark_enabled(event, ctx);
2887
8c9ed8e1 2888 if (group_can_go_on(event, cpuctx, 1))
6e37738a 2889 group_sched_in(event, cpuctx, ctx);
3b6f9e5c
PM
2890
2891 /*
2892 * If this pinned group hasn't been scheduled,
2893 * put it in error state.
2894 */
cdd6c482
IM
2895 if (event->state == PERF_EVENT_STATE_INACTIVE) {
2896 update_group_times(event);
2897 event->state = PERF_EVENT_STATE_ERROR;
53cfbf59 2898 }
3b6f9e5c 2899 }
5b0311e1
FW
2900}
2901
2902static void
2903ctx_flexible_sched_in(struct perf_event_context *ctx,
6e37738a 2904 struct perf_cpu_context *cpuctx)
5b0311e1
FW
2905{
2906 struct perf_event *event;
2907 int can_add_hw = 1;
3b6f9e5c 2908
889ff015
FW
2909 list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
2910 /* Ignore events in OFF or ERROR state */
2911 if (event->state <= PERF_EVENT_STATE_OFF)
3b6f9e5c 2912 continue;
04289bb9
IM
2913 /*
2914 * Listen to the 'cpu' scheduling filter constraint
cdd6c482 2915 * of events:
04289bb9 2916 */
5632ab12 2917 if (!event_filter_match(event))
0793a61d
TG
2918 continue;
2919
e5d1367f
SE
2920 /* may need to reset tstamp_enabled */
2921 if (is_cgroup_event(event))
2922 perf_cgroup_mark_enabled(event, ctx);
2923
9ed6060d 2924 if (group_can_go_on(event, cpuctx, can_add_hw)) {
6e37738a 2925 if (group_sched_in(event, cpuctx, ctx))
dd0e6ba2 2926 can_add_hw = 0;
9ed6060d 2927 }
0793a61d 2928 }
5b0311e1
FW
2929}
2930
2931static void
2932ctx_sched_in(struct perf_event_context *ctx,
2933 struct perf_cpu_context *cpuctx,
e5d1367f
SE
2934 enum event_type_t event_type,
2935 struct task_struct *task)
5b0311e1 2936{
db24d33e 2937 int is_active = ctx->is_active;
c994d613
PZ
2938 u64 now;
2939
2940 lockdep_assert_held(&ctx->lock);
e5d1367f 2941
5b0311e1 2942 if (likely(!ctx->nr_events))
facc4307 2943 return;
5b0311e1 2944
3cbaa590 2945 ctx->is_active |= (event_type | EVENT_TIME);
63e30d3e
PZ
2946 if (ctx->task) {
2947 if (!is_active)
2948 cpuctx->task_ctx = ctx;
2949 else
2950 WARN_ON_ONCE(cpuctx->task_ctx != ctx);
2951 }
2952
3cbaa590
PZ
2953 is_active ^= ctx->is_active; /* changed bits */
2954
2955 if (is_active & EVENT_TIME) {
2956 /* start ctx time */
2957 now = perf_clock();
2958 ctx->timestamp = now;
2959 perf_cgroup_set_timestamp(task, ctx);
2960 }
2961
5b0311e1
FW
2962 /*
2963 * First go through the list and put on any pinned groups
2964 * in order to give them the best chance of going on.
2965 */
3cbaa590 2966 if (is_active & EVENT_PINNED)
6e37738a 2967 ctx_pinned_sched_in(ctx, cpuctx);
5b0311e1
FW
2968
2969 /* Then walk through the lower prio flexible groups */
3cbaa590 2970 if (is_active & EVENT_FLEXIBLE)
6e37738a 2971 ctx_flexible_sched_in(ctx, cpuctx);
235c7fc7
IM
2972}
2973
329c0e01 2974static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
e5d1367f
SE
2975 enum event_type_t event_type,
2976 struct task_struct *task)
329c0e01
FW
2977{
2978 struct perf_event_context *ctx = &cpuctx->ctx;
2979
e5d1367f 2980 ctx_sched_in(ctx, cpuctx, event_type, task);
329c0e01
FW
2981}
2982
e5d1367f
SE
2983static void perf_event_context_sched_in(struct perf_event_context *ctx,
2984 struct task_struct *task)
235c7fc7 2985{
108b02cf 2986 struct perf_cpu_context *cpuctx;
235c7fc7 2987
108b02cf 2988 cpuctx = __get_cpu_context(ctx);
329c0e01
FW
2989 if (cpuctx->task_ctx == ctx)
2990 return;
2991
facc4307 2992 perf_ctx_lock(cpuctx, ctx);
1b9a644f 2993 perf_pmu_disable(ctx->pmu);
329c0e01
FW
2994 /*
2995 * We want to keep the following priority order:
2996 * cpu pinned (that don't need to move), task pinned,
2997 * cpu flexible, task flexible.
2998 */
2999 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
63e30d3e 3000 perf_event_sched_in(cpuctx, ctx, task);
facc4307
PZ
3001 perf_pmu_enable(ctx->pmu);
3002 perf_ctx_unlock(cpuctx, ctx);
235c7fc7
IM
3003}
3004
8dc85d54
PZ
3005/*
3006 * Called from scheduler to add the events of the current task
3007 * with interrupts disabled.
3008 *
3009 * We restore the event value and then enable it.
3010 *
3011 * This does not protect us against NMI, but enable()
3012 * sets the enabled bit in the control field of event _before_
3013 * accessing the event control register. If a NMI hits, then it will
3014 * keep the event running.
3015 */
ab0cce56
JO
3016void __perf_event_task_sched_in(struct task_struct *prev,
3017 struct task_struct *task)
8dc85d54
PZ
3018{
3019 struct perf_event_context *ctx;
3020 int ctxn;
3021
7e41d177
PZ
3022 /*
3023 * If cgroup events exist on this CPU, then we need to check if we have
3024 * to switch in PMU state; cgroup event are system-wide mode only.
3025 *
3026 * Since cgroup events are CPU events, we must schedule these in before
3027 * we schedule in the task events.
3028 */
3029 if (atomic_read(this_cpu_ptr(&perf_cgroup_events)))
3030 perf_cgroup_sched_in(prev, task);
3031
8dc85d54
PZ
3032 for_each_task_context_nr(ctxn) {
3033 ctx = task->perf_event_ctxp[ctxn];
3034 if (likely(!ctx))
3035 continue;
3036
e5d1367f 3037 perf_event_context_sched_in(ctx, task);
8dc85d54 3038 }
d010b332 3039
45ac1403
AH
3040 if (atomic_read(&nr_switch_events))
3041 perf_event_switch(task, prev, true);
3042
ba532500
YZ
3043 if (__this_cpu_read(perf_sched_cb_usages))
3044 perf_pmu_sched_task(prev, task, true);
235c7fc7
IM
3045}
3046
abd50713
PZ
3047static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
3048{
3049 u64 frequency = event->attr.sample_freq;
3050 u64 sec = NSEC_PER_SEC;
3051 u64 divisor, dividend;
3052
3053 int count_fls, nsec_fls, frequency_fls, sec_fls;
3054
3055 count_fls = fls64(count);
3056 nsec_fls = fls64(nsec);
3057 frequency_fls = fls64(frequency);
3058 sec_fls = 30;
3059
3060 /*
3061 * We got @count in @nsec, with a target of sample_freq HZ
3062 * the target period becomes:
3063 *
3064 * @count * 10^9
3065 * period = -------------------
3066 * @nsec * sample_freq
3067 *
3068 */
3069
3070 /*
3071 * Reduce accuracy by one bit such that @a and @b converge
3072 * to a similar magnitude.
3073 */
fe4b04fa 3074#define REDUCE_FLS(a, b) \
abd50713
PZ
3075do { \
3076 if (a##_fls > b##_fls) { \
3077 a >>= 1; \
3078 a##_fls--; \
3079 } else { \
3080 b >>= 1; \
3081 b##_fls--; \
3082 } \
3083} while (0)
3084
3085 /*
3086 * Reduce accuracy until either term fits in a u64, then proceed with
3087 * the other, so that finally we can do a u64/u64 division.
3088 */
3089 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
3090 REDUCE_FLS(nsec, frequency);
3091 REDUCE_FLS(sec, count);
3092 }
3093
3094 if (count_fls + sec_fls > 64) {
3095 divisor = nsec * frequency;
3096
3097 while (count_fls + sec_fls > 64) {
3098 REDUCE_FLS(count, sec);
3099 divisor >>= 1;
3100 }
3101
3102 dividend = count * sec;
3103 } else {
3104 dividend = count * sec;
3105
3106 while (nsec_fls + frequency_fls > 64) {
3107 REDUCE_FLS(nsec, frequency);
3108 dividend >>= 1;
3109 }
3110
3111 divisor = nsec * frequency;
3112 }
3113
f6ab91ad
PZ
3114 if (!divisor)
3115 return dividend;
3116
abd50713
PZ
3117 return div64_u64(dividend, divisor);
3118}
3119
e050e3f0
SE
3120static DEFINE_PER_CPU(int, perf_throttled_count);
3121static DEFINE_PER_CPU(u64, perf_throttled_seq);
3122
f39d47ff 3123static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
bd2b5b12 3124{
cdd6c482 3125 struct hw_perf_event *hwc = &event->hw;
f6ab91ad 3126 s64 period, sample_period;
bd2b5b12
PZ
3127 s64 delta;
3128
abd50713 3129 period = perf_calculate_period(event, nsec, count);
bd2b5b12
PZ
3130
3131 delta = (s64)(period - hwc->sample_period);
3132 delta = (delta + 7) / 8; /* low pass filter */
3133
3134 sample_period = hwc->sample_period + delta;
3135
3136 if (!sample_period)
3137 sample_period = 1;
3138
bd2b5b12 3139 hwc->sample_period = sample_period;
abd50713 3140
e7850595 3141 if (local64_read(&hwc->period_left) > 8*sample_period) {
f39d47ff
SE
3142 if (disable)
3143 event->pmu->stop(event, PERF_EF_UPDATE);
3144
e7850595 3145 local64_set(&hwc->period_left, 0);
f39d47ff
SE
3146
3147 if (disable)
3148 event->pmu->start(event, PERF_EF_RELOAD);
abd50713 3149 }
bd2b5b12
PZ
3150}
3151
e050e3f0
SE
3152/*
3153 * combine freq adjustment with unthrottling to avoid two passes over the
3154 * events. At the same time, make sure, having freq events does not change
3155 * the rate of unthrottling as that would introduce bias.
3156 */
3157static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
3158 int needs_unthr)
60db5e09 3159{
cdd6c482
IM
3160 struct perf_event *event;
3161 struct hw_perf_event *hwc;
e050e3f0 3162 u64 now, period = TICK_NSEC;
abd50713 3163 s64 delta;
60db5e09 3164
e050e3f0
SE
3165 /*
3166 * only need to iterate over all events iff:
3167 * - context have events in frequency mode (needs freq adjust)
3168 * - there are events to unthrottle on this cpu
3169 */
3170 if (!(ctx->nr_freq || needs_unthr))
0f5a2601
PZ
3171 return;
3172
e050e3f0 3173 raw_spin_lock(&ctx->lock);
f39d47ff 3174 perf_pmu_disable(ctx->pmu);
e050e3f0 3175
03541f8b 3176 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
cdd6c482 3177 if (event->state != PERF_EVENT_STATE_ACTIVE)
60db5e09
PZ
3178 continue;
3179
5632ab12 3180 if (!event_filter_match(event))
5d27c23d
PZ
3181 continue;
3182
44377277
AS
3183 perf_pmu_disable(event->pmu);
3184
cdd6c482 3185 hwc = &event->hw;
6a24ed6c 3186
ae23bff1 3187 if (hwc->interrupts == MAX_INTERRUPTS) {
e050e3f0 3188 hwc->interrupts = 0;
cdd6c482 3189 perf_log_throttle(event, 1);
a4eaf7f1 3190 event->pmu->start(event, 0);
a78ac325
PZ
3191 }
3192
cdd6c482 3193 if (!event->attr.freq || !event->attr.sample_freq)
44377277 3194 goto next;
60db5e09 3195
e050e3f0
SE
3196 /*
3197 * stop the event and update event->count
3198 */
3199 event->pmu->stop(event, PERF_EF_UPDATE);
3200
e7850595 3201 now = local64_read(&event->count);
abd50713
PZ
3202 delta = now - hwc->freq_count_stamp;
3203 hwc->freq_count_stamp = now;
60db5e09 3204
e050e3f0
SE
3205 /*
3206 * restart the event
3207 * reload only if value has changed
f39d47ff
SE
3208 * we have stopped the event so tell that
3209 * to perf_adjust_period() to avoid stopping it
3210 * twice.
e050e3f0 3211 */
abd50713 3212 if (delta > 0)
f39d47ff 3213 perf_adjust_period(event, period, delta, false);
e050e3f0
SE
3214
3215 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
44377277
AS
3216 next:
3217 perf_pmu_enable(event->pmu);
60db5e09 3218 }
e050e3f0 3219
f39d47ff 3220 perf_pmu_enable(ctx->pmu);
e050e3f0 3221 raw_spin_unlock(&ctx->lock);
60db5e09
PZ
3222}
3223
235c7fc7 3224/*
cdd6c482 3225 * Round-robin a context's events:
235c7fc7 3226 */
cdd6c482 3227static void rotate_ctx(struct perf_event_context *ctx)
0793a61d 3228{
dddd3379
TG
3229 /*
3230 * Rotate the first entry last of non-pinned groups. Rotation might be
3231 * disabled by the inheritance code.
3232 */
3233 if (!ctx->rotate_disable)
3234 list_rotate_left(&ctx->flexible_groups);
235c7fc7
IM
3235}
3236
9e630205 3237static int perf_rotate_context(struct perf_cpu_context *cpuctx)
235c7fc7 3238{
8dc85d54 3239 struct perf_event_context *ctx = NULL;
2fde4f94 3240 int rotate = 0;
7fc23a53 3241
b5ab4cd5 3242 if (cpuctx->ctx.nr_events) {
b5ab4cd5
PZ
3243 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
3244 rotate = 1;
3245 }
235c7fc7 3246
8dc85d54 3247 ctx = cpuctx->task_ctx;
b5ab4cd5 3248 if (ctx && ctx->nr_events) {
b5ab4cd5
PZ
3249 if (ctx->nr_events != ctx->nr_active)
3250 rotate = 1;
3251 }
9717e6cd 3252
e050e3f0 3253 if (!rotate)
0f5a2601
PZ
3254 goto done;
3255
facc4307 3256 perf_ctx_lock(cpuctx, cpuctx->task_ctx);
1b9a644f 3257 perf_pmu_disable(cpuctx->ctx.pmu);
60db5e09 3258
e050e3f0
SE
3259 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
3260 if (ctx)
3261 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
0793a61d 3262
e050e3f0
SE
3263 rotate_ctx(&cpuctx->ctx);
3264 if (ctx)
3265 rotate_ctx(ctx);
235c7fc7 3266
e050e3f0 3267 perf_event_sched_in(cpuctx, ctx, current);
235c7fc7 3268
0f5a2601
PZ
3269 perf_pmu_enable(cpuctx->ctx.pmu);
3270 perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
b5ab4cd5 3271done:
9e630205
SE
3272
3273 return rotate;
e9d2b064
PZ
3274}
3275
3276void perf_event_task_tick(void)
3277{
2fde4f94
MR
3278 struct list_head *head = this_cpu_ptr(&active_ctx_list);
3279 struct perf_event_context *ctx, *tmp;
e050e3f0 3280 int throttled;
b5ab4cd5 3281
e9d2b064
PZ
3282 WARN_ON(!irqs_disabled());
3283
e050e3f0
SE
3284 __this_cpu_inc(perf_throttled_seq);
3285 throttled = __this_cpu_xchg(perf_throttled_count, 0);
555e0c1e 3286 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
e050e3f0 3287
2fde4f94 3288 list_for_each_entry_safe(ctx, tmp, head, active_ctx_list)
e050e3f0 3289 perf_adjust_freq_unthr_context(ctx, throttled);
0793a61d
TG
3290}
3291
889ff015
FW
3292static int event_enable_on_exec(struct perf_event *event,
3293 struct perf_event_context *ctx)
3294{
3295 if (!event->attr.enable_on_exec)
3296 return 0;
3297
3298 event->attr.enable_on_exec = 0;
3299 if (event->state >= PERF_EVENT_STATE_INACTIVE)
3300 return 0;
3301
1d9b482e 3302 __perf_event_mark_enabled(event);
889ff015
FW
3303
3304 return 1;
3305}
3306
57e7986e 3307/*
cdd6c482 3308 * Enable all of a task's events that have been marked enable-on-exec.
57e7986e
PM
3309 * This expects task == current.
3310 */
c1274499 3311static void perf_event_enable_on_exec(int ctxn)
57e7986e 3312{
c1274499 3313 struct perf_event_context *ctx, *clone_ctx = NULL;
3e349507 3314 struct perf_cpu_context *cpuctx;
cdd6c482 3315 struct perf_event *event;
57e7986e
PM
3316 unsigned long flags;
3317 int enabled = 0;
3318
3319 local_irq_save(flags);
c1274499 3320 ctx = current->perf_event_ctxp[ctxn];
cdd6c482 3321 if (!ctx || !ctx->nr_events)
57e7986e
PM
3322 goto out;
3323
3e349507
PZ
3324 cpuctx = __get_cpu_context(ctx);
3325 perf_ctx_lock(cpuctx, ctx);
7fce2509 3326 ctx_sched_out(ctx, cpuctx, EVENT_TIME);
3e349507
PZ
3327 list_for_each_entry(event, &ctx->event_list, event_entry)
3328 enabled |= event_enable_on_exec(event, ctx);
57e7986e
PM
3329
3330 /*
3e349507 3331 * Unclone and reschedule this context if we enabled any event.
57e7986e 3332 */
3e349507 3333 if (enabled) {
211de6eb 3334 clone_ctx = unclone_ctx(ctx);
3e349507
PZ
3335 ctx_resched(cpuctx, ctx);
3336 }
3337 perf_ctx_unlock(cpuctx, ctx);
57e7986e 3338
9ed6060d 3339out:
57e7986e 3340 local_irq_restore(flags);
211de6eb
PZ
3341
3342 if (clone_ctx)
3343 put_ctx(clone_ctx);
57e7986e
PM
3344}
3345
0492d4c5
PZ
3346struct perf_read_data {
3347 struct perf_event *event;
3348 bool group;
7d88962e 3349 int ret;
0492d4c5
PZ
3350};
3351
0793a61d 3352/*
cdd6c482 3353 * Cross CPU call to read the hardware event
0793a61d 3354 */
cdd6c482 3355static void __perf_event_read(void *info)
0793a61d 3356{
0492d4c5
PZ
3357 struct perf_read_data *data = info;
3358 struct perf_event *sub, *event = data->event;
cdd6c482 3359 struct perf_event_context *ctx = event->ctx;
108b02cf 3360 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
4a00c16e 3361 struct pmu *pmu = event->pmu;
621a01ea 3362
e1ac3614
PM
3363 /*
3364 * If this is a task context, we need to check whether it is
3365 * the current task context of this cpu. If not it has been
3366 * scheduled out before the smp call arrived. In that case
cdd6c482
IM
3367 * event->count would have been updated to a recent sample
3368 * when the event was scheduled out.
e1ac3614
PM
3369 */
3370 if (ctx->task && cpuctx->task_ctx != ctx)
3371 return;
3372
e625cce1 3373 raw_spin_lock(&ctx->lock);
e5d1367f 3374 if (ctx->is_active) {
542e72fc 3375 update_context_time(ctx);
e5d1367f
SE
3376 update_cgrp_time_from_event(event);
3377 }
0492d4c5 3378
cdd6c482 3379 update_event_times(event);
4a00c16e
SB
3380 if (event->state != PERF_EVENT_STATE_ACTIVE)
3381 goto unlock;
0492d4c5 3382
4a00c16e
SB
3383 if (!data->group) {
3384 pmu->read(event);
3385 data->ret = 0;
0492d4c5 3386 goto unlock;
4a00c16e
SB
3387 }
3388
3389 pmu->start_txn(pmu, PERF_PMU_TXN_READ);
3390
3391 pmu->read(event);
0492d4c5
PZ
3392
3393 list_for_each_entry(sub, &event->sibling_list, group_entry) {
3394 update_event_times(sub);
4a00c16e
SB
3395 if (sub->state == PERF_EVENT_STATE_ACTIVE) {
3396 /*
3397 * Use sibling's PMU rather than @event's since
3398 * sibling could be on different (eg: software) PMU.
3399 */
0492d4c5 3400 sub->pmu->read(sub);
4a00c16e 3401 }
0492d4c5 3402 }
4a00c16e
SB
3403
3404 data->ret = pmu->commit_txn(pmu);
0492d4c5
PZ
3405
3406unlock:
e625cce1 3407 raw_spin_unlock(&ctx->lock);
0793a61d
TG
3408}
3409
b5e58793
PZ
3410static inline u64 perf_event_count(struct perf_event *event)
3411{
eacd3ecc
MF
3412 if (event->pmu->count)
3413 return event->pmu->count(event);
3414
3415 return __perf_event_count(event);
b5e58793
PZ
3416}
3417
ffe8690c
KX
3418/*
3419 * NMI-safe method to read a local event, that is an event that
3420 * is:
3421 * - either for the current task, or for this CPU
3422 * - does not have inherit set, for inherited task events
3423 * will not be local and we cannot read them atomically
3424 * - must not have a pmu::count method
3425 */
3426u64 perf_event_read_local(struct perf_event *event)
3427{
3428 unsigned long flags;
3429 u64 val;
3430
3431 /*
3432 * Disabling interrupts avoids all counter scheduling (context
3433 * switches, timer based rotation and IPIs).
3434 */
3435 local_irq_save(flags);
3436
3437 /* If this is a per-task event, it must be for current */
3438 WARN_ON_ONCE((event->attach_state & PERF_ATTACH_TASK) &&
3439 event->hw.target != current);
3440
3441 /* If this is a per-CPU event, it must be for this CPU */
3442 WARN_ON_ONCE(!(event->attach_state & PERF_ATTACH_TASK) &&
3443 event->cpu != smp_processor_id());
3444
3445 /*
3446 * It must not be an event with inherit set, we cannot read
3447 * all child counters from atomic context.
3448 */
3449 WARN_ON_ONCE(event->attr.inherit);
3450
3451 /*
3452 * It must not have a pmu::count method, those are not
3453 * NMI safe.
3454 */
3455 WARN_ON_ONCE(event->pmu->count);
3456
3457 /*
3458 * If the event is currently on this CPU, its either a per-task event,
3459 * or local to this CPU. Furthermore it means its ACTIVE (otherwise
3460 * oncpu == -1).
3461 */
3462 if (event->oncpu == smp_processor_id())
3463 event->pmu->read(event);
3464
3465 val = local64_read(&event->count);
3466 local_irq_restore(flags);
3467
3468 return val;
3469}
3470
7d88962e 3471static int perf_event_read(struct perf_event *event, bool group)
0793a61d 3472{
7d88962e
SB
3473 int ret = 0;
3474
0793a61d 3475 /*
cdd6c482
IM
3476 * If event is enabled and currently active on a CPU, update the
3477 * value in the event structure:
0793a61d 3478 */
cdd6c482 3479 if (event->state == PERF_EVENT_STATE_ACTIVE) {
0492d4c5
PZ
3480 struct perf_read_data data = {
3481 .event = event,
3482 .group = group,
7d88962e 3483 .ret = 0,
0492d4c5 3484 };
cdd6c482 3485 smp_call_function_single(event->oncpu,
0492d4c5 3486 __perf_event_read, &data, 1);
7d88962e 3487 ret = data.ret;
cdd6c482 3488 } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
2b8988c9
PZ
3489 struct perf_event_context *ctx = event->ctx;
3490 unsigned long flags;
3491
e625cce1 3492 raw_spin_lock_irqsave(&ctx->lock, flags);
c530ccd9
SE
3493 /*
3494 * may read while context is not active
3495 * (e.g., thread is blocked), in that case
3496 * we cannot update context time
3497 */
e5d1367f 3498 if (ctx->is_active) {
c530ccd9 3499 update_context_time(ctx);
e5d1367f
SE
3500 update_cgrp_time_from_event(event);
3501 }
0492d4c5
PZ
3502 if (group)
3503 update_group_times(event);
3504 else
3505 update_event_times(event);
e625cce1 3506 raw_spin_unlock_irqrestore(&ctx->lock, flags);
0793a61d 3507 }
7d88962e
SB
3508
3509 return ret;
0793a61d
TG
3510}
3511
a63eaf34 3512/*
cdd6c482 3513 * Initialize the perf_event context in a task_struct:
a63eaf34 3514 */
eb184479 3515static void __perf_event_init_context(struct perf_event_context *ctx)
a63eaf34 3516{
e625cce1 3517 raw_spin_lock_init(&ctx->lock);
a63eaf34 3518 mutex_init(&ctx->mutex);
2fde4f94 3519 INIT_LIST_HEAD(&ctx->active_ctx_list);
889ff015
FW
3520 INIT_LIST_HEAD(&ctx->pinned_groups);
3521 INIT_LIST_HEAD(&ctx->flexible_groups);
a63eaf34
PM
3522 INIT_LIST_HEAD(&ctx->event_list);
3523 atomic_set(&ctx->refcount, 1);
eb184479
PZ
3524}
3525
3526static struct perf_event_context *
3527alloc_perf_context(struct pmu *pmu, struct task_struct *task)
3528{
3529 struct perf_event_context *ctx;
3530
3531 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
3532 if (!ctx)
3533 return NULL;
3534
3535 __perf_event_init_context(ctx);
3536 if (task) {
3537 ctx->task = task;
3538 get_task_struct(task);
0793a61d 3539 }
eb184479
PZ
3540 ctx->pmu = pmu;
3541
3542 return ctx;
a63eaf34
PM
3543}
3544
2ebd4ffb
MH
3545static struct task_struct *
3546find_lively_task_by_vpid(pid_t vpid)
3547{
3548 struct task_struct *task;
0793a61d
TG
3549
3550 rcu_read_lock();
2ebd4ffb 3551 if (!vpid)
0793a61d
TG
3552 task = current;
3553 else
2ebd4ffb 3554 task = find_task_by_vpid(vpid);
0793a61d
TG
3555 if (task)
3556 get_task_struct(task);
3557 rcu_read_unlock();
3558
3559 if (!task)
3560 return ERR_PTR(-ESRCH);
3561
2ebd4ffb 3562 return task;
2ebd4ffb
MH
3563}
3564
fe4b04fa
PZ
3565/*
3566 * Returns a matching context with refcount and pincount.
3567 */
108b02cf 3568static struct perf_event_context *
4af57ef2
YZ
3569find_get_context(struct pmu *pmu, struct task_struct *task,
3570 struct perf_event *event)
0793a61d 3571{
211de6eb 3572 struct perf_event_context *ctx, *clone_ctx = NULL;
22a4f650 3573 struct perf_cpu_context *cpuctx;
4af57ef2 3574 void *task_ctx_data = NULL;
25346b93 3575 unsigned long flags;
8dc85d54 3576 int ctxn, err;
4af57ef2 3577 int cpu = event->cpu;
0793a61d 3578
22a4ec72 3579 if (!task) {
cdd6c482 3580 /* Must be root to operate on a CPU event: */
0764771d 3581 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
0793a61d
TG
3582 return ERR_PTR(-EACCES);
3583
0793a61d 3584 /*
cdd6c482 3585 * We could be clever and allow to attach a event to an
0793a61d
TG
3586 * offline CPU and activate it when the CPU comes up, but
3587 * that's for later.
3588 */
f6325e30 3589 if (!cpu_online(cpu))
0793a61d
TG
3590 return ERR_PTR(-ENODEV);
3591
108b02cf 3592 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
0793a61d 3593 ctx = &cpuctx->ctx;
c93f7669 3594 get_ctx(ctx);
fe4b04fa 3595 ++ctx->pin_count;
0793a61d 3596
0793a61d
TG
3597 return ctx;
3598 }
3599
8dc85d54
PZ
3600 err = -EINVAL;
3601 ctxn = pmu->task_ctx_nr;
3602 if (ctxn < 0)
3603 goto errout;
3604
4af57ef2
YZ
3605 if (event->attach_state & PERF_ATTACH_TASK_DATA) {
3606 task_ctx_data = kzalloc(pmu->task_ctx_size, GFP_KERNEL);
3607 if (!task_ctx_data) {
3608 err = -ENOMEM;
3609 goto errout;
3610 }
3611 }
3612
9ed6060d 3613retry:
8dc85d54 3614 ctx = perf_lock_task_context(task, ctxn, &flags);
c93f7669 3615 if (ctx) {
211de6eb 3616 clone_ctx = unclone_ctx(ctx);
fe4b04fa 3617 ++ctx->pin_count;
4af57ef2
YZ
3618
3619 if (task_ctx_data && !ctx->task_ctx_data) {
3620 ctx->task_ctx_data = task_ctx_data;
3621 task_ctx_data = NULL;
3622 }
e625cce1 3623 raw_spin_unlock_irqrestore(&ctx->lock, flags);
211de6eb
PZ
3624
3625 if (clone_ctx)
3626 put_ctx(clone_ctx);
9137fb28 3627 } else {
eb184479 3628 ctx = alloc_perf_context(pmu, task);
c93f7669
PM
3629 err = -ENOMEM;
3630 if (!ctx)
3631 goto errout;
eb184479 3632
4af57ef2
YZ
3633 if (task_ctx_data) {
3634 ctx->task_ctx_data = task_ctx_data;
3635 task_ctx_data = NULL;
3636 }
3637
dbe08d82
ON
3638 err = 0;
3639 mutex_lock(&task->perf_event_mutex);
3640 /*
3641 * If it has already passed perf_event_exit_task().
3642 * we must see PF_EXITING, it takes this mutex too.
3643 */
3644 if (task->flags & PF_EXITING)
3645 err = -ESRCH;
3646 else if (task->perf_event_ctxp[ctxn])
3647 err = -EAGAIN;
fe4b04fa 3648 else {
9137fb28 3649 get_ctx(ctx);
fe4b04fa 3650 ++ctx->pin_count;
dbe08d82 3651 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
fe4b04fa 3652 }
dbe08d82
ON
3653 mutex_unlock(&task->perf_event_mutex);
3654
3655 if (unlikely(err)) {
9137fb28 3656 put_ctx(ctx);
dbe08d82
ON
3657
3658 if (err == -EAGAIN)
3659 goto retry;
3660 goto errout;
a63eaf34
PM
3661 }
3662 }
3663
4af57ef2 3664 kfree(task_ctx_data);
0793a61d 3665 return ctx;
c93f7669 3666
9ed6060d 3667errout:
4af57ef2 3668 kfree(task_ctx_data);
c93f7669 3669 return ERR_PTR(err);
0793a61d
TG
3670}
3671
6fb2915d 3672static void perf_event_free_filter(struct perf_event *event);
2541517c 3673static void perf_event_free_bpf_prog(struct perf_event *event);
6fb2915d 3674
cdd6c482 3675static void free_event_rcu(struct rcu_head *head)
592903cd 3676{
cdd6c482 3677 struct perf_event *event;
592903cd 3678
cdd6c482
IM
3679 event = container_of(head, struct perf_event, rcu_head);
3680 if (event->ns)
3681 put_pid_ns(event->ns);
6fb2915d 3682 perf_event_free_filter(event);
cdd6c482 3683 kfree(event);
592903cd
PZ
3684}
3685
b69cf536
PZ
3686static void ring_buffer_attach(struct perf_event *event,
3687 struct ring_buffer *rb);
925d519a 3688
4beb31f3 3689static void unaccount_event_cpu(struct perf_event *event, int cpu)
f1600952 3690{
4beb31f3
FW
3691 if (event->parent)
3692 return;
3693
4beb31f3
FW
3694 if (is_cgroup_event(event))
3695 atomic_dec(&per_cpu(perf_cgroup_events, cpu));
3696}
925d519a 3697
555e0c1e
FW
3698#ifdef CONFIG_NO_HZ_FULL
3699static DEFINE_SPINLOCK(nr_freq_lock);
3700#endif
3701
3702static void unaccount_freq_event_nohz(void)
3703{
3704#ifdef CONFIG_NO_HZ_FULL
3705 spin_lock(&nr_freq_lock);
3706 if (atomic_dec_and_test(&nr_freq_events))
3707 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS);
3708 spin_unlock(&nr_freq_lock);
3709#endif
3710}
3711
3712static void unaccount_freq_event(void)
3713{
3714 if (tick_nohz_full_enabled())
3715 unaccount_freq_event_nohz();
3716 else
3717 atomic_dec(&nr_freq_events);
3718}
3719
4beb31f3
FW
3720static void unaccount_event(struct perf_event *event)
3721{
25432ae9
PZ
3722 bool dec = false;
3723
4beb31f3
FW
3724 if (event->parent)
3725 return;
3726
3727 if (event->attach_state & PERF_ATTACH_TASK)
25432ae9 3728 dec = true;
4beb31f3
FW
3729 if (event->attr.mmap || event->attr.mmap_data)
3730 atomic_dec(&nr_mmap_events);
3731 if (event->attr.comm)
3732 atomic_dec(&nr_comm_events);
3733 if (event->attr.task)
3734 atomic_dec(&nr_task_events);
948b26b6 3735 if (event->attr.freq)
555e0c1e 3736 unaccount_freq_event();
45ac1403 3737 if (event->attr.context_switch) {
25432ae9 3738 dec = true;
45ac1403
AH
3739 atomic_dec(&nr_switch_events);
3740 }
4beb31f3 3741 if (is_cgroup_event(event))
25432ae9 3742 dec = true;
4beb31f3 3743 if (has_branch_stack(event))
25432ae9
PZ
3744 dec = true;
3745
9107c89e
PZ
3746 if (dec) {
3747 if (!atomic_add_unless(&perf_sched_count, -1, 1))
3748 schedule_delayed_work(&perf_sched_work, HZ);
3749 }
4beb31f3
FW
3750
3751 unaccount_event_cpu(event, event->cpu);
3752}
925d519a 3753
9107c89e
PZ
3754static void perf_sched_delayed(struct work_struct *work)
3755{
3756 mutex_lock(&perf_sched_mutex);
3757 if (atomic_dec_and_test(&perf_sched_count))
3758 static_branch_disable(&perf_sched_events);
3759 mutex_unlock(&perf_sched_mutex);
3760}
3761
bed5b25a
AS
3762/*
3763 * The following implement mutual exclusion of events on "exclusive" pmus
3764 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled
3765 * at a time, so we disallow creating events that might conflict, namely:
3766 *
3767 * 1) cpu-wide events in the presence of per-task events,
3768 * 2) per-task events in the presence of cpu-wide events,
3769 * 3) two matching events on the same context.
3770 *
3771 * The former two cases are handled in the allocation path (perf_event_alloc(),
a0733e69 3772 * _free_event()), the latter -- before the first perf_install_in_context().
bed5b25a
AS
3773 */
3774static int exclusive_event_init(struct perf_event *event)
3775{
3776 struct pmu *pmu = event->pmu;
3777
3778 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3779 return 0;
3780
3781 /*
3782 * Prevent co-existence of per-task and cpu-wide events on the
3783 * same exclusive pmu.
3784 *
3785 * Negative pmu::exclusive_cnt means there are cpu-wide
3786 * events on this "exclusive" pmu, positive means there are
3787 * per-task events.
3788 *
3789 * Since this is called in perf_event_alloc() path, event::ctx
3790 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK
3791 * to mean "per-task event", because unlike other attach states it
3792 * never gets cleared.
3793 */
3794 if (event->attach_state & PERF_ATTACH_TASK) {
3795 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt))
3796 return -EBUSY;
3797 } else {
3798 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt))
3799 return -EBUSY;
3800 }
3801
3802 return 0;
3803}
3804
3805static void exclusive_event_destroy(struct perf_event *event)
3806{
3807 struct pmu *pmu = event->pmu;
3808
3809 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3810 return;
3811
3812 /* see comment in exclusive_event_init() */
3813 if (event->attach_state & PERF_ATTACH_TASK)
3814 atomic_dec(&pmu->exclusive_cnt);
3815 else
3816 atomic_inc(&pmu->exclusive_cnt);
3817}
3818
3819static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2)
3820{
3821 if ((e1->pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) &&
3822 (e1->cpu == e2->cpu ||
3823 e1->cpu == -1 ||
3824 e2->cpu == -1))
3825 return true;
3826 return false;
3827}
3828
3829/* Called under the same ctx::mutex as perf_install_in_context() */
3830static bool exclusive_event_installable(struct perf_event *event,
3831 struct perf_event_context *ctx)
3832{
3833 struct perf_event *iter_event;
3834 struct pmu *pmu = event->pmu;
3835
3836 if (!(pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE))
3837 return true;
3838
3839 list_for_each_entry(iter_event, &ctx->event_list, event_entry) {
3840 if (exclusive_event_match(iter_event, event))
3841 return false;
3842 }
3843
3844 return true;
3845}
3846
375637bc
AS
3847static void perf_addr_filters_splice(struct perf_event *event,
3848 struct list_head *head);
3849
683ede43 3850static void _free_event(struct perf_event *event)
f1600952 3851{
e360adbe 3852 irq_work_sync(&event->pending);
925d519a 3853
4beb31f3 3854 unaccount_event(event);
9ee318a7 3855
76369139 3856 if (event->rb) {
9bb5d40c
PZ
3857 /*
3858 * Can happen when we close an event with re-directed output.
3859 *
3860 * Since we have a 0 refcount, perf_mmap_close() will skip
3861 * over us; possibly making our ring_buffer_put() the last.
3862 */
3863 mutex_lock(&event->mmap_mutex);
b69cf536 3864 ring_buffer_attach(event, NULL);
9bb5d40c 3865 mutex_unlock(&event->mmap_mutex);
a4be7c27
PZ
3866 }
3867
e5d1367f
SE
3868 if (is_cgroup_event(event))
3869 perf_detach_cgroup(event);
3870
a0733e69
PZ
3871 if (!event->parent) {
3872 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
3873 put_callchain_buffers();
3874 }
3875
3876 perf_event_free_bpf_prog(event);
375637bc
AS
3877 perf_addr_filters_splice(event, NULL);
3878 kfree(event->addr_filters_offs);
a0733e69
PZ
3879
3880 if (event->destroy)
3881 event->destroy(event);
3882
3883 if (event->ctx)
3884 put_ctx(event->ctx);
3885
62a92c8f
AS
3886 exclusive_event_destroy(event);
3887 module_put(event->pmu->module);
a0733e69
PZ
3888
3889 call_rcu(&event->rcu_head, free_event_rcu);
f1600952
PZ
3890}
3891
683ede43
PZ
3892/*
3893 * Used to free events which have a known refcount of 1, such as in error paths
3894 * where the event isn't exposed yet and inherited events.
3895 */
3896static void free_event(struct perf_event *event)
0793a61d 3897{
683ede43
PZ
3898 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
3899 "unexpected event refcount: %ld; ptr=%p\n",
3900 atomic_long_read(&event->refcount), event)) {
3901 /* leak to avoid use-after-free */
3902 return;
3903 }
0793a61d 3904
683ede43 3905 _free_event(event);
0793a61d
TG
3906}
3907
a66a3052 3908/*
f8697762 3909 * Remove user event from the owner task.
a66a3052 3910 */
f8697762 3911static void perf_remove_from_owner(struct perf_event *event)
fb0459d7 3912{
8882135b 3913 struct task_struct *owner;
fb0459d7 3914
8882135b 3915 rcu_read_lock();
8882135b 3916 /*
f47c02c0
PZ
3917 * Matches the smp_store_release() in perf_event_exit_task(). If we
3918 * observe !owner it means the list deletion is complete and we can
3919 * indeed free this event, otherwise we need to serialize on
8882135b
PZ
3920 * owner->perf_event_mutex.
3921 */
f47c02c0 3922 owner = lockless_dereference(event->owner);
8882135b
PZ
3923 if (owner) {
3924 /*
3925 * Since delayed_put_task_struct() also drops the last
3926 * task reference we can safely take a new reference
3927 * while holding the rcu_read_lock().
3928 */
3929 get_task_struct(owner);
3930 }
3931 rcu_read_unlock();
3932
3933 if (owner) {
f63a8daa
PZ
3934 /*
3935 * If we're here through perf_event_exit_task() we're already
3936 * holding ctx->mutex which would be an inversion wrt. the
3937 * normal lock order.
3938 *
3939 * However we can safely take this lock because its the child
3940 * ctx->mutex.
3941 */
3942 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING);
3943
8882135b
PZ
3944 /*
3945 * We have to re-check the event->owner field, if it is cleared
3946 * we raced with perf_event_exit_task(), acquiring the mutex
3947 * ensured they're done, and we can proceed with freeing the
3948 * event.
3949 */
f47c02c0 3950 if (event->owner) {
8882135b 3951 list_del_init(&event->owner_entry);
f47c02c0
PZ
3952 smp_store_release(&event->owner, NULL);
3953 }
8882135b
PZ
3954 mutex_unlock(&owner->perf_event_mutex);
3955 put_task_struct(owner);
3956 }
f8697762
JO
3957}
3958
f8697762
JO
3959static void put_event(struct perf_event *event)
3960{
f8697762
JO
3961 if (!atomic_long_dec_and_test(&event->refcount))
3962 return;
3963
c6e5b732
PZ
3964 _free_event(event);
3965}
3966
3967/*
3968 * Kill an event dead; while event:refcount will preserve the event
3969 * object, it will not preserve its functionality. Once the last 'user'
3970 * gives up the object, we'll destroy the thing.
3971 */
3972int perf_event_release_kernel(struct perf_event *event)
3973{
a4f4bb6d 3974 struct perf_event_context *ctx = event->ctx;
c6e5b732
PZ
3975 struct perf_event *child, *tmp;
3976
a4f4bb6d
PZ
3977 /*
3978 * If we got here through err_file: fput(event_file); we will not have
3979 * attached to a context yet.
3980 */
3981 if (!ctx) {
3982 WARN_ON_ONCE(event->attach_state &
3983 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP));
3984 goto no_ctx;
3985 }
3986
f8697762
JO
3987 if (!is_kernel_event(event))
3988 perf_remove_from_owner(event);
8882135b 3989
5fa7c8ec 3990 ctx = perf_event_ctx_lock(event);
a83fe28e 3991 WARN_ON_ONCE(ctx->parent_ctx);
a69b0ca4 3992 perf_remove_from_context(event, DETACH_GROUP);
683ede43 3993
a69b0ca4 3994 raw_spin_lock_irq(&ctx->lock);
683ede43 3995 /*
a69b0ca4
PZ
3996 * Mark this even as STATE_DEAD, there is no external reference to it
3997 * anymore.
683ede43 3998 *
a69b0ca4
PZ
3999 * Anybody acquiring event->child_mutex after the below loop _must_
4000 * also see this, most importantly inherit_event() which will avoid
4001 * placing more children on the list.
683ede43 4002 *
c6e5b732
PZ
4003 * Thus this guarantees that we will in fact observe and kill _ALL_
4004 * child events.
683ede43 4005 */
a69b0ca4
PZ
4006 event->state = PERF_EVENT_STATE_DEAD;
4007 raw_spin_unlock_irq(&ctx->lock);
4008
4009 perf_event_ctx_unlock(event, ctx);
683ede43 4010
c6e5b732
PZ
4011again:
4012 mutex_lock(&event->child_mutex);
4013 list_for_each_entry(child, &event->child_list, child_list) {
a6fa941d 4014
c6e5b732
PZ
4015 /*
4016 * Cannot change, child events are not migrated, see the
4017 * comment with perf_event_ctx_lock_nested().
4018 */
4019 ctx = lockless_dereference(child->ctx);
4020 /*
4021 * Since child_mutex nests inside ctx::mutex, we must jump
4022 * through hoops. We start by grabbing a reference on the ctx.
4023 *
4024 * Since the event cannot get freed while we hold the
4025 * child_mutex, the context must also exist and have a !0
4026 * reference count.
4027 */
4028 get_ctx(ctx);
4029
4030 /*
4031 * Now that we have a ctx ref, we can drop child_mutex, and
4032 * acquire ctx::mutex without fear of it going away. Then we
4033 * can re-acquire child_mutex.
4034 */
4035 mutex_unlock(&event->child_mutex);
4036 mutex_lock(&ctx->mutex);
4037 mutex_lock(&event->child_mutex);
4038
4039 /*
4040 * Now that we hold ctx::mutex and child_mutex, revalidate our
4041 * state, if child is still the first entry, it didn't get freed
4042 * and we can continue doing so.
4043 */
4044 tmp = list_first_entry_or_null(&event->child_list,
4045 struct perf_event, child_list);
4046 if (tmp == child) {
4047 perf_remove_from_context(child, DETACH_GROUP);
4048 list_del(&child->child_list);
4049 free_event(child);
4050 /*
4051 * This matches the refcount bump in inherit_event();
4052 * this can't be the last reference.
4053 */
4054 put_event(event);
4055 }
4056
4057 mutex_unlock(&event->child_mutex);
4058 mutex_unlock(&ctx->mutex);
4059 put_ctx(ctx);
4060 goto again;
4061 }
4062 mutex_unlock(&event->child_mutex);
4063
a4f4bb6d
PZ
4064no_ctx:
4065 put_event(event); /* Must be the 'last' reference */
683ede43
PZ
4066 return 0;
4067}
4068EXPORT_SYMBOL_GPL(perf_event_release_kernel);
4069
8b10c5e2
PZ
4070/*
4071 * Called when the last reference to the file is gone.
4072 */
a6fa941d
AV
4073static int perf_release(struct inode *inode, struct file *file)
4074{
c6e5b732 4075 perf_event_release_kernel(file->private_data);
a6fa941d 4076 return 0;
fb0459d7 4077}
fb0459d7 4078
59ed446f 4079u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
e53c0994 4080{
cdd6c482 4081 struct perf_event *child;
e53c0994
PZ
4082 u64 total = 0;
4083
59ed446f
PZ
4084 *enabled = 0;
4085 *running = 0;
4086
6f10581a 4087 mutex_lock(&event->child_mutex);
01add3ea 4088
7d88962e 4089 (void)perf_event_read(event, false);
01add3ea
SB
4090 total += perf_event_count(event);
4091
59ed446f
PZ
4092 *enabled += event->total_time_enabled +
4093 atomic64_read(&event->child_total_time_enabled);
4094 *running += event->total_time_running +
4095 atomic64_read(&event->child_total_time_running);
4096
4097 list_for_each_entry(child, &event->child_list, child_list) {
7d88962e 4098 (void)perf_event_read(child, false);
01add3ea 4099 total += perf_event_count(child);
59ed446f
PZ
4100 *enabled += child->total_time_enabled;
4101 *running += child->total_time_running;
4102 }
6f10581a 4103 mutex_unlock(&event->child_mutex);
e53c0994
PZ
4104
4105 return total;
4106}
fb0459d7 4107EXPORT_SYMBOL_GPL(perf_event_read_value);
e53c0994 4108
7d88962e 4109static int __perf_read_group_add(struct perf_event *leader,
fa8c2693 4110 u64 read_format, u64 *values)
3dab77fb 4111{
fa8c2693
PZ
4112 struct perf_event *sub;
4113 int n = 1; /* skip @nr */
7d88962e 4114 int ret;
f63a8daa 4115
7d88962e
SB
4116 ret = perf_event_read(leader, true);
4117 if (ret)
4118 return ret;
abf4868b 4119
fa8c2693
PZ
4120 /*
4121 * Since we co-schedule groups, {enabled,running} times of siblings
4122 * will be identical to those of the leader, so we only publish one
4123 * set.
4124 */
4125 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
4126 values[n++] += leader->total_time_enabled +
4127 atomic64_read(&leader->child_total_time_enabled);
4128 }
3dab77fb 4129
fa8c2693
PZ
4130 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
4131 values[n++] += leader->total_time_running +
4132 atomic64_read(&leader->child_total_time_running);
4133 }
4134
4135 /*
4136 * Write {count,id} tuples for every sibling.
4137 */
4138 values[n++] += perf_event_count(leader);
abf4868b
PZ
4139 if (read_format & PERF_FORMAT_ID)
4140 values[n++] = primary_event_id(leader);
3dab77fb 4141
fa8c2693
PZ
4142 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
4143 values[n++] += perf_event_count(sub);
4144 if (read_format & PERF_FORMAT_ID)
4145 values[n++] = primary_event_id(sub);
4146 }
7d88962e
SB
4147
4148 return 0;
fa8c2693 4149}
3dab77fb 4150
fa8c2693
PZ
4151static int perf_read_group(struct perf_event *event,
4152 u64 read_format, char __user *buf)
4153{
4154 struct perf_event *leader = event->group_leader, *child;
4155 struct perf_event_context *ctx = leader->ctx;
7d88962e 4156 int ret;
fa8c2693 4157 u64 *values;
3dab77fb 4158
fa8c2693 4159 lockdep_assert_held(&ctx->mutex);
3dab77fb 4160
fa8c2693
PZ
4161 values = kzalloc(event->read_size, GFP_KERNEL);
4162 if (!values)
4163 return -ENOMEM;
3dab77fb 4164
fa8c2693
PZ
4165 values[0] = 1 + leader->nr_siblings;
4166
4167 /*
4168 * By locking the child_mutex of the leader we effectively
4169 * lock the child list of all siblings.. XXX explain how.
4170 */
4171 mutex_lock(&leader->child_mutex);
abf4868b 4172
7d88962e
SB
4173 ret = __perf_read_group_add(leader, read_format, values);
4174 if (ret)
4175 goto unlock;
4176
4177 list_for_each_entry(child, &leader->child_list, child_list) {
4178 ret = __perf_read_group_add(child, read_format, values);
4179 if (ret)
4180 goto unlock;
4181 }
abf4868b 4182
fa8c2693 4183 mutex_unlock(&leader->child_mutex);
abf4868b 4184
7d88962e 4185 ret = event->read_size;
fa8c2693
PZ
4186 if (copy_to_user(buf, values, event->read_size))
4187 ret = -EFAULT;
7d88962e 4188 goto out;
fa8c2693 4189
7d88962e
SB
4190unlock:
4191 mutex_unlock(&leader->child_mutex);
4192out:
fa8c2693 4193 kfree(values);
abf4868b 4194 return ret;
3dab77fb
PZ
4195}
4196
b15f495b 4197static int perf_read_one(struct perf_event *event,
3dab77fb
PZ
4198 u64 read_format, char __user *buf)
4199{
59ed446f 4200 u64 enabled, running;
3dab77fb
PZ
4201 u64 values[4];
4202 int n = 0;
4203
59ed446f
PZ
4204 values[n++] = perf_event_read_value(event, &enabled, &running);
4205 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
4206 values[n++] = enabled;
4207 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
4208 values[n++] = running;
3dab77fb 4209 if (read_format & PERF_FORMAT_ID)
cdd6c482 4210 values[n++] = primary_event_id(event);
3dab77fb
PZ
4211
4212 if (copy_to_user(buf, values, n * sizeof(u64)))
4213 return -EFAULT;
4214
4215 return n * sizeof(u64);
4216}
4217
dc633982
JO
4218static bool is_event_hup(struct perf_event *event)
4219{
4220 bool no_children;
4221
a69b0ca4 4222 if (event->state > PERF_EVENT_STATE_EXIT)
dc633982
JO
4223 return false;
4224
4225 mutex_lock(&event->child_mutex);
4226 no_children = list_empty(&event->child_list);
4227 mutex_unlock(&event->child_mutex);
4228 return no_children;
4229}
4230
0793a61d 4231/*
cdd6c482 4232 * Read the performance event - simple non blocking version for now
0793a61d
TG
4233 */
4234static ssize_t
b15f495b 4235__perf_read(struct perf_event *event, char __user *buf, size_t count)
0793a61d 4236{
cdd6c482 4237 u64 read_format = event->attr.read_format;
3dab77fb 4238 int ret;
0793a61d 4239
3b6f9e5c 4240 /*
cdd6c482 4241 * Return end-of-file for a read on a event that is in
3b6f9e5c
PM
4242 * error state (i.e. because it was pinned but it couldn't be
4243 * scheduled on to the CPU at some point).
4244 */
cdd6c482 4245 if (event->state == PERF_EVENT_STATE_ERROR)
3b6f9e5c
PM
4246 return 0;
4247
c320c7b7 4248 if (count < event->read_size)
3dab77fb
PZ
4249 return -ENOSPC;
4250
cdd6c482 4251 WARN_ON_ONCE(event->ctx->parent_ctx);
3dab77fb 4252 if (read_format & PERF_FORMAT_GROUP)
b15f495b 4253 ret = perf_read_group(event, read_format, buf);
3dab77fb 4254 else
b15f495b 4255 ret = perf_read_one(event, read_format, buf);
0793a61d 4256
3dab77fb 4257 return ret;
0793a61d
TG
4258}
4259
0793a61d
TG
4260static ssize_t
4261perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
4262{
cdd6c482 4263 struct perf_event *event = file->private_data;
f63a8daa
PZ
4264 struct perf_event_context *ctx;
4265 int ret;
0793a61d 4266
f63a8daa 4267 ctx = perf_event_ctx_lock(event);
b15f495b 4268 ret = __perf_read(event, buf, count);
f63a8daa
PZ
4269 perf_event_ctx_unlock(event, ctx);
4270
4271 return ret;
0793a61d
TG
4272}
4273
4274static unsigned int perf_poll(struct file *file, poll_table *wait)
4275{
cdd6c482 4276 struct perf_event *event = file->private_data;
76369139 4277 struct ring_buffer *rb;
61b67684 4278 unsigned int events = POLLHUP;
c7138f37 4279
e708d7ad 4280 poll_wait(file, &event->waitq, wait);
179033b3 4281
dc633982 4282 if (is_event_hup(event))
179033b3 4283 return events;
c7138f37 4284
10c6db11 4285 /*
9bb5d40c
PZ
4286 * Pin the event->rb by taking event->mmap_mutex; otherwise
4287 * perf_event_set_output() can swizzle our rb and make us miss wakeups.
10c6db11
PZ
4288 */
4289 mutex_lock(&event->mmap_mutex);
9bb5d40c
PZ
4290 rb = event->rb;
4291 if (rb)
76369139 4292 events = atomic_xchg(&rb->poll, 0);
10c6db11 4293 mutex_unlock(&event->mmap_mutex);
0793a61d
TG
4294 return events;
4295}
4296
f63a8daa 4297static void _perf_event_reset(struct perf_event *event)
6de6a7b9 4298{
7d88962e 4299 (void)perf_event_read(event, false);
e7850595 4300 local64_set(&event->count, 0);
cdd6c482 4301 perf_event_update_userpage(event);
3df5edad
PZ
4302}
4303
c93f7669 4304/*
cdd6c482
IM
4305 * Holding the top-level event's child_mutex means that any
4306 * descendant process that has inherited this event will block
8ba289b8 4307 * in perf_event_exit_event() if it goes to exit, thus satisfying the
cdd6c482 4308 * task existence requirements of perf_event_enable/disable.
c93f7669 4309 */
cdd6c482
IM
4310static void perf_event_for_each_child(struct perf_event *event,
4311 void (*func)(struct perf_event *))
3df5edad 4312{
cdd6c482 4313 struct perf_event *child;
3df5edad 4314
cdd6c482 4315 WARN_ON_ONCE(event->ctx->parent_ctx);
f63a8daa 4316
cdd6c482
IM
4317 mutex_lock(&event->child_mutex);
4318 func(event);
4319 list_for_each_entry(child, &event->child_list, child_list)
3df5edad 4320 func(child);
cdd6c482 4321 mutex_unlock(&event->child_mutex);
3df5edad
PZ
4322}
4323
cdd6c482
IM
4324static void perf_event_for_each(struct perf_event *event,
4325 void (*func)(struct perf_event *))
3df5edad 4326{
cdd6c482
IM
4327 struct perf_event_context *ctx = event->ctx;
4328 struct perf_event *sibling;
3df5edad 4329
f63a8daa
PZ
4330 lockdep_assert_held(&ctx->mutex);
4331
cdd6c482 4332 event = event->group_leader;
75f937f2 4333
cdd6c482 4334 perf_event_for_each_child(event, func);
cdd6c482 4335 list_for_each_entry(sibling, &event->sibling_list, group_entry)
724b6daa 4336 perf_event_for_each_child(sibling, func);
6de6a7b9
PZ
4337}
4338
fae3fde6
PZ
4339static void __perf_event_period(struct perf_event *event,
4340 struct perf_cpu_context *cpuctx,
4341 struct perf_event_context *ctx,
4342 void *info)
c7999c6f 4343{
fae3fde6 4344 u64 value = *((u64 *)info);
c7999c6f 4345 bool active;
08247e31 4346
cdd6c482 4347 if (event->attr.freq) {
cdd6c482 4348 event->attr.sample_freq = value;
08247e31 4349 } else {
cdd6c482
IM
4350 event->attr.sample_period = value;
4351 event->hw.sample_period = value;
08247e31 4352 }
bad7192b
PZ
4353
4354 active = (event->state == PERF_EVENT_STATE_ACTIVE);
4355 if (active) {
4356 perf_pmu_disable(ctx->pmu);
1e02cd40
PZ
4357 /*
4358 * We could be throttled; unthrottle now to avoid the tick
4359 * trying to unthrottle while we already re-started the event.
4360 */
4361 if (event->hw.interrupts == MAX_INTERRUPTS) {
4362 event->hw.interrupts = 0;
4363 perf_log_throttle(event, 1);
4364 }
bad7192b
PZ
4365 event->pmu->stop(event, PERF_EF_UPDATE);
4366 }
4367
4368 local64_set(&event->hw.period_left, 0);
4369
4370 if (active) {
4371 event->pmu->start(event, PERF_EF_RELOAD);
4372 perf_pmu_enable(ctx->pmu);
4373 }
c7999c6f
PZ
4374}
4375
4376static int perf_event_period(struct perf_event *event, u64 __user *arg)
4377{
c7999c6f
PZ
4378 u64 value;
4379
4380 if (!is_sampling_event(event))
4381 return -EINVAL;
4382
4383 if (copy_from_user(&value, arg, sizeof(value)))
4384 return -EFAULT;
4385
4386 if (!value)
4387 return -EINVAL;
4388
4389 if (event->attr.freq && value > sysctl_perf_event_sample_rate)
4390 return -EINVAL;
4391
fae3fde6 4392 event_function_call(event, __perf_event_period, &value);
08247e31 4393
c7999c6f 4394 return 0;
08247e31
PZ
4395}
4396
ac9721f3
PZ
4397static const struct file_operations perf_fops;
4398
2903ff01 4399static inline int perf_fget_light(int fd, struct fd *p)
ac9721f3 4400{
2903ff01
AV
4401 struct fd f = fdget(fd);
4402 if (!f.file)
4403 return -EBADF;
ac9721f3 4404
2903ff01
AV
4405 if (f.file->f_op != &perf_fops) {
4406 fdput(f);
4407 return -EBADF;
ac9721f3 4408 }
2903ff01
AV
4409 *p = f;
4410 return 0;
ac9721f3
PZ
4411}
4412
4413static int perf_event_set_output(struct perf_event *event,
4414 struct perf_event *output_event);
6fb2915d 4415static int perf_event_set_filter(struct perf_event *event, void __user *arg);
2541517c 4416static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd);
a4be7c27 4417
f63a8daa 4418static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
d859e29f 4419{
cdd6c482 4420 void (*func)(struct perf_event *);
3df5edad 4421 u32 flags = arg;
d859e29f
PM
4422
4423 switch (cmd) {
cdd6c482 4424 case PERF_EVENT_IOC_ENABLE:
f63a8daa 4425 func = _perf_event_enable;
d859e29f 4426 break;
cdd6c482 4427 case PERF_EVENT_IOC_DISABLE:
f63a8daa 4428 func = _perf_event_disable;
79f14641 4429 break;
cdd6c482 4430 case PERF_EVENT_IOC_RESET:
f63a8daa 4431 func = _perf_event_reset;
6de6a7b9 4432 break;
3df5edad 4433
cdd6c482 4434 case PERF_EVENT_IOC_REFRESH:
f63a8daa 4435 return _perf_event_refresh(event, arg);
08247e31 4436
cdd6c482
IM
4437 case PERF_EVENT_IOC_PERIOD:
4438 return perf_event_period(event, (u64 __user *)arg);
08247e31 4439
cf4957f1
JO
4440 case PERF_EVENT_IOC_ID:
4441 {
4442 u64 id = primary_event_id(event);
4443
4444 if (copy_to_user((void __user *)arg, &id, sizeof(id)))
4445 return -EFAULT;
4446 return 0;
4447 }
4448
cdd6c482 4449 case PERF_EVENT_IOC_SET_OUTPUT:
ac9721f3 4450 {
ac9721f3 4451 int ret;
ac9721f3 4452 if (arg != -1) {
2903ff01
AV
4453 struct perf_event *output_event;
4454 struct fd output;
4455 ret = perf_fget_light(arg, &output);
4456 if (ret)
4457 return ret;
4458 output_event = output.file->private_data;
4459 ret = perf_event_set_output(event, output_event);
4460 fdput(output);
4461 } else {
4462 ret = perf_event_set_output(event, NULL);
ac9721f3 4463 }
ac9721f3
PZ
4464 return ret;
4465 }
a4be7c27 4466
6fb2915d
LZ
4467 case PERF_EVENT_IOC_SET_FILTER:
4468 return perf_event_set_filter(event, (void __user *)arg);
4469
2541517c
AS
4470 case PERF_EVENT_IOC_SET_BPF:
4471 return perf_event_set_bpf_prog(event, arg);
4472
86e7972f
WN
4473 case PERF_EVENT_IOC_PAUSE_OUTPUT: {
4474 struct ring_buffer *rb;
4475
4476 rcu_read_lock();
4477 rb = rcu_dereference(event->rb);
4478 if (!rb || !rb->nr_pages) {
4479 rcu_read_unlock();
4480 return -EINVAL;
4481 }
4482 rb_toggle_paused(rb, !!arg);
4483 rcu_read_unlock();
4484 return 0;
4485 }
d859e29f 4486 default:
3df5edad 4487 return -ENOTTY;
d859e29f 4488 }
3df5edad
PZ
4489
4490 if (flags & PERF_IOC_FLAG_GROUP)
cdd6c482 4491 perf_event_for_each(event, func);
3df5edad 4492 else
cdd6c482 4493 perf_event_for_each_child(event, func);
3df5edad
PZ
4494
4495 return 0;
d859e29f
PM
4496}
4497
f63a8daa
PZ
4498static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4499{
4500 struct perf_event *event = file->private_data;
4501 struct perf_event_context *ctx;
4502 long ret;
4503
4504 ctx = perf_event_ctx_lock(event);
4505 ret = _perf_ioctl(event, cmd, arg);
4506 perf_event_ctx_unlock(event, ctx);
4507
4508 return ret;
4509}
4510
b3f20785
PM
4511#ifdef CONFIG_COMPAT
4512static long perf_compat_ioctl(struct file *file, unsigned int cmd,
4513 unsigned long arg)
4514{
4515 switch (_IOC_NR(cmd)) {
4516 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
4517 case _IOC_NR(PERF_EVENT_IOC_ID):
4518 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
4519 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
4520 cmd &= ~IOCSIZE_MASK;
4521 cmd |= sizeof(void *) << IOCSIZE_SHIFT;
4522 }
4523 break;
4524 }
4525 return perf_ioctl(file, cmd, arg);
4526}
4527#else
4528# define perf_compat_ioctl NULL
4529#endif
4530
cdd6c482 4531int perf_event_task_enable(void)
771d7cde 4532{
f63a8daa 4533 struct perf_event_context *ctx;
cdd6c482 4534 struct perf_event *event;
771d7cde 4535
cdd6c482 4536 mutex_lock(&current->perf_event_mutex);
f63a8daa
PZ
4537 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4538 ctx = perf_event_ctx_lock(event);
4539 perf_event_for_each_child(event, _perf_event_enable);
4540 perf_event_ctx_unlock(event, ctx);
4541 }
cdd6c482 4542 mutex_unlock(&current->perf_event_mutex);
771d7cde
PZ
4543
4544 return 0;
4545}
4546
cdd6c482 4547int perf_event_task_disable(void)
771d7cde 4548{
f63a8daa 4549 struct perf_event_context *ctx;
cdd6c482 4550 struct perf_event *event;
771d7cde 4551
cdd6c482 4552 mutex_lock(&current->perf_event_mutex);
f63a8daa
PZ
4553 list_for_each_entry(event, &current->perf_event_list, owner_entry) {
4554 ctx = perf_event_ctx_lock(event);
4555 perf_event_for_each_child(event, _perf_event_disable);
4556 perf_event_ctx_unlock(event, ctx);
4557 }
cdd6c482 4558 mutex_unlock(&current->perf_event_mutex);
771d7cde
PZ
4559
4560 return 0;
4561}
4562
cdd6c482 4563static int perf_event_index(struct perf_event *event)
194002b2 4564{
a4eaf7f1
PZ
4565 if (event->hw.state & PERF_HES_STOPPED)
4566 return 0;
4567
cdd6c482 4568 if (event->state != PERF_EVENT_STATE_ACTIVE)
194002b2
PZ
4569 return 0;
4570
35edc2a5 4571 return event->pmu->event_idx(event);
194002b2
PZ
4572}
4573
c4794295 4574static void calc_timer_values(struct perf_event *event,
e3f3541c 4575 u64 *now,
7f310a5d
EM
4576 u64 *enabled,
4577 u64 *running)
c4794295 4578{
e3f3541c 4579 u64 ctx_time;
c4794295 4580
e3f3541c
PZ
4581 *now = perf_clock();
4582 ctx_time = event->shadow_ctx_time + *now;
c4794295
EM
4583 *enabled = ctx_time - event->tstamp_enabled;
4584 *running = ctx_time - event->tstamp_running;
4585}
4586
fa731587
PZ
4587static void perf_event_init_userpage(struct perf_event *event)
4588{
4589 struct perf_event_mmap_page *userpg;
4590 struct ring_buffer *rb;
4591
4592 rcu_read_lock();
4593 rb = rcu_dereference(event->rb);
4594 if (!rb)
4595 goto unlock;
4596
4597 userpg = rb->user_page;
4598
4599 /* Allow new userspace to detect that bit 0 is deprecated */
4600 userpg->cap_bit0_is_deprecated = 1;
4601 userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
e8c6deac
AS
4602 userpg->data_offset = PAGE_SIZE;
4603 userpg->data_size = perf_data_size(rb);
fa731587
PZ
4604
4605unlock:
4606 rcu_read_unlock();
4607}
4608
c1317ec2
AL
4609void __weak arch_perf_update_userpage(
4610 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now)
e3f3541c
PZ
4611{
4612}
4613
38ff667b
PZ
4614/*
4615 * Callers need to ensure there can be no nesting of this function, otherwise
4616 * the seqlock logic goes bad. We can not serialize this because the arch
4617 * code calls this from NMI context.
4618 */
cdd6c482 4619void perf_event_update_userpage(struct perf_event *event)
37d81828 4620{
cdd6c482 4621 struct perf_event_mmap_page *userpg;
76369139 4622 struct ring_buffer *rb;
e3f3541c 4623 u64 enabled, running, now;
38ff667b
PZ
4624
4625 rcu_read_lock();
5ec4c599
PZ
4626 rb = rcu_dereference(event->rb);
4627 if (!rb)
4628 goto unlock;
4629
0d641208
EM
4630 /*
4631 * compute total_time_enabled, total_time_running
4632 * based on snapshot values taken when the event
4633 * was last scheduled in.
4634 *
4635 * we cannot simply called update_context_time()
4636 * because of locking issue as we can be called in
4637 * NMI context
4638 */
e3f3541c 4639 calc_timer_values(event, &now, &enabled, &running);
38ff667b 4640
76369139 4641 userpg = rb->user_page;
7b732a75
PZ
4642 /*
4643 * Disable preemption so as to not let the corresponding user-space
4644 * spin too long if we get preempted.
4645 */
4646 preempt_disable();
37d81828 4647 ++userpg->lock;
92f22a38 4648 barrier();
cdd6c482 4649 userpg->index = perf_event_index(event);
b5e58793 4650 userpg->offset = perf_event_count(event);
365a4038 4651 if (userpg->index)
e7850595 4652 userpg->offset -= local64_read(&event->hw.prev_count);
7b732a75 4653
0d641208 4654 userpg->time_enabled = enabled +
cdd6c482 4655 atomic64_read(&event->child_total_time_enabled);
7f8b4e4e 4656
0d641208 4657 userpg->time_running = running +
cdd6c482 4658 atomic64_read(&event->child_total_time_running);
7f8b4e4e 4659
c1317ec2 4660 arch_perf_update_userpage(event, userpg, now);
e3f3541c 4661
92f22a38 4662 barrier();
37d81828 4663 ++userpg->lock;
7b732a75 4664 preempt_enable();
38ff667b 4665unlock:
7b732a75 4666 rcu_read_unlock();
37d81828
PM
4667}
4668
906010b2
PZ
4669static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
4670{
4671 struct perf_event *event = vma->vm_file->private_data;
76369139 4672 struct ring_buffer *rb;
906010b2
PZ
4673 int ret = VM_FAULT_SIGBUS;
4674
4675 if (vmf->flags & FAULT_FLAG_MKWRITE) {
4676 if (vmf->pgoff == 0)
4677 ret = 0;
4678 return ret;
4679 }
4680
4681 rcu_read_lock();
76369139
FW
4682 rb = rcu_dereference(event->rb);
4683 if (!rb)
906010b2
PZ
4684 goto unlock;
4685
4686 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
4687 goto unlock;
4688
76369139 4689 vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
906010b2
PZ
4690 if (!vmf->page)
4691 goto unlock;
4692
4693 get_page(vmf->page);
4694 vmf->page->mapping = vma->vm_file->f_mapping;
4695 vmf->page->index = vmf->pgoff;
4696
4697 ret = 0;
4698unlock:
4699 rcu_read_unlock();
4700
4701 return ret;
4702}
4703
10c6db11
PZ
4704static void ring_buffer_attach(struct perf_event *event,
4705 struct ring_buffer *rb)
4706{
b69cf536 4707 struct ring_buffer *old_rb = NULL;
10c6db11
PZ
4708 unsigned long flags;
4709
b69cf536
PZ
4710 if (event->rb) {
4711 /*
4712 * Should be impossible, we set this when removing
4713 * event->rb_entry and wait/clear when adding event->rb_entry.
4714 */
4715 WARN_ON_ONCE(event->rcu_pending);
10c6db11 4716
b69cf536 4717 old_rb = event->rb;
b69cf536
PZ
4718 spin_lock_irqsave(&old_rb->event_lock, flags);
4719 list_del_rcu(&event->rb_entry);
4720 spin_unlock_irqrestore(&old_rb->event_lock, flags);
10c6db11 4721
2f993cf0
ON
4722 event->rcu_batches = get_state_synchronize_rcu();
4723 event->rcu_pending = 1;
b69cf536 4724 }
10c6db11 4725
b69cf536 4726 if (rb) {
2f993cf0
ON
4727 if (event->rcu_pending) {
4728 cond_synchronize_rcu(event->rcu_batches);
4729 event->rcu_pending = 0;
4730 }
4731
b69cf536
PZ
4732 spin_lock_irqsave(&rb->event_lock, flags);
4733 list_add_rcu(&event->rb_entry, &rb->event_list);
4734 spin_unlock_irqrestore(&rb->event_lock, flags);
4735 }
4736
4737 rcu_assign_pointer(event->rb, rb);
4738
4739 if (old_rb) {
4740 ring_buffer_put(old_rb);
4741 /*
4742 * Since we detached before setting the new rb, so that we
4743 * could attach the new rb, we could have missed a wakeup.
4744 * Provide it now.
4745 */
4746 wake_up_all(&event->waitq);
4747 }
10c6db11
PZ
4748}
4749
4750static void ring_buffer_wakeup(struct perf_event *event)
4751{
4752 struct ring_buffer *rb;
4753
4754 rcu_read_lock();
4755 rb = rcu_dereference(event->rb);
9bb5d40c
PZ
4756 if (rb) {
4757 list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
4758 wake_up_all(&event->waitq);
4759 }
10c6db11
PZ
4760 rcu_read_unlock();
4761}
4762
fdc26706 4763struct ring_buffer *ring_buffer_get(struct perf_event *event)
7b732a75 4764{
76369139 4765 struct ring_buffer *rb;
7b732a75 4766
ac9721f3 4767 rcu_read_lock();
76369139
FW
4768 rb = rcu_dereference(event->rb);
4769 if (rb) {
4770 if (!atomic_inc_not_zero(&rb->refcount))
4771 rb = NULL;
ac9721f3
PZ
4772 }
4773 rcu_read_unlock();
4774
76369139 4775 return rb;
ac9721f3
PZ
4776}
4777
fdc26706 4778void ring_buffer_put(struct ring_buffer *rb)
ac9721f3 4779{
76369139 4780 if (!atomic_dec_and_test(&rb->refcount))
ac9721f3 4781 return;
7b732a75 4782
9bb5d40c 4783 WARN_ON_ONCE(!list_empty(&rb->event_list));
10c6db11 4784
76369139 4785 call_rcu(&rb->rcu_head, rb_free_rcu);
7b732a75
PZ
4786}
4787
4788static void perf_mmap_open(struct vm_area_struct *vma)
4789{
cdd6c482 4790 struct perf_event *event = vma->vm_file->private_data;
7b732a75 4791
cdd6c482 4792 atomic_inc(&event->mmap_count);
9bb5d40c 4793 atomic_inc(&event->rb->mmap_count);
1e0fb9ec 4794
45bfb2e5
PZ
4795 if (vma->vm_pgoff)
4796 atomic_inc(&event->rb->aux_mmap_count);
4797
1e0fb9ec
AL
4798 if (event->pmu->event_mapped)
4799 event->pmu->event_mapped(event);
7b732a75
PZ
4800}
4801
95ff4ca2
AS
4802static void perf_pmu_output_stop(struct perf_event *event);
4803
9bb5d40c
PZ
4804/*
4805 * A buffer can be mmap()ed multiple times; either directly through the same
4806 * event, or through other events by use of perf_event_set_output().
4807 *
4808 * In order to undo the VM accounting done by perf_mmap() we need to destroy
4809 * the buffer here, where we still have a VM context. This means we need
4810 * to detach all events redirecting to us.
4811 */
7b732a75
PZ
4812static void perf_mmap_close(struct vm_area_struct *vma)
4813{
cdd6c482 4814 struct perf_event *event = vma->vm_file->private_data;
7b732a75 4815
b69cf536 4816 struct ring_buffer *rb = ring_buffer_get(event);
9bb5d40c
PZ
4817 struct user_struct *mmap_user = rb->mmap_user;
4818 int mmap_locked = rb->mmap_locked;
4819 unsigned long size = perf_data_size(rb);
789f90fc 4820
1e0fb9ec
AL
4821 if (event->pmu->event_unmapped)
4822 event->pmu->event_unmapped(event);
4823
45bfb2e5
PZ
4824 /*
4825 * rb->aux_mmap_count will always drop before rb->mmap_count and
4826 * event->mmap_count, so it is ok to use event->mmap_mutex to
4827 * serialize with perf_mmap here.
4828 */
4829 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff &&
4830 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &event->mmap_mutex)) {
95ff4ca2
AS
4831 /*
4832 * Stop all AUX events that are writing to this buffer,
4833 * so that we can free its AUX pages and corresponding PMU
4834 * data. Note that after rb::aux_mmap_count dropped to zero,
4835 * they won't start any more (see perf_aux_output_begin()).
4836 */
4837 perf_pmu_output_stop(event);
4838
4839 /* now it's safe to free the pages */
45bfb2e5
PZ
4840 atomic_long_sub(rb->aux_nr_pages, &mmap_user->locked_vm);
4841 vma->vm_mm->pinned_vm -= rb->aux_mmap_locked;
4842
95ff4ca2 4843 /* this has to be the last one */
45bfb2e5 4844 rb_free_aux(rb);
95ff4ca2
AS
4845 WARN_ON_ONCE(atomic_read(&rb->aux_refcount));
4846
45bfb2e5
PZ
4847 mutex_unlock(&event->mmap_mutex);
4848 }
4849
9bb5d40c
PZ
4850 atomic_dec(&rb->mmap_count);
4851
4852 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
b69cf536 4853 goto out_put;
9bb5d40c 4854
b69cf536 4855 ring_buffer_attach(event, NULL);
9bb5d40c
PZ
4856 mutex_unlock(&event->mmap_mutex);
4857
4858 /* If there's still other mmap()s of this buffer, we're done. */
b69cf536
PZ
4859 if (atomic_read(&rb->mmap_count))
4860 goto out_put;
ac9721f3 4861
9bb5d40c
PZ
4862 /*
4863 * No other mmap()s, detach from all other events that might redirect
4864 * into the now unreachable buffer. Somewhat complicated by the
4865 * fact that rb::event_lock otherwise nests inside mmap_mutex.
4866 */
4867again:
4868 rcu_read_lock();
4869 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
4870 if (!atomic_long_inc_not_zero(&event->refcount)) {
4871 /*
4872 * This event is en-route to free_event() which will
4873 * detach it and remove it from the list.
4874 */
4875 continue;
4876 }
4877 rcu_read_unlock();
789f90fc 4878
9bb5d40c
PZ
4879 mutex_lock(&event->mmap_mutex);
4880 /*
4881 * Check we didn't race with perf_event_set_output() which can
4882 * swizzle the rb from under us while we were waiting to
4883 * acquire mmap_mutex.
4884 *
4885 * If we find a different rb; ignore this event, a next
4886 * iteration will no longer find it on the list. We have to
4887 * still restart the iteration to make sure we're not now
4888 * iterating the wrong list.
4889 */
b69cf536
PZ
4890 if (event->rb == rb)
4891 ring_buffer_attach(event, NULL);
4892
cdd6c482 4893 mutex_unlock(&event->mmap_mutex);
9bb5d40c 4894 put_event(event);
ac9721f3 4895
9bb5d40c
PZ
4896 /*
4897 * Restart the iteration; either we're on the wrong list or
4898 * destroyed its integrity by doing a deletion.
4899 */
4900 goto again;
7b732a75 4901 }
9bb5d40c
PZ
4902 rcu_read_unlock();
4903
4904 /*
4905 * It could be there's still a few 0-ref events on the list; they'll
4906 * get cleaned up by free_event() -- they'll also still have their
4907 * ref on the rb and will free it whenever they are done with it.
4908 *
4909 * Aside from that, this buffer is 'fully' detached and unmapped,
4910 * undo the VM accounting.
4911 */
4912
4913 atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
4914 vma->vm_mm->pinned_vm -= mmap_locked;
4915 free_uid(mmap_user);
4916
b69cf536 4917out_put:
9bb5d40c 4918 ring_buffer_put(rb); /* could be last */
37d81828
PM
4919}
4920
f0f37e2f 4921static const struct vm_operations_struct perf_mmap_vmops = {
43a21ea8 4922 .open = perf_mmap_open,
45bfb2e5 4923 .close = perf_mmap_close, /* non mergable */
43a21ea8
PZ
4924 .fault = perf_mmap_fault,
4925 .page_mkwrite = perf_mmap_fault,
37d81828
PM
4926};
4927
4928static int perf_mmap(struct file *file, struct vm_area_struct *vma)
4929{
cdd6c482 4930 struct perf_event *event = file->private_data;
22a4f650 4931 unsigned long user_locked, user_lock_limit;
789f90fc 4932 struct user_struct *user = current_user();
22a4f650 4933 unsigned long locked, lock_limit;
45bfb2e5 4934 struct ring_buffer *rb = NULL;
7b732a75
PZ
4935 unsigned long vma_size;
4936 unsigned long nr_pages;
45bfb2e5 4937 long user_extra = 0, extra = 0;
d57e34fd 4938 int ret = 0, flags = 0;
37d81828 4939
c7920614
PZ
4940 /*
4941 * Don't allow mmap() of inherited per-task counters. This would
4942 * create a performance issue due to all children writing to the
76369139 4943 * same rb.
c7920614
PZ
4944 */
4945 if (event->cpu == -1 && event->attr.inherit)
4946 return -EINVAL;
4947
43a21ea8 4948 if (!(vma->vm_flags & VM_SHARED))
37d81828 4949 return -EINVAL;
7b732a75
PZ
4950
4951 vma_size = vma->vm_end - vma->vm_start;
45bfb2e5
PZ
4952
4953 if (vma->vm_pgoff == 0) {
4954 nr_pages = (vma_size / PAGE_SIZE) - 1;
4955 } else {
4956 /*
4957 * AUX area mapping: if rb->aux_nr_pages != 0, it's already
4958 * mapped, all subsequent mappings should have the same size
4959 * and offset. Must be above the normal perf buffer.
4960 */
4961 u64 aux_offset, aux_size;
4962
4963 if (!event->rb)
4964 return -EINVAL;
4965
4966 nr_pages = vma_size / PAGE_SIZE;
4967
4968 mutex_lock(&event->mmap_mutex);
4969 ret = -EINVAL;
4970
4971 rb = event->rb;
4972 if (!rb)
4973 goto aux_unlock;
4974
4975 aux_offset = ACCESS_ONCE(rb->user_page->aux_offset);
4976 aux_size = ACCESS_ONCE(rb->user_page->aux_size);
4977
4978 if (aux_offset < perf_data_size(rb) + PAGE_SIZE)
4979 goto aux_unlock;
4980
4981 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT)
4982 goto aux_unlock;
4983
4984 /* already mapped with a different offset */
4985 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff)
4986 goto aux_unlock;
4987
4988 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE)
4989 goto aux_unlock;
4990
4991 /* already mapped with a different size */
4992 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages)
4993 goto aux_unlock;
4994
4995 if (!is_power_of_2(nr_pages))
4996 goto aux_unlock;
4997
4998 if (!atomic_inc_not_zero(&rb->mmap_count))
4999 goto aux_unlock;
5000
5001 if (rb_has_aux(rb)) {
5002 atomic_inc(&rb->aux_mmap_count);
5003 ret = 0;
5004 goto unlock;
5005 }
5006
5007 atomic_set(&rb->aux_mmap_count, 1);
5008 user_extra = nr_pages;
5009
5010 goto accounting;
5011 }
7b732a75 5012
7730d865 5013 /*
76369139 5014 * If we have rb pages ensure they're a power-of-two number, so we
7730d865
PZ
5015 * can do bitmasks instead of modulo.
5016 */
2ed11312 5017 if (nr_pages != 0 && !is_power_of_2(nr_pages))
37d81828
PM
5018 return -EINVAL;
5019
7b732a75 5020 if (vma_size != PAGE_SIZE * (1 + nr_pages))
37d81828
PM
5021 return -EINVAL;
5022
cdd6c482 5023 WARN_ON_ONCE(event->ctx->parent_ctx);
9bb5d40c 5024again:
cdd6c482 5025 mutex_lock(&event->mmap_mutex);
76369139 5026 if (event->rb) {
9bb5d40c 5027 if (event->rb->nr_pages != nr_pages) {
ebb3c4c4 5028 ret = -EINVAL;
9bb5d40c
PZ
5029 goto unlock;
5030 }
5031
5032 if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
5033 /*
5034 * Raced against perf_mmap_close() through
5035 * perf_event_set_output(). Try again, hope for better
5036 * luck.
5037 */
5038 mutex_unlock(&event->mmap_mutex);
5039 goto again;
5040 }
5041
ebb3c4c4
PZ
5042 goto unlock;
5043 }
5044
789f90fc 5045 user_extra = nr_pages + 1;
45bfb2e5
PZ
5046
5047accounting:
cdd6c482 5048 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
a3862d3f
IM
5049
5050 /*
5051 * Increase the limit linearly with more CPUs:
5052 */
5053 user_lock_limit *= num_online_cpus();
5054
789f90fc 5055 user_locked = atomic_long_read(&user->locked_vm) + user_extra;
c5078f78 5056
789f90fc
PZ
5057 if (user_locked > user_lock_limit)
5058 extra = user_locked - user_lock_limit;
7b732a75 5059
78d7d407 5060 lock_limit = rlimit(RLIMIT_MEMLOCK);
7b732a75 5061 lock_limit >>= PAGE_SHIFT;
bc3e53f6 5062 locked = vma->vm_mm->pinned_vm + extra;
7b732a75 5063
459ec28a
IM
5064 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
5065 !capable(CAP_IPC_LOCK)) {
ebb3c4c4
PZ
5066 ret = -EPERM;
5067 goto unlock;
5068 }
7b732a75 5069
45bfb2e5 5070 WARN_ON(!rb && event->rb);
906010b2 5071
d57e34fd 5072 if (vma->vm_flags & VM_WRITE)
76369139 5073 flags |= RING_BUFFER_WRITABLE;
d57e34fd 5074
76369139 5075 if (!rb) {
45bfb2e5
PZ
5076 rb = rb_alloc(nr_pages,
5077 event->attr.watermark ? event->attr.wakeup_watermark : 0,
5078 event->cpu, flags);
26cb63ad 5079
45bfb2e5
PZ
5080 if (!rb) {
5081 ret = -ENOMEM;
5082 goto unlock;
5083 }
43a21ea8 5084
45bfb2e5
PZ
5085 atomic_set(&rb->mmap_count, 1);
5086 rb->mmap_user = get_current_user();
5087 rb->mmap_locked = extra;
26cb63ad 5088
45bfb2e5 5089 ring_buffer_attach(event, rb);
ac9721f3 5090
45bfb2e5
PZ
5091 perf_event_init_userpage(event);
5092 perf_event_update_userpage(event);
5093 } else {
1a594131
AS
5094 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages,
5095 event->attr.aux_watermark, flags);
45bfb2e5
PZ
5096 if (!ret)
5097 rb->aux_mmap_locked = extra;
5098 }
9a0f05cb 5099
ebb3c4c4 5100unlock:
45bfb2e5
PZ
5101 if (!ret) {
5102 atomic_long_add(user_extra, &user->locked_vm);
5103 vma->vm_mm->pinned_vm += extra;
5104
ac9721f3 5105 atomic_inc(&event->mmap_count);
45bfb2e5
PZ
5106 } else if (rb) {
5107 atomic_dec(&rb->mmap_count);
5108 }
5109aux_unlock:
cdd6c482 5110 mutex_unlock(&event->mmap_mutex);
37d81828 5111
9bb5d40c
PZ
5112 /*
5113 * Since pinned accounting is per vm we cannot allow fork() to copy our
5114 * vma.
5115 */
26cb63ad 5116 vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
37d81828 5117 vma->vm_ops = &perf_mmap_vmops;
7b732a75 5118
1e0fb9ec
AL
5119 if (event->pmu->event_mapped)
5120 event->pmu->event_mapped(event);
5121
7b732a75 5122 return ret;
37d81828
PM
5123}
5124
3c446b3d
PZ
5125static int perf_fasync(int fd, struct file *filp, int on)
5126{
496ad9aa 5127 struct inode *inode = file_inode(filp);
cdd6c482 5128 struct perf_event *event = filp->private_data;
3c446b3d
PZ
5129 int retval;
5130
5955102c 5131 inode_lock(inode);
cdd6c482 5132 retval = fasync_helper(fd, filp, on, &event->fasync);
5955102c 5133 inode_unlock(inode);
3c446b3d
PZ
5134
5135 if (retval < 0)
5136 return retval;
5137
5138 return 0;
5139}
5140
0793a61d 5141static const struct file_operations perf_fops = {
3326c1ce 5142 .llseek = no_llseek,
0793a61d
TG
5143 .release = perf_release,
5144 .read = perf_read,
5145 .poll = perf_poll,
d859e29f 5146 .unlocked_ioctl = perf_ioctl,
b3f20785 5147 .compat_ioctl = perf_compat_ioctl,
37d81828 5148 .mmap = perf_mmap,
3c446b3d 5149 .fasync = perf_fasync,
0793a61d
TG
5150};
5151
925d519a 5152/*
cdd6c482 5153 * Perf event wakeup
925d519a
PZ
5154 *
5155 * If there's data, ensure we set the poll() state and publish everything
5156 * to user-space before waking everybody up.
5157 */
5158
fed66e2c
PZ
5159static inline struct fasync_struct **perf_event_fasync(struct perf_event *event)
5160{
5161 /* only the parent has fasync state */
5162 if (event->parent)
5163 event = event->parent;
5164 return &event->fasync;
5165}
5166
cdd6c482 5167void perf_event_wakeup(struct perf_event *event)
925d519a 5168{
10c6db11 5169 ring_buffer_wakeup(event);
4c9e2542 5170
cdd6c482 5171 if (event->pending_kill) {
fed66e2c 5172 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill);
cdd6c482 5173 event->pending_kill = 0;
4c9e2542 5174 }
925d519a
PZ
5175}
5176
e360adbe 5177static void perf_pending_event(struct irq_work *entry)
79f14641 5178{
cdd6c482
IM
5179 struct perf_event *event = container_of(entry,
5180 struct perf_event, pending);
d525211f
PZ
5181 int rctx;
5182
5183 rctx = perf_swevent_get_recursion_context();
5184 /*
5185 * If we 'fail' here, that's OK, it means recursion is already disabled
5186 * and we won't recurse 'further'.
5187 */
79f14641 5188
cdd6c482
IM
5189 if (event->pending_disable) {
5190 event->pending_disable = 0;
fae3fde6 5191 perf_event_disable_local(event);
79f14641
PZ
5192 }
5193
cdd6c482
IM
5194 if (event->pending_wakeup) {
5195 event->pending_wakeup = 0;
5196 perf_event_wakeup(event);
79f14641 5197 }
d525211f
PZ
5198
5199 if (rctx >= 0)
5200 perf_swevent_put_recursion_context(rctx);
79f14641
PZ
5201}
5202
39447b38
ZY
5203/*
5204 * We assume there is only KVM supporting the callbacks.
5205 * Later on, we might change it to a list if there is
5206 * another virtualization implementation supporting the callbacks.
5207 */
5208struct perf_guest_info_callbacks *perf_guest_cbs;
5209
5210int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5211{
5212 perf_guest_cbs = cbs;
5213 return 0;
5214}
5215EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
5216
5217int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
5218{
5219 perf_guest_cbs = NULL;
5220 return 0;
5221}
5222EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
5223
4018994f
JO
5224static void
5225perf_output_sample_regs(struct perf_output_handle *handle,
5226 struct pt_regs *regs, u64 mask)
5227{
5228 int bit;
5229
5230 for_each_set_bit(bit, (const unsigned long *) &mask,
5231 sizeof(mask) * BITS_PER_BYTE) {
5232 u64 val;
5233
5234 val = perf_reg_value(regs, bit);
5235 perf_output_put(handle, val);
5236 }
5237}
5238
60e2364e 5239static void perf_sample_regs_user(struct perf_regs *regs_user,
88a7c26a
AL
5240 struct pt_regs *regs,
5241 struct pt_regs *regs_user_copy)
4018994f 5242{
88a7c26a
AL
5243 if (user_mode(regs)) {
5244 regs_user->abi = perf_reg_abi(current);
2565711f 5245 regs_user->regs = regs;
88a7c26a
AL
5246 } else if (current->mm) {
5247 perf_get_regs_user(regs_user, regs, regs_user_copy);
2565711f
PZ
5248 } else {
5249 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
5250 regs_user->regs = NULL;
4018994f
JO
5251 }
5252}
5253
60e2364e
SE
5254static void perf_sample_regs_intr(struct perf_regs *regs_intr,
5255 struct pt_regs *regs)
5256{
5257 regs_intr->regs = regs;
5258 regs_intr->abi = perf_reg_abi(current);
5259}
5260
5261
c5ebcedb
JO
5262/*
5263 * Get remaining task size from user stack pointer.
5264 *
5265 * It'd be better to take stack vma map and limit this more
5266 * precisly, but there's no way to get it safely under interrupt,
5267 * so using TASK_SIZE as limit.
5268 */
5269static u64 perf_ustack_task_size(struct pt_regs *regs)
5270{
5271 unsigned long addr = perf_user_stack_pointer(regs);
5272
5273 if (!addr || addr >= TASK_SIZE)
5274 return 0;
5275
5276 return TASK_SIZE - addr;
5277}
5278
5279static u16
5280perf_sample_ustack_size(u16 stack_size, u16 header_size,
5281 struct pt_regs *regs)
5282{
5283 u64 task_size;
5284
5285 /* No regs, no stack pointer, no dump. */
5286 if (!regs)
5287 return 0;
5288
5289 /*
5290 * Check if we fit in with the requested stack size into the:
5291 * - TASK_SIZE
5292 * If we don't, we limit the size to the TASK_SIZE.
5293 *
5294 * - remaining sample size
5295 * If we don't, we customize the stack size to
5296 * fit in to the remaining sample size.
5297 */
5298
5299 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
5300 stack_size = min(stack_size, (u16) task_size);
5301
5302 /* Current header size plus static size and dynamic size. */
5303 header_size += 2 * sizeof(u64);
5304
5305 /* Do we fit in with the current stack dump size? */
5306 if ((u16) (header_size + stack_size) < header_size) {
5307 /*
5308 * If we overflow the maximum size for the sample,
5309 * we customize the stack dump size to fit in.
5310 */
5311 stack_size = USHRT_MAX - header_size - sizeof(u64);
5312 stack_size = round_up(stack_size, sizeof(u64));
5313 }
5314
5315 return stack_size;
5316}
5317
5318static void
5319perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
5320 struct pt_regs *regs)
5321{
5322 /* Case of a kernel thread, nothing to dump */
5323 if (!regs) {
5324 u64 size = 0;
5325 perf_output_put(handle, size);
5326 } else {
5327 unsigned long sp;
5328 unsigned int rem;
5329 u64 dyn_size;
5330
5331 /*
5332 * We dump:
5333 * static size
5334 * - the size requested by user or the best one we can fit
5335 * in to the sample max size
5336 * data
5337 * - user stack dump data
5338 * dynamic size
5339 * - the actual dumped size
5340 */
5341
5342 /* Static size. */
5343 perf_output_put(handle, dump_size);
5344
5345 /* Data. */
5346 sp = perf_user_stack_pointer(regs);
5347 rem = __output_copy_user(handle, (void *) sp, dump_size);
5348 dyn_size = dump_size - rem;
5349
5350 perf_output_skip(handle, rem);
5351
5352 /* Dynamic size. */
5353 perf_output_put(handle, dyn_size);
5354 }
5355}
5356
c980d109
ACM
5357static void __perf_event_header__init_id(struct perf_event_header *header,
5358 struct perf_sample_data *data,
5359 struct perf_event *event)
6844c09d
ACM
5360{
5361 u64 sample_type = event->attr.sample_type;
5362
5363 data->type = sample_type;
5364 header->size += event->id_header_size;
5365
5366 if (sample_type & PERF_SAMPLE_TID) {
5367 /* namespace issues */
5368 data->tid_entry.pid = perf_event_pid(event, current);
5369 data->tid_entry.tid = perf_event_tid(event, current);
5370 }
5371
5372 if (sample_type & PERF_SAMPLE_TIME)
34f43927 5373 data->time = perf_event_clock(event);
6844c09d 5374
ff3d527c 5375 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
6844c09d
ACM
5376 data->id = primary_event_id(event);
5377
5378 if (sample_type & PERF_SAMPLE_STREAM_ID)
5379 data->stream_id = event->id;
5380
5381 if (sample_type & PERF_SAMPLE_CPU) {
5382 data->cpu_entry.cpu = raw_smp_processor_id();
5383 data->cpu_entry.reserved = 0;
5384 }
5385}
5386
76369139
FW
5387void perf_event_header__init_id(struct perf_event_header *header,
5388 struct perf_sample_data *data,
5389 struct perf_event *event)
c980d109
ACM
5390{
5391 if (event->attr.sample_id_all)
5392 __perf_event_header__init_id(header, data, event);
5393}
5394
5395static void __perf_event__output_id_sample(struct perf_output_handle *handle,
5396 struct perf_sample_data *data)
5397{
5398 u64 sample_type = data->type;
5399
5400 if (sample_type & PERF_SAMPLE_TID)
5401 perf_output_put(handle, data->tid_entry);
5402
5403 if (sample_type & PERF_SAMPLE_TIME)
5404 perf_output_put(handle, data->time);
5405
5406 if (sample_type & PERF_SAMPLE_ID)
5407 perf_output_put(handle, data->id);
5408
5409 if (sample_type & PERF_SAMPLE_STREAM_ID)
5410 perf_output_put(handle, data->stream_id);
5411
5412 if (sample_type & PERF_SAMPLE_CPU)
5413 perf_output_put(handle, data->cpu_entry);
ff3d527c
AH
5414
5415 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5416 perf_output_put(handle, data->id);
c980d109
ACM
5417}
5418
76369139
FW
5419void perf_event__output_id_sample(struct perf_event *event,
5420 struct perf_output_handle *handle,
5421 struct perf_sample_data *sample)
c980d109
ACM
5422{
5423 if (event->attr.sample_id_all)
5424 __perf_event__output_id_sample(handle, sample);
5425}
5426
3dab77fb 5427static void perf_output_read_one(struct perf_output_handle *handle,
eed01528
SE
5428 struct perf_event *event,
5429 u64 enabled, u64 running)
3dab77fb 5430{
cdd6c482 5431 u64 read_format = event->attr.read_format;
3dab77fb
PZ
5432 u64 values[4];
5433 int n = 0;
5434
b5e58793 5435 values[n++] = perf_event_count(event);
3dab77fb 5436 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
eed01528 5437 values[n++] = enabled +
cdd6c482 5438 atomic64_read(&event->child_total_time_enabled);
3dab77fb
PZ
5439 }
5440 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
eed01528 5441 values[n++] = running +
cdd6c482 5442 atomic64_read(&event->child_total_time_running);
3dab77fb
PZ
5443 }
5444 if (read_format & PERF_FORMAT_ID)
cdd6c482 5445 values[n++] = primary_event_id(event);
3dab77fb 5446
76369139 5447 __output_copy(handle, values, n * sizeof(u64));
3dab77fb
PZ
5448}
5449
5450/*
cdd6c482 5451 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
3dab77fb
PZ
5452 */
5453static void perf_output_read_group(struct perf_output_handle *handle,
eed01528
SE
5454 struct perf_event *event,
5455 u64 enabled, u64 running)
3dab77fb 5456{
cdd6c482
IM
5457 struct perf_event *leader = event->group_leader, *sub;
5458 u64 read_format = event->attr.read_format;
3dab77fb
PZ
5459 u64 values[5];
5460 int n = 0;
5461
5462 values[n++] = 1 + leader->nr_siblings;
5463
5464 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
eed01528 5465 values[n++] = enabled;
3dab77fb
PZ
5466
5467 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
eed01528 5468 values[n++] = running;
3dab77fb 5469
cdd6c482 5470 if (leader != event)
3dab77fb
PZ
5471 leader->pmu->read(leader);
5472
b5e58793 5473 values[n++] = perf_event_count(leader);
3dab77fb 5474 if (read_format & PERF_FORMAT_ID)
cdd6c482 5475 values[n++] = primary_event_id(leader);
3dab77fb 5476
76369139 5477 __output_copy(handle, values, n * sizeof(u64));
3dab77fb 5478
65abc865 5479 list_for_each_entry(sub, &leader->sibling_list, group_entry) {
3dab77fb
PZ
5480 n = 0;
5481
6f5ab001
JO
5482 if ((sub != event) &&
5483 (sub->state == PERF_EVENT_STATE_ACTIVE))
3dab77fb
PZ
5484 sub->pmu->read(sub);
5485
b5e58793 5486 values[n++] = perf_event_count(sub);
3dab77fb 5487 if (read_format & PERF_FORMAT_ID)
cdd6c482 5488 values[n++] = primary_event_id(sub);
3dab77fb 5489
76369139 5490 __output_copy(handle, values, n * sizeof(u64));
3dab77fb
PZ
5491 }
5492}
5493
eed01528
SE
5494#define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
5495 PERF_FORMAT_TOTAL_TIME_RUNNING)
5496
3dab77fb 5497static void perf_output_read(struct perf_output_handle *handle,
cdd6c482 5498 struct perf_event *event)
3dab77fb 5499{
e3f3541c 5500 u64 enabled = 0, running = 0, now;
eed01528
SE
5501 u64 read_format = event->attr.read_format;
5502
5503 /*
5504 * compute total_time_enabled, total_time_running
5505 * based on snapshot values taken when the event
5506 * was last scheduled in.
5507 *
5508 * we cannot simply called update_context_time()
5509 * because of locking issue as we are called in
5510 * NMI context
5511 */
c4794295 5512 if (read_format & PERF_FORMAT_TOTAL_TIMES)
e3f3541c 5513 calc_timer_values(event, &now, &enabled, &running);
eed01528 5514
cdd6c482 5515 if (event->attr.read_format & PERF_FORMAT_GROUP)
eed01528 5516 perf_output_read_group(handle, event, enabled, running);
3dab77fb 5517 else
eed01528 5518 perf_output_read_one(handle, event, enabled, running);
3dab77fb
PZ
5519}
5520
5622f295
MM
5521void perf_output_sample(struct perf_output_handle *handle,
5522 struct perf_event_header *header,
5523 struct perf_sample_data *data,
cdd6c482 5524 struct perf_event *event)
5622f295
MM
5525{
5526 u64 sample_type = data->type;
5527
5528 perf_output_put(handle, *header);
5529
ff3d527c
AH
5530 if (sample_type & PERF_SAMPLE_IDENTIFIER)
5531 perf_output_put(handle, data->id);
5532
5622f295
MM
5533 if (sample_type & PERF_SAMPLE_IP)
5534 perf_output_put(handle, data->ip);
5535
5536 if (sample_type & PERF_SAMPLE_TID)
5537 perf_output_put(handle, data->tid_entry);
5538
5539 if (sample_type & PERF_SAMPLE_TIME)
5540 perf_output_put(handle, data->time);
5541
5542 if (sample_type & PERF_SAMPLE_ADDR)
5543 perf_output_put(handle, data->addr);
5544
5545 if (sample_type & PERF_SAMPLE_ID)
5546 perf_output_put(handle, data->id);
5547
5548 if (sample_type & PERF_SAMPLE_STREAM_ID)
5549 perf_output_put(handle, data->stream_id);
5550
5551 if (sample_type & PERF_SAMPLE_CPU)
5552 perf_output_put(handle, data->cpu_entry);
5553
5554 if (sample_type & PERF_SAMPLE_PERIOD)
5555 perf_output_put(handle, data->period);
5556
5557 if (sample_type & PERF_SAMPLE_READ)
cdd6c482 5558 perf_output_read(handle, event);
5622f295
MM
5559
5560 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5561 if (data->callchain) {
5562 int size = 1;
5563
5564 if (data->callchain)
5565 size += data->callchain->nr;
5566
5567 size *= sizeof(u64);
5568
76369139 5569 __output_copy(handle, data->callchain, size);
5622f295
MM
5570 } else {
5571 u64 nr = 0;
5572 perf_output_put(handle, nr);
5573 }
5574 }
5575
5576 if (sample_type & PERF_SAMPLE_RAW) {
5577 if (data->raw) {
fa128e6a
AS
5578 u32 raw_size = data->raw->size;
5579 u32 real_size = round_up(raw_size + sizeof(u32),
5580 sizeof(u64)) - sizeof(u32);
5581 u64 zero = 0;
5582
5583 perf_output_put(handle, real_size);
5584 __output_copy(handle, data->raw->data, raw_size);
5585 if (real_size - raw_size)
5586 __output_copy(handle, &zero, real_size - raw_size);
5622f295
MM
5587 } else {
5588 struct {
5589 u32 size;
5590 u32 data;
5591 } raw = {
5592 .size = sizeof(u32),
5593 .data = 0,
5594 };
5595 perf_output_put(handle, raw);
5596 }
5597 }
a7ac67ea 5598
bce38cd5
SE
5599 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5600 if (data->br_stack) {
5601 size_t size;
5602
5603 size = data->br_stack->nr
5604 * sizeof(struct perf_branch_entry);
5605
5606 perf_output_put(handle, data->br_stack->nr);
5607 perf_output_copy(handle, data->br_stack->entries, size);
5608 } else {
5609 /*
5610 * we always store at least the value of nr
5611 */
5612 u64 nr = 0;
5613 perf_output_put(handle, nr);
5614 }
5615 }
4018994f
JO
5616
5617 if (sample_type & PERF_SAMPLE_REGS_USER) {
5618 u64 abi = data->regs_user.abi;
5619
5620 /*
5621 * If there are no regs to dump, notice it through
5622 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5623 */
5624 perf_output_put(handle, abi);
5625
5626 if (abi) {
5627 u64 mask = event->attr.sample_regs_user;
5628 perf_output_sample_regs(handle,
5629 data->regs_user.regs,
5630 mask);
5631 }
5632 }
c5ebcedb 5633
a5cdd40c 5634 if (sample_type & PERF_SAMPLE_STACK_USER) {
c5ebcedb
JO
5635 perf_output_sample_ustack(handle,
5636 data->stack_user_size,
5637 data->regs_user.regs);
a5cdd40c 5638 }
c3feedf2
AK
5639
5640 if (sample_type & PERF_SAMPLE_WEIGHT)
5641 perf_output_put(handle, data->weight);
d6be9ad6
SE
5642
5643 if (sample_type & PERF_SAMPLE_DATA_SRC)
5644 perf_output_put(handle, data->data_src.val);
a5cdd40c 5645
fdfbbd07
AK
5646 if (sample_type & PERF_SAMPLE_TRANSACTION)
5647 perf_output_put(handle, data->txn);
5648
60e2364e
SE
5649 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5650 u64 abi = data->regs_intr.abi;
5651 /*
5652 * If there are no regs to dump, notice it through
5653 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
5654 */
5655 perf_output_put(handle, abi);
5656
5657 if (abi) {
5658 u64 mask = event->attr.sample_regs_intr;
5659
5660 perf_output_sample_regs(handle,
5661 data->regs_intr.regs,
5662 mask);
5663 }
5664 }
5665
a5cdd40c
PZ
5666 if (!event->attr.watermark) {
5667 int wakeup_events = event->attr.wakeup_events;
5668
5669 if (wakeup_events) {
5670 struct ring_buffer *rb = handle->rb;
5671 int events = local_inc_return(&rb->events);
5672
5673 if (events >= wakeup_events) {
5674 local_sub(wakeup_events, &rb->events);
5675 local_inc(&rb->wakeup);
5676 }
5677 }
5678 }
5622f295
MM
5679}
5680
5681void perf_prepare_sample(struct perf_event_header *header,
5682 struct perf_sample_data *data,
cdd6c482 5683 struct perf_event *event,
5622f295 5684 struct pt_regs *regs)
7b732a75 5685{
cdd6c482 5686 u64 sample_type = event->attr.sample_type;
7b732a75 5687
cdd6c482 5688 header->type = PERF_RECORD_SAMPLE;
c320c7b7 5689 header->size = sizeof(*header) + event->header_size;
5622f295
MM
5690
5691 header->misc = 0;
5692 header->misc |= perf_misc_flags(regs);
6fab0192 5693
c980d109 5694 __perf_event_header__init_id(header, data, event);
6844c09d 5695
c320c7b7 5696 if (sample_type & PERF_SAMPLE_IP)
5622f295
MM
5697 data->ip = perf_instruction_pointer(regs);
5698
b23f3325 5699 if (sample_type & PERF_SAMPLE_CALLCHAIN) {
5622f295 5700 int size = 1;
394ee076 5701
e6dab5ff 5702 data->callchain = perf_callchain(event, regs);
5622f295
MM
5703
5704 if (data->callchain)
5705 size += data->callchain->nr;
5706
5707 header->size += size * sizeof(u64);
394ee076
PZ
5708 }
5709
3a43ce68 5710 if (sample_type & PERF_SAMPLE_RAW) {
a044560c
PZ
5711 int size = sizeof(u32);
5712
5713 if (data->raw)
5714 size += data->raw->size;
5715 else
5716 size += sizeof(u32);
5717
fa128e6a 5718 header->size += round_up(size, sizeof(u64));
7f453c24 5719 }
bce38cd5
SE
5720
5721 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
5722 int size = sizeof(u64); /* nr */
5723 if (data->br_stack) {
5724 size += data->br_stack->nr
5725 * sizeof(struct perf_branch_entry);
5726 }
5727 header->size += size;
5728 }
4018994f 5729
2565711f 5730 if (sample_type & (PERF_SAMPLE_REGS_USER | PERF_SAMPLE_STACK_USER))
88a7c26a
AL
5731 perf_sample_regs_user(&data->regs_user, regs,
5732 &data->regs_user_copy);
2565711f 5733
4018994f
JO
5734 if (sample_type & PERF_SAMPLE_REGS_USER) {
5735 /* regs dump ABI info */
5736 int size = sizeof(u64);
5737
4018994f
JO
5738 if (data->regs_user.regs) {
5739 u64 mask = event->attr.sample_regs_user;
5740 size += hweight64(mask) * sizeof(u64);
5741 }
5742
5743 header->size += size;
5744 }
c5ebcedb
JO
5745
5746 if (sample_type & PERF_SAMPLE_STACK_USER) {
5747 /*
5748 * Either we need PERF_SAMPLE_STACK_USER bit to be allways
5749 * processed as the last one or have additional check added
5750 * in case new sample type is added, because we could eat
5751 * up the rest of the sample size.
5752 */
c5ebcedb
JO
5753 u16 stack_size = event->attr.sample_stack_user;
5754 u16 size = sizeof(u64);
5755
c5ebcedb 5756 stack_size = perf_sample_ustack_size(stack_size, header->size,
2565711f 5757 data->regs_user.regs);
c5ebcedb
JO
5758
5759 /*
5760 * If there is something to dump, add space for the dump
5761 * itself and for the field that tells the dynamic size,
5762 * which is how many have been actually dumped.
5763 */
5764 if (stack_size)
5765 size += sizeof(u64) + stack_size;
5766
5767 data->stack_user_size = stack_size;
5768 header->size += size;
5769 }
60e2364e
SE
5770
5771 if (sample_type & PERF_SAMPLE_REGS_INTR) {
5772 /* regs dump ABI info */
5773 int size = sizeof(u64);
5774
5775 perf_sample_regs_intr(&data->regs_intr, regs);
5776
5777 if (data->regs_intr.regs) {
5778 u64 mask = event->attr.sample_regs_intr;
5779
5780 size += hweight64(mask) * sizeof(u64);
5781 }
5782
5783 header->size += size;
5784 }
5622f295 5785}
7f453c24 5786
9ecda41a
WN
5787static void __always_inline
5788__perf_event_output(struct perf_event *event,
5789 struct perf_sample_data *data,
5790 struct pt_regs *regs,
5791 int (*output_begin)(struct perf_output_handle *,
5792 struct perf_event *,
5793 unsigned int))
5622f295
MM
5794{
5795 struct perf_output_handle handle;
5796 struct perf_event_header header;
689802b2 5797
927c7a9e
FW
5798 /* protect the callchain buffers */
5799 rcu_read_lock();
5800
cdd6c482 5801 perf_prepare_sample(&header, data, event, regs);
5c148194 5802
9ecda41a 5803 if (output_begin(&handle, event, header.size))
927c7a9e 5804 goto exit;
0322cd6e 5805
cdd6c482 5806 perf_output_sample(&handle, &header, data, event);
f413cdb8 5807
8a057d84 5808 perf_output_end(&handle);
927c7a9e
FW
5809
5810exit:
5811 rcu_read_unlock();
0322cd6e
PZ
5812}
5813
9ecda41a
WN
5814void
5815perf_event_output_forward(struct perf_event *event,
5816 struct perf_sample_data *data,
5817 struct pt_regs *regs)
5818{
5819 __perf_event_output(event, data, regs, perf_output_begin_forward);
5820}
5821
5822void
5823perf_event_output_backward(struct perf_event *event,
5824 struct perf_sample_data *data,
5825 struct pt_regs *regs)
5826{
5827 __perf_event_output(event, data, regs, perf_output_begin_backward);
5828}
5829
5830void
5831perf_event_output(struct perf_event *event,
5832 struct perf_sample_data *data,
5833 struct pt_regs *regs)
5834{
5835 __perf_event_output(event, data, regs, perf_output_begin);
5836}
5837
38b200d6 5838/*
cdd6c482 5839 * read event_id
38b200d6
PZ
5840 */
5841
5842struct perf_read_event {
5843 struct perf_event_header header;
5844
5845 u32 pid;
5846 u32 tid;
38b200d6
PZ
5847};
5848
5849static void
cdd6c482 5850perf_event_read_event(struct perf_event *event,
38b200d6
PZ
5851 struct task_struct *task)
5852{
5853 struct perf_output_handle handle;
c980d109 5854 struct perf_sample_data sample;
dfc65094 5855 struct perf_read_event read_event = {
38b200d6 5856 .header = {
cdd6c482 5857 .type = PERF_RECORD_READ,
38b200d6 5858 .misc = 0,
c320c7b7 5859 .size = sizeof(read_event) + event->read_size,
38b200d6 5860 },
cdd6c482
IM
5861 .pid = perf_event_pid(event, task),
5862 .tid = perf_event_tid(event, task),
38b200d6 5863 };
3dab77fb 5864 int ret;
38b200d6 5865
c980d109 5866 perf_event_header__init_id(&read_event.header, &sample, event);
a7ac67ea 5867 ret = perf_output_begin(&handle, event, read_event.header.size);
38b200d6
PZ
5868 if (ret)
5869 return;
5870
dfc65094 5871 perf_output_put(&handle, read_event);
cdd6c482 5872 perf_output_read(&handle, event);
c980d109 5873 perf_event__output_id_sample(event, &handle, &sample);
3dab77fb 5874
38b200d6
PZ
5875 perf_output_end(&handle);
5876}
5877
52d857a8
JO
5878typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
5879
5880static void
5881perf_event_aux_ctx(struct perf_event_context *ctx,
52d857a8 5882 perf_event_aux_output_cb output,
b73e4fef 5883 void *data, bool all)
52d857a8
JO
5884{
5885 struct perf_event *event;
5886
5887 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
b73e4fef
AS
5888 if (!all) {
5889 if (event->state < PERF_EVENT_STATE_INACTIVE)
5890 continue;
5891 if (!event_filter_match(event))
5892 continue;
5893 }
5894
67516844 5895 output(event, data);
52d857a8
JO
5896 }
5897}
5898
4e93ad60
JO
5899static void
5900perf_event_aux_task_ctx(perf_event_aux_output_cb output, void *data,
5901 struct perf_event_context *task_ctx)
5902{
5903 rcu_read_lock();
5904 preempt_disable();
b73e4fef 5905 perf_event_aux_ctx(task_ctx, output, data, false);
4e93ad60
JO
5906 preempt_enable();
5907 rcu_read_unlock();
5908}
5909
52d857a8 5910static void
67516844 5911perf_event_aux(perf_event_aux_output_cb output, void *data,
52d857a8
JO
5912 struct perf_event_context *task_ctx)
5913{
5914 struct perf_cpu_context *cpuctx;
5915 struct perf_event_context *ctx;
5916 struct pmu *pmu;
5917 int ctxn;
5918
4e93ad60
JO
5919 /*
5920 * If we have task_ctx != NULL we only notify
5921 * the task context itself. The task_ctx is set
5922 * only for EXIT events before releasing task
5923 * context.
5924 */
5925 if (task_ctx) {
5926 perf_event_aux_task_ctx(output, data, task_ctx);
5927 return;
5928 }
5929
52d857a8
JO
5930 rcu_read_lock();
5931 list_for_each_entry_rcu(pmu, &pmus, entry) {
5932 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
5933 if (cpuctx->unique_pmu != pmu)
5934 goto next;
b73e4fef 5935 perf_event_aux_ctx(&cpuctx->ctx, output, data, false);
52d857a8
JO
5936 ctxn = pmu->task_ctx_nr;
5937 if (ctxn < 0)
5938 goto next;
5939 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
5940 if (ctx)
b73e4fef 5941 perf_event_aux_ctx(ctx, output, data, false);
52d857a8
JO
5942next:
5943 put_cpu_ptr(pmu->pmu_cpu_context);
5944 }
52d857a8 5945 rcu_read_unlock();
95ff4ca2
AS
5946}
5947
375637bc
AS
5948/*
5949 * Clear all file-based filters at exec, they'll have to be
5950 * re-instated when/if these objects are mmapped again.
5951 */
5952static void perf_event_addr_filters_exec(struct perf_event *event, void *data)
5953{
5954 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
5955 struct perf_addr_filter *filter;
5956 unsigned int restart = 0, count = 0;
5957 unsigned long flags;
5958
5959 if (!has_addr_filter(event))
5960 return;
5961
5962 raw_spin_lock_irqsave(&ifh->lock, flags);
5963 list_for_each_entry(filter, &ifh->list, entry) {
5964 if (filter->inode) {
5965 event->addr_filters_offs[count] = 0;
5966 restart++;
5967 }
5968
5969 count++;
5970 }
5971
5972 if (restart)
5973 event->addr_filters_gen++;
5974 raw_spin_unlock_irqrestore(&ifh->lock, flags);
5975
5976 if (restart)
5977 perf_event_restart(event);
5978}
5979
5980void perf_event_exec(void)
5981{
5982 struct perf_event_context *ctx;
5983 int ctxn;
5984
5985 rcu_read_lock();
5986 for_each_task_context_nr(ctxn) {
5987 ctx = current->perf_event_ctxp[ctxn];
5988 if (!ctx)
5989 continue;
5990
5991 perf_event_enable_on_exec(ctxn);
5992
5993 perf_event_aux_ctx(ctx, perf_event_addr_filters_exec, NULL,
5994 true);
5995 }
5996 rcu_read_unlock();
5997}
5998
95ff4ca2
AS
5999struct remote_output {
6000 struct ring_buffer *rb;
6001 int err;
6002};
6003
6004static void __perf_event_output_stop(struct perf_event *event, void *data)
6005{
6006 struct perf_event *parent = event->parent;
6007 struct remote_output *ro = data;
6008 struct ring_buffer *rb = ro->rb;
375637bc
AS
6009 struct stop_event_data sd = {
6010 .event = event,
6011 };
95ff4ca2
AS
6012
6013 if (!has_aux(event))
6014 return;
6015
6016 if (!parent)
6017 parent = event;
6018
6019 /*
6020 * In case of inheritance, it will be the parent that links to the
6021 * ring-buffer, but it will be the child that's actually using it:
6022 */
6023 if (rcu_dereference(parent->rb) == rb)
375637bc 6024 ro->err = __perf_event_stop(&sd);
95ff4ca2
AS
6025}
6026
6027static int __perf_pmu_output_stop(void *info)
6028{
6029 struct perf_event *event = info;
6030 struct pmu *pmu = event->pmu;
6031 struct perf_cpu_context *cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
6032 struct remote_output ro = {
6033 .rb = event->rb,
6034 };
6035
6036 rcu_read_lock();
b73e4fef 6037 perf_event_aux_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false);
95ff4ca2
AS
6038 if (cpuctx->task_ctx)
6039 perf_event_aux_ctx(cpuctx->task_ctx, __perf_event_output_stop,
b73e4fef 6040 &ro, false);
95ff4ca2
AS
6041 rcu_read_unlock();
6042
6043 return ro.err;
6044}
6045
6046static void perf_pmu_output_stop(struct perf_event *event)
6047{
6048 struct perf_event *iter;
6049 int err, cpu;
6050
6051restart:
6052 rcu_read_lock();
6053 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) {
6054 /*
6055 * For per-CPU events, we need to make sure that neither they
6056 * nor their children are running; for cpu==-1 events it's
6057 * sufficient to stop the event itself if it's active, since
6058 * it can't have children.
6059 */
6060 cpu = iter->cpu;
6061 if (cpu == -1)
6062 cpu = READ_ONCE(iter->oncpu);
6063
6064 if (cpu == -1)
6065 continue;
6066
6067 err = cpu_function_call(cpu, __perf_pmu_output_stop, event);
6068 if (err == -EAGAIN) {
6069 rcu_read_unlock();
6070 goto restart;
6071 }
6072 }
6073 rcu_read_unlock();
52d857a8
JO
6074}
6075
60313ebe 6076/*
9f498cc5
PZ
6077 * task tracking -- fork/exit
6078 *
13d7a241 6079 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
60313ebe
PZ
6080 */
6081
9f498cc5 6082struct perf_task_event {
3a80b4a3 6083 struct task_struct *task;
cdd6c482 6084 struct perf_event_context *task_ctx;
60313ebe
PZ
6085
6086 struct {
6087 struct perf_event_header header;
6088
6089 u32 pid;
6090 u32 ppid;
9f498cc5
PZ
6091 u32 tid;
6092 u32 ptid;
393b2ad8 6093 u64 time;
cdd6c482 6094 } event_id;
60313ebe
PZ
6095};
6096
67516844
JO
6097static int perf_event_task_match(struct perf_event *event)
6098{
13d7a241
SE
6099 return event->attr.comm || event->attr.mmap ||
6100 event->attr.mmap2 || event->attr.mmap_data ||
6101 event->attr.task;
67516844
JO
6102}
6103
cdd6c482 6104static void perf_event_task_output(struct perf_event *event,
52d857a8 6105 void *data)
60313ebe 6106{
52d857a8 6107 struct perf_task_event *task_event = data;
60313ebe 6108 struct perf_output_handle handle;
c980d109 6109 struct perf_sample_data sample;
9f498cc5 6110 struct task_struct *task = task_event->task;
c980d109 6111 int ret, size = task_event->event_id.header.size;
8bb39f9a 6112
67516844
JO
6113 if (!perf_event_task_match(event))
6114 return;
6115
c980d109 6116 perf_event_header__init_id(&task_event->event_id.header, &sample, event);
60313ebe 6117
c980d109 6118 ret = perf_output_begin(&handle, event,
a7ac67ea 6119 task_event->event_id.header.size);
ef60777c 6120 if (ret)
c980d109 6121 goto out;
60313ebe 6122
cdd6c482
IM
6123 task_event->event_id.pid = perf_event_pid(event, task);
6124 task_event->event_id.ppid = perf_event_pid(event, current);
60313ebe 6125
cdd6c482
IM
6126 task_event->event_id.tid = perf_event_tid(event, task);
6127 task_event->event_id.ptid = perf_event_tid(event, current);
9f498cc5 6128
34f43927
PZ
6129 task_event->event_id.time = perf_event_clock(event);
6130
cdd6c482 6131 perf_output_put(&handle, task_event->event_id);
393b2ad8 6132
c980d109
ACM
6133 perf_event__output_id_sample(event, &handle, &sample);
6134
60313ebe 6135 perf_output_end(&handle);
c980d109
ACM
6136out:
6137 task_event->event_id.header.size = size;
60313ebe
PZ
6138}
6139
cdd6c482
IM
6140static void perf_event_task(struct task_struct *task,
6141 struct perf_event_context *task_ctx,
3a80b4a3 6142 int new)
60313ebe 6143{
9f498cc5 6144 struct perf_task_event task_event;
60313ebe 6145
cdd6c482
IM
6146 if (!atomic_read(&nr_comm_events) &&
6147 !atomic_read(&nr_mmap_events) &&
6148 !atomic_read(&nr_task_events))
60313ebe
PZ
6149 return;
6150
9f498cc5 6151 task_event = (struct perf_task_event){
3a80b4a3
PZ
6152 .task = task,
6153 .task_ctx = task_ctx,
cdd6c482 6154 .event_id = {
60313ebe 6155 .header = {
cdd6c482 6156 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
573402db 6157 .misc = 0,
cdd6c482 6158 .size = sizeof(task_event.event_id),
60313ebe 6159 },
573402db
PZ
6160 /* .pid */
6161 /* .ppid */
9f498cc5
PZ
6162 /* .tid */
6163 /* .ptid */
34f43927 6164 /* .time */
60313ebe
PZ
6165 },
6166 };
6167
67516844 6168 perf_event_aux(perf_event_task_output,
52d857a8
JO
6169 &task_event,
6170 task_ctx);
9f498cc5
PZ
6171}
6172
cdd6c482 6173void perf_event_fork(struct task_struct *task)
9f498cc5 6174{
cdd6c482 6175 perf_event_task(task, NULL, 1);
60313ebe
PZ
6176}
6177
8d1b2d93
PZ
6178/*
6179 * comm tracking
6180 */
6181
6182struct perf_comm_event {
22a4f650
IM
6183 struct task_struct *task;
6184 char *comm;
8d1b2d93
PZ
6185 int comm_size;
6186
6187 struct {
6188 struct perf_event_header header;
6189
6190 u32 pid;
6191 u32 tid;
cdd6c482 6192 } event_id;
8d1b2d93
PZ
6193};
6194
67516844
JO
6195static int perf_event_comm_match(struct perf_event *event)
6196{
6197 return event->attr.comm;
6198}
6199
cdd6c482 6200static void perf_event_comm_output(struct perf_event *event,
52d857a8 6201 void *data)
8d1b2d93 6202{
52d857a8 6203 struct perf_comm_event *comm_event = data;
8d1b2d93 6204 struct perf_output_handle handle;
c980d109 6205 struct perf_sample_data sample;
cdd6c482 6206 int size = comm_event->event_id.header.size;
c980d109
ACM
6207 int ret;
6208
67516844
JO
6209 if (!perf_event_comm_match(event))
6210 return;
6211
c980d109
ACM
6212 perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
6213 ret = perf_output_begin(&handle, event,
a7ac67ea 6214 comm_event->event_id.header.size);
8d1b2d93
PZ
6215
6216 if (ret)
c980d109 6217 goto out;
8d1b2d93 6218
cdd6c482
IM
6219 comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
6220 comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
709e50cf 6221
cdd6c482 6222 perf_output_put(&handle, comm_event->event_id);
76369139 6223 __output_copy(&handle, comm_event->comm,
8d1b2d93 6224 comm_event->comm_size);
c980d109
ACM
6225
6226 perf_event__output_id_sample(event, &handle, &sample);
6227
8d1b2d93 6228 perf_output_end(&handle);
c980d109
ACM
6229out:
6230 comm_event->event_id.header.size = size;
8d1b2d93
PZ
6231}
6232
cdd6c482 6233static void perf_event_comm_event(struct perf_comm_event *comm_event)
8d1b2d93 6234{
413ee3b4 6235 char comm[TASK_COMM_LEN];
8d1b2d93 6236 unsigned int size;
8d1b2d93 6237
413ee3b4 6238 memset(comm, 0, sizeof(comm));
96b02d78 6239 strlcpy(comm, comm_event->task->comm, sizeof(comm));
888fcee0 6240 size = ALIGN(strlen(comm)+1, sizeof(u64));
8d1b2d93
PZ
6241
6242 comm_event->comm = comm;
6243 comm_event->comm_size = size;
6244
cdd6c482 6245 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
8dc85d54 6246
67516844 6247 perf_event_aux(perf_event_comm_output,
52d857a8
JO
6248 comm_event,
6249 NULL);
8d1b2d93
PZ
6250}
6251
82b89778 6252void perf_event_comm(struct task_struct *task, bool exec)
8d1b2d93 6253{
9ee318a7
PZ
6254 struct perf_comm_event comm_event;
6255
cdd6c482 6256 if (!atomic_read(&nr_comm_events))
9ee318a7 6257 return;
a63eaf34 6258
9ee318a7 6259 comm_event = (struct perf_comm_event){
8d1b2d93 6260 .task = task,
573402db
PZ
6261 /* .comm */
6262 /* .comm_size */
cdd6c482 6263 .event_id = {
573402db 6264 .header = {
cdd6c482 6265 .type = PERF_RECORD_COMM,
82b89778 6266 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
573402db
PZ
6267 /* .size */
6268 },
6269 /* .pid */
6270 /* .tid */
8d1b2d93
PZ
6271 },
6272 };
6273
cdd6c482 6274 perf_event_comm_event(&comm_event);
8d1b2d93
PZ
6275}
6276
0a4a9391
PZ
6277/*
6278 * mmap tracking
6279 */
6280
6281struct perf_mmap_event {
089dd79d
PZ
6282 struct vm_area_struct *vma;
6283
6284 const char *file_name;
6285 int file_size;
13d7a241
SE
6286 int maj, min;
6287 u64 ino;
6288 u64 ino_generation;
f972eb63 6289 u32 prot, flags;
0a4a9391
PZ
6290
6291 struct {
6292 struct perf_event_header header;
6293
6294 u32 pid;
6295 u32 tid;
6296 u64 start;
6297 u64 len;
6298 u64 pgoff;
cdd6c482 6299 } event_id;
0a4a9391
PZ
6300};
6301
67516844
JO
6302static int perf_event_mmap_match(struct perf_event *event,
6303 void *data)
6304{
6305 struct perf_mmap_event *mmap_event = data;
6306 struct vm_area_struct *vma = mmap_event->vma;
6307 int executable = vma->vm_flags & VM_EXEC;
6308
6309 return (!executable && event->attr.mmap_data) ||
13d7a241 6310 (executable && (event->attr.mmap || event->attr.mmap2));
67516844
JO
6311}
6312
cdd6c482 6313static void perf_event_mmap_output(struct perf_event *event,
52d857a8 6314 void *data)
0a4a9391 6315{
52d857a8 6316 struct perf_mmap_event *mmap_event = data;
0a4a9391 6317 struct perf_output_handle handle;
c980d109 6318 struct perf_sample_data sample;
cdd6c482 6319 int size = mmap_event->event_id.header.size;
c980d109 6320 int ret;
0a4a9391 6321
67516844
JO
6322 if (!perf_event_mmap_match(event, data))
6323 return;
6324
13d7a241
SE
6325 if (event->attr.mmap2) {
6326 mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
6327 mmap_event->event_id.header.size += sizeof(mmap_event->maj);
6328 mmap_event->event_id.header.size += sizeof(mmap_event->min);
6329 mmap_event->event_id.header.size += sizeof(mmap_event->ino);
d008d525 6330 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
f972eb63
PZ
6331 mmap_event->event_id.header.size += sizeof(mmap_event->prot);
6332 mmap_event->event_id.header.size += sizeof(mmap_event->flags);
13d7a241
SE
6333 }
6334
c980d109
ACM
6335 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
6336 ret = perf_output_begin(&handle, event,
a7ac67ea 6337 mmap_event->event_id.header.size);
0a4a9391 6338 if (ret)
c980d109 6339 goto out;
0a4a9391 6340
cdd6c482
IM
6341 mmap_event->event_id.pid = perf_event_pid(event, current);
6342 mmap_event->event_id.tid = perf_event_tid(event, current);
709e50cf 6343
cdd6c482 6344 perf_output_put(&handle, mmap_event->event_id);
13d7a241
SE
6345
6346 if (event->attr.mmap2) {
6347 perf_output_put(&handle, mmap_event->maj);
6348 perf_output_put(&handle, mmap_event->min);
6349 perf_output_put(&handle, mmap_event->ino);
6350 perf_output_put(&handle, mmap_event->ino_generation);
f972eb63
PZ
6351 perf_output_put(&handle, mmap_event->prot);
6352 perf_output_put(&handle, mmap_event->flags);
13d7a241
SE
6353 }
6354
76369139 6355 __output_copy(&handle, mmap_event->file_name,
0a4a9391 6356 mmap_event->file_size);
c980d109
ACM
6357
6358 perf_event__output_id_sample(event, &handle, &sample);
6359
78d613eb 6360 perf_output_end(&handle);
c980d109
ACM
6361out:
6362 mmap_event->event_id.header.size = size;
0a4a9391
PZ
6363}
6364
cdd6c482 6365static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
0a4a9391 6366{
089dd79d
PZ
6367 struct vm_area_struct *vma = mmap_event->vma;
6368 struct file *file = vma->vm_file;
13d7a241
SE
6369 int maj = 0, min = 0;
6370 u64 ino = 0, gen = 0;
f972eb63 6371 u32 prot = 0, flags = 0;
0a4a9391
PZ
6372 unsigned int size;
6373 char tmp[16];
6374 char *buf = NULL;
2c42cfbf 6375 char *name;
413ee3b4 6376
0a4a9391 6377 if (file) {
13d7a241
SE
6378 struct inode *inode;
6379 dev_t dev;
3ea2f2b9 6380
2c42cfbf 6381 buf = kmalloc(PATH_MAX, GFP_KERNEL);
0a4a9391 6382 if (!buf) {
c7e548b4
ON
6383 name = "//enomem";
6384 goto cpy_name;
0a4a9391 6385 }
413ee3b4 6386 /*
3ea2f2b9 6387 * d_path() works from the end of the rb backwards, so we
413ee3b4
AB
6388 * need to add enough zero bytes after the string to handle
6389 * the 64bit alignment we do later.
6390 */
9bf39ab2 6391 name = file_path(file, buf, PATH_MAX - sizeof(u64));
0a4a9391 6392 if (IS_ERR(name)) {
c7e548b4
ON
6393 name = "//toolong";
6394 goto cpy_name;
0a4a9391 6395 }
13d7a241
SE
6396 inode = file_inode(vma->vm_file);
6397 dev = inode->i_sb->s_dev;
6398 ino = inode->i_ino;
6399 gen = inode->i_generation;
6400 maj = MAJOR(dev);
6401 min = MINOR(dev);
f972eb63
PZ
6402
6403 if (vma->vm_flags & VM_READ)
6404 prot |= PROT_READ;
6405 if (vma->vm_flags & VM_WRITE)
6406 prot |= PROT_WRITE;
6407 if (vma->vm_flags & VM_EXEC)
6408 prot |= PROT_EXEC;
6409
6410 if (vma->vm_flags & VM_MAYSHARE)
6411 flags = MAP_SHARED;
6412 else
6413 flags = MAP_PRIVATE;
6414
6415 if (vma->vm_flags & VM_DENYWRITE)
6416 flags |= MAP_DENYWRITE;
6417 if (vma->vm_flags & VM_MAYEXEC)
6418 flags |= MAP_EXECUTABLE;
6419 if (vma->vm_flags & VM_LOCKED)
6420 flags |= MAP_LOCKED;
6421 if (vma->vm_flags & VM_HUGETLB)
6422 flags |= MAP_HUGETLB;
6423
c7e548b4 6424 goto got_name;
0a4a9391 6425 } else {
fbe26abe
JO
6426 if (vma->vm_ops && vma->vm_ops->name) {
6427 name = (char *) vma->vm_ops->name(vma);
6428 if (name)
6429 goto cpy_name;
6430 }
6431
2c42cfbf 6432 name = (char *)arch_vma_name(vma);
c7e548b4
ON
6433 if (name)
6434 goto cpy_name;
089dd79d 6435
32c5fb7e 6436 if (vma->vm_start <= vma->vm_mm->start_brk &&
3af9e859 6437 vma->vm_end >= vma->vm_mm->brk) {
c7e548b4
ON
6438 name = "[heap]";
6439 goto cpy_name;
32c5fb7e
ON
6440 }
6441 if (vma->vm_start <= vma->vm_mm->start_stack &&
3af9e859 6442 vma->vm_end >= vma->vm_mm->start_stack) {
c7e548b4
ON
6443 name = "[stack]";
6444 goto cpy_name;
089dd79d
PZ
6445 }
6446
c7e548b4
ON
6447 name = "//anon";
6448 goto cpy_name;
0a4a9391
PZ
6449 }
6450
c7e548b4
ON
6451cpy_name:
6452 strlcpy(tmp, name, sizeof(tmp));
6453 name = tmp;
0a4a9391 6454got_name:
2c42cfbf
PZ
6455 /*
6456 * Since our buffer works in 8 byte units we need to align our string
6457 * size to a multiple of 8. However, we must guarantee the tail end is
6458 * zero'd out to avoid leaking random bits to userspace.
6459 */
6460 size = strlen(name)+1;
6461 while (!IS_ALIGNED(size, sizeof(u64)))
6462 name[size++] = '\0';
0a4a9391
PZ
6463
6464 mmap_event->file_name = name;
6465 mmap_event->file_size = size;
13d7a241
SE
6466 mmap_event->maj = maj;
6467 mmap_event->min = min;
6468 mmap_event->ino = ino;
6469 mmap_event->ino_generation = gen;
f972eb63
PZ
6470 mmap_event->prot = prot;
6471 mmap_event->flags = flags;
0a4a9391 6472
2fe85427
SE
6473 if (!(vma->vm_flags & VM_EXEC))
6474 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
6475
cdd6c482 6476 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
0a4a9391 6477
67516844 6478 perf_event_aux(perf_event_mmap_output,
52d857a8
JO
6479 mmap_event,
6480 NULL);
665c2142 6481
0a4a9391
PZ
6482 kfree(buf);
6483}
6484
375637bc
AS
6485/*
6486 * Whether this @filter depends on a dynamic object which is not loaded
6487 * yet or its load addresses are not known.
6488 */
6489static bool perf_addr_filter_needs_mmap(struct perf_addr_filter *filter)
6490{
6491 return filter->filter && filter->inode;
6492}
6493
6494/*
6495 * Check whether inode and address range match filter criteria.
6496 */
6497static bool perf_addr_filter_match(struct perf_addr_filter *filter,
6498 struct file *file, unsigned long offset,
6499 unsigned long size)
6500{
6501 if (filter->inode != file->f_inode)
6502 return false;
6503
6504 if (filter->offset > offset + size)
6505 return false;
6506
6507 if (filter->offset + filter->size < offset)
6508 return false;
6509
6510 return true;
6511}
6512
6513static void __perf_addr_filters_adjust(struct perf_event *event, void *data)
6514{
6515 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
6516 struct vm_area_struct *vma = data;
6517 unsigned long off = vma->vm_pgoff << PAGE_SHIFT, flags;
6518 struct file *file = vma->vm_file;
6519 struct perf_addr_filter *filter;
6520 unsigned int restart = 0, count = 0;
6521
6522 if (!has_addr_filter(event))
6523 return;
6524
6525 if (!file)
6526 return;
6527
6528 raw_spin_lock_irqsave(&ifh->lock, flags);
6529 list_for_each_entry(filter, &ifh->list, entry) {
6530 if (perf_addr_filter_match(filter, file, off,
6531 vma->vm_end - vma->vm_start)) {
6532 event->addr_filters_offs[count] = vma->vm_start;
6533 restart++;
6534 }
6535
6536 count++;
6537 }
6538
6539 if (restart)
6540 event->addr_filters_gen++;
6541 raw_spin_unlock_irqrestore(&ifh->lock, flags);
6542
6543 if (restart)
6544 perf_event_restart(event);
6545}
6546
6547/*
6548 * Adjust all task's events' filters to the new vma
6549 */
6550static void perf_addr_filters_adjust(struct vm_area_struct *vma)
6551{
6552 struct perf_event_context *ctx;
6553 int ctxn;
6554
6555 rcu_read_lock();
6556 for_each_task_context_nr(ctxn) {
6557 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
6558 if (!ctx)
6559 continue;
6560
6561 perf_event_aux_ctx(ctx, __perf_addr_filters_adjust, vma, true);
6562 }
6563 rcu_read_unlock();
6564}
6565
3af9e859 6566void perf_event_mmap(struct vm_area_struct *vma)
0a4a9391 6567{
9ee318a7
PZ
6568 struct perf_mmap_event mmap_event;
6569
cdd6c482 6570 if (!atomic_read(&nr_mmap_events))
9ee318a7
PZ
6571 return;
6572
6573 mmap_event = (struct perf_mmap_event){
089dd79d 6574 .vma = vma,
573402db
PZ
6575 /* .file_name */
6576 /* .file_size */
cdd6c482 6577 .event_id = {
573402db 6578 .header = {
cdd6c482 6579 .type = PERF_RECORD_MMAP,
39447b38 6580 .misc = PERF_RECORD_MISC_USER,
573402db
PZ
6581 /* .size */
6582 },
6583 /* .pid */
6584 /* .tid */
089dd79d
PZ
6585 .start = vma->vm_start,
6586 .len = vma->vm_end - vma->vm_start,
3a0304e9 6587 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
0a4a9391 6588 },
13d7a241
SE
6589 /* .maj (attr_mmap2 only) */
6590 /* .min (attr_mmap2 only) */
6591 /* .ino (attr_mmap2 only) */
6592 /* .ino_generation (attr_mmap2 only) */
f972eb63
PZ
6593 /* .prot (attr_mmap2 only) */
6594 /* .flags (attr_mmap2 only) */
0a4a9391
PZ
6595 };
6596
375637bc 6597 perf_addr_filters_adjust(vma);
cdd6c482 6598 perf_event_mmap_event(&mmap_event);
0a4a9391
PZ
6599}
6600
68db7e98
AS
6601void perf_event_aux_event(struct perf_event *event, unsigned long head,
6602 unsigned long size, u64 flags)
6603{
6604 struct perf_output_handle handle;
6605 struct perf_sample_data sample;
6606 struct perf_aux_event {
6607 struct perf_event_header header;
6608 u64 offset;
6609 u64 size;
6610 u64 flags;
6611 } rec = {
6612 .header = {
6613 .type = PERF_RECORD_AUX,
6614 .misc = 0,
6615 .size = sizeof(rec),
6616 },
6617 .offset = head,
6618 .size = size,
6619 .flags = flags,
6620 };
6621 int ret;
6622
6623 perf_event_header__init_id(&rec.header, &sample, event);
6624 ret = perf_output_begin(&handle, event, rec.header.size);
6625
6626 if (ret)
6627 return;
6628
6629 perf_output_put(&handle, rec);
6630 perf_event__output_id_sample(event, &handle, &sample);
6631
6632 perf_output_end(&handle);
6633}
6634
f38b0dbb
KL
6635/*
6636 * Lost/dropped samples logging
6637 */
6638void perf_log_lost_samples(struct perf_event *event, u64 lost)
6639{
6640 struct perf_output_handle handle;
6641 struct perf_sample_data sample;
6642 int ret;
6643
6644 struct {
6645 struct perf_event_header header;
6646 u64 lost;
6647 } lost_samples_event = {
6648 .header = {
6649 .type = PERF_RECORD_LOST_SAMPLES,
6650 .misc = 0,
6651 .size = sizeof(lost_samples_event),
6652 },
6653 .lost = lost,
6654 };
6655
6656 perf_event_header__init_id(&lost_samples_event.header, &sample, event);
6657
6658 ret = perf_output_begin(&handle, event,
6659 lost_samples_event.header.size);
6660 if (ret)
6661 return;
6662
6663 perf_output_put(&handle, lost_samples_event);
6664 perf_event__output_id_sample(event, &handle, &sample);
6665 perf_output_end(&handle);
6666}
6667
45ac1403
AH
6668/*
6669 * context_switch tracking
6670 */
6671
6672struct perf_switch_event {
6673 struct task_struct *task;
6674 struct task_struct *next_prev;
6675
6676 struct {
6677 struct perf_event_header header;
6678 u32 next_prev_pid;
6679 u32 next_prev_tid;
6680 } event_id;
6681};
6682
6683static int perf_event_switch_match(struct perf_event *event)
6684{
6685 return event->attr.context_switch;
6686}
6687
6688static void perf_event_switch_output(struct perf_event *event, void *data)
6689{
6690 struct perf_switch_event *se = data;
6691 struct perf_output_handle handle;
6692 struct perf_sample_data sample;
6693 int ret;
6694
6695 if (!perf_event_switch_match(event))
6696 return;
6697
6698 /* Only CPU-wide events are allowed to see next/prev pid/tid */
6699 if (event->ctx->task) {
6700 se->event_id.header.type = PERF_RECORD_SWITCH;
6701 se->event_id.header.size = sizeof(se->event_id.header);
6702 } else {
6703 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE;
6704 se->event_id.header.size = sizeof(se->event_id);
6705 se->event_id.next_prev_pid =
6706 perf_event_pid(event, se->next_prev);
6707 se->event_id.next_prev_tid =
6708 perf_event_tid(event, se->next_prev);
6709 }
6710
6711 perf_event_header__init_id(&se->event_id.header, &sample, event);
6712
6713 ret = perf_output_begin(&handle, event, se->event_id.header.size);
6714 if (ret)
6715 return;
6716
6717 if (event->ctx->task)
6718 perf_output_put(&handle, se->event_id.header);
6719 else
6720 perf_output_put(&handle, se->event_id);
6721
6722 perf_event__output_id_sample(event, &handle, &sample);
6723
6724 perf_output_end(&handle);
6725}
6726
6727static void perf_event_switch(struct task_struct *task,
6728 struct task_struct *next_prev, bool sched_in)
6729{
6730 struct perf_switch_event switch_event;
6731
6732 /* N.B. caller checks nr_switch_events != 0 */
6733
6734 switch_event = (struct perf_switch_event){
6735 .task = task,
6736 .next_prev = next_prev,
6737 .event_id = {
6738 .header = {
6739 /* .type */
6740 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT,
6741 /* .size */
6742 },
6743 /* .next_prev_pid */
6744 /* .next_prev_tid */
6745 },
6746 };
6747
6748 perf_event_aux(perf_event_switch_output,
6749 &switch_event,
6750 NULL);
6751}
6752
a78ac325
PZ
6753/*
6754 * IRQ throttle logging
6755 */
6756
cdd6c482 6757static void perf_log_throttle(struct perf_event *event, int enable)
a78ac325
PZ
6758{
6759 struct perf_output_handle handle;
c980d109 6760 struct perf_sample_data sample;
a78ac325
PZ
6761 int ret;
6762
6763 struct {
6764 struct perf_event_header header;
6765 u64 time;
cca3f454 6766 u64 id;
7f453c24 6767 u64 stream_id;
a78ac325
PZ
6768 } throttle_event = {
6769 .header = {
cdd6c482 6770 .type = PERF_RECORD_THROTTLE,
a78ac325
PZ
6771 .misc = 0,
6772 .size = sizeof(throttle_event),
6773 },
34f43927 6774 .time = perf_event_clock(event),
cdd6c482
IM
6775 .id = primary_event_id(event),
6776 .stream_id = event->id,
a78ac325
PZ
6777 };
6778
966ee4d6 6779 if (enable)
cdd6c482 6780 throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
966ee4d6 6781
c980d109
ACM
6782 perf_event_header__init_id(&throttle_event.header, &sample, event);
6783
6784 ret = perf_output_begin(&handle, event,
a7ac67ea 6785 throttle_event.header.size);
a78ac325
PZ
6786 if (ret)
6787 return;
6788
6789 perf_output_put(&handle, throttle_event);
c980d109 6790 perf_event__output_id_sample(event, &handle, &sample);
a78ac325
PZ
6791 perf_output_end(&handle);
6792}
6793
ec0d7729
AS
6794static void perf_log_itrace_start(struct perf_event *event)
6795{
6796 struct perf_output_handle handle;
6797 struct perf_sample_data sample;
6798 struct perf_aux_event {
6799 struct perf_event_header header;
6800 u32 pid;
6801 u32 tid;
6802 } rec;
6803 int ret;
6804
6805 if (event->parent)
6806 event = event->parent;
6807
6808 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) ||
6809 event->hw.itrace_started)
6810 return;
6811
ec0d7729
AS
6812 rec.header.type = PERF_RECORD_ITRACE_START;
6813 rec.header.misc = 0;
6814 rec.header.size = sizeof(rec);
6815 rec.pid = perf_event_pid(event, current);
6816 rec.tid = perf_event_tid(event, current);
6817
6818 perf_event_header__init_id(&rec.header, &sample, event);
6819 ret = perf_output_begin(&handle, event, rec.header.size);
6820
6821 if (ret)
6822 return;
6823
6824 perf_output_put(&handle, rec);
6825 perf_event__output_id_sample(event, &handle, &sample);
6826
6827 perf_output_end(&handle);
6828}
6829
f6c7d5fe 6830/*
cdd6c482 6831 * Generic event overflow handling, sampling.
f6c7d5fe
PZ
6832 */
6833
a8b0ca17 6834static int __perf_event_overflow(struct perf_event *event,
5622f295
MM
6835 int throttle, struct perf_sample_data *data,
6836 struct pt_regs *regs)
f6c7d5fe 6837{
cdd6c482
IM
6838 int events = atomic_read(&event->event_limit);
6839 struct hw_perf_event *hwc = &event->hw;
e050e3f0 6840 u64 seq;
79f14641
PZ
6841 int ret = 0;
6842
96398826
PZ
6843 /*
6844 * Non-sampling counters might still use the PMI to fold short
6845 * hardware counters, ignore those.
6846 */
6847 if (unlikely(!is_sampling_event(event)))
6848 return 0;
6849
e050e3f0
SE
6850 seq = __this_cpu_read(perf_throttled_seq);
6851 if (seq != hwc->interrupts_seq) {
6852 hwc->interrupts_seq = seq;
6853 hwc->interrupts = 1;
6854 } else {
6855 hwc->interrupts++;
6856 if (unlikely(throttle
6857 && hwc->interrupts >= max_samples_per_tick)) {
6858 __this_cpu_inc(perf_throttled_count);
555e0c1e 6859 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS);
163ec435
PZ
6860 hwc->interrupts = MAX_INTERRUPTS;
6861 perf_log_throttle(event, 0);
a78ac325
PZ
6862 ret = 1;
6863 }
e050e3f0 6864 }
60db5e09 6865
cdd6c482 6866 if (event->attr.freq) {
def0a9b2 6867 u64 now = perf_clock();
abd50713 6868 s64 delta = now - hwc->freq_time_stamp;
bd2b5b12 6869
abd50713 6870 hwc->freq_time_stamp = now;
bd2b5b12 6871
abd50713 6872 if (delta > 0 && delta < 2*TICK_NSEC)
f39d47ff 6873 perf_adjust_period(event, delta, hwc->last_period, true);
bd2b5b12
PZ
6874 }
6875
2023b359
PZ
6876 /*
6877 * XXX event_limit might not quite work as expected on inherited
cdd6c482 6878 * events
2023b359
PZ
6879 */
6880
cdd6c482
IM
6881 event->pending_kill = POLL_IN;
6882 if (events && atomic_dec_and_test(&event->event_limit)) {
79f14641 6883 ret = 1;
cdd6c482 6884 event->pending_kill = POLL_HUP;
a8b0ca17
PZ
6885 event->pending_disable = 1;
6886 irq_work_queue(&event->pending);
79f14641
PZ
6887 }
6888
1879445d 6889 event->overflow_handler(event, data, regs);
453f19ee 6890
fed66e2c 6891 if (*perf_event_fasync(event) && event->pending_kill) {
a8b0ca17
PZ
6892 event->pending_wakeup = 1;
6893 irq_work_queue(&event->pending);
f506b3dc
PZ
6894 }
6895
79f14641 6896 return ret;
f6c7d5fe
PZ
6897}
6898
a8b0ca17 6899int perf_event_overflow(struct perf_event *event,
5622f295
MM
6900 struct perf_sample_data *data,
6901 struct pt_regs *regs)
850bc73f 6902{
a8b0ca17 6903 return __perf_event_overflow(event, 1, data, regs);
850bc73f
PZ
6904}
6905
15dbf27c 6906/*
cdd6c482 6907 * Generic software event infrastructure
15dbf27c
PZ
6908 */
6909
b28ab83c
PZ
6910struct swevent_htable {
6911 struct swevent_hlist *swevent_hlist;
6912 struct mutex hlist_mutex;
6913 int hlist_refcount;
6914
6915 /* Recursion avoidance in each contexts */
6916 int recursion[PERF_NR_CONTEXTS];
6917};
6918
6919static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
6920
7b4b6658 6921/*
cdd6c482
IM
6922 * We directly increment event->count and keep a second value in
6923 * event->hw.period_left to count intervals. This period event
7b4b6658
PZ
6924 * is kept in the range [-sample_period, 0] so that we can use the
6925 * sign as trigger.
6926 */
6927
ab573844 6928u64 perf_swevent_set_period(struct perf_event *event)
15dbf27c 6929{
cdd6c482 6930 struct hw_perf_event *hwc = &event->hw;
7b4b6658
PZ
6931 u64 period = hwc->last_period;
6932 u64 nr, offset;
6933 s64 old, val;
6934
6935 hwc->last_period = hwc->sample_period;
15dbf27c
PZ
6936
6937again:
e7850595 6938 old = val = local64_read(&hwc->period_left);
7b4b6658
PZ
6939 if (val < 0)
6940 return 0;
15dbf27c 6941
7b4b6658
PZ
6942 nr = div64_u64(period + val, period);
6943 offset = nr * period;
6944 val -= offset;
e7850595 6945 if (local64_cmpxchg(&hwc->period_left, old, val) != old)
7b4b6658 6946 goto again;
15dbf27c 6947
7b4b6658 6948 return nr;
15dbf27c
PZ
6949}
6950
0cff784a 6951static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
a8b0ca17 6952 struct perf_sample_data *data,
5622f295 6953 struct pt_regs *regs)
15dbf27c 6954{
cdd6c482 6955 struct hw_perf_event *hwc = &event->hw;
850bc73f 6956 int throttle = 0;
15dbf27c 6957
0cff784a
PZ
6958 if (!overflow)
6959 overflow = perf_swevent_set_period(event);
15dbf27c 6960
7b4b6658
PZ
6961 if (hwc->interrupts == MAX_INTERRUPTS)
6962 return;
15dbf27c 6963
7b4b6658 6964 for (; overflow; overflow--) {
a8b0ca17 6965 if (__perf_event_overflow(event, throttle,
5622f295 6966 data, regs)) {
7b4b6658
PZ
6967 /*
6968 * We inhibit the overflow from happening when
6969 * hwc->interrupts == MAX_INTERRUPTS.
6970 */
6971 break;
6972 }
cf450a73 6973 throttle = 1;
7b4b6658 6974 }
15dbf27c
PZ
6975}
6976
a4eaf7f1 6977static void perf_swevent_event(struct perf_event *event, u64 nr,
a8b0ca17 6978 struct perf_sample_data *data,
5622f295 6979 struct pt_regs *regs)
7b4b6658 6980{
cdd6c482 6981 struct hw_perf_event *hwc = &event->hw;
d6d020e9 6982
e7850595 6983 local64_add(nr, &event->count);
d6d020e9 6984
0cff784a
PZ
6985 if (!regs)
6986 return;
6987
6c7e550f 6988 if (!is_sampling_event(event))
7b4b6658 6989 return;
d6d020e9 6990
5d81e5cf
AV
6991 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
6992 data->period = nr;
6993 return perf_swevent_overflow(event, 1, data, regs);
6994 } else
6995 data->period = event->hw.last_period;
6996
0cff784a 6997 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
a8b0ca17 6998 return perf_swevent_overflow(event, 1, data, regs);
0cff784a 6999
e7850595 7000 if (local64_add_negative(nr, &hwc->period_left))
7b4b6658 7001 return;
df1a132b 7002
a8b0ca17 7003 perf_swevent_overflow(event, 0, data, regs);
d6d020e9
PZ
7004}
7005
f5ffe02e
FW
7006static int perf_exclude_event(struct perf_event *event,
7007 struct pt_regs *regs)
7008{
a4eaf7f1 7009 if (event->hw.state & PERF_HES_STOPPED)
91b2f482 7010 return 1;
a4eaf7f1 7011
f5ffe02e
FW
7012 if (regs) {
7013 if (event->attr.exclude_user && user_mode(regs))
7014 return 1;
7015
7016 if (event->attr.exclude_kernel && !user_mode(regs))
7017 return 1;
7018 }
7019
7020 return 0;
7021}
7022
cdd6c482 7023static int perf_swevent_match(struct perf_event *event,
1c432d89 7024 enum perf_type_id type,
6fb2915d
LZ
7025 u32 event_id,
7026 struct perf_sample_data *data,
7027 struct pt_regs *regs)
15dbf27c 7028{
cdd6c482 7029 if (event->attr.type != type)
a21ca2ca 7030 return 0;
f5ffe02e 7031
cdd6c482 7032 if (event->attr.config != event_id)
15dbf27c
PZ
7033 return 0;
7034
f5ffe02e
FW
7035 if (perf_exclude_event(event, regs))
7036 return 0;
15dbf27c
PZ
7037
7038 return 1;
7039}
7040
76e1d904
FW
7041static inline u64 swevent_hash(u64 type, u32 event_id)
7042{
7043 u64 val = event_id | (type << 32);
7044
7045 return hash_64(val, SWEVENT_HLIST_BITS);
7046}
7047
49f135ed
FW
7048static inline struct hlist_head *
7049__find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
76e1d904 7050{
49f135ed
FW
7051 u64 hash = swevent_hash(type, event_id);
7052
7053 return &hlist->heads[hash];
7054}
76e1d904 7055
49f135ed
FW
7056/* For the read side: events when they trigger */
7057static inline struct hlist_head *
b28ab83c 7058find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
49f135ed
FW
7059{
7060 struct swevent_hlist *hlist;
76e1d904 7061
b28ab83c 7062 hlist = rcu_dereference(swhash->swevent_hlist);
76e1d904
FW
7063 if (!hlist)
7064 return NULL;
7065
49f135ed
FW
7066 return __find_swevent_head(hlist, type, event_id);
7067}
7068
7069/* For the event head insertion and removal in the hlist */
7070static inline struct hlist_head *
b28ab83c 7071find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
49f135ed
FW
7072{
7073 struct swevent_hlist *hlist;
7074 u32 event_id = event->attr.config;
7075 u64 type = event->attr.type;
7076
7077 /*
7078 * Event scheduling is always serialized against hlist allocation
7079 * and release. Which makes the protected version suitable here.
7080 * The context lock guarantees that.
7081 */
b28ab83c 7082 hlist = rcu_dereference_protected(swhash->swevent_hlist,
49f135ed
FW
7083 lockdep_is_held(&event->ctx->lock));
7084 if (!hlist)
7085 return NULL;
7086
7087 return __find_swevent_head(hlist, type, event_id);
76e1d904
FW
7088}
7089
7090static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
a8b0ca17 7091 u64 nr,
76e1d904
FW
7092 struct perf_sample_data *data,
7093 struct pt_regs *regs)
15dbf27c 7094{
4a32fea9 7095 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
cdd6c482 7096 struct perf_event *event;
76e1d904 7097 struct hlist_head *head;
15dbf27c 7098
76e1d904 7099 rcu_read_lock();
b28ab83c 7100 head = find_swevent_head_rcu(swhash, type, event_id);
76e1d904
FW
7101 if (!head)
7102 goto end;
7103
b67bfe0d 7104 hlist_for_each_entry_rcu(event, head, hlist_entry) {
6fb2915d 7105 if (perf_swevent_match(event, type, event_id, data, regs))
a8b0ca17 7106 perf_swevent_event(event, nr, data, regs);
15dbf27c 7107 }
76e1d904
FW
7108end:
7109 rcu_read_unlock();
15dbf27c
PZ
7110}
7111
86038c5e
PZI
7112DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
7113
4ed7c92d 7114int perf_swevent_get_recursion_context(void)
96f6d444 7115{
4a32fea9 7116 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
96f6d444 7117
b28ab83c 7118 return get_recursion_context(swhash->recursion);
96f6d444 7119}
645e8cc0 7120EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
96f6d444 7121
98b5c2c6 7122void perf_swevent_put_recursion_context(int rctx)
15dbf27c 7123{
4a32fea9 7124 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
927c7a9e 7125
b28ab83c 7126 put_recursion_context(swhash->recursion, rctx);
ce71b9df 7127}
15dbf27c 7128
86038c5e 7129void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
b8e83514 7130{
a4234bfc 7131 struct perf_sample_data data;
4ed7c92d 7132
86038c5e 7133 if (WARN_ON_ONCE(!regs))
4ed7c92d 7134 return;
a4234bfc 7135
fd0d000b 7136 perf_sample_data_init(&data, addr, 0);
a8b0ca17 7137 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
86038c5e
PZI
7138}
7139
7140void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
7141{
7142 int rctx;
7143
7144 preempt_disable_notrace();
7145 rctx = perf_swevent_get_recursion_context();
7146 if (unlikely(rctx < 0))
7147 goto fail;
7148
7149 ___perf_sw_event(event_id, nr, regs, addr);
4ed7c92d
PZ
7150
7151 perf_swevent_put_recursion_context(rctx);
86038c5e 7152fail:
1c024eca 7153 preempt_enable_notrace();
b8e83514
PZ
7154}
7155
cdd6c482 7156static void perf_swevent_read(struct perf_event *event)
15dbf27c 7157{
15dbf27c
PZ
7158}
7159
a4eaf7f1 7160static int perf_swevent_add(struct perf_event *event, int flags)
15dbf27c 7161{
4a32fea9 7162 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
cdd6c482 7163 struct hw_perf_event *hwc = &event->hw;
76e1d904
FW
7164 struct hlist_head *head;
7165
6c7e550f 7166 if (is_sampling_event(event)) {
7b4b6658 7167 hwc->last_period = hwc->sample_period;
cdd6c482 7168 perf_swevent_set_period(event);
7b4b6658 7169 }
76e1d904 7170
a4eaf7f1
PZ
7171 hwc->state = !(flags & PERF_EF_START);
7172
b28ab83c 7173 head = find_swevent_head(swhash, event);
12ca6ad2 7174 if (WARN_ON_ONCE(!head))
76e1d904
FW
7175 return -EINVAL;
7176
7177 hlist_add_head_rcu(&event->hlist_entry, head);
6a694a60 7178 perf_event_update_userpage(event);
76e1d904 7179
15dbf27c
PZ
7180 return 0;
7181}
7182
a4eaf7f1 7183static void perf_swevent_del(struct perf_event *event, int flags)
15dbf27c 7184{
76e1d904 7185 hlist_del_rcu(&event->hlist_entry);
15dbf27c
PZ
7186}
7187
a4eaf7f1 7188static void perf_swevent_start(struct perf_event *event, int flags)
5c92d124 7189{
a4eaf7f1 7190 event->hw.state = 0;
d6d020e9 7191}
aa9c4c0f 7192
a4eaf7f1 7193static void perf_swevent_stop(struct perf_event *event, int flags)
d6d020e9 7194{
a4eaf7f1 7195 event->hw.state = PERF_HES_STOPPED;
bae43c99
IM
7196}
7197
49f135ed
FW
7198/* Deref the hlist from the update side */
7199static inline struct swevent_hlist *
b28ab83c 7200swevent_hlist_deref(struct swevent_htable *swhash)
49f135ed 7201{
b28ab83c
PZ
7202 return rcu_dereference_protected(swhash->swevent_hlist,
7203 lockdep_is_held(&swhash->hlist_mutex));
49f135ed
FW
7204}
7205
b28ab83c 7206static void swevent_hlist_release(struct swevent_htable *swhash)
76e1d904 7207{
b28ab83c 7208 struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
76e1d904 7209
49f135ed 7210 if (!hlist)
76e1d904
FW
7211 return;
7212
70691d4a 7213 RCU_INIT_POINTER(swhash->swevent_hlist, NULL);
fa4bbc4c 7214 kfree_rcu(hlist, rcu_head);
76e1d904
FW
7215}
7216
3b364d7b 7217static void swevent_hlist_put_cpu(int cpu)
76e1d904 7218{
b28ab83c 7219 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
76e1d904 7220
b28ab83c 7221 mutex_lock(&swhash->hlist_mutex);
76e1d904 7222
b28ab83c
PZ
7223 if (!--swhash->hlist_refcount)
7224 swevent_hlist_release(swhash);
76e1d904 7225
b28ab83c 7226 mutex_unlock(&swhash->hlist_mutex);
76e1d904
FW
7227}
7228
3b364d7b 7229static void swevent_hlist_put(void)
76e1d904
FW
7230{
7231 int cpu;
7232
76e1d904 7233 for_each_possible_cpu(cpu)
3b364d7b 7234 swevent_hlist_put_cpu(cpu);
76e1d904
FW
7235}
7236
3b364d7b 7237static int swevent_hlist_get_cpu(int cpu)
76e1d904 7238{
b28ab83c 7239 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
76e1d904
FW
7240 int err = 0;
7241
b28ab83c 7242 mutex_lock(&swhash->hlist_mutex);
b28ab83c 7243 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
76e1d904
FW
7244 struct swevent_hlist *hlist;
7245
7246 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
7247 if (!hlist) {
7248 err = -ENOMEM;
7249 goto exit;
7250 }
b28ab83c 7251 rcu_assign_pointer(swhash->swevent_hlist, hlist);
76e1d904 7252 }
b28ab83c 7253 swhash->hlist_refcount++;
9ed6060d 7254exit:
b28ab83c 7255 mutex_unlock(&swhash->hlist_mutex);
76e1d904
FW
7256
7257 return err;
7258}
7259
3b364d7b 7260static int swevent_hlist_get(void)
76e1d904 7261{
3b364d7b 7262 int err, cpu, failed_cpu;
76e1d904 7263
76e1d904
FW
7264 get_online_cpus();
7265 for_each_possible_cpu(cpu) {
3b364d7b 7266 err = swevent_hlist_get_cpu(cpu);
76e1d904
FW
7267 if (err) {
7268 failed_cpu = cpu;
7269 goto fail;
7270 }
7271 }
7272 put_online_cpus();
7273
7274 return 0;
9ed6060d 7275fail:
76e1d904
FW
7276 for_each_possible_cpu(cpu) {
7277 if (cpu == failed_cpu)
7278 break;
3b364d7b 7279 swevent_hlist_put_cpu(cpu);
76e1d904
FW
7280 }
7281
7282 put_online_cpus();
7283 return err;
7284}
7285
c5905afb 7286struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
95476b64 7287
b0a873eb
PZ
7288static void sw_perf_event_destroy(struct perf_event *event)
7289{
7290 u64 event_id = event->attr.config;
95476b64 7291
b0a873eb
PZ
7292 WARN_ON(event->parent);
7293
c5905afb 7294 static_key_slow_dec(&perf_swevent_enabled[event_id]);
3b364d7b 7295 swevent_hlist_put();
b0a873eb
PZ
7296}
7297
7298static int perf_swevent_init(struct perf_event *event)
7299{
8176cced 7300 u64 event_id = event->attr.config;
b0a873eb
PZ
7301
7302 if (event->attr.type != PERF_TYPE_SOFTWARE)
7303 return -ENOENT;
7304
2481c5fa
SE
7305 /*
7306 * no branch sampling for software events
7307 */
7308 if (has_branch_stack(event))
7309 return -EOPNOTSUPP;
7310
b0a873eb
PZ
7311 switch (event_id) {
7312 case PERF_COUNT_SW_CPU_CLOCK:
7313 case PERF_COUNT_SW_TASK_CLOCK:
7314 return -ENOENT;
7315
7316 default:
7317 break;
7318 }
7319
ce677831 7320 if (event_id >= PERF_COUNT_SW_MAX)
b0a873eb
PZ
7321 return -ENOENT;
7322
7323 if (!event->parent) {
7324 int err;
7325
3b364d7b 7326 err = swevent_hlist_get();
b0a873eb
PZ
7327 if (err)
7328 return err;
7329
c5905afb 7330 static_key_slow_inc(&perf_swevent_enabled[event_id]);
b0a873eb
PZ
7331 event->destroy = sw_perf_event_destroy;
7332 }
7333
7334 return 0;
7335}
7336
7337static struct pmu perf_swevent = {
89a1e187 7338 .task_ctx_nr = perf_sw_context,
95476b64 7339
34f43927
PZ
7340 .capabilities = PERF_PMU_CAP_NO_NMI,
7341
b0a873eb 7342 .event_init = perf_swevent_init,
a4eaf7f1
PZ
7343 .add = perf_swevent_add,
7344 .del = perf_swevent_del,
7345 .start = perf_swevent_start,
7346 .stop = perf_swevent_stop,
1c024eca 7347 .read = perf_swevent_read,
1c024eca
PZ
7348};
7349
b0a873eb
PZ
7350#ifdef CONFIG_EVENT_TRACING
7351
1c024eca
PZ
7352static int perf_tp_filter_match(struct perf_event *event,
7353 struct perf_sample_data *data)
7354{
7355 void *record = data->raw->data;
7356
b71b437e
PZ
7357 /* only top level events have filters set */
7358 if (event->parent)
7359 event = event->parent;
7360
1c024eca
PZ
7361 if (likely(!event->filter) || filter_match_preds(event->filter, record))
7362 return 1;
7363 return 0;
7364}
7365
7366static int perf_tp_event_match(struct perf_event *event,
7367 struct perf_sample_data *data,
7368 struct pt_regs *regs)
7369{
a0f7d0f7
FW
7370 if (event->hw.state & PERF_HES_STOPPED)
7371 return 0;
580d607c
PZ
7372 /*
7373 * All tracepoints are from kernel-space.
7374 */
7375 if (event->attr.exclude_kernel)
1c024eca
PZ
7376 return 0;
7377
7378 if (!perf_tp_filter_match(event, data))
7379 return 0;
7380
7381 return 1;
7382}
7383
85b67bcb
AS
7384void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
7385 struct trace_event_call *call, u64 count,
7386 struct pt_regs *regs, struct hlist_head *head,
7387 struct task_struct *task)
7388{
7389 struct bpf_prog *prog = call->prog;
7390
7391 if (prog) {
7392 *(struct pt_regs **)raw_data = regs;
7393 if (!trace_call_bpf(prog, raw_data) || hlist_empty(head)) {
7394 perf_swevent_put_recursion_context(rctx);
7395 return;
7396 }
7397 }
7398 perf_tp_event(call->event.type, count, raw_data, size, regs, head,
7399 rctx, task);
7400}
7401EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit);
7402
1e1dcd93 7403void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size,
e6dab5ff
AV
7404 struct pt_regs *regs, struct hlist_head *head, int rctx,
7405 struct task_struct *task)
95476b64
FW
7406{
7407 struct perf_sample_data data;
1c024eca 7408 struct perf_event *event;
1c024eca 7409
95476b64
FW
7410 struct perf_raw_record raw = {
7411 .size = entry_size,
7412 .data = record,
7413 };
7414
1e1dcd93 7415 perf_sample_data_init(&data, 0, 0);
95476b64
FW
7416 data.raw = &raw;
7417
1e1dcd93
AS
7418 perf_trace_buf_update(record, event_type);
7419
b67bfe0d 7420 hlist_for_each_entry_rcu(event, head, hlist_entry) {
1c024eca 7421 if (perf_tp_event_match(event, &data, regs))
a8b0ca17 7422 perf_swevent_event(event, count, &data, regs);
4f41c013 7423 }
ecc55f84 7424
e6dab5ff
AV
7425 /*
7426 * If we got specified a target task, also iterate its context and
7427 * deliver this event there too.
7428 */
7429 if (task && task != current) {
7430 struct perf_event_context *ctx;
7431 struct trace_entry *entry = record;
7432
7433 rcu_read_lock();
7434 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
7435 if (!ctx)
7436 goto unlock;
7437
7438 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
7439 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7440 continue;
7441 if (event->attr.config != entry->type)
7442 continue;
7443 if (perf_tp_event_match(event, &data, regs))
7444 perf_swevent_event(event, count, &data, regs);
7445 }
7446unlock:
7447 rcu_read_unlock();
7448 }
7449
ecc55f84 7450 perf_swevent_put_recursion_context(rctx);
95476b64
FW
7451}
7452EXPORT_SYMBOL_GPL(perf_tp_event);
7453
cdd6c482 7454static void tp_perf_event_destroy(struct perf_event *event)
e077df4f 7455{
1c024eca 7456 perf_trace_destroy(event);
e077df4f
PZ
7457}
7458
b0a873eb 7459static int perf_tp_event_init(struct perf_event *event)
e077df4f 7460{
76e1d904
FW
7461 int err;
7462
b0a873eb
PZ
7463 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7464 return -ENOENT;
7465
2481c5fa
SE
7466 /*
7467 * no branch sampling for tracepoint events
7468 */
7469 if (has_branch_stack(event))
7470 return -EOPNOTSUPP;
7471
1c024eca
PZ
7472 err = perf_trace_init(event);
7473 if (err)
b0a873eb 7474 return err;
e077df4f 7475
cdd6c482 7476 event->destroy = tp_perf_event_destroy;
e077df4f 7477
b0a873eb
PZ
7478 return 0;
7479}
7480
7481static struct pmu perf_tracepoint = {
89a1e187
PZ
7482 .task_ctx_nr = perf_sw_context,
7483
b0a873eb 7484 .event_init = perf_tp_event_init,
a4eaf7f1
PZ
7485 .add = perf_trace_add,
7486 .del = perf_trace_del,
7487 .start = perf_swevent_start,
7488 .stop = perf_swevent_stop,
b0a873eb 7489 .read = perf_swevent_read,
b0a873eb
PZ
7490};
7491
7492static inline void perf_tp_register(void)
7493{
2e80a82a 7494 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
e077df4f 7495}
6fb2915d 7496
6fb2915d
LZ
7497static void perf_event_free_filter(struct perf_event *event)
7498{
7499 ftrace_profile_free_filter(event);
7500}
7501
2541517c
AS
7502static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7503{
98b5c2c6 7504 bool is_kprobe, is_tracepoint;
2541517c
AS
7505 struct bpf_prog *prog;
7506
7507 if (event->attr.type != PERF_TYPE_TRACEPOINT)
7508 return -EINVAL;
7509
7510 if (event->tp_event->prog)
7511 return -EEXIST;
7512
98b5c2c6
AS
7513 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_UKPROBE;
7514 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
7515 if (!is_kprobe && !is_tracepoint)
7516 /* bpf programs can only be attached to u/kprobe or tracepoint */
2541517c
AS
7517 return -EINVAL;
7518
7519 prog = bpf_prog_get(prog_fd);
7520 if (IS_ERR(prog))
7521 return PTR_ERR(prog);
7522
98b5c2c6
AS
7523 if ((is_kprobe && prog->type != BPF_PROG_TYPE_KPROBE) ||
7524 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT)) {
2541517c
AS
7525 /* valid fd, but invalid bpf program type */
7526 bpf_prog_put(prog);
7527 return -EINVAL;
7528 }
7529
32bbe007
AS
7530 if (is_tracepoint) {
7531 int off = trace_event_get_offsets(event->tp_event);
7532
7533 if (prog->aux->max_ctx_offset > off) {
7534 bpf_prog_put(prog);
7535 return -EACCES;
7536 }
7537 }
2541517c
AS
7538 event->tp_event->prog = prog;
7539
7540 return 0;
7541}
7542
7543static void perf_event_free_bpf_prog(struct perf_event *event)
7544{
7545 struct bpf_prog *prog;
7546
7547 if (!event->tp_event)
7548 return;
7549
7550 prog = event->tp_event->prog;
7551 if (prog) {
7552 event->tp_event->prog = NULL;
ceb56070 7553 bpf_prog_put_rcu(prog);
2541517c
AS
7554 }
7555}
7556
e077df4f 7557#else
6fb2915d 7558
b0a873eb 7559static inline void perf_tp_register(void)
e077df4f 7560{
e077df4f 7561}
6fb2915d 7562
6fb2915d
LZ
7563static void perf_event_free_filter(struct perf_event *event)
7564{
7565}
7566
2541517c
AS
7567static int perf_event_set_bpf_prog(struct perf_event *event, u32 prog_fd)
7568{
7569 return -ENOENT;
7570}
7571
7572static void perf_event_free_bpf_prog(struct perf_event *event)
7573{
7574}
07b139c8 7575#endif /* CONFIG_EVENT_TRACING */
e077df4f 7576
24f1e32c 7577#ifdef CONFIG_HAVE_HW_BREAKPOINT
f5ffe02e 7578void perf_bp_event(struct perf_event *bp, void *data)
24f1e32c 7579{
f5ffe02e
FW
7580 struct perf_sample_data sample;
7581 struct pt_regs *regs = data;
7582
fd0d000b 7583 perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
f5ffe02e 7584
a4eaf7f1 7585 if (!bp->hw.state && !perf_exclude_event(bp, regs))
a8b0ca17 7586 perf_swevent_event(bp, 1, &sample, regs);
24f1e32c
FW
7587}
7588#endif
7589
375637bc
AS
7590/*
7591 * Allocate a new address filter
7592 */
7593static struct perf_addr_filter *
7594perf_addr_filter_new(struct perf_event *event, struct list_head *filters)
7595{
7596 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu);
7597 struct perf_addr_filter *filter;
7598
7599 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node);
7600 if (!filter)
7601 return NULL;
7602
7603 INIT_LIST_HEAD(&filter->entry);
7604 list_add_tail(&filter->entry, filters);
7605
7606 return filter;
7607}
7608
7609static void free_filters_list(struct list_head *filters)
7610{
7611 struct perf_addr_filter *filter, *iter;
7612
7613 list_for_each_entry_safe(filter, iter, filters, entry) {
7614 if (filter->inode)
7615 iput(filter->inode);
7616 list_del(&filter->entry);
7617 kfree(filter);
7618 }
7619}
7620
7621/*
7622 * Free existing address filters and optionally install new ones
7623 */
7624static void perf_addr_filters_splice(struct perf_event *event,
7625 struct list_head *head)
7626{
7627 unsigned long flags;
7628 LIST_HEAD(list);
7629
7630 if (!has_addr_filter(event))
7631 return;
7632
7633 /* don't bother with children, they don't have their own filters */
7634 if (event->parent)
7635 return;
7636
7637 raw_spin_lock_irqsave(&event->addr_filters.lock, flags);
7638
7639 list_splice_init(&event->addr_filters.list, &list);
7640 if (head)
7641 list_splice(head, &event->addr_filters.list);
7642
7643 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags);
7644
7645 free_filters_list(&list);
7646}
7647
7648/*
7649 * Scan through mm's vmas and see if one of them matches the
7650 * @filter; if so, adjust filter's address range.
7651 * Called with mm::mmap_sem down for reading.
7652 */
7653static unsigned long perf_addr_filter_apply(struct perf_addr_filter *filter,
7654 struct mm_struct *mm)
7655{
7656 struct vm_area_struct *vma;
7657
7658 for (vma = mm->mmap; vma; vma = vma->vm_next) {
7659 struct file *file = vma->vm_file;
7660 unsigned long off = vma->vm_pgoff << PAGE_SHIFT;
7661 unsigned long vma_size = vma->vm_end - vma->vm_start;
7662
7663 if (!file)
7664 continue;
7665
7666 if (!perf_addr_filter_match(filter, file, off, vma_size))
7667 continue;
7668
7669 return vma->vm_start;
7670 }
7671
7672 return 0;
7673}
7674
7675/*
7676 * Update event's address range filters based on the
7677 * task's existing mappings, if any.
7678 */
7679static void perf_event_addr_filters_apply(struct perf_event *event)
7680{
7681 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event);
7682 struct task_struct *task = READ_ONCE(event->ctx->task);
7683 struct perf_addr_filter *filter;
7684 struct mm_struct *mm = NULL;
7685 unsigned int count = 0;
7686 unsigned long flags;
7687
7688 /*
7689 * We may observe TASK_TOMBSTONE, which means that the event tear-down
7690 * will stop on the parent's child_mutex that our caller is also holding
7691 */
7692 if (task == TASK_TOMBSTONE)
7693 return;
7694
7695 mm = get_task_mm(event->ctx->task);
7696 if (!mm)
7697 goto restart;
7698
7699 down_read(&mm->mmap_sem);
7700
7701 raw_spin_lock_irqsave(&ifh->lock, flags);
7702 list_for_each_entry(filter, &ifh->list, entry) {
7703 event->addr_filters_offs[count] = 0;
7704
7705 if (perf_addr_filter_needs_mmap(filter))
7706 event->addr_filters_offs[count] =
7707 perf_addr_filter_apply(filter, mm);
7708
7709 count++;
7710 }
7711
7712 event->addr_filters_gen++;
7713 raw_spin_unlock_irqrestore(&ifh->lock, flags);
7714
7715 up_read(&mm->mmap_sem);
7716
7717 mmput(mm);
7718
7719restart:
7720 perf_event_restart(event);
7721}
7722
7723/*
7724 * Address range filtering: limiting the data to certain
7725 * instruction address ranges. Filters are ioctl()ed to us from
7726 * userspace as ascii strings.
7727 *
7728 * Filter string format:
7729 *
7730 * ACTION RANGE_SPEC
7731 * where ACTION is one of the
7732 * * "filter": limit the trace to this region
7733 * * "start": start tracing from this address
7734 * * "stop": stop tracing at this address/region;
7735 * RANGE_SPEC is
7736 * * for kernel addresses: <start address>[/<size>]
7737 * * for object files: <start address>[/<size>]@</path/to/object/file>
7738 *
7739 * if <size> is not specified, the range is treated as a single address.
7740 */
7741enum {
7742 IF_ACT_FILTER,
7743 IF_ACT_START,
7744 IF_ACT_STOP,
7745 IF_SRC_FILE,
7746 IF_SRC_KERNEL,
7747 IF_SRC_FILEADDR,
7748 IF_SRC_KERNELADDR,
7749};
7750
7751enum {
7752 IF_STATE_ACTION = 0,
7753 IF_STATE_SOURCE,
7754 IF_STATE_END,
7755};
7756
7757static const match_table_t if_tokens = {
7758 { IF_ACT_FILTER, "filter" },
7759 { IF_ACT_START, "start" },
7760 { IF_ACT_STOP, "stop" },
7761 { IF_SRC_FILE, "%u/%u@%s" },
7762 { IF_SRC_KERNEL, "%u/%u" },
7763 { IF_SRC_FILEADDR, "%u@%s" },
7764 { IF_SRC_KERNELADDR, "%u" },
7765};
7766
7767/*
7768 * Address filter string parser
7769 */
7770static int
7771perf_event_parse_addr_filter(struct perf_event *event, char *fstr,
7772 struct list_head *filters)
7773{
7774 struct perf_addr_filter *filter = NULL;
7775 char *start, *orig, *filename = NULL;
7776 struct path path;
7777 substring_t args[MAX_OPT_ARGS];
7778 int state = IF_STATE_ACTION, token;
7779 unsigned int kernel = 0;
7780 int ret = -EINVAL;
7781
7782 orig = fstr = kstrdup(fstr, GFP_KERNEL);
7783 if (!fstr)
7784 return -ENOMEM;
7785
7786 while ((start = strsep(&fstr, " ,\n")) != NULL) {
7787 ret = -EINVAL;
7788
7789 if (!*start)
7790 continue;
7791
7792 /* filter definition begins */
7793 if (state == IF_STATE_ACTION) {
7794 filter = perf_addr_filter_new(event, filters);
7795 if (!filter)
7796 goto fail;
7797 }
7798
7799 token = match_token(start, if_tokens, args);
7800 switch (token) {
7801 case IF_ACT_FILTER:
7802 case IF_ACT_START:
7803 filter->filter = 1;
7804
7805 case IF_ACT_STOP:
7806 if (state != IF_STATE_ACTION)
7807 goto fail;
7808
7809 state = IF_STATE_SOURCE;
7810 break;
7811
7812 case IF_SRC_KERNELADDR:
7813 case IF_SRC_KERNEL:
7814 kernel = 1;
7815
7816 case IF_SRC_FILEADDR:
7817 case IF_SRC_FILE:
7818 if (state != IF_STATE_SOURCE)
7819 goto fail;
7820
7821 if (token == IF_SRC_FILE || token == IF_SRC_KERNEL)
7822 filter->range = 1;
7823
7824 *args[0].to = 0;
7825 ret = kstrtoul(args[0].from, 0, &filter->offset);
7826 if (ret)
7827 goto fail;
7828
7829 if (filter->range) {
7830 *args[1].to = 0;
7831 ret = kstrtoul(args[1].from, 0, &filter->size);
7832 if (ret)
7833 goto fail;
7834 }
7835
7836 if (token == IF_SRC_FILE) {
7837 filename = match_strdup(&args[2]);
7838 if (!filename) {
7839 ret = -ENOMEM;
7840 goto fail;
7841 }
7842 }
7843
7844 state = IF_STATE_END;
7845 break;
7846
7847 default:
7848 goto fail;
7849 }
7850
7851 /*
7852 * Filter definition is fully parsed, validate and install it.
7853 * Make sure that it doesn't contradict itself or the event's
7854 * attribute.
7855 */
7856 if (state == IF_STATE_END) {
7857 if (kernel && event->attr.exclude_kernel)
7858 goto fail;
7859
7860 if (!kernel) {
7861 if (!filename)
7862 goto fail;
7863
7864 /* look up the path and grab its inode */
7865 ret = kern_path(filename, LOOKUP_FOLLOW, &path);
7866 if (ret)
7867 goto fail_free_name;
7868
7869 filter->inode = igrab(d_inode(path.dentry));
7870 path_put(&path);
7871 kfree(filename);
7872 filename = NULL;
7873
7874 ret = -EINVAL;
7875 if (!filter->inode ||
7876 !S_ISREG(filter->inode->i_mode))
7877 /* free_filters_list() will iput() */
7878 goto fail;
7879 }
7880
7881 /* ready to consume more filters */
7882 state = IF_STATE_ACTION;
7883 filter = NULL;
7884 }
7885 }
7886
7887 if (state != IF_STATE_ACTION)
7888 goto fail;
7889
7890 kfree(orig);
7891
7892 return 0;
7893
7894fail_free_name:
7895 kfree(filename);
7896fail:
7897 free_filters_list(filters);
7898 kfree(orig);
7899
7900 return ret;
7901}
7902
7903static int
7904perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
7905{
7906 LIST_HEAD(filters);
7907 int ret;
7908
7909 /*
7910 * Since this is called in perf_ioctl() path, we're already holding
7911 * ctx::mutex.
7912 */
7913 lockdep_assert_held(&event->ctx->mutex);
7914
7915 if (WARN_ON_ONCE(event->parent))
7916 return -EINVAL;
7917
7918 /*
7919 * For now, we only support filtering in per-task events; doing so
7920 * for CPU-wide events requires additional context switching trickery,
7921 * since same object code will be mapped at different virtual
7922 * addresses in different processes.
7923 */
7924 if (!event->ctx->task)
7925 return -EOPNOTSUPP;
7926
7927 ret = perf_event_parse_addr_filter(event, filter_str, &filters);
7928 if (ret)
7929 return ret;
7930
7931 ret = event->pmu->addr_filters_validate(&filters);
7932 if (ret) {
7933 free_filters_list(&filters);
7934 return ret;
7935 }
7936
7937 /* remove existing filters, if any */
7938 perf_addr_filters_splice(event, &filters);
7939
7940 /* install new filters */
7941 perf_event_for_each_child(event, perf_event_addr_filters_apply);
7942
7943 return ret;
7944}
7945
c796bbbe
AS
7946static int perf_event_set_filter(struct perf_event *event, void __user *arg)
7947{
7948 char *filter_str;
7949 int ret = -EINVAL;
7950
375637bc
AS
7951 if ((event->attr.type != PERF_TYPE_TRACEPOINT ||
7952 !IS_ENABLED(CONFIG_EVENT_TRACING)) &&
7953 !has_addr_filter(event))
c796bbbe
AS
7954 return -EINVAL;
7955
7956 filter_str = strndup_user(arg, PAGE_SIZE);
7957 if (IS_ERR(filter_str))
7958 return PTR_ERR(filter_str);
7959
7960 if (IS_ENABLED(CONFIG_EVENT_TRACING) &&
7961 event->attr.type == PERF_TYPE_TRACEPOINT)
7962 ret = ftrace_profile_set_filter(event, event->attr.config,
7963 filter_str);
375637bc
AS
7964 else if (has_addr_filter(event))
7965 ret = perf_event_set_addr_filter(event, filter_str);
c796bbbe
AS
7966
7967 kfree(filter_str);
7968 return ret;
7969}
7970
b0a873eb
PZ
7971/*
7972 * hrtimer based swevent callback
7973 */
f29ac756 7974
b0a873eb 7975static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
f29ac756 7976{
b0a873eb
PZ
7977 enum hrtimer_restart ret = HRTIMER_RESTART;
7978 struct perf_sample_data data;
7979 struct pt_regs *regs;
7980 struct perf_event *event;
7981 u64 period;
f29ac756 7982
b0a873eb 7983 event = container_of(hrtimer, struct perf_event, hw.hrtimer);
ba3dd36c
PZ
7984
7985 if (event->state != PERF_EVENT_STATE_ACTIVE)
7986 return HRTIMER_NORESTART;
7987
b0a873eb 7988 event->pmu->read(event);
f344011c 7989
fd0d000b 7990 perf_sample_data_init(&data, 0, event->hw.last_period);
b0a873eb
PZ
7991 regs = get_irq_regs();
7992
7993 if (regs && !perf_exclude_event(event, regs)) {
77aeeebd 7994 if (!(event->attr.exclude_idle && is_idle_task(current)))
33b07b8b 7995 if (__perf_event_overflow(event, 1, &data, regs))
b0a873eb
PZ
7996 ret = HRTIMER_NORESTART;
7997 }
24f1e32c 7998
b0a873eb
PZ
7999 period = max_t(u64, 10000, event->hw.sample_period);
8000 hrtimer_forward_now(hrtimer, ns_to_ktime(period));
24f1e32c 8001
b0a873eb 8002 return ret;
f29ac756
PZ
8003}
8004
b0a873eb 8005static void perf_swevent_start_hrtimer(struct perf_event *event)
5c92d124 8006{
b0a873eb 8007 struct hw_perf_event *hwc = &event->hw;
5d508e82
FBH
8008 s64 period;
8009
8010 if (!is_sampling_event(event))
8011 return;
f5ffe02e 8012
5d508e82
FBH
8013 period = local64_read(&hwc->period_left);
8014 if (period) {
8015 if (period < 0)
8016 period = 10000;
fa407f35 8017
5d508e82
FBH
8018 local64_set(&hwc->period_left, 0);
8019 } else {
8020 period = max_t(u64, 10000, hwc->sample_period);
8021 }
3497d206
TG
8022 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period),
8023 HRTIMER_MODE_REL_PINNED);
24f1e32c 8024}
b0a873eb
PZ
8025
8026static void perf_swevent_cancel_hrtimer(struct perf_event *event)
24f1e32c 8027{
b0a873eb
PZ
8028 struct hw_perf_event *hwc = &event->hw;
8029
6c7e550f 8030 if (is_sampling_event(event)) {
b0a873eb 8031 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
fa407f35 8032 local64_set(&hwc->period_left, ktime_to_ns(remaining));
b0a873eb
PZ
8033
8034 hrtimer_cancel(&hwc->hrtimer);
8035 }
24f1e32c
FW
8036}
8037
ba3dd36c
PZ
8038static void perf_swevent_init_hrtimer(struct perf_event *event)
8039{
8040 struct hw_perf_event *hwc = &event->hw;
8041
8042 if (!is_sampling_event(event))
8043 return;
8044
8045 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
8046 hwc->hrtimer.function = perf_swevent_hrtimer;
8047
8048 /*
8049 * Since hrtimers have a fixed rate, we can do a static freq->period
8050 * mapping and avoid the whole period adjust feedback stuff.
8051 */
8052 if (event->attr.freq) {
8053 long freq = event->attr.sample_freq;
8054
8055 event->attr.sample_period = NSEC_PER_SEC / freq;
8056 hwc->sample_period = event->attr.sample_period;
8057 local64_set(&hwc->period_left, hwc->sample_period);
778141e3 8058 hwc->last_period = hwc->sample_period;
ba3dd36c
PZ
8059 event->attr.freq = 0;
8060 }
8061}
8062
b0a873eb
PZ
8063/*
8064 * Software event: cpu wall time clock
8065 */
8066
8067static void cpu_clock_event_update(struct perf_event *event)
24f1e32c 8068{
b0a873eb
PZ
8069 s64 prev;
8070 u64 now;
8071
a4eaf7f1 8072 now = local_clock();
b0a873eb
PZ
8073 prev = local64_xchg(&event->hw.prev_count, now);
8074 local64_add(now - prev, &event->count);
24f1e32c 8075}
24f1e32c 8076
a4eaf7f1 8077static void cpu_clock_event_start(struct perf_event *event, int flags)
b0a873eb 8078{
a4eaf7f1 8079 local64_set(&event->hw.prev_count, local_clock());
b0a873eb 8080 perf_swevent_start_hrtimer(event);
b0a873eb
PZ
8081}
8082
a4eaf7f1 8083static void cpu_clock_event_stop(struct perf_event *event, int flags)
f29ac756 8084{
b0a873eb
PZ
8085 perf_swevent_cancel_hrtimer(event);
8086 cpu_clock_event_update(event);
8087}
f29ac756 8088
a4eaf7f1
PZ
8089static int cpu_clock_event_add(struct perf_event *event, int flags)
8090{
8091 if (flags & PERF_EF_START)
8092 cpu_clock_event_start(event, flags);
6a694a60 8093 perf_event_update_userpage(event);
a4eaf7f1
PZ
8094
8095 return 0;
8096}
8097
8098static void cpu_clock_event_del(struct perf_event *event, int flags)
8099{
8100 cpu_clock_event_stop(event, flags);
8101}
8102
b0a873eb
PZ
8103static void cpu_clock_event_read(struct perf_event *event)
8104{
8105 cpu_clock_event_update(event);
8106}
f344011c 8107
b0a873eb
PZ
8108static int cpu_clock_event_init(struct perf_event *event)
8109{
8110 if (event->attr.type != PERF_TYPE_SOFTWARE)
8111 return -ENOENT;
8112
8113 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
8114 return -ENOENT;
8115
2481c5fa
SE
8116 /*
8117 * no branch sampling for software events
8118 */
8119 if (has_branch_stack(event))
8120 return -EOPNOTSUPP;
8121
ba3dd36c
PZ
8122 perf_swevent_init_hrtimer(event);
8123
b0a873eb 8124 return 0;
f29ac756
PZ
8125}
8126
b0a873eb 8127static struct pmu perf_cpu_clock = {
89a1e187
PZ
8128 .task_ctx_nr = perf_sw_context,
8129
34f43927
PZ
8130 .capabilities = PERF_PMU_CAP_NO_NMI,
8131
b0a873eb 8132 .event_init = cpu_clock_event_init,
a4eaf7f1
PZ
8133 .add = cpu_clock_event_add,
8134 .del = cpu_clock_event_del,
8135 .start = cpu_clock_event_start,
8136 .stop = cpu_clock_event_stop,
b0a873eb
PZ
8137 .read = cpu_clock_event_read,
8138};
8139
8140/*
8141 * Software event: task time clock
8142 */
8143
8144static void task_clock_event_update(struct perf_event *event, u64 now)
5c92d124 8145{
b0a873eb
PZ
8146 u64 prev;
8147 s64 delta;
5c92d124 8148
b0a873eb
PZ
8149 prev = local64_xchg(&event->hw.prev_count, now);
8150 delta = now - prev;
8151 local64_add(delta, &event->count);
8152}
5c92d124 8153
a4eaf7f1 8154static void task_clock_event_start(struct perf_event *event, int flags)
b0a873eb 8155{
a4eaf7f1 8156 local64_set(&event->hw.prev_count, event->ctx->time);
b0a873eb 8157 perf_swevent_start_hrtimer(event);
b0a873eb
PZ
8158}
8159
a4eaf7f1 8160static void task_clock_event_stop(struct perf_event *event, int flags)
b0a873eb
PZ
8161{
8162 perf_swevent_cancel_hrtimer(event);
8163 task_clock_event_update(event, event->ctx->time);
a4eaf7f1
PZ
8164}
8165
8166static int task_clock_event_add(struct perf_event *event, int flags)
8167{
8168 if (flags & PERF_EF_START)
8169 task_clock_event_start(event, flags);
6a694a60 8170 perf_event_update_userpage(event);
b0a873eb 8171
a4eaf7f1
PZ
8172 return 0;
8173}
8174
8175static void task_clock_event_del(struct perf_event *event, int flags)
8176{
8177 task_clock_event_stop(event, PERF_EF_UPDATE);
b0a873eb
PZ
8178}
8179
8180static void task_clock_event_read(struct perf_event *event)
8181{
768a06e2
PZ
8182 u64 now = perf_clock();
8183 u64 delta = now - event->ctx->timestamp;
8184 u64 time = event->ctx->time + delta;
b0a873eb
PZ
8185
8186 task_clock_event_update(event, time);
8187}
8188
8189static int task_clock_event_init(struct perf_event *event)
6fb2915d 8190{
b0a873eb
PZ
8191 if (event->attr.type != PERF_TYPE_SOFTWARE)
8192 return -ENOENT;
8193
8194 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
8195 return -ENOENT;
8196
2481c5fa
SE
8197 /*
8198 * no branch sampling for software events
8199 */
8200 if (has_branch_stack(event))
8201 return -EOPNOTSUPP;
8202
ba3dd36c
PZ
8203 perf_swevent_init_hrtimer(event);
8204
b0a873eb 8205 return 0;
6fb2915d
LZ
8206}
8207
b0a873eb 8208static struct pmu perf_task_clock = {
89a1e187
PZ
8209 .task_ctx_nr = perf_sw_context,
8210
34f43927
PZ
8211 .capabilities = PERF_PMU_CAP_NO_NMI,
8212
b0a873eb 8213 .event_init = task_clock_event_init,
a4eaf7f1
PZ
8214 .add = task_clock_event_add,
8215 .del = task_clock_event_del,
8216 .start = task_clock_event_start,
8217 .stop = task_clock_event_stop,
b0a873eb
PZ
8218 .read = task_clock_event_read,
8219};
6fb2915d 8220
ad5133b7 8221static void perf_pmu_nop_void(struct pmu *pmu)
e077df4f 8222{
e077df4f 8223}
6fb2915d 8224
fbbe0701
SB
8225static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags)
8226{
8227}
8228
ad5133b7 8229static int perf_pmu_nop_int(struct pmu *pmu)
6fb2915d 8230{
ad5133b7 8231 return 0;
6fb2915d
LZ
8232}
8233
18ab2cd3 8234static DEFINE_PER_CPU(unsigned int, nop_txn_flags);
fbbe0701
SB
8235
8236static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags)
6fb2915d 8237{
fbbe0701
SB
8238 __this_cpu_write(nop_txn_flags, flags);
8239
8240 if (flags & ~PERF_PMU_TXN_ADD)
8241 return;
8242
ad5133b7 8243 perf_pmu_disable(pmu);
6fb2915d
LZ
8244}
8245
ad5133b7
PZ
8246static int perf_pmu_commit_txn(struct pmu *pmu)
8247{
fbbe0701
SB
8248 unsigned int flags = __this_cpu_read(nop_txn_flags);
8249
8250 __this_cpu_write(nop_txn_flags, 0);
8251
8252 if (flags & ~PERF_PMU_TXN_ADD)
8253 return 0;
8254
ad5133b7
PZ
8255 perf_pmu_enable(pmu);
8256 return 0;
8257}
e077df4f 8258
ad5133b7 8259static void perf_pmu_cancel_txn(struct pmu *pmu)
24f1e32c 8260{
fbbe0701
SB
8261 unsigned int flags = __this_cpu_read(nop_txn_flags);
8262
8263 __this_cpu_write(nop_txn_flags, 0);
8264
8265 if (flags & ~PERF_PMU_TXN_ADD)
8266 return;
8267
ad5133b7 8268 perf_pmu_enable(pmu);
24f1e32c
FW
8269}
8270
35edc2a5
PZ
8271static int perf_event_idx_default(struct perf_event *event)
8272{
c719f560 8273 return 0;
35edc2a5
PZ
8274}
8275
8dc85d54
PZ
8276/*
8277 * Ensures all contexts with the same task_ctx_nr have the same
8278 * pmu_cpu_context too.
8279 */
9e317041 8280static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
24f1e32c 8281{
8dc85d54 8282 struct pmu *pmu;
b326e956 8283
8dc85d54
PZ
8284 if (ctxn < 0)
8285 return NULL;
24f1e32c 8286
8dc85d54
PZ
8287 list_for_each_entry(pmu, &pmus, entry) {
8288 if (pmu->task_ctx_nr == ctxn)
8289 return pmu->pmu_cpu_context;
8290 }
24f1e32c 8291
8dc85d54 8292 return NULL;
24f1e32c
FW
8293}
8294
51676957 8295static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
24f1e32c 8296{
51676957
PZ
8297 int cpu;
8298
8299 for_each_possible_cpu(cpu) {
8300 struct perf_cpu_context *cpuctx;
8301
8302 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8303
3f1f3320
PZ
8304 if (cpuctx->unique_pmu == old_pmu)
8305 cpuctx->unique_pmu = pmu;
51676957
PZ
8306 }
8307}
8308
8309static void free_pmu_context(struct pmu *pmu)
8310{
8311 struct pmu *i;
f5ffe02e 8312
8dc85d54 8313 mutex_lock(&pmus_lock);
0475f9ea 8314 /*
8dc85d54 8315 * Like a real lame refcount.
0475f9ea 8316 */
51676957
PZ
8317 list_for_each_entry(i, &pmus, entry) {
8318 if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
8319 update_pmu_context(i, pmu);
8dc85d54 8320 goto out;
51676957 8321 }
8dc85d54 8322 }
d6d020e9 8323
51676957 8324 free_percpu(pmu->pmu_cpu_context);
8dc85d54
PZ
8325out:
8326 mutex_unlock(&pmus_lock);
24f1e32c 8327}
6e855cd4
AS
8328
8329/*
8330 * Let userspace know that this PMU supports address range filtering:
8331 */
8332static ssize_t nr_addr_filters_show(struct device *dev,
8333 struct device_attribute *attr,
8334 char *page)
8335{
8336 struct pmu *pmu = dev_get_drvdata(dev);
8337
8338 return snprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters);
8339}
8340DEVICE_ATTR_RO(nr_addr_filters);
8341
2e80a82a 8342static struct idr pmu_idr;
d6d020e9 8343
abe43400
PZ
8344static ssize_t
8345type_show(struct device *dev, struct device_attribute *attr, char *page)
8346{
8347 struct pmu *pmu = dev_get_drvdata(dev);
8348
8349 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
8350}
90826ca7 8351static DEVICE_ATTR_RO(type);
abe43400 8352
62b85639
SE
8353static ssize_t
8354perf_event_mux_interval_ms_show(struct device *dev,
8355 struct device_attribute *attr,
8356 char *page)
8357{
8358 struct pmu *pmu = dev_get_drvdata(dev);
8359
8360 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
8361}
8362
272325c4
PZ
8363static DEFINE_MUTEX(mux_interval_mutex);
8364
62b85639
SE
8365static ssize_t
8366perf_event_mux_interval_ms_store(struct device *dev,
8367 struct device_attribute *attr,
8368 const char *buf, size_t count)
8369{
8370 struct pmu *pmu = dev_get_drvdata(dev);
8371 int timer, cpu, ret;
8372
8373 ret = kstrtoint(buf, 0, &timer);
8374 if (ret)
8375 return ret;
8376
8377 if (timer < 1)
8378 return -EINVAL;
8379
8380 /* same value, noting to do */
8381 if (timer == pmu->hrtimer_interval_ms)
8382 return count;
8383
272325c4 8384 mutex_lock(&mux_interval_mutex);
62b85639
SE
8385 pmu->hrtimer_interval_ms = timer;
8386
8387 /* update all cpuctx for this PMU */
272325c4
PZ
8388 get_online_cpus();
8389 for_each_online_cpu(cpu) {
62b85639
SE
8390 struct perf_cpu_context *cpuctx;
8391 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
8392 cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
8393
272325c4
PZ
8394 cpu_function_call(cpu,
8395 (remote_function_f)perf_mux_hrtimer_restart, cpuctx);
62b85639 8396 }
272325c4
PZ
8397 put_online_cpus();
8398 mutex_unlock(&mux_interval_mutex);
62b85639
SE
8399
8400 return count;
8401}
90826ca7 8402static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
62b85639 8403
90826ca7
GKH
8404static struct attribute *pmu_dev_attrs[] = {
8405 &dev_attr_type.attr,
8406 &dev_attr_perf_event_mux_interval_ms.attr,
8407 NULL,
abe43400 8408};
90826ca7 8409ATTRIBUTE_GROUPS(pmu_dev);
abe43400
PZ
8410
8411static int pmu_bus_running;
8412static struct bus_type pmu_bus = {
8413 .name = "event_source",
90826ca7 8414 .dev_groups = pmu_dev_groups,
abe43400
PZ
8415};
8416
8417static void pmu_dev_release(struct device *dev)
8418{
8419 kfree(dev);
8420}
8421
8422static int pmu_dev_alloc(struct pmu *pmu)
8423{
8424 int ret = -ENOMEM;
8425
8426 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
8427 if (!pmu->dev)
8428 goto out;
8429
0c9d42ed 8430 pmu->dev->groups = pmu->attr_groups;
abe43400
PZ
8431 device_initialize(pmu->dev);
8432 ret = dev_set_name(pmu->dev, "%s", pmu->name);
8433 if (ret)
8434 goto free_dev;
8435
8436 dev_set_drvdata(pmu->dev, pmu);
8437 pmu->dev->bus = &pmu_bus;
8438 pmu->dev->release = pmu_dev_release;
8439 ret = device_add(pmu->dev);
8440 if (ret)
8441 goto free_dev;
8442
6e855cd4
AS
8443 /* For PMUs with address filters, throw in an extra attribute: */
8444 if (pmu->nr_addr_filters)
8445 ret = device_create_file(pmu->dev, &dev_attr_nr_addr_filters);
8446
8447 if (ret)
8448 goto del_dev;
8449
abe43400
PZ
8450out:
8451 return ret;
8452
6e855cd4
AS
8453del_dev:
8454 device_del(pmu->dev);
8455
abe43400
PZ
8456free_dev:
8457 put_device(pmu->dev);
8458 goto out;
8459}
8460
547e9fd7 8461static struct lock_class_key cpuctx_mutex;
facc4307 8462static struct lock_class_key cpuctx_lock;
547e9fd7 8463
03d8e80b 8464int perf_pmu_register(struct pmu *pmu, const char *name, int type)
24f1e32c 8465{
108b02cf 8466 int cpu, ret;
24f1e32c 8467
b0a873eb 8468 mutex_lock(&pmus_lock);
33696fc0
PZ
8469 ret = -ENOMEM;
8470 pmu->pmu_disable_count = alloc_percpu(int);
8471 if (!pmu->pmu_disable_count)
8472 goto unlock;
f29ac756 8473
2e80a82a
PZ
8474 pmu->type = -1;
8475 if (!name)
8476 goto skip_type;
8477 pmu->name = name;
8478
8479 if (type < 0) {
0e9c3be2
TH
8480 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
8481 if (type < 0) {
8482 ret = type;
2e80a82a
PZ
8483 goto free_pdc;
8484 }
8485 }
8486 pmu->type = type;
8487
abe43400
PZ
8488 if (pmu_bus_running) {
8489 ret = pmu_dev_alloc(pmu);
8490 if (ret)
8491 goto free_idr;
8492 }
8493
2e80a82a 8494skip_type:
26657848
PZ
8495 if (pmu->task_ctx_nr == perf_hw_context) {
8496 static int hw_context_taken = 0;
8497
5101ef20
MR
8498 /*
8499 * Other than systems with heterogeneous CPUs, it never makes
8500 * sense for two PMUs to share perf_hw_context. PMUs which are
8501 * uncore must use perf_invalid_context.
8502 */
8503 if (WARN_ON_ONCE(hw_context_taken &&
8504 !(pmu->capabilities & PERF_PMU_CAP_HETEROGENEOUS_CPUS)))
26657848
PZ
8505 pmu->task_ctx_nr = perf_invalid_context;
8506
8507 hw_context_taken = 1;
8508 }
8509
8dc85d54
PZ
8510 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
8511 if (pmu->pmu_cpu_context)
8512 goto got_cpu_context;
f29ac756 8513
c4814202 8514 ret = -ENOMEM;
108b02cf
PZ
8515 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
8516 if (!pmu->pmu_cpu_context)
abe43400 8517 goto free_dev;
f344011c 8518
108b02cf
PZ
8519 for_each_possible_cpu(cpu) {
8520 struct perf_cpu_context *cpuctx;
8521
8522 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
eb184479 8523 __perf_event_init_context(&cpuctx->ctx);
547e9fd7 8524 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
facc4307 8525 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
108b02cf 8526 cpuctx->ctx.pmu = pmu;
9e630205 8527
272325c4 8528 __perf_mux_hrtimer_init(cpuctx, cpu);
9e630205 8529
3f1f3320 8530 cpuctx->unique_pmu = pmu;
108b02cf 8531 }
76e1d904 8532
8dc85d54 8533got_cpu_context:
ad5133b7
PZ
8534 if (!pmu->start_txn) {
8535 if (pmu->pmu_enable) {
8536 /*
8537 * If we have pmu_enable/pmu_disable calls, install
8538 * transaction stubs that use that to try and batch
8539 * hardware accesses.
8540 */
8541 pmu->start_txn = perf_pmu_start_txn;
8542 pmu->commit_txn = perf_pmu_commit_txn;
8543 pmu->cancel_txn = perf_pmu_cancel_txn;
8544 } else {
fbbe0701 8545 pmu->start_txn = perf_pmu_nop_txn;
ad5133b7
PZ
8546 pmu->commit_txn = perf_pmu_nop_int;
8547 pmu->cancel_txn = perf_pmu_nop_void;
f344011c 8548 }
5c92d124 8549 }
15dbf27c 8550
ad5133b7
PZ
8551 if (!pmu->pmu_enable) {
8552 pmu->pmu_enable = perf_pmu_nop_void;
8553 pmu->pmu_disable = perf_pmu_nop_void;
8554 }
8555
35edc2a5
PZ
8556 if (!pmu->event_idx)
8557 pmu->event_idx = perf_event_idx_default;
8558
b0a873eb 8559 list_add_rcu(&pmu->entry, &pmus);
bed5b25a 8560 atomic_set(&pmu->exclusive_cnt, 0);
33696fc0
PZ
8561 ret = 0;
8562unlock:
b0a873eb
PZ
8563 mutex_unlock(&pmus_lock);
8564
33696fc0 8565 return ret;
108b02cf 8566
abe43400
PZ
8567free_dev:
8568 device_del(pmu->dev);
8569 put_device(pmu->dev);
8570
2e80a82a
PZ
8571free_idr:
8572 if (pmu->type >= PERF_TYPE_MAX)
8573 idr_remove(&pmu_idr, pmu->type);
8574
108b02cf
PZ
8575free_pdc:
8576 free_percpu(pmu->pmu_disable_count);
8577 goto unlock;
f29ac756 8578}
c464c76e 8579EXPORT_SYMBOL_GPL(perf_pmu_register);
f29ac756 8580
b0a873eb 8581void perf_pmu_unregister(struct pmu *pmu)
5c92d124 8582{
b0a873eb
PZ
8583 mutex_lock(&pmus_lock);
8584 list_del_rcu(&pmu->entry);
8585 mutex_unlock(&pmus_lock);
5c92d124 8586
0475f9ea 8587 /*
cde8e884
PZ
8588 * We dereference the pmu list under both SRCU and regular RCU, so
8589 * synchronize against both of those.
0475f9ea 8590 */
b0a873eb 8591 synchronize_srcu(&pmus_srcu);
cde8e884 8592 synchronize_rcu();
d6d020e9 8593
33696fc0 8594 free_percpu(pmu->pmu_disable_count);
2e80a82a
PZ
8595 if (pmu->type >= PERF_TYPE_MAX)
8596 idr_remove(&pmu_idr, pmu->type);
6e855cd4
AS
8597 if (pmu->nr_addr_filters)
8598 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters);
abe43400
PZ
8599 device_del(pmu->dev);
8600 put_device(pmu->dev);
51676957 8601 free_pmu_context(pmu);
b0a873eb 8602}
c464c76e 8603EXPORT_SYMBOL_GPL(perf_pmu_unregister);
d6d020e9 8604
cc34b98b
MR
8605static int perf_try_init_event(struct pmu *pmu, struct perf_event *event)
8606{
ccd41c86 8607 struct perf_event_context *ctx = NULL;
cc34b98b
MR
8608 int ret;
8609
8610 if (!try_module_get(pmu->module))
8611 return -ENODEV;
ccd41c86
PZ
8612
8613 if (event->group_leader != event) {
8b10c5e2
PZ
8614 /*
8615 * This ctx->mutex can nest when we're called through
8616 * inheritance. See the perf_event_ctx_lock_nested() comment.
8617 */
8618 ctx = perf_event_ctx_lock_nested(event->group_leader,
8619 SINGLE_DEPTH_NESTING);
ccd41c86
PZ
8620 BUG_ON(!ctx);
8621 }
8622
cc34b98b
MR
8623 event->pmu = pmu;
8624 ret = pmu->event_init(event);
ccd41c86
PZ
8625
8626 if (ctx)
8627 perf_event_ctx_unlock(event->group_leader, ctx);
8628
cc34b98b
MR
8629 if (ret)
8630 module_put(pmu->module);
8631
8632 return ret;
8633}
8634
18ab2cd3 8635static struct pmu *perf_init_event(struct perf_event *event)
b0a873eb
PZ
8636{
8637 struct pmu *pmu = NULL;
8638 int idx;
940c5b29 8639 int ret;
b0a873eb
PZ
8640
8641 idx = srcu_read_lock(&pmus_srcu);
2e80a82a
PZ
8642
8643 rcu_read_lock();
8644 pmu = idr_find(&pmu_idr, event->attr.type);
8645 rcu_read_unlock();
940c5b29 8646 if (pmu) {
cc34b98b 8647 ret = perf_try_init_event(pmu, event);
940c5b29
LM
8648 if (ret)
8649 pmu = ERR_PTR(ret);
2e80a82a 8650 goto unlock;
940c5b29 8651 }
2e80a82a 8652
b0a873eb 8653 list_for_each_entry_rcu(pmu, &pmus, entry) {
cc34b98b 8654 ret = perf_try_init_event(pmu, event);
b0a873eb 8655 if (!ret)
e5f4d339 8656 goto unlock;
76e1d904 8657
b0a873eb
PZ
8658 if (ret != -ENOENT) {
8659 pmu = ERR_PTR(ret);
e5f4d339 8660 goto unlock;
f344011c 8661 }
5c92d124 8662 }
e5f4d339
PZ
8663 pmu = ERR_PTR(-ENOENT);
8664unlock:
b0a873eb 8665 srcu_read_unlock(&pmus_srcu, idx);
15dbf27c 8666
4aeb0b42 8667 return pmu;
5c92d124
IM
8668}
8669
4beb31f3
FW
8670static void account_event_cpu(struct perf_event *event, int cpu)
8671{
8672 if (event->parent)
8673 return;
8674
4beb31f3
FW
8675 if (is_cgroup_event(event))
8676 atomic_inc(&per_cpu(perf_cgroup_events, cpu));
8677}
8678
555e0c1e
FW
8679/* Freq events need the tick to stay alive (see perf_event_task_tick). */
8680static void account_freq_event_nohz(void)
8681{
8682#ifdef CONFIG_NO_HZ_FULL
8683 /* Lock so we don't race with concurrent unaccount */
8684 spin_lock(&nr_freq_lock);
8685 if (atomic_inc_return(&nr_freq_events) == 1)
8686 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS);
8687 spin_unlock(&nr_freq_lock);
8688#endif
8689}
8690
8691static void account_freq_event(void)
8692{
8693 if (tick_nohz_full_enabled())
8694 account_freq_event_nohz();
8695 else
8696 atomic_inc(&nr_freq_events);
8697}
8698
8699
766d6c07
FW
8700static void account_event(struct perf_event *event)
8701{
25432ae9
PZ
8702 bool inc = false;
8703
4beb31f3
FW
8704 if (event->parent)
8705 return;
8706
766d6c07 8707 if (event->attach_state & PERF_ATTACH_TASK)
25432ae9 8708 inc = true;
766d6c07
FW
8709 if (event->attr.mmap || event->attr.mmap_data)
8710 atomic_inc(&nr_mmap_events);
8711 if (event->attr.comm)
8712 atomic_inc(&nr_comm_events);
8713 if (event->attr.task)
8714 atomic_inc(&nr_task_events);
555e0c1e
FW
8715 if (event->attr.freq)
8716 account_freq_event();
45ac1403
AH
8717 if (event->attr.context_switch) {
8718 atomic_inc(&nr_switch_events);
25432ae9 8719 inc = true;
45ac1403 8720 }
4beb31f3 8721 if (has_branch_stack(event))
25432ae9 8722 inc = true;
4beb31f3 8723 if (is_cgroup_event(event))
25432ae9
PZ
8724 inc = true;
8725
9107c89e
PZ
8726 if (inc) {
8727 if (atomic_inc_not_zero(&perf_sched_count))
8728 goto enabled;
8729
8730 mutex_lock(&perf_sched_mutex);
8731 if (!atomic_read(&perf_sched_count)) {
8732 static_branch_enable(&perf_sched_events);
8733 /*
8734 * Guarantee that all CPUs observe they key change and
8735 * call the perf scheduling hooks before proceeding to
8736 * install events that need them.
8737 */
8738 synchronize_sched();
8739 }
8740 /*
8741 * Now that we have waited for the sync_sched(), allow further
8742 * increments to by-pass the mutex.
8743 */
8744 atomic_inc(&perf_sched_count);
8745 mutex_unlock(&perf_sched_mutex);
8746 }
8747enabled:
4beb31f3
FW
8748
8749 account_event_cpu(event, event->cpu);
766d6c07
FW
8750}
8751
0793a61d 8752/*
cdd6c482 8753 * Allocate and initialize a event structure
0793a61d 8754 */
cdd6c482 8755static struct perf_event *
c3f00c70 8756perf_event_alloc(struct perf_event_attr *attr, int cpu,
d580ff86
PZ
8757 struct task_struct *task,
8758 struct perf_event *group_leader,
8759 struct perf_event *parent_event,
4dc0da86 8760 perf_overflow_handler_t overflow_handler,
79dff51e 8761 void *context, int cgroup_fd)
0793a61d 8762{
51b0fe39 8763 struct pmu *pmu;
cdd6c482
IM
8764 struct perf_event *event;
8765 struct hw_perf_event *hwc;
90983b16 8766 long err = -EINVAL;
0793a61d 8767
66832eb4
ON
8768 if ((unsigned)cpu >= nr_cpu_ids) {
8769 if (!task || cpu != -1)
8770 return ERR_PTR(-EINVAL);
8771 }
8772
c3f00c70 8773 event = kzalloc(sizeof(*event), GFP_KERNEL);
cdd6c482 8774 if (!event)
d5d2bc0d 8775 return ERR_PTR(-ENOMEM);
0793a61d 8776
04289bb9 8777 /*
cdd6c482 8778 * Single events are their own group leaders, with an
04289bb9
IM
8779 * empty sibling list:
8780 */
8781 if (!group_leader)
cdd6c482 8782 group_leader = event;
04289bb9 8783
cdd6c482
IM
8784 mutex_init(&event->child_mutex);
8785 INIT_LIST_HEAD(&event->child_list);
fccc714b 8786
cdd6c482
IM
8787 INIT_LIST_HEAD(&event->group_entry);
8788 INIT_LIST_HEAD(&event->event_entry);
8789 INIT_LIST_HEAD(&event->sibling_list);
10c6db11 8790 INIT_LIST_HEAD(&event->rb_entry);
71ad88ef 8791 INIT_LIST_HEAD(&event->active_entry);
375637bc 8792 INIT_LIST_HEAD(&event->addr_filters.list);
f3ae75de
SE
8793 INIT_HLIST_NODE(&event->hlist_entry);
8794
10c6db11 8795
cdd6c482 8796 init_waitqueue_head(&event->waitq);
e360adbe 8797 init_irq_work(&event->pending, perf_pending_event);
0793a61d 8798
cdd6c482 8799 mutex_init(&event->mmap_mutex);
375637bc 8800 raw_spin_lock_init(&event->addr_filters.lock);
7b732a75 8801
a6fa941d 8802 atomic_long_set(&event->refcount, 1);
cdd6c482
IM
8803 event->cpu = cpu;
8804 event->attr = *attr;
8805 event->group_leader = group_leader;
8806 event->pmu = NULL;
cdd6c482 8807 event->oncpu = -1;
a96bbc16 8808
cdd6c482 8809 event->parent = parent_event;
b84fbc9f 8810
17cf22c3 8811 event->ns = get_pid_ns(task_active_pid_ns(current));
cdd6c482 8812 event->id = atomic64_inc_return(&perf_event_id);
a96bbc16 8813
cdd6c482 8814 event->state = PERF_EVENT_STATE_INACTIVE;
329d876d 8815
d580ff86
PZ
8816 if (task) {
8817 event->attach_state = PERF_ATTACH_TASK;
d580ff86 8818 /*
50f16a8b
PZ
8819 * XXX pmu::event_init needs to know what task to account to
8820 * and we cannot use the ctx information because we need the
8821 * pmu before we get a ctx.
d580ff86 8822 */
50f16a8b 8823 event->hw.target = task;
d580ff86
PZ
8824 }
8825
34f43927
PZ
8826 event->clock = &local_clock;
8827 if (parent_event)
8828 event->clock = parent_event->clock;
8829
4dc0da86 8830 if (!overflow_handler && parent_event) {
b326e956 8831 overflow_handler = parent_event->overflow_handler;
4dc0da86
AK
8832 context = parent_event->overflow_handler_context;
8833 }
66832eb4 8834
1879445d
WN
8835 if (overflow_handler) {
8836 event->overflow_handler = overflow_handler;
8837 event->overflow_handler_context = context;
9ecda41a
WN
8838 } else if (is_write_backward(event)){
8839 event->overflow_handler = perf_event_output_backward;
8840 event->overflow_handler_context = NULL;
1879445d 8841 } else {
9ecda41a 8842 event->overflow_handler = perf_event_output_forward;
1879445d
WN
8843 event->overflow_handler_context = NULL;
8844 }
97eaf530 8845
0231bb53 8846 perf_event__state_init(event);
a86ed508 8847
4aeb0b42 8848 pmu = NULL;
b8e83514 8849
cdd6c482 8850 hwc = &event->hw;
bd2b5b12 8851 hwc->sample_period = attr->sample_period;
0d48696f 8852 if (attr->freq && attr->sample_freq)
bd2b5b12 8853 hwc->sample_period = 1;
eced1dfc 8854 hwc->last_period = hwc->sample_period;
bd2b5b12 8855
e7850595 8856 local64_set(&hwc->period_left, hwc->sample_period);
60db5e09 8857
2023b359 8858 /*
cdd6c482 8859 * we currently do not support PERF_FORMAT_GROUP on inherited events
2023b359 8860 */
3dab77fb 8861 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
90983b16 8862 goto err_ns;
a46a2300
YZ
8863
8864 if (!has_branch_stack(event))
8865 event->attr.branch_sample_type = 0;
2023b359 8866
79dff51e
MF
8867 if (cgroup_fd != -1) {
8868 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader);
8869 if (err)
8870 goto err_ns;
8871 }
8872
b0a873eb 8873 pmu = perf_init_event(event);
4aeb0b42 8874 if (!pmu)
90983b16
FW
8875 goto err_ns;
8876 else if (IS_ERR(pmu)) {
4aeb0b42 8877 err = PTR_ERR(pmu);
90983b16 8878 goto err_ns;
621a01ea 8879 }
d5d2bc0d 8880
bed5b25a
AS
8881 err = exclusive_event_init(event);
8882 if (err)
8883 goto err_pmu;
8884
375637bc
AS
8885 if (has_addr_filter(event)) {
8886 event->addr_filters_offs = kcalloc(pmu->nr_addr_filters,
8887 sizeof(unsigned long),
8888 GFP_KERNEL);
8889 if (!event->addr_filters_offs)
8890 goto err_per_task;
8891
8892 /* force hw sync on the address filters */
8893 event->addr_filters_gen = 1;
8894 }
8895
cdd6c482 8896 if (!event->parent) {
927c7a9e
FW
8897 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
8898 err = get_callchain_buffers();
90983b16 8899 if (err)
375637bc 8900 goto err_addr_filters;
d010b332 8901 }
f344011c 8902 }
9ee318a7 8903
927a5570
AS
8904 /* symmetric to unaccount_event() in _free_event() */
8905 account_event(event);
8906
cdd6c482 8907 return event;
90983b16 8908
375637bc
AS
8909err_addr_filters:
8910 kfree(event->addr_filters_offs);
8911
bed5b25a
AS
8912err_per_task:
8913 exclusive_event_destroy(event);
8914
90983b16
FW
8915err_pmu:
8916 if (event->destroy)
8917 event->destroy(event);
c464c76e 8918 module_put(pmu->module);
90983b16 8919err_ns:
79dff51e
MF
8920 if (is_cgroup_event(event))
8921 perf_detach_cgroup(event);
90983b16
FW
8922 if (event->ns)
8923 put_pid_ns(event->ns);
8924 kfree(event);
8925
8926 return ERR_PTR(err);
0793a61d
TG
8927}
8928
cdd6c482
IM
8929static int perf_copy_attr(struct perf_event_attr __user *uattr,
8930 struct perf_event_attr *attr)
974802ea 8931{
974802ea 8932 u32 size;
cdf8073d 8933 int ret;
974802ea
PZ
8934
8935 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
8936 return -EFAULT;
8937
8938 /*
8939 * zero the full structure, so that a short copy will be nice.
8940 */
8941 memset(attr, 0, sizeof(*attr));
8942
8943 ret = get_user(size, &uattr->size);
8944 if (ret)
8945 return ret;
8946
8947 if (size > PAGE_SIZE) /* silly large */
8948 goto err_size;
8949
8950 if (!size) /* abi compat */
8951 size = PERF_ATTR_SIZE_VER0;
8952
8953 if (size < PERF_ATTR_SIZE_VER0)
8954 goto err_size;
8955
8956 /*
8957 * If we're handed a bigger struct than we know of,
cdf8073d
IS
8958 * ensure all the unknown bits are 0 - i.e. new
8959 * user-space does not rely on any kernel feature
8960 * extensions we dont know about yet.
974802ea
PZ
8961 */
8962 if (size > sizeof(*attr)) {
cdf8073d
IS
8963 unsigned char __user *addr;
8964 unsigned char __user *end;
8965 unsigned char val;
974802ea 8966
cdf8073d
IS
8967 addr = (void __user *)uattr + sizeof(*attr);
8968 end = (void __user *)uattr + size;
974802ea 8969
cdf8073d 8970 for (; addr < end; addr++) {
974802ea
PZ
8971 ret = get_user(val, addr);
8972 if (ret)
8973 return ret;
8974 if (val)
8975 goto err_size;
8976 }
b3e62e35 8977 size = sizeof(*attr);
974802ea
PZ
8978 }
8979
8980 ret = copy_from_user(attr, uattr, size);
8981 if (ret)
8982 return -EFAULT;
8983
cd757645 8984 if (attr->__reserved_1)
974802ea
PZ
8985 return -EINVAL;
8986
8987 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
8988 return -EINVAL;
8989
8990 if (attr->read_format & ~(PERF_FORMAT_MAX-1))
8991 return -EINVAL;
8992
bce38cd5
SE
8993 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
8994 u64 mask = attr->branch_sample_type;
8995
8996 /* only using defined bits */
8997 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
8998 return -EINVAL;
8999
9000 /* at least one branch bit must be set */
9001 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
9002 return -EINVAL;
9003
bce38cd5
SE
9004 /* propagate priv level, when not set for branch */
9005 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
9006
9007 /* exclude_kernel checked on syscall entry */
9008 if (!attr->exclude_kernel)
9009 mask |= PERF_SAMPLE_BRANCH_KERNEL;
9010
9011 if (!attr->exclude_user)
9012 mask |= PERF_SAMPLE_BRANCH_USER;
9013
9014 if (!attr->exclude_hv)
9015 mask |= PERF_SAMPLE_BRANCH_HV;
9016 /*
9017 * adjust user setting (for HW filter setup)
9018 */
9019 attr->branch_sample_type = mask;
9020 }
e712209a
SE
9021 /* privileged levels capture (kernel, hv): check permissions */
9022 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
2b923c8f
SE
9023 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9024 return -EACCES;
bce38cd5 9025 }
4018994f 9026
c5ebcedb 9027 if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
4018994f 9028 ret = perf_reg_validate(attr->sample_regs_user);
c5ebcedb
JO
9029 if (ret)
9030 return ret;
9031 }
9032
9033 if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
9034 if (!arch_perf_have_user_stack_dump())
9035 return -ENOSYS;
9036
9037 /*
9038 * We have __u32 type for the size, but so far
9039 * we can only use __u16 as maximum due to the
9040 * __u16 sample size limit.
9041 */
9042 if (attr->sample_stack_user >= USHRT_MAX)
9043 ret = -EINVAL;
9044 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
9045 ret = -EINVAL;
9046 }
4018994f 9047
60e2364e
SE
9048 if (attr->sample_type & PERF_SAMPLE_REGS_INTR)
9049 ret = perf_reg_validate(attr->sample_regs_intr);
974802ea
PZ
9050out:
9051 return ret;
9052
9053err_size:
9054 put_user(sizeof(*attr), &uattr->size);
9055 ret = -E2BIG;
9056 goto out;
9057}
9058
ac9721f3
PZ
9059static int
9060perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
a4be7c27 9061{
b69cf536 9062 struct ring_buffer *rb = NULL;
a4be7c27
PZ
9063 int ret = -EINVAL;
9064
ac9721f3 9065 if (!output_event)
a4be7c27
PZ
9066 goto set;
9067
ac9721f3
PZ
9068 /* don't allow circular references */
9069 if (event == output_event)
a4be7c27
PZ
9070 goto out;
9071
0f139300
PZ
9072 /*
9073 * Don't allow cross-cpu buffers
9074 */
9075 if (output_event->cpu != event->cpu)
9076 goto out;
9077
9078 /*
76369139 9079 * If its not a per-cpu rb, it must be the same task.
0f139300
PZ
9080 */
9081 if (output_event->cpu == -1 && output_event->ctx != event->ctx)
9082 goto out;
9083
34f43927
PZ
9084 /*
9085 * Mixing clocks in the same buffer is trouble you don't need.
9086 */
9087 if (output_event->clock != event->clock)
9088 goto out;
9089
9ecda41a
WN
9090 /*
9091 * Either writing ring buffer from beginning or from end.
9092 * Mixing is not allowed.
9093 */
9094 if (is_write_backward(output_event) != is_write_backward(event))
9095 goto out;
9096
45bfb2e5
PZ
9097 /*
9098 * If both events generate aux data, they must be on the same PMU
9099 */
9100 if (has_aux(event) && has_aux(output_event) &&
9101 event->pmu != output_event->pmu)
9102 goto out;
9103
a4be7c27 9104set:
cdd6c482 9105 mutex_lock(&event->mmap_mutex);
ac9721f3
PZ
9106 /* Can't redirect output if we've got an active mmap() */
9107 if (atomic_read(&event->mmap_count))
9108 goto unlock;
a4be7c27 9109
ac9721f3 9110 if (output_event) {
76369139
FW
9111 /* get the rb we want to redirect to */
9112 rb = ring_buffer_get(output_event);
9113 if (!rb)
ac9721f3 9114 goto unlock;
a4be7c27
PZ
9115 }
9116
b69cf536 9117 ring_buffer_attach(event, rb);
9bb5d40c 9118
a4be7c27 9119 ret = 0;
ac9721f3
PZ
9120unlock:
9121 mutex_unlock(&event->mmap_mutex);
9122
a4be7c27 9123out:
a4be7c27
PZ
9124 return ret;
9125}
9126
f63a8daa
PZ
9127static void mutex_lock_double(struct mutex *a, struct mutex *b)
9128{
9129 if (b < a)
9130 swap(a, b);
9131
9132 mutex_lock(a);
9133 mutex_lock_nested(b, SINGLE_DEPTH_NESTING);
9134}
9135
34f43927
PZ
9136static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id)
9137{
9138 bool nmi_safe = false;
9139
9140 switch (clk_id) {
9141 case CLOCK_MONOTONIC:
9142 event->clock = &ktime_get_mono_fast_ns;
9143 nmi_safe = true;
9144 break;
9145
9146 case CLOCK_MONOTONIC_RAW:
9147 event->clock = &ktime_get_raw_fast_ns;
9148 nmi_safe = true;
9149 break;
9150
9151 case CLOCK_REALTIME:
9152 event->clock = &ktime_get_real_ns;
9153 break;
9154
9155 case CLOCK_BOOTTIME:
9156 event->clock = &ktime_get_boot_ns;
9157 break;
9158
9159 case CLOCK_TAI:
9160 event->clock = &ktime_get_tai_ns;
9161 break;
9162
9163 default:
9164 return -EINVAL;
9165 }
9166
9167 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI))
9168 return -EINVAL;
9169
9170 return 0;
9171}
9172
0793a61d 9173/**
cdd6c482 9174 * sys_perf_event_open - open a performance event, associate it to a task/cpu
9f66a381 9175 *
cdd6c482 9176 * @attr_uptr: event_id type attributes for monitoring/sampling
0793a61d 9177 * @pid: target pid
9f66a381 9178 * @cpu: target cpu
cdd6c482 9179 * @group_fd: group leader event fd
0793a61d 9180 */
cdd6c482
IM
9181SYSCALL_DEFINE5(perf_event_open,
9182 struct perf_event_attr __user *, attr_uptr,
2743a5b0 9183 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
0793a61d 9184{
b04243ef
PZ
9185 struct perf_event *group_leader = NULL, *output_event = NULL;
9186 struct perf_event *event, *sibling;
cdd6c482 9187 struct perf_event_attr attr;
f63a8daa 9188 struct perf_event_context *ctx, *uninitialized_var(gctx);
cdd6c482 9189 struct file *event_file = NULL;
2903ff01 9190 struct fd group = {NULL, 0};
38a81da2 9191 struct task_struct *task = NULL;
89a1e187 9192 struct pmu *pmu;
ea635c64 9193 int event_fd;
b04243ef 9194 int move_group = 0;
dc86cabe 9195 int err;
a21b0b35 9196 int f_flags = O_RDWR;
79dff51e 9197 int cgroup_fd = -1;
0793a61d 9198
2743a5b0 9199 /* for future expandability... */
e5d1367f 9200 if (flags & ~PERF_FLAG_ALL)
2743a5b0
PM
9201 return -EINVAL;
9202
dc86cabe
IM
9203 err = perf_copy_attr(attr_uptr, &attr);
9204 if (err)
9205 return err;
eab656ae 9206
0764771d
PZ
9207 if (!attr.exclude_kernel) {
9208 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
9209 return -EACCES;
9210 }
9211
df58ab24 9212 if (attr.freq) {
cdd6c482 9213 if (attr.sample_freq > sysctl_perf_event_sample_rate)
df58ab24 9214 return -EINVAL;
0819b2e3
PZ
9215 } else {
9216 if (attr.sample_period & (1ULL << 63))
9217 return -EINVAL;
df58ab24
PZ
9218 }
9219
e5d1367f
SE
9220 /*
9221 * In cgroup mode, the pid argument is used to pass the fd
9222 * opened to the cgroup directory in cgroupfs. The cpu argument
9223 * designates the cpu on which to monitor threads from that
9224 * cgroup.
9225 */
9226 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
9227 return -EINVAL;
9228
a21b0b35
YD
9229 if (flags & PERF_FLAG_FD_CLOEXEC)
9230 f_flags |= O_CLOEXEC;
9231
9232 event_fd = get_unused_fd_flags(f_flags);
ea635c64
AV
9233 if (event_fd < 0)
9234 return event_fd;
9235
ac9721f3 9236 if (group_fd != -1) {
2903ff01
AV
9237 err = perf_fget_light(group_fd, &group);
9238 if (err)
d14b12d7 9239 goto err_fd;
2903ff01 9240 group_leader = group.file->private_data;
ac9721f3
PZ
9241 if (flags & PERF_FLAG_FD_OUTPUT)
9242 output_event = group_leader;
9243 if (flags & PERF_FLAG_FD_NO_GROUP)
9244 group_leader = NULL;
9245 }
9246
e5d1367f 9247 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
c6be5a5c
PZ
9248 task = find_lively_task_by_vpid(pid);
9249 if (IS_ERR(task)) {
9250 err = PTR_ERR(task);
9251 goto err_group_fd;
9252 }
9253 }
9254
1f4ee503
PZ
9255 if (task && group_leader &&
9256 group_leader->attr.inherit != attr.inherit) {
9257 err = -EINVAL;
9258 goto err_task;
9259 }
9260
fbfc623f
YZ
9261 get_online_cpus();
9262
79c9ce57
PZ
9263 if (task) {
9264 err = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
9265 if (err)
9266 goto err_cpus;
9267
9268 /*
9269 * Reuse ptrace permission checks for now.
9270 *
9271 * We must hold cred_guard_mutex across this and any potential
9272 * perf_install_in_context() call for this new event to
9273 * serialize against exec() altering our credentials (and the
9274 * perf_event_exit_task() that could imply).
9275 */
9276 err = -EACCES;
9277 if (!ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
9278 goto err_cred;
9279 }
9280
79dff51e
MF
9281 if (flags & PERF_FLAG_PID_CGROUP)
9282 cgroup_fd = pid;
9283
4dc0da86 9284 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
79dff51e 9285 NULL, NULL, cgroup_fd);
d14b12d7
SE
9286 if (IS_ERR(event)) {
9287 err = PTR_ERR(event);
79c9ce57 9288 goto err_cred;
d14b12d7
SE
9289 }
9290
53b25335
VW
9291 if (is_sampling_event(event)) {
9292 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
9293 err = -ENOTSUPP;
9294 goto err_alloc;
9295 }
9296 }
9297
89a1e187
PZ
9298 /*
9299 * Special case software events and allow them to be part of
9300 * any hardware group.
9301 */
9302 pmu = event->pmu;
b04243ef 9303
34f43927
PZ
9304 if (attr.use_clockid) {
9305 err = perf_event_set_clock(event, attr.clockid);
9306 if (err)
9307 goto err_alloc;
9308 }
9309
b04243ef
PZ
9310 if (group_leader &&
9311 (is_software_event(event) != is_software_event(group_leader))) {
9312 if (is_software_event(event)) {
9313 /*
9314 * If event and group_leader are not both a software
9315 * event, and event is, then group leader is not.
9316 *
9317 * Allow the addition of software events to !software
9318 * groups, this is safe because software events never
9319 * fail to schedule.
9320 */
9321 pmu = group_leader->pmu;
9322 } else if (is_software_event(group_leader) &&
9323 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
9324 /*
9325 * In case the group is a pure software group, and we
9326 * try to add a hardware event, move the whole group to
9327 * the hardware context.
9328 */
9329 move_group = 1;
9330 }
9331 }
89a1e187
PZ
9332
9333 /*
9334 * Get the target context (task or percpu):
9335 */
4af57ef2 9336 ctx = find_get_context(pmu, task, event);
89a1e187
PZ
9337 if (IS_ERR(ctx)) {
9338 err = PTR_ERR(ctx);
c6be5a5c 9339 goto err_alloc;
89a1e187
PZ
9340 }
9341
bed5b25a
AS
9342 if ((pmu->capabilities & PERF_PMU_CAP_EXCLUSIVE) && group_leader) {
9343 err = -EBUSY;
9344 goto err_context;
9345 }
9346
ccff286d 9347 /*
cdd6c482 9348 * Look up the group leader (we will attach this event to it):
04289bb9 9349 */
ac9721f3 9350 if (group_leader) {
dc86cabe 9351 err = -EINVAL;
04289bb9 9352
04289bb9 9353 /*
ccff286d
IM
9354 * Do not allow a recursive hierarchy (this new sibling
9355 * becoming part of another group-sibling):
9356 */
9357 if (group_leader->group_leader != group_leader)
c3f00c70 9358 goto err_context;
34f43927
PZ
9359
9360 /* All events in a group should have the same clock */
9361 if (group_leader->clock != event->clock)
9362 goto err_context;
9363
ccff286d
IM
9364 /*
9365 * Do not allow to attach to a group in a different
9366 * task or CPU context:
04289bb9 9367 */
b04243ef 9368 if (move_group) {
c3c87e77
PZ
9369 /*
9370 * Make sure we're both on the same task, or both
9371 * per-cpu events.
9372 */
9373 if (group_leader->ctx->task != ctx->task)
9374 goto err_context;
9375
9376 /*
9377 * Make sure we're both events for the same CPU;
9378 * grouping events for different CPUs is broken; since
9379 * you can never concurrently schedule them anyhow.
9380 */
9381 if (group_leader->cpu != event->cpu)
b04243ef
PZ
9382 goto err_context;
9383 } else {
9384 if (group_leader->ctx != ctx)
9385 goto err_context;
9386 }
9387
3b6f9e5c
PM
9388 /*
9389 * Only a group leader can be exclusive or pinned
9390 */
0d48696f 9391 if (attr.exclusive || attr.pinned)
c3f00c70 9392 goto err_context;
ac9721f3
PZ
9393 }
9394
9395 if (output_event) {
9396 err = perf_event_set_output(event, output_event);
9397 if (err)
c3f00c70 9398 goto err_context;
ac9721f3 9399 }
0793a61d 9400
a21b0b35
YD
9401 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
9402 f_flags);
ea635c64
AV
9403 if (IS_ERR(event_file)) {
9404 err = PTR_ERR(event_file);
201c2f85 9405 event_file = NULL;
c3f00c70 9406 goto err_context;
ea635c64 9407 }
9b51f66d 9408
b04243ef 9409 if (move_group) {
f63a8daa 9410 gctx = group_leader->ctx;
f55fc2a5 9411 mutex_lock_double(&gctx->mutex, &ctx->mutex);
84c4e620
PZ
9412 if (gctx->task == TASK_TOMBSTONE) {
9413 err = -ESRCH;
9414 goto err_locked;
9415 }
f55fc2a5
PZ
9416 } else {
9417 mutex_lock(&ctx->mutex);
9418 }
9419
84c4e620
PZ
9420 if (ctx->task == TASK_TOMBSTONE) {
9421 err = -ESRCH;
9422 goto err_locked;
9423 }
9424
a723968c
PZ
9425 if (!perf_event_validate_size(event)) {
9426 err = -E2BIG;
9427 goto err_locked;
9428 }
9429
f55fc2a5
PZ
9430 /*
9431 * Must be under the same ctx::mutex as perf_install_in_context(),
9432 * because we need to serialize with concurrent event creation.
9433 */
9434 if (!exclusive_event_installable(event, ctx)) {
9435 /* exclusive and group stuff are assumed mutually exclusive */
9436 WARN_ON_ONCE(move_group);
f63a8daa 9437
f55fc2a5
PZ
9438 err = -EBUSY;
9439 goto err_locked;
9440 }
f63a8daa 9441
f55fc2a5
PZ
9442 WARN_ON_ONCE(ctx->parent_ctx);
9443
79c9ce57
PZ
9444 /*
9445 * This is the point on no return; we cannot fail hereafter. This is
9446 * where we start modifying current state.
9447 */
9448
f55fc2a5 9449 if (move_group) {
f63a8daa
PZ
9450 /*
9451 * See perf_event_ctx_lock() for comments on the details
9452 * of swizzling perf_event::ctx.
9453 */
45a0e07a 9454 perf_remove_from_context(group_leader, 0);
0231bb53 9455
b04243ef
PZ
9456 list_for_each_entry(sibling, &group_leader->sibling_list,
9457 group_entry) {
45a0e07a 9458 perf_remove_from_context(sibling, 0);
b04243ef
PZ
9459 put_ctx(gctx);
9460 }
b04243ef 9461
f63a8daa
PZ
9462 /*
9463 * Wait for everybody to stop referencing the events through
9464 * the old lists, before installing it on new lists.
9465 */
0cda4c02 9466 synchronize_rcu();
f63a8daa 9467
8f95b435
PZI
9468 /*
9469 * Install the group siblings before the group leader.
9470 *
9471 * Because a group leader will try and install the entire group
9472 * (through the sibling list, which is still in-tact), we can
9473 * end up with siblings installed in the wrong context.
9474 *
9475 * By installing siblings first we NO-OP because they're not
9476 * reachable through the group lists.
9477 */
b04243ef
PZ
9478 list_for_each_entry(sibling, &group_leader->sibling_list,
9479 group_entry) {
8f95b435 9480 perf_event__state_init(sibling);
9fc81d87 9481 perf_install_in_context(ctx, sibling, sibling->cpu);
b04243ef
PZ
9482 get_ctx(ctx);
9483 }
8f95b435
PZI
9484
9485 /*
9486 * Removing from the context ends up with disabled
9487 * event. What we want here is event in the initial
9488 * startup state, ready to be add into new context.
9489 */
9490 perf_event__state_init(group_leader);
9491 perf_install_in_context(ctx, group_leader, group_leader->cpu);
9492 get_ctx(ctx);
b04243ef 9493
f55fc2a5
PZ
9494 /*
9495 * Now that all events are installed in @ctx, nothing
9496 * references @gctx anymore, so drop the last reference we have
9497 * on it.
9498 */
9499 put_ctx(gctx);
bed5b25a
AS
9500 }
9501
f73e22ab
PZ
9502 /*
9503 * Precalculate sample_data sizes; do while holding ctx::mutex such
9504 * that we're serialized against further additions and before
9505 * perf_install_in_context() which is the point the event is active and
9506 * can use these values.
9507 */
9508 perf_event__header_size(event);
9509 perf_event__id_header_size(event);
9510
78cd2c74
PZ
9511 event->owner = current;
9512
e2d37cd2 9513 perf_install_in_context(ctx, event, event->cpu);
fe4b04fa 9514 perf_unpin_context(ctx);
f63a8daa 9515
f55fc2a5 9516 if (move_group)
f63a8daa 9517 mutex_unlock(&gctx->mutex);
d859e29f 9518 mutex_unlock(&ctx->mutex);
9b51f66d 9519
79c9ce57
PZ
9520 if (task) {
9521 mutex_unlock(&task->signal->cred_guard_mutex);
9522 put_task_struct(task);
9523 }
9524
fbfc623f
YZ
9525 put_online_cpus();
9526
cdd6c482
IM
9527 mutex_lock(&current->perf_event_mutex);
9528 list_add_tail(&event->owner_entry, &current->perf_event_list);
9529 mutex_unlock(&current->perf_event_mutex);
082ff5a2 9530
8a49542c
PZ
9531 /*
9532 * Drop the reference on the group_event after placing the
9533 * new event on the sibling_list. This ensures destruction
9534 * of the group leader will find the pointer to itself in
9535 * perf_group_detach().
9536 */
2903ff01 9537 fdput(group);
ea635c64
AV
9538 fd_install(event_fd, event_file);
9539 return event_fd;
0793a61d 9540
f55fc2a5
PZ
9541err_locked:
9542 if (move_group)
9543 mutex_unlock(&gctx->mutex);
9544 mutex_unlock(&ctx->mutex);
9545/* err_file: */
9546 fput(event_file);
c3f00c70 9547err_context:
fe4b04fa 9548 perf_unpin_context(ctx);
ea635c64 9549 put_ctx(ctx);
c6be5a5c 9550err_alloc:
13005627
PZ
9551 /*
9552 * If event_file is set, the fput() above will have called ->release()
9553 * and that will take care of freeing the event.
9554 */
9555 if (!event_file)
9556 free_event(event);
79c9ce57
PZ
9557err_cred:
9558 if (task)
9559 mutex_unlock(&task->signal->cred_guard_mutex);
1f4ee503 9560err_cpus:
fbfc623f 9561 put_online_cpus();
1f4ee503 9562err_task:
e7d0bc04
PZ
9563 if (task)
9564 put_task_struct(task);
89a1e187 9565err_group_fd:
2903ff01 9566 fdput(group);
ea635c64
AV
9567err_fd:
9568 put_unused_fd(event_fd);
dc86cabe 9569 return err;
0793a61d
TG
9570}
9571
fb0459d7
AV
9572/**
9573 * perf_event_create_kernel_counter
9574 *
9575 * @attr: attributes of the counter to create
9576 * @cpu: cpu in which the counter is bound
38a81da2 9577 * @task: task to profile (NULL for percpu)
fb0459d7
AV
9578 */
9579struct perf_event *
9580perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
38a81da2 9581 struct task_struct *task,
4dc0da86
AK
9582 perf_overflow_handler_t overflow_handler,
9583 void *context)
fb0459d7 9584{
fb0459d7 9585 struct perf_event_context *ctx;
c3f00c70 9586 struct perf_event *event;
fb0459d7 9587 int err;
d859e29f 9588
fb0459d7
AV
9589 /*
9590 * Get the target context (task or percpu):
9591 */
d859e29f 9592
4dc0da86 9593 event = perf_event_alloc(attr, cpu, task, NULL, NULL,
79dff51e 9594 overflow_handler, context, -1);
c3f00c70
PZ
9595 if (IS_ERR(event)) {
9596 err = PTR_ERR(event);
9597 goto err;
9598 }
d859e29f 9599
f8697762 9600 /* Mark owner so we could distinguish it from user events. */
63b6da39 9601 event->owner = TASK_TOMBSTONE;
f8697762 9602
4af57ef2 9603 ctx = find_get_context(event->pmu, task, event);
c6567f64
FW
9604 if (IS_ERR(ctx)) {
9605 err = PTR_ERR(ctx);
c3f00c70 9606 goto err_free;
d859e29f 9607 }
fb0459d7 9608
fb0459d7
AV
9609 WARN_ON_ONCE(ctx->parent_ctx);
9610 mutex_lock(&ctx->mutex);
84c4e620
PZ
9611 if (ctx->task == TASK_TOMBSTONE) {
9612 err = -ESRCH;
9613 goto err_unlock;
9614 }
9615
bed5b25a 9616 if (!exclusive_event_installable(event, ctx)) {
bed5b25a 9617 err = -EBUSY;
84c4e620 9618 goto err_unlock;
bed5b25a
AS
9619 }
9620
fb0459d7 9621 perf_install_in_context(ctx, event, cpu);
fe4b04fa 9622 perf_unpin_context(ctx);
fb0459d7
AV
9623 mutex_unlock(&ctx->mutex);
9624
fb0459d7
AV
9625 return event;
9626
84c4e620
PZ
9627err_unlock:
9628 mutex_unlock(&ctx->mutex);
9629 perf_unpin_context(ctx);
9630 put_ctx(ctx);
c3f00c70
PZ
9631err_free:
9632 free_event(event);
9633err:
c6567f64 9634 return ERR_PTR(err);
9b51f66d 9635}
fb0459d7 9636EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
9b51f66d 9637
0cda4c02
YZ
9638void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
9639{
9640 struct perf_event_context *src_ctx;
9641 struct perf_event_context *dst_ctx;
9642 struct perf_event *event, *tmp;
9643 LIST_HEAD(events);
9644
9645 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
9646 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
9647
f63a8daa
PZ
9648 /*
9649 * See perf_event_ctx_lock() for comments on the details
9650 * of swizzling perf_event::ctx.
9651 */
9652 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex);
0cda4c02
YZ
9653 list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
9654 event_entry) {
45a0e07a 9655 perf_remove_from_context(event, 0);
9a545de0 9656 unaccount_event_cpu(event, src_cpu);
0cda4c02 9657 put_ctx(src_ctx);
9886167d 9658 list_add(&event->migrate_entry, &events);
0cda4c02 9659 }
0cda4c02 9660
8f95b435
PZI
9661 /*
9662 * Wait for the events to quiesce before re-instating them.
9663 */
0cda4c02
YZ
9664 synchronize_rcu();
9665
8f95b435
PZI
9666 /*
9667 * Re-instate events in 2 passes.
9668 *
9669 * Skip over group leaders and only install siblings on this first
9670 * pass, siblings will not get enabled without a leader, however a
9671 * leader will enable its siblings, even if those are still on the old
9672 * context.
9673 */
9674 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
9675 if (event->group_leader == event)
9676 continue;
9677
9678 list_del(&event->migrate_entry);
9679 if (event->state >= PERF_EVENT_STATE_OFF)
9680 event->state = PERF_EVENT_STATE_INACTIVE;
9681 account_event_cpu(event, dst_cpu);
9682 perf_install_in_context(dst_ctx, event, dst_cpu);
9683 get_ctx(dst_ctx);
9684 }
9685
9686 /*
9687 * Once all the siblings are setup properly, install the group leaders
9688 * to make it go.
9689 */
9886167d
PZ
9690 list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
9691 list_del(&event->migrate_entry);
0cda4c02
YZ
9692 if (event->state >= PERF_EVENT_STATE_OFF)
9693 event->state = PERF_EVENT_STATE_INACTIVE;
9a545de0 9694 account_event_cpu(event, dst_cpu);
0cda4c02
YZ
9695 perf_install_in_context(dst_ctx, event, dst_cpu);
9696 get_ctx(dst_ctx);
9697 }
9698 mutex_unlock(&dst_ctx->mutex);
f63a8daa 9699 mutex_unlock(&src_ctx->mutex);
0cda4c02
YZ
9700}
9701EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
9702
cdd6c482 9703static void sync_child_event(struct perf_event *child_event,
38b200d6 9704 struct task_struct *child)
d859e29f 9705{
cdd6c482 9706 struct perf_event *parent_event = child_event->parent;
8bc20959 9707 u64 child_val;
d859e29f 9708
cdd6c482
IM
9709 if (child_event->attr.inherit_stat)
9710 perf_event_read_event(child_event, child);
38b200d6 9711
b5e58793 9712 child_val = perf_event_count(child_event);
d859e29f
PM
9713
9714 /*
9715 * Add back the child's count to the parent's count:
9716 */
a6e6dea6 9717 atomic64_add(child_val, &parent_event->child_count);
cdd6c482
IM
9718 atomic64_add(child_event->total_time_enabled,
9719 &parent_event->child_total_time_enabled);
9720 atomic64_add(child_event->total_time_running,
9721 &parent_event->child_total_time_running);
d859e29f
PM
9722}
9723
9b51f66d 9724static void
8ba289b8
PZ
9725perf_event_exit_event(struct perf_event *child_event,
9726 struct perf_event_context *child_ctx,
9727 struct task_struct *child)
9b51f66d 9728{
8ba289b8
PZ
9729 struct perf_event *parent_event = child_event->parent;
9730
1903d50c
PZ
9731 /*
9732 * Do not destroy the 'original' grouping; because of the context
9733 * switch optimization the original events could've ended up in a
9734 * random child task.
9735 *
9736 * If we were to destroy the original group, all group related
9737 * operations would cease to function properly after this random
9738 * child dies.
9739 *
9740 * Do destroy all inherited groups, we don't care about those
9741 * and being thorough is better.
9742 */
32132a3d
PZ
9743 raw_spin_lock_irq(&child_ctx->lock);
9744 WARN_ON_ONCE(child_ctx->is_active);
9745
8ba289b8 9746 if (parent_event)
32132a3d
PZ
9747 perf_group_detach(child_event);
9748 list_del_event(child_event, child_ctx);
a69b0ca4 9749 child_event->state = PERF_EVENT_STATE_EXIT; /* is_event_hup() */
32132a3d 9750 raw_spin_unlock_irq(&child_ctx->lock);
0cc0c027 9751
9b51f66d 9752 /*
8ba289b8 9753 * Parent events are governed by their filedesc, retain them.
9b51f66d 9754 */
8ba289b8 9755 if (!parent_event) {
179033b3 9756 perf_event_wakeup(child_event);
8ba289b8 9757 return;
4bcf349a 9758 }
8ba289b8
PZ
9759 /*
9760 * Child events can be cleaned up.
9761 */
9762
9763 sync_child_event(child_event, child);
9764
9765 /*
9766 * Remove this event from the parent's list
9767 */
9768 WARN_ON_ONCE(parent_event->ctx->parent_ctx);
9769 mutex_lock(&parent_event->child_mutex);
9770 list_del_init(&child_event->child_list);
9771 mutex_unlock(&parent_event->child_mutex);
9772
9773 /*
9774 * Kick perf_poll() for is_event_hup().
9775 */
9776 perf_event_wakeup(parent_event);
9777 free_event(child_event);
9778 put_event(parent_event);
9b51f66d
IM
9779}
9780
8dc85d54 9781static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
9b51f66d 9782{
211de6eb 9783 struct perf_event_context *child_ctx, *clone_ctx = NULL;
63b6da39 9784 struct perf_event *child_event, *next;
63b6da39
PZ
9785
9786 WARN_ON_ONCE(child != current);
9b51f66d 9787
6a3351b6 9788 child_ctx = perf_pin_task_context(child, ctxn);
63b6da39 9789 if (!child_ctx)
9b51f66d
IM
9790 return;
9791
ad3a37de 9792 /*
6a3351b6
PZ
9793 * In order to reduce the amount of tricky in ctx tear-down, we hold
9794 * ctx::mutex over the entire thing. This serializes against almost
9795 * everything that wants to access the ctx.
9796 *
9797 * The exception is sys_perf_event_open() /
9798 * perf_event_create_kernel_count() which does find_get_context()
9799 * without ctx::mutex (it cannot because of the move_group double mutex
9800 * lock thing). See the comments in perf_install_in_context().
ad3a37de 9801 */
6a3351b6 9802 mutex_lock(&child_ctx->mutex);
c93f7669
PM
9803
9804 /*
6a3351b6
PZ
9805 * In a single ctx::lock section, de-schedule the events and detach the
9806 * context from the task such that we cannot ever get it scheduled back
9807 * in.
c93f7669 9808 */
6a3351b6 9809 raw_spin_lock_irq(&child_ctx->lock);
63b6da39 9810 task_ctx_sched_out(__get_cpu_context(child_ctx), child_ctx);
4a1c0f26 9811
71a851b4 9812 /*
63b6da39
PZ
9813 * Now that the context is inactive, destroy the task <-> ctx relation
9814 * and mark the context dead.
71a851b4 9815 */
63b6da39
PZ
9816 RCU_INIT_POINTER(child->perf_event_ctxp[ctxn], NULL);
9817 put_ctx(child_ctx); /* cannot be last */
9818 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE);
9819 put_task_struct(current); /* cannot be last */
4a1c0f26 9820
211de6eb 9821 clone_ctx = unclone_ctx(child_ctx);
6a3351b6 9822 raw_spin_unlock_irq(&child_ctx->lock);
9f498cc5 9823
211de6eb
PZ
9824 if (clone_ctx)
9825 put_ctx(clone_ctx);
4a1c0f26 9826
9f498cc5 9827 /*
cdd6c482
IM
9828 * Report the task dead after unscheduling the events so that we
9829 * won't get any samples after PERF_RECORD_EXIT. We can however still
9830 * get a few PERF_RECORD_READ events.
9f498cc5 9831 */
cdd6c482 9832 perf_event_task(child, child_ctx, 0);
a63eaf34 9833
ebf905fc 9834 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
8ba289b8 9835 perf_event_exit_event(child_event, child_ctx, child);
8bc20959 9836
a63eaf34
PM
9837 mutex_unlock(&child_ctx->mutex);
9838
9839 put_ctx(child_ctx);
9b51f66d
IM
9840}
9841
8dc85d54
PZ
9842/*
9843 * When a child task exits, feed back event values to parent events.
79c9ce57
PZ
9844 *
9845 * Can be called with cred_guard_mutex held when called from
9846 * install_exec_creds().
8dc85d54
PZ
9847 */
9848void perf_event_exit_task(struct task_struct *child)
9849{
8882135b 9850 struct perf_event *event, *tmp;
8dc85d54
PZ
9851 int ctxn;
9852
8882135b
PZ
9853 mutex_lock(&child->perf_event_mutex);
9854 list_for_each_entry_safe(event, tmp, &child->perf_event_list,
9855 owner_entry) {
9856 list_del_init(&event->owner_entry);
9857
9858 /*
9859 * Ensure the list deletion is visible before we clear
9860 * the owner, closes a race against perf_release() where
9861 * we need to serialize on the owner->perf_event_mutex.
9862 */
f47c02c0 9863 smp_store_release(&event->owner, NULL);
8882135b
PZ
9864 }
9865 mutex_unlock(&child->perf_event_mutex);
9866
8dc85d54
PZ
9867 for_each_task_context_nr(ctxn)
9868 perf_event_exit_task_context(child, ctxn);
4e93ad60
JO
9869
9870 /*
9871 * The perf_event_exit_task_context calls perf_event_task
9872 * with child's task_ctx, which generates EXIT events for
9873 * child contexts and sets child->perf_event_ctxp[] to NULL.
9874 * At this point we need to send EXIT events to cpu contexts.
9875 */
9876 perf_event_task(child, NULL, 0);
8dc85d54
PZ
9877}
9878
889ff015
FW
9879static void perf_free_event(struct perf_event *event,
9880 struct perf_event_context *ctx)
9881{
9882 struct perf_event *parent = event->parent;
9883
9884 if (WARN_ON_ONCE(!parent))
9885 return;
9886
9887 mutex_lock(&parent->child_mutex);
9888 list_del_init(&event->child_list);
9889 mutex_unlock(&parent->child_mutex);
9890
a6fa941d 9891 put_event(parent);
889ff015 9892
652884fe 9893 raw_spin_lock_irq(&ctx->lock);
8a49542c 9894 perf_group_detach(event);
889ff015 9895 list_del_event(event, ctx);
652884fe 9896 raw_spin_unlock_irq(&ctx->lock);
889ff015
FW
9897 free_event(event);
9898}
9899
bbbee908 9900/*
652884fe 9901 * Free an unexposed, unused context as created by inheritance by
8dc85d54 9902 * perf_event_init_task below, used by fork() in case of fail.
652884fe
PZ
9903 *
9904 * Not all locks are strictly required, but take them anyway to be nice and
9905 * help out with the lockdep assertions.
bbbee908 9906 */
cdd6c482 9907void perf_event_free_task(struct task_struct *task)
bbbee908 9908{
8dc85d54 9909 struct perf_event_context *ctx;
cdd6c482 9910 struct perf_event *event, *tmp;
8dc85d54 9911 int ctxn;
bbbee908 9912
8dc85d54
PZ
9913 for_each_task_context_nr(ctxn) {
9914 ctx = task->perf_event_ctxp[ctxn];
9915 if (!ctx)
9916 continue;
bbbee908 9917
8dc85d54 9918 mutex_lock(&ctx->mutex);
bbbee908 9919again:
8dc85d54
PZ
9920 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
9921 group_entry)
9922 perf_free_event(event, ctx);
bbbee908 9923
8dc85d54
PZ
9924 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
9925 group_entry)
9926 perf_free_event(event, ctx);
bbbee908 9927
8dc85d54
PZ
9928 if (!list_empty(&ctx->pinned_groups) ||
9929 !list_empty(&ctx->flexible_groups))
9930 goto again;
bbbee908 9931
8dc85d54 9932 mutex_unlock(&ctx->mutex);
bbbee908 9933
8dc85d54
PZ
9934 put_ctx(ctx);
9935 }
889ff015
FW
9936}
9937
4e231c79
PZ
9938void perf_event_delayed_put(struct task_struct *task)
9939{
9940 int ctxn;
9941
9942 for_each_task_context_nr(ctxn)
9943 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
9944}
9945
e03e7ee3 9946struct file *perf_event_get(unsigned int fd)
ffe8690c 9947{
e03e7ee3 9948 struct file *file;
ffe8690c 9949
e03e7ee3
AS
9950 file = fget_raw(fd);
9951 if (!file)
9952 return ERR_PTR(-EBADF);
ffe8690c 9953
e03e7ee3
AS
9954 if (file->f_op != &perf_fops) {
9955 fput(file);
9956 return ERR_PTR(-EBADF);
9957 }
ffe8690c 9958
e03e7ee3 9959 return file;
ffe8690c
KX
9960}
9961
9962const struct perf_event_attr *perf_event_attrs(struct perf_event *event)
9963{
9964 if (!event)
9965 return ERR_PTR(-EINVAL);
9966
9967 return &event->attr;
9968}
9969
97dee4f3
PZ
9970/*
9971 * inherit a event from parent task to child task:
9972 */
9973static struct perf_event *
9974inherit_event(struct perf_event *parent_event,
9975 struct task_struct *parent,
9976 struct perf_event_context *parent_ctx,
9977 struct task_struct *child,
9978 struct perf_event *group_leader,
9979 struct perf_event_context *child_ctx)
9980{
1929def9 9981 enum perf_event_active_state parent_state = parent_event->state;
97dee4f3 9982 struct perf_event *child_event;
cee010ec 9983 unsigned long flags;
97dee4f3
PZ
9984
9985 /*
9986 * Instead of creating recursive hierarchies of events,
9987 * we link inherited events back to the original parent,
9988 * which has a filp for sure, which we use as the reference
9989 * count:
9990 */
9991 if (parent_event->parent)
9992 parent_event = parent_event->parent;
9993
9994 child_event = perf_event_alloc(&parent_event->attr,
9995 parent_event->cpu,
d580ff86 9996 child,
97dee4f3 9997 group_leader, parent_event,
79dff51e 9998 NULL, NULL, -1);
97dee4f3
PZ
9999 if (IS_ERR(child_event))
10000 return child_event;
a6fa941d 10001
c6e5b732
PZ
10002 /*
10003 * is_orphaned_event() and list_add_tail(&parent_event->child_list)
10004 * must be under the same lock in order to serialize against
10005 * perf_event_release_kernel(), such that either we must observe
10006 * is_orphaned_event() or they will observe us on the child_list.
10007 */
10008 mutex_lock(&parent_event->child_mutex);
fadfe7be
JO
10009 if (is_orphaned_event(parent_event) ||
10010 !atomic_long_inc_not_zero(&parent_event->refcount)) {
c6e5b732 10011 mutex_unlock(&parent_event->child_mutex);
a6fa941d
AV
10012 free_event(child_event);
10013 return NULL;
10014 }
10015
97dee4f3
PZ
10016 get_ctx(child_ctx);
10017
10018 /*
10019 * Make the child state follow the state of the parent event,
10020 * not its attr.disabled bit. We hold the parent's mutex,
10021 * so we won't race with perf_event_{en, dis}able_family.
10022 */
1929def9 10023 if (parent_state >= PERF_EVENT_STATE_INACTIVE)
97dee4f3
PZ
10024 child_event->state = PERF_EVENT_STATE_INACTIVE;
10025 else
10026 child_event->state = PERF_EVENT_STATE_OFF;
10027
10028 if (parent_event->attr.freq) {
10029 u64 sample_period = parent_event->hw.sample_period;
10030 struct hw_perf_event *hwc = &child_event->hw;
10031
10032 hwc->sample_period = sample_period;
10033 hwc->last_period = sample_period;
10034
10035 local64_set(&hwc->period_left, sample_period);
10036 }
10037
10038 child_event->ctx = child_ctx;
10039 child_event->overflow_handler = parent_event->overflow_handler;
4dc0da86
AK
10040 child_event->overflow_handler_context
10041 = parent_event->overflow_handler_context;
97dee4f3 10042
614b6780
TG
10043 /*
10044 * Precalculate sample_data sizes
10045 */
10046 perf_event__header_size(child_event);
6844c09d 10047 perf_event__id_header_size(child_event);
614b6780 10048
97dee4f3
PZ
10049 /*
10050 * Link it up in the child's context:
10051 */
cee010ec 10052 raw_spin_lock_irqsave(&child_ctx->lock, flags);
97dee4f3 10053 add_event_to_ctx(child_event, child_ctx);
cee010ec 10054 raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
97dee4f3 10055
97dee4f3
PZ
10056 /*
10057 * Link this into the parent event's child list
10058 */
97dee4f3
PZ
10059 list_add_tail(&child_event->child_list, &parent_event->child_list);
10060 mutex_unlock(&parent_event->child_mutex);
10061
10062 return child_event;
10063}
10064
10065static int inherit_group(struct perf_event *parent_event,
10066 struct task_struct *parent,
10067 struct perf_event_context *parent_ctx,
10068 struct task_struct *child,
10069 struct perf_event_context *child_ctx)
10070{
10071 struct perf_event *leader;
10072 struct perf_event *sub;
10073 struct perf_event *child_ctr;
10074
10075 leader = inherit_event(parent_event, parent, parent_ctx,
10076 child, NULL, child_ctx);
10077 if (IS_ERR(leader))
10078 return PTR_ERR(leader);
10079 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
10080 child_ctr = inherit_event(sub, parent, parent_ctx,
10081 child, leader, child_ctx);
10082 if (IS_ERR(child_ctr))
10083 return PTR_ERR(child_ctr);
10084 }
10085 return 0;
889ff015
FW
10086}
10087
10088static int
10089inherit_task_group(struct perf_event *event, struct task_struct *parent,
10090 struct perf_event_context *parent_ctx,
8dc85d54 10091 struct task_struct *child, int ctxn,
889ff015
FW
10092 int *inherited_all)
10093{
10094 int ret;
8dc85d54 10095 struct perf_event_context *child_ctx;
889ff015
FW
10096
10097 if (!event->attr.inherit) {
10098 *inherited_all = 0;
10099 return 0;
bbbee908
PZ
10100 }
10101
fe4b04fa 10102 child_ctx = child->perf_event_ctxp[ctxn];
889ff015
FW
10103 if (!child_ctx) {
10104 /*
10105 * This is executed from the parent task context, so
10106 * inherit events that have been marked for cloning.
10107 * First allocate and initialize a context for the
10108 * child.
10109 */
bbbee908 10110
734df5ab 10111 child_ctx = alloc_perf_context(parent_ctx->pmu, child);
889ff015
FW
10112 if (!child_ctx)
10113 return -ENOMEM;
bbbee908 10114
8dc85d54 10115 child->perf_event_ctxp[ctxn] = child_ctx;
889ff015
FW
10116 }
10117
10118 ret = inherit_group(event, parent, parent_ctx,
10119 child, child_ctx);
10120
10121 if (ret)
10122 *inherited_all = 0;
10123
10124 return ret;
bbbee908
PZ
10125}
10126
9b51f66d 10127/*
cdd6c482 10128 * Initialize the perf_event context in task_struct
9b51f66d 10129 */
985c8dcb 10130static int perf_event_init_context(struct task_struct *child, int ctxn)
9b51f66d 10131{
889ff015 10132 struct perf_event_context *child_ctx, *parent_ctx;
cdd6c482
IM
10133 struct perf_event_context *cloned_ctx;
10134 struct perf_event *event;
9b51f66d 10135 struct task_struct *parent = current;
564c2b21 10136 int inherited_all = 1;
dddd3379 10137 unsigned long flags;
6ab423e0 10138 int ret = 0;
9b51f66d 10139
8dc85d54 10140 if (likely(!parent->perf_event_ctxp[ctxn]))
6ab423e0
PZ
10141 return 0;
10142
ad3a37de 10143 /*
25346b93
PM
10144 * If the parent's context is a clone, pin it so it won't get
10145 * swapped under us.
ad3a37de 10146 */
8dc85d54 10147 parent_ctx = perf_pin_task_context(parent, ctxn);
ffb4ef21
PZ
10148 if (!parent_ctx)
10149 return 0;
25346b93 10150
ad3a37de
PM
10151 /*
10152 * No need to check if parent_ctx != NULL here; since we saw
10153 * it non-NULL earlier, the only reason for it to become NULL
10154 * is if we exit, and since we're currently in the middle of
10155 * a fork we can't be exiting at the same time.
10156 */
ad3a37de 10157
9b51f66d
IM
10158 /*
10159 * Lock the parent list. No need to lock the child - not PID
10160 * hashed yet and not running, so nobody can access it.
10161 */
d859e29f 10162 mutex_lock(&parent_ctx->mutex);
9b51f66d
IM
10163
10164 /*
10165 * We dont have to disable NMIs - we are only looking at
10166 * the list, not manipulating it:
10167 */
889ff015 10168 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
8dc85d54
PZ
10169 ret = inherit_task_group(event, parent, parent_ctx,
10170 child, ctxn, &inherited_all);
889ff015
FW
10171 if (ret)
10172 break;
10173 }
b93f7978 10174
dddd3379
TG
10175 /*
10176 * We can't hold ctx->lock when iterating the ->flexible_group list due
10177 * to allocations, but we need to prevent rotation because
10178 * rotate_ctx() will change the list from interrupt context.
10179 */
10180 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10181 parent_ctx->rotate_disable = 1;
10182 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
10183
889ff015 10184 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
8dc85d54
PZ
10185 ret = inherit_task_group(event, parent, parent_ctx,
10186 child, ctxn, &inherited_all);
889ff015 10187 if (ret)
9b51f66d 10188 break;
564c2b21
PM
10189 }
10190
dddd3379
TG
10191 raw_spin_lock_irqsave(&parent_ctx->lock, flags);
10192 parent_ctx->rotate_disable = 0;
dddd3379 10193
8dc85d54 10194 child_ctx = child->perf_event_ctxp[ctxn];
889ff015 10195
05cbaa28 10196 if (child_ctx && inherited_all) {
564c2b21
PM
10197 /*
10198 * Mark the child context as a clone of the parent
10199 * context, or of whatever the parent is a clone of.
c5ed5145
PZ
10200 *
10201 * Note that if the parent is a clone, the holding of
10202 * parent_ctx->lock avoids it from being uncloned.
564c2b21 10203 */
c5ed5145 10204 cloned_ctx = parent_ctx->parent_ctx;
ad3a37de
PM
10205 if (cloned_ctx) {
10206 child_ctx->parent_ctx = cloned_ctx;
25346b93 10207 child_ctx->parent_gen = parent_ctx->parent_gen;
564c2b21
PM
10208 } else {
10209 child_ctx->parent_ctx = parent_ctx;
10210 child_ctx->parent_gen = parent_ctx->generation;
10211 }
10212 get_ctx(child_ctx->parent_ctx);
9b51f66d
IM
10213 }
10214
c5ed5145 10215 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
d859e29f 10216 mutex_unlock(&parent_ctx->mutex);
6ab423e0 10217
25346b93 10218 perf_unpin_context(parent_ctx);
fe4b04fa 10219 put_ctx(parent_ctx);
ad3a37de 10220
6ab423e0 10221 return ret;
9b51f66d
IM
10222}
10223
8dc85d54
PZ
10224/*
10225 * Initialize the perf_event context in task_struct
10226 */
10227int perf_event_init_task(struct task_struct *child)
10228{
10229 int ctxn, ret;
10230
8550d7cb
ON
10231 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
10232 mutex_init(&child->perf_event_mutex);
10233 INIT_LIST_HEAD(&child->perf_event_list);
10234
8dc85d54
PZ
10235 for_each_task_context_nr(ctxn) {
10236 ret = perf_event_init_context(child, ctxn);
6c72e350
PZ
10237 if (ret) {
10238 perf_event_free_task(child);
8dc85d54 10239 return ret;
6c72e350 10240 }
8dc85d54
PZ
10241 }
10242
10243 return 0;
10244}
10245
220b140b
PM
10246static void __init perf_event_init_all_cpus(void)
10247{
b28ab83c 10248 struct swevent_htable *swhash;
220b140b 10249 int cpu;
220b140b
PM
10250
10251 for_each_possible_cpu(cpu) {
b28ab83c
PZ
10252 swhash = &per_cpu(swevent_htable, cpu);
10253 mutex_init(&swhash->hlist_mutex);
2fde4f94 10254 INIT_LIST_HEAD(&per_cpu(active_ctx_list, cpu));
220b140b
PM
10255 }
10256}
10257
0db0628d 10258static void perf_event_init_cpu(int cpu)
0793a61d 10259{
108b02cf 10260 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
0793a61d 10261
b28ab83c 10262 mutex_lock(&swhash->hlist_mutex);
059fcd8c 10263 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) {
76e1d904
FW
10264 struct swevent_hlist *hlist;
10265
b28ab83c
PZ
10266 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
10267 WARN_ON(!hlist);
10268 rcu_assign_pointer(swhash->swevent_hlist, hlist);
76e1d904 10269 }
b28ab83c 10270 mutex_unlock(&swhash->hlist_mutex);
0793a61d
TG
10271}
10272
2965faa5 10273#if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE
108b02cf 10274static void __perf_event_exit_context(void *__info)
0793a61d 10275{
108b02cf 10276 struct perf_event_context *ctx = __info;
fae3fde6
PZ
10277 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
10278 struct perf_event *event;
0793a61d 10279
fae3fde6
PZ
10280 raw_spin_lock(&ctx->lock);
10281 list_for_each_entry(event, &ctx->event_list, event_entry)
45a0e07a 10282 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP);
fae3fde6 10283 raw_spin_unlock(&ctx->lock);
0793a61d 10284}
108b02cf
PZ
10285
10286static void perf_event_exit_cpu_context(int cpu)
10287{
10288 struct perf_event_context *ctx;
10289 struct pmu *pmu;
10290 int idx;
10291
10292 idx = srcu_read_lock(&pmus_srcu);
10293 list_for_each_entry_rcu(pmu, &pmus, entry) {
917bdd1c 10294 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
108b02cf
PZ
10295
10296 mutex_lock(&ctx->mutex);
10297 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
10298 mutex_unlock(&ctx->mutex);
10299 }
10300 srcu_read_unlock(&pmus_srcu, idx);
108b02cf
PZ
10301}
10302
cdd6c482 10303static void perf_event_exit_cpu(int cpu)
0793a61d 10304{
e3703f8c 10305 perf_event_exit_cpu_context(cpu);
0793a61d
TG
10306}
10307#else
cdd6c482 10308static inline void perf_event_exit_cpu(int cpu) { }
0793a61d
TG
10309#endif
10310
c277443c
PZ
10311static int
10312perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
10313{
10314 int cpu;
10315
10316 for_each_online_cpu(cpu)
10317 perf_event_exit_cpu(cpu);
10318
10319 return NOTIFY_OK;
10320}
10321
10322/*
10323 * Run the perf reboot notifier at the very last possible moment so that
10324 * the generic watchdog code runs as long as possible.
10325 */
10326static struct notifier_block perf_reboot_notifier = {
10327 .notifier_call = perf_reboot,
10328 .priority = INT_MIN,
10329};
10330
0db0628d 10331static int
0793a61d
TG
10332perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
10333{
10334 unsigned int cpu = (long)hcpu;
10335
4536e4d1 10336 switch (action & ~CPU_TASKS_FROZEN) {
0793a61d
TG
10337
10338 case CPU_UP_PREPARE:
1dcaac1c
PZ
10339 /*
10340 * This must be done before the CPU comes alive, because the
10341 * moment we can run tasks we can encounter (software) events.
10342 *
10343 * Specifically, someone can have inherited events on kthreadd
10344 * or a pre-existing worker thread that gets re-bound.
10345 */
cdd6c482 10346 perf_event_init_cpu(cpu);
0793a61d
TG
10347 break;
10348
10349 case CPU_DOWN_PREPARE:
1dcaac1c
PZ
10350 /*
10351 * This must be done before the CPU dies because after that an
10352 * active event might want to IPI the CPU and that'll not work
10353 * so great for dead CPUs.
10354 *
10355 * XXX smp_call_function_single() return -ENXIO without a warn
10356 * so we could possibly deal with this.
10357 *
10358 * This is safe against new events arriving because
10359 * sys_perf_event_open() serializes against hotplug using
10360 * get_online_cpus().
10361 */
cdd6c482 10362 perf_event_exit_cpu(cpu);
0793a61d 10363 break;
0793a61d
TG
10364 default:
10365 break;
10366 }
10367
10368 return NOTIFY_OK;
10369}
10370
cdd6c482 10371void __init perf_event_init(void)
0793a61d 10372{
3c502e7a
JW
10373 int ret;
10374
2e80a82a
PZ
10375 idr_init(&pmu_idr);
10376
220b140b 10377 perf_event_init_all_cpus();
b0a873eb 10378 init_srcu_struct(&pmus_srcu);
2e80a82a
PZ
10379 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
10380 perf_pmu_register(&perf_cpu_clock, NULL, -1);
10381 perf_pmu_register(&perf_task_clock, NULL, -1);
b0a873eb
PZ
10382 perf_tp_register();
10383 perf_cpu_notifier(perf_cpu_notify);
c277443c 10384 register_reboot_notifier(&perf_reboot_notifier);
3c502e7a
JW
10385
10386 ret = init_hw_breakpoint();
10387 WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
b2029520 10388
b01c3a00
JO
10389 /*
10390 * Build time assertion that we keep the data_head at the intended
10391 * location. IOW, validation we got the __reserved[] size right.
10392 */
10393 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
10394 != 1024);
0793a61d 10395}
abe43400 10396
fd979c01
CS
10397ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr,
10398 char *page)
10399{
10400 struct perf_pmu_events_attr *pmu_attr =
10401 container_of(attr, struct perf_pmu_events_attr, attr);
10402
10403 if (pmu_attr->event_str)
10404 return sprintf(page, "%s\n", pmu_attr->event_str);
10405
10406 return 0;
10407}
675965b0 10408EXPORT_SYMBOL_GPL(perf_event_sysfs_show);
fd979c01 10409
abe43400
PZ
10410static int __init perf_event_sysfs_init(void)
10411{
10412 struct pmu *pmu;
10413 int ret;
10414
10415 mutex_lock(&pmus_lock);
10416
10417 ret = bus_register(&pmu_bus);
10418 if (ret)
10419 goto unlock;
10420
10421 list_for_each_entry(pmu, &pmus, entry) {
10422 if (!pmu->name || pmu->type < 0)
10423 continue;
10424
10425 ret = pmu_dev_alloc(pmu);
10426 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
10427 }
10428 pmu_bus_running = 1;
10429 ret = 0;
10430
10431unlock:
10432 mutex_unlock(&pmus_lock);
10433
10434 return ret;
10435}
10436device_initcall(perf_event_sysfs_init);
e5d1367f
SE
10437
10438#ifdef CONFIG_CGROUP_PERF
eb95419b
TH
10439static struct cgroup_subsys_state *
10440perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
e5d1367f
SE
10441{
10442 struct perf_cgroup *jc;
e5d1367f 10443
1b15d055 10444 jc = kzalloc(sizeof(*jc), GFP_KERNEL);
e5d1367f
SE
10445 if (!jc)
10446 return ERR_PTR(-ENOMEM);
10447
e5d1367f
SE
10448 jc->info = alloc_percpu(struct perf_cgroup_info);
10449 if (!jc->info) {
10450 kfree(jc);
10451 return ERR_PTR(-ENOMEM);
10452 }
10453
e5d1367f
SE
10454 return &jc->css;
10455}
10456
eb95419b 10457static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
e5d1367f 10458{
eb95419b
TH
10459 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
10460
e5d1367f
SE
10461 free_percpu(jc->info);
10462 kfree(jc);
10463}
10464
10465static int __perf_cgroup_move(void *info)
10466{
10467 struct task_struct *task = info;
ddaaf4e2 10468 rcu_read_lock();
e5d1367f 10469 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
ddaaf4e2 10470 rcu_read_unlock();
e5d1367f
SE
10471 return 0;
10472}
10473
1f7dd3e5 10474static void perf_cgroup_attach(struct cgroup_taskset *tset)
e5d1367f 10475{
bb9d97b6 10476 struct task_struct *task;
1f7dd3e5 10477 struct cgroup_subsys_state *css;
bb9d97b6 10478
1f7dd3e5 10479 cgroup_taskset_for_each(task, css, tset)
bb9d97b6 10480 task_function_call(task, __perf_cgroup_move, task);
e5d1367f
SE
10481}
10482
073219e9 10483struct cgroup_subsys perf_event_cgrp_subsys = {
92fb9748
TH
10484 .css_alloc = perf_cgroup_css_alloc,
10485 .css_free = perf_cgroup_css_free,
bb9d97b6 10486 .attach = perf_cgroup_attach,
e5d1367f
SE
10487};
10488#endif /* CONFIG_CGROUP_PERF */
This page took 1.53818 seconds and 5 git commands to generate.