1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Performance events core code: 4 * 5 * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> 6 * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar 7 * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra 8 * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> 9 */ 10 11 #include <linux/fs.h> 12 #include <linux/mm.h> 13 #include <linux/cpu.h> 14 #include <linux/smp.h> 15 #include <linux/idr.h> 16 #include <linux/file.h> 17 #include <linux/poll.h> 18 #include <linux/slab.h> 19 #include <linux/hash.h> 20 #include <linux/tick.h> 21 #include <linux/sysfs.h> 22 #include <linux/dcache.h> 23 #include <linux/percpu.h> 24 #include <linux/ptrace.h> 25 #include <linux/reboot.h> 26 #include <linux/vmstat.h> 27 #include <linux/device.h> 28 #include <linux/export.h> 29 #include <linux/vmalloc.h> 30 #include <linux/hardirq.h> 31 #include <linux/hugetlb.h> 32 #include <linux/rculist.h> 33 #include <linux/uaccess.h> 34 #include <linux/syscalls.h> 35 #include <linux/anon_inodes.h> 36 #include <linux/kernel_stat.h> 37 #include <linux/cgroup.h> 38 #include <linux/perf_event.h> 39 #include <linux/trace_events.h> 40 #include <linux/hw_breakpoint.h> 41 #include <linux/mm_types.h> 42 #include <linux/module.h> 43 #include <linux/mman.h> 44 #include <linux/compat.h> 45 #include <linux/bpf.h> 46 #include <linux/filter.h> 47 #include <linux/namei.h> 48 #include <linux/parser.h> 49 #include <linux/sched/clock.h> 50 #include <linux/sched/mm.h> 51 #include <linux/proc_ns.h> 52 #include <linux/mount.h> 53 #include <linux/min_heap.h> 54 #include <linux/highmem.h> 55 #include <linux/pgtable.h> 56 #include <linux/buildid.h> 57 #include <linux/task_work.h> 58 59 #include "internal.h" 60 61 #include <asm/irq_regs.h> 62 63 typedef int (*remote_function_f)(void *); 64 65 struct remote_function_call { 66 struct task_struct *p; 67 remote_function_f func; 68 void *info; 69 int ret; 70 }; 71 72 static void remote_function(void *data) 73 { 74 struct remote_function_call *tfc = data; 75 struct task_struct *p = tfc->p; 76 77 if (p) { 78 /* -EAGAIN */ 79 if (task_cpu(p) != smp_processor_id()) 80 return; 81 82 /* 83 * Now that we're on right CPU with IRQs disabled, we can test 84 * if we hit the right task without races. 85 */ 86 87 tfc->ret = -ESRCH; /* No such (running) process */ 88 if (p != current) 89 return; 90 } 91 92 tfc->ret = tfc->func(tfc->info); 93 } 94 95 /** 96 * task_function_call - call a function on the cpu on which a task runs 97 * @p: the task to evaluate 98 * @func: the function to be called 99 * @info: the function call argument 100 * 101 * Calls the function @func when the task is currently running. This might 102 * be on the current CPU, which just calls the function directly. This will 103 * retry due to any failures in smp_call_function_single(), such as if the 104 * task_cpu() goes offline concurrently. 105 * 106 * returns @func return value or -ESRCH or -ENXIO when the process isn't running 107 */ 108 static int 109 task_function_call(struct task_struct *p, remote_function_f func, void *info) 110 { 111 struct remote_function_call data = { 112 .p = p, 113 .func = func, 114 .info = info, 115 .ret = -EAGAIN, 116 }; 117 int ret; 118 119 for (;;) { 120 ret = smp_call_function_single(task_cpu(p), remote_function, 121 &data, 1); 122 if (!ret) 123 ret = data.ret; 124 125 if (ret != -EAGAIN) 126 break; 127 128 cond_resched(); 129 } 130 131 return ret; 132 } 133 134 /** 135 * cpu_function_call - call a function on the cpu 136 * @cpu: target cpu to queue this function 137 * @func: the function to be called 138 * @info: the function call argument 139 * 140 * Calls the function @func on the remote cpu. 141 * 142 * returns: @func return value or -ENXIO when the cpu is offline 143 */ 144 static int cpu_function_call(int cpu, remote_function_f func, void *info) 145 { 146 struct remote_function_call data = { 147 .p = NULL, 148 .func = func, 149 .info = info, 150 .ret = -ENXIO, /* No such CPU */ 151 }; 152 153 smp_call_function_single(cpu, remote_function, &data, 1); 154 155 return data.ret; 156 } 157 158 enum event_type_t { 159 EVENT_FLEXIBLE = 0x01, 160 EVENT_PINNED = 0x02, 161 EVENT_TIME = 0x04, 162 EVENT_FROZEN = 0x08, 163 /* see ctx_resched() for details */ 164 EVENT_CPU = 0x10, 165 EVENT_CGROUP = 0x20, 166 167 /* compound helpers */ 168 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, 169 EVENT_TIME_FROZEN = EVENT_TIME | EVENT_FROZEN, 170 }; 171 172 static inline void __perf_ctx_lock(struct perf_event_context *ctx) 173 { 174 raw_spin_lock(&ctx->lock); 175 WARN_ON_ONCE(ctx->is_active & EVENT_FROZEN); 176 } 177 178 static void perf_ctx_lock(struct perf_cpu_context *cpuctx, 179 struct perf_event_context *ctx) 180 { 181 __perf_ctx_lock(&cpuctx->ctx); 182 if (ctx) 183 __perf_ctx_lock(ctx); 184 } 185 186 static inline void __perf_ctx_unlock(struct perf_event_context *ctx) 187 { 188 /* 189 * If ctx_sched_in() didn't again set any ALL flags, clean up 190 * after ctx_sched_out() by clearing is_active. 191 */ 192 if (ctx->is_active & EVENT_FROZEN) { 193 if (!(ctx->is_active & EVENT_ALL)) 194 ctx->is_active = 0; 195 else 196 ctx->is_active &= ~EVENT_FROZEN; 197 } 198 raw_spin_unlock(&ctx->lock); 199 } 200 201 static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, 202 struct perf_event_context *ctx) 203 { 204 if (ctx) 205 __perf_ctx_unlock(ctx); 206 __perf_ctx_unlock(&cpuctx->ctx); 207 } 208 209 #define TASK_TOMBSTONE ((void *)-1L) 210 211 static bool is_kernel_event(struct perf_event *event) 212 { 213 return READ_ONCE(event->owner) == TASK_TOMBSTONE; 214 } 215 216 static DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context); 217 218 struct perf_event_context *perf_cpu_task_ctx(void) 219 { 220 lockdep_assert_irqs_disabled(); 221 return this_cpu_ptr(&perf_cpu_context)->task_ctx; 222 } 223 224 /* 225 * On task ctx scheduling... 226 * 227 * When !ctx->nr_events a task context will not be scheduled. This means 228 * we can disable the scheduler hooks (for performance) without leaving 229 * pending task ctx state. 230 * 231 * This however results in two special cases: 232 * 233 * - removing the last event from a task ctx; this is relatively straight 234 * forward and is done in __perf_remove_from_context. 235 * 236 * - adding the first event to a task ctx; this is tricky because we cannot 237 * rely on ctx->is_active and therefore cannot use event_function_call(). 238 * See perf_install_in_context(). 239 * 240 * If ctx->nr_events, then ctx->is_active and cpuctx->task_ctx are set. 241 */ 242 243 typedef void (*event_f)(struct perf_event *, struct perf_cpu_context *, 244 struct perf_event_context *, void *); 245 246 struct event_function_struct { 247 struct perf_event *event; 248 event_f func; 249 void *data; 250 }; 251 252 static int event_function(void *info) 253 { 254 struct event_function_struct *efs = info; 255 struct perf_event *event = efs->event; 256 struct perf_event_context *ctx = event->ctx; 257 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 258 struct perf_event_context *task_ctx = cpuctx->task_ctx; 259 int ret = 0; 260 261 lockdep_assert_irqs_disabled(); 262 263 perf_ctx_lock(cpuctx, task_ctx); 264 /* 265 * Since we do the IPI call without holding ctx->lock things can have 266 * changed, double check we hit the task we set out to hit. 267 */ 268 if (ctx->task) { 269 if (ctx->task != current) { 270 ret = -ESRCH; 271 goto unlock; 272 } 273 274 /* 275 * We only use event_function_call() on established contexts, 276 * and event_function() is only ever called when active (or 277 * rather, we'll have bailed in task_function_call() or the 278 * above ctx->task != current test), therefore we must have 279 * ctx->is_active here. 280 */ 281 WARN_ON_ONCE(!ctx->is_active); 282 /* 283 * And since we have ctx->is_active, cpuctx->task_ctx must 284 * match. 285 */ 286 WARN_ON_ONCE(task_ctx != ctx); 287 } else { 288 WARN_ON_ONCE(&cpuctx->ctx != ctx); 289 } 290 291 efs->func(event, cpuctx, ctx, efs->data); 292 unlock: 293 perf_ctx_unlock(cpuctx, task_ctx); 294 295 return ret; 296 } 297 298 static void event_function_call(struct perf_event *event, event_f func, void *data) 299 { 300 struct perf_event_context *ctx = event->ctx; 301 struct task_struct *task = READ_ONCE(ctx->task); /* verified in event_function */ 302 struct perf_cpu_context *cpuctx; 303 struct event_function_struct efs = { 304 .event = event, 305 .func = func, 306 .data = data, 307 }; 308 309 if (!event->parent) { 310 /* 311 * If this is a !child event, we must hold ctx::mutex to 312 * stabilize the event->ctx relation. See 313 * perf_event_ctx_lock(). 314 */ 315 lockdep_assert_held(&ctx->mutex); 316 } 317 318 if (!task) { 319 cpu_function_call(event->cpu, event_function, &efs); 320 return; 321 } 322 323 if (task == TASK_TOMBSTONE) 324 return; 325 326 again: 327 if (!task_function_call(task, event_function, &efs)) 328 return; 329 330 local_irq_disable(); 331 cpuctx = this_cpu_ptr(&perf_cpu_context); 332 perf_ctx_lock(cpuctx, ctx); 333 /* 334 * Reload the task pointer, it might have been changed by 335 * a concurrent perf_event_context_sched_out(). 336 */ 337 task = ctx->task; 338 if (task == TASK_TOMBSTONE) 339 goto unlock; 340 if (ctx->is_active) { 341 perf_ctx_unlock(cpuctx, ctx); 342 local_irq_enable(); 343 goto again; 344 } 345 func(event, NULL, ctx, data); 346 unlock: 347 perf_ctx_unlock(cpuctx, ctx); 348 local_irq_enable(); 349 } 350 351 /* 352 * Similar to event_function_call() + event_function(), but hard assumes IRQs 353 * are already disabled and we're on the right CPU. 354 */ 355 static void event_function_local(struct perf_event *event, event_f func, void *data) 356 { 357 struct perf_event_context *ctx = event->ctx; 358 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 359 struct task_struct *task = READ_ONCE(ctx->task); 360 struct perf_event_context *task_ctx = NULL; 361 362 lockdep_assert_irqs_disabled(); 363 364 if (task) { 365 if (task == TASK_TOMBSTONE) 366 return; 367 368 task_ctx = ctx; 369 } 370 371 perf_ctx_lock(cpuctx, task_ctx); 372 373 task = ctx->task; 374 if (task == TASK_TOMBSTONE) 375 goto unlock; 376 377 if (task) { 378 /* 379 * We must be either inactive or active and the right task, 380 * otherwise we're screwed, since we cannot IPI to somewhere 381 * else. 382 */ 383 if (ctx->is_active) { 384 if (WARN_ON_ONCE(task != current)) 385 goto unlock; 386 387 if (WARN_ON_ONCE(cpuctx->task_ctx != ctx)) 388 goto unlock; 389 } 390 } else { 391 WARN_ON_ONCE(&cpuctx->ctx != ctx); 392 } 393 394 func(event, cpuctx, ctx, data); 395 unlock: 396 perf_ctx_unlock(cpuctx, task_ctx); 397 } 398 399 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ 400 PERF_FLAG_FD_OUTPUT |\ 401 PERF_FLAG_PID_CGROUP |\ 402 PERF_FLAG_FD_CLOEXEC) 403 404 /* 405 * branch priv levels that need permission checks 406 */ 407 #define PERF_SAMPLE_BRANCH_PERM_PLM \ 408 (PERF_SAMPLE_BRANCH_KERNEL |\ 409 PERF_SAMPLE_BRANCH_HV) 410 411 /* 412 * perf_sched_events : >0 events exist 413 */ 414 415 static void perf_sched_delayed(struct work_struct *work); 416 DEFINE_STATIC_KEY_FALSE(perf_sched_events); 417 static DECLARE_DELAYED_WORK(perf_sched_work, perf_sched_delayed); 418 static DEFINE_MUTEX(perf_sched_mutex); 419 static atomic_t perf_sched_count; 420 421 static DEFINE_PER_CPU(struct pmu_event_list, pmu_sb_events); 422 423 static atomic_t nr_mmap_events __read_mostly; 424 static atomic_t nr_comm_events __read_mostly; 425 static atomic_t nr_namespaces_events __read_mostly; 426 static atomic_t nr_task_events __read_mostly; 427 static atomic_t nr_freq_events __read_mostly; 428 static atomic_t nr_switch_events __read_mostly; 429 static atomic_t nr_ksymbol_events __read_mostly; 430 static atomic_t nr_bpf_events __read_mostly; 431 static atomic_t nr_cgroup_events __read_mostly; 432 static atomic_t nr_text_poke_events __read_mostly; 433 static atomic_t nr_build_id_events __read_mostly; 434 435 static LIST_HEAD(pmus); 436 static DEFINE_MUTEX(pmus_lock); 437 static struct srcu_struct pmus_srcu; 438 static cpumask_var_t perf_online_mask; 439 static cpumask_var_t perf_online_core_mask; 440 static cpumask_var_t perf_online_die_mask; 441 static cpumask_var_t perf_online_cluster_mask; 442 static cpumask_var_t perf_online_pkg_mask; 443 static cpumask_var_t perf_online_sys_mask; 444 static struct kmem_cache *perf_event_cache; 445 446 /* 447 * perf event paranoia level: 448 * -1 - not paranoid at all 449 * 0 - disallow raw tracepoint access for unpriv 450 * 1 - disallow cpu events for unpriv 451 * 2 - disallow kernel profiling for unpriv 452 */ 453 int sysctl_perf_event_paranoid __read_mostly = 2; 454 455 /* Minimum for 512 kiB + 1 user control page */ 456 int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */ 457 458 /* 459 * max perf event sample rate 460 */ 461 #define DEFAULT_MAX_SAMPLE_RATE 100000 462 #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE) 463 #define DEFAULT_CPU_TIME_MAX_PERCENT 25 464 465 int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; 466 467 static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); 468 static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS; 469 470 static int perf_sample_allowed_ns __read_mostly = 471 DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100; 472 473 static void update_perf_cpu_limits(void) 474 { 475 u64 tmp = perf_sample_period_ns; 476 477 tmp *= sysctl_perf_cpu_time_max_percent; 478 tmp = div_u64(tmp, 100); 479 if (!tmp) 480 tmp = 1; 481 482 WRITE_ONCE(perf_sample_allowed_ns, tmp); 483 } 484 485 static bool perf_rotate_context(struct perf_cpu_pmu_context *cpc); 486 487 int perf_event_max_sample_rate_handler(const struct ctl_table *table, int write, 488 void *buffer, size_t *lenp, loff_t *ppos) 489 { 490 int ret; 491 int perf_cpu = sysctl_perf_cpu_time_max_percent; 492 /* 493 * If throttling is disabled don't allow the write: 494 */ 495 if (write && (perf_cpu == 100 || perf_cpu == 0)) 496 return -EINVAL; 497 498 ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 499 if (ret || !write) 500 return ret; 501 502 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ); 503 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; 504 update_perf_cpu_limits(); 505 506 return 0; 507 } 508 509 int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT; 510 511 int perf_cpu_time_max_percent_handler(const struct ctl_table *table, int write, 512 void *buffer, size_t *lenp, loff_t *ppos) 513 { 514 int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos); 515 516 if (ret || !write) 517 return ret; 518 519 if (sysctl_perf_cpu_time_max_percent == 100 || 520 sysctl_perf_cpu_time_max_percent == 0) { 521 printk(KERN_WARNING 522 "perf: Dynamic interrupt throttling disabled, can hang your system!\n"); 523 WRITE_ONCE(perf_sample_allowed_ns, 0); 524 } else { 525 update_perf_cpu_limits(); 526 } 527 528 return 0; 529 } 530 531 /* 532 * perf samples are done in some very critical code paths (NMIs). 533 * If they take too much CPU time, the system can lock up and not 534 * get any real work done. This will drop the sample rate when 535 * we detect that events are taking too long. 536 */ 537 #define NR_ACCUMULATED_SAMPLES 128 538 static DEFINE_PER_CPU(u64, running_sample_length); 539 540 static u64 __report_avg; 541 static u64 __report_allowed; 542 543 static void perf_duration_warn(struct irq_work *w) 544 { 545 printk_ratelimited(KERN_INFO 546 "perf: interrupt took too long (%lld > %lld), lowering " 547 "kernel.perf_event_max_sample_rate to %d\n", 548 __report_avg, __report_allowed, 549 sysctl_perf_event_sample_rate); 550 } 551 552 static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn); 553 554 void perf_sample_event_took(u64 sample_len_ns) 555 { 556 u64 max_len = READ_ONCE(perf_sample_allowed_ns); 557 u64 running_len; 558 u64 avg_len; 559 u32 max; 560 561 if (max_len == 0) 562 return; 563 564 /* Decay the counter by 1 average sample. */ 565 running_len = __this_cpu_read(running_sample_length); 566 running_len -= running_len/NR_ACCUMULATED_SAMPLES; 567 running_len += sample_len_ns; 568 __this_cpu_write(running_sample_length, running_len); 569 570 /* 571 * Note: this will be biased artificially low until we have 572 * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us 573 * from having to maintain a count. 574 */ 575 avg_len = running_len/NR_ACCUMULATED_SAMPLES; 576 if (avg_len <= max_len) 577 return; 578 579 __report_avg = avg_len; 580 __report_allowed = max_len; 581 582 /* 583 * Compute a throttle threshold 25% below the current duration. 584 */ 585 avg_len += avg_len / 4; 586 max = (TICK_NSEC / 100) * sysctl_perf_cpu_time_max_percent; 587 if (avg_len < max) 588 max /= (u32)avg_len; 589 else 590 max = 1; 591 592 WRITE_ONCE(perf_sample_allowed_ns, avg_len); 593 WRITE_ONCE(max_samples_per_tick, max); 594 595 sysctl_perf_event_sample_rate = max * HZ; 596 perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate; 597 598 if (!irq_work_queue(&perf_duration_work)) { 599 early_printk("perf: interrupt took too long (%lld > %lld), lowering " 600 "kernel.perf_event_max_sample_rate to %d\n", 601 __report_avg, __report_allowed, 602 sysctl_perf_event_sample_rate); 603 } 604 } 605 606 static atomic64_t perf_event_id; 607 608 static void update_context_time(struct perf_event_context *ctx); 609 static u64 perf_event_time(struct perf_event *event); 610 611 void __weak perf_event_print_debug(void) { } 612 613 static inline u64 perf_clock(void) 614 { 615 return local_clock(); 616 } 617 618 static inline u64 perf_event_clock(struct perf_event *event) 619 { 620 return event->clock(); 621 } 622 623 /* 624 * State based event timekeeping... 625 * 626 * The basic idea is to use event->state to determine which (if any) time 627 * fields to increment with the current delta. This means we only need to 628 * update timestamps when we change state or when they are explicitly requested 629 * (read). 630 * 631 * Event groups make things a little more complicated, but not terribly so. The 632 * rules for a group are that if the group leader is OFF the entire group is 633 * OFF, irrespective of what the group member states are. This results in 634 * __perf_effective_state(). 635 * 636 * A further ramification is that when a group leader flips between OFF and 637 * !OFF, we need to update all group member times. 638 * 639 * 640 * NOTE: perf_event_time() is based on the (cgroup) context time, and thus we 641 * need to make sure the relevant context time is updated before we try and 642 * update our timestamps. 643 */ 644 645 static __always_inline enum perf_event_state 646 __perf_effective_state(struct perf_event *event) 647 { 648 struct perf_event *leader = event->group_leader; 649 650 if (leader->state <= PERF_EVENT_STATE_OFF) 651 return leader->state; 652 653 return event->state; 654 } 655 656 static __always_inline void 657 __perf_update_times(struct perf_event *event, u64 now, u64 *enabled, u64 *running) 658 { 659 enum perf_event_state state = __perf_effective_state(event); 660 u64 delta = now - event->tstamp; 661 662 *enabled = event->total_time_enabled; 663 if (state >= PERF_EVENT_STATE_INACTIVE) 664 *enabled += delta; 665 666 *running = event->total_time_running; 667 if (state >= PERF_EVENT_STATE_ACTIVE) 668 *running += delta; 669 } 670 671 static void perf_event_update_time(struct perf_event *event) 672 { 673 u64 now = perf_event_time(event); 674 675 __perf_update_times(event, now, &event->total_time_enabled, 676 &event->total_time_running); 677 event->tstamp = now; 678 } 679 680 static void perf_event_update_sibling_time(struct perf_event *leader) 681 { 682 struct perf_event *sibling; 683 684 for_each_sibling_event(sibling, leader) 685 perf_event_update_time(sibling); 686 } 687 688 static void 689 perf_event_set_state(struct perf_event *event, enum perf_event_state state) 690 { 691 if (event->state == state) 692 return; 693 694 perf_event_update_time(event); 695 /* 696 * If a group leader gets enabled/disabled all its siblings 697 * are affected too. 698 */ 699 if ((event->state < 0) ^ (state < 0)) 700 perf_event_update_sibling_time(event); 701 702 WRITE_ONCE(event->state, state); 703 } 704 705 /* 706 * UP store-release, load-acquire 707 */ 708 709 #define __store_release(ptr, val) \ 710 do { \ 711 barrier(); \ 712 WRITE_ONCE(*(ptr), (val)); \ 713 } while (0) 714 715 #define __load_acquire(ptr) \ 716 ({ \ 717 __unqual_scalar_typeof(*(ptr)) ___p = READ_ONCE(*(ptr)); \ 718 barrier(); \ 719 ___p; \ 720 }) 721 722 #define for_each_epc(_epc, _ctx, _pmu, _cgroup) \ 723 list_for_each_entry(_epc, &((_ctx)->pmu_ctx_list), pmu_ctx_entry) \ 724 if (_cgroup && !_epc->nr_cgroups) \ 725 continue; \ 726 else if (_pmu && _epc->pmu != _pmu) \ 727 continue; \ 728 else 729 730 static void perf_ctx_disable(struct perf_event_context *ctx, bool cgroup) 731 { 732 struct perf_event_pmu_context *pmu_ctx; 733 734 for_each_epc(pmu_ctx, ctx, NULL, cgroup) 735 perf_pmu_disable(pmu_ctx->pmu); 736 } 737 738 static void perf_ctx_enable(struct perf_event_context *ctx, bool cgroup) 739 { 740 struct perf_event_pmu_context *pmu_ctx; 741 742 for_each_epc(pmu_ctx, ctx, NULL, cgroup) 743 perf_pmu_enable(pmu_ctx->pmu); 744 } 745 746 static void ctx_sched_out(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_t event_type); 747 static void ctx_sched_in(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_t event_type); 748 749 #ifdef CONFIG_CGROUP_PERF 750 751 static inline bool 752 perf_cgroup_match(struct perf_event *event) 753 { 754 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 755 756 /* @event doesn't care about cgroup */ 757 if (!event->cgrp) 758 return true; 759 760 /* wants specific cgroup scope but @cpuctx isn't associated with any */ 761 if (!cpuctx->cgrp) 762 return false; 763 764 /* 765 * Cgroup scoping is recursive. An event enabled for a cgroup is 766 * also enabled for all its descendant cgroups. If @cpuctx's 767 * cgroup is a descendant of @event's (the test covers identity 768 * case), it's a match. 769 */ 770 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, 771 event->cgrp->css.cgroup); 772 } 773 774 static inline void perf_detach_cgroup(struct perf_event *event) 775 { 776 css_put(&event->cgrp->css); 777 event->cgrp = NULL; 778 } 779 780 static inline int is_cgroup_event(struct perf_event *event) 781 { 782 return event->cgrp != NULL; 783 } 784 785 static inline u64 perf_cgroup_event_time(struct perf_event *event) 786 { 787 struct perf_cgroup_info *t; 788 789 t = per_cpu_ptr(event->cgrp->info, event->cpu); 790 return t->time; 791 } 792 793 static inline u64 perf_cgroup_event_time_now(struct perf_event *event, u64 now) 794 { 795 struct perf_cgroup_info *t; 796 797 t = per_cpu_ptr(event->cgrp->info, event->cpu); 798 if (!__load_acquire(&t->active)) 799 return t->time; 800 now += READ_ONCE(t->timeoffset); 801 return now; 802 } 803 804 static inline void __update_cgrp_time(struct perf_cgroup_info *info, u64 now, bool adv) 805 { 806 if (adv) 807 info->time += now - info->timestamp; 808 info->timestamp = now; 809 /* 810 * see update_context_time() 811 */ 812 WRITE_ONCE(info->timeoffset, info->time - info->timestamp); 813 } 814 815 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx, bool final) 816 { 817 struct perf_cgroup *cgrp = cpuctx->cgrp; 818 struct cgroup_subsys_state *css; 819 struct perf_cgroup_info *info; 820 821 if (cgrp) { 822 u64 now = perf_clock(); 823 824 for (css = &cgrp->css; css; css = css->parent) { 825 cgrp = container_of(css, struct perf_cgroup, css); 826 info = this_cpu_ptr(cgrp->info); 827 828 __update_cgrp_time(info, now, true); 829 if (final) 830 __store_release(&info->active, 0); 831 } 832 } 833 } 834 835 static inline void update_cgrp_time_from_event(struct perf_event *event) 836 { 837 struct perf_cgroup_info *info; 838 839 /* 840 * ensure we access cgroup data only when needed and 841 * when we know the cgroup is pinned (css_get) 842 */ 843 if (!is_cgroup_event(event)) 844 return; 845 846 info = this_cpu_ptr(event->cgrp->info); 847 /* 848 * Do not update time when cgroup is not active 849 */ 850 if (info->active) 851 __update_cgrp_time(info, perf_clock(), true); 852 } 853 854 static inline void 855 perf_cgroup_set_timestamp(struct perf_cpu_context *cpuctx) 856 { 857 struct perf_event_context *ctx = &cpuctx->ctx; 858 struct perf_cgroup *cgrp = cpuctx->cgrp; 859 struct perf_cgroup_info *info; 860 struct cgroup_subsys_state *css; 861 862 /* 863 * ctx->lock held by caller 864 * ensure we do not access cgroup data 865 * unless we have the cgroup pinned (css_get) 866 */ 867 if (!cgrp) 868 return; 869 870 WARN_ON_ONCE(!ctx->nr_cgroups); 871 872 for (css = &cgrp->css; css; css = css->parent) { 873 cgrp = container_of(css, struct perf_cgroup, css); 874 info = this_cpu_ptr(cgrp->info); 875 __update_cgrp_time(info, ctx->timestamp, false); 876 __store_release(&info->active, 1); 877 } 878 } 879 880 /* 881 * reschedule events based on the cgroup constraint of task. 882 */ 883 static void perf_cgroup_switch(struct task_struct *task) 884 { 885 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 886 struct perf_cgroup *cgrp; 887 888 /* 889 * cpuctx->cgrp is set when the first cgroup event enabled, 890 * and is cleared when the last cgroup event disabled. 891 */ 892 if (READ_ONCE(cpuctx->cgrp) == NULL) 893 return; 894 895 WARN_ON_ONCE(cpuctx->ctx.nr_cgroups == 0); 896 897 cgrp = perf_cgroup_from_task(task, NULL); 898 if (READ_ONCE(cpuctx->cgrp) == cgrp) 899 return; 900 901 perf_ctx_lock(cpuctx, cpuctx->task_ctx); 902 perf_ctx_disable(&cpuctx->ctx, true); 903 904 ctx_sched_out(&cpuctx->ctx, NULL, EVENT_ALL|EVENT_CGROUP); 905 /* 906 * must not be done before ctxswout due 907 * to update_cgrp_time_from_cpuctx() in 908 * ctx_sched_out() 909 */ 910 cpuctx->cgrp = cgrp; 911 /* 912 * set cgrp before ctxsw in to allow 913 * perf_cgroup_set_timestamp() in ctx_sched_in() 914 * to not have to pass task around 915 */ 916 ctx_sched_in(&cpuctx->ctx, NULL, EVENT_ALL|EVENT_CGROUP); 917 918 perf_ctx_enable(&cpuctx->ctx, true); 919 perf_ctx_unlock(cpuctx, cpuctx->task_ctx); 920 } 921 922 static int perf_cgroup_ensure_storage(struct perf_event *event, 923 struct cgroup_subsys_state *css) 924 { 925 struct perf_cpu_context *cpuctx; 926 struct perf_event **storage; 927 int cpu, heap_size, ret = 0; 928 929 /* 930 * Allow storage to have sufficient space for an iterator for each 931 * possibly nested cgroup plus an iterator for events with no cgroup. 932 */ 933 for (heap_size = 1; css; css = css->parent) 934 heap_size++; 935 936 for_each_possible_cpu(cpu) { 937 cpuctx = per_cpu_ptr(&perf_cpu_context, cpu); 938 if (heap_size <= cpuctx->heap_size) 939 continue; 940 941 storage = kmalloc_node(heap_size * sizeof(struct perf_event *), 942 GFP_KERNEL, cpu_to_node(cpu)); 943 if (!storage) { 944 ret = -ENOMEM; 945 break; 946 } 947 948 raw_spin_lock_irq(&cpuctx->ctx.lock); 949 if (cpuctx->heap_size < heap_size) { 950 swap(cpuctx->heap, storage); 951 if (storage == cpuctx->heap_default) 952 storage = NULL; 953 cpuctx->heap_size = heap_size; 954 } 955 raw_spin_unlock_irq(&cpuctx->ctx.lock); 956 957 kfree(storage); 958 } 959 960 return ret; 961 } 962 963 static inline int perf_cgroup_connect(int fd, struct perf_event *event, 964 struct perf_event_attr *attr, 965 struct perf_event *group_leader) 966 { 967 struct perf_cgroup *cgrp; 968 struct cgroup_subsys_state *css; 969 CLASS(fd, f)(fd); 970 int ret = 0; 971 972 if (fd_empty(f)) 973 return -EBADF; 974 975 css = css_tryget_online_from_dir(fd_file(f)->f_path.dentry, 976 &perf_event_cgrp_subsys); 977 if (IS_ERR(css)) 978 return PTR_ERR(css); 979 980 ret = perf_cgroup_ensure_storage(event, css); 981 if (ret) 982 return ret; 983 984 cgrp = container_of(css, struct perf_cgroup, css); 985 event->cgrp = cgrp; 986 987 /* 988 * all events in a group must monitor 989 * the same cgroup because a task belongs 990 * to only one perf cgroup at a time 991 */ 992 if (group_leader && group_leader->cgrp != cgrp) { 993 perf_detach_cgroup(event); 994 ret = -EINVAL; 995 } 996 return ret; 997 } 998 999 static inline void 1000 perf_cgroup_event_enable(struct perf_event *event, struct perf_event_context *ctx) 1001 { 1002 struct perf_cpu_context *cpuctx; 1003 1004 if (!is_cgroup_event(event)) 1005 return; 1006 1007 event->pmu_ctx->nr_cgroups++; 1008 1009 /* 1010 * Because cgroup events are always per-cpu events, 1011 * @ctx == &cpuctx->ctx. 1012 */ 1013 cpuctx = container_of(ctx, struct perf_cpu_context, ctx); 1014 1015 if (ctx->nr_cgroups++) 1016 return; 1017 1018 cpuctx->cgrp = perf_cgroup_from_task(current, ctx); 1019 } 1020 1021 static inline void 1022 perf_cgroup_event_disable(struct perf_event *event, struct perf_event_context *ctx) 1023 { 1024 struct perf_cpu_context *cpuctx; 1025 1026 if (!is_cgroup_event(event)) 1027 return; 1028 1029 event->pmu_ctx->nr_cgroups--; 1030 1031 /* 1032 * Because cgroup events are always per-cpu events, 1033 * @ctx == &cpuctx->ctx. 1034 */ 1035 cpuctx = container_of(ctx, struct perf_cpu_context, ctx); 1036 1037 if (--ctx->nr_cgroups) 1038 return; 1039 1040 cpuctx->cgrp = NULL; 1041 } 1042 1043 #else /* !CONFIG_CGROUP_PERF */ 1044 1045 static inline bool 1046 perf_cgroup_match(struct perf_event *event) 1047 { 1048 return true; 1049 } 1050 1051 static inline void perf_detach_cgroup(struct perf_event *event) 1052 {} 1053 1054 static inline int is_cgroup_event(struct perf_event *event) 1055 { 1056 return 0; 1057 } 1058 1059 static inline void update_cgrp_time_from_event(struct perf_event *event) 1060 { 1061 } 1062 1063 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx, 1064 bool final) 1065 { 1066 } 1067 1068 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, 1069 struct perf_event_attr *attr, 1070 struct perf_event *group_leader) 1071 { 1072 return -EINVAL; 1073 } 1074 1075 static inline void 1076 perf_cgroup_set_timestamp(struct perf_cpu_context *cpuctx) 1077 { 1078 } 1079 1080 static inline u64 perf_cgroup_event_time(struct perf_event *event) 1081 { 1082 return 0; 1083 } 1084 1085 static inline u64 perf_cgroup_event_time_now(struct perf_event *event, u64 now) 1086 { 1087 return 0; 1088 } 1089 1090 static inline void 1091 perf_cgroup_event_enable(struct perf_event *event, struct perf_event_context *ctx) 1092 { 1093 } 1094 1095 static inline void 1096 perf_cgroup_event_disable(struct perf_event *event, struct perf_event_context *ctx) 1097 { 1098 } 1099 1100 static void perf_cgroup_switch(struct task_struct *task) 1101 { 1102 } 1103 #endif 1104 1105 /* 1106 * set default to be dependent on timer tick just 1107 * like original code 1108 */ 1109 #define PERF_CPU_HRTIMER (1000 / HZ) 1110 /* 1111 * function must be called with interrupts disabled 1112 */ 1113 static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr) 1114 { 1115 struct perf_cpu_pmu_context *cpc; 1116 bool rotations; 1117 1118 lockdep_assert_irqs_disabled(); 1119 1120 cpc = container_of(hr, struct perf_cpu_pmu_context, hrtimer); 1121 rotations = perf_rotate_context(cpc); 1122 1123 raw_spin_lock(&cpc->hrtimer_lock); 1124 if (rotations) 1125 hrtimer_forward_now(hr, cpc->hrtimer_interval); 1126 else 1127 cpc->hrtimer_active = 0; 1128 raw_spin_unlock(&cpc->hrtimer_lock); 1129 1130 return rotations ? HRTIMER_RESTART : HRTIMER_NORESTART; 1131 } 1132 1133 static void __perf_mux_hrtimer_init(struct perf_cpu_pmu_context *cpc, int cpu) 1134 { 1135 struct hrtimer *timer = &cpc->hrtimer; 1136 struct pmu *pmu = cpc->epc.pmu; 1137 u64 interval; 1138 1139 /* 1140 * check default is sane, if not set then force to 1141 * default interval (1/tick) 1142 */ 1143 interval = pmu->hrtimer_interval_ms; 1144 if (interval < 1) 1145 interval = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER; 1146 1147 cpc->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * interval); 1148 1149 raw_spin_lock_init(&cpc->hrtimer_lock); 1150 hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS_PINNED_HARD); 1151 timer->function = perf_mux_hrtimer_handler; 1152 } 1153 1154 static int perf_mux_hrtimer_restart(struct perf_cpu_pmu_context *cpc) 1155 { 1156 struct hrtimer *timer = &cpc->hrtimer; 1157 unsigned long flags; 1158 1159 raw_spin_lock_irqsave(&cpc->hrtimer_lock, flags); 1160 if (!cpc->hrtimer_active) { 1161 cpc->hrtimer_active = 1; 1162 hrtimer_forward_now(timer, cpc->hrtimer_interval); 1163 hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED_HARD); 1164 } 1165 raw_spin_unlock_irqrestore(&cpc->hrtimer_lock, flags); 1166 1167 return 0; 1168 } 1169 1170 static int perf_mux_hrtimer_restart_ipi(void *arg) 1171 { 1172 return perf_mux_hrtimer_restart(arg); 1173 } 1174 1175 void perf_pmu_disable(struct pmu *pmu) 1176 { 1177 int *count = this_cpu_ptr(pmu->pmu_disable_count); 1178 if (!(*count)++) 1179 pmu->pmu_disable(pmu); 1180 } 1181 1182 void perf_pmu_enable(struct pmu *pmu) 1183 { 1184 int *count = this_cpu_ptr(pmu->pmu_disable_count); 1185 if (!--(*count)) 1186 pmu->pmu_enable(pmu); 1187 } 1188 1189 static void perf_assert_pmu_disabled(struct pmu *pmu) 1190 { 1191 WARN_ON_ONCE(*this_cpu_ptr(pmu->pmu_disable_count) == 0); 1192 } 1193 1194 static void get_ctx(struct perf_event_context *ctx) 1195 { 1196 refcount_inc(&ctx->refcount); 1197 } 1198 1199 static void *alloc_task_ctx_data(struct pmu *pmu) 1200 { 1201 if (pmu->task_ctx_cache) 1202 return kmem_cache_zalloc(pmu->task_ctx_cache, GFP_KERNEL); 1203 1204 return NULL; 1205 } 1206 1207 static void free_task_ctx_data(struct pmu *pmu, void *task_ctx_data) 1208 { 1209 if (pmu->task_ctx_cache && task_ctx_data) 1210 kmem_cache_free(pmu->task_ctx_cache, task_ctx_data); 1211 } 1212 1213 static void free_ctx(struct rcu_head *head) 1214 { 1215 struct perf_event_context *ctx; 1216 1217 ctx = container_of(head, struct perf_event_context, rcu_head); 1218 kfree(ctx); 1219 } 1220 1221 static void put_ctx(struct perf_event_context *ctx) 1222 { 1223 if (refcount_dec_and_test(&ctx->refcount)) { 1224 if (ctx->parent_ctx) 1225 put_ctx(ctx->parent_ctx); 1226 if (ctx->task && ctx->task != TASK_TOMBSTONE) 1227 put_task_struct(ctx->task); 1228 call_rcu(&ctx->rcu_head, free_ctx); 1229 } 1230 } 1231 1232 /* 1233 * Because of perf_event::ctx migration in sys_perf_event_open::move_group and 1234 * perf_pmu_migrate_context() we need some magic. 1235 * 1236 * Those places that change perf_event::ctx will hold both 1237 * perf_event_ctx::mutex of the 'old' and 'new' ctx value. 1238 * 1239 * Lock ordering is by mutex address. There are two other sites where 1240 * perf_event_context::mutex nests and those are: 1241 * 1242 * - perf_event_exit_task_context() [ child , 0 ] 1243 * perf_event_exit_event() 1244 * put_event() [ parent, 1 ] 1245 * 1246 * - perf_event_init_context() [ parent, 0 ] 1247 * inherit_task_group() 1248 * inherit_group() 1249 * inherit_event() 1250 * perf_event_alloc() 1251 * perf_init_event() 1252 * perf_try_init_event() [ child , 1 ] 1253 * 1254 * While it appears there is an obvious deadlock here -- the parent and child 1255 * nesting levels are inverted between the two. This is in fact safe because 1256 * life-time rules separate them. That is an exiting task cannot fork, and a 1257 * spawning task cannot (yet) exit. 1258 * 1259 * But remember that these are parent<->child context relations, and 1260 * migration does not affect children, therefore these two orderings should not 1261 * interact. 1262 * 1263 * The change in perf_event::ctx does not affect children (as claimed above) 1264 * because the sys_perf_event_open() case will install a new event and break 1265 * the ctx parent<->child relation, and perf_pmu_migrate_context() is only 1266 * concerned with cpuctx and that doesn't have children. 1267 * 1268 * The places that change perf_event::ctx will issue: 1269 * 1270 * perf_remove_from_context(); 1271 * synchronize_rcu(); 1272 * perf_install_in_context(); 1273 * 1274 * to affect the change. The remove_from_context() + synchronize_rcu() should 1275 * quiesce the event, after which we can install it in the new location. This 1276 * means that only external vectors (perf_fops, prctl) can perturb the event 1277 * while in transit. Therefore all such accessors should also acquire 1278 * perf_event_context::mutex to serialize against this. 1279 * 1280 * However; because event->ctx can change while we're waiting to acquire 1281 * ctx->mutex we must be careful and use the below perf_event_ctx_lock() 1282 * function. 1283 * 1284 * Lock order: 1285 * exec_update_lock 1286 * task_struct::perf_event_mutex 1287 * perf_event_context::mutex 1288 * perf_event::child_mutex; 1289 * perf_event_context::lock 1290 * mmap_lock 1291 * perf_event::mmap_mutex 1292 * perf_buffer::aux_mutex 1293 * perf_addr_filters_head::lock 1294 * 1295 * cpu_hotplug_lock 1296 * pmus_lock 1297 * cpuctx->mutex / perf_event_context::mutex 1298 */ 1299 static struct perf_event_context * 1300 perf_event_ctx_lock_nested(struct perf_event *event, int nesting) 1301 { 1302 struct perf_event_context *ctx; 1303 1304 again: 1305 rcu_read_lock(); 1306 ctx = READ_ONCE(event->ctx); 1307 if (!refcount_inc_not_zero(&ctx->refcount)) { 1308 rcu_read_unlock(); 1309 goto again; 1310 } 1311 rcu_read_unlock(); 1312 1313 mutex_lock_nested(&ctx->mutex, nesting); 1314 if (event->ctx != ctx) { 1315 mutex_unlock(&ctx->mutex); 1316 put_ctx(ctx); 1317 goto again; 1318 } 1319 1320 return ctx; 1321 } 1322 1323 static inline struct perf_event_context * 1324 perf_event_ctx_lock(struct perf_event *event) 1325 { 1326 return perf_event_ctx_lock_nested(event, 0); 1327 } 1328 1329 static void perf_event_ctx_unlock(struct perf_event *event, 1330 struct perf_event_context *ctx) 1331 { 1332 mutex_unlock(&ctx->mutex); 1333 put_ctx(ctx); 1334 } 1335 1336 /* 1337 * This must be done under the ctx->lock, such as to serialize against 1338 * context_equiv(), therefore we cannot call put_ctx() since that might end up 1339 * calling scheduler related locks and ctx->lock nests inside those. 1340 */ 1341 static __must_check struct perf_event_context * 1342 unclone_ctx(struct perf_event_context *ctx) 1343 { 1344 struct perf_event_context *parent_ctx = ctx->parent_ctx; 1345 1346 lockdep_assert_held(&ctx->lock); 1347 1348 if (parent_ctx) 1349 ctx->parent_ctx = NULL; 1350 ctx->generation++; 1351 1352 return parent_ctx; 1353 } 1354 1355 static u32 perf_event_pid_type(struct perf_event *event, struct task_struct *p, 1356 enum pid_type type) 1357 { 1358 u32 nr; 1359 /* 1360 * only top level events have the pid namespace they were created in 1361 */ 1362 if (event->parent) 1363 event = event->parent; 1364 1365 nr = __task_pid_nr_ns(p, type, event->ns); 1366 /* avoid -1 if it is idle thread or runs in another ns */ 1367 if (!nr && !pid_alive(p)) 1368 nr = -1; 1369 return nr; 1370 } 1371 1372 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) 1373 { 1374 return perf_event_pid_type(event, p, PIDTYPE_TGID); 1375 } 1376 1377 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) 1378 { 1379 return perf_event_pid_type(event, p, PIDTYPE_PID); 1380 } 1381 1382 /* 1383 * If we inherit events we want to return the parent event id 1384 * to userspace. 1385 */ 1386 static u64 primary_event_id(struct perf_event *event) 1387 { 1388 u64 id = event->id; 1389 1390 if (event->parent) 1391 id = event->parent->id; 1392 1393 return id; 1394 } 1395 1396 /* 1397 * Get the perf_event_context for a task and lock it. 1398 * 1399 * This has to cope with the fact that until it is locked, 1400 * the context could get moved to another task. 1401 */ 1402 static struct perf_event_context * 1403 perf_lock_task_context(struct task_struct *task, unsigned long *flags) 1404 { 1405 struct perf_event_context *ctx; 1406 1407 retry: 1408 /* 1409 * One of the few rules of preemptible RCU is that one cannot do 1410 * rcu_read_unlock() while holding a scheduler (or nested) lock when 1411 * part of the read side critical section was irqs-enabled -- see 1412 * rcu_read_unlock_special(). 1413 * 1414 * Since ctx->lock nests under rq->lock we must ensure the entire read 1415 * side critical section has interrupts disabled. 1416 */ 1417 local_irq_save(*flags); 1418 rcu_read_lock(); 1419 ctx = rcu_dereference(task->perf_event_ctxp); 1420 if (ctx) { 1421 /* 1422 * If this context is a clone of another, it might 1423 * get swapped for another underneath us by 1424 * perf_event_task_sched_out, though the 1425 * rcu_read_lock() protects us from any context 1426 * getting freed. Lock the context and check if it 1427 * got swapped before we could get the lock, and retry 1428 * if so. If we locked the right context, then it 1429 * can't get swapped on us any more. 1430 */ 1431 raw_spin_lock(&ctx->lock); 1432 if (ctx != rcu_dereference(task->perf_event_ctxp)) { 1433 raw_spin_unlock(&ctx->lock); 1434 rcu_read_unlock(); 1435 local_irq_restore(*flags); 1436 goto retry; 1437 } 1438 1439 if (ctx->task == TASK_TOMBSTONE || 1440 !refcount_inc_not_zero(&ctx->refcount)) { 1441 raw_spin_unlock(&ctx->lock); 1442 ctx = NULL; 1443 } else { 1444 WARN_ON_ONCE(ctx->task != task); 1445 } 1446 } 1447 rcu_read_unlock(); 1448 if (!ctx) 1449 local_irq_restore(*flags); 1450 return ctx; 1451 } 1452 1453 /* 1454 * Get the context for a task and increment its pin_count so it 1455 * can't get swapped to another task. This also increments its 1456 * reference count so that the context can't get freed. 1457 */ 1458 static struct perf_event_context * 1459 perf_pin_task_context(struct task_struct *task) 1460 { 1461 struct perf_event_context *ctx; 1462 unsigned long flags; 1463 1464 ctx = perf_lock_task_context(task, &flags); 1465 if (ctx) { 1466 ++ctx->pin_count; 1467 raw_spin_unlock_irqrestore(&ctx->lock, flags); 1468 } 1469 return ctx; 1470 } 1471 1472 static void perf_unpin_context(struct perf_event_context *ctx) 1473 { 1474 unsigned long flags; 1475 1476 raw_spin_lock_irqsave(&ctx->lock, flags); 1477 --ctx->pin_count; 1478 raw_spin_unlock_irqrestore(&ctx->lock, flags); 1479 } 1480 1481 /* 1482 * Update the record of the current time in a context. 1483 */ 1484 static void __update_context_time(struct perf_event_context *ctx, bool adv) 1485 { 1486 u64 now = perf_clock(); 1487 1488 lockdep_assert_held(&ctx->lock); 1489 1490 if (adv) 1491 ctx->time += now - ctx->timestamp; 1492 ctx->timestamp = now; 1493 1494 /* 1495 * The above: time' = time + (now - timestamp), can be re-arranged 1496 * into: time` = now + (time - timestamp), which gives a single value 1497 * offset to compute future time without locks on. 1498 * 1499 * See perf_event_time_now(), which can be used from NMI context where 1500 * it's (obviously) not possible to acquire ctx->lock in order to read 1501 * both the above values in a consistent manner. 1502 */ 1503 WRITE_ONCE(ctx->timeoffset, ctx->time - ctx->timestamp); 1504 } 1505 1506 static void update_context_time(struct perf_event_context *ctx) 1507 { 1508 __update_context_time(ctx, true); 1509 } 1510 1511 static u64 perf_event_time(struct perf_event *event) 1512 { 1513 struct perf_event_context *ctx = event->ctx; 1514 1515 if (unlikely(!ctx)) 1516 return 0; 1517 1518 if (is_cgroup_event(event)) 1519 return perf_cgroup_event_time(event); 1520 1521 return ctx->time; 1522 } 1523 1524 static u64 perf_event_time_now(struct perf_event *event, u64 now) 1525 { 1526 struct perf_event_context *ctx = event->ctx; 1527 1528 if (unlikely(!ctx)) 1529 return 0; 1530 1531 if (is_cgroup_event(event)) 1532 return perf_cgroup_event_time_now(event, now); 1533 1534 if (!(__load_acquire(&ctx->is_active) & EVENT_TIME)) 1535 return ctx->time; 1536 1537 now += READ_ONCE(ctx->timeoffset); 1538 return now; 1539 } 1540 1541 static enum event_type_t get_event_type(struct perf_event *event) 1542 { 1543 struct perf_event_context *ctx = event->ctx; 1544 enum event_type_t event_type; 1545 1546 lockdep_assert_held(&ctx->lock); 1547 1548 /* 1549 * It's 'group type', really, because if our group leader is 1550 * pinned, so are we. 1551 */ 1552 if (event->group_leader != event) 1553 event = event->group_leader; 1554 1555 event_type = event->attr.pinned ? EVENT_PINNED : EVENT_FLEXIBLE; 1556 if (!ctx->task) 1557 event_type |= EVENT_CPU; 1558 1559 return event_type; 1560 } 1561 1562 /* 1563 * Helper function to initialize event group nodes. 1564 */ 1565 static void init_event_group(struct perf_event *event) 1566 { 1567 RB_CLEAR_NODE(&event->group_node); 1568 event->group_index = 0; 1569 } 1570 1571 /* 1572 * Extract pinned or flexible groups from the context 1573 * based on event attrs bits. 1574 */ 1575 static struct perf_event_groups * 1576 get_event_groups(struct perf_event *event, struct perf_event_context *ctx) 1577 { 1578 if (event->attr.pinned) 1579 return &ctx->pinned_groups; 1580 else 1581 return &ctx->flexible_groups; 1582 } 1583 1584 /* 1585 * Helper function to initializes perf_event_group trees. 1586 */ 1587 static void perf_event_groups_init(struct perf_event_groups *groups) 1588 { 1589 groups->tree = RB_ROOT; 1590 groups->index = 0; 1591 } 1592 1593 static inline struct cgroup *event_cgroup(const struct perf_event *event) 1594 { 1595 struct cgroup *cgroup = NULL; 1596 1597 #ifdef CONFIG_CGROUP_PERF 1598 if (event->cgrp) 1599 cgroup = event->cgrp->css.cgroup; 1600 #endif 1601 1602 return cgroup; 1603 } 1604 1605 /* 1606 * Compare function for event groups; 1607 * 1608 * Implements complex key that first sorts by CPU and then by virtual index 1609 * which provides ordering when rotating groups for the same CPU. 1610 */ 1611 static __always_inline int 1612 perf_event_groups_cmp(const int left_cpu, const struct pmu *left_pmu, 1613 const struct cgroup *left_cgroup, const u64 left_group_index, 1614 const struct perf_event *right) 1615 { 1616 if (left_cpu < right->cpu) 1617 return -1; 1618 if (left_cpu > right->cpu) 1619 return 1; 1620 1621 if (left_pmu) { 1622 if (left_pmu < right->pmu_ctx->pmu) 1623 return -1; 1624 if (left_pmu > right->pmu_ctx->pmu) 1625 return 1; 1626 } 1627 1628 #ifdef CONFIG_CGROUP_PERF 1629 { 1630 const struct cgroup *right_cgroup = event_cgroup(right); 1631 1632 if (left_cgroup != right_cgroup) { 1633 if (!left_cgroup) { 1634 /* 1635 * Left has no cgroup but right does, no 1636 * cgroups come first. 1637 */ 1638 return -1; 1639 } 1640 if (!right_cgroup) { 1641 /* 1642 * Right has no cgroup but left does, no 1643 * cgroups come first. 1644 */ 1645 return 1; 1646 } 1647 /* Two dissimilar cgroups, order by id. */ 1648 if (cgroup_id(left_cgroup) < cgroup_id(right_cgroup)) 1649 return -1; 1650 1651 return 1; 1652 } 1653 } 1654 #endif 1655 1656 if (left_group_index < right->group_index) 1657 return -1; 1658 if (left_group_index > right->group_index) 1659 return 1; 1660 1661 return 0; 1662 } 1663 1664 #define __node_2_pe(node) \ 1665 rb_entry((node), struct perf_event, group_node) 1666 1667 static inline bool __group_less(struct rb_node *a, const struct rb_node *b) 1668 { 1669 struct perf_event *e = __node_2_pe(a); 1670 return perf_event_groups_cmp(e->cpu, e->pmu_ctx->pmu, event_cgroup(e), 1671 e->group_index, __node_2_pe(b)) < 0; 1672 } 1673 1674 struct __group_key { 1675 int cpu; 1676 struct pmu *pmu; 1677 struct cgroup *cgroup; 1678 }; 1679 1680 static inline int __group_cmp(const void *key, const struct rb_node *node) 1681 { 1682 const struct __group_key *a = key; 1683 const struct perf_event *b = __node_2_pe(node); 1684 1685 /* partial/subtree match: @cpu, @pmu, @cgroup; ignore: @group_index */ 1686 return perf_event_groups_cmp(a->cpu, a->pmu, a->cgroup, b->group_index, b); 1687 } 1688 1689 static inline int 1690 __group_cmp_ignore_cgroup(const void *key, const struct rb_node *node) 1691 { 1692 const struct __group_key *a = key; 1693 const struct perf_event *b = __node_2_pe(node); 1694 1695 /* partial/subtree match: @cpu, @pmu, ignore: @cgroup, @group_index */ 1696 return perf_event_groups_cmp(a->cpu, a->pmu, event_cgroup(b), 1697 b->group_index, b); 1698 } 1699 1700 /* 1701 * Insert @event into @groups' tree; using 1702 * {@event->cpu, @event->pmu_ctx->pmu, event_cgroup(@event), ++@groups->index} 1703 * as key. This places it last inside the {cpu,pmu,cgroup} subtree. 1704 */ 1705 static void 1706 perf_event_groups_insert(struct perf_event_groups *groups, 1707 struct perf_event *event) 1708 { 1709 event->group_index = ++groups->index; 1710 1711 rb_add(&event->group_node, &groups->tree, __group_less); 1712 } 1713 1714 /* 1715 * Helper function to insert event into the pinned or flexible groups. 1716 */ 1717 static void 1718 add_event_to_groups(struct perf_event *event, struct perf_event_context *ctx) 1719 { 1720 struct perf_event_groups *groups; 1721 1722 groups = get_event_groups(event, ctx); 1723 perf_event_groups_insert(groups, event); 1724 } 1725 1726 /* 1727 * Delete a group from a tree. 1728 */ 1729 static void 1730 perf_event_groups_delete(struct perf_event_groups *groups, 1731 struct perf_event *event) 1732 { 1733 WARN_ON_ONCE(RB_EMPTY_NODE(&event->group_node) || 1734 RB_EMPTY_ROOT(&groups->tree)); 1735 1736 rb_erase(&event->group_node, &groups->tree); 1737 init_event_group(event); 1738 } 1739 1740 /* 1741 * Helper function to delete event from its groups. 1742 */ 1743 static void 1744 del_event_from_groups(struct perf_event *event, struct perf_event_context *ctx) 1745 { 1746 struct perf_event_groups *groups; 1747 1748 groups = get_event_groups(event, ctx); 1749 perf_event_groups_delete(groups, event); 1750 } 1751 1752 /* 1753 * Get the leftmost event in the {cpu,pmu,cgroup} subtree. 1754 */ 1755 static struct perf_event * 1756 perf_event_groups_first(struct perf_event_groups *groups, int cpu, 1757 struct pmu *pmu, struct cgroup *cgrp) 1758 { 1759 struct __group_key key = { 1760 .cpu = cpu, 1761 .pmu = pmu, 1762 .cgroup = cgrp, 1763 }; 1764 struct rb_node *node; 1765 1766 node = rb_find_first(&key, &groups->tree, __group_cmp); 1767 if (node) 1768 return __node_2_pe(node); 1769 1770 return NULL; 1771 } 1772 1773 static struct perf_event * 1774 perf_event_groups_next(struct perf_event *event, struct pmu *pmu) 1775 { 1776 struct __group_key key = { 1777 .cpu = event->cpu, 1778 .pmu = pmu, 1779 .cgroup = event_cgroup(event), 1780 }; 1781 struct rb_node *next; 1782 1783 next = rb_next_match(&key, &event->group_node, __group_cmp); 1784 if (next) 1785 return __node_2_pe(next); 1786 1787 return NULL; 1788 } 1789 1790 #define perf_event_groups_for_cpu_pmu(event, groups, cpu, pmu) \ 1791 for (event = perf_event_groups_first(groups, cpu, pmu, NULL); \ 1792 event; event = perf_event_groups_next(event, pmu)) 1793 1794 /* 1795 * Iterate through the whole groups tree. 1796 */ 1797 #define perf_event_groups_for_each(event, groups) \ 1798 for (event = rb_entry_safe(rb_first(&((groups)->tree)), \ 1799 typeof(*event), group_node); event; \ 1800 event = rb_entry_safe(rb_next(&event->group_node), \ 1801 typeof(*event), group_node)) 1802 1803 /* 1804 * Does the event attribute request inherit with PERF_SAMPLE_READ 1805 */ 1806 static inline bool has_inherit_and_sample_read(struct perf_event_attr *attr) 1807 { 1808 return attr->inherit && (attr->sample_type & PERF_SAMPLE_READ); 1809 } 1810 1811 /* 1812 * Add an event from the lists for its context. 1813 * Must be called with ctx->mutex and ctx->lock held. 1814 */ 1815 static void 1816 list_add_event(struct perf_event *event, struct perf_event_context *ctx) 1817 { 1818 lockdep_assert_held(&ctx->lock); 1819 1820 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); 1821 event->attach_state |= PERF_ATTACH_CONTEXT; 1822 1823 event->tstamp = perf_event_time(event); 1824 1825 /* 1826 * If we're a stand alone event or group leader, we go to the context 1827 * list, group events are kept attached to the group so that 1828 * perf_group_detach can, at all times, locate all siblings. 1829 */ 1830 if (event->group_leader == event) { 1831 event->group_caps = event->event_caps; 1832 add_event_to_groups(event, ctx); 1833 } 1834 1835 list_add_rcu(&event->event_entry, &ctx->event_list); 1836 ctx->nr_events++; 1837 if (event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT) 1838 ctx->nr_user++; 1839 if (event->attr.inherit_stat) 1840 ctx->nr_stat++; 1841 if (has_inherit_and_sample_read(&event->attr)) 1842 local_inc(&ctx->nr_no_switch_fast); 1843 1844 if (event->state > PERF_EVENT_STATE_OFF) 1845 perf_cgroup_event_enable(event, ctx); 1846 1847 ctx->generation++; 1848 event->pmu_ctx->nr_events++; 1849 } 1850 1851 /* 1852 * Initialize event state based on the perf_event_attr::disabled. 1853 */ 1854 static inline void perf_event__state_init(struct perf_event *event) 1855 { 1856 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : 1857 PERF_EVENT_STATE_INACTIVE; 1858 } 1859 1860 static int __perf_event_read_size(u64 read_format, int nr_siblings) 1861 { 1862 int entry = sizeof(u64); /* value */ 1863 int size = 0; 1864 int nr = 1; 1865 1866 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 1867 size += sizeof(u64); 1868 1869 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 1870 size += sizeof(u64); 1871 1872 if (read_format & PERF_FORMAT_ID) 1873 entry += sizeof(u64); 1874 1875 if (read_format & PERF_FORMAT_LOST) 1876 entry += sizeof(u64); 1877 1878 if (read_format & PERF_FORMAT_GROUP) { 1879 nr += nr_siblings; 1880 size += sizeof(u64); 1881 } 1882 1883 /* 1884 * Since perf_event_validate_size() limits this to 16k and inhibits 1885 * adding more siblings, this will never overflow. 1886 */ 1887 return size + nr * entry; 1888 } 1889 1890 static void __perf_event_header_size(struct perf_event *event, u64 sample_type) 1891 { 1892 struct perf_sample_data *data; 1893 u16 size = 0; 1894 1895 if (sample_type & PERF_SAMPLE_IP) 1896 size += sizeof(data->ip); 1897 1898 if (sample_type & PERF_SAMPLE_ADDR) 1899 size += sizeof(data->addr); 1900 1901 if (sample_type & PERF_SAMPLE_PERIOD) 1902 size += sizeof(data->period); 1903 1904 if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) 1905 size += sizeof(data->weight.full); 1906 1907 if (sample_type & PERF_SAMPLE_READ) 1908 size += event->read_size; 1909 1910 if (sample_type & PERF_SAMPLE_DATA_SRC) 1911 size += sizeof(data->data_src.val); 1912 1913 if (sample_type & PERF_SAMPLE_TRANSACTION) 1914 size += sizeof(data->txn); 1915 1916 if (sample_type & PERF_SAMPLE_PHYS_ADDR) 1917 size += sizeof(data->phys_addr); 1918 1919 if (sample_type & PERF_SAMPLE_CGROUP) 1920 size += sizeof(data->cgroup); 1921 1922 if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) 1923 size += sizeof(data->data_page_size); 1924 1925 if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) 1926 size += sizeof(data->code_page_size); 1927 1928 event->header_size = size; 1929 } 1930 1931 /* 1932 * Called at perf_event creation and when events are attached/detached from a 1933 * group. 1934 */ 1935 static void perf_event__header_size(struct perf_event *event) 1936 { 1937 event->read_size = 1938 __perf_event_read_size(event->attr.read_format, 1939 event->group_leader->nr_siblings); 1940 __perf_event_header_size(event, event->attr.sample_type); 1941 } 1942 1943 static void perf_event__id_header_size(struct perf_event *event) 1944 { 1945 struct perf_sample_data *data; 1946 u64 sample_type = event->attr.sample_type; 1947 u16 size = 0; 1948 1949 if (sample_type & PERF_SAMPLE_TID) 1950 size += sizeof(data->tid_entry); 1951 1952 if (sample_type & PERF_SAMPLE_TIME) 1953 size += sizeof(data->time); 1954 1955 if (sample_type & PERF_SAMPLE_IDENTIFIER) 1956 size += sizeof(data->id); 1957 1958 if (sample_type & PERF_SAMPLE_ID) 1959 size += sizeof(data->id); 1960 1961 if (sample_type & PERF_SAMPLE_STREAM_ID) 1962 size += sizeof(data->stream_id); 1963 1964 if (sample_type & PERF_SAMPLE_CPU) 1965 size += sizeof(data->cpu_entry); 1966 1967 event->id_header_size = size; 1968 } 1969 1970 /* 1971 * Check that adding an event to the group does not result in anybody 1972 * overflowing the 64k event limit imposed by the output buffer. 1973 * 1974 * Specifically, check that the read_size for the event does not exceed 16k, 1975 * read_size being the one term that grows with groups size. Since read_size 1976 * depends on per-event read_format, also (re)check the existing events. 1977 * 1978 * This leaves 48k for the constant size fields and things like callchains, 1979 * branch stacks and register sets. 1980 */ 1981 static bool perf_event_validate_size(struct perf_event *event) 1982 { 1983 struct perf_event *sibling, *group_leader = event->group_leader; 1984 1985 if (__perf_event_read_size(event->attr.read_format, 1986 group_leader->nr_siblings + 1) > 16*1024) 1987 return false; 1988 1989 if (__perf_event_read_size(group_leader->attr.read_format, 1990 group_leader->nr_siblings + 1) > 16*1024) 1991 return false; 1992 1993 /* 1994 * When creating a new group leader, group_leader->ctx is initialized 1995 * after the size has been validated, but we cannot safely use 1996 * for_each_sibling_event() until group_leader->ctx is set. A new group 1997 * leader cannot have any siblings yet, so we can safely skip checking 1998 * the non-existent siblings. 1999 */ 2000 if (event == group_leader) 2001 return true; 2002 2003 for_each_sibling_event(sibling, group_leader) { 2004 if (__perf_event_read_size(sibling->attr.read_format, 2005 group_leader->nr_siblings + 1) > 16*1024) 2006 return false; 2007 } 2008 2009 return true; 2010 } 2011 2012 static void perf_group_attach(struct perf_event *event) 2013 { 2014 struct perf_event *group_leader = event->group_leader, *pos; 2015 2016 lockdep_assert_held(&event->ctx->lock); 2017 2018 /* 2019 * We can have double attach due to group movement (move_group) in 2020 * perf_event_open(). 2021 */ 2022 if (event->attach_state & PERF_ATTACH_GROUP) 2023 return; 2024 2025 event->attach_state |= PERF_ATTACH_GROUP; 2026 2027 if (group_leader == event) 2028 return; 2029 2030 WARN_ON_ONCE(group_leader->ctx != event->ctx); 2031 2032 group_leader->group_caps &= event->event_caps; 2033 2034 list_add_tail(&event->sibling_list, &group_leader->sibling_list); 2035 group_leader->nr_siblings++; 2036 group_leader->group_generation++; 2037 2038 perf_event__header_size(group_leader); 2039 2040 for_each_sibling_event(pos, group_leader) 2041 perf_event__header_size(pos); 2042 } 2043 2044 /* 2045 * Remove an event from the lists for its context. 2046 * Must be called with ctx->mutex and ctx->lock held. 2047 */ 2048 static void 2049 list_del_event(struct perf_event *event, struct perf_event_context *ctx) 2050 { 2051 WARN_ON_ONCE(event->ctx != ctx); 2052 lockdep_assert_held(&ctx->lock); 2053 2054 /* 2055 * We can have double detach due to exit/hot-unplug + close. 2056 */ 2057 if (!(event->attach_state & PERF_ATTACH_CONTEXT)) 2058 return; 2059 2060 event->attach_state &= ~PERF_ATTACH_CONTEXT; 2061 2062 ctx->nr_events--; 2063 if (event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT) 2064 ctx->nr_user--; 2065 if (event->attr.inherit_stat) 2066 ctx->nr_stat--; 2067 if (has_inherit_and_sample_read(&event->attr)) 2068 local_dec(&ctx->nr_no_switch_fast); 2069 2070 list_del_rcu(&event->event_entry); 2071 2072 if (event->group_leader == event) 2073 del_event_from_groups(event, ctx); 2074 2075 /* 2076 * If event was in error state, then keep it 2077 * that way, otherwise bogus counts will be 2078 * returned on read(). The only way to get out 2079 * of error state is by explicit re-enabling 2080 * of the event 2081 */ 2082 if (event->state > PERF_EVENT_STATE_OFF) { 2083 perf_cgroup_event_disable(event, ctx); 2084 perf_event_set_state(event, PERF_EVENT_STATE_OFF); 2085 } 2086 2087 ctx->generation++; 2088 event->pmu_ctx->nr_events--; 2089 } 2090 2091 static int 2092 perf_aux_output_match(struct perf_event *event, struct perf_event *aux_event) 2093 { 2094 if (!has_aux(aux_event)) 2095 return 0; 2096 2097 if (!event->pmu->aux_output_match) 2098 return 0; 2099 2100 return event->pmu->aux_output_match(aux_event); 2101 } 2102 2103 static void put_event(struct perf_event *event); 2104 static void event_sched_out(struct perf_event *event, 2105 struct perf_event_context *ctx); 2106 2107 static void perf_put_aux_event(struct perf_event *event) 2108 { 2109 struct perf_event_context *ctx = event->ctx; 2110 struct perf_event *iter; 2111 2112 /* 2113 * If event uses aux_event tear down the link 2114 */ 2115 if (event->aux_event) { 2116 iter = event->aux_event; 2117 event->aux_event = NULL; 2118 put_event(iter); 2119 return; 2120 } 2121 2122 /* 2123 * If the event is an aux_event, tear down all links to 2124 * it from other events. 2125 */ 2126 for_each_sibling_event(iter, event->group_leader) { 2127 if (iter->aux_event != event) 2128 continue; 2129 2130 iter->aux_event = NULL; 2131 put_event(event); 2132 2133 /* 2134 * If it's ACTIVE, schedule it out and put it into ERROR 2135 * state so that we don't try to schedule it again. Note 2136 * that perf_event_enable() will clear the ERROR status. 2137 */ 2138 event_sched_out(iter, ctx); 2139 perf_event_set_state(event, PERF_EVENT_STATE_ERROR); 2140 } 2141 } 2142 2143 static bool perf_need_aux_event(struct perf_event *event) 2144 { 2145 return event->attr.aux_output || has_aux_action(event); 2146 } 2147 2148 static int perf_get_aux_event(struct perf_event *event, 2149 struct perf_event *group_leader) 2150 { 2151 /* 2152 * Our group leader must be an aux event if we want to be 2153 * an aux_output. This way, the aux event will precede its 2154 * aux_output events in the group, and therefore will always 2155 * schedule first. 2156 */ 2157 if (!group_leader) 2158 return 0; 2159 2160 /* 2161 * aux_output and aux_sample_size are mutually exclusive. 2162 */ 2163 if (event->attr.aux_output && event->attr.aux_sample_size) 2164 return 0; 2165 2166 if (event->attr.aux_output && 2167 !perf_aux_output_match(event, group_leader)) 2168 return 0; 2169 2170 if ((event->attr.aux_pause || event->attr.aux_resume) && 2171 !(group_leader->pmu->capabilities & PERF_PMU_CAP_AUX_PAUSE)) 2172 return 0; 2173 2174 if (event->attr.aux_sample_size && !group_leader->pmu->snapshot_aux) 2175 return 0; 2176 2177 if (!atomic_long_inc_not_zero(&group_leader->refcount)) 2178 return 0; 2179 2180 /* 2181 * Link aux_outputs to their aux event; this is undone in 2182 * perf_group_detach() by perf_put_aux_event(). When the 2183 * group in torn down, the aux_output events loose their 2184 * link to the aux_event and can't schedule any more. 2185 */ 2186 event->aux_event = group_leader; 2187 2188 return 1; 2189 } 2190 2191 static inline struct list_head *get_event_list(struct perf_event *event) 2192 { 2193 return event->attr.pinned ? &event->pmu_ctx->pinned_active : 2194 &event->pmu_ctx->flexible_active; 2195 } 2196 2197 /* 2198 * Events that have PERF_EV_CAP_SIBLING require being part of a group and 2199 * cannot exist on their own, schedule them out and move them into the ERROR 2200 * state. Also see _perf_event_enable(), it will not be able to recover 2201 * this ERROR state. 2202 */ 2203 static inline void perf_remove_sibling_event(struct perf_event *event) 2204 { 2205 event_sched_out(event, event->ctx); 2206 perf_event_set_state(event, PERF_EVENT_STATE_ERROR); 2207 } 2208 2209 static void perf_group_detach(struct perf_event *event) 2210 { 2211 struct perf_event *leader = event->group_leader; 2212 struct perf_event *sibling, *tmp; 2213 struct perf_event_context *ctx = event->ctx; 2214 2215 lockdep_assert_held(&ctx->lock); 2216 2217 /* 2218 * We can have double detach due to exit/hot-unplug + close. 2219 */ 2220 if (!(event->attach_state & PERF_ATTACH_GROUP)) 2221 return; 2222 2223 event->attach_state &= ~PERF_ATTACH_GROUP; 2224 2225 perf_put_aux_event(event); 2226 2227 /* 2228 * If this is a sibling, remove it from its group. 2229 */ 2230 if (leader != event) { 2231 list_del_init(&event->sibling_list); 2232 event->group_leader->nr_siblings--; 2233 event->group_leader->group_generation++; 2234 goto out; 2235 } 2236 2237 /* 2238 * If this was a group event with sibling events then 2239 * upgrade the siblings to singleton events by adding them 2240 * to whatever list we are on. 2241 */ 2242 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, sibling_list) { 2243 2244 if (sibling->event_caps & PERF_EV_CAP_SIBLING) 2245 perf_remove_sibling_event(sibling); 2246 2247 sibling->group_leader = sibling; 2248 list_del_init(&sibling->sibling_list); 2249 2250 /* Inherit group flags from the previous leader */ 2251 sibling->group_caps = event->group_caps; 2252 2253 if (sibling->attach_state & PERF_ATTACH_CONTEXT) { 2254 add_event_to_groups(sibling, event->ctx); 2255 2256 if (sibling->state == PERF_EVENT_STATE_ACTIVE) 2257 list_add_tail(&sibling->active_list, get_event_list(sibling)); 2258 } 2259 2260 WARN_ON_ONCE(sibling->ctx != event->ctx); 2261 } 2262 2263 out: 2264 for_each_sibling_event(tmp, leader) 2265 perf_event__header_size(tmp); 2266 2267 perf_event__header_size(leader); 2268 } 2269 2270 static void sync_child_event(struct perf_event *child_event); 2271 2272 static void perf_child_detach(struct perf_event *event) 2273 { 2274 struct perf_event *parent_event = event->parent; 2275 2276 if (!(event->attach_state & PERF_ATTACH_CHILD)) 2277 return; 2278 2279 event->attach_state &= ~PERF_ATTACH_CHILD; 2280 2281 if (WARN_ON_ONCE(!parent_event)) 2282 return; 2283 2284 lockdep_assert_held(&parent_event->child_mutex); 2285 2286 sync_child_event(event); 2287 list_del_init(&event->child_list); 2288 } 2289 2290 static bool is_orphaned_event(struct perf_event *event) 2291 { 2292 return event->state == PERF_EVENT_STATE_DEAD; 2293 } 2294 2295 static inline int 2296 event_filter_match(struct perf_event *event) 2297 { 2298 return (event->cpu == -1 || event->cpu == smp_processor_id()) && 2299 perf_cgroup_match(event); 2300 } 2301 2302 static void 2303 event_sched_out(struct perf_event *event, struct perf_event_context *ctx) 2304 { 2305 struct perf_event_pmu_context *epc = event->pmu_ctx; 2306 struct perf_cpu_pmu_context *cpc = this_cpu_ptr(epc->pmu->cpu_pmu_context); 2307 enum perf_event_state state = PERF_EVENT_STATE_INACTIVE; 2308 2309 // XXX cpc serialization, probably per-cpu IRQ disabled 2310 2311 WARN_ON_ONCE(event->ctx != ctx); 2312 lockdep_assert_held(&ctx->lock); 2313 2314 if (event->state != PERF_EVENT_STATE_ACTIVE) 2315 return; 2316 2317 /* 2318 * Asymmetry; we only schedule events _IN_ through ctx_sched_in(), but 2319 * we can schedule events _OUT_ individually through things like 2320 * __perf_remove_from_context(). 2321 */ 2322 list_del_init(&event->active_list); 2323 2324 perf_pmu_disable(event->pmu); 2325 2326 event->pmu->del(event, 0); 2327 event->oncpu = -1; 2328 2329 if (event->pending_disable) { 2330 event->pending_disable = 0; 2331 perf_cgroup_event_disable(event, ctx); 2332 state = PERF_EVENT_STATE_OFF; 2333 } 2334 2335 perf_event_set_state(event, state); 2336 2337 if (!is_software_event(event)) 2338 cpc->active_oncpu--; 2339 if (event->attr.freq && event->attr.sample_freq) { 2340 ctx->nr_freq--; 2341 epc->nr_freq--; 2342 } 2343 if (event->attr.exclusive || !cpc->active_oncpu) 2344 cpc->exclusive = 0; 2345 2346 perf_pmu_enable(event->pmu); 2347 } 2348 2349 static void 2350 group_sched_out(struct perf_event *group_event, struct perf_event_context *ctx) 2351 { 2352 struct perf_event *event; 2353 2354 if (group_event->state != PERF_EVENT_STATE_ACTIVE) 2355 return; 2356 2357 perf_assert_pmu_disabled(group_event->pmu_ctx->pmu); 2358 2359 event_sched_out(group_event, ctx); 2360 2361 /* 2362 * Schedule out siblings (if any): 2363 */ 2364 for_each_sibling_event(event, group_event) 2365 event_sched_out(event, ctx); 2366 } 2367 2368 static inline void 2369 __ctx_time_update(struct perf_cpu_context *cpuctx, struct perf_event_context *ctx, bool final) 2370 { 2371 if (ctx->is_active & EVENT_TIME) { 2372 if (ctx->is_active & EVENT_FROZEN) 2373 return; 2374 update_context_time(ctx); 2375 update_cgrp_time_from_cpuctx(cpuctx, final); 2376 } 2377 } 2378 2379 static inline void 2380 ctx_time_update(struct perf_cpu_context *cpuctx, struct perf_event_context *ctx) 2381 { 2382 __ctx_time_update(cpuctx, ctx, false); 2383 } 2384 2385 /* 2386 * To be used inside perf_ctx_lock() / perf_ctx_unlock(). Lasts until perf_ctx_unlock(). 2387 */ 2388 static inline void 2389 ctx_time_freeze(struct perf_cpu_context *cpuctx, struct perf_event_context *ctx) 2390 { 2391 ctx_time_update(cpuctx, ctx); 2392 if (ctx->is_active & EVENT_TIME) 2393 ctx->is_active |= EVENT_FROZEN; 2394 } 2395 2396 static inline void 2397 ctx_time_update_event(struct perf_event_context *ctx, struct perf_event *event) 2398 { 2399 if (ctx->is_active & EVENT_TIME) { 2400 if (ctx->is_active & EVENT_FROZEN) 2401 return; 2402 update_context_time(ctx); 2403 update_cgrp_time_from_event(event); 2404 } 2405 } 2406 2407 #define DETACH_GROUP 0x01UL 2408 #define DETACH_CHILD 0x02UL 2409 #define DETACH_DEAD 0x04UL 2410 2411 /* 2412 * Cross CPU call to remove a performance event 2413 * 2414 * We disable the event on the hardware level first. After that we 2415 * remove it from the context list. 2416 */ 2417 static void 2418 __perf_remove_from_context(struct perf_event *event, 2419 struct perf_cpu_context *cpuctx, 2420 struct perf_event_context *ctx, 2421 void *info) 2422 { 2423 struct perf_event_pmu_context *pmu_ctx = event->pmu_ctx; 2424 unsigned long flags = (unsigned long)info; 2425 2426 ctx_time_update(cpuctx, ctx); 2427 2428 /* 2429 * Ensure event_sched_out() switches to OFF, at the very least 2430 * this avoids raising perf_pending_task() at this time. 2431 */ 2432 if (flags & DETACH_DEAD) 2433 event->pending_disable = 1; 2434 event_sched_out(event, ctx); 2435 if (flags & DETACH_GROUP) 2436 perf_group_detach(event); 2437 if (flags & DETACH_CHILD) 2438 perf_child_detach(event); 2439 list_del_event(event, ctx); 2440 if (flags & DETACH_DEAD) 2441 event->state = PERF_EVENT_STATE_DEAD; 2442 2443 if (!pmu_ctx->nr_events) { 2444 pmu_ctx->rotate_necessary = 0; 2445 2446 if (ctx->task && ctx->is_active) { 2447 struct perf_cpu_pmu_context *cpc; 2448 2449 cpc = this_cpu_ptr(pmu_ctx->pmu->cpu_pmu_context); 2450 WARN_ON_ONCE(cpc->task_epc && cpc->task_epc != pmu_ctx); 2451 cpc->task_epc = NULL; 2452 } 2453 } 2454 2455 if (!ctx->nr_events && ctx->is_active) { 2456 if (ctx == &cpuctx->ctx) 2457 update_cgrp_time_from_cpuctx(cpuctx, true); 2458 2459 ctx->is_active = 0; 2460 if (ctx->task) { 2461 WARN_ON_ONCE(cpuctx->task_ctx != ctx); 2462 cpuctx->task_ctx = NULL; 2463 } 2464 } 2465 } 2466 2467 /* 2468 * Remove the event from a task's (or a CPU's) list of events. 2469 * 2470 * If event->ctx is a cloned context, callers must make sure that 2471 * every task struct that event->ctx->task could possibly point to 2472 * remains valid. This is OK when called from perf_release since 2473 * that only calls us on the top-level context, which can't be a clone. 2474 * When called from perf_event_exit_task, it's OK because the 2475 * context has been detached from its task. 2476 */ 2477 static void perf_remove_from_context(struct perf_event *event, unsigned long flags) 2478 { 2479 struct perf_event_context *ctx = event->ctx; 2480 2481 lockdep_assert_held(&ctx->mutex); 2482 2483 /* 2484 * Because of perf_event_exit_task(), perf_remove_from_context() ought 2485 * to work in the face of TASK_TOMBSTONE, unlike every other 2486 * event_function_call() user. 2487 */ 2488 raw_spin_lock_irq(&ctx->lock); 2489 if (!ctx->is_active) { 2490 __perf_remove_from_context(event, this_cpu_ptr(&perf_cpu_context), 2491 ctx, (void *)flags); 2492 raw_spin_unlock_irq(&ctx->lock); 2493 return; 2494 } 2495 raw_spin_unlock_irq(&ctx->lock); 2496 2497 event_function_call(event, __perf_remove_from_context, (void *)flags); 2498 } 2499 2500 /* 2501 * Cross CPU call to disable a performance event 2502 */ 2503 static void __perf_event_disable(struct perf_event *event, 2504 struct perf_cpu_context *cpuctx, 2505 struct perf_event_context *ctx, 2506 void *info) 2507 { 2508 if (event->state < PERF_EVENT_STATE_INACTIVE) 2509 return; 2510 2511 perf_pmu_disable(event->pmu_ctx->pmu); 2512 ctx_time_update_event(ctx, event); 2513 2514 if (event == event->group_leader) 2515 group_sched_out(event, ctx); 2516 else 2517 event_sched_out(event, ctx); 2518 2519 perf_event_set_state(event, PERF_EVENT_STATE_OFF); 2520 perf_cgroup_event_disable(event, ctx); 2521 2522 perf_pmu_enable(event->pmu_ctx->pmu); 2523 } 2524 2525 /* 2526 * Disable an event. 2527 * 2528 * If event->ctx is a cloned context, callers must make sure that 2529 * every task struct that event->ctx->task could possibly point to 2530 * remains valid. This condition is satisfied when called through 2531 * perf_event_for_each_child or perf_event_for_each because they 2532 * hold the top-level event's child_mutex, so any descendant that 2533 * goes to exit will block in perf_event_exit_event(). 2534 * 2535 * When called from perf_pending_disable it's OK because event->ctx 2536 * is the current context on this CPU and preemption is disabled, 2537 * hence we can't get into perf_event_task_sched_out for this context. 2538 */ 2539 static void _perf_event_disable(struct perf_event *event) 2540 { 2541 struct perf_event_context *ctx = event->ctx; 2542 2543 raw_spin_lock_irq(&ctx->lock); 2544 if (event->state <= PERF_EVENT_STATE_OFF) { 2545 raw_spin_unlock_irq(&ctx->lock); 2546 return; 2547 } 2548 raw_spin_unlock_irq(&ctx->lock); 2549 2550 event_function_call(event, __perf_event_disable, NULL); 2551 } 2552 2553 void perf_event_disable_local(struct perf_event *event) 2554 { 2555 event_function_local(event, __perf_event_disable, NULL); 2556 } 2557 2558 /* 2559 * Strictly speaking kernel users cannot create groups and therefore this 2560 * interface does not need the perf_event_ctx_lock() magic. 2561 */ 2562 void perf_event_disable(struct perf_event *event) 2563 { 2564 struct perf_event_context *ctx; 2565 2566 ctx = perf_event_ctx_lock(event); 2567 _perf_event_disable(event); 2568 perf_event_ctx_unlock(event, ctx); 2569 } 2570 EXPORT_SYMBOL_GPL(perf_event_disable); 2571 2572 void perf_event_disable_inatomic(struct perf_event *event) 2573 { 2574 event->pending_disable = 1; 2575 irq_work_queue(&event->pending_disable_irq); 2576 } 2577 2578 #define MAX_INTERRUPTS (~0ULL) 2579 2580 static void perf_log_throttle(struct perf_event *event, int enable); 2581 static void perf_log_itrace_start(struct perf_event *event); 2582 2583 static int 2584 event_sched_in(struct perf_event *event, struct perf_event_context *ctx) 2585 { 2586 struct perf_event_pmu_context *epc = event->pmu_ctx; 2587 struct perf_cpu_pmu_context *cpc = this_cpu_ptr(epc->pmu->cpu_pmu_context); 2588 int ret = 0; 2589 2590 WARN_ON_ONCE(event->ctx != ctx); 2591 2592 lockdep_assert_held(&ctx->lock); 2593 2594 if (event->state <= PERF_EVENT_STATE_OFF) 2595 return 0; 2596 2597 WRITE_ONCE(event->oncpu, smp_processor_id()); 2598 /* 2599 * Order event::oncpu write to happen before the ACTIVE state is 2600 * visible. This allows perf_event_{stop,read}() to observe the correct 2601 * ->oncpu if it sees ACTIVE. 2602 */ 2603 smp_wmb(); 2604 perf_event_set_state(event, PERF_EVENT_STATE_ACTIVE); 2605 2606 /* 2607 * Unthrottle events, since we scheduled we might have missed several 2608 * ticks already, also for a heavily scheduling task there is little 2609 * guarantee it'll get a tick in a timely manner. 2610 */ 2611 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { 2612 perf_log_throttle(event, 1); 2613 event->hw.interrupts = 0; 2614 } 2615 2616 perf_pmu_disable(event->pmu); 2617 2618 perf_log_itrace_start(event); 2619 2620 if (event->pmu->add(event, PERF_EF_START)) { 2621 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); 2622 event->oncpu = -1; 2623 ret = -EAGAIN; 2624 goto out; 2625 } 2626 2627 if (!is_software_event(event)) 2628 cpc->active_oncpu++; 2629 if (event->attr.freq && event->attr.sample_freq) { 2630 ctx->nr_freq++; 2631 epc->nr_freq++; 2632 } 2633 if (event->attr.exclusive) 2634 cpc->exclusive = 1; 2635 2636 out: 2637 perf_pmu_enable(event->pmu); 2638 2639 return ret; 2640 } 2641 2642 static int 2643 group_sched_in(struct perf_event *group_event, struct perf_event_context *ctx) 2644 { 2645 struct perf_event *event, *partial_group = NULL; 2646 struct pmu *pmu = group_event->pmu_ctx->pmu; 2647 2648 if (group_event->state == PERF_EVENT_STATE_OFF) 2649 return 0; 2650 2651 pmu->start_txn(pmu, PERF_PMU_TXN_ADD); 2652 2653 if (event_sched_in(group_event, ctx)) 2654 goto error; 2655 2656 /* 2657 * Schedule in siblings as one group (if any): 2658 */ 2659 for_each_sibling_event(event, group_event) { 2660 if (event_sched_in(event, ctx)) { 2661 partial_group = event; 2662 goto group_error; 2663 } 2664 } 2665 2666 if (!pmu->commit_txn(pmu)) 2667 return 0; 2668 2669 group_error: 2670 /* 2671 * Groups can be scheduled in as one unit only, so undo any 2672 * partial group before returning: 2673 * The events up to the failed event are scheduled out normally. 2674 */ 2675 for_each_sibling_event(event, group_event) { 2676 if (event == partial_group) 2677 break; 2678 2679 event_sched_out(event, ctx); 2680 } 2681 event_sched_out(group_event, ctx); 2682 2683 error: 2684 pmu->cancel_txn(pmu); 2685 return -EAGAIN; 2686 } 2687 2688 /* 2689 * Work out whether we can put this event group on the CPU now. 2690 */ 2691 static int group_can_go_on(struct perf_event *event, int can_add_hw) 2692 { 2693 struct perf_event_pmu_context *epc = event->pmu_ctx; 2694 struct perf_cpu_pmu_context *cpc = this_cpu_ptr(epc->pmu->cpu_pmu_context); 2695 2696 /* 2697 * Groups consisting entirely of software events can always go on. 2698 */ 2699 if (event->group_caps & PERF_EV_CAP_SOFTWARE) 2700 return 1; 2701 /* 2702 * If an exclusive group is already on, no other hardware 2703 * events can go on. 2704 */ 2705 if (cpc->exclusive) 2706 return 0; 2707 /* 2708 * If this group is exclusive and there are already 2709 * events on the CPU, it can't go on. 2710 */ 2711 if (event->attr.exclusive && !list_empty(get_event_list(event))) 2712 return 0; 2713 /* 2714 * Otherwise, try to add it if all previous groups were able 2715 * to go on. 2716 */ 2717 return can_add_hw; 2718 } 2719 2720 static void add_event_to_ctx(struct perf_event *event, 2721 struct perf_event_context *ctx) 2722 { 2723 list_add_event(event, ctx); 2724 perf_group_attach(event); 2725 } 2726 2727 static void task_ctx_sched_out(struct perf_event_context *ctx, 2728 struct pmu *pmu, 2729 enum event_type_t event_type) 2730 { 2731 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 2732 2733 if (!cpuctx->task_ctx) 2734 return; 2735 2736 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) 2737 return; 2738 2739 ctx_sched_out(ctx, pmu, event_type); 2740 } 2741 2742 static void perf_event_sched_in(struct perf_cpu_context *cpuctx, 2743 struct perf_event_context *ctx, 2744 struct pmu *pmu) 2745 { 2746 ctx_sched_in(&cpuctx->ctx, pmu, EVENT_PINNED); 2747 if (ctx) 2748 ctx_sched_in(ctx, pmu, EVENT_PINNED); 2749 ctx_sched_in(&cpuctx->ctx, pmu, EVENT_FLEXIBLE); 2750 if (ctx) 2751 ctx_sched_in(ctx, pmu, EVENT_FLEXIBLE); 2752 } 2753 2754 /* 2755 * We want to maintain the following priority of scheduling: 2756 * - CPU pinned (EVENT_CPU | EVENT_PINNED) 2757 * - task pinned (EVENT_PINNED) 2758 * - CPU flexible (EVENT_CPU | EVENT_FLEXIBLE) 2759 * - task flexible (EVENT_FLEXIBLE). 2760 * 2761 * In order to avoid unscheduling and scheduling back in everything every 2762 * time an event is added, only do it for the groups of equal priority and 2763 * below. 2764 * 2765 * This can be called after a batch operation on task events, in which case 2766 * event_type is a bit mask of the types of events involved. For CPU events, 2767 * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE. 2768 */ 2769 static void ctx_resched(struct perf_cpu_context *cpuctx, 2770 struct perf_event_context *task_ctx, 2771 struct pmu *pmu, enum event_type_t event_type) 2772 { 2773 bool cpu_event = !!(event_type & EVENT_CPU); 2774 struct perf_event_pmu_context *epc; 2775 2776 /* 2777 * If pinned groups are involved, flexible groups also need to be 2778 * scheduled out. 2779 */ 2780 if (event_type & EVENT_PINNED) 2781 event_type |= EVENT_FLEXIBLE; 2782 2783 event_type &= EVENT_ALL; 2784 2785 for_each_epc(epc, &cpuctx->ctx, pmu, false) 2786 perf_pmu_disable(epc->pmu); 2787 2788 if (task_ctx) { 2789 for_each_epc(epc, task_ctx, pmu, false) 2790 perf_pmu_disable(epc->pmu); 2791 2792 task_ctx_sched_out(task_ctx, pmu, event_type); 2793 } 2794 2795 /* 2796 * Decide which cpu ctx groups to schedule out based on the types 2797 * of events that caused rescheduling: 2798 * - EVENT_CPU: schedule out corresponding groups; 2799 * - EVENT_PINNED task events: schedule out EVENT_FLEXIBLE groups; 2800 * - otherwise, do nothing more. 2801 */ 2802 if (cpu_event) 2803 ctx_sched_out(&cpuctx->ctx, pmu, event_type); 2804 else if (event_type & EVENT_PINNED) 2805 ctx_sched_out(&cpuctx->ctx, pmu, EVENT_FLEXIBLE); 2806 2807 perf_event_sched_in(cpuctx, task_ctx, pmu); 2808 2809 for_each_epc(epc, &cpuctx->ctx, pmu, false) 2810 perf_pmu_enable(epc->pmu); 2811 2812 if (task_ctx) { 2813 for_each_epc(epc, task_ctx, pmu, false) 2814 perf_pmu_enable(epc->pmu); 2815 } 2816 } 2817 2818 void perf_pmu_resched(struct pmu *pmu) 2819 { 2820 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 2821 struct perf_event_context *task_ctx = cpuctx->task_ctx; 2822 2823 perf_ctx_lock(cpuctx, task_ctx); 2824 ctx_resched(cpuctx, task_ctx, pmu, EVENT_ALL|EVENT_CPU); 2825 perf_ctx_unlock(cpuctx, task_ctx); 2826 } 2827 2828 /* 2829 * Cross CPU call to install and enable a performance event 2830 * 2831 * Very similar to remote_function() + event_function() but cannot assume that 2832 * things like ctx->is_active and cpuctx->task_ctx are set. 2833 */ 2834 static int __perf_install_in_context(void *info) 2835 { 2836 struct perf_event *event = info; 2837 struct perf_event_context *ctx = event->ctx; 2838 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 2839 struct perf_event_context *task_ctx = cpuctx->task_ctx; 2840 bool reprogram = true; 2841 int ret = 0; 2842 2843 raw_spin_lock(&cpuctx->ctx.lock); 2844 if (ctx->task) { 2845 raw_spin_lock(&ctx->lock); 2846 task_ctx = ctx; 2847 2848 reprogram = (ctx->task == current); 2849 2850 /* 2851 * If the task is running, it must be running on this CPU, 2852 * otherwise we cannot reprogram things. 2853 * 2854 * If its not running, we don't care, ctx->lock will 2855 * serialize against it becoming runnable. 2856 */ 2857 if (task_curr(ctx->task) && !reprogram) { 2858 ret = -ESRCH; 2859 goto unlock; 2860 } 2861 2862 WARN_ON_ONCE(reprogram && cpuctx->task_ctx && cpuctx->task_ctx != ctx); 2863 } else if (task_ctx) { 2864 raw_spin_lock(&task_ctx->lock); 2865 } 2866 2867 #ifdef CONFIG_CGROUP_PERF 2868 if (event->state > PERF_EVENT_STATE_OFF && is_cgroup_event(event)) { 2869 /* 2870 * If the current cgroup doesn't match the event's 2871 * cgroup, we should not try to schedule it. 2872 */ 2873 struct perf_cgroup *cgrp = perf_cgroup_from_task(current, ctx); 2874 reprogram = cgroup_is_descendant(cgrp->css.cgroup, 2875 event->cgrp->css.cgroup); 2876 } 2877 #endif 2878 2879 if (reprogram) { 2880 ctx_time_freeze(cpuctx, ctx); 2881 add_event_to_ctx(event, ctx); 2882 ctx_resched(cpuctx, task_ctx, event->pmu_ctx->pmu, 2883 get_event_type(event)); 2884 } else { 2885 add_event_to_ctx(event, ctx); 2886 } 2887 2888 unlock: 2889 perf_ctx_unlock(cpuctx, task_ctx); 2890 2891 return ret; 2892 } 2893 2894 static bool exclusive_event_installable(struct perf_event *event, 2895 struct perf_event_context *ctx); 2896 2897 /* 2898 * Attach a performance event to a context. 2899 * 2900 * Very similar to event_function_call, see comment there. 2901 */ 2902 static void 2903 perf_install_in_context(struct perf_event_context *ctx, 2904 struct perf_event *event, 2905 int cpu) 2906 { 2907 struct task_struct *task = READ_ONCE(ctx->task); 2908 2909 lockdep_assert_held(&ctx->mutex); 2910 2911 WARN_ON_ONCE(!exclusive_event_installable(event, ctx)); 2912 2913 if (event->cpu != -1) 2914 WARN_ON_ONCE(event->cpu != cpu); 2915 2916 /* 2917 * Ensures that if we can observe event->ctx, both the event and ctx 2918 * will be 'complete'. See perf_iterate_sb_cpu(). 2919 */ 2920 smp_store_release(&event->ctx, ctx); 2921 2922 /* 2923 * perf_event_attr::disabled events will not run and can be initialized 2924 * without IPI. Except when this is the first event for the context, in 2925 * that case we need the magic of the IPI to set ctx->is_active. 2926 * 2927 * The IOC_ENABLE that is sure to follow the creation of a disabled 2928 * event will issue the IPI and reprogram the hardware. 2929 */ 2930 if (__perf_effective_state(event) == PERF_EVENT_STATE_OFF && 2931 ctx->nr_events && !is_cgroup_event(event)) { 2932 raw_spin_lock_irq(&ctx->lock); 2933 if (ctx->task == TASK_TOMBSTONE) { 2934 raw_spin_unlock_irq(&ctx->lock); 2935 return; 2936 } 2937 add_event_to_ctx(event, ctx); 2938 raw_spin_unlock_irq(&ctx->lock); 2939 return; 2940 } 2941 2942 if (!task) { 2943 cpu_function_call(cpu, __perf_install_in_context, event); 2944 return; 2945 } 2946 2947 /* 2948 * Should not happen, we validate the ctx is still alive before calling. 2949 */ 2950 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) 2951 return; 2952 2953 /* 2954 * Installing events is tricky because we cannot rely on ctx->is_active 2955 * to be set in case this is the nr_events 0 -> 1 transition. 2956 * 2957 * Instead we use task_curr(), which tells us if the task is running. 2958 * However, since we use task_curr() outside of rq::lock, we can race 2959 * against the actual state. This means the result can be wrong. 2960 * 2961 * If we get a false positive, we retry, this is harmless. 2962 * 2963 * If we get a false negative, things are complicated. If we are after 2964 * perf_event_context_sched_in() ctx::lock will serialize us, and the 2965 * value must be correct. If we're before, it doesn't matter since 2966 * perf_event_context_sched_in() will program the counter. 2967 * 2968 * However, this hinges on the remote context switch having observed 2969 * our task->perf_event_ctxp[] store, such that it will in fact take 2970 * ctx::lock in perf_event_context_sched_in(). 2971 * 2972 * We do this by task_function_call(), if the IPI fails to hit the task 2973 * we know any future context switch of task must see the 2974 * perf_event_ctpx[] store. 2975 */ 2976 2977 /* 2978 * This smp_mb() orders the task->perf_event_ctxp[] store with the 2979 * task_cpu() load, such that if the IPI then does not find the task 2980 * running, a future context switch of that task must observe the 2981 * store. 2982 */ 2983 smp_mb(); 2984 again: 2985 if (!task_function_call(task, __perf_install_in_context, event)) 2986 return; 2987 2988 raw_spin_lock_irq(&ctx->lock); 2989 task = ctx->task; 2990 if (WARN_ON_ONCE(task == TASK_TOMBSTONE)) { 2991 /* 2992 * Cannot happen because we already checked above (which also 2993 * cannot happen), and we hold ctx->mutex, which serializes us 2994 * against perf_event_exit_task_context(). 2995 */ 2996 raw_spin_unlock_irq(&ctx->lock); 2997 return; 2998 } 2999 /* 3000 * If the task is not running, ctx->lock will avoid it becoming so, 3001 * thus we can safely install the event. 3002 */ 3003 if (task_curr(task)) { 3004 raw_spin_unlock_irq(&ctx->lock); 3005 goto again; 3006 } 3007 add_event_to_ctx(event, ctx); 3008 raw_spin_unlock_irq(&ctx->lock); 3009 } 3010 3011 /* 3012 * Cross CPU call to enable a performance event 3013 */ 3014 static void __perf_event_enable(struct perf_event *event, 3015 struct perf_cpu_context *cpuctx, 3016 struct perf_event_context *ctx, 3017 void *info) 3018 { 3019 struct perf_event *leader = event->group_leader; 3020 struct perf_event_context *task_ctx; 3021 3022 if (event->state >= PERF_EVENT_STATE_INACTIVE || 3023 event->state <= PERF_EVENT_STATE_ERROR) 3024 return; 3025 3026 ctx_time_freeze(cpuctx, ctx); 3027 3028 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); 3029 perf_cgroup_event_enable(event, ctx); 3030 3031 if (!ctx->is_active) 3032 return; 3033 3034 if (!event_filter_match(event)) 3035 return; 3036 3037 /* 3038 * If the event is in a group and isn't the group leader, 3039 * then don't put it on unless the group is on. 3040 */ 3041 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) 3042 return; 3043 3044 task_ctx = cpuctx->task_ctx; 3045 if (ctx->task) 3046 WARN_ON_ONCE(task_ctx != ctx); 3047 3048 ctx_resched(cpuctx, task_ctx, event->pmu_ctx->pmu, get_event_type(event)); 3049 } 3050 3051 /* 3052 * Enable an event. 3053 * 3054 * If event->ctx is a cloned context, callers must make sure that 3055 * every task struct that event->ctx->task could possibly point to 3056 * remains valid. This condition is satisfied when called through 3057 * perf_event_for_each_child or perf_event_for_each as described 3058 * for perf_event_disable. 3059 */ 3060 static void _perf_event_enable(struct perf_event *event) 3061 { 3062 struct perf_event_context *ctx = event->ctx; 3063 3064 raw_spin_lock_irq(&ctx->lock); 3065 if (event->state >= PERF_EVENT_STATE_INACTIVE || 3066 event->state < PERF_EVENT_STATE_ERROR) { 3067 out: 3068 raw_spin_unlock_irq(&ctx->lock); 3069 return; 3070 } 3071 3072 /* 3073 * If the event is in error state, clear that first. 3074 * 3075 * That way, if we see the event in error state below, we know that it 3076 * has gone back into error state, as distinct from the task having 3077 * been scheduled away before the cross-call arrived. 3078 */ 3079 if (event->state == PERF_EVENT_STATE_ERROR) { 3080 /* 3081 * Detached SIBLING events cannot leave ERROR state. 3082 */ 3083 if (event->event_caps & PERF_EV_CAP_SIBLING && 3084 event->group_leader == event) 3085 goto out; 3086 3087 event->state = PERF_EVENT_STATE_OFF; 3088 } 3089 raw_spin_unlock_irq(&ctx->lock); 3090 3091 event_function_call(event, __perf_event_enable, NULL); 3092 } 3093 3094 /* 3095 * See perf_event_disable(); 3096 */ 3097 void perf_event_enable(struct perf_event *event) 3098 { 3099 struct perf_event_context *ctx; 3100 3101 ctx = perf_event_ctx_lock(event); 3102 _perf_event_enable(event); 3103 perf_event_ctx_unlock(event, ctx); 3104 } 3105 EXPORT_SYMBOL_GPL(perf_event_enable); 3106 3107 struct stop_event_data { 3108 struct perf_event *event; 3109 unsigned int restart; 3110 }; 3111 3112 static int __perf_event_stop(void *info) 3113 { 3114 struct stop_event_data *sd = info; 3115 struct perf_event *event = sd->event; 3116 3117 /* if it's already INACTIVE, do nothing */ 3118 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) 3119 return 0; 3120 3121 /* matches smp_wmb() in event_sched_in() */ 3122 smp_rmb(); 3123 3124 /* 3125 * There is a window with interrupts enabled before we get here, 3126 * so we need to check again lest we try to stop another CPU's event. 3127 */ 3128 if (READ_ONCE(event->oncpu) != smp_processor_id()) 3129 return -EAGAIN; 3130 3131 event->pmu->stop(event, PERF_EF_UPDATE); 3132 3133 /* 3134 * May race with the actual stop (through perf_pmu_output_stop()), 3135 * but it is only used for events with AUX ring buffer, and such 3136 * events will refuse to restart because of rb::aux_mmap_count==0, 3137 * see comments in perf_aux_output_begin(). 3138 * 3139 * Since this is happening on an event-local CPU, no trace is lost 3140 * while restarting. 3141 */ 3142 if (sd->restart) 3143 event->pmu->start(event, 0); 3144 3145 return 0; 3146 } 3147 3148 static int perf_event_stop(struct perf_event *event, int restart) 3149 { 3150 struct stop_event_data sd = { 3151 .event = event, 3152 .restart = restart, 3153 }; 3154 int ret = 0; 3155 3156 do { 3157 if (READ_ONCE(event->state) != PERF_EVENT_STATE_ACTIVE) 3158 return 0; 3159 3160 /* matches smp_wmb() in event_sched_in() */ 3161 smp_rmb(); 3162 3163 /* 3164 * We only want to restart ACTIVE events, so if the event goes 3165 * inactive here (event->oncpu==-1), there's nothing more to do; 3166 * fall through with ret==-ENXIO. 3167 */ 3168 ret = cpu_function_call(READ_ONCE(event->oncpu), 3169 __perf_event_stop, &sd); 3170 } while (ret == -EAGAIN); 3171 3172 return ret; 3173 } 3174 3175 /* 3176 * In order to contain the amount of racy and tricky in the address filter 3177 * configuration management, it is a two part process: 3178 * 3179 * (p1) when userspace mappings change as a result of (1) or (2) or (3) below, 3180 * we update the addresses of corresponding vmas in 3181 * event::addr_filter_ranges array and bump the event::addr_filters_gen; 3182 * (p2) when an event is scheduled in (pmu::add), it calls 3183 * perf_event_addr_filters_sync() which calls pmu::addr_filters_sync() 3184 * if the generation has changed since the previous call. 3185 * 3186 * If (p1) happens while the event is active, we restart it to force (p2). 3187 * 3188 * (1) perf_addr_filters_apply(): adjusting filters' offsets based on 3189 * pre-existing mappings, called once when new filters arrive via SET_FILTER 3190 * ioctl; 3191 * (2) perf_addr_filters_adjust(): adjusting filters' offsets based on newly 3192 * registered mapping, called for every new mmap(), with mm::mmap_lock down 3193 * for reading; 3194 * (3) perf_event_addr_filters_exec(): clearing filters' offsets in the process 3195 * of exec. 3196 */ 3197 void perf_event_addr_filters_sync(struct perf_event *event) 3198 { 3199 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); 3200 3201 if (!has_addr_filter(event)) 3202 return; 3203 3204 raw_spin_lock(&ifh->lock); 3205 if (event->addr_filters_gen != event->hw.addr_filters_gen) { 3206 event->pmu->addr_filters_sync(event); 3207 event->hw.addr_filters_gen = event->addr_filters_gen; 3208 } 3209 raw_spin_unlock(&ifh->lock); 3210 } 3211 EXPORT_SYMBOL_GPL(perf_event_addr_filters_sync); 3212 3213 static int _perf_event_refresh(struct perf_event *event, int refresh) 3214 { 3215 /* 3216 * not supported on inherited events 3217 */ 3218 if (event->attr.inherit || !is_sampling_event(event)) 3219 return -EINVAL; 3220 3221 atomic_add(refresh, &event->event_limit); 3222 _perf_event_enable(event); 3223 3224 return 0; 3225 } 3226 3227 /* 3228 * See perf_event_disable() 3229 */ 3230 int perf_event_refresh(struct perf_event *event, int refresh) 3231 { 3232 struct perf_event_context *ctx; 3233 int ret; 3234 3235 ctx = perf_event_ctx_lock(event); 3236 ret = _perf_event_refresh(event, refresh); 3237 perf_event_ctx_unlock(event, ctx); 3238 3239 return ret; 3240 } 3241 EXPORT_SYMBOL_GPL(perf_event_refresh); 3242 3243 static int perf_event_modify_breakpoint(struct perf_event *bp, 3244 struct perf_event_attr *attr) 3245 { 3246 int err; 3247 3248 _perf_event_disable(bp); 3249 3250 err = modify_user_hw_breakpoint_check(bp, attr, true); 3251 3252 if (!bp->attr.disabled) 3253 _perf_event_enable(bp); 3254 3255 return err; 3256 } 3257 3258 /* 3259 * Copy event-type-independent attributes that may be modified. 3260 */ 3261 static void perf_event_modify_copy_attr(struct perf_event_attr *to, 3262 const struct perf_event_attr *from) 3263 { 3264 to->sig_data = from->sig_data; 3265 } 3266 3267 static int perf_event_modify_attr(struct perf_event *event, 3268 struct perf_event_attr *attr) 3269 { 3270 int (*func)(struct perf_event *, struct perf_event_attr *); 3271 struct perf_event *child; 3272 int err; 3273 3274 if (event->attr.type != attr->type) 3275 return -EINVAL; 3276 3277 switch (event->attr.type) { 3278 case PERF_TYPE_BREAKPOINT: 3279 func = perf_event_modify_breakpoint; 3280 break; 3281 default: 3282 /* Place holder for future additions. */ 3283 return -EOPNOTSUPP; 3284 } 3285 3286 WARN_ON_ONCE(event->ctx->parent_ctx); 3287 3288 mutex_lock(&event->child_mutex); 3289 /* 3290 * Event-type-independent attributes must be copied before event-type 3291 * modification, which will validate that final attributes match the 3292 * source attributes after all relevant attributes have been copied. 3293 */ 3294 perf_event_modify_copy_attr(&event->attr, attr); 3295 err = func(event, attr); 3296 if (err) 3297 goto out; 3298 list_for_each_entry(child, &event->child_list, child_list) { 3299 perf_event_modify_copy_attr(&child->attr, attr); 3300 err = func(child, attr); 3301 if (err) 3302 goto out; 3303 } 3304 out: 3305 mutex_unlock(&event->child_mutex); 3306 return err; 3307 } 3308 3309 static void __pmu_ctx_sched_out(struct perf_event_pmu_context *pmu_ctx, 3310 enum event_type_t event_type) 3311 { 3312 struct perf_event_context *ctx = pmu_ctx->ctx; 3313 struct perf_event *event, *tmp; 3314 struct pmu *pmu = pmu_ctx->pmu; 3315 3316 if (ctx->task && !(ctx->is_active & EVENT_ALL)) { 3317 struct perf_cpu_pmu_context *cpc; 3318 3319 cpc = this_cpu_ptr(pmu->cpu_pmu_context); 3320 WARN_ON_ONCE(cpc->task_epc && cpc->task_epc != pmu_ctx); 3321 cpc->task_epc = NULL; 3322 } 3323 3324 if (!(event_type & EVENT_ALL)) 3325 return; 3326 3327 perf_pmu_disable(pmu); 3328 if (event_type & EVENT_PINNED) { 3329 list_for_each_entry_safe(event, tmp, 3330 &pmu_ctx->pinned_active, 3331 active_list) 3332 group_sched_out(event, ctx); 3333 } 3334 3335 if (event_type & EVENT_FLEXIBLE) { 3336 list_for_each_entry_safe(event, tmp, 3337 &pmu_ctx->flexible_active, 3338 active_list) 3339 group_sched_out(event, ctx); 3340 /* 3341 * Since we cleared EVENT_FLEXIBLE, also clear 3342 * rotate_necessary, is will be reset by 3343 * ctx_flexible_sched_in() when needed. 3344 */ 3345 pmu_ctx->rotate_necessary = 0; 3346 } 3347 perf_pmu_enable(pmu); 3348 } 3349 3350 /* 3351 * Be very careful with the @pmu argument since this will change ctx state. 3352 * The @pmu argument works for ctx_resched(), because that is symmetric in 3353 * ctx_sched_out() / ctx_sched_in() usage and the ctx state ends up invariant. 3354 * 3355 * However, if you were to be asymmetrical, you could end up with messed up 3356 * state, eg. ctx->is_active cleared even though most EPCs would still actually 3357 * be active. 3358 */ 3359 static void 3360 ctx_sched_out(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_t event_type) 3361 { 3362 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 3363 struct perf_event_pmu_context *pmu_ctx; 3364 int is_active = ctx->is_active; 3365 bool cgroup = event_type & EVENT_CGROUP; 3366 3367 event_type &= ~EVENT_CGROUP; 3368 3369 lockdep_assert_held(&ctx->lock); 3370 3371 if (likely(!ctx->nr_events)) { 3372 /* 3373 * See __perf_remove_from_context(). 3374 */ 3375 WARN_ON_ONCE(ctx->is_active); 3376 if (ctx->task) 3377 WARN_ON_ONCE(cpuctx->task_ctx); 3378 return; 3379 } 3380 3381 /* 3382 * Always update time if it was set; not only when it changes. 3383 * Otherwise we can 'forget' to update time for any but the last 3384 * context we sched out. For example: 3385 * 3386 * ctx_sched_out(.event_type = EVENT_FLEXIBLE) 3387 * ctx_sched_out(.event_type = EVENT_PINNED) 3388 * 3389 * would only update time for the pinned events. 3390 */ 3391 __ctx_time_update(cpuctx, ctx, ctx == &cpuctx->ctx); 3392 3393 /* 3394 * CPU-release for the below ->is_active store, 3395 * see __load_acquire() in perf_event_time_now() 3396 */ 3397 barrier(); 3398 ctx->is_active &= ~event_type; 3399 3400 if (!(ctx->is_active & EVENT_ALL)) { 3401 /* 3402 * For FROZEN, preserve TIME|FROZEN such that perf_event_time_now() 3403 * does not observe a hole. perf_ctx_unlock() will clean up. 3404 */ 3405 if (ctx->is_active & EVENT_FROZEN) 3406 ctx->is_active &= EVENT_TIME_FROZEN; 3407 else 3408 ctx->is_active = 0; 3409 } 3410 3411 if (ctx->task) { 3412 WARN_ON_ONCE(cpuctx->task_ctx != ctx); 3413 if (!(ctx->is_active & EVENT_ALL)) 3414 cpuctx->task_ctx = NULL; 3415 } 3416 3417 is_active ^= ctx->is_active; /* changed bits */ 3418 3419 for_each_epc(pmu_ctx, ctx, pmu, cgroup) 3420 __pmu_ctx_sched_out(pmu_ctx, is_active); 3421 } 3422 3423 /* 3424 * Test whether two contexts are equivalent, i.e. whether they have both been 3425 * cloned from the same version of the same context. 3426 * 3427 * Equivalence is measured using a generation number in the context that is 3428 * incremented on each modification to it; see unclone_ctx(), list_add_event() 3429 * and list_del_event(). 3430 */ 3431 static int context_equiv(struct perf_event_context *ctx1, 3432 struct perf_event_context *ctx2) 3433 { 3434 lockdep_assert_held(&ctx1->lock); 3435 lockdep_assert_held(&ctx2->lock); 3436 3437 /* Pinning disables the swap optimization */ 3438 if (ctx1->pin_count || ctx2->pin_count) 3439 return 0; 3440 3441 /* If ctx1 is the parent of ctx2 */ 3442 if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen) 3443 return 1; 3444 3445 /* If ctx2 is the parent of ctx1 */ 3446 if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation) 3447 return 1; 3448 3449 /* 3450 * If ctx1 and ctx2 have the same parent; we flatten the parent 3451 * hierarchy, see perf_event_init_context(). 3452 */ 3453 if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx && 3454 ctx1->parent_gen == ctx2->parent_gen) 3455 return 1; 3456 3457 /* Unmatched */ 3458 return 0; 3459 } 3460 3461 static void __perf_event_sync_stat(struct perf_event *event, 3462 struct perf_event *next_event) 3463 { 3464 u64 value; 3465 3466 if (!event->attr.inherit_stat) 3467 return; 3468 3469 /* 3470 * Update the event value, we cannot use perf_event_read() 3471 * because we're in the middle of a context switch and have IRQs 3472 * disabled, which upsets smp_call_function_single(), however 3473 * we know the event must be on the current CPU, therefore we 3474 * don't need to use it. 3475 */ 3476 if (event->state == PERF_EVENT_STATE_ACTIVE) 3477 event->pmu->read(event); 3478 3479 perf_event_update_time(event); 3480 3481 /* 3482 * In order to keep per-task stats reliable we need to flip the event 3483 * values when we flip the contexts. 3484 */ 3485 value = local64_read(&next_event->count); 3486 value = local64_xchg(&event->count, value); 3487 local64_set(&next_event->count, value); 3488 3489 swap(event->total_time_enabled, next_event->total_time_enabled); 3490 swap(event->total_time_running, next_event->total_time_running); 3491 3492 /* 3493 * Since we swizzled the values, update the user visible data too. 3494 */ 3495 perf_event_update_userpage(event); 3496 perf_event_update_userpage(next_event); 3497 } 3498 3499 static void perf_event_sync_stat(struct perf_event_context *ctx, 3500 struct perf_event_context *next_ctx) 3501 { 3502 struct perf_event *event, *next_event; 3503 3504 if (!ctx->nr_stat) 3505 return; 3506 3507 update_context_time(ctx); 3508 3509 event = list_first_entry(&ctx->event_list, 3510 struct perf_event, event_entry); 3511 3512 next_event = list_first_entry(&next_ctx->event_list, 3513 struct perf_event, event_entry); 3514 3515 while (&event->event_entry != &ctx->event_list && 3516 &next_event->event_entry != &next_ctx->event_list) { 3517 3518 __perf_event_sync_stat(event, next_event); 3519 3520 event = list_next_entry(event, event_entry); 3521 next_event = list_next_entry(next_event, event_entry); 3522 } 3523 } 3524 3525 #define double_list_for_each_entry(pos1, pos2, head1, head2, member) \ 3526 for (pos1 = list_first_entry(head1, typeof(*pos1), member), \ 3527 pos2 = list_first_entry(head2, typeof(*pos2), member); \ 3528 !list_entry_is_head(pos1, head1, member) && \ 3529 !list_entry_is_head(pos2, head2, member); \ 3530 pos1 = list_next_entry(pos1, member), \ 3531 pos2 = list_next_entry(pos2, member)) 3532 3533 static void perf_event_swap_task_ctx_data(struct perf_event_context *prev_ctx, 3534 struct perf_event_context *next_ctx) 3535 { 3536 struct perf_event_pmu_context *prev_epc, *next_epc; 3537 3538 if (!prev_ctx->nr_task_data) 3539 return; 3540 3541 double_list_for_each_entry(prev_epc, next_epc, 3542 &prev_ctx->pmu_ctx_list, &next_ctx->pmu_ctx_list, 3543 pmu_ctx_entry) { 3544 3545 if (WARN_ON_ONCE(prev_epc->pmu != next_epc->pmu)) 3546 continue; 3547 3548 /* 3549 * PMU specific parts of task perf context can require 3550 * additional synchronization. As an example of such 3551 * synchronization see implementation details of Intel 3552 * LBR call stack data profiling; 3553 */ 3554 if (prev_epc->pmu->swap_task_ctx) 3555 prev_epc->pmu->swap_task_ctx(prev_epc, next_epc); 3556 else 3557 swap(prev_epc->task_ctx_data, next_epc->task_ctx_data); 3558 } 3559 } 3560 3561 static void perf_ctx_sched_task_cb(struct perf_event_context *ctx, bool sched_in) 3562 { 3563 struct perf_event_pmu_context *pmu_ctx; 3564 struct perf_cpu_pmu_context *cpc; 3565 3566 list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { 3567 cpc = this_cpu_ptr(pmu_ctx->pmu->cpu_pmu_context); 3568 3569 if (cpc->sched_cb_usage && pmu_ctx->pmu->sched_task) 3570 pmu_ctx->pmu->sched_task(pmu_ctx, sched_in); 3571 } 3572 } 3573 3574 static void 3575 perf_event_context_sched_out(struct task_struct *task, struct task_struct *next) 3576 { 3577 struct perf_event_context *ctx = task->perf_event_ctxp; 3578 struct perf_event_context *next_ctx; 3579 struct perf_event_context *parent, *next_parent; 3580 int do_switch = 1; 3581 3582 if (likely(!ctx)) 3583 return; 3584 3585 rcu_read_lock(); 3586 next_ctx = rcu_dereference(next->perf_event_ctxp); 3587 if (!next_ctx) 3588 goto unlock; 3589 3590 parent = rcu_dereference(ctx->parent_ctx); 3591 next_parent = rcu_dereference(next_ctx->parent_ctx); 3592 3593 /* If neither context have a parent context; they cannot be clones. */ 3594 if (!parent && !next_parent) 3595 goto unlock; 3596 3597 if (next_parent == ctx || next_ctx == parent || next_parent == parent) { 3598 /* 3599 * Looks like the two contexts are clones, so we might be 3600 * able to optimize the context switch. We lock both 3601 * contexts and check that they are clones under the 3602 * lock (including re-checking that neither has been 3603 * uncloned in the meantime). It doesn't matter which 3604 * order we take the locks because no other cpu could 3605 * be trying to lock both of these tasks. 3606 */ 3607 raw_spin_lock(&ctx->lock); 3608 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); 3609 if (context_equiv(ctx, next_ctx)) { 3610 3611 perf_ctx_disable(ctx, false); 3612 3613 /* PMIs are disabled; ctx->nr_no_switch_fast is stable. */ 3614 if (local_read(&ctx->nr_no_switch_fast) || 3615 local_read(&next_ctx->nr_no_switch_fast)) { 3616 /* 3617 * Must not swap out ctx when there's pending 3618 * events that rely on the ctx->task relation. 3619 * 3620 * Likewise, when a context contains inherit + 3621 * SAMPLE_READ events they should be switched 3622 * out using the slow path so that they are 3623 * treated as if they were distinct contexts. 3624 */ 3625 raw_spin_unlock(&next_ctx->lock); 3626 rcu_read_unlock(); 3627 goto inside_switch; 3628 } 3629 3630 WRITE_ONCE(ctx->task, next); 3631 WRITE_ONCE(next_ctx->task, task); 3632 3633 perf_ctx_sched_task_cb(ctx, false); 3634 perf_event_swap_task_ctx_data(ctx, next_ctx); 3635 3636 perf_ctx_enable(ctx, false); 3637 3638 /* 3639 * RCU_INIT_POINTER here is safe because we've not 3640 * modified the ctx and the above modification of 3641 * ctx->task and ctx->task_ctx_data are immaterial 3642 * since those values are always verified under 3643 * ctx->lock which we're now holding. 3644 */ 3645 RCU_INIT_POINTER(task->perf_event_ctxp, next_ctx); 3646 RCU_INIT_POINTER(next->perf_event_ctxp, ctx); 3647 3648 do_switch = 0; 3649 3650 perf_event_sync_stat(ctx, next_ctx); 3651 } 3652 raw_spin_unlock(&next_ctx->lock); 3653 raw_spin_unlock(&ctx->lock); 3654 } 3655 unlock: 3656 rcu_read_unlock(); 3657 3658 if (do_switch) { 3659 raw_spin_lock(&ctx->lock); 3660 perf_ctx_disable(ctx, false); 3661 3662 inside_switch: 3663 perf_ctx_sched_task_cb(ctx, false); 3664 task_ctx_sched_out(ctx, NULL, EVENT_ALL); 3665 3666 perf_ctx_enable(ctx, false); 3667 raw_spin_unlock(&ctx->lock); 3668 } 3669 } 3670 3671 static DEFINE_PER_CPU(struct list_head, sched_cb_list); 3672 static DEFINE_PER_CPU(int, perf_sched_cb_usages); 3673 3674 void perf_sched_cb_dec(struct pmu *pmu) 3675 { 3676 struct perf_cpu_pmu_context *cpc = this_cpu_ptr(pmu->cpu_pmu_context); 3677 3678 this_cpu_dec(perf_sched_cb_usages); 3679 barrier(); 3680 3681 if (!--cpc->sched_cb_usage) 3682 list_del(&cpc->sched_cb_entry); 3683 } 3684 3685 3686 void perf_sched_cb_inc(struct pmu *pmu) 3687 { 3688 struct perf_cpu_pmu_context *cpc = this_cpu_ptr(pmu->cpu_pmu_context); 3689 3690 if (!cpc->sched_cb_usage++) 3691 list_add(&cpc->sched_cb_entry, this_cpu_ptr(&sched_cb_list)); 3692 3693 barrier(); 3694 this_cpu_inc(perf_sched_cb_usages); 3695 } 3696 3697 /* 3698 * This function provides the context switch callback to the lower code 3699 * layer. It is invoked ONLY when the context switch callback is enabled. 3700 * 3701 * This callback is relevant even to per-cpu events; for example multi event 3702 * PEBS requires this to provide PID/TID information. This requires we flush 3703 * all queued PEBS records before we context switch to a new task. 3704 */ 3705 static void __perf_pmu_sched_task(struct perf_cpu_pmu_context *cpc, bool sched_in) 3706 { 3707 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 3708 struct pmu *pmu; 3709 3710 pmu = cpc->epc.pmu; 3711 3712 /* software PMUs will not have sched_task */ 3713 if (WARN_ON_ONCE(!pmu->sched_task)) 3714 return; 3715 3716 perf_ctx_lock(cpuctx, cpuctx->task_ctx); 3717 perf_pmu_disable(pmu); 3718 3719 pmu->sched_task(cpc->task_epc, sched_in); 3720 3721 perf_pmu_enable(pmu); 3722 perf_ctx_unlock(cpuctx, cpuctx->task_ctx); 3723 } 3724 3725 static void perf_pmu_sched_task(struct task_struct *prev, 3726 struct task_struct *next, 3727 bool sched_in) 3728 { 3729 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 3730 struct perf_cpu_pmu_context *cpc; 3731 3732 /* cpuctx->task_ctx will be handled in perf_event_context_sched_in/out */ 3733 if (prev == next || cpuctx->task_ctx) 3734 return; 3735 3736 list_for_each_entry(cpc, this_cpu_ptr(&sched_cb_list), sched_cb_entry) 3737 __perf_pmu_sched_task(cpc, sched_in); 3738 } 3739 3740 static void perf_event_switch(struct task_struct *task, 3741 struct task_struct *next_prev, bool sched_in); 3742 3743 /* 3744 * Called from scheduler to remove the events of the current task, 3745 * with interrupts disabled. 3746 * 3747 * We stop each event and update the event value in event->count. 3748 * 3749 * This does not protect us against NMI, but disable() 3750 * sets the disabled bit in the control field of event _before_ 3751 * accessing the event control register. If a NMI hits, then it will 3752 * not restart the event. 3753 */ 3754 void __perf_event_task_sched_out(struct task_struct *task, 3755 struct task_struct *next) 3756 { 3757 if (__this_cpu_read(perf_sched_cb_usages)) 3758 perf_pmu_sched_task(task, next, false); 3759 3760 if (atomic_read(&nr_switch_events)) 3761 perf_event_switch(task, next, false); 3762 3763 perf_event_context_sched_out(task, next); 3764 3765 /* 3766 * if cgroup events exist on this CPU, then we need 3767 * to check if we have to switch out PMU state. 3768 * cgroup event are system-wide mode only 3769 */ 3770 perf_cgroup_switch(next); 3771 } 3772 3773 static bool perf_less_group_idx(const void *l, const void *r, void __always_unused *args) 3774 { 3775 const struct perf_event *le = *(const struct perf_event **)l; 3776 const struct perf_event *re = *(const struct perf_event **)r; 3777 3778 return le->group_index < re->group_index; 3779 } 3780 3781 static void swap_ptr(void *l, void *r, void __always_unused *args) 3782 { 3783 void **lp = l, **rp = r; 3784 3785 swap(*lp, *rp); 3786 } 3787 3788 DEFINE_MIN_HEAP(struct perf_event *, perf_event_min_heap); 3789 3790 static const struct min_heap_callbacks perf_min_heap = { 3791 .less = perf_less_group_idx, 3792 .swp = swap_ptr, 3793 }; 3794 3795 static void __heap_add(struct perf_event_min_heap *heap, struct perf_event *event) 3796 { 3797 struct perf_event **itrs = heap->data; 3798 3799 if (event) { 3800 itrs[heap->nr] = event; 3801 heap->nr++; 3802 } 3803 } 3804 3805 static void __link_epc(struct perf_event_pmu_context *pmu_ctx) 3806 { 3807 struct perf_cpu_pmu_context *cpc; 3808 3809 if (!pmu_ctx->ctx->task) 3810 return; 3811 3812 cpc = this_cpu_ptr(pmu_ctx->pmu->cpu_pmu_context); 3813 WARN_ON_ONCE(cpc->task_epc && cpc->task_epc != pmu_ctx); 3814 cpc->task_epc = pmu_ctx; 3815 } 3816 3817 static noinline int visit_groups_merge(struct perf_event_context *ctx, 3818 struct perf_event_groups *groups, int cpu, 3819 struct pmu *pmu, 3820 int (*func)(struct perf_event *, void *), 3821 void *data) 3822 { 3823 #ifdef CONFIG_CGROUP_PERF 3824 struct cgroup_subsys_state *css = NULL; 3825 #endif 3826 struct perf_cpu_context *cpuctx = NULL; 3827 /* Space for per CPU and/or any CPU event iterators. */ 3828 struct perf_event *itrs[2]; 3829 struct perf_event_min_heap event_heap; 3830 struct perf_event **evt; 3831 int ret; 3832 3833 if (pmu->filter && pmu->filter(pmu, cpu)) 3834 return 0; 3835 3836 if (!ctx->task) { 3837 cpuctx = this_cpu_ptr(&perf_cpu_context); 3838 event_heap = (struct perf_event_min_heap){ 3839 .data = cpuctx->heap, 3840 .nr = 0, 3841 .size = cpuctx->heap_size, 3842 }; 3843 3844 lockdep_assert_held(&cpuctx->ctx.lock); 3845 3846 #ifdef CONFIG_CGROUP_PERF 3847 if (cpuctx->cgrp) 3848 css = &cpuctx->cgrp->css; 3849 #endif 3850 } else { 3851 event_heap = (struct perf_event_min_heap){ 3852 .data = itrs, 3853 .nr = 0, 3854 .size = ARRAY_SIZE(itrs), 3855 }; 3856 /* Events not within a CPU context may be on any CPU. */ 3857 __heap_add(&event_heap, perf_event_groups_first(groups, -1, pmu, NULL)); 3858 } 3859 evt = event_heap.data; 3860 3861 __heap_add(&event_heap, perf_event_groups_first(groups, cpu, pmu, NULL)); 3862 3863 #ifdef CONFIG_CGROUP_PERF 3864 for (; css; css = css->parent) 3865 __heap_add(&event_heap, perf_event_groups_first(groups, cpu, pmu, css->cgroup)); 3866 #endif 3867 3868 if (event_heap.nr) { 3869 __link_epc((*evt)->pmu_ctx); 3870 perf_assert_pmu_disabled((*evt)->pmu_ctx->pmu); 3871 } 3872 3873 min_heapify_all(&event_heap, &perf_min_heap, NULL); 3874 3875 while (event_heap.nr) { 3876 ret = func(*evt, data); 3877 if (ret) 3878 return ret; 3879 3880 *evt = perf_event_groups_next(*evt, pmu); 3881 if (*evt) 3882 min_heap_sift_down(&event_heap, 0, &perf_min_heap, NULL); 3883 else 3884 min_heap_pop(&event_heap, &perf_min_heap, NULL); 3885 } 3886 3887 return 0; 3888 } 3889 3890 /* 3891 * Because the userpage is strictly per-event (there is no concept of context, 3892 * so there cannot be a context indirection), every userpage must be updated 3893 * when context time starts :-( 3894 * 3895 * IOW, we must not miss EVENT_TIME edges. 3896 */ 3897 static inline bool event_update_userpage(struct perf_event *event) 3898 { 3899 if (likely(!atomic_read(&event->mmap_count))) 3900 return false; 3901 3902 perf_event_update_time(event); 3903 perf_event_update_userpage(event); 3904 3905 return true; 3906 } 3907 3908 static inline void group_update_userpage(struct perf_event *group_event) 3909 { 3910 struct perf_event *event; 3911 3912 if (!event_update_userpage(group_event)) 3913 return; 3914 3915 for_each_sibling_event(event, group_event) 3916 event_update_userpage(event); 3917 } 3918 3919 static int merge_sched_in(struct perf_event *event, void *data) 3920 { 3921 struct perf_event_context *ctx = event->ctx; 3922 int *can_add_hw = data; 3923 3924 if (event->state <= PERF_EVENT_STATE_OFF) 3925 return 0; 3926 3927 if (!event_filter_match(event)) 3928 return 0; 3929 3930 if (group_can_go_on(event, *can_add_hw)) { 3931 if (!group_sched_in(event, ctx)) 3932 list_add_tail(&event->active_list, get_event_list(event)); 3933 } 3934 3935 if (event->state == PERF_EVENT_STATE_INACTIVE) { 3936 *can_add_hw = 0; 3937 if (event->attr.pinned) { 3938 perf_cgroup_event_disable(event, ctx); 3939 perf_event_set_state(event, PERF_EVENT_STATE_ERROR); 3940 } else { 3941 struct perf_cpu_pmu_context *cpc; 3942 3943 event->pmu_ctx->rotate_necessary = 1; 3944 cpc = this_cpu_ptr(event->pmu_ctx->pmu->cpu_pmu_context); 3945 perf_mux_hrtimer_restart(cpc); 3946 group_update_userpage(event); 3947 } 3948 } 3949 3950 return 0; 3951 } 3952 3953 static void pmu_groups_sched_in(struct perf_event_context *ctx, 3954 struct perf_event_groups *groups, 3955 struct pmu *pmu) 3956 { 3957 int can_add_hw = 1; 3958 visit_groups_merge(ctx, groups, smp_processor_id(), pmu, 3959 merge_sched_in, &can_add_hw); 3960 } 3961 3962 static void __pmu_ctx_sched_in(struct perf_event_pmu_context *pmu_ctx, 3963 enum event_type_t event_type) 3964 { 3965 struct perf_event_context *ctx = pmu_ctx->ctx; 3966 3967 if (event_type & EVENT_PINNED) 3968 pmu_groups_sched_in(ctx, &ctx->pinned_groups, pmu_ctx->pmu); 3969 if (event_type & EVENT_FLEXIBLE) 3970 pmu_groups_sched_in(ctx, &ctx->flexible_groups, pmu_ctx->pmu); 3971 } 3972 3973 static void 3974 ctx_sched_in(struct perf_event_context *ctx, struct pmu *pmu, enum event_type_t event_type) 3975 { 3976 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 3977 struct perf_event_pmu_context *pmu_ctx; 3978 int is_active = ctx->is_active; 3979 bool cgroup = event_type & EVENT_CGROUP; 3980 3981 event_type &= ~EVENT_CGROUP; 3982 3983 lockdep_assert_held(&ctx->lock); 3984 3985 if (likely(!ctx->nr_events)) 3986 return; 3987 3988 if (!(is_active & EVENT_TIME)) { 3989 /* start ctx time */ 3990 __update_context_time(ctx, false); 3991 perf_cgroup_set_timestamp(cpuctx); 3992 /* 3993 * CPU-release for the below ->is_active store, 3994 * see __load_acquire() in perf_event_time_now() 3995 */ 3996 barrier(); 3997 } 3998 3999 ctx->is_active |= (event_type | EVENT_TIME); 4000 if (ctx->task) { 4001 if (!(is_active & EVENT_ALL)) 4002 cpuctx->task_ctx = ctx; 4003 else 4004 WARN_ON_ONCE(cpuctx->task_ctx != ctx); 4005 } 4006 4007 is_active ^= ctx->is_active; /* changed bits */ 4008 4009 /* 4010 * First go through the list and put on any pinned groups 4011 * in order to give them the best chance of going on. 4012 */ 4013 if (is_active & EVENT_PINNED) { 4014 for_each_epc(pmu_ctx, ctx, pmu, cgroup) 4015 __pmu_ctx_sched_in(pmu_ctx, EVENT_PINNED); 4016 } 4017 4018 /* Then walk through the lower prio flexible groups */ 4019 if (is_active & EVENT_FLEXIBLE) { 4020 for_each_epc(pmu_ctx, ctx, pmu, cgroup) 4021 __pmu_ctx_sched_in(pmu_ctx, EVENT_FLEXIBLE); 4022 } 4023 } 4024 4025 static void perf_event_context_sched_in(struct task_struct *task) 4026 { 4027 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 4028 struct perf_event_context *ctx; 4029 4030 rcu_read_lock(); 4031 ctx = rcu_dereference(task->perf_event_ctxp); 4032 if (!ctx) 4033 goto rcu_unlock; 4034 4035 if (cpuctx->task_ctx == ctx) { 4036 perf_ctx_lock(cpuctx, ctx); 4037 perf_ctx_disable(ctx, false); 4038 4039 perf_ctx_sched_task_cb(ctx, true); 4040 4041 perf_ctx_enable(ctx, false); 4042 perf_ctx_unlock(cpuctx, ctx); 4043 goto rcu_unlock; 4044 } 4045 4046 perf_ctx_lock(cpuctx, ctx); 4047 /* 4048 * We must check ctx->nr_events while holding ctx->lock, such 4049 * that we serialize against perf_install_in_context(). 4050 */ 4051 if (!ctx->nr_events) 4052 goto unlock; 4053 4054 perf_ctx_disable(ctx, false); 4055 /* 4056 * We want to keep the following priority order: 4057 * cpu pinned (that don't need to move), task pinned, 4058 * cpu flexible, task flexible. 4059 * 4060 * However, if task's ctx is not carrying any pinned 4061 * events, no need to flip the cpuctx's events around. 4062 */ 4063 if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree)) { 4064 perf_ctx_disable(&cpuctx->ctx, false); 4065 ctx_sched_out(&cpuctx->ctx, NULL, EVENT_FLEXIBLE); 4066 } 4067 4068 perf_event_sched_in(cpuctx, ctx, NULL); 4069 4070 perf_ctx_sched_task_cb(cpuctx->task_ctx, true); 4071 4072 if (!RB_EMPTY_ROOT(&ctx->pinned_groups.tree)) 4073 perf_ctx_enable(&cpuctx->ctx, false); 4074 4075 perf_ctx_enable(ctx, false); 4076 4077 unlock: 4078 perf_ctx_unlock(cpuctx, ctx); 4079 rcu_unlock: 4080 rcu_read_unlock(); 4081 } 4082 4083 /* 4084 * Called from scheduler to add the events of the current task 4085 * with interrupts disabled. 4086 * 4087 * We restore the event value and then enable it. 4088 * 4089 * This does not protect us against NMI, but enable() 4090 * sets the enabled bit in the control field of event _before_ 4091 * accessing the event control register. If a NMI hits, then it will 4092 * keep the event running. 4093 */ 4094 void __perf_event_task_sched_in(struct task_struct *prev, 4095 struct task_struct *task) 4096 { 4097 perf_event_context_sched_in(task); 4098 4099 if (atomic_read(&nr_switch_events)) 4100 perf_event_switch(task, prev, true); 4101 4102 if (__this_cpu_read(perf_sched_cb_usages)) 4103 perf_pmu_sched_task(prev, task, true); 4104 } 4105 4106 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) 4107 { 4108 u64 frequency = event->attr.sample_freq; 4109 u64 sec = NSEC_PER_SEC; 4110 u64 divisor, dividend; 4111 4112 int count_fls, nsec_fls, frequency_fls, sec_fls; 4113 4114 count_fls = fls64(count); 4115 nsec_fls = fls64(nsec); 4116 frequency_fls = fls64(frequency); 4117 sec_fls = 30; 4118 4119 /* 4120 * We got @count in @nsec, with a target of sample_freq HZ 4121 * the target period becomes: 4122 * 4123 * @count * 10^9 4124 * period = ------------------- 4125 * @nsec * sample_freq 4126 * 4127 */ 4128 4129 /* 4130 * Reduce accuracy by one bit such that @a and @b converge 4131 * to a similar magnitude. 4132 */ 4133 #define REDUCE_FLS(a, b) \ 4134 do { \ 4135 if (a##_fls > b##_fls) { \ 4136 a >>= 1; \ 4137 a##_fls--; \ 4138 } else { \ 4139 b >>= 1; \ 4140 b##_fls--; \ 4141 } \ 4142 } while (0) 4143 4144 /* 4145 * Reduce accuracy until either term fits in a u64, then proceed with 4146 * the other, so that finally we can do a u64/u64 division. 4147 */ 4148 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { 4149 REDUCE_FLS(nsec, frequency); 4150 REDUCE_FLS(sec, count); 4151 } 4152 4153 if (count_fls + sec_fls > 64) { 4154 divisor = nsec * frequency; 4155 4156 while (count_fls + sec_fls > 64) { 4157 REDUCE_FLS(count, sec); 4158 divisor >>= 1; 4159 } 4160 4161 dividend = count * sec; 4162 } else { 4163 dividend = count * sec; 4164 4165 while (nsec_fls + frequency_fls > 64) { 4166 REDUCE_FLS(nsec, frequency); 4167 dividend >>= 1; 4168 } 4169 4170 divisor = nsec * frequency; 4171 } 4172 4173 if (!divisor) 4174 return dividend; 4175 4176 return div64_u64(dividend, divisor); 4177 } 4178 4179 static DEFINE_PER_CPU(int, perf_throttled_count); 4180 static DEFINE_PER_CPU(u64, perf_throttled_seq); 4181 4182 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable) 4183 { 4184 struct hw_perf_event *hwc = &event->hw; 4185 s64 period, sample_period; 4186 s64 delta; 4187 4188 period = perf_calculate_period(event, nsec, count); 4189 4190 delta = (s64)(period - hwc->sample_period); 4191 if (delta >= 0) 4192 delta += 7; 4193 else 4194 delta -= 7; 4195 delta /= 8; /* low pass filter */ 4196 4197 sample_period = hwc->sample_period + delta; 4198 4199 if (!sample_period) 4200 sample_period = 1; 4201 4202 hwc->sample_period = sample_period; 4203 4204 if (local64_read(&hwc->period_left) > 8*sample_period) { 4205 if (disable) 4206 event->pmu->stop(event, PERF_EF_UPDATE); 4207 4208 local64_set(&hwc->period_left, 0); 4209 4210 if (disable) 4211 event->pmu->start(event, PERF_EF_RELOAD); 4212 } 4213 } 4214 4215 static void perf_adjust_freq_unthr_events(struct list_head *event_list) 4216 { 4217 struct perf_event *event; 4218 struct hw_perf_event *hwc; 4219 u64 now, period = TICK_NSEC; 4220 s64 delta; 4221 4222 list_for_each_entry(event, event_list, active_list) { 4223 if (event->state != PERF_EVENT_STATE_ACTIVE) 4224 continue; 4225 4226 // XXX use visit thingy to avoid the -1,cpu match 4227 if (!event_filter_match(event)) 4228 continue; 4229 4230 hwc = &event->hw; 4231 4232 if (hwc->interrupts == MAX_INTERRUPTS) { 4233 hwc->interrupts = 0; 4234 perf_log_throttle(event, 1); 4235 if (!event->attr.freq || !event->attr.sample_freq) 4236 event->pmu->start(event, 0); 4237 } 4238 4239 if (!event->attr.freq || !event->attr.sample_freq) 4240 continue; 4241 4242 /* 4243 * stop the event and update event->count 4244 */ 4245 event->pmu->stop(event, PERF_EF_UPDATE); 4246 4247 now = local64_read(&event->count); 4248 delta = now - hwc->freq_count_stamp; 4249 hwc->freq_count_stamp = now; 4250 4251 /* 4252 * restart the event 4253 * reload only if value has changed 4254 * we have stopped the event so tell that 4255 * to perf_adjust_period() to avoid stopping it 4256 * twice. 4257 */ 4258 if (delta > 0) 4259 perf_adjust_period(event, period, delta, false); 4260 4261 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); 4262 } 4263 } 4264 4265 /* 4266 * combine freq adjustment with unthrottling to avoid two passes over the 4267 * events. At the same time, make sure, having freq events does not change 4268 * the rate of unthrottling as that would introduce bias. 4269 */ 4270 static void 4271 perf_adjust_freq_unthr_context(struct perf_event_context *ctx, bool unthrottle) 4272 { 4273 struct perf_event_pmu_context *pmu_ctx; 4274 4275 /* 4276 * only need to iterate over all events iff: 4277 * - context have events in frequency mode (needs freq adjust) 4278 * - there are events to unthrottle on this cpu 4279 */ 4280 if (!(ctx->nr_freq || unthrottle)) 4281 return; 4282 4283 raw_spin_lock(&ctx->lock); 4284 4285 list_for_each_entry(pmu_ctx, &ctx->pmu_ctx_list, pmu_ctx_entry) { 4286 if (!(pmu_ctx->nr_freq || unthrottle)) 4287 continue; 4288 if (!perf_pmu_ctx_is_active(pmu_ctx)) 4289 continue; 4290 if (pmu_ctx->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) 4291 continue; 4292 4293 perf_pmu_disable(pmu_ctx->pmu); 4294 perf_adjust_freq_unthr_events(&pmu_ctx->pinned_active); 4295 perf_adjust_freq_unthr_events(&pmu_ctx->flexible_active); 4296 perf_pmu_enable(pmu_ctx->pmu); 4297 } 4298 4299 raw_spin_unlock(&ctx->lock); 4300 } 4301 4302 /* 4303 * Move @event to the tail of the @ctx's elegible events. 4304 */ 4305 static void rotate_ctx(struct perf_event_context *ctx, struct perf_event *event) 4306 { 4307 /* 4308 * Rotate the first entry last of non-pinned groups. Rotation might be 4309 * disabled by the inheritance code. 4310 */ 4311 if (ctx->rotate_disable) 4312 return; 4313 4314 perf_event_groups_delete(&ctx->flexible_groups, event); 4315 perf_event_groups_insert(&ctx->flexible_groups, event); 4316 } 4317 4318 /* pick an event from the flexible_groups to rotate */ 4319 static inline struct perf_event * 4320 ctx_event_to_rotate(struct perf_event_pmu_context *pmu_ctx) 4321 { 4322 struct perf_event *event; 4323 struct rb_node *node; 4324 struct rb_root *tree; 4325 struct __group_key key = { 4326 .pmu = pmu_ctx->pmu, 4327 }; 4328 4329 /* pick the first active flexible event */ 4330 event = list_first_entry_or_null(&pmu_ctx->flexible_active, 4331 struct perf_event, active_list); 4332 if (event) 4333 goto out; 4334 4335 /* if no active flexible event, pick the first event */ 4336 tree = &pmu_ctx->ctx->flexible_groups.tree; 4337 4338 if (!pmu_ctx->ctx->task) { 4339 key.cpu = smp_processor_id(); 4340 4341 node = rb_find_first(&key, tree, __group_cmp_ignore_cgroup); 4342 if (node) 4343 event = __node_2_pe(node); 4344 goto out; 4345 } 4346 4347 key.cpu = -1; 4348 node = rb_find_first(&key, tree, __group_cmp_ignore_cgroup); 4349 if (node) { 4350 event = __node_2_pe(node); 4351 goto out; 4352 } 4353 4354 key.cpu = smp_processor_id(); 4355 node = rb_find_first(&key, tree, __group_cmp_ignore_cgroup); 4356 if (node) 4357 event = __node_2_pe(node); 4358 4359 out: 4360 /* 4361 * Unconditionally clear rotate_necessary; if ctx_flexible_sched_in() 4362 * finds there are unschedulable events, it will set it again. 4363 */ 4364 pmu_ctx->rotate_necessary = 0; 4365 4366 return event; 4367 } 4368 4369 static bool perf_rotate_context(struct perf_cpu_pmu_context *cpc) 4370 { 4371 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 4372 struct perf_event_pmu_context *cpu_epc, *task_epc = NULL; 4373 struct perf_event *cpu_event = NULL, *task_event = NULL; 4374 int cpu_rotate, task_rotate; 4375 struct pmu *pmu; 4376 4377 /* 4378 * Since we run this from IRQ context, nobody can install new 4379 * events, thus the event count values are stable. 4380 */ 4381 4382 cpu_epc = &cpc->epc; 4383 pmu = cpu_epc->pmu; 4384 task_epc = cpc->task_epc; 4385 4386 cpu_rotate = cpu_epc->rotate_necessary; 4387 task_rotate = task_epc ? task_epc->rotate_necessary : 0; 4388 4389 if (!(cpu_rotate || task_rotate)) 4390 return false; 4391 4392 perf_ctx_lock(cpuctx, cpuctx->task_ctx); 4393 perf_pmu_disable(pmu); 4394 4395 if (task_rotate) 4396 task_event = ctx_event_to_rotate(task_epc); 4397 if (cpu_rotate) 4398 cpu_event = ctx_event_to_rotate(cpu_epc); 4399 4400 /* 4401 * As per the order given at ctx_resched() first 'pop' task flexible 4402 * and then, if needed CPU flexible. 4403 */ 4404 if (task_event || (task_epc && cpu_event)) { 4405 update_context_time(task_epc->ctx); 4406 __pmu_ctx_sched_out(task_epc, EVENT_FLEXIBLE); 4407 } 4408 4409 if (cpu_event) { 4410 update_context_time(&cpuctx->ctx); 4411 __pmu_ctx_sched_out(cpu_epc, EVENT_FLEXIBLE); 4412 rotate_ctx(&cpuctx->ctx, cpu_event); 4413 __pmu_ctx_sched_in(cpu_epc, EVENT_FLEXIBLE); 4414 } 4415 4416 if (task_event) 4417 rotate_ctx(task_epc->ctx, task_event); 4418 4419 if (task_event || (task_epc && cpu_event)) 4420 __pmu_ctx_sched_in(task_epc, EVENT_FLEXIBLE); 4421 4422 perf_pmu_enable(pmu); 4423 perf_ctx_unlock(cpuctx, cpuctx->task_ctx); 4424 4425 return true; 4426 } 4427 4428 void perf_event_task_tick(void) 4429 { 4430 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 4431 struct perf_event_context *ctx; 4432 int throttled; 4433 4434 lockdep_assert_irqs_disabled(); 4435 4436 __this_cpu_inc(perf_throttled_seq); 4437 throttled = __this_cpu_xchg(perf_throttled_count, 0); 4438 tick_dep_clear_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); 4439 4440 perf_adjust_freq_unthr_context(&cpuctx->ctx, !!throttled); 4441 4442 rcu_read_lock(); 4443 ctx = rcu_dereference(current->perf_event_ctxp); 4444 if (ctx) 4445 perf_adjust_freq_unthr_context(ctx, !!throttled); 4446 rcu_read_unlock(); 4447 } 4448 4449 static int event_enable_on_exec(struct perf_event *event, 4450 struct perf_event_context *ctx) 4451 { 4452 if (!event->attr.enable_on_exec) 4453 return 0; 4454 4455 event->attr.enable_on_exec = 0; 4456 if (event->state >= PERF_EVENT_STATE_INACTIVE) 4457 return 0; 4458 4459 perf_event_set_state(event, PERF_EVENT_STATE_INACTIVE); 4460 4461 return 1; 4462 } 4463 4464 /* 4465 * Enable all of a task's events that have been marked enable-on-exec. 4466 * This expects task == current. 4467 */ 4468 static void perf_event_enable_on_exec(struct perf_event_context *ctx) 4469 { 4470 struct perf_event_context *clone_ctx = NULL; 4471 enum event_type_t event_type = 0; 4472 struct perf_cpu_context *cpuctx; 4473 struct perf_event *event; 4474 unsigned long flags; 4475 int enabled = 0; 4476 4477 local_irq_save(flags); 4478 if (WARN_ON_ONCE(current->perf_event_ctxp != ctx)) 4479 goto out; 4480 4481 if (!ctx->nr_events) 4482 goto out; 4483 4484 cpuctx = this_cpu_ptr(&perf_cpu_context); 4485 perf_ctx_lock(cpuctx, ctx); 4486 ctx_time_freeze(cpuctx, ctx); 4487 4488 list_for_each_entry(event, &ctx->event_list, event_entry) { 4489 enabled |= event_enable_on_exec(event, ctx); 4490 event_type |= get_event_type(event); 4491 } 4492 4493 /* 4494 * Unclone and reschedule this context if we enabled any event. 4495 */ 4496 if (enabled) { 4497 clone_ctx = unclone_ctx(ctx); 4498 ctx_resched(cpuctx, ctx, NULL, event_type); 4499 } 4500 perf_ctx_unlock(cpuctx, ctx); 4501 4502 out: 4503 local_irq_restore(flags); 4504 4505 if (clone_ctx) 4506 put_ctx(clone_ctx); 4507 } 4508 4509 static void perf_remove_from_owner(struct perf_event *event); 4510 static void perf_event_exit_event(struct perf_event *event, 4511 struct perf_event_context *ctx); 4512 4513 /* 4514 * Removes all events from the current task that have been marked 4515 * remove-on-exec, and feeds their values back to parent events. 4516 */ 4517 static void perf_event_remove_on_exec(struct perf_event_context *ctx) 4518 { 4519 struct perf_event_context *clone_ctx = NULL; 4520 struct perf_event *event, *next; 4521 unsigned long flags; 4522 bool modified = false; 4523 4524 mutex_lock(&ctx->mutex); 4525 4526 if (WARN_ON_ONCE(ctx->task != current)) 4527 goto unlock; 4528 4529 list_for_each_entry_safe(event, next, &ctx->event_list, event_entry) { 4530 if (!event->attr.remove_on_exec) 4531 continue; 4532 4533 if (!is_kernel_event(event)) 4534 perf_remove_from_owner(event); 4535 4536 modified = true; 4537 4538 perf_event_exit_event(event, ctx); 4539 } 4540 4541 raw_spin_lock_irqsave(&ctx->lock, flags); 4542 if (modified) 4543 clone_ctx = unclone_ctx(ctx); 4544 raw_spin_unlock_irqrestore(&ctx->lock, flags); 4545 4546 unlock: 4547 mutex_unlock(&ctx->mutex); 4548 4549 if (clone_ctx) 4550 put_ctx(clone_ctx); 4551 } 4552 4553 struct perf_read_data { 4554 struct perf_event *event; 4555 bool group; 4556 int ret; 4557 }; 4558 4559 static inline const struct cpumask *perf_scope_cpu_topology_cpumask(unsigned int scope, int cpu); 4560 4561 static int __perf_event_read_cpu(struct perf_event *event, int event_cpu) 4562 { 4563 int local_cpu = smp_processor_id(); 4564 u16 local_pkg, event_pkg; 4565 4566 if ((unsigned)event_cpu >= nr_cpu_ids) 4567 return event_cpu; 4568 4569 if (event->group_caps & PERF_EV_CAP_READ_SCOPE) { 4570 const struct cpumask *cpumask = perf_scope_cpu_topology_cpumask(event->pmu->scope, event_cpu); 4571 4572 if (cpumask && cpumask_test_cpu(local_cpu, cpumask)) 4573 return local_cpu; 4574 } 4575 4576 if (event->group_caps & PERF_EV_CAP_READ_ACTIVE_PKG) { 4577 event_pkg = topology_physical_package_id(event_cpu); 4578 local_pkg = topology_physical_package_id(local_cpu); 4579 4580 if (event_pkg == local_pkg) 4581 return local_cpu; 4582 } 4583 4584 return event_cpu; 4585 } 4586 4587 /* 4588 * Cross CPU call to read the hardware event 4589 */ 4590 static void __perf_event_read(void *info) 4591 { 4592 struct perf_read_data *data = info; 4593 struct perf_event *sub, *event = data->event; 4594 struct perf_event_context *ctx = event->ctx; 4595 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 4596 struct pmu *pmu = event->pmu; 4597 4598 /* 4599 * If this is a task context, we need to check whether it is 4600 * the current task context of this cpu. If not it has been 4601 * scheduled out before the smp call arrived. In that case 4602 * event->count would have been updated to a recent sample 4603 * when the event was scheduled out. 4604 */ 4605 if (ctx->task && cpuctx->task_ctx != ctx) 4606 return; 4607 4608 raw_spin_lock(&ctx->lock); 4609 ctx_time_update_event(ctx, event); 4610 4611 perf_event_update_time(event); 4612 if (data->group) 4613 perf_event_update_sibling_time(event); 4614 4615 if (event->state != PERF_EVENT_STATE_ACTIVE) 4616 goto unlock; 4617 4618 if (!data->group) { 4619 pmu->read(event); 4620 data->ret = 0; 4621 goto unlock; 4622 } 4623 4624 pmu->start_txn(pmu, PERF_PMU_TXN_READ); 4625 4626 pmu->read(event); 4627 4628 for_each_sibling_event(sub, event) { 4629 if (sub->state == PERF_EVENT_STATE_ACTIVE) { 4630 /* 4631 * Use sibling's PMU rather than @event's since 4632 * sibling could be on different (eg: software) PMU. 4633 */ 4634 sub->pmu->read(sub); 4635 } 4636 } 4637 4638 data->ret = pmu->commit_txn(pmu); 4639 4640 unlock: 4641 raw_spin_unlock(&ctx->lock); 4642 } 4643 4644 static inline u64 perf_event_count(struct perf_event *event, bool self) 4645 { 4646 if (self) 4647 return local64_read(&event->count); 4648 4649 return local64_read(&event->count) + atomic64_read(&event->child_count); 4650 } 4651 4652 static void calc_timer_values(struct perf_event *event, 4653 u64 *now, 4654 u64 *enabled, 4655 u64 *running) 4656 { 4657 u64 ctx_time; 4658 4659 *now = perf_clock(); 4660 ctx_time = perf_event_time_now(event, *now); 4661 __perf_update_times(event, ctx_time, enabled, running); 4662 } 4663 4664 /* 4665 * NMI-safe method to read a local event, that is an event that 4666 * is: 4667 * - either for the current task, or for this CPU 4668 * - does not have inherit set, for inherited task events 4669 * will not be local and we cannot read them atomically 4670 * - must not have a pmu::count method 4671 */ 4672 int perf_event_read_local(struct perf_event *event, u64 *value, 4673 u64 *enabled, u64 *running) 4674 { 4675 unsigned long flags; 4676 int event_oncpu; 4677 int event_cpu; 4678 int ret = 0; 4679 4680 /* 4681 * Disabling interrupts avoids all counter scheduling (context 4682 * switches, timer based rotation and IPIs). 4683 */ 4684 local_irq_save(flags); 4685 4686 /* 4687 * It must not be an event with inherit set, we cannot read 4688 * all child counters from atomic context. 4689 */ 4690 if (event->attr.inherit) { 4691 ret = -EOPNOTSUPP; 4692 goto out; 4693 } 4694 4695 /* If this is a per-task event, it must be for current */ 4696 if ((event->attach_state & PERF_ATTACH_TASK) && 4697 event->hw.target != current) { 4698 ret = -EINVAL; 4699 goto out; 4700 } 4701 4702 /* 4703 * Get the event CPU numbers, and adjust them to local if the event is 4704 * a per-package event that can be read locally 4705 */ 4706 event_oncpu = __perf_event_read_cpu(event, event->oncpu); 4707 event_cpu = __perf_event_read_cpu(event, event->cpu); 4708 4709 /* If this is a per-CPU event, it must be for this CPU */ 4710 if (!(event->attach_state & PERF_ATTACH_TASK) && 4711 event_cpu != smp_processor_id()) { 4712 ret = -EINVAL; 4713 goto out; 4714 } 4715 4716 /* If this is a pinned event it must be running on this CPU */ 4717 if (event->attr.pinned && event_oncpu != smp_processor_id()) { 4718 ret = -EBUSY; 4719 goto out; 4720 } 4721 4722 /* 4723 * If the event is currently on this CPU, its either a per-task event, 4724 * or local to this CPU. Furthermore it means its ACTIVE (otherwise 4725 * oncpu == -1). 4726 */ 4727 if (event_oncpu == smp_processor_id()) 4728 event->pmu->read(event); 4729 4730 *value = local64_read(&event->count); 4731 if (enabled || running) { 4732 u64 __enabled, __running, __now; 4733 4734 calc_timer_values(event, &__now, &__enabled, &__running); 4735 if (enabled) 4736 *enabled = __enabled; 4737 if (running) 4738 *running = __running; 4739 } 4740 out: 4741 local_irq_restore(flags); 4742 4743 return ret; 4744 } 4745 4746 static int perf_event_read(struct perf_event *event, bool group) 4747 { 4748 enum perf_event_state state = READ_ONCE(event->state); 4749 int event_cpu, ret = 0; 4750 4751 /* 4752 * If event is enabled and currently active on a CPU, update the 4753 * value in the event structure: 4754 */ 4755 again: 4756 if (state == PERF_EVENT_STATE_ACTIVE) { 4757 struct perf_read_data data; 4758 4759 /* 4760 * Orders the ->state and ->oncpu loads such that if we see 4761 * ACTIVE we must also see the right ->oncpu. 4762 * 4763 * Matches the smp_wmb() from event_sched_in(). 4764 */ 4765 smp_rmb(); 4766 4767 event_cpu = READ_ONCE(event->oncpu); 4768 if ((unsigned)event_cpu >= nr_cpu_ids) 4769 return 0; 4770 4771 data = (struct perf_read_data){ 4772 .event = event, 4773 .group = group, 4774 .ret = 0, 4775 }; 4776 4777 preempt_disable(); 4778 event_cpu = __perf_event_read_cpu(event, event_cpu); 4779 4780 /* 4781 * Purposely ignore the smp_call_function_single() return 4782 * value. 4783 * 4784 * If event_cpu isn't a valid CPU it means the event got 4785 * scheduled out and that will have updated the event count. 4786 * 4787 * Therefore, either way, we'll have an up-to-date event count 4788 * after this. 4789 */ 4790 (void)smp_call_function_single(event_cpu, __perf_event_read, &data, 1); 4791 preempt_enable(); 4792 ret = data.ret; 4793 4794 } else if (state == PERF_EVENT_STATE_INACTIVE) { 4795 struct perf_event_context *ctx = event->ctx; 4796 unsigned long flags; 4797 4798 raw_spin_lock_irqsave(&ctx->lock, flags); 4799 state = event->state; 4800 if (state != PERF_EVENT_STATE_INACTIVE) { 4801 raw_spin_unlock_irqrestore(&ctx->lock, flags); 4802 goto again; 4803 } 4804 4805 /* 4806 * May read while context is not active (e.g., thread is 4807 * blocked), in that case we cannot update context time 4808 */ 4809 ctx_time_update_event(ctx, event); 4810 4811 perf_event_update_time(event); 4812 if (group) 4813 perf_event_update_sibling_time(event); 4814 raw_spin_unlock_irqrestore(&ctx->lock, flags); 4815 } 4816 4817 return ret; 4818 } 4819 4820 /* 4821 * Initialize the perf_event context in a task_struct: 4822 */ 4823 static void __perf_event_init_context(struct perf_event_context *ctx) 4824 { 4825 raw_spin_lock_init(&ctx->lock); 4826 mutex_init(&ctx->mutex); 4827 INIT_LIST_HEAD(&ctx->pmu_ctx_list); 4828 perf_event_groups_init(&ctx->pinned_groups); 4829 perf_event_groups_init(&ctx->flexible_groups); 4830 INIT_LIST_HEAD(&ctx->event_list); 4831 refcount_set(&ctx->refcount, 1); 4832 } 4833 4834 static void 4835 __perf_init_event_pmu_context(struct perf_event_pmu_context *epc, struct pmu *pmu) 4836 { 4837 epc->pmu = pmu; 4838 INIT_LIST_HEAD(&epc->pmu_ctx_entry); 4839 INIT_LIST_HEAD(&epc->pinned_active); 4840 INIT_LIST_HEAD(&epc->flexible_active); 4841 atomic_set(&epc->refcount, 1); 4842 } 4843 4844 static struct perf_event_context * 4845 alloc_perf_context(struct task_struct *task) 4846 { 4847 struct perf_event_context *ctx; 4848 4849 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); 4850 if (!ctx) 4851 return NULL; 4852 4853 __perf_event_init_context(ctx); 4854 if (task) 4855 ctx->task = get_task_struct(task); 4856 4857 return ctx; 4858 } 4859 4860 static struct task_struct * 4861 find_lively_task_by_vpid(pid_t vpid) 4862 { 4863 struct task_struct *task; 4864 4865 rcu_read_lock(); 4866 if (!vpid) 4867 task = current; 4868 else 4869 task = find_task_by_vpid(vpid); 4870 if (task) 4871 get_task_struct(task); 4872 rcu_read_unlock(); 4873 4874 if (!task) 4875 return ERR_PTR(-ESRCH); 4876 4877 return task; 4878 } 4879 4880 /* 4881 * Returns a matching context with refcount and pincount. 4882 */ 4883 static struct perf_event_context * 4884 find_get_context(struct task_struct *task, struct perf_event *event) 4885 { 4886 struct perf_event_context *ctx, *clone_ctx = NULL; 4887 struct perf_cpu_context *cpuctx; 4888 unsigned long flags; 4889 int err; 4890 4891 if (!task) { 4892 /* Must be root to operate on a CPU event: */ 4893 err = perf_allow_cpu(&event->attr); 4894 if (err) 4895 return ERR_PTR(err); 4896 4897 cpuctx = per_cpu_ptr(&perf_cpu_context, event->cpu); 4898 ctx = &cpuctx->ctx; 4899 get_ctx(ctx); 4900 raw_spin_lock_irqsave(&ctx->lock, flags); 4901 ++ctx->pin_count; 4902 raw_spin_unlock_irqrestore(&ctx->lock, flags); 4903 4904 return ctx; 4905 } 4906 4907 err = -EINVAL; 4908 retry: 4909 ctx = perf_lock_task_context(task, &flags); 4910 if (ctx) { 4911 clone_ctx = unclone_ctx(ctx); 4912 ++ctx->pin_count; 4913 4914 raw_spin_unlock_irqrestore(&ctx->lock, flags); 4915 4916 if (clone_ctx) 4917 put_ctx(clone_ctx); 4918 } else { 4919 ctx = alloc_perf_context(task); 4920 err = -ENOMEM; 4921 if (!ctx) 4922 goto errout; 4923 4924 err = 0; 4925 mutex_lock(&task->perf_event_mutex); 4926 /* 4927 * If it has already passed perf_event_exit_task(). 4928 * we must see PF_EXITING, it takes this mutex too. 4929 */ 4930 if (task->flags & PF_EXITING) 4931 err = -ESRCH; 4932 else if (task->perf_event_ctxp) 4933 err = -EAGAIN; 4934 else { 4935 get_ctx(ctx); 4936 ++ctx->pin_count; 4937 rcu_assign_pointer(task->perf_event_ctxp, ctx); 4938 } 4939 mutex_unlock(&task->perf_event_mutex); 4940 4941 if (unlikely(err)) { 4942 put_ctx(ctx); 4943 4944 if (err == -EAGAIN) 4945 goto retry; 4946 goto errout; 4947 } 4948 } 4949 4950 return ctx; 4951 4952 errout: 4953 return ERR_PTR(err); 4954 } 4955 4956 static struct perf_event_pmu_context * 4957 find_get_pmu_context(struct pmu *pmu, struct perf_event_context *ctx, 4958 struct perf_event *event) 4959 { 4960 struct perf_event_pmu_context *new = NULL, *epc; 4961 void *task_ctx_data = NULL; 4962 4963 if (!ctx->task) { 4964 /* 4965 * perf_pmu_migrate_context() / __perf_pmu_install_event() 4966 * relies on the fact that find_get_pmu_context() cannot fail 4967 * for CPU contexts. 4968 */ 4969 struct perf_cpu_pmu_context *cpc; 4970 4971 cpc = per_cpu_ptr(pmu->cpu_pmu_context, event->cpu); 4972 epc = &cpc->epc; 4973 raw_spin_lock_irq(&ctx->lock); 4974 if (!epc->ctx) { 4975 atomic_set(&epc->refcount, 1); 4976 epc->embedded = 1; 4977 list_add(&epc->pmu_ctx_entry, &ctx->pmu_ctx_list); 4978 epc->ctx = ctx; 4979 } else { 4980 WARN_ON_ONCE(epc->ctx != ctx); 4981 atomic_inc(&epc->refcount); 4982 } 4983 raw_spin_unlock_irq(&ctx->lock); 4984 return epc; 4985 } 4986 4987 new = kzalloc(sizeof(*epc), GFP_KERNEL); 4988 if (!new) 4989 return ERR_PTR(-ENOMEM); 4990 4991 if (event->attach_state & PERF_ATTACH_TASK_DATA) { 4992 task_ctx_data = alloc_task_ctx_data(pmu); 4993 if (!task_ctx_data) { 4994 kfree(new); 4995 return ERR_PTR(-ENOMEM); 4996 } 4997 } 4998 4999 __perf_init_event_pmu_context(new, pmu); 5000 5001 /* 5002 * XXX 5003 * 5004 * lockdep_assert_held(&ctx->mutex); 5005 * 5006 * can't because perf_event_init_task() doesn't actually hold the 5007 * child_ctx->mutex. 5008 */ 5009 5010 raw_spin_lock_irq(&ctx->lock); 5011 list_for_each_entry(epc, &ctx->pmu_ctx_list, pmu_ctx_entry) { 5012 if (epc->pmu == pmu) { 5013 WARN_ON_ONCE(epc->ctx != ctx); 5014 atomic_inc(&epc->refcount); 5015 goto found_epc; 5016 } 5017 } 5018 5019 epc = new; 5020 new = NULL; 5021 5022 list_add(&epc->pmu_ctx_entry, &ctx->pmu_ctx_list); 5023 epc->ctx = ctx; 5024 5025 found_epc: 5026 if (task_ctx_data && !epc->task_ctx_data) { 5027 epc->task_ctx_data = task_ctx_data; 5028 task_ctx_data = NULL; 5029 ctx->nr_task_data++; 5030 } 5031 raw_spin_unlock_irq(&ctx->lock); 5032 5033 free_task_ctx_data(pmu, task_ctx_data); 5034 kfree(new); 5035 5036 return epc; 5037 } 5038 5039 static void get_pmu_ctx(struct perf_event_pmu_context *epc) 5040 { 5041 WARN_ON_ONCE(!atomic_inc_not_zero(&epc->refcount)); 5042 } 5043 5044 static void free_epc_rcu(struct rcu_head *head) 5045 { 5046 struct perf_event_pmu_context *epc = container_of(head, typeof(*epc), rcu_head); 5047 5048 kfree(epc->task_ctx_data); 5049 kfree(epc); 5050 } 5051 5052 static void put_pmu_ctx(struct perf_event_pmu_context *epc) 5053 { 5054 struct perf_event_context *ctx = epc->ctx; 5055 unsigned long flags; 5056 5057 /* 5058 * XXX 5059 * 5060 * lockdep_assert_held(&ctx->mutex); 5061 * 5062 * can't because of the call-site in _free_event()/put_event() 5063 * which isn't always called under ctx->mutex. 5064 */ 5065 if (!atomic_dec_and_raw_lock_irqsave(&epc->refcount, &ctx->lock, flags)) 5066 return; 5067 5068 WARN_ON_ONCE(list_empty(&epc->pmu_ctx_entry)); 5069 5070 list_del_init(&epc->pmu_ctx_entry); 5071 epc->ctx = NULL; 5072 5073 WARN_ON_ONCE(!list_empty(&epc->pinned_active)); 5074 WARN_ON_ONCE(!list_empty(&epc->flexible_active)); 5075 5076 raw_spin_unlock_irqrestore(&ctx->lock, flags); 5077 5078 if (epc->embedded) 5079 return; 5080 5081 call_rcu(&epc->rcu_head, free_epc_rcu); 5082 } 5083 5084 static void perf_event_free_filter(struct perf_event *event); 5085 5086 static void free_event_rcu(struct rcu_head *head) 5087 { 5088 struct perf_event *event = container_of(head, typeof(*event), rcu_head); 5089 5090 if (event->ns) 5091 put_pid_ns(event->ns); 5092 perf_event_free_filter(event); 5093 kmem_cache_free(perf_event_cache, event); 5094 } 5095 5096 static void ring_buffer_attach(struct perf_event *event, 5097 struct perf_buffer *rb); 5098 5099 static void detach_sb_event(struct perf_event *event) 5100 { 5101 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); 5102 5103 raw_spin_lock(&pel->lock); 5104 list_del_rcu(&event->sb_list); 5105 raw_spin_unlock(&pel->lock); 5106 } 5107 5108 static bool is_sb_event(struct perf_event *event) 5109 { 5110 struct perf_event_attr *attr = &event->attr; 5111 5112 if (event->parent) 5113 return false; 5114 5115 if (event->attach_state & PERF_ATTACH_TASK) 5116 return false; 5117 5118 if (attr->mmap || attr->mmap_data || attr->mmap2 || 5119 attr->comm || attr->comm_exec || 5120 attr->task || attr->ksymbol || 5121 attr->context_switch || attr->text_poke || 5122 attr->bpf_event) 5123 return true; 5124 return false; 5125 } 5126 5127 static void unaccount_pmu_sb_event(struct perf_event *event) 5128 { 5129 if (is_sb_event(event)) 5130 detach_sb_event(event); 5131 } 5132 5133 #ifdef CONFIG_NO_HZ_FULL 5134 static DEFINE_SPINLOCK(nr_freq_lock); 5135 #endif 5136 5137 static void unaccount_freq_event_nohz(void) 5138 { 5139 #ifdef CONFIG_NO_HZ_FULL 5140 spin_lock(&nr_freq_lock); 5141 if (atomic_dec_and_test(&nr_freq_events)) 5142 tick_nohz_dep_clear(TICK_DEP_BIT_PERF_EVENTS); 5143 spin_unlock(&nr_freq_lock); 5144 #endif 5145 } 5146 5147 static void unaccount_freq_event(void) 5148 { 5149 if (tick_nohz_full_enabled()) 5150 unaccount_freq_event_nohz(); 5151 else 5152 atomic_dec(&nr_freq_events); 5153 } 5154 5155 static void unaccount_event(struct perf_event *event) 5156 { 5157 bool dec = false; 5158 5159 if (event->parent) 5160 return; 5161 5162 if (event->attach_state & (PERF_ATTACH_TASK | PERF_ATTACH_SCHED_CB)) 5163 dec = true; 5164 if (event->attr.mmap || event->attr.mmap_data) 5165 atomic_dec(&nr_mmap_events); 5166 if (event->attr.build_id) 5167 atomic_dec(&nr_build_id_events); 5168 if (event->attr.comm) 5169 atomic_dec(&nr_comm_events); 5170 if (event->attr.namespaces) 5171 atomic_dec(&nr_namespaces_events); 5172 if (event->attr.cgroup) 5173 atomic_dec(&nr_cgroup_events); 5174 if (event->attr.task) 5175 atomic_dec(&nr_task_events); 5176 if (event->attr.freq) 5177 unaccount_freq_event(); 5178 if (event->attr.context_switch) { 5179 dec = true; 5180 atomic_dec(&nr_switch_events); 5181 } 5182 if (is_cgroup_event(event)) 5183 dec = true; 5184 if (has_branch_stack(event)) 5185 dec = true; 5186 if (event->attr.ksymbol) 5187 atomic_dec(&nr_ksymbol_events); 5188 if (event->attr.bpf_event) 5189 atomic_dec(&nr_bpf_events); 5190 if (event->attr.text_poke) 5191 atomic_dec(&nr_text_poke_events); 5192 5193 if (dec) { 5194 if (!atomic_add_unless(&perf_sched_count, -1, 1)) 5195 schedule_delayed_work(&perf_sched_work, HZ); 5196 } 5197 5198 unaccount_pmu_sb_event(event); 5199 } 5200 5201 static void perf_sched_delayed(struct work_struct *work) 5202 { 5203 mutex_lock(&perf_sched_mutex); 5204 if (atomic_dec_and_test(&perf_sched_count)) 5205 static_branch_disable(&perf_sched_events); 5206 mutex_unlock(&perf_sched_mutex); 5207 } 5208 5209 /* 5210 * The following implement mutual exclusion of events on "exclusive" pmus 5211 * (PERF_PMU_CAP_EXCLUSIVE). Such pmus can only have one event scheduled 5212 * at a time, so we disallow creating events that might conflict, namely: 5213 * 5214 * 1) cpu-wide events in the presence of per-task events, 5215 * 2) per-task events in the presence of cpu-wide events, 5216 * 3) two matching events on the same perf_event_context. 5217 * 5218 * The former two cases are handled in the allocation path (perf_event_alloc(), 5219 * _free_event()), the latter -- before the first perf_install_in_context(). 5220 */ 5221 static int exclusive_event_init(struct perf_event *event) 5222 { 5223 struct pmu *pmu = event->pmu; 5224 5225 if (!is_exclusive_pmu(pmu)) 5226 return 0; 5227 5228 /* 5229 * Prevent co-existence of per-task and cpu-wide events on the 5230 * same exclusive pmu. 5231 * 5232 * Negative pmu::exclusive_cnt means there are cpu-wide 5233 * events on this "exclusive" pmu, positive means there are 5234 * per-task events. 5235 * 5236 * Since this is called in perf_event_alloc() path, event::ctx 5237 * doesn't exist yet; it is, however, safe to use PERF_ATTACH_TASK 5238 * to mean "per-task event", because unlike other attach states it 5239 * never gets cleared. 5240 */ 5241 if (event->attach_state & PERF_ATTACH_TASK) { 5242 if (!atomic_inc_unless_negative(&pmu->exclusive_cnt)) 5243 return -EBUSY; 5244 } else { 5245 if (!atomic_dec_unless_positive(&pmu->exclusive_cnt)) 5246 return -EBUSY; 5247 } 5248 5249 return 0; 5250 } 5251 5252 static void exclusive_event_destroy(struct perf_event *event) 5253 { 5254 struct pmu *pmu = event->pmu; 5255 5256 if (!is_exclusive_pmu(pmu)) 5257 return; 5258 5259 /* see comment in exclusive_event_init() */ 5260 if (event->attach_state & PERF_ATTACH_TASK) 5261 atomic_dec(&pmu->exclusive_cnt); 5262 else 5263 atomic_inc(&pmu->exclusive_cnt); 5264 } 5265 5266 static bool exclusive_event_match(struct perf_event *e1, struct perf_event *e2) 5267 { 5268 if ((e1->pmu == e2->pmu) && 5269 (e1->cpu == e2->cpu || 5270 e1->cpu == -1 || 5271 e2->cpu == -1)) 5272 return true; 5273 return false; 5274 } 5275 5276 static bool exclusive_event_installable(struct perf_event *event, 5277 struct perf_event_context *ctx) 5278 { 5279 struct perf_event *iter_event; 5280 struct pmu *pmu = event->pmu; 5281 5282 lockdep_assert_held(&ctx->mutex); 5283 5284 if (!is_exclusive_pmu(pmu)) 5285 return true; 5286 5287 list_for_each_entry(iter_event, &ctx->event_list, event_entry) { 5288 if (exclusive_event_match(iter_event, event)) 5289 return false; 5290 } 5291 5292 return true; 5293 } 5294 5295 static void perf_addr_filters_splice(struct perf_event *event, 5296 struct list_head *head); 5297 5298 static void perf_pending_task_sync(struct perf_event *event) 5299 { 5300 struct callback_head *head = &event->pending_task; 5301 5302 if (!event->pending_work) 5303 return; 5304 /* 5305 * If the task is queued to the current task's queue, we 5306 * obviously can't wait for it to complete. Simply cancel it. 5307 */ 5308 if (task_work_cancel(current, head)) { 5309 event->pending_work = 0; 5310 local_dec(&event->ctx->nr_no_switch_fast); 5311 return; 5312 } 5313 5314 /* 5315 * All accesses related to the event are within the same RCU section in 5316 * perf_pending_task(). The RCU grace period before the event is freed 5317 * will make sure all those accesses are complete by then. 5318 */ 5319 rcuwait_wait_event(&event->pending_work_wait, !event->pending_work, TASK_UNINTERRUPTIBLE); 5320 } 5321 5322 static void _free_event(struct perf_event *event) 5323 { 5324 irq_work_sync(&event->pending_irq); 5325 irq_work_sync(&event->pending_disable_irq); 5326 perf_pending_task_sync(event); 5327 5328 unaccount_event(event); 5329 5330 security_perf_event_free(event); 5331 5332 if (event->rb) { 5333 /* 5334 * Can happen when we close an event with re-directed output. 5335 * 5336 * Since we have a 0 refcount, perf_mmap_close() will skip 5337 * over us; possibly making our ring_buffer_put() the last. 5338 */ 5339 mutex_lock(&event->mmap_mutex); 5340 ring_buffer_attach(event, NULL); 5341 mutex_unlock(&event->mmap_mutex); 5342 } 5343 5344 if (is_cgroup_event(event)) 5345 perf_detach_cgroup(event); 5346 5347 if (!event->parent) { 5348 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) 5349 put_callchain_buffers(); 5350 } 5351 5352 perf_event_free_bpf_prog(event); 5353 perf_addr_filters_splice(event, NULL); 5354 kfree(event->addr_filter_ranges); 5355 5356 if (event->destroy) 5357 event->destroy(event); 5358 5359 /* 5360 * Must be after ->destroy(), due to uprobe_perf_close() using 5361 * hw.target. 5362 */ 5363 if (event->hw.target) 5364 put_task_struct(event->hw.target); 5365 5366 if (event->pmu_ctx) 5367 put_pmu_ctx(event->pmu_ctx); 5368 5369 /* 5370 * perf_event_free_task() relies on put_ctx() being 'last', in particular 5371 * all task references must be cleaned up. 5372 */ 5373 if (event->ctx) 5374 put_ctx(event->ctx); 5375 5376 exclusive_event_destroy(event); 5377 module_put(event->pmu->module); 5378 5379 call_rcu(&event->rcu_head, free_event_rcu); 5380 } 5381 5382 /* 5383 * Used to free events which have a known refcount of 1, such as in error paths 5384 * where the event isn't exposed yet and inherited events. 5385 */ 5386 static void free_event(struct perf_event *event) 5387 { 5388 if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1, 5389 "unexpected event refcount: %ld; ptr=%p\n", 5390 atomic_long_read(&event->refcount), event)) { 5391 /* leak to avoid use-after-free */ 5392 return; 5393 } 5394 5395 _free_event(event); 5396 } 5397 5398 /* 5399 * Remove user event from the owner task. 5400 */ 5401 static void perf_remove_from_owner(struct perf_event *event) 5402 { 5403 struct task_struct *owner; 5404 5405 rcu_read_lock(); 5406 /* 5407 * Matches the smp_store_release() in perf_event_exit_task(). If we 5408 * observe !owner it means the list deletion is complete and we can 5409 * indeed free this event, otherwise we need to serialize on 5410 * owner->perf_event_mutex. 5411 */ 5412 owner = READ_ONCE(event->owner); 5413 if (owner) { 5414 /* 5415 * Since delayed_put_task_struct() also drops the last 5416 * task reference we can safely take a new reference 5417 * while holding the rcu_read_lock(). 5418 */ 5419 get_task_struct(owner); 5420 } 5421 rcu_read_unlock(); 5422 5423 if (owner) { 5424 /* 5425 * If we're here through perf_event_exit_task() we're already 5426 * holding ctx->mutex which would be an inversion wrt. the 5427 * normal lock order. 5428 * 5429 * However we can safely take this lock because its the child 5430 * ctx->mutex. 5431 */ 5432 mutex_lock_nested(&owner->perf_event_mutex, SINGLE_DEPTH_NESTING); 5433 5434 /* 5435 * We have to re-check the event->owner field, if it is cleared 5436 * we raced with perf_event_exit_task(), acquiring the mutex 5437 * ensured they're done, and we can proceed with freeing the 5438 * event. 5439 */ 5440 if (event->owner) { 5441 list_del_init(&event->owner_entry); 5442 smp_store_release(&event->owner, NULL); 5443 } 5444 mutex_unlock(&owner->perf_event_mutex); 5445 put_task_struct(owner); 5446 } 5447 } 5448 5449 static void put_event(struct perf_event *event) 5450 { 5451 if (!atomic_long_dec_and_test(&event->refcount)) 5452 return; 5453 5454 _free_event(event); 5455 } 5456 5457 /* 5458 * Kill an event dead; while event:refcount will preserve the event 5459 * object, it will not preserve its functionality. Once the last 'user' 5460 * gives up the object, we'll destroy the thing. 5461 */ 5462 int perf_event_release_kernel(struct perf_event *event) 5463 { 5464 struct perf_event_context *ctx = event->ctx; 5465 struct perf_event *child, *tmp; 5466 LIST_HEAD(free_list); 5467 5468 /* 5469 * If we got here through err_alloc: free_event(event); we will not 5470 * have attached to a context yet. 5471 */ 5472 if (!ctx) { 5473 WARN_ON_ONCE(event->attach_state & 5474 (PERF_ATTACH_CONTEXT|PERF_ATTACH_GROUP)); 5475 goto no_ctx; 5476 } 5477 5478 if (!is_kernel_event(event)) 5479 perf_remove_from_owner(event); 5480 5481 ctx = perf_event_ctx_lock(event); 5482 WARN_ON_ONCE(ctx->parent_ctx); 5483 5484 /* 5485 * Mark this event as STATE_DEAD, there is no external reference to it 5486 * anymore. 5487 * 5488 * Anybody acquiring event->child_mutex after the below loop _must_ 5489 * also see this, most importantly inherit_event() which will avoid 5490 * placing more children on the list. 5491 * 5492 * Thus this guarantees that we will in fact observe and kill _ALL_ 5493 * child events. 5494 */ 5495 perf_remove_from_context(event, DETACH_GROUP|DETACH_DEAD); 5496 5497 perf_event_ctx_unlock(event, ctx); 5498 5499 again: 5500 mutex_lock(&event->child_mutex); 5501 list_for_each_entry(child, &event->child_list, child_list) { 5502 void *var = NULL; 5503 5504 /* 5505 * Cannot change, child events are not migrated, see the 5506 * comment with perf_event_ctx_lock_nested(). 5507 */ 5508 ctx = READ_ONCE(child->ctx); 5509 /* 5510 * Since child_mutex nests inside ctx::mutex, we must jump 5511 * through hoops. We start by grabbing a reference on the ctx. 5512 * 5513 * Since the event cannot get freed while we hold the 5514 * child_mutex, the context must also exist and have a !0 5515 * reference count. 5516 */ 5517 get_ctx(ctx); 5518 5519 /* 5520 * Now that we have a ctx ref, we can drop child_mutex, and 5521 * acquire ctx::mutex without fear of it going away. Then we 5522 * can re-acquire child_mutex. 5523 */ 5524 mutex_unlock(&event->child_mutex); 5525 mutex_lock(&ctx->mutex); 5526 mutex_lock(&event->child_mutex); 5527 5528 /* 5529 * Now that we hold ctx::mutex and child_mutex, revalidate our 5530 * state, if child is still the first entry, it didn't get freed 5531 * and we can continue doing so. 5532 */ 5533 tmp = list_first_entry_or_null(&event->child_list, 5534 struct perf_event, child_list); 5535 if (tmp == child) { 5536 perf_remove_from_context(child, DETACH_GROUP); 5537 list_move(&child->child_list, &free_list); 5538 /* 5539 * This matches the refcount bump in inherit_event(); 5540 * this can't be the last reference. 5541 */ 5542 put_event(event); 5543 } else { 5544 var = &ctx->refcount; 5545 } 5546 5547 mutex_unlock(&event->child_mutex); 5548 mutex_unlock(&ctx->mutex); 5549 put_ctx(ctx); 5550 5551 if (var) { 5552 /* 5553 * If perf_event_free_task() has deleted all events from the 5554 * ctx while the child_mutex got released above, make sure to 5555 * notify about the preceding put_ctx(). 5556 */ 5557 smp_mb(); /* pairs with wait_var_event() */ 5558 wake_up_var(var); 5559 } 5560 goto again; 5561 } 5562 mutex_unlock(&event->child_mutex); 5563 5564 list_for_each_entry_safe(child, tmp, &free_list, child_list) { 5565 void *var = &child->ctx->refcount; 5566 5567 list_del(&child->child_list); 5568 free_event(child); 5569 5570 /* 5571 * Wake any perf_event_free_task() waiting for this event to be 5572 * freed. 5573 */ 5574 smp_mb(); /* pairs with wait_var_event() */ 5575 wake_up_var(var); 5576 } 5577 5578 no_ctx: 5579 put_event(event); /* Must be the 'last' reference */ 5580 return 0; 5581 } 5582 EXPORT_SYMBOL_GPL(perf_event_release_kernel); 5583 5584 /* 5585 * Called when the last reference to the file is gone. 5586 */ 5587 static int perf_release(struct inode *inode, struct file *file) 5588 { 5589 perf_event_release_kernel(file->private_data); 5590 return 0; 5591 } 5592 5593 static u64 __perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) 5594 { 5595 struct perf_event *child; 5596 u64 total = 0; 5597 5598 *enabled = 0; 5599 *running = 0; 5600 5601 mutex_lock(&event->child_mutex); 5602 5603 (void)perf_event_read(event, false); 5604 total += perf_event_count(event, false); 5605 5606 *enabled += event->total_time_enabled + 5607 atomic64_read(&event->child_total_time_enabled); 5608 *running += event->total_time_running + 5609 atomic64_read(&event->child_total_time_running); 5610 5611 list_for_each_entry(child, &event->child_list, child_list) { 5612 (void)perf_event_read(child, false); 5613 total += perf_event_count(child, false); 5614 *enabled += child->total_time_enabled; 5615 *running += child->total_time_running; 5616 } 5617 mutex_unlock(&event->child_mutex); 5618 5619 return total; 5620 } 5621 5622 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) 5623 { 5624 struct perf_event_context *ctx; 5625 u64 count; 5626 5627 ctx = perf_event_ctx_lock(event); 5628 count = __perf_event_read_value(event, enabled, running); 5629 perf_event_ctx_unlock(event, ctx); 5630 5631 return count; 5632 } 5633 EXPORT_SYMBOL_GPL(perf_event_read_value); 5634 5635 static int __perf_read_group_add(struct perf_event *leader, 5636 u64 read_format, u64 *values) 5637 { 5638 struct perf_event_context *ctx = leader->ctx; 5639 struct perf_event *sub, *parent; 5640 unsigned long flags; 5641 int n = 1; /* skip @nr */ 5642 int ret; 5643 5644 ret = perf_event_read(leader, true); 5645 if (ret) 5646 return ret; 5647 5648 raw_spin_lock_irqsave(&ctx->lock, flags); 5649 /* 5650 * Verify the grouping between the parent and child (inherited) 5651 * events is still in tact. 5652 * 5653 * Specifically: 5654 * - leader->ctx->lock pins leader->sibling_list 5655 * - parent->child_mutex pins parent->child_list 5656 * - parent->ctx->mutex pins parent->sibling_list 5657 * 5658 * Because parent->ctx != leader->ctx (and child_list nests inside 5659 * ctx->mutex), group destruction is not atomic between children, also 5660 * see perf_event_release_kernel(). Additionally, parent can grow the 5661 * group. 5662 * 5663 * Therefore it is possible to have parent and child groups in a 5664 * different configuration and summing over such a beast makes no sense 5665 * what so ever. 5666 * 5667 * Reject this. 5668 */ 5669 parent = leader->parent; 5670 if (parent && 5671 (parent->group_generation != leader->group_generation || 5672 parent->nr_siblings != leader->nr_siblings)) { 5673 ret = -ECHILD; 5674 goto unlock; 5675 } 5676 5677 /* 5678 * Since we co-schedule groups, {enabled,running} times of siblings 5679 * will be identical to those of the leader, so we only publish one 5680 * set. 5681 */ 5682 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { 5683 values[n++] += leader->total_time_enabled + 5684 atomic64_read(&leader->child_total_time_enabled); 5685 } 5686 5687 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { 5688 values[n++] += leader->total_time_running + 5689 atomic64_read(&leader->child_total_time_running); 5690 } 5691 5692 /* 5693 * Write {count,id} tuples for every sibling. 5694 */ 5695 values[n++] += perf_event_count(leader, false); 5696 if (read_format & PERF_FORMAT_ID) 5697 values[n++] = primary_event_id(leader); 5698 if (read_format & PERF_FORMAT_LOST) 5699 values[n++] = atomic64_read(&leader->lost_samples); 5700 5701 for_each_sibling_event(sub, leader) { 5702 values[n++] += perf_event_count(sub, false); 5703 if (read_format & PERF_FORMAT_ID) 5704 values[n++] = primary_event_id(sub); 5705 if (read_format & PERF_FORMAT_LOST) 5706 values[n++] = atomic64_read(&sub->lost_samples); 5707 } 5708 5709 unlock: 5710 raw_spin_unlock_irqrestore(&ctx->lock, flags); 5711 return ret; 5712 } 5713 5714 static int perf_read_group(struct perf_event *event, 5715 u64 read_format, char __user *buf) 5716 { 5717 struct perf_event *leader = event->group_leader, *child; 5718 struct perf_event_context *ctx = leader->ctx; 5719 int ret; 5720 u64 *values; 5721 5722 lockdep_assert_held(&ctx->mutex); 5723 5724 values = kzalloc(event->read_size, GFP_KERNEL); 5725 if (!values) 5726 return -ENOMEM; 5727 5728 values[0] = 1 + leader->nr_siblings; 5729 5730 mutex_lock(&leader->child_mutex); 5731 5732 ret = __perf_read_group_add(leader, read_format, values); 5733 if (ret) 5734 goto unlock; 5735 5736 list_for_each_entry(child, &leader->child_list, child_list) { 5737 ret = __perf_read_group_add(child, read_format, values); 5738 if (ret) 5739 goto unlock; 5740 } 5741 5742 mutex_unlock(&leader->child_mutex); 5743 5744 ret = event->read_size; 5745 if (copy_to_user(buf, values, event->read_size)) 5746 ret = -EFAULT; 5747 goto out; 5748 5749 unlock: 5750 mutex_unlock(&leader->child_mutex); 5751 out: 5752 kfree(values); 5753 return ret; 5754 } 5755 5756 static int perf_read_one(struct perf_event *event, 5757 u64 read_format, char __user *buf) 5758 { 5759 u64 enabled, running; 5760 u64 values[5]; 5761 int n = 0; 5762 5763 values[n++] = __perf_event_read_value(event, &enabled, &running); 5764 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 5765 values[n++] = enabled; 5766 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 5767 values[n++] = running; 5768 if (read_format & PERF_FORMAT_ID) 5769 values[n++] = primary_event_id(event); 5770 if (read_format & PERF_FORMAT_LOST) 5771 values[n++] = atomic64_read(&event->lost_samples); 5772 5773 if (copy_to_user(buf, values, n * sizeof(u64))) 5774 return -EFAULT; 5775 5776 return n * sizeof(u64); 5777 } 5778 5779 static bool is_event_hup(struct perf_event *event) 5780 { 5781 bool no_children; 5782 5783 if (event->state > PERF_EVENT_STATE_EXIT) 5784 return false; 5785 5786 mutex_lock(&event->child_mutex); 5787 no_children = list_empty(&event->child_list); 5788 mutex_unlock(&event->child_mutex); 5789 return no_children; 5790 } 5791 5792 /* 5793 * Read the performance event - simple non blocking version for now 5794 */ 5795 static ssize_t 5796 __perf_read(struct perf_event *event, char __user *buf, size_t count) 5797 { 5798 u64 read_format = event->attr.read_format; 5799 int ret; 5800 5801 /* 5802 * Return end-of-file for a read on an event that is in 5803 * error state (i.e. because it was pinned but it couldn't be 5804 * scheduled on to the CPU at some point). 5805 */ 5806 if (event->state == PERF_EVENT_STATE_ERROR) 5807 return 0; 5808 5809 if (count < event->read_size) 5810 return -ENOSPC; 5811 5812 WARN_ON_ONCE(event->ctx->parent_ctx); 5813 if (read_format & PERF_FORMAT_GROUP) 5814 ret = perf_read_group(event, read_format, buf); 5815 else 5816 ret = perf_read_one(event, read_format, buf); 5817 5818 return ret; 5819 } 5820 5821 static ssize_t 5822 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) 5823 { 5824 struct perf_event *event = file->private_data; 5825 struct perf_event_context *ctx; 5826 int ret; 5827 5828 ret = security_perf_event_read(event); 5829 if (ret) 5830 return ret; 5831 5832 ctx = perf_event_ctx_lock(event); 5833 ret = __perf_read(event, buf, count); 5834 perf_event_ctx_unlock(event, ctx); 5835 5836 return ret; 5837 } 5838 5839 static __poll_t perf_poll(struct file *file, poll_table *wait) 5840 { 5841 struct perf_event *event = file->private_data; 5842 struct perf_buffer *rb; 5843 __poll_t events = EPOLLHUP; 5844 5845 poll_wait(file, &event->waitq, wait); 5846 5847 if (is_event_hup(event)) 5848 return events; 5849 5850 /* 5851 * Pin the event->rb by taking event->mmap_mutex; otherwise 5852 * perf_event_set_output() can swizzle our rb and make us miss wakeups. 5853 */ 5854 mutex_lock(&event->mmap_mutex); 5855 rb = event->rb; 5856 if (rb) 5857 events = atomic_xchg(&rb->poll, 0); 5858 mutex_unlock(&event->mmap_mutex); 5859 return events; 5860 } 5861 5862 static void _perf_event_reset(struct perf_event *event) 5863 { 5864 (void)perf_event_read(event, false); 5865 local64_set(&event->count, 0); 5866 perf_event_update_userpage(event); 5867 } 5868 5869 /* Assume it's not an event with inherit set. */ 5870 u64 perf_event_pause(struct perf_event *event, bool reset) 5871 { 5872 struct perf_event_context *ctx; 5873 u64 count; 5874 5875 ctx = perf_event_ctx_lock(event); 5876 WARN_ON_ONCE(event->attr.inherit); 5877 _perf_event_disable(event); 5878 count = local64_read(&event->count); 5879 if (reset) 5880 local64_set(&event->count, 0); 5881 perf_event_ctx_unlock(event, ctx); 5882 5883 return count; 5884 } 5885 EXPORT_SYMBOL_GPL(perf_event_pause); 5886 5887 /* 5888 * Holding the top-level event's child_mutex means that any 5889 * descendant process that has inherited this event will block 5890 * in perf_event_exit_event() if it goes to exit, thus satisfying the 5891 * task existence requirements of perf_event_enable/disable. 5892 */ 5893 static void perf_event_for_each_child(struct perf_event *event, 5894 void (*func)(struct perf_event *)) 5895 { 5896 struct perf_event *child; 5897 5898 WARN_ON_ONCE(event->ctx->parent_ctx); 5899 5900 mutex_lock(&event->child_mutex); 5901 func(event); 5902 list_for_each_entry(child, &event->child_list, child_list) 5903 func(child); 5904 mutex_unlock(&event->child_mutex); 5905 } 5906 5907 static void perf_event_for_each(struct perf_event *event, 5908 void (*func)(struct perf_event *)) 5909 { 5910 struct perf_event_context *ctx = event->ctx; 5911 struct perf_event *sibling; 5912 5913 lockdep_assert_held(&ctx->mutex); 5914 5915 event = event->group_leader; 5916 5917 perf_event_for_each_child(event, func); 5918 for_each_sibling_event(sibling, event) 5919 perf_event_for_each_child(sibling, func); 5920 } 5921 5922 static void __perf_event_period(struct perf_event *event, 5923 struct perf_cpu_context *cpuctx, 5924 struct perf_event_context *ctx, 5925 void *info) 5926 { 5927 u64 value = *((u64 *)info); 5928 bool active; 5929 5930 if (event->attr.freq) { 5931 event->attr.sample_freq = value; 5932 } else { 5933 event->attr.sample_period = value; 5934 event->hw.sample_period = value; 5935 } 5936 5937 active = (event->state == PERF_EVENT_STATE_ACTIVE); 5938 if (active) { 5939 perf_pmu_disable(event->pmu); 5940 /* 5941 * We could be throttled; unthrottle now to avoid the tick 5942 * trying to unthrottle while we already re-started the event. 5943 */ 5944 if (event->hw.interrupts == MAX_INTERRUPTS) { 5945 event->hw.interrupts = 0; 5946 perf_log_throttle(event, 1); 5947 } 5948 event->pmu->stop(event, PERF_EF_UPDATE); 5949 } 5950 5951 local64_set(&event->hw.period_left, 0); 5952 5953 if (active) { 5954 event->pmu->start(event, PERF_EF_RELOAD); 5955 perf_pmu_enable(event->pmu); 5956 } 5957 } 5958 5959 static int perf_event_check_period(struct perf_event *event, u64 value) 5960 { 5961 return event->pmu->check_period(event, value); 5962 } 5963 5964 static int _perf_event_period(struct perf_event *event, u64 value) 5965 { 5966 if (!is_sampling_event(event)) 5967 return -EINVAL; 5968 5969 if (!value) 5970 return -EINVAL; 5971 5972 if (event->attr.freq && value > sysctl_perf_event_sample_rate) 5973 return -EINVAL; 5974 5975 if (perf_event_check_period(event, value)) 5976 return -EINVAL; 5977 5978 if (!event->attr.freq && (value & (1ULL << 63))) 5979 return -EINVAL; 5980 5981 event_function_call(event, __perf_event_period, &value); 5982 5983 return 0; 5984 } 5985 5986 int perf_event_period(struct perf_event *event, u64 value) 5987 { 5988 struct perf_event_context *ctx; 5989 int ret; 5990 5991 ctx = perf_event_ctx_lock(event); 5992 ret = _perf_event_period(event, value); 5993 perf_event_ctx_unlock(event, ctx); 5994 5995 return ret; 5996 } 5997 EXPORT_SYMBOL_GPL(perf_event_period); 5998 5999 static const struct file_operations perf_fops; 6000 6001 static inline bool is_perf_file(struct fd f) 6002 { 6003 return !fd_empty(f) && fd_file(f)->f_op == &perf_fops; 6004 } 6005 6006 static int perf_event_set_output(struct perf_event *event, 6007 struct perf_event *output_event); 6008 static int perf_event_set_filter(struct perf_event *event, void __user *arg); 6009 static int perf_copy_attr(struct perf_event_attr __user *uattr, 6010 struct perf_event_attr *attr); 6011 6012 static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg) 6013 { 6014 void (*func)(struct perf_event *); 6015 u32 flags = arg; 6016 6017 switch (cmd) { 6018 case PERF_EVENT_IOC_ENABLE: 6019 func = _perf_event_enable; 6020 break; 6021 case PERF_EVENT_IOC_DISABLE: 6022 func = _perf_event_disable; 6023 break; 6024 case PERF_EVENT_IOC_RESET: 6025 func = _perf_event_reset; 6026 break; 6027 6028 case PERF_EVENT_IOC_REFRESH: 6029 return _perf_event_refresh(event, arg); 6030 6031 case PERF_EVENT_IOC_PERIOD: 6032 { 6033 u64 value; 6034 6035 if (copy_from_user(&value, (u64 __user *)arg, sizeof(value))) 6036 return -EFAULT; 6037 6038 return _perf_event_period(event, value); 6039 } 6040 case PERF_EVENT_IOC_ID: 6041 { 6042 u64 id = primary_event_id(event); 6043 6044 if (copy_to_user((void __user *)arg, &id, sizeof(id))) 6045 return -EFAULT; 6046 return 0; 6047 } 6048 6049 case PERF_EVENT_IOC_SET_OUTPUT: 6050 { 6051 CLASS(fd, output)(arg); // arg == -1 => empty 6052 struct perf_event *output_event = NULL; 6053 if (arg != -1) { 6054 if (!is_perf_file(output)) 6055 return -EBADF; 6056 output_event = fd_file(output)->private_data; 6057 } 6058 return perf_event_set_output(event, output_event); 6059 } 6060 6061 case PERF_EVENT_IOC_SET_FILTER: 6062 return perf_event_set_filter(event, (void __user *)arg); 6063 6064 case PERF_EVENT_IOC_SET_BPF: 6065 { 6066 struct bpf_prog *prog; 6067 int err; 6068 6069 prog = bpf_prog_get(arg); 6070 if (IS_ERR(prog)) 6071 return PTR_ERR(prog); 6072 6073 err = perf_event_set_bpf_prog(event, prog, 0); 6074 if (err) { 6075 bpf_prog_put(prog); 6076 return err; 6077 } 6078 6079 return 0; 6080 } 6081 6082 case PERF_EVENT_IOC_PAUSE_OUTPUT: { 6083 struct perf_buffer *rb; 6084 6085 rcu_read_lock(); 6086 rb = rcu_dereference(event->rb); 6087 if (!rb || !rb->nr_pages) { 6088 rcu_read_unlock(); 6089 return -EINVAL; 6090 } 6091 rb_toggle_paused(rb, !!arg); 6092 rcu_read_unlock(); 6093 return 0; 6094 } 6095 6096 case PERF_EVENT_IOC_QUERY_BPF: 6097 return perf_event_query_prog_array(event, (void __user *)arg); 6098 6099 case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: { 6100 struct perf_event_attr new_attr; 6101 int err = perf_copy_attr((struct perf_event_attr __user *)arg, 6102 &new_attr); 6103 6104 if (err) 6105 return err; 6106 6107 return perf_event_modify_attr(event, &new_attr); 6108 } 6109 default: 6110 return -ENOTTY; 6111 } 6112 6113 if (flags & PERF_IOC_FLAG_GROUP) 6114 perf_event_for_each(event, func); 6115 else 6116 perf_event_for_each_child(event, func); 6117 6118 return 0; 6119 } 6120 6121 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 6122 { 6123 struct perf_event *event = file->private_data; 6124 struct perf_event_context *ctx; 6125 long ret; 6126 6127 /* Treat ioctl like writes as it is likely a mutating operation. */ 6128 ret = security_perf_event_write(event); 6129 if (ret) 6130 return ret; 6131 6132 ctx = perf_event_ctx_lock(event); 6133 ret = _perf_ioctl(event, cmd, arg); 6134 perf_event_ctx_unlock(event, ctx); 6135 6136 return ret; 6137 } 6138 6139 #ifdef CONFIG_COMPAT 6140 static long perf_compat_ioctl(struct file *file, unsigned int cmd, 6141 unsigned long arg) 6142 { 6143 switch (_IOC_NR(cmd)) { 6144 case _IOC_NR(PERF_EVENT_IOC_SET_FILTER): 6145 case _IOC_NR(PERF_EVENT_IOC_ID): 6146 case _IOC_NR(PERF_EVENT_IOC_QUERY_BPF): 6147 case _IOC_NR(PERF_EVENT_IOC_MODIFY_ATTRIBUTES): 6148 /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */ 6149 if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) { 6150 cmd &= ~IOCSIZE_MASK; 6151 cmd |= sizeof(void *) << IOCSIZE_SHIFT; 6152 } 6153 break; 6154 } 6155 return perf_ioctl(file, cmd, arg); 6156 } 6157 #else 6158 # define perf_compat_ioctl NULL 6159 #endif 6160 6161 int perf_event_task_enable(void) 6162 { 6163 struct perf_event_context *ctx; 6164 struct perf_event *event; 6165 6166 mutex_lock(¤t->perf_event_mutex); 6167 list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { 6168 ctx = perf_event_ctx_lock(event); 6169 perf_event_for_each_child(event, _perf_event_enable); 6170 perf_event_ctx_unlock(event, ctx); 6171 } 6172 mutex_unlock(¤t->perf_event_mutex); 6173 6174 return 0; 6175 } 6176 6177 int perf_event_task_disable(void) 6178 { 6179 struct perf_event_context *ctx; 6180 struct perf_event *event; 6181 6182 mutex_lock(¤t->perf_event_mutex); 6183 list_for_each_entry(event, ¤t->perf_event_list, owner_entry) { 6184 ctx = perf_event_ctx_lock(event); 6185 perf_event_for_each_child(event, _perf_event_disable); 6186 perf_event_ctx_unlock(event, ctx); 6187 } 6188 mutex_unlock(¤t->perf_event_mutex); 6189 6190 return 0; 6191 } 6192 6193 static int perf_event_index(struct perf_event *event) 6194 { 6195 if (event->hw.state & PERF_HES_STOPPED) 6196 return 0; 6197 6198 if (event->state != PERF_EVENT_STATE_ACTIVE) 6199 return 0; 6200 6201 return event->pmu->event_idx(event); 6202 } 6203 6204 static void perf_event_init_userpage(struct perf_event *event) 6205 { 6206 struct perf_event_mmap_page *userpg; 6207 struct perf_buffer *rb; 6208 6209 rcu_read_lock(); 6210 rb = rcu_dereference(event->rb); 6211 if (!rb) 6212 goto unlock; 6213 6214 userpg = rb->user_page; 6215 6216 /* Allow new userspace to detect that bit 0 is deprecated */ 6217 userpg->cap_bit0_is_deprecated = 1; 6218 userpg->size = offsetof(struct perf_event_mmap_page, __reserved); 6219 userpg->data_offset = PAGE_SIZE; 6220 userpg->data_size = perf_data_size(rb); 6221 6222 unlock: 6223 rcu_read_unlock(); 6224 } 6225 6226 void __weak arch_perf_update_userpage( 6227 struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now) 6228 { 6229 } 6230 6231 /* 6232 * Callers need to ensure there can be no nesting of this function, otherwise 6233 * the seqlock logic goes bad. We can not serialize this because the arch 6234 * code calls this from NMI context. 6235 */ 6236 void perf_event_update_userpage(struct perf_event *event) 6237 { 6238 struct perf_event_mmap_page *userpg; 6239 struct perf_buffer *rb; 6240 u64 enabled, running, now; 6241 6242 rcu_read_lock(); 6243 rb = rcu_dereference(event->rb); 6244 if (!rb) 6245 goto unlock; 6246 6247 /* 6248 * compute total_time_enabled, total_time_running 6249 * based on snapshot values taken when the event 6250 * was last scheduled in. 6251 * 6252 * we cannot simply called update_context_time() 6253 * because of locking issue as we can be called in 6254 * NMI context 6255 */ 6256 calc_timer_values(event, &now, &enabled, &running); 6257 6258 userpg = rb->user_page; 6259 /* 6260 * Disable preemption to guarantee consistent time stamps are stored to 6261 * the user page. 6262 */ 6263 preempt_disable(); 6264 ++userpg->lock; 6265 barrier(); 6266 userpg->index = perf_event_index(event); 6267 userpg->offset = perf_event_count(event, false); 6268 if (userpg->index) 6269 userpg->offset -= local64_read(&event->hw.prev_count); 6270 6271 userpg->time_enabled = enabled + 6272 atomic64_read(&event->child_total_time_enabled); 6273 6274 userpg->time_running = running + 6275 atomic64_read(&event->child_total_time_running); 6276 6277 arch_perf_update_userpage(event, userpg, now); 6278 6279 barrier(); 6280 ++userpg->lock; 6281 preempt_enable(); 6282 unlock: 6283 rcu_read_unlock(); 6284 } 6285 EXPORT_SYMBOL_GPL(perf_event_update_userpage); 6286 6287 static vm_fault_t perf_mmap_fault(struct vm_fault *vmf) 6288 { 6289 struct perf_event *event = vmf->vma->vm_file->private_data; 6290 struct perf_buffer *rb; 6291 vm_fault_t ret = VM_FAULT_SIGBUS; 6292 6293 if (vmf->flags & FAULT_FLAG_MKWRITE) { 6294 if (vmf->pgoff == 0) 6295 ret = 0; 6296 return ret; 6297 } 6298 6299 rcu_read_lock(); 6300 rb = rcu_dereference(event->rb); 6301 if (!rb) 6302 goto unlock; 6303 6304 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) 6305 goto unlock; 6306 6307 vmf->page = perf_mmap_to_page(rb, vmf->pgoff); 6308 if (!vmf->page) 6309 goto unlock; 6310 6311 get_page(vmf->page); 6312 vmf->page->mapping = vmf->vma->vm_file->f_mapping; 6313 vmf->page->index = vmf->pgoff; 6314 6315 ret = 0; 6316 unlock: 6317 rcu_read_unlock(); 6318 6319 return ret; 6320 } 6321 6322 static void ring_buffer_attach(struct perf_event *event, 6323 struct perf_buffer *rb) 6324 { 6325 struct perf_buffer *old_rb = NULL; 6326 unsigned long flags; 6327 6328 WARN_ON_ONCE(event->parent); 6329 6330 if (event->rb) { 6331 /* 6332 * Should be impossible, we set this when removing 6333 * event->rb_entry and wait/clear when adding event->rb_entry. 6334 */ 6335 WARN_ON_ONCE(event->rcu_pending); 6336 6337 old_rb = event->rb; 6338 spin_lock_irqsave(&old_rb->event_lock, flags); 6339 list_del_rcu(&event->rb_entry); 6340 spin_unlock_irqrestore(&old_rb->event_lock, flags); 6341 6342 event->rcu_batches = get_state_synchronize_rcu(); 6343 event->rcu_pending = 1; 6344 } 6345 6346 if (rb) { 6347 if (event->rcu_pending) { 6348 cond_synchronize_rcu(event->rcu_batches); 6349 event->rcu_pending = 0; 6350 } 6351 6352 spin_lock_irqsave(&rb->event_lock, flags); 6353 list_add_rcu(&event->rb_entry, &rb->event_list); 6354 spin_unlock_irqrestore(&rb->event_lock, flags); 6355 } 6356 6357 /* 6358 * Avoid racing with perf_mmap_close(AUX): stop the event 6359 * before swizzling the event::rb pointer; if it's getting 6360 * unmapped, its aux_mmap_count will be 0 and it won't 6361 * restart. See the comment in __perf_pmu_output_stop(). 6362 * 6363 * Data will inevitably be lost when set_output is done in 6364 * mid-air, but then again, whoever does it like this is 6365 * not in for the data anyway. 6366 */ 6367 if (has_aux(event)) 6368 perf_event_stop(event, 0); 6369 6370 rcu_assign_pointer(event->rb, rb); 6371 6372 if (old_rb) { 6373 ring_buffer_put(old_rb); 6374 /* 6375 * Since we detached before setting the new rb, so that we 6376 * could attach the new rb, we could have missed a wakeup. 6377 * Provide it now. 6378 */ 6379 wake_up_all(&event->waitq); 6380 } 6381 } 6382 6383 static void ring_buffer_wakeup(struct perf_event *event) 6384 { 6385 struct perf_buffer *rb; 6386 6387 if (event->parent) 6388 event = event->parent; 6389 6390 rcu_read_lock(); 6391 rb = rcu_dereference(event->rb); 6392 if (rb) { 6393 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) 6394 wake_up_all(&event->waitq); 6395 } 6396 rcu_read_unlock(); 6397 } 6398 6399 struct perf_buffer *ring_buffer_get(struct perf_event *event) 6400 { 6401 struct perf_buffer *rb; 6402 6403 if (event->parent) 6404 event = event->parent; 6405 6406 rcu_read_lock(); 6407 rb = rcu_dereference(event->rb); 6408 if (rb) { 6409 if (!refcount_inc_not_zero(&rb->refcount)) 6410 rb = NULL; 6411 } 6412 rcu_read_unlock(); 6413 6414 return rb; 6415 } 6416 6417 void ring_buffer_put(struct perf_buffer *rb) 6418 { 6419 if (!refcount_dec_and_test(&rb->refcount)) 6420 return; 6421 6422 WARN_ON_ONCE(!list_empty(&rb->event_list)); 6423 6424 call_rcu(&rb->rcu_head, rb_free_rcu); 6425 } 6426 6427 static void perf_mmap_open(struct vm_area_struct *vma) 6428 { 6429 struct perf_event *event = vma->vm_file->private_data; 6430 6431 atomic_inc(&event->mmap_count); 6432 atomic_inc(&event->rb->mmap_count); 6433 6434 if (vma->vm_pgoff) 6435 atomic_inc(&event->rb->aux_mmap_count); 6436 6437 if (event->pmu->event_mapped) 6438 event->pmu->event_mapped(event, vma->vm_mm); 6439 } 6440 6441 static void perf_pmu_output_stop(struct perf_event *event); 6442 6443 /* 6444 * A buffer can be mmap()ed multiple times; either directly through the same 6445 * event, or through other events by use of perf_event_set_output(). 6446 * 6447 * In order to undo the VM accounting done by perf_mmap() we need to destroy 6448 * the buffer here, where we still have a VM context. This means we need 6449 * to detach all events redirecting to us. 6450 */ 6451 static void perf_mmap_close(struct vm_area_struct *vma) 6452 { 6453 struct perf_event *event = vma->vm_file->private_data; 6454 struct perf_buffer *rb = ring_buffer_get(event); 6455 struct user_struct *mmap_user = rb->mmap_user; 6456 int mmap_locked = rb->mmap_locked; 6457 unsigned long size = perf_data_size(rb); 6458 bool detach_rest = false; 6459 6460 if (event->pmu->event_unmapped) 6461 event->pmu->event_unmapped(event, vma->vm_mm); 6462 6463 /* 6464 * The AUX buffer is strictly a sub-buffer, serialize using aux_mutex 6465 * to avoid complications. 6466 */ 6467 if (rb_has_aux(rb) && vma->vm_pgoff == rb->aux_pgoff && 6468 atomic_dec_and_mutex_lock(&rb->aux_mmap_count, &rb->aux_mutex)) { 6469 /* 6470 * Stop all AUX events that are writing to this buffer, 6471 * so that we can free its AUX pages and corresponding PMU 6472 * data. Note that after rb::aux_mmap_count dropped to zero, 6473 * they won't start any more (see perf_aux_output_begin()). 6474 */ 6475 perf_pmu_output_stop(event); 6476 6477 /* now it's safe to free the pages */ 6478 atomic_long_sub(rb->aux_nr_pages - rb->aux_mmap_locked, &mmap_user->locked_vm); 6479 atomic64_sub(rb->aux_mmap_locked, &vma->vm_mm->pinned_vm); 6480 6481 /* this has to be the last one */ 6482 rb_free_aux(rb); 6483 WARN_ON_ONCE(refcount_read(&rb->aux_refcount)); 6484 6485 mutex_unlock(&rb->aux_mutex); 6486 } 6487 6488 if (atomic_dec_and_test(&rb->mmap_count)) 6489 detach_rest = true; 6490 6491 if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) 6492 goto out_put; 6493 6494 ring_buffer_attach(event, NULL); 6495 mutex_unlock(&event->mmap_mutex); 6496 6497 /* If there's still other mmap()s of this buffer, we're done. */ 6498 if (!detach_rest) 6499 goto out_put; 6500 6501 /* 6502 * No other mmap()s, detach from all other events that might redirect 6503 * into the now unreachable buffer. Somewhat complicated by the 6504 * fact that rb::event_lock otherwise nests inside mmap_mutex. 6505 */ 6506 again: 6507 rcu_read_lock(); 6508 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) { 6509 if (!atomic_long_inc_not_zero(&event->refcount)) { 6510 /* 6511 * This event is en-route to free_event() which will 6512 * detach it and remove it from the list. 6513 */ 6514 continue; 6515 } 6516 rcu_read_unlock(); 6517 6518 mutex_lock(&event->mmap_mutex); 6519 /* 6520 * Check we didn't race with perf_event_set_output() which can 6521 * swizzle the rb from under us while we were waiting to 6522 * acquire mmap_mutex. 6523 * 6524 * If we find a different rb; ignore this event, a next 6525 * iteration will no longer find it on the list. We have to 6526 * still restart the iteration to make sure we're not now 6527 * iterating the wrong list. 6528 */ 6529 if (event->rb == rb) 6530 ring_buffer_attach(event, NULL); 6531 6532 mutex_unlock(&event->mmap_mutex); 6533 put_event(event); 6534 6535 /* 6536 * Restart the iteration; either we're on the wrong list or 6537 * destroyed its integrity by doing a deletion. 6538 */ 6539 goto again; 6540 } 6541 rcu_read_unlock(); 6542 6543 /* 6544 * It could be there's still a few 0-ref events on the list; they'll 6545 * get cleaned up by free_event() -- they'll also still have their 6546 * ref on the rb and will free it whenever they are done with it. 6547 * 6548 * Aside from that, this buffer is 'fully' detached and unmapped, 6549 * undo the VM accounting. 6550 */ 6551 6552 atomic_long_sub((size >> PAGE_SHIFT) + 1 - mmap_locked, 6553 &mmap_user->locked_vm); 6554 atomic64_sub(mmap_locked, &vma->vm_mm->pinned_vm); 6555 free_uid(mmap_user); 6556 6557 out_put: 6558 ring_buffer_put(rb); /* could be last */ 6559 } 6560 6561 static const struct vm_operations_struct perf_mmap_vmops = { 6562 .open = perf_mmap_open, 6563 .close = perf_mmap_close, /* non mergeable */ 6564 .fault = perf_mmap_fault, 6565 .page_mkwrite = perf_mmap_fault, 6566 }; 6567 6568 static int perf_mmap(struct file *file, struct vm_area_struct *vma) 6569 { 6570 struct perf_event *event = file->private_data; 6571 unsigned long user_locked, user_lock_limit; 6572 struct user_struct *user = current_user(); 6573 struct mutex *aux_mutex = NULL; 6574 struct perf_buffer *rb = NULL; 6575 unsigned long locked, lock_limit; 6576 unsigned long vma_size; 6577 unsigned long nr_pages; 6578 long user_extra = 0, extra = 0; 6579 int ret = 0, flags = 0; 6580 6581 /* 6582 * Don't allow mmap() of inherited per-task counters. This would 6583 * create a performance issue due to all children writing to the 6584 * same rb. 6585 */ 6586 if (event->cpu == -1 && event->attr.inherit) 6587 return -EINVAL; 6588 6589 if (!(vma->vm_flags & VM_SHARED)) 6590 return -EINVAL; 6591 6592 ret = security_perf_event_read(event); 6593 if (ret) 6594 return ret; 6595 6596 vma_size = vma->vm_end - vma->vm_start; 6597 6598 if (vma->vm_pgoff == 0) { 6599 nr_pages = (vma_size / PAGE_SIZE) - 1; 6600 } else { 6601 /* 6602 * AUX area mapping: if rb->aux_nr_pages != 0, it's already 6603 * mapped, all subsequent mappings should have the same size 6604 * and offset. Must be above the normal perf buffer. 6605 */ 6606 u64 aux_offset, aux_size; 6607 6608 if (!event->rb) 6609 return -EINVAL; 6610 6611 nr_pages = vma_size / PAGE_SIZE; 6612 if (nr_pages > INT_MAX) 6613 return -ENOMEM; 6614 6615 mutex_lock(&event->mmap_mutex); 6616 ret = -EINVAL; 6617 6618 rb = event->rb; 6619 if (!rb) 6620 goto aux_unlock; 6621 6622 aux_mutex = &rb->aux_mutex; 6623 mutex_lock(aux_mutex); 6624 6625 aux_offset = READ_ONCE(rb->user_page->aux_offset); 6626 aux_size = READ_ONCE(rb->user_page->aux_size); 6627 6628 if (aux_offset < perf_data_size(rb) + PAGE_SIZE) 6629 goto aux_unlock; 6630 6631 if (aux_offset != vma->vm_pgoff << PAGE_SHIFT) 6632 goto aux_unlock; 6633 6634 /* already mapped with a different offset */ 6635 if (rb_has_aux(rb) && rb->aux_pgoff != vma->vm_pgoff) 6636 goto aux_unlock; 6637 6638 if (aux_size != vma_size || aux_size != nr_pages * PAGE_SIZE) 6639 goto aux_unlock; 6640 6641 /* already mapped with a different size */ 6642 if (rb_has_aux(rb) && rb->aux_nr_pages != nr_pages) 6643 goto aux_unlock; 6644 6645 if (!is_power_of_2(nr_pages)) 6646 goto aux_unlock; 6647 6648 if (!atomic_inc_not_zero(&rb->mmap_count)) 6649 goto aux_unlock; 6650 6651 if (rb_has_aux(rb)) { 6652 atomic_inc(&rb->aux_mmap_count); 6653 ret = 0; 6654 goto unlock; 6655 } 6656 6657 atomic_set(&rb->aux_mmap_count, 1); 6658 user_extra = nr_pages; 6659 6660 goto accounting; 6661 } 6662 6663 /* 6664 * If we have rb pages ensure they're a power-of-two number, so we 6665 * can do bitmasks instead of modulo. 6666 */ 6667 if (nr_pages != 0 && !is_power_of_2(nr_pages)) 6668 return -EINVAL; 6669 6670 if (vma_size != PAGE_SIZE * (1 + nr_pages)) 6671 return -EINVAL; 6672 6673 WARN_ON_ONCE(event->ctx->parent_ctx); 6674 again: 6675 mutex_lock(&event->mmap_mutex); 6676 if (event->rb) { 6677 if (data_page_nr(event->rb) != nr_pages) { 6678 ret = -EINVAL; 6679 goto unlock; 6680 } 6681 6682 if (!atomic_inc_not_zero(&event->rb->mmap_count)) { 6683 /* 6684 * Raced against perf_mmap_close(); remove the 6685 * event and try again. 6686 */ 6687 ring_buffer_attach(event, NULL); 6688 mutex_unlock(&event->mmap_mutex); 6689 goto again; 6690 } 6691 6692 goto unlock; 6693 } 6694 6695 user_extra = nr_pages + 1; 6696 6697 accounting: 6698 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); 6699 6700 /* 6701 * Increase the limit linearly with more CPUs: 6702 */ 6703 user_lock_limit *= num_online_cpus(); 6704 6705 user_locked = atomic_long_read(&user->locked_vm); 6706 6707 /* 6708 * sysctl_perf_event_mlock may have changed, so that 6709 * user->locked_vm > user_lock_limit 6710 */ 6711 if (user_locked > user_lock_limit) 6712 user_locked = user_lock_limit; 6713 user_locked += user_extra; 6714 6715 if (user_locked > user_lock_limit) { 6716 /* 6717 * charge locked_vm until it hits user_lock_limit; 6718 * charge the rest from pinned_vm 6719 */ 6720 extra = user_locked - user_lock_limit; 6721 user_extra -= extra; 6722 } 6723 6724 lock_limit = rlimit(RLIMIT_MEMLOCK); 6725 lock_limit >>= PAGE_SHIFT; 6726 locked = atomic64_read(&vma->vm_mm->pinned_vm) + extra; 6727 6728 if ((locked > lock_limit) && perf_is_paranoid() && 6729 !capable(CAP_IPC_LOCK)) { 6730 ret = -EPERM; 6731 goto unlock; 6732 } 6733 6734 WARN_ON(!rb && event->rb); 6735 6736 if (vma->vm_flags & VM_WRITE) 6737 flags |= RING_BUFFER_WRITABLE; 6738 6739 if (!rb) { 6740 rb = rb_alloc(nr_pages, 6741 event->attr.watermark ? event->attr.wakeup_watermark : 0, 6742 event->cpu, flags); 6743 6744 if (!rb) { 6745 ret = -ENOMEM; 6746 goto unlock; 6747 } 6748 6749 atomic_set(&rb->mmap_count, 1); 6750 rb->mmap_user = get_current_user(); 6751 rb->mmap_locked = extra; 6752 6753 ring_buffer_attach(event, rb); 6754 6755 perf_event_update_time(event); 6756 perf_event_init_userpage(event); 6757 perf_event_update_userpage(event); 6758 } else { 6759 ret = rb_alloc_aux(rb, event, vma->vm_pgoff, nr_pages, 6760 event->attr.aux_watermark, flags); 6761 if (!ret) 6762 rb->aux_mmap_locked = extra; 6763 } 6764 6765 unlock: 6766 if (!ret) { 6767 atomic_long_add(user_extra, &user->locked_vm); 6768 atomic64_add(extra, &vma->vm_mm->pinned_vm); 6769 6770 atomic_inc(&event->mmap_count); 6771 } else if (rb) { 6772 atomic_dec(&rb->mmap_count); 6773 } 6774 aux_unlock: 6775 if (aux_mutex) 6776 mutex_unlock(aux_mutex); 6777 mutex_unlock(&event->mmap_mutex); 6778 6779 /* 6780 * Since pinned accounting is per vm we cannot allow fork() to copy our 6781 * vma. 6782 */ 6783 vm_flags_set(vma, VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP); 6784 vma->vm_ops = &perf_mmap_vmops; 6785 6786 if (event->pmu->event_mapped) 6787 event->pmu->event_mapped(event, vma->vm_mm); 6788 6789 return ret; 6790 } 6791 6792 static int perf_fasync(int fd, struct file *filp, int on) 6793 { 6794 struct inode *inode = file_inode(filp); 6795 struct perf_event *event = filp->private_data; 6796 int retval; 6797 6798 inode_lock(inode); 6799 retval = fasync_helper(fd, filp, on, &event->fasync); 6800 inode_unlock(inode); 6801 6802 if (retval < 0) 6803 return retval; 6804 6805 return 0; 6806 } 6807 6808 static const struct file_operations perf_fops = { 6809 .release = perf_release, 6810 .read = perf_read, 6811 .poll = perf_poll, 6812 .unlocked_ioctl = perf_ioctl, 6813 .compat_ioctl = perf_compat_ioctl, 6814 .mmap = perf_mmap, 6815 .fasync = perf_fasync, 6816 }; 6817 6818 /* 6819 * Perf event wakeup 6820 * 6821 * If there's data, ensure we set the poll() state and publish everything 6822 * to user-space before waking everybody up. 6823 */ 6824 6825 void perf_event_wakeup(struct perf_event *event) 6826 { 6827 ring_buffer_wakeup(event); 6828 6829 if (event->pending_kill) { 6830 kill_fasync(perf_event_fasync(event), SIGIO, event->pending_kill); 6831 event->pending_kill = 0; 6832 } 6833 } 6834 6835 static void perf_sigtrap(struct perf_event *event) 6836 { 6837 /* 6838 * We'd expect this to only occur if the irq_work is delayed and either 6839 * ctx->task or current has changed in the meantime. This can be the 6840 * case on architectures that do not implement arch_irq_work_raise(). 6841 */ 6842 if (WARN_ON_ONCE(event->ctx->task != current)) 6843 return; 6844 6845 /* 6846 * Both perf_pending_task() and perf_pending_irq() can race with the 6847 * task exiting. 6848 */ 6849 if (current->flags & PF_EXITING) 6850 return; 6851 6852 send_sig_perf((void __user *)event->pending_addr, 6853 event->orig_type, event->attr.sig_data); 6854 } 6855 6856 /* 6857 * Deliver the pending work in-event-context or follow the context. 6858 */ 6859 static void __perf_pending_disable(struct perf_event *event) 6860 { 6861 int cpu = READ_ONCE(event->oncpu); 6862 6863 /* 6864 * If the event isn't running; we done. event_sched_out() will have 6865 * taken care of things. 6866 */ 6867 if (cpu < 0) 6868 return; 6869 6870 /* 6871 * Yay, we hit home and are in the context of the event. 6872 */ 6873 if (cpu == smp_processor_id()) { 6874 if (event->pending_disable) { 6875 event->pending_disable = 0; 6876 perf_event_disable_local(event); 6877 } 6878 return; 6879 } 6880 6881 /* 6882 * CPU-A CPU-B 6883 * 6884 * perf_event_disable_inatomic() 6885 * @pending_disable = CPU-A; 6886 * irq_work_queue(); 6887 * 6888 * sched-out 6889 * @pending_disable = -1; 6890 * 6891 * sched-in 6892 * perf_event_disable_inatomic() 6893 * @pending_disable = CPU-B; 6894 * irq_work_queue(); // FAILS 6895 * 6896 * irq_work_run() 6897 * perf_pending_disable() 6898 * 6899 * But the event runs on CPU-B and wants disabling there. 6900 */ 6901 irq_work_queue_on(&event->pending_disable_irq, cpu); 6902 } 6903 6904 static void perf_pending_disable(struct irq_work *entry) 6905 { 6906 struct perf_event *event = container_of(entry, struct perf_event, pending_disable_irq); 6907 int rctx; 6908 6909 /* 6910 * If we 'fail' here, that's OK, it means recursion is already disabled 6911 * and we won't recurse 'further'. 6912 */ 6913 rctx = perf_swevent_get_recursion_context(); 6914 __perf_pending_disable(event); 6915 if (rctx >= 0) 6916 perf_swevent_put_recursion_context(rctx); 6917 } 6918 6919 static void perf_pending_irq(struct irq_work *entry) 6920 { 6921 struct perf_event *event = container_of(entry, struct perf_event, pending_irq); 6922 int rctx; 6923 6924 /* 6925 * If we 'fail' here, that's OK, it means recursion is already disabled 6926 * and we won't recurse 'further'. 6927 */ 6928 rctx = perf_swevent_get_recursion_context(); 6929 6930 /* 6931 * The wakeup isn't bound to the context of the event -- it can happen 6932 * irrespective of where the event is. 6933 */ 6934 if (event->pending_wakeup) { 6935 event->pending_wakeup = 0; 6936 perf_event_wakeup(event); 6937 } 6938 6939 if (rctx >= 0) 6940 perf_swevent_put_recursion_context(rctx); 6941 } 6942 6943 static void perf_pending_task(struct callback_head *head) 6944 { 6945 struct perf_event *event = container_of(head, struct perf_event, pending_task); 6946 int rctx; 6947 6948 /* 6949 * All accesses to the event must belong to the same implicit RCU read-side 6950 * critical section as the ->pending_work reset. See comment in 6951 * perf_pending_task_sync(). 6952 */ 6953 rcu_read_lock(); 6954 /* 6955 * If we 'fail' here, that's OK, it means recursion is already disabled 6956 * and we won't recurse 'further'. 6957 */ 6958 rctx = perf_swevent_get_recursion_context(); 6959 6960 if (event->pending_work) { 6961 event->pending_work = 0; 6962 perf_sigtrap(event); 6963 local_dec(&event->ctx->nr_no_switch_fast); 6964 rcuwait_wake_up(&event->pending_work_wait); 6965 } 6966 rcu_read_unlock(); 6967 6968 if (rctx >= 0) 6969 perf_swevent_put_recursion_context(rctx); 6970 } 6971 6972 #ifdef CONFIG_GUEST_PERF_EVENTS 6973 struct perf_guest_info_callbacks __rcu *perf_guest_cbs; 6974 6975 DEFINE_STATIC_CALL_RET0(__perf_guest_state, *perf_guest_cbs->state); 6976 DEFINE_STATIC_CALL_RET0(__perf_guest_get_ip, *perf_guest_cbs->get_ip); 6977 DEFINE_STATIC_CALL_RET0(__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr); 6978 6979 void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) 6980 { 6981 if (WARN_ON_ONCE(rcu_access_pointer(perf_guest_cbs))) 6982 return; 6983 6984 rcu_assign_pointer(perf_guest_cbs, cbs); 6985 static_call_update(__perf_guest_state, cbs->state); 6986 static_call_update(__perf_guest_get_ip, cbs->get_ip); 6987 6988 /* Implementing ->handle_intel_pt_intr is optional. */ 6989 if (cbs->handle_intel_pt_intr) 6990 static_call_update(__perf_guest_handle_intel_pt_intr, 6991 cbs->handle_intel_pt_intr); 6992 } 6993 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); 6994 6995 void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) 6996 { 6997 if (WARN_ON_ONCE(rcu_access_pointer(perf_guest_cbs) != cbs)) 6998 return; 6999 7000 rcu_assign_pointer(perf_guest_cbs, NULL); 7001 static_call_update(__perf_guest_state, (void *)&__static_call_return0); 7002 static_call_update(__perf_guest_get_ip, (void *)&__static_call_return0); 7003 static_call_update(__perf_guest_handle_intel_pt_intr, 7004 (void *)&__static_call_return0); 7005 synchronize_rcu(); 7006 } 7007 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); 7008 #endif 7009 7010 static bool should_sample_guest(struct perf_event *event) 7011 { 7012 return !event->attr.exclude_guest && perf_guest_state(); 7013 } 7014 7015 unsigned long perf_misc_flags(struct perf_event *event, 7016 struct pt_regs *regs) 7017 { 7018 if (should_sample_guest(event)) 7019 return perf_arch_guest_misc_flags(regs); 7020 7021 return perf_arch_misc_flags(regs); 7022 } 7023 7024 unsigned long perf_instruction_pointer(struct perf_event *event, 7025 struct pt_regs *regs) 7026 { 7027 if (should_sample_guest(event)) 7028 return perf_guest_get_ip(); 7029 7030 return perf_arch_instruction_pointer(regs); 7031 } 7032 7033 static void 7034 perf_output_sample_regs(struct perf_output_handle *handle, 7035 struct pt_regs *regs, u64 mask) 7036 { 7037 int bit; 7038 DECLARE_BITMAP(_mask, 64); 7039 7040 bitmap_from_u64(_mask, mask); 7041 for_each_set_bit(bit, _mask, sizeof(mask) * BITS_PER_BYTE) { 7042 u64 val; 7043 7044 val = perf_reg_value(regs, bit); 7045 perf_output_put(handle, val); 7046 } 7047 } 7048 7049 static void perf_sample_regs_user(struct perf_regs *regs_user, 7050 struct pt_regs *regs) 7051 { 7052 if (user_mode(regs)) { 7053 regs_user->abi = perf_reg_abi(current); 7054 regs_user->regs = regs; 7055 } else if (!(current->flags & PF_KTHREAD)) { 7056 perf_get_regs_user(regs_user, regs); 7057 } else { 7058 regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE; 7059 regs_user->regs = NULL; 7060 } 7061 } 7062 7063 static void perf_sample_regs_intr(struct perf_regs *regs_intr, 7064 struct pt_regs *regs) 7065 { 7066 regs_intr->regs = regs; 7067 regs_intr->abi = perf_reg_abi(current); 7068 } 7069 7070 7071 /* 7072 * Get remaining task size from user stack pointer. 7073 * 7074 * It'd be better to take stack vma map and limit this more 7075 * precisely, but there's no way to get it safely under interrupt, 7076 * so using TASK_SIZE as limit. 7077 */ 7078 static u64 perf_ustack_task_size(struct pt_regs *regs) 7079 { 7080 unsigned long addr = perf_user_stack_pointer(regs); 7081 7082 if (!addr || addr >= TASK_SIZE) 7083 return 0; 7084 7085 return TASK_SIZE - addr; 7086 } 7087 7088 static u16 7089 perf_sample_ustack_size(u16 stack_size, u16 header_size, 7090 struct pt_regs *regs) 7091 { 7092 u64 task_size; 7093 7094 /* No regs, no stack pointer, no dump. */ 7095 if (!regs) 7096 return 0; 7097 7098 /* 7099 * Check if we fit in with the requested stack size into the: 7100 * - TASK_SIZE 7101 * If we don't, we limit the size to the TASK_SIZE. 7102 * 7103 * - remaining sample size 7104 * If we don't, we customize the stack size to 7105 * fit in to the remaining sample size. 7106 */ 7107 7108 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); 7109 stack_size = min(stack_size, (u16) task_size); 7110 7111 /* Current header size plus static size and dynamic size. */ 7112 header_size += 2 * sizeof(u64); 7113 7114 /* Do we fit in with the current stack dump size? */ 7115 if ((u16) (header_size + stack_size) < header_size) { 7116 /* 7117 * If we overflow the maximum size for the sample, 7118 * we customize the stack dump size to fit in. 7119 */ 7120 stack_size = USHRT_MAX - header_size - sizeof(u64); 7121 stack_size = round_up(stack_size, sizeof(u64)); 7122 } 7123 7124 return stack_size; 7125 } 7126 7127 static void 7128 perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, 7129 struct pt_regs *regs) 7130 { 7131 /* Case of a kernel thread, nothing to dump */ 7132 if (!regs) { 7133 u64 size = 0; 7134 perf_output_put(handle, size); 7135 } else { 7136 unsigned long sp; 7137 unsigned int rem; 7138 u64 dyn_size; 7139 7140 /* 7141 * We dump: 7142 * static size 7143 * - the size requested by user or the best one we can fit 7144 * in to the sample max size 7145 * data 7146 * - user stack dump data 7147 * dynamic size 7148 * - the actual dumped size 7149 */ 7150 7151 /* Static size. */ 7152 perf_output_put(handle, dump_size); 7153 7154 /* Data. */ 7155 sp = perf_user_stack_pointer(regs); 7156 rem = __output_copy_user(handle, (void *) sp, dump_size); 7157 dyn_size = dump_size - rem; 7158 7159 perf_output_skip(handle, rem); 7160 7161 /* Dynamic size. */ 7162 perf_output_put(handle, dyn_size); 7163 } 7164 } 7165 7166 static unsigned long perf_prepare_sample_aux(struct perf_event *event, 7167 struct perf_sample_data *data, 7168 size_t size) 7169 { 7170 struct perf_event *sampler = event->aux_event; 7171 struct perf_buffer *rb; 7172 7173 data->aux_size = 0; 7174 7175 if (!sampler) 7176 goto out; 7177 7178 if (WARN_ON_ONCE(READ_ONCE(sampler->state) != PERF_EVENT_STATE_ACTIVE)) 7179 goto out; 7180 7181 if (WARN_ON_ONCE(READ_ONCE(sampler->oncpu) != smp_processor_id())) 7182 goto out; 7183 7184 rb = ring_buffer_get(sampler); 7185 if (!rb) 7186 goto out; 7187 7188 /* 7189 * If this is an NMI hit inside sampling code, don't take 7190 * the sample. See also perf_aux_sample_output(). 7191 */ 7192 if (READ_ONCE(rb->aux_in_sampling)) { 7193 data->aux_size = 0; 7194 } else { 7195 size = min_t(size_t, size, perf_aux_size(rb)); 7196 data->aux_size = ALIGN(size, sizeof(u64)); 7197 } 7198 ring_buffer_put(rb); 7199 7200 out: 7201 return data->aux_size; 7202 } 7203 7204 static long perf_pmu_snapshot_aux(struct perf_buffer *rb, 7205 struct perf_event *event, 7206 struct perf_output_handle *handle, 7207 unsigned long size) 7208 { 7209 unsigned long flags; 7210 long ret; 7211 7212 /* 7213 * Normal ->start()/->stop() callbacks run in IRQ mode in scheduler 7214 * paths. If we start calling them in NMI context, they may race with 7215 * the IRQ ones, that is, for example, re-starting an event that's just 7216 * been stopped, which is why we're using a separate callback that 7217 * doesn't change the event state. 7218 * 7219 * IRQs need to be disabled to prevent IPIs from racing with us. 7220 */ 7221 local_irq_save(flags); 7222 /* 7223 * Guard against NMI hits inside the critical section; 7224 * see also perf_prepare_sample_aux(). 7225 */ 7226 WRITE_ONCE(rb->aux_in_sampling, 1); 7227 barrier(); 7228 7229 ret = event->pmu->snapshot_aux(event, handle, size); 7230 7231 barrier(); 7232 WRITE_ONCE(rb->aux_in_sampling, 0); 7233 local_irq_restore(flags); 7234 7235 return ret; 7236 } 7237 7238 static void perf_aux_sample_output(struct perf_event *event, 7239 struct perf_output_handle *handle, 7240 struct perf_sample_data *data) 7241 { 7242 struct perf_event *sampler = event->aux_event; 7243 struct perf_buffer *rb; 7244 unsigned long pad; 7245 long size; 7246 7247 if (WARN_ON_ONCE(!sampler || !data->aux_size)) 7248 return; 7249 7250 rb = ring_buffer_get(sampler); 7251 if (!rb) 7252 return; 7253 7254 size = perf_pmu_snapshot_aux(rb, sampler, handle, data->aux_size); 7255 7256 /* 7257 * An error here means that perf_output_copy() failed (returned a 7258 * non-zero surplus that it didn't copy), which in its current 7259 * enlightened implementation is not possible. If that changes, we'd 7260 * like to know. 7261 */ 7262 if (WARN_ON_ONCE(size < 0)) 7263 goto out_put; 7264 7265 /* 7266 * The pad comes from ALIGN()ing data->aux_size up to u64 in 7267 * perf_prepare_sample_aux(), so should not be more than that. 7268 */ 7269 pad = data->aux_size - size; 7270 if (WARN_ON_ONCE(pad >= sizeof(u64))) 7271 pad = 8; 7272 7273 if (pad) { 7274 u64 zero = 0; 7275 perf_output_copy(handle, &zero, pad); 7276 } 7277 7278 out_put: 7279 ring_buffer_put(rb); 7280 } 7281 7282 /* 7283 * A set of common sample data types saved even for non-sample records 7284 * when event->attr.sample_id_all is set. 7285 */ 7286 #define PERF_SAMPLE_ID_ALL (PERF_SAMPLE_TID | PERF_SAMPLE_TIME | \ 7287 PERF_SAMPLE_ID | PERF_SAMPLE_STREAM_ID | \ 7288 PERF_SAMPLE_CPU | PERF_SAMPLE_IDENTIFIER) 7289 7290 static void __perf_event_header__init_id(struct perf_sample_data *data, 7291 struct perf_event *event, 7292 u64 sample_type) 7293 { 7294 data->type = event->attr.sample_type; 7295 data->sample_flags |= data->type & PERF_SAMPLE_ID_ALL; 7296 7297 if (sample_type & PERF_SAMPLE_TID) { 7298 /* namespace issues */ 7299 data->tid_entry.pid = perf_event_pid(event, current); 7300 data->tid_entry.tid = perf_event_tid(event, current); 7301 } 7302 7303 if (sample_type & PERF_SAMPLE_TIME) 7304 data->time = perf_event_clock(event); 7305 7306 if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER)) 7307 data->id = primary_event_id(event); 7308 7309 if (sample_type & PERF_SAMPLE_STREAM_ID) 7310 data->stream_id = event->id; 7311 7312 if (sample_type & PERF_SAMPLE_CPU) { 7313 data->cpu_entry.cpu = raw_smp_processor_id(); 7314 data->cpu_entry.reserved = 0; 7315 } 7316 } 7317 7318 void perf_event_header__init_id(struct perf_event_header *header, 7319 struct perf_sample_data *data, 7320 struct perf_event *event) 7321 { 7322 if (event->attr.sample_id_all) { 7323 header->size += event->id_header_size; 7324 __perf_event_header__init_id(data, event, event->attr.sample_type); 7325 } 7326 } 7327 7328 static void __perf_event__output_id_sample(struct perf_output_handle *handle, 7329 struct perf_sample_data *data) 7330 { 7331 u64 sample_type = data->type; 7332 7333 if (sample_type & PERF_SAMPLE_TID) 7334 perf_output_put(handle, data->tid_entry); 7335 7336 if (sample_type & PERF_SAMPLE_TIME) 7337 perf_output_put(handle, data->time); 7338 7339 if (sample_type & PERF_SAMPLE_ID) 7340 perf_output_put(handle, data->id); 7341 7342 if (sample_type & PERF_SAMPLE_STREAM_ID) 7343 perf_output_put(handle, data->stream_id); 7344 7345 if (sample_type & PERF_SAMPLE_CPU) 7346 perf_output_put(handle, data->cpu_entry); 7347 7348 if (sample_type & PERF_SAMPLE_IDENTIFIER) 7349 perf_output_put(handle, data->id); 7350 } 7351 7352 void perf_event__output_id_sample(struct perf_event *event, 7353 struct perf_output_handle *handle, 7354 struct perf_sample_data *sample) 7355 { 7356 if (event->attr.sample_id_all) 7357 __perf_event__output_id_sample(handle, sample); 7358 } 7359 7360 static void perf_output_read_one(struct perf_output_handle *handle, 7361 struct perf_event *event, 7362 u64 enabled, u64 running) 7363 { 7364 u64 read_format = event->attr.read_format; 7365 u64 values[5]; 7366 int n = 0; 7367 7368 values[n++] = perf_event_count(event, has_inherit_and_sample_read(&event->attr)); 7369 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { 7370 values[n++] = enabled + 7371 atomic64_read(&event->child_total_time_enabled); 7372 } 7373 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { 7374 values[n++] = running + 7375 atomic64_read(&event->child_total_time_running); 7376 } 7377 if (read_format & PERF_FORMAT_ID) 7378 values[n++] = primary_event_id(event); 7379 if (read_format & PERF_FORMAT_LOST) 7380 values[n++] = atomic64_read(&event->lost_samples); 7381 7382 __output_copy(handle, values, n * sizeof(u64)); 7383 } 7384 7385 static void perf_output_read_group(struct perf_output_handle *handle, 7386 struct perf_event *event, 7387 u64 enabled, u64 running) 7388 { 7389 struct perf_event *leader = event->group_leader, *sub; 7390 u64 read_format = event->attr.read_format; 7391 unsigned long flags; 7392 u64 values[6]; 7393 int n = 0; 7394 bool self = has_inherit_and_sample_read(&event->attr); 7395 7396 /* 7397 * Disabling interrupts avoids all counter scheduling 7398 * (context switches, timer based rotation and IPIs). 7399 */ 7400 local_irq_save(flags); 7401 7402 values[n++] = 1 + leader->nr_siblings; 7403 7404 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 7405 values[n++] = enabled; 7406 7407 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 7408 values[n++] = running; 7409 7410 if ((leader != event) && 7411 (leader->state == PERF_EVENT_STATE_ACTIVE)) 7412 leader->pmu->read(leader); 7413 7414 values[n++] = perf_event_count(leader, self); 7415 if (read_format & PERF_FORMAT_ID) 7416 values[n++] = primary_event_id(leader); 7417 if (read_format & PERF_FORMAT_LOST) 7418 values[n++] = atomic64_read(&leader->lost_samples); 7419 7420 __output_copy(handle, values, n * sizeof(u64)); 7421 7422 for_each_sibling_event(sub, leader) { 7423 n = 0; 7424 7425 if ((sub != event) && 7426 (sub->state == PERF_EVENT_STATE_ACTIVE)) 7427 sub->pmu->read(sub); 7428 7429 values[n++] = perf_event_count(sub, self); 7430 if (read_format & PERF_FORMAT_ID) 7431 values[n++] = primary_event_id(sub); 7432 if (read_format & PERF_FORMAT_LOST) 7433 values[n++] = atomic64_read(&sub->lost_samples); 7434 7435 __output_copy(handle, values, n * sizeof(u64)); 7436 } 7437 7438 local_irq_restore(flags); 7439 } 7440 7441 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ 7442 PERF_FORMAT_TOTAL_TIME_RUNNING) 7443 7444 /* 7445 * XXX PERF_SAMPLE_READ vs inherited events seems difficult. 7446 * 7447 * The problem is that its both hard and excessively expensive to iterate the 7448 * child list, not to mention that its impossible to IPI the children running 7449 * on another CPU, from interrupt/NMI context. 7450 * 7451 * Instead the combination of PERF_SAMPLE_READ and inherit will track per-thread 7452 * counts rather than attempting to accumulate some value across all children on 7453 * all cores. 7454 */ 7455 static void perf_output_read(struct perf_output_handle *handle, 7456 struct perf_event *event) 7457 { 7458 u64 enabled = 0, running = 0, now; 7459 u64 read_format = event->attr.read_format; 7460 7461 /* 7462 * compute total_time_enabled, total_time_running 7463 * based on snapshot values taken when the event 7464 * was last scheduled in. 7465 * 7466 * we cannot simply called update_context_time() 7467 * because of locking issue as we are called in 7468 * NMI context 7469 */ 7470 if (read_format & PERF_FORMAT_TOTAL_TIMES) 7471 calc_timer_values(event, &now, &enabled, &running); 7472 7473 if (event->attr.read_format & PERF_FORMAT_GROUP) 7474 perf_output_read_group(handle, event, enabled, running); 7475 else 7476 perf_output_read_one(handle, event, enabled, running); 7477 } 7478 7479 void perf_output_sample(struct perf_output_handle *handle, 7480 struct perf_event_header *header, 7481 struct perf_sample_data *data, 7482 struct perf_event *event) 7483 { 7484 u64 sample_type = data->type; 7485 7486 perf_output_put(handle, *header); 7487 7488 if (sample_type & PERF_SAMPLE_IDENTIFIER) 7489 perf_output_put(handle, data->id); 7490 7491 if (sample_type & PERF_SAMPLE_IP) 7492 perf_output_put(handle, data->ip); 7493 7494 if (sample_type & PERF_SAMPLE_TID) 7495 perf_output_put(handle, data->tid_entry); 7496 7497 if (sample_type & PERF_SAMPLE_TIME) 7498 perf_output_put(handle, data->time); 7499 7500 if (sample_type & PERF_SAMPLE_ADDR) 7501 perf_output_put(handle, data->addr); 7502 7503 if (sample_type & PERF_SAMPLE_ID) 7504 perf_output_put(handle, data->id); 7505 7506 if (sample_type & PERF_SAMPLE_STREAM_ID) 7507 perf_output_put(handle, data->stream_id); 7508 7509 if (sample_type & PERF_SAMPLE_CPU) 7510 perf_output_put(handle, data->cpu_entry); 7511 7512 if (sample_type & PERF_SAMPLE_PERIOD) 7513 perf_output_put(handle, data->period); 7514 7515 if (sample_type & PERF_SAMPLE_READ) 7516 perf_output_read(handle, event); 7517 7518 if (sample_type & PERF_SAMPLE_CALLCHAIN) { 7519 int size = 1; 7520 7521 size += data->callchain->nr; 7522 size *= sizeof(u64); 7523 __output_copy(handle, data->callchain, size); 7524 } 7525 7526 if (sample_type & PERF_SAMPLE_RAW) { 7527 struct perf_raw_record *raw = data->raw; 7528 7529 if (raw) { 7530 struct perf_raw_frag *frag = &raw->frag; 7531 7532 perf_output_put(handle, raw->size); 7533 do { 7534 if (frag->copy) { 7535 __output_custom(handle, frag->copy, 7536 frag->data, frag->size); 7537 } else { 7538 __output_copy(handle, frag->data, 7539 frag->size); 7540 } 7541 if (perf_raw_frag_last(frag)) 7542 break; 7543 frag = frag->next; 7544 } while (1); 7545 if (frag->pad) 7546 __output_skip(handle, NULL, frag->pad); 7547 } else { 7548 struct { 7549 u32 size; 7550 u32 data; 7551 } raw = { 7552 .size = sizeof(u32), 7553 .data = 0, 7554 }; 7555 perf_output_put(handle, raw); 7556 } 7557 } 7558 7559 if (sample_type & PERF_SAMPLE_BRANCH_STACK) { 7560 if (data->br_stack) { 7561 size_t size; 7562 7563 size = data->br_stack->nr 7564 * sizeof(struct perf_branch_entry); 7565 7566 perf_output_put(handle, data->br_stack->nr); 7567 if (branch_sample_hw_index(event)) 7568 perf_output_put(handle, data->br_stack->hw_idx); 7569 perf_output_copy(handle, data->br_stack->entries, size); 7570 /* 7571 * Add the extension space which is appended 7572 * right after the struct perf_branch_stack. 7573 */ 7574 if (data->br_stack_cntr) { 7575 size = data->br_stack->nr * sizeof(u64); 7576 perf_output_copy(handle, data->br_stack_cntr, size); 7577 } 7578 } else { 7579 /* 7580 * we always store at least the value of nr 7581 */ 7582 u64 nr = 0; 7583 perf_output_put(handle, nr); 7584 } 7585 } 7586 7587 if (sample_type & PERF_SAMPLE_REGS_USER) { 7588 u64 abi = data->regs_user.abi; 7589 7590 /* 7591 * If there are no regs to dump, notice it through 7592 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). 7593 */ 7594 perf_output_put(handle, abi); 7595 7596 if (abi) { 7597 u64 mask = event->attr.sample_regs_user; 7598 perf_output_sample_regs(handle, 7599 data->regs_user.regs, 7600 mask); 7601 } 7602 } 7603 7604 if (sample_type & PERF_SAMPLE_STACK_USER) { 7605 perf_output_sample_ustack(handle, 7606 data->stack_user_size, 7607 data->regs_user.regs); 7608 } 7609 7610 if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) 7611 perf_output_put(handle, data->weight.full); 7612 7613 if (sample_type & PERF_SAMPLE_DATA_SRC) 7614 perf_output_put(handle, data->data_src.val); 7615 7616 if (sample_type & PERF_SAMPLE_TRANSACTION) 7617 perf_output_put(handle, data->txn); 7618 7619 if (sample_type & PERF_SAMPLE_REGS_INTR) { 7620 u64 abi = data->regs_intr.abi; 7621 /* 7622 * If there are no regs to dump, notice it through 7623 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). 7624 */ 7625 perf_output_put(handle, abi); 7626 7627 if (abi) { 7628 u64 mask = event->attr.sample_regs_intr; 7629 7630 perf_output_sample_regs(handle, 7631 data->regs_intr.regs, 7632 mask); 7633 } 7634 } 7635 7636 if (sample_type & PERF_SAMPLE_PHYS_ADDR) 7637 perf_output_put(handle, data->phys_addr); 7638 7639 if (sample_type & PERF_SAMPLE_CGROUP) 7640 perf_output_put(handle, data->cgroup); 7641 7642 if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) 7643 perf_output_put(handle, data->data_page_size); 7644 7645 if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) 7646 perf_output_put(handle, data->code_page_size); 7647 7648 if (sample_type & PERF_SAMPLE_AUX) { 7649 perf_output_put(handle, data->aux_size); 7650 7651 if (data->aux_size) 7652 perf_aux_sample_output(event, handle, data); 7653 } 7654 7655 if (!event->attr.watermark) { 7656 int wakeup_events = event->attr.wakeup_events; 7657 7658 if (wakeup_events) { 7659 struct perf_buffer *rb = handle->rb; 7660 int events = local_inc_return(&rb->events); 7661 7662 if (events >= wakeup_events) { 7663 local_sub(wakeup_events, &rb->events); 7664 local_inc(&rb->wakeup); 7665 } 7666 } 7667 } 7668 } 7669 7670 static u64 perf_virt_to_phys(u64 virt) 7671 { 7672 u64 phys_addr = 0; 7673 7674 if (!virt) 7675 return 0; 7676 7677 if (virt >= TASK_SIZE) { 7678 /* If it's vmalloc()d memory, leave phys_addr as 0 */ 7679 if (virt_addr_valid((void *)(uintptr_t)virt) && 7680 !(virt >= VMALLOC_START && virt < VMALLOC_END)) 7681 phys_addr = (u64)virt_to_phys((void *)(uintptr_t)virt); 7682 } else { 7683 /* 7684 * Walking the pages tables for user address. 7685 * Interrupts are disabled, so it prevents any tear down 7686 * of the page tables. 7687 * Try IRQ-safe get_user_page_fast_only first. 7688 * If failed, leave phys_addr as 0. 7689 */ 7690 if (current->mm != NULL) { 7691 struct page *p; 7692 7693 pagefault_disable(); 7694 if (get_user_page_fast_only(virt, 0, &p)) { 7695 phys_addr = page_to_phys(p) + virt % PAGE_SIZE; 7696 put_page(p); 7697 } 7698 pagefault_enable(); 7699 } 7700 } 7701 7702 return phys_addr; 7703 } 7704 7705 /* 7706 * Return the pagetable size of a given virtual address. 7707 */ 7708 static u64 perf_get_pgtable_size(struct mm_struct *mm, unsigned long addr) 7709 { 7710 u64 size = 0; 7711 7712 #ifdef CONFIG_HAVE_GUP_FAST 7713 pgd_t *pgdp, pgd; 7714 p4d_t *p4dp, p4d; 7715 pud_t *pudp, pud; 7716 pmd_t *pmdp, pmd; 7717 pte_t *ptep, pte; 7718 7719 pgdp = pgd_offset(mm, addr); 7720 pgd = READ_ONCE(*pgdp); 7721 if (pgd_none(pgd)) 7722 return 0; 7723 7724 if (pgd_leaf(pgd)) 7725 return pgd_leaf_size(pgd); 7726 7727 p4dp = p4d_offset_lockless(pgdp, pgd, addr); 7728 p4d = READ_ONCE(*p4dp); 7729 if (!p4d_present(p4d)) 7730 return 0; 7731 7732 if (p4d_leaf(p4d)) 7733 return p4d_leaf_size(p4d); 7734 7735 pudp = pud_offset_lockless(p4dp, p4d, addr); 7736 pud = READ_ONCE(*pudp); 7737 if (!pud_present(pud)) 7738 return 0; 7739 7740 if (pud_leaf(pud)) 7741 return pud_leaf_size(pud); 7742 7743 pmdp = pmd_offset_lockless(pudp, pud, addr); 7744 again: 7745 pmd = pmdp_get_lockless(pmdp); 7746 if (!pmd_present(pmd)) 7747 return 0; 7748 7749 if (pmd_leaf(pmd)) 7750 return pmd_leaf_size(pmd); 7751 7752 ptep = pte_offset_map(&pmd, addr); 7753 if (!ptep) 7754 goto again; 7755 7756 pte = ptep_get_lockless(ptep); 7757 if (pte_present(pte)) 7758 size = __pte_leaf_size(pmd, pte); 7759 pte_unmap(ptep); 7760 #endif /* CONFIG_HAVE_GUP_FAST */ 7761 7762 return size; 7763 } 7764 7765 static u64 perf_get_page_size(unsigned long addr) 7766 { 7767 struct mm_struct *mm; 7768 unsigned long flags; 7769 u64 size; 7770 7771 if (!addr) 7772 return 0; 7773 7774 /* 7775 * Software page-table walkers must disable IRQs, 7776 * which prevents any tear down of the page tables. 7777 */ 7778 local_irq_save(flags); 7779 7780 mm = current->mm; 7781 if (!mm) { 7782 /* 7783 * For kernel threads and the like, use init_mm so that 7784 * we can find kernel memory. 7785 */ 7786 mm = &init_mm; 7787 } 7788 7789 size = perf_get_pgtable_size(mm, addr); 7790 7791 local_irq_restore(flags); 7792 7793 return size; 7794 } 7795 7796 static struct perf_callchain_entry __empty_callchain = { .nr = 0, }; 7797 7798 struct perf_callchain_entry * 7799 perf_callchain(struct perf_event *event, struct pt_regs *regs) 7800 { 7801 bool kernel = !event->attr.exclude_callchain_kernel; 7802 bool user = !event->attr.exclude_callchain_user; 7803 /* Disallow cross-task user callchains. */ 7804 bool crosstask = event->ctx->task && event->ctx->task != current; 7805 const u32 max_stack = event->attr.sample_max_stack; 7806 struct perf_callchain_entry *callchain; 7807 7808 if (!kernel && !user) 7809 return &__empty_callchain; 7810 7811 callchain = get_perf_callchain(regs, 0, kernel, user, 7812 max_stack, crosstask, true); 7813 return callchain ?: &__empty_callchain; 7814 } 7815 7816 static __always_inline u64 __cond_set(u64 flags, u64 s, u64 d) 7817 { 7818 return d * !!(flags & s); 7819 } 7820 7821 void perf_prepare_sample(struct perf_sample_data *data, 7822 struct perf_event *event, 7823 struct pt_regs *regs) 7824 { 7825 u64 sample_type = event->attr.sample_type; 7826 u64 filtered_sample_type; 7827 7828 /* 7829 * Add the sample flags that are dependent to others. And clear the 7830 * sample flags that have already been done by the PMU driver. 7831 */ 7832 filtered_sample_type = sample_type; 7833 filtered_sample_type |= __cond_set(sample_type, PERF_SAMPLE_CODE_PAGE_SIZE, 7834 PERF_SAMPLE_IP); 7835 filtered_sample_type |= __cond_set(sample_type, PERF_SAMPLE_DATA_PAGE_SIZE | 7836 PERF_SAMPLE_PHYS_ADDR, PERF_SAMPLE_ADDR); 7837 filtered_sample_type |= __cond_set(sample_type, PERF_SAMPLE_STACK_USER, 7838 PERF_SAMPLE_REGS_USER); 7839 filtered_sample_type &= ~data->sample_flags; 7840 7841 if (filtered_sample_type == 0) { 7842 /* Make sure it has the correct data->type for output */ 7843 data->type = event->attr.sample_type; 7844 return; 7845 } 7846 7847 __perf_event_header__init_id(data, event, filtered_sample_type); 7848 7849 if (filtered_sample_type & PERF_SAMPLE_IP) { 7850 data->ip = perf_instruction_pointer(event, regs); 7851 data->sample_flags |= PERF_SAMPLE_IP; 7852 } 7853 7854 if (filtered_sample_type & PERF_SAMPLE_CALLCHAIN) 7855 perf_sample_save_callchain(data, event, regs); 7856 7857 if (filtered_sample_type & PERF_SAMPLE_RAW) { 7858 data->raw = NULL; 7859 data->dyn_size += sizeof(u64); 7860 data->sample_flags |= PERF_SAMPLE_RAW; 7861 } 7862 7863 if (filtered_sample_type & PERF_SAMPLE_BRANCH_STACK) { 7864 data->br_stack = NULL; 7865 data->dyn_size += sizeof(u64); 7866 data->sample_flags |= PERF_SAMPLE_BRANCH_STACK; 7867 } 7868 7869 if (filtered_sample_type & PERF_SAMPLE_REGS_USER) 7870 perf_sample_regs_user(&data->regs_user, regs); 7871 7872 /* 7873 * It cannot use the filtered_sample_type here as REGS_USER can be set 7874 * by STACK_USER (using __cond_set() above) and we don't want to update 7875 * the dyn_size if it's not requested by users. 7876 */ 7877 if ((sample_type & ~data->sample_flags) & PERF_SAMPLE_REGS_USER) { 7878 /* regs dump ABI info */ 7879 int size = sizeof(u64); 7880 7881 if (data->regs_user.regs) { 7882 u64 mask = event->attr.sample_regs_user; 7883 size += hweight64(mask) * sizeof(u64); 7884 } 7885 7886 data->dyn_size += size; 7887 data->sample_flags |= PERF_SAMPLE_REGS_USER; 7888 } 7889 7890 if (filtered_sample_type & PERF_SAMPLE_STACK_USER) { 7891 /* 7892 * Either we need PERF_SAMPLE_STACK_USER bit to be always 7893 * processed as the last one or have additional check added 7894 * in case new sample type is added, because we could eat 7895 * up the rest of the sample size. 7896 */ 7897 u16 stack_size = event->attr.sample_stack_user; 7898 u16 header_size = perf_sample_data_size(data, event); 7899 u16 size = sizeof(u64); 7900 7901 stack_size = perf_sample_ustack_size(stack_size, header_size, 7902 data->regs_user.regs); 7903 7904 /* 7905 * If there is something to dump, add space for the dump 7906 * itself and for the field that tells the dynamic size, 7907 * which is how many have been actually dumped. 7908 */ 7909 if (stack_size) 7910 size += sizeof(u64) + stack_size; 7911 7912 data->stack_user_size = stack_size; 7913 data->dyn_size += size; 7914 data->sample_flags |= PERF_SAMPLE_STACK_USER; 7915 } 7916 7917 if (filtered_sample_type & PERF_SAMPLE_WEIGHT_TYPE) { 7918 data->weight.full = 0; 7919 data->sample_flags |= PERF_SAMPLE_WEIGHT_TYPE; 7920 } 7921 7922 if (filtered_sample_type & PERF_SAMPLE_DATA_SRC) { 7923 data->data_src.val = PERF_MEM_NA; 7924 data->sample_flags |= PERF_SAMPLE_DATA_SRC; 7925 } 7926 7927 if (filtered_sample_type & PERF_SAMPLE_TRANSACTION) { 7928 data->txn = 0; 7929 data->sample_flags |= PERF_SAMPLE_TRANSACTION; 7930 } 7931 7932 if (filtered_sample_type & PERF_SAMPLE_ADDR) { 7933 data->addr = 0; 7934 data->sample_flags |= PERF_SAMPLE_ADDR; 7935 } 7936 7937 if (filtered_sample_type & PERF_SAMPLE_REGS_INTR) { 7938 /* regs dump ABI info */ 7939 int size = sizeof(u64); 7940 7941 perf_sample_regs_intr(&data->regs_intr, regs); 7942 7943 if (data->regs_intr.regs) { 7944 u64 mask = event->attr.sample_regs_intr; 7945 7946 size += hweight64(mask) * sizeof(u64); 7947 } 7948 7949 data->dyn_size += size; 7950 data->sample_flags |= PERF_SAMPLE_REGS_INTR; 7951 } 7952 7953 if (filtered_sample_type & PERF_SAMPLE_PHYS_ADDR) { 7954 data->phys_addr = perf_virt_to_phys(data->addr); 7955 data->sample_flags |= PERF_SAMPLE_PHYS_ADDR; 7956 } 7957 7958 #ifdef CONFIG_CGROUP_PERF 7959 if (filtered_sample_type & PERF_SAMPLE_CGROUP) { 7960 struct cgroup *cgrp; 7961 7962 /* protected by RCU */ 7963 cgrp = task_css_check(current, perf_event_cgrp_id, 1)->cgroup; 7964 data->cgroup = cgroup_id(cgrp); 7965 data->sample_flags |= PERF_SAMPLE_CGROUP; 7966 } 7967 #endif 7968 7969 /* 7970 * PERF_DATA_PAGE_SIZE requires PERF_SAMPLE_ADDR. If the user doesn't 7971 * require PERF_SAMPLE_ADDR, kernel implicitly retrieve the data->addr, 7972 * but the value will not dump to the userspace. 7973 */ 7974 if (filtered_sample_type & PERF_SAMPLE_DATA_PAGE_SIZE) { 7975 data->data_page_size = perf_get_page_size(data->addr); 7976 data->sample_flags |= PERF_SAMPLE_DATA_PAGE_SIZE; 7977 } 7978 7979 if (filtered_sample_type & PERF_SAMPLE_CODE_PAGE_SIZE) { 7980 data->code_page_size = perf_get_page_size(data->ip); 7981 data->sample_flags |= PERF_SAMPLE_CODE_PAGE_SIZE; 7982 } 7983 7984 if (filtered_sample_type & PERF_SAMPLE_AUX) { 7985 u64 size; 7986 u16 header_size = perf_sample_data_size(data, event); 7987 7988 header_size += sizeof(u64); /* size */ 7989 7990 /* 7991 * Given the 16bit nature of header::size, an AUX sample can 7992 * easily overflow it, what with all the preceding sample bits. 7993 * Make sure this doesn't happen by using up to U16_MAX bytes 7994 * per sample in total (rounded down to 8 byte boundary). 7995 */ 7996 size = min_t(size_t, U16_MAX - header_size, 7997 event->attr.aux_sample_size); 7998 size = rounddown(size, 8); 7999 size = perf_prepare_sample_aux(event, data, size); 8000 8001 WARN_ON_ONCE(size + header_size > U16_MAX); 8002 data->dyn_size += size + sizeof(u64); /* size above */ 8003 data->sample_flags |= PERF_SAMPLE_AUX; 8004 } 8005 } 8006 8007 void perf_prepare_header(struct perf_event_header *header, 8008 struct perf_sample_data *data, 8009 struct perf_event *event, 8010 struct pt_regs *regs) 8011 { 8012 header->type = PERF_RECORD_SAMPLE; 8013 header->size = perf_sample_data_size(data, event); 8014 header->misc = perf_misc_flags(event, regs); 8015 8016 /* 8017 * If you're adding more sample types here, you likely need to do 8018 * something about the overflowing header::size, like repurpose the 8019 * lowest 3 bits of size, which should be always zero at the moment. 8020 * This raises a more important question, do we really need 512k sized 8021 * samples and why, so good argumentation is in order for whatever you 8022 * do here next. 8023 */ 8024 WARN_ON_ONCE(header->size & 7); 8025 } 8026 8027 static void __perf_event_aux_pause(struct perf_event *event, bool pause) 8028 { 8029 if (pause) { 8030 if (!event->hw.aux_paused) { 8031 event->hw.aux_paused = 1; 8032 event->pmu->stop(event, PERF_EF_PAUSE); 8033 } 8034 } else { 8035 if (event->hw.aux_paused) { 8036 event->hw.aux_paused = 0; 8037 event->pmu->start(event, PERF_EF_RESUME); 8038 } 8039 } 8040 } 8041 8042 static void perf_event_aux_pause(struct perf_event *event, bool pause) 8043 { 8044 struct perf_buffer *rb; 8045 8046 if (WARN_ON_ONCE(!event)) 8047 return; 8048 8049 rb = ring_buffer_get(event); 8050 if (!rb) 8051 return; 8052 8053 scoped_guard (irqsave) { 8054 /* 8055 * Guard against self-recursion here. Another event could trip 8056 * this same from NMI context. 8057 */ 8058 if (READ_ONCE(rb->aux_in_pause_resume)) 8059 break; 8060 8061 WRITE_ONCE(rb->aux_in_pause_resume, 1); 8062 barrier(); 8063 __perf_event_aux_pause(event, pause); 8064 barrier(); 8065 WRITE_ONCE(rb->aux_in_pause_resume, 0); 8066 } 8067 ring_buffer_put(rb); 8068 } 8069 8070 static __always_inline int 8071 __perf_event_output(struct perf_event *event, 8072 struct perf_sample_data *data, 8073 struct pt_regs *regs, 8074 int (*output_begin)(struct perf_output_handle *, 8075 struct perf_sample_data *, 8076 struct perf_event *, 8077 unsigned int)) 8078 { 8079 struct perf_output_handle handle; 8080 struct perf_event_header header; 8081 int err; 8082 8083 /* protect the callchain buffers */ 8084 rcu_read_lock(); 8085 8086 perf_prepare_sample(data, event, regs); 8087 perf_prepare_header(&header, data, event, regs); 8088 8089 err = output_begin(&handle, data, event, header.size); 8090 if (err) 8091 goto exit; 8092 8093 perf_output_sample(&handle, &header, data, event); 8094 8095 perf_output_end(&handle); 8096 8097 exit: 8098 rcu_read_unlock(); 8099 return err; 8100 } 8101 8102 void 8103 perf_event_output_forward(struct perf_event *event, 8104 struct perf_sample_data *data, 8105 struct pt_regs *regs) 8106 { 8107 __perf_event_output(event, data, regs, perf_output_begin_forward); 8108 } 8109 8110 void 8111 perf_event_output_backward(struct perf_event *event, 8112 struct perf_sample_data *data, 8113 struct pt_regs *regs) 8114 { 8115 __perf_event_output(event, data, regs, perf_output_begin_backward); 8116 } 8117 8118 int 8119 perf_event_output(struct perf_event *event, 8120 struct perf_sample_data *data, 8121 struct pt_regs *regs) 8122 { 8123 return __perf_event_output(event, data, regs, perf_output_begin); 8124 } 8125 8126 /* 8127 * read event_id 8128 */ 8129 8130 struct perf_read_event { 8131 struct perf_event_header header; 8132 8133 u32 pid; 8134 u32 tid; 8135 }; 8136 8137 static void 8138 perf_event_read_event(struct perf_event *event, 8139 struct task_struct *task) 8140 { 8141 struct perf_output_handle handle; 8142 struct perf_sample_data sample; 8143 struct perf_read_event read_event = { 8144 .header = { 8145 .type = PERF_RECORD_READ, 8146 .misc = 0, 8147 .size = sizeof(read_event) + event->read_size, 8148 }, 8149 .pid = perf_event_pid(event, task), 8150 .tid = perf_event_tid(event, task), 8151 }; 8152 int ret; 8153 8154 perf_event_header__init_id(&read_event.header, &sample, event); 8155 ret = perf_output_begin(&handle, &sample, event, read_event.header.size); 8156 if (ret) 8157 return; 8158 8159 perf_output_put(&handle, read_event); 8160 perf_output_read(&handle, event); 8161 perf_event__output_id_sample(event, &handle, &sample); 8162 8163 perf_output_end(&handle); 8164 } 8165 8166 typedef void (perf_iterate_f)(struct perf_event *event, void *data); 8167 8168 static void 8169 perf_iterate_ctx(struct perf_event_context *ctx, 8170 perf_iterate_f output, 8171 void *data, bool all) 8172 { 8173 struct perf_event *event; 8174 8175 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { 8176 if (!all) { 8177 if (event->state < PERF_EVENT_STATE_INACTIVE) 8178 continue; 8179 if (!event_filter_match(event)) 8180 continue; 8181 } 8182 8183 output(event, data); 8184 } 8185 } 8186 8187 static void perf_iterate_sb_cpu(perf_iterate_f output, void *data) 8188 { 8189 struct pmu_event_list *pel = this_cpu_ptr(&pmu_sb_events); 8190 struct perf_event *event; 8191 8192 list_for_each_entry_rcu(event, &pel->list, sb_list) { 8193 /* 8194 * Skip events that are not fully formed yet; ensure that 8195 * if we observe event->ctx, both event and ctx will be 8196 * complete enough. See perf_install_in_context(). 8197 */ 8198 if (!smp_load_acquire(&event->ctx)) 8199 continue; 8200 8201 if (event->state < PERF_EVENT_STATE_INACTIVE) 8202 continue; 8203 if (!event_filter_match(event)) 8204 continue; 8205 output(event, data); 8206 } 8207 } 8208 8209 /* 8210 * Iterate all events that need to receive side-band events. 8211 * 8212 * For new callers; ensure that account_pmu_sb_event() includes 8213 * your event, otherwise it might not get delivered. 8214 */ 8215 static void 8216 perf_iterate_sb(perf_iterate_f output, void *data, 8217 struct perf_event_context *task_ctx) 8218 { 8219 struct perf_event_context *ctx; 8220 8221 rcu_read_lock(); 8222 preempt_disable(); 8223 8224 /* 8225 * If we have task_ctx != NULL we only notify the task context itself. 8226 * The task_ctx is set only for EXIT events before releasing task 8227 * context. 8228 */ 8229 if (task_ctx) { 8230 perf_iterate_ctx(task_ctx, output, data, false); 8231 goto done; 8232 } 8233 8234 perf_iterate_sb_cpu(output, data); 8235 8236 ctx = rcu_dereference(current->perf_event_ctxp); 8237 if (ctx) 8238 perf_iterate_ctx(ctx, output, data, false); 8239 done: 8240 preempt_enable(); 8241 rcu_read_unlock(); 8242 } 8243 8244 /* 8245 * Clear all file-based filters at exec, they'll have to be 8246 * re-instated when/if these objects are mmapped again. 8247 */ 8248 static void perf_event_addr_filters_exec(struct perf_event *event, void *data) 8249 { 8250 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); 8251 struct perf_addr_filter *filter; 8252 unsigned int restart = 0, count = 0; 8253 unsigned long flags; 8254 8255 if (!has_addr_filter(event)) 8256 return; 8257 8258 raw_spin_lock_irqsave(&ifh->lock, flags); 8259 list_for_each_entry(filter, &ifh->list, entry) { 8260 if (filter->path.dentry) { 8261 event->addr_filter_ranges[count].start = 0; 8262 event->addr_filter_ranges[count].size = 0; 8263 restart++; 8264 } 8265 8266 count++; 8267 } 8268 8269 if (restart) 8270 event->addr_filters_gen++; 8271 raw_spin_unlock_irqrestore(&ifh->lock, flags); 8272 8273 if (restart) 8274 perf_event_stop(event, 1); 8275 } 8276 8277 void perf_event_exec(void) 8278 { 8279 struct perf_event_context *ctx; 8280 8281 ctx = perf_pin_task_context(current); 8282 if (!ctx) 8283 return; 8284 8285 perf_event_enable_on_exec(ctx); 8286 perf_event_remove_on_exec(ctx); 8287 perf_iterate_ctx(ctx, perf_event_addr_filters_exec, NULL, true); 8288 8289 perf_unpin_context(ctx); 8290 put_ctx(ctx); 8291 } 8292 8293 struct remote_output { 8294 struct perf_buffer *rb; 8295 int err; 8296 }; 8297 8298 static void __perf_event_output_stop(struct perf_event *event, void *data) 8299 { 8300 struct perf_event *parent = event->parent; 8301 struct remote_output *ro = data; 8302 struct perf_buffer *rb = ro->rb; 8303 struct stop_event_data sd = { 8304 .event = event, 8305 }; 8306 8307 if (!has_aux(event)) 8308 return; 8309 8310 if (!parent) 8311 parent = event; 8312 8313 /* 8314 * In case of inheritance, it will be the parent that links to the 8315 * ring-buffer, but it will be the child that's actually using it. 8316 * 8317 * We are using event::rb to determine if the event should be stopped, 8318 * however this may race with ring_buffer_attach() (through set_output), 8319 * which will make us skip the event that actually needs to be stopped. 8320 * So ring_buffer_attach() has to stop an aux event before re-assigning 8321 * its rb pointer. 8322 */ 8323 if (rcu_dereference(parent->rb) == rb) 8324 ro->err = __perf_event_stop(&sd); 8325 } 8326 8327 static int __perf_pmu_output_stop(void *info) 8328 { 8329 struct perf_event *event = info; 8330 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 8331 struct remote_output ro = { 8332 .rb = event->rb, 8333 }; 8334 8335 rcu_read_lock(); 8336 perf_iterate_ctx(&cpuctx->ctx, __perf_event_output_stop, &ro, false); 8337 if (cpuctx->task_ctx) 8338 perf_iterate_ctx(cpuctx->task_ctx, __perf_event_output_stop, 8339 &ro, false); 8340 rcu_read_unlock(); 8341 8342 return ro.err; 8343 } 8344 8345 static void perf_pmu_output_stop(struct perf_event *event) 8346 { 8347 struct perf_event *iter; 8348 int err, cpu; 8349 8350 restart: 8351 rcu_read_lock(); 8352 list_for_each_entry_rcu(iter, &event->rb->event_list, rb_entry) { 8353 /* 8354 * For per-CPU events, we need to make sure that neither they 8355 * nor their children are running; for cpu==-1 events it's 8356 * sufficient to stop the event itself if it's active, since 8357 * it can't have children. 8358 */ 8359 cpu = iter->cpu; 8360 if (cpu == -1) 8361 cpu = READ_ONCE(iter->oncpu); 8362 8363 if (cpu == -1) 8364 continue; 8365 8366 err = cpu_function_call(cpu, __perf_pmu_output_stop, event); 8367 if (err == -EAGAIN) { 8368 rcu_read_unlock(); 8369 goto restart; 8370 } 8371 } 8372 rcu_read_unlock(); 8373 } 8374 8375 /* 8376 * task tracking -- fork/exit 8377 * 8378 * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task 8379 */ 8380 8381 struct perf_task_event { 8382 struct task_struct *task; 8383 struct perf_event_context *task_ctx; 8384 8385 struct { 8386 struct perf_event_header header; 8387 8388 u32 pid; 8389 u32 ppid; 8390 u32 tid; 8391 u32 ptid; 8392 u64 time; 8393 } event_id; 8394 }; 8395 8396 static int perf_event_task_match(struct perf_event *event) 8397 { 8398 return event->attr.comm || event->attr.mmap || 8399 event->attr.mmap2 || event->attr.mmap_data || 8400 event->attr.task; 8401 } 8402 8403 static void perf_event_task_output(struct perf_event *event, 8404 void *data) 8405 { 8406 struct perf_task_event *task_event = data; 8407 struct perf_output_handle handle; 8408 struct perf_sample_data sample; 8409 struct task_struct *task = task_event->task; 8410 int ret, size = task_event->event_id.header.size; 8411 8412 if (!perf_event_task_match(event)) 8413 return; 8414 8415 perf_event_header__init_id(&task_event->event_id.header, &sample, event); 8416 8417 ret = perf_output_begin(&handle, &sample, event, 8418 task_event->event_id.header.size); 8419 if (ret) 8420 goto out; 8421 8422 task_event->event_id.pid = perf_event_pid(event, task); 8423 task_event->event_id.tid = perf_event_tid(event, task); 8424 8425 if (task_event->event_id.header.type == PERF_RECORD_EXIT) { 8426 task_event->event_id.ppid = perf_event_pid(event, 8427 task->real_parent); 8428 task_event->event_id.ptid = perf_event_pid(event, 8429 task->real_parent); 8430 } else { /* PERF_RECORD_FORK */ 8431 task_event->event_id.ppid = perf_event_pid(event, current); 8432 task_event->event_id.ptid = perf_event_tid(event, current); 8433 } 8434 8435 task_event->event_id.time = perf_event_clock(event); 8436 8437 perf_output_put(&handle, task_event->event_id); 8438 8439 perf_event__output_id_sample(event, &handle, &sample); 8440 8441 perf_output_end(&handle); 8442 out: 8443 task_event->event_id.header.size = size; 8444 } 8445 8446 static void perf_event_task(struct task_struct *task, 8447 struct perf_event_context *task_ctx, 8448 int new) 8449 { 8450 struct perf_task_event task_event; 8451 8452 if (!atomic_read(&nr_comm_events) && 8453 !atomic_read(&nr_mmap_events) && 8454 !atomic_read(&nr_task_events)) 8455 return; 8456 8457 task_event = (struct perf_task_event){ 8458 .task = task, 8459 .task_ctx = task_ctx, 8460 .event_id = { 8461 .header = { 8462 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, 8463 .misc = 0, 8464 .size = sizeof(task_event.event_id), 8465 }, 8466 /* .pid */ 8467 /* .ppid */ 8468 /* .tid */ 8469 /* .ptid */ 8470 /* .time */ 8471 }, 8472 }; 8473 8474 perf_iterate_sb(perf_event_task_output, 8475 &task_event, 8476 task_ctx); 8477 } 8478 8479 void perf_event_fork(struct task_struct *task) 8480 { 8481 perf_event_task(task, NULL, 1); 8482 perf_event_namespaces(task); 8483 } 8484 8485 /* 8486 * comm tracking 8487 */ 8488 8489 struct perf_comm_event { 8490 struct task_struct *task; 8491 char *comm; 8492 int comm_size; 8493 8494 struct { 8495 struct perf_event_header header; 8496 8497 u32 pid; 8498 u32 tid; 8499 } event_id; 8500 }; 8501 8502 static int perf_event_comm_match(struct perf_event *event) 8503 { 8504 return event->attr.comm; 8505 } 8506 8507 static void perf_event_comm_output(struct perf_event *event, 8508 void *data) 8509 { 8510 struct perf_comm_event *comm_event = data; 8511 struct perf_output_handle handle; 8512 struct perf_sample_data sample; 8513 int size = comm_event->event_id.header.size; 8514 int ret; 8515 8516 if (!perf_event_comm_match(event)) 8517 return; 8518 8519 perf_event_header__init_id(&comm_event->event_id.header, &sample, event); 8520 ret = perf_output_begin(&handle, &sample, event, 8521 comm_event->event_id.header.size); 8522 8523 if (ret) 8524 goto out; 8525 8526 comm_event->event_id.pid = perf_event_pid(event, comm_event->task); 8527 comm_event->event_id.tid = perf_event_tid(event, comm_event->task); 8528 8529 perf_output_put(&handle, comm_event->event_id); 8530 __output_copy(&handle, comm_event->comm, 8531 comm_event->comm_size); 8532 8533 perf_event__output_id_sample(event, &handle, &sample); 8534 8535 perf_output_end(&handle); 8536 out: 8537 comm_event->event_id.header.size = size; 8538 } 8539 8540 static void perf_event_comm_event(struct perf_comm_event *comm_event) 8541 { 8542 char comm[TASK_COMM_LEN]; 8543 unsigned int size; 8544 8545 memset(comm, 0, sizeof(comm)); 8546 strscpy(comm, comm_event->task->comm, sizeof(comm)); 8547 size = ALIGN(strlen(comm)+1, sizeof(u64)); 8548 8549 comm_event->comm = comm; 8550 comm_event->comm_size = size; 8551 8552 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; 8553 8554 perf_iterate_sb(perf_event_comm_output, 8555 comm_event, 8556 NULL); 8557 } 8558 8559 void perf_event_comm(struct task_struct *task, bool exec) 8560 { 8561 struct perf_comm_event comm_event; 8562 8563 if (!atomic_read(&nr_comm_events)) 8564 return; 8565 8566 comm_event = (struct perf_comm_event){ 8567 .task = task, 8568 /* .comm */ 8569 /* .comm_size */ 8570 .event_id = { 8571 .header = { 8572 .type = PERF_RECORD_COMM, 8573 .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0, 8574 /* .size */ 8575 }, 8576 /* .pid */ 8577 /* .tid */ 8578 }, 8579 }; 8580 8581 perf_event_comm_event(&comm_event); 8582 } 8583 8584 /* 8585 * namespaces tracking 8586 */ 8587 8588 struct perf_namespaces_event { 8589 struct task_struct *task; 8590 8591 struct { 8592 struct perf_event_header header; 8593 8594 u32 pid; 8595 u32 tid; 8596 u64 nr_namespaces; 8597 struct perf_ns_link_info link_info[NR_NAMESPACES]; 8598 } event_id; 8599 }; 8600 8601 static int perf_event_namespaces_match(struct perf_event *event) 8602 { 8603 return event->attr.namespaces; 8604 } 8605 8606 static void perf_event_namespaces_output(struct perf_event *event, 8607 void *data) 8608 { 8609 struct perf_namespaces_event *namespaces_event = data; 8610 struct perf_output_handle handle; 8611 struct perf_sample_data sample; 8612 u16 header_size = namespaces_event->event_id.header.size; 8613 int ret; 8614 8615 if (!perf_event_namespaces_match(event)) 8616 return; 8617 8618 perf_event_header__init_id(&namespaces_event->event_id.header, 8619 &sample, event); 8620 ret = perf_output_begin(&handle, &sample, event, 8621 namespaces_event->event_id.header.size); 8622 if (ret) 8623 goto out; 8624 8625 namespaces_event->event_id.pid = perf_event_pid(event, 8626 namespaces_event->task); 8627 namespaces_event->event_id.tid = perf_event_tid(event, 8628 namespaces_event->task); 8629 8630 perf_output_put(&handle, namespaces_event->event_id); 8631 8632 perf_event__output_id_sample(event, &handle, &sample); 8633 8634 perf_output_end(&handle); 8635 out: 8636 namespaces_event->event_id.header.size = header_size; 8637 } 8638 8639 static void perf_fill_ns_link_info(struct perf_ns_link_info *ns_link_info, 8640 struct task_struct *task, 8641 const struct proc_ns_operations *ns_ops) 8642 { 8643 struct path ns_path; 8644 struct inode *ns_inode; 8645 int error; 8646 8647 error = ns_get_path(&ns_path, task, ns_ops); 8648 if (!error) { 8649 ns_inode = ns_path.dentry->d_inode; 8650 ns_link_info->dev = new_encode_dev(ns_inode->i_sb->s_dev); 8651 ns_link_info->ino = ns_inode->i_ino; 8652 path_put(&ns_path); 8653 } 8654 } 8655 8656 void perf_event_namespaces(struct task_struct *task) 8657 { 8658 struct perf_namespaces_event namespaces_event; 8659 struct perf_ns_link_info *ns_link_info; 8660 8661 if (!atomic_read(&nr_namespaces_events)) 8662 return; 8663 8664 namespaces_event = (struct perf_namespaces_event){ 8665 .task = task, 8666 .event_id = { 8667 .header = { 8668 .type = PERF_RECORD_NAMESPACES, 8669 .misc = 0, 8670 .size = sizeof(namespaces_event.event_id), 8671 }, 8672 /* .pid */ 8673 /* .tid */ 8674 .nr_namespaces = NR_NAMESPACES, 8675 /* .link_info[NR_NAMESPACES] */ 8676 }, 8677 }; 8678 8679 ns_link_info = namespaces_event.event_id.link_info; 8680 8681 perf_fill_ns_link_info(&ns_link_info[MNT_NS_INDEX], 8682 task, &mntns_operations); 8683 8684 #ifdef CONFIG_USER_NS 8685 perf_fill_ns_link_info(&ns_link_info[USER_NS_INDEX], 8686 task, &userns_operations); 8687 #endif 8688 #ifdef CONFIG_NET_NS 8689 perf_fill_ns_link_info(&ns_link_info[NET_NS_INDEX], 8690 task, &netns_operations); 8691 #endif 8692 #ifdef CONFIG_UTS_NS 8693 perf_fill_ns_link_info(&ns_link_info[UTS_NS_INDEX], 8694 task, &utsns_operations); 8695 #endif 8696 #ifdef CONFIG_IPC_NS 8697 perf_fill_ns_link_info(&ns_link_info[IPC_NS_INDEX], 8698 task, &ipcns_operations); 8699 #endif 8700 #ifdef CONFIG_PID_NS 8701 perf_fill_ns_link_info(&ns_link_info[PID_NS_INDEX], 8702 task, &pidns_operations); 8703 #endif 8704 #ifdef CONFIG_CGROUPS 8705 perf_fill_ns_link_info(&ns_link_info[CGROUP_NS_INDEX], 8706 task, &cgroupns_operations); 8707 #endif 8708 8709 perf_iterate_sb(perf_event_namespaces_output, 8710 &namespaces_event, 8711 NULL); 8712 } 8713 8714 /* 8715 * cgroup tracking 8716 */ 8717 #ifdef CONFIG_CGROUP_PERF 8718 8719 struct perf_cgroup_event { 8720 char *path; 8721 int path_size; 8722 struct { 8723 struct perf_event_header header; 8724 u64 id; 8725 char path[]; 8726 } event_id; 8727 }; 8728 8729 static int perf_event_cgroup_match(struct perf_event *event) 8730 { 8731 return event->attr.cgroup; 8732 } 8733 8734 static void perf_event_cgroup_output(struct perf_event *event, void *data) 8735 { 8736 struct perf_cgroup_event *cgroup_event = data; 8737 struct perf_output_handle handle; 8738 struct perf_sample_data sample; 8739 u16 header_size = cgroup_event->event_id.header.size; 8740 int ret; 8741 8742 if (!perf_event_cgroup_match(event)) 8743 return; 8744 8745 perf_event_header__init_id(&cgroup_event->event_id.header, 8746 &sample, event); 8747 ret = perf_output_begin(&handle, &sample, event, 8748 cgroup_event->event_id.header.size); 8749 if (ret) 8750 goto out; 8751 8752 perf_output_put(&handle, cgroup_event->event_id); 8753 __output_copy(&handle, cgroup_event->path, cgroup_event->path_size); 8754 8755 perf_event__output_id_sample(event, &handle, &sample); 8756 8757 perf_output_end(&handle); 8758 out: 8759 cgroup_event->event_id.header.size = header_size; 8760 } 8761 8762 static void perf_event_cgroup(struct cgroup *cgrp) 8763 { 8764 struct perf_cgroup_event cgroup_event; 8765 char path_enomem[16] = "//enomem"; 8766 char *pathname; 8767 size_t size; 8768 8769 if (!atomic_read(&nr_cgroup_events)) 8770 return; 8771 8772 cgroup_event = (struct perf_cgroup_event){ 8773 .event_id = { 8774 .header = { 8775 .type = PERF_RECORD_CGROUP, 8776 .misc = 0, 8777 .size = sizeof(cgroup_event.event_id), 8778 }, 8779 .id = cgroup_id(cgrp), 8780 }, 8781 }; 8782 8783 pathname = kmalloc(PATH_MAX, GFP_KERNEL); 8784 if (pathname == NULL) { 8785 cgroup_event.path = path_enomem; 8786 } else { 8787 /* just to be sure to have enough space for alignment */ 8788 cgroup_path(cgrp, pathname, PATH_MAX - sizeof(u64)); 8789 cgroup_event.path = pathname; 8790 } 8791 8792 /* 8793 * Since our buffer works in 8 byte units we need to align our string 8794 * size to a multiple of 8. However, we must guarantee the tail end is 8795 * zero'd out to avoid leaking random bits to userspace. 8796 */ 8797 size = strlen(cgroup_event.path) + 1; 8798 while (!IS_ALIGNED(size, sizeof(u64))) 8799 cgroup_event.path[size++] = '\0'; 8800 8801 cgroup_event.event_id.header.size += size; 8802 cgroup_event.path_size = size; 8803 8804 perf_iterate_sb(perf_event_cgroup_output, 8805 &cgroup_event, 8806 NULL); 8807 8808 kfree(pathname); 8809 } 8810 8811 #endif 8812 8813 /* 8814 * mmap tracking 8815 */ 8816 8817 struct perf_mmap_event { 8818 struct vm_area_struct *vma; 8819 8820 const char *file_name; 8821 int file_size; 8822 int maj, min; 8823 u64 ino; 8824 u64 ino_generation; 8825 u32 prot, flags; 8826 u8 build_id[BUILD_ID_SIZE_MAX]; 8827 u32 build_id_size; 8828 8829 struct { 8830 struct perf_event_header header; 8831 8832 u32 pid; 8833 u32 tid; 8834 u64 start; 8835 u64 len; 8836 u64 pgoff; 8837 } event_id; 8838 }; 8839 8840 static int perf_event_mmap_match(struct perf_event *event, 8841 void *data) 8842 { 8843 struct perf_mmap_event *mmap_event = data; 8844 struct vm_area_struct *vma = mmap_event->vma; 8845 int executable = vma->vm_flags & VM_EXEC; 8846 8847 return (!executable && event->attr.mmap_data) || 8848 (executable && (event->attr.mmap || event->attr.mmap2)); 8849 } 8850 8851 static void perf_event_mmap_output(struct perf_event *event, 8852 void *data) 8853 { 8854 struct perf_mmap_event *mmap_event = data; 8855 struct perf_output_handle handle; 8856 struct perf_sample_data sample; 8857 int size = mmap_event->event_id.header.size; 8858 u32 type = mmap_event->event_id.header.type; 8859 bool use_build_id; 8860 int ret; 8861 8862 if (!perf_event_mmap_match(event, data)) 8863 return; 8864 8865 if (event->attr.mmap2) { 8866 mmap_event->event_id.header.type = PERF_RECORD_MMAP2; 8867 mmap_event->event_id.header.size += sizeof(mmap_event->maj); 8868 mmap_event->event_id.header.size += sizeof(mmap_event->min); 8869 mmap_event->event_id.header.size += sizeof(mmap_event->ino); 8870 mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation); 8871 mmap_event->event_id.header.size += sizeof(mmap_event->prot); 8872 mmap_event->event_id.header.size += sizeof(mmap_event->flags); 8873 } 8874 8875 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); 8876 ret = perf_output_begin(&handle, &sample, event, 8877 mmap_event->event_id.header.size); 8878 if (ret) 8879 goto out; 8880 8881 mmap_event->event_id.pid = perf_event_pid(event, current); 8882 mmap_event->event_id.tid = perf_event_tid(event, current); 8883 8884 use_build_id = event->attr.build_id && mmap_event->build_id_size; 8885 8886 if (event->attr.mmap2 && use_build_id) 8887 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_BUILD_ID; 8888 8889 perf_output_put(&handle, mmap_event->event_id); 8890 8891 if (event->attr.mmap2) { 8892 if (use_build_id) { 8893 u8 size[4] = { (u8) mmap_event->build_id_size, 0, 0, 0 }; 8894 8895 __output_copy(&handle, size, 4); 8896 __output_copy(&handle, mmap_event->build_id, BUILD_ID_SIZE_MAX); 8897 } else { 8898 perf_output_put(&handle, mmap_event->maj); 8899 perf_output_put(&handle, mmap_event->min); 8900 perf_output_put(&handle, mmap_event->ino); 8901 perf_output_put(&handle, mmap_event->ino_generation); 8902 } 8903 perf_output_put(&handle, mmap_event->prot); 8904 perf_output_put(&handle, mmap_event->flags); 8905 } 8906 8907 __output_copy(&handle, mmap_event->file_name, 8908 mmap_event->file_size); 8909 8910 perf_event__output_id_sample(event, &handle, &sample); 8911 8912 perf_output_end(&handle); 8913 out: 8914 mmap_event->event_id.header.size = size; 8915 mmap_event->event_id.header.type = type; 8916 } 8917 8918 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) 8919 { 8920 struct vm_area_struct *vma = mmap_event->vma; 8921 struct file *file = vma->vm_file; 8922 int maj = 0, min = 0; 8923 u64 ino = 0, gen = 0; 8924 u32 prot = 0, flags = 0; 8925 unsigned int size; 8926 char tmp[16]; 8927 char *buf = NULL; 8928 char *name = NULL; 8929 8930 if (vma->vm_flags & VM_READ) 8931 prot |= PROT_READ; 8932 if (vma->vm_flags & VM_WRITE) 8933 prot |= PROT_WRITE; 8934 if (vma->vm_flags & VM_EXEC) 8935 prot |= PROT_EXEC; 8936 8937 if (vma->vm_flags & VM_MAYSHARE) 8938 flags = MAP_SHARED; 8939 else 8940 flags = MAP_PRIVATE; 8941 8942 if (vma->vm_flags & VM_LOCKED) 8943 flags |= MAP_LOCKED; 8944 if (is_vm_hugetlb_page(vma)) 8945 flags |= MAP_HUGETLB; 8946 8947 if (file) { 8948 struct inode *inode; 8949 dev_t dev; 8950 8951 buf = kmalloc(PATH_MAX, GFP_KERNEL); 8952 if (!buf) { 8953 name = "//enomem"; 8954 goto cpy_name; 8955 } 8956 /* 8957 * d_path() works from the end of the rb backwards, so we 8958 * need to add enough zero bytes after the string to handle 8959 * the 64bit alignment we do later. 8960 */ 8961 name = file_path(file, buf, PATH_MAX - sizeof(u64)); 8962 if (IS_ERR(name)) { 8963 name = "//toolong"; 8964 goto cpy_name; 8965 } 8966 inode = file_inode(vma->vm_file); 8967 dev = inode->i_sb->s_dev; 8968 ino = inode->i_ino; 8969 gen = inode->i_generation; 8970 maj = MAJOR(dev); 8971 min = MINOR(dev); 8972 8973 goto got_name; 8974 } else { 8975 if (vma->vm_ops && vma->vm_ops->name) 8976 name = (char *) vma->vm_ops->name(vma); 8977 if (!name) 8978 name = (char *)arch_vma_name(vma); 8979 if (!name) { 8980 if (vma_is_initial_heap(vma)) 8981 name = "[heap]"; 8982 else if (vma_is_initial_stack(vma)) 8983 name = "[stack]"; 8984 else 8985 name = "//anon"; 8986 } 8987 } 8988 8989 cpy_name: 8990 strscpy(tmp, name, sizeof(tmp)); 8991 name = tmp; 8992 got_name: 8993 /* 8994 * Since our buffer works in 8 byte units we need to align our string 8995 * size to a multiple of 8. However, we must guarantee the tail end is 8996 * zero'd out to avoid leaking random bits to userspace. 8997 */ 8998 size = strlen(name)+1; 8999 while (!IS_ALIGNED(size, sizeof(u64))) 9000 name[size++] = '\0'; 9001 9002 mmap_event->file_name = name; 9003 mmap_event->file_size = size; 9004 mmap_event->maj = maj; 9005 mmap_event->min = min; 9006 mmap_event->ino = ino; 9007 mmap_event->ino_generation = gen; 9008 mmap_event->prot = prot; 9009 mmap_event->flags = flags; 9010 9011 if (!(vma->vm_flags & VM_EXEC)) 9012 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; 9013 9014 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; 9015 9016 if (atomic_read(&nr_build_id_events)) 9017 build_id_parse_nofault(vma, mmap_event->build_id, &mmap_event->build_id_size); 9018 9019 perf_iterate_sb(perf_event_mmap_output, 9020 mmap_event, 9021 NULL); 9022 9023 kfree(buf); 9024 } 9025 9026 /* 9027 * Check whether inode and address range match filter criteria. 9028 */ 9029 static bool perf_addr_filter_match(struct perf_addr_filter *filter, 9030 struct file *file, unsigned long offset, 9031 unsigned long size) 9032 { 9033 /* d_inode(NULL) won't be equal to any mapped user-space file */ 9034 if (!filter->path.dentry) 9035 return false; 9036 9037 if (d_inode(filter->path.dentry) != file_inode(file)) 9038 return false; 9039 9040 if (filter->offset > offset + size) 9041 return false; 9042 9043 if (filter->offset + filter->size < offset) 9044 return false; 9045 9046 return true; 9047 } 9048 9049 static bool perf_addr_filter_vma_adjust(struct perf_addr_filter *filter, 9050 struct vm_area_struct *vma, 9051 struct perf_addr_filter_range *fr) 9052 { 9053 unsigned long vma_size = vma->vm_end - vma->vm_start; 9054 unsigned long off = vma->vm_pgoff << PAGE_SHIFT; 9055 struct file *file = vma->vm_file; 9056 9057 if (!perf_addr_filter_match(filter, file, off, vma_size)) 9058 return false; 9059 9060 if (filter->offset < off) { 9061 fr->start = vma->vm_start; 9062 fr->size = min(vma_size, filter->size - (off - filter->offset)); 9063 } else { 9064 fr->start = vma->vm_start + filter->offset - off; 9065 fr->size = min(vma->vm_end - fr->start, filter->size); 9066 } 9067 9068 return true; 9069 } 9070 9071 static void __perf_addr_filters_adjust(struct perf_event *event, void *data) 9072 { 9073 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); 9074 struct vm_area_struct *vma = data; 9075 struct perf_addr_filter *filter; 9076 unsigned int restart = 0, count = 0; 9077 unsigned long flags; 9078 9079 if (!has_addr_filter(event)) 9080 return; 9081 9082 if (!vma->vm_file) 9083 return; 9084 9085 raw_spin_lock_irqsave(&ifh->lock, flags); 9086 list_for_each_entry(filter, &ifh->list, entry) { 9087 if (perf_addr_filter_vma_adjust(filter, vma, 9088 &event->addr_filter_ranges[count])) 9089 restart++; 9090 9091 count++; 9092 } 9093 9094 if (restart) 9095 event->addr_filters_gen++; 9096 raw_spin_unlock_irqrestore(&ifh->lock, flags); 9097 9098 if (restart) 9099 perf_event_stop(event, 1); 9100 } 9101 9102 /* 9103 * Adjust all task's events' filters to the new vma 9104 */ 9105 static void perf_addr_filters_adjust(struct vm_area_struct *vma) 9106 { 9107 struct perf_event_context *ctx; 9108 9109 /* 9110 * Data tracing isn't supported yet and as such there is no need 9111 * to keep track of anything that isn't related to executable code: 9112 */ 9113 if (!(vma->vm_flags & VM_EXEC)) 9114 return; 9115 9116 rcu_read_lock(); 9117 ctx = rcu_dereference(current->perf_event_ctxp); 9118 if (ctx) 9119 perf_iterate_ctx(ctx, __perf_addr_filters_adjust, vma, true); 9120 rcu_read_unlock(); 9121 } 9122 9123 void perf_event_mmap(struct vm_area_struct *vma) 9124 { 9125 struct perf_mmap_event mmap_event; 9126 9127 if (!atomic_read(&nr_mmap_events)) 9128 return; 9129 9130 mmap_event = (struct perf_mmap_event){ 9131 .vma = vma, 9132 /* .file_name */ 9133 /* .file_size */ 9134 .event_id = { 9135 .header = { 9136 .type = PERF_RECORD_MMAP, 9137 .misc = PERF_RECORD_MISC_USER, 9138 /* .size */ 9139 }, 9140 /* .pid */ 9141 /* .tid */ 9142 .start = vma->vm_start, 9143 .len = vma->vm_end - vma->vm_start, 9144 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, 9145 }, 9146 /* .maj (attr_mmap2 only) */ 9147 /* .min (attr_mmap2 only) */ 9148 /* .ino (attr_mmap2 only) */ 9149 /* .ino_generation (attr_mmap2 only) */ 9150 /* .prot (attr_mmap2 only) */ 9151 /* .flags (attr_mmap2 only) */ 9152 }; 9153 9154 perf_addr_filters_adjust(vma); 9155 perf_event_mmap_event(&mmap_event); 9156 } 9157 9158 void perf_event_aux_event(struct perf_event *event, unsigned long head, 9159 unsigned long size, u64 flags) 9160 { 9161 struct perf_output_handle handle; 9162 struct perf_sample_data sample; 9163 struct perf_aux_event { 9164 struct perf_event_header header; 9165 u64 offset; 9166 u64 size; 9167 u64 flags; 9168 } rec = { 9169 .header = { 9170 .type = PERF_RECORD_AUX, 9171 .misc = 0, 9172 .size = sizeof(rec), 9173 }, 9174 .offset = head, 9175 .size = size, 9176 .flags = flags, 9177 }; 9178 int ret; 9179 9180 perf_event_header__init_id(&rec.header, &sample, event); 9181 ret = perf_output_begin(&handle, &sample, event, rec.header.size); 9182 9183 if (ret) 9184 return; 9185 9186 perf_output_put(&handle, rec); 9187 perf_event__output_id_sample(event, &handle, &sample); 9188 9189 perf_output_end(&handle); 9190 } 9191 9192 /* 9193 * Lost/dropped samples logging 9194 */ 9195 void perf_log_lost_samples(struct perf_event *event, u64 lost) 9196 { 9197 struct perf_output_handle handle; 9198 struct perf_sample_data sample; 9199 int ret; 9200 9201 struct { 9202 struct perf_event_header header; 9203 u64 lost; 9204 } lost_samples_event = { 9205 .header = { 9206 .type = PERF_RECORD_LOST_SAMPLES, 9207 .misc = 0, 9208 .size = sizeof(lost_samples_event), 9209 }, 9210 .lost = lost, 9211 }; 9212 9213 perf_event_header__init_id(&lost_samples_event.header, &sample, event); 9214 9215 ret = perf_output_begin(&handle, &sample, event, 9216 lost_samples_event.header.size); 9217 if (ret) 9218 return; 9219 9220 perf_output_put(&handle, lost_samples_event); 9221 perf_event__output_id_sample(event, &handle, &sample); 9222 perf_output_end(&handle); 9223 } 9224 9225 /* 9226 * context_switch tracking 9227 */ 9228 9229 struct perf_switch_event { 9230 struct task_struct *task; 9231 struct task_struct *next_prev; 9232 9233 struct { 9234 struct perf_event_header header; 9235 u32 next_prev_pid; 9236 u32 next_prev_tid; 9237 } event_id; 9238 }; 9239 9240 static int perf_event_switch_match(struct perf_event *event) 9241 { 9242 return event->attr.context_switch; 9243 } 9244 9245 static void perf_event_switch_output(struct perf_event *event, void *data) 9246 { 9247 struct perf_switch_event *se = data; 9248 struct perf_output_handle handle; 9249 struct perf_sample_data sample; 9250 int ret; 9251 9252 if (!perf_event_switch_match(event)) 9253 return; 9254 9255 /* Only CPU-wide events are allowed to see next/prev pid/tid */ 9256 if (event->ctx->task) { 9257 se->event_id.header.type = PERF_RECORD_SWITCH; 9258 se->event_id.header.size = sizeof(se->event_id.header); 9259 } else { 9260 se->event_id.header.type = PERF_RECORD_SWITCH_CPU_WIDE; 9261 se->event_id.header.size = sizeof(se->event_id); 9262 se->event_id.next_prev_pid = 9263 perf_event_pid(event, se->next_prev); 9264 se->event_id.next_prev_tid = 9265 perf_event_tid(event, se->next_prev); 9266 } 9267 9268 perf_event_header__init_id(&se->event_id.header, &sample, event); 9269 9270 ret = perf_output_begin(&handle, &sample, event, se->event_id.header.size); 9271 if (ret) 9272 return; 9273 9274 if (event->ctx->task) 9275 perf_output_put(&handle, se->event_id.header); 9276 else 9277 perf_output_put(&handle, se->event_id); 9278 9279 perf_event__output_id_sample(event, &handle, &sample); 9280 9281 perf_output_end(&handle); 9282 } 9283 9284 static void perf_event_switch(struct task_struct *task, 9285 struct task_struct *next_prev, bool sched_in) 9286 { 9287 struct perf_switch_event switch_event; 9288 9289 /* N.B. caller checks nr_switch_events != 0 */ 9290 9291 switch_event = (struct perf_switch_event){ 9292 .task = task, 9293 .next_prev = next_prev, 9294 .event_id = { 9295 .header = { 9296 /* .type */ 9297 .misc = sched_in ? 0 : PERF_RECORD_MISC_SWITCH_OUT, 9298 /* .size */ 9299 }, 9300 /* .next_prev_pid */ 9301 /* .next_prev_tid */ 9302 }, 9303 }; 9304 9305 if (!sched_in && task_is_runnable(task)) { 9306 switch_event.event_id.header.misc |= 9307 PERF_RECORD_MISC_SWITCH_OUT_PREEMPT; 9308 } 9309 9310 perf_iterate_sb(perf_event_switch_output, &switch_event, NULL); 9311 } 9312 9313 /* 9314 * IRQ throttle logging 9315 */ 9316 9317 static void perf_log_throttle(struct perf_event *event, int enable) 9318 { 9319 struct perf_output_handle handle; 9320 struct perf_sample_data sample; 9321 int ret; 9322 9323 struct { 9324 struct perf_event_header header; 9325 u64 time; 9326 u64 id; 9327 u64 stream_id; 9328 } throttle_event = { 9329 .header = { 9330 .type = PERF_RECORD_THROTTLE, 9331 .misc = 0, 9332 .size = sizeof(throttle_event), 9333 }, 9334 .time = perf_event_clock(event), 9335 .id = primary_event_id(event), 9336 .stream_id = event->id, 9337 }; 9338 9339 if (enable) 9340 throttle_event.header.type = PERF_RECORD_UNTHROTTLE; 9341 9342 perf_event_header__init_id(&throttle_event.header, &sample, event); 9343 9344 ret = perf_output_begin(&handle, &sample, event, 9345 throttle_event.header.size); 9346 if (ret) 9347 return; 9348 9349 perf_output_put(&handle, throttle_event); 9350 perf_event__output_id_sample(event, &handle, &sample); 9351 perf_output_end(&handle); 9352 } 9353 9354 /* 9355 * ksymbol register/unregister tracking 9356 */ 9357 9358 struct perf_ksymbol_event { 9359 const char *name; 9360 int name_len; 9361 struct { 9362 struct perf_event_header header; 9363 u64 addr; 9364 u32 len; 9365 u16 ksym_type; 9366 u16 flags; 9367 } event_id; 9368 }; 9369 9370 static int perf_event_ksymbol_match(struct perf_event *event) 9371 { 9372 return event->attr.ksymbol; 9373 } 9374 9375 static void perf_event_ksymbol_output(struct perf_event *event, void *data) 9376 { 9377 struct perf_ksymbol_event *ksymbol_event = data; 9378 struct perf_output_handle handle; 9379 struct perf_sample_data sample; 9380 int ret; 9381 9382 if (!perf_event_ksymbol_match(event)) 9383 return; 9384 9385 perf_event_header__init_id(&ksymbol_event->event_id.header, 9386 &sample, event); 9387 ret = perf_output_begin(&handle, &sample, event, 9388 ksymbol_event->event_id.header.size); 9389 if (ret) 9390 return; 9391 9392 perf_output_put(&handle, ksymbol_event->event_id); 9393 __output_copy(&handle, ksymbol_event->name, ksymbol_event->name_len); 9394 perf_event__output_id_sample(event, &handle, &sample); 9395 9396 perf_output_end(&handle); 9397 } 9398 9399 void perf_event_ksymbol(u16 ksym_type, u64 addr, u32 len, bool unregister, 9400 const char *sym) 9401 { 9402 struct perf_ksymbol_event ksymbol_event; 9403 char name[KSYM_NAME_LEN]; 9404 u16 flags = 0; 9405 int name_len; 9406 9407 if (!atomic_read(&nr_ksymbol_events)) 9408 return; 9409 9410 if (ksym_type >= PERF_RECORD_KSYMBOL_TYPE_MAX || 9411 ksym_type == PERF_RECORD_KSYMBOL_TYPE_UNKNOWN) 9412 goto err; 9413 9414 strscpy(name, sym, KSYM_NAME_LEN); 9415 name_len = strlen(name) + 1; 9416 while (!IS_ALIGNED(name_len, sizeof(u64))) 9417 name[name_len++] = '\0'; 9418 BUILD_BUG_ON(KSYM_NAME_LEN % sizeof(u64)); 9419 9420 if (unregister) 9421 flags |= PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER; 9422 9423 ksymbol_event = (struct perf_ksymbol_event){ 9424 .name = name, 9425 .name_len = name_len, 9426 .event_id = { 9427 .header = { 9428 .type = PERF_RECORD_KSYMBOL, 9429 .size = sizeof(ksymbol_event.event_id) + 9430 name_len, 9431 }, 9432 .addr = addr, 9433 .len = len, 9434 .ksym_type = ksym_type, 9435 .flags = flags, 9436 }, 9437 }; 9438 9439 perf_iterate_sb(perf_event_ksymbol_output, &ksymbol_event, NULL); 9440 return; 9441 err: 9442 WARN_ONCE(1, "%s: Invalid KSYMBOL type 0x%x\n", __func__, ksym_type); 9443 } 9444 9445 /* 9446 * bpf program load/unload tracking 9447 */ 9448 9449 struct perf_bpf_event { 9450 struct bpf_prog *prog; 9451 struct { 9452 struct perf_event_header header; 9453 u16 type; 9454 u16 flags; 9455 u32 id; 9456 u8 tag[BPF_TAG_SIZE]; 9457 } event_id; 9458 }; 9459 9460 static int perf_event_bpf_match(struct perf_event *event) 9461 { 9462 return event->attr.bpf_event; 9463 } 9464 9465 static void perf_event_bpf_output(struct perf_event *event, void *data) 9466 { 9467 struct perf_bpf_event *bpf_event = data; 9468 struct perf_output_handle handle; 9469 struct perf_sample_data sample; 9470 int ret; 9471 9472 if (!perf_event_bpf_match(event)) 9473 return; 9474 9475 perf_event_header__init_id(&bpf_event->event_id.header, 9476 &sample, event); 9477 ret = perf_output_begin(&handle, &sample, event, 9478 bpf_event->event_id.header.size); 9479 if (ret) 9480 return; 9481 9482 perf_output_put(&handle, bpf_event->event_id); 9483 perf_event__output_id_sample(event, &handle, &sample); 9484 9485 perf_output_end(&handle); 9486 } 9487 9488 static void perf_event_bpf_emit_ksymbols(struct bpf_prog *prog, 9489 enum perf_bpf_event_type type) 9490 { 9491 bool unregister = type == PERF_BPF_EVENT_PROG_UNLOAD; 9492 int i; 9493 9494 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, 9495 (u64)(unsigned long)prog->bpf_func, 9496 prog->jited_len, unregister, 9497 prog->aux->ksym.name); 9498 9499 for (i = 1; i < prog->aux->func_cnt; i++) { 9500 struct bpf_prog *subprog = prog->aux->func[i]; 9501 9502 perf_event_ksymbol( 9503 PERF_RECORD_KSYMBOL_TYPE_BPF, 9504 (u64)(unsigned long)subprog->bpf_func, 9505 subprog->jited_len, unregister, 9506 subprog->aux->ksym.name); 9507 } 9508 } 9509 9510 void perf_event_bpf_event(struct bpf_prog *prog, 9511 enum perf_bpf_event_type type, 9512 u16 flags) 9513 { 9514 struct perf_bpf_event bpf_event; 9515 9516 switch (type) { 9517 case PERF_BPF_EVENT_PROG_LOAD: 9518 case PERF_BPF_EVENT_PROG_UNLOAD: 9519 if (atomic_read(&nr_ksymbol_events)) 9520 perf_event_bpf_emit_ksymbols(prog, type); 9521 break; 9522 default: 9523 return; 9524 } 9525 9526 if (!atomic_read(&nr_bpf_events)) 9527 return; 9528 9529 bpf_event = (struct perf_bpf_event){ 9530 .prog = prog, 9531 .event_id = { 9532 .header = { 9533 .type = PERF_RECORD_BPF_EVENT, 9534 .size = sizeof(bpf_event.event_id), 9535 }, 9536 .type = type, 9537 .flags = flags, 9538 .id = prog->aux->id, 9539 }, 9540 }; 9541 9542 BUILD_BUG_ON(BPF_TAG_SIZE % sizeof(u64)); 9543 9544 memcpy(bpf_event.event_id.tag, prog->tag, BPF_TAG_SIZE); 9545 perf_iterate_sb(perf_event_bpf_output, &bpf_event, NULL); 9546 } 9547 9548 struct perf_text_poke_event { 9549 const void *old_bytes; 9550 const void *new_bytes; 9551 size_t pad; 9552 u16 old_len; 9553 u16 new_len; 9554 9555 struct { 9556 struct perf_event_header header; 9557 9558 u64 addr; 9559 } event_id; 9560 }; 9561 9562 static int perf_event_text_poke_match(struct perf_event *event) 9563 { 9564 return event->attr.text_poke; 9565 } 9566 9567 static void perf_event_text_poke_output(struct perf_event *event, void *data) 9568 { 9569 struct perf_text_poke_event *text_poke_event = data; 9570 struct perf_output_handle handle; 9571 struct perf_sample_data sample; 9572 u64 padding = 0; 9573 int ret; 9574 9575 if (!perf_event_text_poke_match(event)) 9576 return; 9577 9578 perf_event_header__init_id(&text_poke_event->event_id.header, &sample, event); 9579 9580 ret = perf_output_begin(&handle, &sample, event, 9581 text_poke_event->event_id.header.size); 9582 if (ret) 9583 return; 9584 9585 perf_output_put(&handle, text_poke_event->event_id); 9586 perf_output_put(&handle, text_poke_event->old_len); 9587 perf_output_put(&handle, text_poke_event->new_len); 9588 9589 __output_copy(&handle, text_poke_event->old_bytes, text_poke_event->old_len); 9590 __output_copy(&handle, text_poke_event->new_bytes, text_poke_event->new_len); 9591 9592 if (text_poke_event->pad) 9593 __output_copy(&handle, &padding, text_poke_event->pad); 9594 9595 perf_event__output_id_sample(event, &handle, &sample); 9596 9597 perf_output_end(&handle); 9598 } 9599 9600 void perf_event_text_poke(const void *addr, const void *old_bytes, 9601 size_t old_len, const void *new_bytes, size_t new_len) 9602 { 9603 struct perf_text_poke_event text_poke_event; 9604 size_t tot, pad; 9605 9606 if (!atomic_read(&nr_text_poke_events)) 9607 return; 9608 9609 tot = sizeof(text_poke_event.old_len) + old_len; 9610 tot += sizeof(text_poke_event.new_len) + new_len; 9611 pad = ALIGN(tot, sizeof(u64)) - tot; 9612 9613 text_poke_event = (struct perf_text_poke_event){ 9614 .old_bytes = old_bytes, 9615 .new_bytes = new_bytes, 9616 .pad = pad, 9617 .old_len = old_len, 9618 .new_len = new_len, 9619 .event_id = { 9620 .header = { 9621 .type = PERF_RECORD_TEXT_POKE, 9622 .misc = PERF_RECORD_MISC_KERNEL, 9623 .size = sizeof(text_poke_event.event_id) + tot + pad, 9624 }, 9625 .addr = (unsigned long)addr, 9626 }, 9627 }; 9628 9629 perf_iterate_sb(perf_event_text_poke_output, &text_poke_event, NULL); 9630 } 9631 9632 void perf_event_itrace_started(struct perf_event *event) 9633 { 9634 event->attach_state |= PERF_ATTACH_ITRACE; 9635 } 9636 9637 static void perf_log_itrace_start(struct perf_event *event) 9638 { 9639 struct perf_output_handle handle; 9640 struct perf_sample_data sample; 9641 struct perf_aux_event { 9642 struct perf_event_header header; 9643 u32 pid; 9644 u32 tid; 9645 } rec; 9646 int ret; 9647 9648 if (event->parent) 9649 event = event->parent; 9650 9651 if (!(event->pmu->capabilities & PERF_PMU_CAP_ITRACE) || 9652 event->attach_state & PERF_ATTACH_ITRACE) 9653 return; 9654 9655 rec.header.type = PERF_RECORD_ITRACE_START; 9656 rec.header.misc = 0; 9657 rec.header.size = sizeof(rec); 9658 rec.pid = perf_event_pid(event, current); 9659 rec.tid = perf_event_tid(event, current); 9660 9661 perf_event_header__init_id(&rec.header, &sample, event); 9662 ret = perf_output_begin(&handle, &sample, event, rec.header.size); 9663 9664 if (ret) 9665 return; 9666 9667 perf_output_put(&handle, rec); 9668 perf_event__output_id_sample(event, &handle, &sample); 9669 9670 perf_output_end(&handle); 9671 } 9672 9673 void perf_report_aux_output_id(struct perf_event *event, u64 hw_id) 9674 { 9675 struct perf_output_handle handle; 9676 struct perf_sample_data sample; 9677 struct perf_aux_event { 9678 struct perf_event_header header; 9679 u64 hw_id; 9680 } rec; 9681 int ret; 9682 9683 if (event->parent) 9684 event = event->parent; 9685 9686 rec.header.type = PERF_RECORD_AUX_OUTPUT_HW_ID; 9687 rec.header.misc = 0; 9688 rec.header.size = sizeof(rec); 9689 rec.hw_id = hw_id; 9690 9691 perf_event_header__init_id(&rec.header, &sample, event); 9692 ret = perf_output_begin(&handle, &sample, event, rec.header.size); 9693 9694 if (ret) 9695 return; 9696 9697 perf_output_put(&handle, rec); 9698 perf_event__output_id_sample(event, &handle, &sample); 9699 9700 perf_output_end(&handle); 9701 } 9702 EXPORT_SYMBOL_GPL(perf_report_aux_output_id); 9703 9704 static int 9705 __perf_event_account_interrupt(struct perf_event *event, int throttle) 9706 { 9707 struct hw_perf_event *hwc = &event->hw; 9708 int ret = 0; 9709 u64 seq; 9710 9711 seq = __this_cpu_read(perf_throttled_seq); 9712 if (seq != hwc->interrupts_seq) { 9713 hwc->interrupts_seq = seq; 9714 hwc->interrupts = 1; 9715 } else { 9716 hwc->interrupts++; 9717 if (unlikely(throttle && 9718 hwc->interrupts > max_samples_per_tick)) { 9719 __this_cpu_inc(perf_throttled_count); 9720 tick_dep_set_cpu(smp_processor_id(), TICK_DEP_BIT_PERF_EVENTS); 9721 hwc->interrupts = MAX_INTERRUPTS; 9722 perf_log_throttle(event, 0); 9723 ret = 1; 9724 } 9725 } 9726 9727 if (event->attr.freq) { 9728 u64 now = perf_clock(); 9729 s64 delta = now - hwc->freq_time_stamp; 9730 9731 hwc->freq_time_stamp = now; 9732 9733 if (delta > 0 && delta < 2*TICK_NSEC) 9734 perf_adjust_period(event, delta, hwc->last_period, true); 9735 } 9736 9737 return ret; 9738 } 9739 9740 int perf_event_account_interrupt(struct perf_event *event) 9741 { 9742 return __perf_event_account_interrupt(event, 1); 9743 } 9744 9745 static inline bool sample_is_allowed(struct perf_event *event, struct pt_regs *regs) 9746 { 9747 /* 9748 * Due to interrupt latency (AKA "skid"), we may enter the 9749 * kernel before taking an overflow, even if the PMU is only 9750 * counting user events. 9751 */ 9752 if (event->attr.exclude_kernel && !user_mode(regs)) 9753 return false; 9754 9755 return true; 9756 } 9757 9758 #ifdef CONFIG_BPF_SYSCALL 9759 static int bpf_overflow_handler(struct perf_event *event, 9760 struct perf_sample_data *data, 9761 struct pt_regs *regs) 9762 { 9763 struct bpf_perf_event_data_kern ctx = { 9764 .data = data, 9765 .event = event, 9766 }; 9767 struct bpf_prog *prog; 9768 int ret = 0; 9769 9770 ctx.regs = perf_arch_bpf_user_pt_regs(regs); 9771 if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1)) 9772 goto out; 9773 rcu_read_lock(); 9774 prog = READ_ONCE(event->prog); 9775 if (prog) { 9776 perf_prepare_sample(data, event, regs); 9777 ret = bpf_prog_run(prog, &ctx); 9778 } 9779 rcu_read_unlock(); 9780 out: 9781 __this_cpu_dec(bpf_prog_active); 9782 9783 return ret; 9784 } 9785 9786 static inline int perf_event_set_bpf_handler(struct perf_event *event, 9787 struct bpf_prog *prog, 9788 u64 bpf_cookie) 9789 { 9790 if (event->overflow_handler_context) 9791 /* hw breakpoint or kernel counter */ 9792 return -EINVAL; 9793 9794 if (event->prog) 9795 return -EEXIST; 9796 9797 if (prog->type != BPF_PROG_TYPE_PERF_EVENT) 9798 return -EINVAL; 9799 9800 if (event->attr.precise_ip && 9801 prog->call_get_stack && 9802 (!(event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) || 9803 event->attr.exclude_callchain_kernel || 9804 event->attr.exclude_callchain_user)) { 9805 /* 9806 * On perf_event with precise_ip, calling bpf_get_stack() 9807 * may trigger unwinder warnings and occasional crashes. 9808 * bpf_get_[stack|stackid] works around this issue by using 9809 * callchain attached to perf_sample_data. If the 9810 * perf_event does not full (kernel and user) callchain 9811 * attached to perf_sample_data, do not allow attaching BPF 9812 * program that calls bpf_get_[stack|stackid]. 9813 */ 9814 return -EPROTO; 9815 } 9816 9817 event->prog = prog; 9818 event->bpf_cookie = bpf_cookie; 9819 return 0; 9820 } 9821 9822 static inline void perf_event_free_bpf_handler(struct perf_event *event) 9823 { 9824 struct bpf_prog *prog = event->prog; 9825 9826 if (!prog) 9827 return; 9828 9829 event->prog = NULL; 9830 bpf_prog_put(prog); 9831 } 9832 #else 9833 static inline int bpf_overflow_handler(struct perf_event *event, 9834 struct perf_sample_data *data, 9835 struct pt_regs *regs) 9836 { 9837 return 1; 9838 } 9839 9840 static inline int perf_event_set_bpf_handler(struct perf_event *event, 9841 struct bpf_prog *prog, 9842 u64 bpf_cookie) 9843 { 9844 return -EOPNOTSUPP; 9845 } 9846 9847 static inline void perf_event_free_bpf_handler(struct perf_event *event) 9848 { 9849 } 9850 #endif 9851 9852 /* 9853 * Generic event overflow handling, sampling. 9854 */ 9855 9856 static int __perf_event_overflow(struct perf_event *event, 9857 int throttle, struct perf_sample_data *data, 9858 struct pt_regs *regs) 9859 { 9860 int events = atomic_read(&event->event_limit); 9861 int ret = 0; 9862 9863 /* 9864 * Non-sampling counters might still use the PMI to fold short 9865 * hardware counters, ignore those. 9866 */ 9867 if (unlikely(!is_sampling_event(event))) 9868 return 0; 9869 9870 ret = __perf_event_account_interrupt(event, throttle); 9871 9872 if (event->attr.aux_pause) 9873 perf_event_aux_pause(event->aux_event, true); 9874 9875 if (event->prog && event->prog->type == BPF_PROG_TYPE_PERF_EVENT && 9876 !bpf_overflow_handler(event, data, regs)) 9877 goto out; 9878 9879 /* 9880 * XXX event_limit might not quite work as expected on inherited 9881 * events 9882 */ 9883 9884 event->pending_kill = POLL_IN; 9885 if (events && atomic_dec_and_test(&event->event_limit)) { 9886 ret = 1; 9887 event->pending_kill = POLL_HUP; 9888 perf_event_disable_inatomic(event); 9889 } 9890 9891 if (event->attr.sigtrap) { 9892 /* 9893 * The desired behaviour of sigtrap vs invalid samples is a bit 9894 * tricky; on the one hand, one should not loose the SIGTRAP if 9895 * it is the first event, on the other hand, we should also not 9896 * trigger the WARN or override the data address. 9897 */ 9898 bool valid_sample = sample_is_allowed(event, regs); 9899 unsigned int pending_id = 1; 9900 enum task_work_notify_mode notify_mode; 9901 9902 if (regs) 9903 pending_id = hash32_ptr((void *)instruction_pointer(regs)) ?: 1; 9904 9905 notify_mode = in_nmi() ? TWA_NMI_CURRENT : TWA_RESUME; 9906 9907 if (!event->pending_work && 9908 !task_work_add(current, &event->pending_task, notify_mode)) { 9909 event->pending_work = pending_id; 9910 local_inc(&event->ctx->nr_no_switch_fast); 9911 9912 event->pending_addr = 0; 9913 if (valid_sample && (data->sample_flags & PERF_SAMPLE_ADDR)) 9914 event->pending_addr = data->addr; 9915 9916 } else if (event->attr.exclude_kernel && valid_sample) { 9917 /* 9918 * Should not be able to return to user space without 9919 * consuming pending_work; with exceptions: 9920 * 9921 * 1. Where !exclude_kernel, events can overflow again 9922 * in the kernel without returning to user space. 9923 * 9924 * 2. Events that can overflow again before the IRQ- 9925 * work without user space progress (e.g. hrtimer). 9926 * To approximate progress (with false negatives), 9927 * check 32-bit hash of the current IP. 9928 */ 9929 WARN_ON_ONCE(event->pending_work != pending_id); 9930 } 9931 } 9932 9933 READ_ONCE(event->overflow_handler)(event, data, regs); 9934 9935 if (*perf_event_fasync(event) && event->pending_kill) { 9936 event->pending_wakeup = 1; 9937 irq_work_queue(&event->pending_irq); 9938 } 9939 out: 9940 if (event->attr.aux_resume) 9941 perf_event_aux_pause(event->aux_event, false); 9942 9943 return ret; 9944 } 9945 9946 int perf_event_overflow(struct perf_event *event, 9947 struct perf_sample_data *data, 9948 struct pt_regs *regs) 9949 { 9950 return __perf_event_overflow(event, 1, data, regs); 9951 } 9952 9953 /* 9954 * Generic software event infrastructure 9955 */ 9956 9957 struct swevent_htable { 9958 struct swevent_hlist *swevent_hlist; 9959 struct mutex hlist_mutex; 9960 int hlist_refcount; 9961 }; 9962 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); 9963 9964 /* 9965 * We directly increment event->count and keep a second value in 9966 * event->hw.period_left to count intervals. This period event 9967 * is kept in the range [-sample_period, 0] so that we can use the 9968 * sign as trigger. 9969 */ 9970 9971 u64 perf_swevent_set_period(struct perf_event *event) 9972 { 9973 struct hw_perf_event *hwc = &event->hw; 9974 u64 period = hwc->last_period; 9975 u64 nr, offset; 9976 s64 old, val; 9977 9978 hwc->last_period = hwc->sample_period; 9979 9980 old = local64_read(&hwc->period_left); 9981 do { 9982 val = old; 9983 if (val < 0) 9984 return 0; 9985 9986 nr = div64_u64(period + val, period); 9987 offset = nr * period; 9988 val -= offset; 9989 } while (!local64_try_cmpxchg(&hwc->period_left, &old, val)); 9990 9991 return nr; 9992 } 9993 9994 static void perf_swevent_overflow(struct perf_event *event, u64 overflow, 9995 struct perf_sample_data *data, 9996 struct pt_regs *regs) 9997 { 9998 struct hw_perf_event *hwc = &event->hw; 9999 int throttle = 0; 10000 10001 if (!overflow) 10002 overflow = perf_swevent_set_period(event); 10003 10004 if (hwc->interrupts == MAX_INTERRUPTS) 10005 return; 10006 10007 for (; overflow; overflow--) { 10008 if (__perf_event_overflow(event, throttle, 10009 data, regs)) { 10010 /* 10011 * We inhibit the overflow from happening when 10012 * hwc->interrupts == MAX_INTERRUPTS. 10013 */ 10014 break; 10015 } 10016 throttle = 1; 10017 } 10018 } 10019 10020 static void perf_swevent_event(struct perf_event *event, u64 nr, 10021 struct perf_sample_data *data, 10022 struct pt_regs *regs) 10023 { 10024 struct hw_perf_event *hwc = &event->hw; 10025 10026 local64_add(nr, &event->count); 10027 10028 if (!regs) 10029 return; 10030 10031 if (!is_sampling_event(event)) 10032 return; 10033 10034 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { 10035 data->period = nr; 10036 return perf_swevent_overflow(event, 1, data, regs); 10037 } else 10038 data->period = event->hw.last_period; 10039 10040 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) 10041 return perf_swevent_overflow(event, 1, data, regs); 10042 10043 if (local64_add_negative(nr, &hwc->period_left)) 10044 return; 10045 10046 perf_swevent_overflow(event, 0, data, regs); 10047 } 10048 10049 static int perf_exclude_event(struct perf_event *event, 10050 struct pt_regs *regs) 10051 { 10052 if (event->hw.state & PERF_HES_STOPPED) 10053 return 1; 10054 10055 if (regs) { 10056 if (event->attr.exclude_user && user_mode(regs)) 10057 return 1; 10058 10059 if (event->attr.exclude_kernel && !user_mode(regs)) 10060 return 1; 10061 } 10062 10063 return 0; 10064 } 10065 10066 static int perf_swevent_match(struct perf_event *event, 10067 enum perf_type_id type, 10068 u32 event_id, 10069 struct perf_sample_data *data, 10070 struct pt_regs *regs) 10071 { 10072 if (event->attr.type != type) 10073 return 0; 10074 10075 if (event->attr.config != event_id) 10076 return 0; 10077 10078 if (perf_exclude_event(event, regs)) 10079 return 0; 10080 10081 return 1; 10082 } 10083 10084 static inline u64 swevent_hash(u64 type, u32 event_id) 10085 { 10086 u64 val = event_id | (type << 32); 10087 10088 return hash_64(val, SWEVENT_HLIST_BITS); 10089 } 10090 10091 static inline struct hlist_head * 10092 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) 10093 { 10094 u64 hash = swevent_hash(type, event_id); 10095 10096 return &hlist->heads[hash]; 10097 } 10098 10099 /* For the read side: events when they trigger */ 10100 static inline struct hlist_head * 10101 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id) 10102 { 10103 struct swevent_hlist *hlist; 10104 10105 hlist = rcu_dereference(swhash->swevent_hlist); 10106 if (!hlist) 10107 return NULL; 10108 10109 return __find_swevent_head(hlist, type, event_id); 10110 } 10111 10112 /* For the event head insertion and removal in the hlist */ 10113 static inline struct hlist_head * 10114 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) 10115 { 10116 struct swevent_hlist *hlist; 10117 u32 event_id = event->attr.config; 10118 u64 type = event->attr.type; 10119 10120 /* 10121 * Event scheduling is always serialized against hlist allocation 10122 * and release. Which makes the protected version suitable here. 10123 * The context lock guarantees that. 10124 */ 10125 hlist = rcu_dereference_protected(swhash->swevent_hlist, 10126 lockdep_is_held(&event->ctx->lock)); 10127 if (!hlist) 10128 return NULL; 10129 10130 return __find_swevent_head(hlist, type, event_id); 10131 } 10132 10133 static void do_perf_sw_event(enum perf_type_id type, u32 event_id, 10134 u64 nr, 10135 struct perf_sample_data *data, 10136 struct pt_regs *regs) 10137 { 10138 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); 10139 struct perf_event *event; 10140 struct hlist_head *head; 10141 10142 rcu_read_lock(); 10143 head = find_swevent_head_rcu(swhash, type, event_id); 10144 if (!head) 10145 goto end; 10146 10147 hlist_for_each_entry_rcu(event, head, hlist_entry) { 10148 if (perf_swevent_match(event, type, event_id, data, regs)) 10149 perf_swevent_event(event, nr, data, regs); 10150 } 10151 end: 10152 rcu_read_unlock(); 10153 } 10154 10155 DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]); 10156 10157 int perf_swevent_get_recursion_context(void) 10158 { 10159 return get_recursion_context(current->perf_recursion); 10160 } 10161 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); 10162 10163 void perf_swevent_put_recursion_context(int rctx) 10164 { 10165 put_recursion_context(current->perf_recursion, rctx); 10166 } 10167 10168 void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) 10169 { 10170 struct perf_sample_data data; 10171 10172 if (WARN_ON_ONCE(!regs)) 10173 return; 10174 10175 perf_sample_data_init(&data, addr, 0); 10176 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); 10177 } 10178 10179 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) 10180 { 10181 int rctx; 10182 10183 preempt_disable_notrace(); 10184 rctx = perf_swevent_get_recursion_context(); 10185 if (unlikely(rctx < 0)) 10186 goto fail; 10187 10188 ___perf_sw_event(event_id, nr, regs, addr); 10189 10190 perf_swevent_put_recursion_context(rctx); 10191 fail: 10192 preempt_enable_notrace(); 10193 } 10194 10195 static void perf_swevent_read(struct perf_event *event) 10196 { 10197 } 10198 10199 static int perf_swevent_add(struct perf_event *event, int flags) 10200 { 10201 struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable); 10202 struct hw_perf_event *hwc = &event->hw; 10203 struct hlist_head *head; 10204 10205 if (is_sampling_event(event)) { 10206 hwc->last_period = hwc->sample_period; 10207 perf_swevent_set_period(event); 10208 } 10209 10210 hwc->state = !(flags & PERF_EF_START); 10211 10212 head = find_swevent_head(swhash, event); 10213 if (WARN_ON_ONCE(!head)) 10214 return -EINVAL; 10215 10216 hlist_add_head_rcu(&event->hlist_entry, head); 10217 perf_event_update_userpage(event); 10218 10219 return 0; 10220 } 10221 10222 static void perf_swevent_del(struct perf_event *event, int flags) 10223 { 10224 hlist_del_rcu(&event->hlist_entry); 10225 } 10226 10227 static void perf_swevent_start(struct perf_event *event, int flags) 10228 { 10229 event->hw.state = 0; 10230 } 10231 10232 static void perf_swevent_stop(struct perf_event *event, int flags) 10233 { 10234 event->hw.state = PERF_HES_STOPPED; 10235 } 10236 10237 /* Deref the hlist from the update side */ 10238 static inline struct swevent_hlist * 10239 swevent_hlist_deref(struct swevent_htable *swhash) 10240 { 10241 return rcu_dereference_protected(swhash->swevent_hlist, 10242 lockdep_is_held(&swhash->hlist_mutex)); 10243 } 10244 10245 static void swevent_hlist_release(struct swevent_htable *swhash) 10246 { 10247 struct swevent_hlist *hlist = swevent_hlist_deref(swhash); 10248 10249 if (!hlist) 10250 return; 10251 10252 RCU_INIT_POINTER(swhash->swevent_hlist, NULL); 10253 kfree_rcu(hlist, rcu_head); 10254 } 10255 10256 static void swevent_hlist_put_cpu(int cpu) 10257 { 10258 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); 10259 10260 mutex_lock(&swhash->hlist_mutex); 10261 10262 if (!--swhash->hlist_refcount) 10263 swevent_hlist_release(swhash); 10264 10265 mutex_unlock(&swhash->hlist_mutex); 10266 } 10267 10268 static void swevent_hlist_put(void) 10269 { 10270 int cpu; 10271 10272 for_each_possible_cpu(cpu) 10273 swevent_hlist_put_cpu(cpu); 10274 } 10275 10276 static int swevent_hlist_get_cpu(int cpu) 10277 { 10278 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); 10279 int err = 0; 10280 10281 mutex_lock(&swhash->hlist_mutex); 10282 if (!swevent_hlist_deref(swhash) && 10283 cpumask_test_cpu(cpu, perf_online_mask)) { 10284 struct swevent_hlist *hlist; 10285 10286 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); 10287 if (!hlist) { 10288 err = -ENOMEM; 10289 goto exit; 10290 } 10291 rcu_assign_pointer(swhash->swevent_hlist, hlist); 10292 } 10293 swhash->hlist_refcount++; 10294 exit: 10295 mutex_unlock(&swhash->hlist_mutex); 10296 10297 return err; 10298 } 10299 10300 static int swevent_hlist_get(void) 10301 { 10302 int err, cpu, failed_cpu; 10303 10304 mutex_lock(&pmus_lock); 10305 for_each_possible_cpu(cpu) { 10306 err = swevent_hlist_get_cpu(cpu); 10307 if (err) { 10308 failed_cpu = cpu; 10309 goto fail; 10310 } 10311 } 10312 mutex_unlock(&pmus_lock); 10313 return 0; 10314 fail: 10315 for_each_possible_cpu(cpu) { 10316 if (cpu == failed_cpu) 10317 break; 10318 swevent_hlist_put_cpu(cpu); 10319 } 10320 mutex_unlock(&pmus_lock); 10321 return err; 10322 } 10323 10324 struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; 10325 10326 static void sw_perf_event_destroy(struct perf_event *event) 10327 { 10328 u64 event_id = event->attr.config; 10329 10330 WARN_ON(event->parent); 10331 10332 static_key_slow_dec(&perf_swevent_enabled[event_id]); 10333 swevent_hlist_put(); 10334 } 10335 10336 static struct pmu perf_cpu_clock; /* fwd declaration */ 10337 static struct pmu perf_task_clock; 10338 10339 static int perf_swevent_init(struct perf_event *event) 10340 { 10341 u64 event_id = event->attr.config; 10342 10343 if (event->attr.type != PERF_TYPE_SOFTWARE) 10344 return -ENOENT; 10345 10346 /* 10347 * no branch sampling for software events 10348 */ 10349 if (has_branch_stack(event)) 10350 return -EOPNOTSUPP; 10351 10352 switch (event_id) { 10353 case PERF_COUNT_SW_CPU_CLOCK: 10354 event->attr.type = perf_cpu_clock.type; 10355 return -ENOENT; 10356 case PERF_COUNT_SW_TASK_CLOCK: 10357 event->attr.type = perf_task_clock.type; 10358 return -ENOENT; 10359 10360 default: 10361 break; 10362 } 10363 10364 if (event_id >= PERF_COUNT_SW_MAX) 10365 return -ENOENT; 10366 10367 if (!event->parent) { 10368 int err; 10369 10370 err = swevent_hlist_get(); 10371 if (err) 10372 return err; 10373 10374 static_key_slow_inc(&perf_swevent_enabled[event_id]); 10375 event->destroy = sw_perf_event_destroy; 10376 } 10377 10378 return 0; 10379 } 10380 10381 static struct pmu perf_swevent = { 10382 .task_ctx_nr = perf_sw_context, 10383 10384 .capabilities = PERF_PMU_CAP_NO_NMI, 10385 10386 .event_init = perf_swevent_init, 10387 .add = perf_swevent_add, 10388 .del = perf_swevent_del, 10389 .start = perf_swevent_start, 10390 .stop = perf_swevent_stop, 10391 .read = perf_swevent_read, 10392 }; 10393 10394 #ifdef CONFIG_EVENT_TRACING 10395 10396 static void tp_perf_event_destroy(struct perf_event *event) 10397 { 10398 perf_trace_destroy(event); 10399 } 10400 10401 static int perf_tp_event_init(struct perf_event *event) 10402 { 10403 int err; 10404 10405 if (event->attr.type != PERF_TYPE_TRACEPOINT) 10406 return -ENOENT; 10407 10408 /* 10409 * no branch sampling for tracepoint events 10410 */ 10411 if (has_branch_stack(event)) 10412 return -EOPNOTSUPP; 10413 10414 err = perf_trace_init(event); 10415 if (err) 10416 return err; 10417 10418 event->destroy = tp_perf_event_destroy; 10419 10420 return 0; 10421 } 10422 10423 static struct pmu perf_tracepoint = { 10424 .task_ctx_nr = perf_sw_context, 10425 10426 .event_init = perf_tp_event_init, 10427 .add = perf_trace_add, 10428 .del = perf_trace_del, 10429 .start = perf_swevent_start, 10430 .stop = perf_swevent_stop, 10431 .read = perf_swevent_read, 10432 }; 10433 10434 static int perf_tp_filter_match(struct perf_event *event, 10435 struct perf_sample_data *data) 10436 { 10437 void *record = data->raw->frag.data; 10438 10439 /* only top level events have filters set */ 10440 if (event->parent) 10441 event = event->parent; 10442 10443 if (likely(!event->filter) || filter_match_preds(event->filter, record)) 10444 return 1; 10445 return 0; 10446 } 10447 10448 static int perf_tp_event_match(struct perf_event *event, 10449 struct perf_sample_data *data, 10450 struct pt_regs *regs) 10451 { 10452 if (event->hw.state & PERF_HES_STOPPED) 10453 return 0; 10454 /* 10455 * If exclude_kernel, only trace user-space tracepoints (uprobes) 10456 */ 10457 if (event->attr.exclude_kernel && !user_mode(regs)) 10458 return 0; 10459 10460 if (!perf_tp_filter_match(event, data)) 10461 return 0; 10462 10463 return 1; 10464 } 10465 10466 void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx, 10467 struct trace_event_call *call, u64 count, 10468 struct pt_regs *regs, struct hlist_head *head, 10469 struct task_struct *task) 10470 { 10471 if (bpf_prog_array_valid(call)) { 10472 *(struct pt_regs **)raw_data = regs; 10473 if (!trace_call_bpf(call, raw_data) || hlist_empty(head)) { 10474 perf_swevent_put_recursion_context(rctx); 10475 return; 10476 } 10477 } 10478 perf_tp_event(call->event.type, count, raw_data, size, regs, head, 10479 rctx, task); 10480 } 10481 EXPORT_SYMBOL_GPL(perf_trace_run_bpf_submit); 10482 10483 static void __perf_tp_event_target_task(u64 count, void *record, 10484 struct pt_regs *regs, 10485 struct perf_sample_data *data, 10486 struct perf_event *event) 10487 { 10488 struct trace_entry *entry = record; 10489 10490 if (event->attr.config != entry->type) 10491 return; 10492 /* Cannot deliver synchronous signal to other task. */ 10493 if (event->attr.sigtrap) 10494 return; 10495 if (perf_tp_event_match(event, data, regs)) 10496 perf_swevent_event(event, count, data, regs); 10497 } 10498 10499 static void perf_tp_event_target_task(u64 count, void *record, 10500 struct pt_regs *regs, 10501 struct perf_sample_data *data, 10502 struct perf_event_context *ctx) 10503 { 10504 unsigned int cpu = smp_processor_id(); 10505 struct pmu *pmu = &perf_tracepoint; 10506 struct perf_event *event, *sibling; 10507 10508 perf_event_groups_for_cpu_pmu(event, &ctx->pinned_groups, cpu, pmu) { 10509 __perf_tp_event_target_task(count, record, regs, data, event); 10510 for_each_sibling_event(sibling, event) 10511 __perf_tp_event_target_task(count, record, regs, data, sibling); 10512 } 10513 10514 perf_event_groups_for_cpu_pmu(event, &ctx->flexible_groups, cpu, pmu) { 10515 __perf_tp_event_target_task(count, record, regs, data, event); 10516 for_each_sibling_event(sibling, event) 10517 __perf_tp_event_target_task(count, record, regs, data, sibling); 10518 } 10519 } 10520 10521 void perf_tp_event(u16 event_type, u64 count, void *record, int entry_size, 10522 struct pt_regs *regs, struct hlist_head *head, int rctx, 10523 struct task_struct *task) 10524 { 10525 struct perf_sample_data data; 10526 struct perf_event *event; 10527 10528 struct perf_raw_record raw = { 10529 .frag = { 10530 .size = entry_size, 10531 .data = record, 10532 }, 10533 }; 10534 10535 perf_sample_data_init(&data, 0, 0); 10536 perf_sample_save_raw_data(&data, &raw); 10537 10538 perf_trace_buf_update(record, event_type); 10539 10540 hlist_for_each_entry_rcu(event, head, hlist_entry) { 10541 if (perf_tp_event_match(event, &data, regs)) { 10542 perf_swevent_event(event, count, &data, regs); 10543 10544 /* 10545 * Here use the same on-stack perf_sample_data, 10546 * some members in data are event-specific and 10547 * need to be re-computed for different sweveents. 10548 * Re-initialize data->sample_flags safely to avoid 10549 * the problem that next event skips preparing data 10550 * because data->sample_flags is set. 10551 */ 10552 perf_sample_data_init(&data, 0, 0); 10553 perf_sample_save_raw_data(&data, &raw); 10554 } 10555 } 10556 10557 /* 10558 * If we got specified a target task, also iterate its context and 10559 * deliver this event there too. 10560 */ 10561 if (task && task != current) { 10562 struct perf_event_context *ctx; 10563 10564 rcu_read_lock(); 10565 ctx = rcu_dereference(task->perf_event_ctxp); 10566 if (!ctx) 10567 goto unlock; 10568 10569 raw_spin_lock(&ctx->lock); 10570 perf_tp_event_target_task(count, record, regs, &data, ctx); 10571 raw_spin_unlock(&ctx->lock); 10572 unlock: 10573 rcu_read_unlock(); 10574 } 10575 10576 perf_swevent_put_recursion_context(rctx); 10577 } 10578 EXPORT_SYMBOL_GPL(perf_tp_event); 10579 10580 #if defined(CONFIG_KPROBE_EVENTS) || defined(CONFIG_UPROBE_EVENTS) 10581 /* 10582 * Flags in config, used by dynamic PMU kprobe and uprobe 10583 * The flags should match following PMU_FORMAT_ATTR(). 10584 * 10585 * PERF_PROBE_CONFIG_IS_RETPROBE if set, create kretprobe/uretprobe 10586 * if not set, create kprobe/uprobe 10587 * 10588 * The following values specify a reference counter (or semaphore in the 10589 * terminology of tools like dtrace, systemtap, etc.) Userspace Statically 10590 * Defined Tracepoints (USDT). Currently, we use 40 bit for the offset. 10591 * 10592 * PERF_UPROBE_REF_CTR_OFFSET_BITS # of bits in config as th offset 10593 * PERF_UPROBE_REF_CTR_OFFSET_SHIFT # of bits to shift left 10594 */ 10595 enum perf_probe_config { 10596 PERF_PROBE_CONFIG_IS_RETPROBE = 1U << 0, /* [k,u]retprobe */ 10597 PERF_UPROBE_REF_CTR_OFFSET_BITS = 32, 10598 PERF_UPROBE_REF_CTR_OFFSET_SHIFT = 64 - PERF_UPROBE_REF_CTR_OFFSET_BITS, 10599 }; 10600 10601 PMU_FORMAT_ATTR(retprobe, "config:0"); 10602 #endif 10603 10604 #ifdef CONFIG_KPROBE_EVENTS 10605 static struct attribute *kprobe_attrs[] = { 10606 &format_attr_retprobe.attr, 10607 NULL, 10608 }; 10609 10610 static struct attribute_group kprobe_format_group = { 10611 .name = "format", 10612 .attrs = kprobe_attrs, 10613 }; 10614 10615 static const struct attribute_group *kprobe_attr_groups[] = { 10616 &kprobe_format_group, 10617 NULL, 10618 }; 10619 10620 static int perf_kprobe_event_init(struct perf_event *event); 10621 static struct pmu perf_kprobe = { 10622 .task_ctx_nr = perf_sw_context, 10623 .event_init = perf_kprobe_event_init, 10624 .add = perf_trace_add, 10625 .del = perf_trace_del, 10626 .start = perf_swevent_start, 10627 .stop = perf_swevent_stop, 10628 .read = perf_swevent_read, 10629 .attr_groups = kprobe_attr_groups, 10630 }; 10631 10632 static int perf_kprobe_event_init(struct perf_event *event) 10633 { 10634 int err; 10635 bool is_retprobe; 10636 10637 if (event->attr.type != perf_kprobe.type) 10638 return -ENOENT; 10639 10640 if (!perfmon_capable()) 10641 return -EACCES; 10642 10643 /* 10644 * no branch sampling for probe events 10645 */ 10646 if (has_branch_stack(event)) 10647 return -EOPNOTSUPP; 10648 10649 is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE; 10650 err = perf_kprobe_init(event, is_retprobe); 10651 if (err) 10652 return err; 10653 10654 event->destroy = perf_kprobe_destroy; 10655 10656 return 0; 10657 } 10658 #endif /* CONFIG_KPROBE_EVENTS */ 10659 10660 #ifdef CONFIG_UPROBE_EVENTS 10661 PMU_FORMAT_ATTR(ref_ctr_offset, "config:32-63"); 10662 10663 static struct attribute *uprobe_attrs[] = { 10664 &format_attr_retprobe.attr, 10665 &format_attr_ref_ctr_offset.attr, 10666 NULL, 10667 }; 10668 10669 static struct attribute_group uprobe_format_group = { 10670 .name = "format", 10671 .attrs = uprobe_attrs, 10672 }; 10673 10674 static const struct attribute_group *uprobe_attr_groups[] = { 10675 &uprobe_format_group, 10676 NULL, 10677 }; 10678 10679 static int perf_uprobe_event_init(struct perf_event *event); 10680 static struct pmu perf_uprobe = { 10681 .task_ctx_nr = perf_sw_context, 10682 .event_init = perf_uprobe_event_init, 10683 .add = perf_trace_add, 10684 .del = perf_trace_del, 10685 .start = perf_swevent_start, 10686 .stop = perf_swevent_stop, 10687 .read = perf_swevent_read, 10688 .attr_groups = uprobe_attr_groups, 10689 }; 10690 10691 static int perf_uprobe_event_init(struct perf_event *event) 10692 { 10693 int err; 10694 unsigned long ref_ctr_offset; 10695 bool is_retprobe; 10696 10697 if (event->attr.type != perf_uprobe.type) 10698 return -ENOENT; 10699 10700 if (!perfmon_capable()) 10701 return -EACCES; 10702 10703 /* 10704 * no branch sampling for probe events 10705 */ 10706 if (has_branch_stack(event)) 10707 return -EOPNOTSUPP; 10708 10709 is_retprobe = event->attr.config & PERF_PROBE_CONFIG_IS_RETPROBE; 10710 ref_ctr_offset = event->attr.config >> PERF_UPROBE_REF_CTR_OFFSET_SHIFT; 10711 err = perf_uprobe_init(event, ref_ctr_offset, is_retprobe); 10712 if (err) 10713 return err; 10714 10715 event->destroy = perf_uprobe_destroy; 10716 10717 return 0; 10718 } 10719 #endif /* CONFIG_UPROBE_EVENTS */ 10720 10721 static inline void perf_tp_register(void) 10722 { 10723 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); 10724 #ifdef CONFIG_KPROBE_EVENTS 10725 perf_pmu_register(&perf_kprobe, "kprobe", -1); 10726 #endif 10727 #ifdef CONFIG_UPROBE_EVENTS 10728 perf_pmu_register(&perf_uprobe, "uprobe", -1); 10729 #endif 10730 } 10731 10732 static void perf_event_free_filter(struct perf_event *event) 10733 { 10734 ftrace_profile_free_filter(event); 10735 } 10736 10737 /* 10738 * returns true if the event is a tracepoint, or a kprobe/upprobe created 10739 * with perf_event_open() 10740 */ 10741 static inline bool perf_event_is_tracing(struct perf_event *event) 10742 { 10743 if (event->pmu == &perf_tracepoint) 10744 return true; 10745 #ifdef CONFIG_KPROBE_EVENTS 10746 if (event->pmu == &perf_kprobe) 10747 return true; 10748 #endif 10749 #ifdef CONFIG_UPROBE_EVENTS 10750 if (event->pmu == &perf_uprobe) 10751 return true; 10752 #endif 10753 return false; 10754 } 10755 10756 int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog, 10757 u64 bpf_cookie) 10758 { 10759 bool is_kprobe, is_uprobe, is_tracepoint, is_syscall_tp; 10760 10761 if (!perf_event_is_tracing(event)) 10762 return perf_event_set_bpf_handler(event, prog, bpf_cookie); 10763 10764 is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_KPROBE; 10765 is_uprobe = event->tp_event->flags & TRACE_EVENT_FL_UPROBE; 10766 is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT; 10767 is_syscall_tp = is_syscall_trace_event(event->tp_event); 10768 if (!is_kprobe && !is_uprobe && !is_tracepoint && !is_syscall_tp) 10769 /* bpf programs can only be attached to u/kprobe or tracepoint */ 10770 return -EINVAL; 10771 10772 if (((is_kprobe || is_uprobe) && prog->type != BPF_PROG_TYPE_KPROBE) || 10773 (is_tracepoint && prog->type != BPF_PROG_TYPE_TRACEPOINT) || 10774 (is_syscall_tp && prog->type != BPF_PROG_TYPE_TRACEPOINT)) 10775 return -EINVAL; 10776 10777 if (prog->type == BPF_PROG_TYPE_KPROBE && prog->sleepable && !is_uprobe) 10778 /* only uprobe programs are allowed to be sleepable */ 10779 return -EINVAL; 10780 10781 /* Kprobe override only works for kprobes, not uprobes. */ 10782 if (prog->kprobe_override && !is_kprobe) 10783 return -EINVAL; 10784 10785 if (is_tracepoint || is_syscall_tp) { 10786 int off = trace_event_get_offsets(event->tp_event); 10787 10788 if (prog->aux->max_ctx_offset > off) 10789 return -EACCES; 10790 } 10791 10792 return perf_event_attach_bpf_prog(event, prog, bpf_cookie); 10793 } 10794 10795 void perf_event_free_bpf_prog(struct perf_event *event) 10796 { 10797 if (!perf_event_is_tracing(event)) { 10798 perf_event_free_bpf_handler(event); 10799 return; 10800 } 10801 perf_event_detach_bpf_prog(event); 10802 } 10803 10804 #else 10805 10806 static inline void perf_tp_register(void) 10807 { 10808 } 10809 10810 static void perf_event_free_filter(struct perf_event *event) 10811 { 10812 } 10813 10814 int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog, 10815 u64 bpf_cookie) 10816 { 10817 return -ENOENT; 10818 } 10819 10820 void perf_event_free_bpf_prog(struct perf_event *event) 10821 { 10822 } 10823 #endif /* CONFIG_EVENT_TRACING */ 10824 10825 #ifdef CONFIG_HAVE_HW_BREAKPOINT 10826 void perf_bp_event(struct perf_event *bp, void *data) 10827 { 10828 struct perf_sample_data sample; 10829 struct pt_regs *regs = data; 10830 10831 perf_sample_data_init(&sample, bp->attr.bp_addr, 0); 10832 10833 if (!bp->hw.state && !perf_exclude_event(bp, regs)) 10834 perf_swevent_event(bp, 1, &sample, regs); 10835 } 10836 #endif 10837 10838 /* 10839 * Allocate a new address filter 10840 */ 10841 static struct perf_addr_filter * 10842 perf_addr_filter_new(struct perf_event *event, struct list_head *filters) 10843 { 10844 int node = cpu_to_node(event->cpu == -1 ? 0 : event->cpu); 10845 struct perf_addr_filter *filter; 10846 10847 filter = kzalloc_node(sizeof(*filter), GFP_KERNEL, node); 10848 if (!filter) 10849 return NULL; 10850 10851 INIT_LIST_HEAD(&filter->entry); 10852 list_add_tail(&filter->entry, filters); 10853 10854 return filter; 10855 } 10856 10857 static void free_filters_list(struct list_head *filters) 10858 { 10859 struct perf_addr_filter *filter, *iter; 10860 10861 list_for_each_entry_safe(filter, iter, filters, entry) { 10862 path_put(&filter->path); 10863 list_del(&filter->entry); 10864 kfree(filter); 10865 } 10866 } 10867 10868 /* 10869 * Free existing address filters and optionally install new ones 10870 */ 10871 static void perf_addr_filters_splice(struct perf_event *event, 10872 struct list_head *head) 10873 { 10874 unsigned long flags; 10875 LIST_HEAD(list); 10876 10877 if (!has_addr_filter(event)) 10878 return; 10879 10880 /* don't bother with children, they don't have their own filters */ 10881 if (event->parent) 10882 return; 10883 10884 raw_spin_lock_irqsave(&event->addr_filters.lock, flags); 10885 10886 list_splice_init(&event->addr_filters.list, &list); 10887 if (head) 10888 list_splice(head, &event->addr_filters.list); 10889 10890 raw_spin_unlock_irqrestore(&event->addr_filters.lock, flags); 10891 10892 free_filters_list(&list); 10893 } 10894 10895 /* 10896 * Scan through mm's vmas and see if one of them matches the 10897 * @filter; if so, adjust filter's address range. 10898 * Called with mm::mmap_lock down for reading. 10899 */ 10900 static void perf_addr_filter_apply(struct perf_addr_filter *filter, 10901 struct mm_struct *mm, 10902 struct perf_addr_filter_range *fr) 10903 { 10904 struct vm_area_struct *vma; 10905 VMA_ITERATOR(vmi, mm, 0); 10906 10907 for_each_vma(vmi, vma) { 10908 if (!vma->vm_file) 10909 continue; 10910 10911 if (perf_addr_filter_vma_adjust(filter, vma, fr)) 10912 return; 10913 } 10914 } 10915 10916 /* 10917 * Update event's address range filters based on the 10918 * task's existing mappings, if any. 10919 */ 10920 static void perf_event_addr_filters_apply(struct perf_event *event) 10921 { 10922 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); 10923 struct task_struct *task = READ_ONCE(event->ctx->task); 10924 struct perf_addr_filter *filter; 10925 struct mm_struct *mm = NULL; 10926 unsigned int count = 0; 10927 unsigned long flags; 10928 10929 /* 10930 * We may observe TASK_TOMBSTONE, which means that the event tear-down 10931 * will stop on the parent's child_mutex that our caller is also holding 10932 */ 10933 if (task == TASK_TOMBSTONE) 10934 return; 10935 10936 if (ifh->nr_file_filters) { 10937 mm = get_task_mm(task); 10938 if (!mm) 10939 goto restart; 10940 10941 mmap_read_lock(mm); 10942 } 10943 10944 raw_spin_lock_irqsave(&ifh->lock, flags); 10945 list_for_each_entry(filter, &ifh->list, entry) { 10946 if (filter->path.dentry) { 10947 /* 10948 * Adjust base offset if the filter is associated to a 10949 * binary that needs to be mapped: 10950 */ 10951 event->addr_filter_ranges[count].start = 0; 10952 event->addr_filter_ranges[count].size = 0; 10953 10954 perf_addr_filter_apply(filter, mm, &event->addr_filter_ranges[count]); 10955 } else { 10956 event->addr_filter_ranges[count].start = filter->offset; 10957 event->addr_filter_ranges[count].size = filter->size; 10958 } 10959 10960 count++; 10961 } 10962 10963 event->addr_filters_gen++; 10964 raw_spin_unlock_irqrestore(&ifh->lock, flags); 10965 10966 if (ifh->nr_file_filters) { 10967 mmap_read_unlock(mm); 10968 10969 mmput(mm); 10970 } 10971 10972 restart: 10973 perf_event_stop(event, 1); 10974 } 10975 10976 /* 10977 * Address range filtering: limiting the data to certain 10978 * instruction address ranges. Filters are ioctl()ed to us from 10979 * userspace as ascii strings. 10980 * 10981 * Filter string format: 10982 * 10983 * ACTION RANGE_SPEC 10984 * where ACTION is one of the 10985 * * "filter": limit the trace to this region 10986 * * "start": start tracing from this address 10987 * * "stop": stop tracing at this address/region; 10988 * RANGE_SPEC is 10989 * * for kernel addresses: <start address>[/<size>] 10990 * * for object files: <start address>[/<size>]@</path/to/object/file> 10991 * 10992 * if <size> is not specified or is zero, the range is treated as a single 10993 * address; not valid for ACTION=="filter". 10994 */ 10995 enum { 10996 IF_ACT_NONE = -1, 10997 IF_ACT_FILTER, 10998 IF_ACT_START, 10999 IF_ACT_STOP, 11000 IF_SRC_FILE, 11001 IF_SRC_KERNEL, 11002 IF_SRC_FILEADDR, 11003 IF_SRC_KERNELADDR, 11004 }; 11005 11006 enum { 11007 IF_STATE_ACTION = 0, 11008 IF_STATE_SOURCE, 11009 IF_STATE_END, 11010 }; 11011 11012 static const match_table_t if_tokens = { 11013 { IF_ACT_FILTER, "filter" }, 11014 { IF_ACT_START, "start" }, 11015 { IF_ACT_STOP, "stop" }, 11016 { IF_SRC_FILE, "%u/%u@%s" }, 11017 { IF_SRC_KERNEL, "%u/%u" }, 11018 { IF_SRC_FILEADDR, "%u@%s" }, 11019 { IF_SRC_KERNELADDR, "%u" }, 11020 { IF_ACT_NONE, NULL }, 11021 }; 11022 11023 /* 11024 * Address filter string parser 11025 */ 11026 static int 11027 perf_event_parse_addr_filter(struct perf_event *event, char *fstr, 11028 struct list_head *filters) 11029 { 11030 struct perf_addr_filter *filter = NULL; 11031 char *start, *orig, *filename = NULL; 11032 substring_t args[MAX_OPT_ARGS]; 11033 int state = IF_STATE_ACTION, token; 11034 unsigned int kernel = 0; 11035 int ret = -EINVAL; 11036 11037 orig = fstr = kstrdup(fstr, GFP_KERNEL); 11038 if (!fstr) 11039 return -ENOMEM; 11040 11041 while ((start = strsep(&fstr, " ,\n")) != NULL) { 11042 static const enum perf_addr_filter_action_t actions[] = { 11043 [IF_ACT_FILTER] = PERF_ADDR_FILTER_ACTION_FILTER, 11044 [IF_ACT_START] = PERF_ADDR_FILTER_ACTION_START, 11045 [IF_ACT_STOP] = PERF_ADDR_FILTER_ACTION_STOP, 11046 }; 11047 ret = -EINVAL; 11048 11049 if (!*start) 11050 continue; 11051 11052 /* filter definition begins */ 11053 if (state == IF_STATE_ACTION) { 11054 filter = perf_addr_filter_new(event, filters); 11055 if (!filter) 11056 goto fail; 11057 } 11058 11059 token = match_token(start, if_tokens, args); 11060 switch (token) { 11061 case IF_ACT_FILTER: 11062 case IF_ACT_START: 11063 case IF_ACT_STOP: 11064 if (state != IF_STATE_ACTION) 11065 goto fail; 11066 11067 filter->action = actions[token]; 11068 state = IF_STATE_SOURCE; 11069 break; 11070 11071 case IF_SRC_KERNELADDR: 11072 case IF_SRC_KERNEL: 11073 kernel = 1; 11074 fallthrough; 11075 11076 case IF_SRC_FILEADDR: 11077 case IF_SRC_FILE: 11078 if (state != IF_STATE_SOURCE) 11079 goto fail; 11080 11081 *args[0].to = 0; 11082 ret = kstrtoul(args[0].from, 0, &filter->offset); 11083 if (ret) 11084 goto fail; 11085 11086 if (token == IF_SRC_KERNEL || token == IF_SRC_FILE) { 11087 *args[1].to = 0; 11088 ret = kstrtoul(args[1].from, 0, &filter->size); 11089 if (ret) 11090 goto fail; 11091 } 11092 11093 if (token == IF_SRC_FILE || token == IF_SRC_FILEADDR) { 11094 int fpos = token == IF_SRC_FILE ? 2 : 1; 11095 11096 kfree(filename); 11097 filename = match_strdup(&args[fpos]); 11098 if (!filename) { 11099 ret = -ENOMEM; 11100 goto fail; 11101 } 11102 } 11103 11104 state = IF_STATE_END; 11105 break; 11106 11107 default: 11108 goto fail; 11109 } 11110 11111 /* 11112 * Filter definition is fully parsed, validate and install it. 11113 * Make sure that it doesn't contradict itself or the event's 11114 * attribute. 11115 */ 11116 if (state == IF_STATE_END) { 11117 ret = -EINVAL; 11118 11119 /* 11120 * ACTION "filter" must have a non-zero length region 11121 * specified. 11122 */ 11123 if (filter->action == PERF_ADDR_FILTER_ACTION_FILTER && 11124 !filter->size) 11125 goto fail; 11126 11127 if (!kernel) { 11128 if (!filename) 11129 goto fail; 11130 11131 /* 11132 * For now, we only support file-based filters 11133 * in per-task events; doing so for CPU-wide 11134 * events requires additional context switching 11135 * trickery, since same object code will be 11136 * mapped at different virtual addresses in 11137 * different processes. 11138 */ 11139 ret = -EOPNOTSUPP; 11140 if (!event->ctx->task) 11141 goto fail; 11142 11143 /* look up the path and grab its inode */ 11144 ret = kern_path(filename, LOOKUP_FOLLOW, 11145 &filter->path); 11146 if (ret) 11147 goto fail; 11148 11149 ret = -EINVAL; 11150 if (!filter->path.dentry || 11151 !S_ISREG(d_inode(filter->path.dentry) 11152 ->i_mode)) 11153 goto fail; 11154 11155 event->addr_filters.nr_file_filters++; 11156 } 11157 11158 /* ready to consume more filters */ 11159 kfree(filename); 11160 filename = NULL; 11161 state = IF_STATE_ACTION; 11162 filter = NULL; 11163 kernel = 0; 11164 } 11165 } 11166 11167 if (state != IF_STATE_ACTION) 11168 goto fail; 11169 11170 kfree(filename); 11171 kfree(orig); 11172 11173 return 0; 11174 11175 fail: 11176 kfree(filename); 11177 free_filters_list(filters); 11178 kfree(orig); 11179 11180 return ret; 11181 } 11182 11183 static int 11184 perf_event_set_addr_filter(struct perf_event *event, char *filter_str) 11185 { 11186 LIST_HEAD(filters); 11187 int ret; 11188 11189 /* 11190 * Since this is called in perf_ioctl() path, we're already holding 11191 * ctx::mutex. 11192 */ 11193 lockdep_assert_held(&event->ctx->mutex); 11194 11195 if (WARN_ON_ONCE(event->parent)) 11196 return -EINVAL; 11197 11198 ret = perf_event_parse_addr_filter(event, filter_str, &filters); 11199 if (ret) 11200 goto fail_clear_files; 11201 11202 ret = event->pmu->addr_filters_validate(&filters); 11203 if (ret) 11204 goto fail_free_filters; 11205 11206 /* remove existing filters, if any */ 11207 perf_addr_filters_splice(event, &filters); 11208 11209 /* install new filters */ 11210 perf_event_for_each_child(event, perf_event_addr_filters_apply); 11211 11212 return ret; 11213 11214 fail_free_filters: 11215 free_filters_list(&filters); 11216 11217 fail_clear_files: 11218 event->addr_filters.nr_file_filters = 0; 11219 11220 return ret; 11221 } 11222 11223 static int perf_event_set_filter(struct perf_event *event, void __user *arg) 11224 { 11225 int ret = -EINVAL; 11226 char *filter_str; 11227 11228 filter_str = strndup_user(arg, PAGE_SIZE); 11229 if (IS_ERR(filter_str)) 11230 return PTR_ERR(filter_str); 11231 11232 #ifdef CONFIG_EVENT_TRACING 11233 if (perf_event_is_tracing(event)) { 11234 struct perf_event_context *ctx = event->ctx; 11235 11236 /* 11237 * Beware, here be dragons!! 11238 * 11239 * the tracepoint muck will deadlock against ctx->mutex, but 11240 * the tracepoint stuff does not actually need it. So 11241 * temporarily drop ctx->mutex. As per perf_event_ctx_lock() we 11242 * already have a reference on ctx. 11243 * 11244 * This can result in event getting moved to a different ctx, 11245 * but that does not affect the tracepoint state. 11246 */ 11247 mutex_unlock(&ctx->mutex); 11248 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); 11249 mutex_lock(&ctx->mutex); 11250 } else 11251 #endif 11252 if (has_addr_filter(event)) 11253 ret = perf_event_set_addr_filter(event, filter_str); 11254 11255 kfree(filter_str); 11256 return ret; 11257 } 11258 11259 /* 11260 * hrtimer based swevent callback 11261 */ 11262 11263 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) 11264 { 11265 enum hrtimer_restart ret = HRTIMER_RESTART; 11266 struct perf_sample_data data; 11267 struct pt_regs *regs; 11268 struct perf_event *event; 11269 u64 period; 11270 11271 event = container_of(hrtimer, struct perf_event, hw.hrtimer); 11272 11273 if (event->state != PERF_EVENT_STATE_ACTIVE) 11274 return HRTIMER_NORESTART; 11275 11276 event->pmu->read(event); 11277 11278 perf_sample_data_init(&data, 0, event->hw.last_period); 11279 regs = get_irq_regs(); 11280 11281 if (regs && !perf_exclude_event(event, regs)) { 11282 if (!(event->attr.exclude_idle && is_idle_task(current))) 11283 if (__perf_event_overflow(event, 1, &data, regs)) 11284 ret = HRTIMER_NORESTART; 11285 } 11286 11287 period = max_t(u64, 10000, event->hw.sample_period); 11288 hrtimer_forward_now(hrtimer, ns_to_ktime(period)); 11289 11290 return ret; 11291 } 11292 11293 static void perf_swevent_start_hrtimer(struct perf_event *event) 11294 { 11295 struct hw_perf_event *hwc = &event->hw; 11296 s64 period; 11297 11298 if (!is_sampling_event(event)) 11299 return; 11300 11301 period = local64_read(&hwc->period_left); 11302 if (period) { 11303 if (period < 0) 11304 period = 10000; 11305 11306 local64_set(&hwc->period_left, 0); 11307 } else { 11308 period = max_t(u64, 10000, hwc->sample_period); 11309 } 11310 hrtimer_start(&hwc->hrtimer, ns_to_ktime(period), 11311 HRTIMER_MODE_REL_PINNED_HARD); 11312 } 11313 11314 static void perf_swevent_cancel_hrtimer(struct perf_event *event) 11315 { 11316 struct hw_perf_event *hwc = &event->hw; 11317 11318 if (is_sampling_event(event)) { 11319 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); 11320 local64_set(&hwc->period_left, ktime_to_ns(remaining)); 11321 11322 hrtimer_cancel(&hwc->hrtimer); 11323 } 11324 } 11325 11326 static void perf_swevent_init_hrtimer(struct perf_event *event) 11327 { 11328 struct hw_perf_event *hwc = &event->hw; 11329 11330 if (!is_sampling_event(event)) 11331 return; 11332 11333 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD); 11334 hwc->hrtimer.function = perf_swevent_hrtimer; 11335 11336 /* 11337 * Since hrtimers have a fixed rate, we can do a static freq->period 11338 * mapping and avoid the whole period adjust feedback stuff. 11339 */ 11340 if (event->attr.freq) { 11341 long freq = event->attr.sample_freq; 11342 11343 event->attr.sample_period = NSEC_PER_SEC / freq; 11344 hwc->sample_period = event->attr.sample_period; 11345 local64_set(&hwc->period_left, hwc->sample_period); 11346 hwc->last_period = hwc->sample_period; 11347 event->attr.freq = 0; 11348 } 11349 } 11350 11351 /* 11352 * Software event: cpu wall time clock 11353 */ 11354 11355 static void cpu_clock_event_update(struct perf_event *event) 11356 { 11357 s64 prev; 11358 u64 now; 11359 11360 now = local_clock(); 11361 prev = local64_xchg(&event->hw.prev_count, now); 11362 local64_add(now - prev, &event->count); 11363 } 11364 11365 static void cpu_clock_event_start(struct perf_event *event, int flags) 11366 { 11367 local64_set(&event->hw.prev_count, local_clock()); 11368 perf_swevent_start_hrtimer(event); 11369 } 11370 11371 static void cpu_clock_event_stop(struct perf_event *event, int flags) 11372 { 11373 perf_swevent_cancel_hrtimer(event); 11374 cpu_clock_event_update(event); 11375 } 11376 11377 static int cpu_clock_event_add(struct perf_event *event, int flags) 11378 { 11379 if (flags & PERF_EF_START) 11380 cpu_clock_event_start(event, flags); 11381 perf_event_update_userpage(event); 11382 11383 return 0; 11384 } 11385 11386 static void cpu_clock_event_del(struct perf_event *event, int flags) 11387 { 11388 cpu_clock_event_stop(event, flags); 11389 } 11390 11391 static void cpu_clock_event_read(struct perf_event *event) 11392 { 11393 cpu_clock_event_update(event); 11394 } 11395 11396 static int cpu_clock_event_init(struct perf_event *event) 11397 { 11398 if (event->attr.type != perf_cpu_clock.type) 11399 return -ENOENT; 11400 11401 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) 11402 return -ENOENT; 11403 11404 /* 11405 * no branch sampling for software events 11406 */ 11407 if (has_branch_stack(event)) 11408 return -EOPNOTSUPP; 11409 11410 perf_swevent_init_hrtimer(event); 11411 11412 return 0; 11413 } 11414 11415 static struct pmu perf_cpu_clock = { 11416 .task_ctx_nr = perf_sw_context, 11417 11418 .capabilities = PERF_PMU_CAP_NO_NMI, 11419 .dev = PMU_NULL_DEV, 11420 11421 .event_init = cpu_clock_event_init, 11422 .add = cpu_clock_event_add, 11423 .del = cpu_clock_event_del, 11424 .start = cpu_clock_event_start, 11425 .stop = cpu_clock_event_stop, 11426 .read = cpu_clock_event_read, 11427 }; 11428 11429 /* 11430 * Software event: task time clock 11431 */ 11432 11433 static void task_clock_event_update(struct perf_event *event, u64 now) 11434 { 11435 u64 prev; 11436 s64 delta; 11437 11438 prev = local64_xchg(&event->hw.prev_count, now); 11439 delta = now - prev; 11440 local64_add(delta, &event->count); 11441 } 11442 11443 static void task_clock_event_start(struct perf_event *event, int flags) 11444 { 11445 local64_set(&event->hw.prev_count, event->ctx->time); 11446 perf_swevent_start_hrtimer(event); 11447 } 11448 11449 static void task_clock_event_stop(struct perf_event *event, int flags) 11450 { 11451 perf_swevent_cancel_hrtimer(event); 11452 task_clock_event_update(event, event->ctx->time); 11453 } 11454 11455 static int task_clock_event_add(struct perf_event *event, int flags) 11456 { 11457 if (flags & PERF_EF_START) 11458 task_clock_event_start(event, flags); 11459 perf_event_update_userpage(event); 11460 11461 return 0; 11462 } 11463 11464 static void task_clock_event_del(struct perf_event *event, int flags) 11465 { 11466 task_clock_event_stop(event, PERF_EF_UPDATE); 11467 } 11468 11469 static void task_clock_event_read(struct perf_event *event) 11470 { 11471 u64 now = perf_clock(); 11472 u64 delta = now - event->ctx->timestamp; 11473 u64 time = event->ctx->time + delta; 11474 11475 task_clock_event_update(event, time); 11476 } 11477 11478 static int task_clock_event_init(struct perf_event *event) 11479 { 11480 if (event->attr.type != perf_task_clock.type) 11481 return -ENOENT; 11482 11483 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) 11484 return -ENOENT; 11485 11486 /* 11487 * no branch sampling for software events 11488 */ 11489 if (has_branch_stack(event)) 11490 return -EOPNOTSUPP; 11491 11492 perf_swevent_init_hrtimer(event); 11493 11494 return 0; 11495 } 11496 11497 static struct pmu perf_task_clock = { 11498 .task_ctx_nr = perf_sw_context, 11499 11500 .capabilities = PERF_PMU_CAP_NO_NMI, 11501 .dev = PMU_NULL_DEV, 11502 11503 .event_init = task_clock_event_init, 11504 .add = task_clock_event_add, 11505 .del = task_clock_event_del, 11506 .start = task_clock_event_start, 11507 .stop = task_clock_event_stop, 11508 .read = task_clock_event_read, 11509 }; 11510 11511 static void perf_pmu_nop_void(struct pmu *pmu) 11512 { 11513 } 11514 11515 static void perf_pmu_nop_txn(struct pmu *pmu, unsigned int flags) 11516 { 11517 } 11518 11519 static int perf_pmu_nop_int(struct pmu *pmu) 11520 { 11521 return 0; 11522 } 11523 11524 static int perf_event_nop_int(struct perf_event *event, u64 value) 11525 { 11526 return 0; 11527 } 11528 11529 static DEFINE_PER_CPU(unsigned int, nop_txn_flags); 11530 11531 static void perf_pmu_start_txn(struct pmu *pmu, unsigned int flags) 11532 { 11533 __this_cpu_write(nop_txn_flags, flags); 11534 11535 if (flags & ~PERF_PMU_TXN_ADD) 11536 return; 11537 11538 perf_pmu_disable(pmu); 11539 } 11540 11541 static int perf_pmu_commit_txn(struct pmu *pmu) 11542 { 11543 unsigned int flags = __this_cpu_read(nop_txn_flags); 11544 11545 __this_cpu_write(nop_txn_flags, 0); 11546 11547 if (flags & ~PERF_PMU_TXN_ADD) 11548 return 0; 11549 11550 perf_pmu_enable(pmu); 11551 return 0; 11552 } 11553 11554 static void perf_pmu_cancel_txn(struct pmu *pmu) 11555 { 11556 unsigned int flags = __this_cpu_read(nop_txn_flags); 11557 11558 __this_cpu_write(nop_txn_flags, 0); 11559 11560 if (flags & ~PERF_PMU_TXN_ADD) 11561 return; 11562 11563 perf_pmu_enable(pmu); 11564 } 11565 11566 static int perf_event_idx_default(struct perf_event *event) 11567 { 11568 return 0; 11569 } 11570 11571 static void free_pmu_context(struct pmu *pmu) 11572 { 11573 free_percpu(pmu->cpu_pmu_context); 11574 } 11575 11576 /* 11577 * Let userspace know that this PMU supports address range filtering: 11578 */ 11579 static ssize_t nr_addr_filters_show(struct device *dev, 11580 struct device_attribute *attr, 11581 char *page) 11582 { 11583 struct pmu *pmu = dev_get_drvdata(dev); 11584 11585 return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->nr_addr_filters); 11586 } 11587 DEVICE_ATTR_RO(nr_addr_filters); 11588 11589 static struct idr pmu_idr; 11590 11591 static ssize_t 11592 type_show(struct device *dev, struct device_attribute *attr, char *page) 11593 { 11594 struct pmu *pmu = dev_get_drvdata(dev); 11595 11596 return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->type); 11597 } 11598 static DEVICE_ATTR_RO(type); 11599 11600 static ssize_t 11601 perf_event_mux_interval_ms_show(struct device *dev, 11602 struct device_attribute *attr, 11603 char *page) 11604 { 11605 struct pmu *pmu = dev_get_drvdata(dev); 11606 11607 return scnprintf(page, PAGE_SIZE - 1, "%d\n", pmu->hrtimer_interval_ms); 11608 } 11609 11610 static DEFINE_MUTEX(mux_interval_mutex); 11611 11612 static ssize_t 11613 perf_event_mux_interval_ms_store(struct device *dev, 11614 struct device_attribute *attr, 11615 const char *buf, size_t count) 11616 { 11617 struct pmu *pmu = dev_get_drvdata(dev); 11618 int timer, cpu, ret; 11619 11620 ret = kstrtoint(buf, 0, &timer); 11621 if (ret) 11622 return ret; 11623 11624 if (timer < 1) 11625 return -EINVAL; 11626 11627 /* same value, noting to do */ 11628 if (timer == pmu->hrtimer_interval_ms) 11629 return count; 11630 11631 mutex_lock(&mux_interval_mutex); 11632 pmu->hrtimer_interval_ms = timer; 11633 11634 /* update all cpuctx for this PMU */ 11635 cpus_read_lock(); 11636 for_each_online_cpu(cpu) { 11637 struct perf_cpu_pmu_context *cpc; 11638 cpc = per_cpu_ptr(pmu->cpu_pmu_context, cpu); 11639 cpc->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer); 11640 11641 cpu_function_call(cpu, perf_mux_hrtimer_restart_ipi, cpc); 11642 } 11643 cpus_read_unlock(); 11644 mutex_unlock(&mux_interval_mutex); 11645 11646 return count; 11647 } 11648 static DEVICE_ATTR_RW(perf_event_mux_interval_ms); 11649 11650 static inline const struct cpumask *perf_scope_cpu_topology_cpumask(unsigned int scope, int cpu) 11651 { 11652 switch (scope) { 11653 case PERF_PMU_SCOPE_CORE: 11654 return topology_sibling_cpumask(cpu); 11655 case PERF_PMU_SCOPE_DIE: 11656 return topology_die_cpumask(cpu); 11657 case PERF_PMU_SCOPE_CLUSTER: 11658 return topology_cluster_cpumask(cpu); 11659 case PERF_PMU_SCOPE_PKG: 11660 return topology_core_cpumask(cpu); 11661 case PERF_PMU_SCOPE_SYS_WIDE: 11662 return cpu_online_mask; 11663 } 11664 11665 return NULL; 11666 } 11667 11668 static inline struct cpumask *perf_scope_cpumask(unsigned int scope) 11669 { 11670 switch (scope) { 11671 case PERF_PMU_SCOPE_CORE: 11672 return perf_online_core_mask; 11673 case PERF_PMU_SCOPE_DIE: 11674 return perf_online_die_mask; 11675 case PERF_PMU_SCOPE_CLUSTER: 11676 return perf_online_cluster_mask; 11677 case PERF_PMU_SCOPE_PKG: 11678 return perf_online_pkg_mask; 11679 case PERF_PMU_SCOPE_SYS_WIDE: 11680 return perf_online_sys_mask; 11681 } 11682 11683 return NULL; 11684 } 11685 11686 static ssize_t cpumask_show(struct device *dev, struct device_attribute *attr, 11687 char *buf) 11688 { 11689 struct pmu *pmu = dev_get_drvdata(dev); 11690 struct cpumask *mask = perf_scope_cpumask(pmu->scope); 11691 11692 if (mask) 11693 return cpumap_print_to_pagebuf(true, buf, mask); 11694 return 0; 11695 } 11696 11697 static DEVICE_ATTR_RO(cpumask); 11698 11699 static struct attribute *pmu_dev_attrs[] = { 11700 &dev_attr_type.attr, 11701 &dev_attr_perf_event_mux_interval_ms.attr, 11702 &dev_attr_nr_addr_filters.attr, 11703 &dev_attr_cpumask.attr, 11704 NULL, 11705 }; 11706 11707 static umode_t pmu_dev_is_visible(struct kobject *kobj, struct attribute *a, int n) 11708 { 11709 struct device *dev = kobj_to_dev(kobj); 11710 struct pmu *pmu = dev_get_drvdata(dev); 11711 11712 if (n == 2 && !pmu->nr_addr_filters) 11713 return 0; 11714 11715 /* cpumask */ 11716 if (n == 3 && pmu->scope == PERF_PMU_SCOPE_NONE) 11717 return 0; 11718 11719 return a->mode; 11720 } 11721 11722 static struct attribute_group pmu_dev_attr_group = { 11723 .is_visible = pmu_dev_is_visible, 11724 .attrs = pmu_dev_attrs, 11725 }; 11726 11727 static const struct attribute_group *pmu_dev_groups[] = { 11728 &pmu_dev_attr_group, 11729 NULL, 11730 }; 11731 11732 static int pmu_bus_running; 11733 static struct bus_type pmu_bus = { 11734 .name = "event_source", 11735 .dev_groups = pmu_dev_groups, 11736 }; 11737 11738 static void pmu_dev_release(struct device *dev) 11739 { 11740 kfree(dev); 11741 } 11742 11743 static int pmu_dev_alloc(struct pmu *pmu) 11744 { 11745 int ret = -ENOMEM; 11746 11747 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); 11748 if (!pmu->dev) 11749 goto out; 11750 11751 pmu->dev->groups = pmu->attr_groups; 11752 device_initialize(pmu->dev); 11753 11754 dev_set_drvdata(pmu->dev, pmu); 11755 pmu->dev->bus = &pmu_bus; 11756 pmu->dev->parent = pmu->parent; 11757 pmu->dev->release = pmu_dev_release; 11758 11759 ret = dev_set_name(pmu->dev, "%s", pmu->name); 11760 if (ret) 11761 goto free_dev; 11762 11763 ret = device_add(pmu->dev); 11764 if (ret) 11765 goto free_dev; 11766 11767 if (pmu->attr_update) { 11768 ret = sysfs_update_groups(&pmu->dev->kobj, pmu->attr_update); 11769 if (ret) 11770 goto del_dev; 11771 } 11772 11773 out: 11774 return ret; 11775 11776 del_dev: 11777 device_del(pmu->dev); 11778 11779 free_dev: 11780 put_device(pmu->dev); 11781 goto out; 11782 } 11783 11784 static struct lock_class_key cpuctx_mutex; 11785 static struct lock_class_key cpuctx_lock; 11786 11787 int perf_pmu_register(struct pmu *pmu, const char *name, int type) 11788 { 11789 int cpu, ret, max = PERF_TYPE_MAX; 11790 11791 mutex_lock(&pmus_lock); 11792 ret = -ENOMEM; 11793 pmu->pmu_disable_count = alloc_percpu(int); 11794 if (!pmu->pmu_disable_count) 11795 goto unlock; 11796 11797 pmu->type = -1; 11798 if (WARN_ONCE(!name, "Can not register anonymous pmu.\n")) { 11799 ret = -EINVAL; 11800 goto free_pdc; 11801 } 11802 11803 if (WARN_ONCE(pmu->scope >= PERF_PMU_MAX_SCOPE, "Can not register a pmu with an invalid scope.\n")) { 11804 ret = -EINVAL; 11805 goto free_pdc; 11806 } 11807 11808 pmu->name = name; 11809 11810 if (type >= 0) 11811 max = type; 11812 11813 ret = idr_alloc(&pmu_idr, pmu, max, 0, GFP_KERNEL); 11814 if (ret < 0) 11815 goto free_pdc; 11816 11817 WARN_ON(type >= 0 && ret != type); 11818 11819 type = ret; 11820 pmu->type = type; 11821 11822 if (pmu_bus_running && !pmu->dev) { 11823 ret = pmu_dev_alloc(pmu); 11824 if (ret) 11825 goto free_idr; 11826 } 11827 11828 ret = -ENOMEM; 11829 pmu->cpu_pmu_context = alloc_percpu(struct perf_cpu_pmu_context); 11830 if (!pmu->cpu_pmu_context) 11831 goto free_dev; 11832 11833 for_each_possible_cpu(cpu) { 11834 struct perf_cpu_pmu_context *cpc; 11835 11836 cpc = per_cpu_ptr(pmu->cpu_pmu_context, cpu); 11837 __perf_init_event_pmu_context(&cpc->epc, pmu); 11838 __perf_mux_hrtimer_init(cpc, cpu); 11839 } 11840 11841 if (!pmu->start_txn) { 11842 if (pmu->pmu_enable) { 11843 /* 11844 * If we have pmu_enable/pmu_disable calls, install 11845 * transaction stubs that use that to try and batch 11846 * hardware accesses. 11847 */ 11848 pmu->start_txn = perf_pmu_start_txn; 11849 pmu->commit_txn = perf_pmu_commit_txn; 11850 pmu->cancel_txn = perf_pmu_cancel_txn; 11851 } else { 11852 pmu->start_txn = perf_pmu_nop_txn; 11853 pmu->commit_txn = perf_pmu_nop_int; 11854 pmu->cancel_txn = perf_pmu_nop_void; 11855 } 11856 } 11857 11858 if (!pmu->pmu_enable) { 11859 pmu->pmu_enable = perf_pmu_nop_void; 11860 pmu->pmu_disable = perf_pmu_nop_void; 11861 } 11862 11863 if (!pmu->check_period) 11864 pmu->check_period = perf_event_nop_int; 11865 11866 if (!pmu->event_idx) 11867 pmu->event_idx = perf_event_idx_default; 11868 11869 list_add_rcu(&pmu->entry, &pmus); 11870 atomic_set(&pmu->exclusive_cnt, 0); 11871 ret = 0; 11872 unlock: 11873 mutex_unlock(&pmus_lock); 11874 11875 return ret; 11876 11877 free_dev: 11878 if (pmu->dev && pmu->dev != PMU_NULL_DEV) { 11879 device_del(pmu->dev); 11880 put_device(pmu->dev); 11881 } 11882 11883 free_idr: 11884 idr_remove(&pmu_idr, pmu->type); 11885 11886 free_pdc: 11887 free_percpu(pmu->pmu_disable_count); 11888 goto unlock; 11889 } 11890 EXPORT_SYMBOL_GPL(perf_pmu_register); 11891 11892 void perf_pmu_unregister(struct pmu *pmu) 11893 { 11894 mutex_lock(&pmus_lock); 11895 list_del_rcu(&pmu->entry); 11896 11897 /* 11898 * We dereference the pmu list under both SRCU and regular RCU, so 11899 * synchronize against both of those. 11900 */ 11901 synchronize_srcu(&pmus_srcu); 11902 synchronize_rcu(); 11903 11904 free_percpu(pmu->pmu_disable_count); 11905 idr_remove(&pmu_idr, pmu->type); 11906 if (pmu_bus_running && pmu->dev && pmu->dev != PMU_NULL_DEV) { 11907 if (pmu->nr_addr_filters) 11908 device_remove_file(pmu->dev, &dev_attr_nr_addr_filters); 11909 device_del(pmu->dev); 11910 put_device(pmu->dev); 11911 } 11912 free_pmu_context(pmu); 11913 mutex_unlock(&pmus_lock); 11914 } 11915 EXPORT_SYMBOL_GPL(perf_pmu_unregister); 11916 11917 static inline bool has_extended_regs(struct perf_event *event) 11918 { 11919 return (event->attr.sample_regs_user & PERF_REG_EXTENDED_MASK) || 11920 (event->attr.sample_regs_intr & PERF_REG_EXTENDED_MASK); 11921 } 11922 11923 static int perf_try_init_event(struct pmu *pmu, struct perf_event *event) 11924 { 11925 struct perf_event_context *ctx = NULL; 11926 int ret; 11927 11928 if (!try_module_get(pmu->module)) 11929 return -ENODEV; 11930 11931 /* 11932 * A number of pmu->event_init() methods iterate the sibling_list to, 11933 * for example, validate if the group fits on the PMU. Therefore, 11934 * if this is a sibling event, acquire the ctx->mutex to protect 11935 * the sibling_list. 11936 */ 11937 if (event->group_leader != event && pmu->task_ctx_nr != perf_sw_context) { 11938 /* 11939 * This ctx->mutex can nest when we're called through 11940 * inheritance. See the perf_event_ctx_lock_nested() comment. 11941 */ 11942 ctx = perf_event_ctx_lock_nested(event->group_leader, 11943 SINGLE_DEPTH_NESTING); 11944 BUG_ON(!ctx); 11945 } 11946 11947 event->pmu = pmu; 11948 ret = pmu->event_init(event); 11949 11950 if (ctx) 11951 perf_event_ctx_unlock(event->group_leader, ctx); 11952 11953 if (!ret) { 11954 if (!(pmu->capabilities & PERF_PMU_CAP_EXTENDED_REGS) && 11955 has_extended_regs(event)) 11956 ret = -EOPNOTSUPP; 11957 11958 if (pmu->capabilities & PERF_PMU_CAP_NO_EXCLUDE && 11959 event_has_any_exclude_flag(event)) 11960 ret = -EINVAL; 11961 11962 if (pmu->scope != PERF_PMU_SCOPE_NONE && event->cpu >= 0) { 11963 const struct cpumask *cpumask = perf_scope_cpu_topology_cpumask(pmu->scope, event->cpu); 11964 struct cpumask *pmu_cpumask = perf_scope_cpumask(pmu->scope); 11965 int cpu; 11966 11967 if (pmu_cpumask && cpumask) { 11968 cpu = cpumask_any_and(pmu_cpumask, cpumask); 11969 if (cpu >= nr_cpu_ids) 11970 ret = -ENODEV; 11971 else 11972 event->event_caps |= PERF_EV_CAP_READ_SCOPE; 11973 } else { 11974 ret = -ENODEV; 11975 } 11976 } 11977 11978 if (ret && event->destroy) 11979 event->destroy(event); 11980 } 11981 11982 if (ret) 11983 module_put(pmu->module); 11984 11985 return ret; 11986 } 11987 11988 static struct pmu *perf_init_event(struct perf_event *event) 11989 { 11990 bool extended_type = false; 11991 int idx, type, ret; 11992 struct pmu *pmu; 11993 11994 idx = srcu_read_lock(&pmus_srcu); 11995 11996 /* 11997 * Save original type before calling pmu->event_init() since certain 11998 * pmus overwrites event->attr.type to forward event to another pmu. 11999 */ 12000 event->orig_type = event->attr.type; 12001 12002 /* Try parent's PMU first: */ 12003 if (event->parent && event->parent->pmu) { 12004 pmu = event->parent->pmu; 12005 ret = perf_try_init_event(pmu, event); 12006 if (!ret) 12007 goto unlock; 12008 } 12009 12010 /* 12011 * PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE 12012 * are often aliases for PERF_TYPE_RAW. 12013 */ 12014 type = event->attr.type; 12015 if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE) { 12016 type = event->attr.config >> PERF_PMU_TYPE_SHIFT; 12017 if (!type) { 12018 type = PERF_TYPE_RAW; 12019 } else { 12020 extended_type = true; 12021 event->attr.config &= PERF_HW_EVENT_MASK; 12022 } 12023 } 12024 12025 again: 12026 rcu_read_lock(); 12027 pmu = idr_find(&pmu_idr, type); 12028 rcu_read_unlock(); 12029 if (pmu) { 12030 if (event->attr.type != type && type != PERF_TYPE_RAW && 12031 !(pmu->capabilities & PERF_PMU_CAP_EXTENDED_HW_TYPE)) 12032 goto fail; 12033 12034 ret = perf_try_init_event(pmu, event); 12035 if (ret == -ENOENT && event->attr.type != type && !extended_type) { 12036 type = event->attr.type; 12037 goto again; 12038 } 12039 12040 if (ret) 12041 pmu = ERR_PTR(ret); 12042 12043 goto unlock; 12044 } 12045 12046 list_for_each_entry_rcu(pmu, &pmus, entry, lockdep_is_held(&pmus_srcu)) { 12047 ret = perf_try_init_event(pmu, event); 12048 if (!ret) 12049 goto unlock; 12050 12051 if (ret != -ENOENT) { 12052 pmu = ERR_PTR(ret); 12053 goto unlock; 12054 } 12055 } 12056 fail: 12057 pmu = ERR_PTR(-ENOENT); 12058 unlock: 12059 srcu_read_unlock(&pmus_srcu, idx); 12060 12061 return pmu; 12062 } 12063 12064 static void attach_sb_event(struct perf_event *event) 12065 { 12066 struct pmu_event_list *pel = per_cpu_ptr(&pmu_sb_events, event->cpu); 12067 12068 raw_spin_lock(&pel->lock); 12069 list_add_rcu(&event->sb_list, &pel->list); 12070 raw_spin_unlock(&pel->lock); 12071 } 12072 12073 /* 12074 * We keep a list of all !task (and therefore per-cpu) events 12075 * that need to receive side-band records. 12076 * 12077 * This avoids having to scan all the various PMU per-cpu contexts 12078 * looking for them. 12079 */ 12080 static void account_pmu_sb_event(struct perf_event *event) 12081 { 12082 if (is_sb_event(event)) 12083 attach_sb_event(event); 12084 } 12085 12086 /* Freq events need the tick to stay alive (see perf_event_task_tick). */ 12087 static void account_freq_event_nohz(void) 12088 { 12089 #ifdef CONFIG_NO_HZ_FULL 12090 /* Lock so we don't race with concurrent unaccount */ 12091 spin_lock(&nr_freq_lock); 12092 if (atomic_inc_return(&nr_freq_events) == 1) 12093 tick_nohz_dep_set(TICK_DEP_BIT_PERF_EVENTS); 12094 spin_unlock(&nr_freq_lock); 12095 #endif 12096 } 12097 12098 static void account_freq_event(void) 12099 { 12100 if (tick_nohz_full_enabled()) 12101 account_freq_event_nohz(); 12102 else 12103 atomic_inc(&nr_freq_events); 12104 } 12105 12106 12107 static void account_event(struct perf_event *event) 12108 { 12109 bool inc = false; 12110 12111 if (event->parent) 12112 return; 12113 12114 if (event->attach_state & (PERF_ATTACH_TASK | PERF_ATTACH_SCHED_CB)) 12115 inc = true; 12116 if (event->attr.mmap || event->attr.mmap_data) 12117 atomic_inc(&nr_mmap_events); 12118 if (event->attr.build_id) 12119 atomic_inc(&nr_build_id_events); 12120 if (event->attr.comm) 12121 atomic_inc(&nr_comm_events); 12122 if (event->attr.namespaces) 12123 atomic_inc(&nr_namespaces_events); 12124 if (event->attr.cgroup) 12125 atomic_inc(&nr_cgroup_events); 12126 if (event->attr.task) 12127 atomic_inc(&nr_task_events); 12128 if (event->attr.freq) 12129 account_freq_event(); 12130 if (event->attr.context_switch) { 12131 atomic_inc(&nr_switch_events); 12132 inc = true; 12133 } 12134 if (has_branch_stack(event)) 12135 inc = true; 12136 if (is_cgroup_event(event)) 12137 inc = true; 12138 if (event->attr.ksymbol) 12139 atomic_inc(&nr_ksymbol_events); 12140 if (event->attr.bpf_event) 12141 atomic_inc(&nr_bpf_events); 12142 if (event->attr.text_poke) 12143 atomic_inc(&nr_text_poke_events); 12144 12145 if (inc) { 12146 /* 12147 * We need the mutex here because static_branch_enable() 12148 * must complete *before* the perf_sched_count increment 12149 * becomes visible. 12150 */ 12151 if (atomic_inc_not_zero(&perf_sched_count)) 12152 goto enabled; 12153 12154 mutex_lock(&perf_sched_mutex); 12155 if (!atomic_read(&perf_sched_count)) { 12156 static_branch_enable(&perf_sched_events); 12157 /* 12158 * Guarantee that all CPUs observe they key change and 12159 * call the perf scheduling hooks before proceeding to 12160 * install events that need them. 12161 */ 12162 synchronize_rcu(); 12163 } 12164 /* 12165 * Now that we have waited for the sync_sched(), allow further 12166 * increments to by-pass the mutex. 12167 */ 12168 atomic_inc(&perf_sched_count); 12169 mutex_unlock(&perf_sched_mutex); 12170 } 12171 enabled: 12172 12173 account_pmu_sb_event(event); 12174 } 12175 12176 /* 12177 * Allocate and initialize an event structure 12178 */ 12179 static struct perf_event * 12180 perf_event_alloc(struct perf_event_attr *attr, int cpu, 12181 struct task_struct *task, 12182 struct perf_event *group_leader, 12183 struct perf_event *parent_event, 12184 perf_overflow_handler_t overflow_handler, 12185 void *context, int cgroup_fd) 12186 { 12187 struct pmu *pmu; 12188 struct perf_event *event; 12189 struct hw_perf_event *hwc; 12190 long err = -EINVAL; 12191 int node; 12192 12193 if ((unsigned)cpu >= nr_cpu_ids) { 12194 if (!task || cpu != -1) 12195 return ERR_PTR(-EINVAL); 12196 } 12197 if (attr->sigtrap && !task) { 12198 /* Requires a task: avoid signalling random tasks. */ 12199 return ERR_PTR(-EINVAL); 12200 } 12201 12202 node = (cpu >= 0) ? cpu_to_node(cpu) : -1; 12203 event = kmem_cache_alloc_node(perf_event_cache, GFP_KERNEL | __GFP_ZERO, 12204 node); 12205 if (!event) 12206 return ERR_PTR(-ENOMEM); 12207 12208 /* 12209 * Single events are their own group leaders, with an 12210 * empty sibling list: 12211 */ 12212 if (!group_leader) 12213 group_leader = event; 12214 12215 mutex_init(&event->child_mutex); 12216 INIT_LIST_HEAD(&event->child_list); 12217 12218 INIT_LIST_HEAD(&event->event_entry); 12219 INIT_LIST_HEAD(&event->sibling_list); 12220 INIT_LIST_HEAD(&event->active_list); 12221 init_event_group(event); 12222 INIT_LIST_HEAD(&event->rb_entry); 12223 INIT_LIST_HEAD(&event->active_entry); 12224 INIT_LIST_HEAD(&event->addr_filters.list); 12225 INIT_HLIST_NODE(&event->hlist_entry); 12226 12227 12228 init_waitqueue_head(&event->waitq); 12229 init_irq_work(&event->pending_irq, perf_pending_irq); 12230 event->pending_disable_irq = IRQ_WORK_INIT_HARD(perf_pending_disable); 12231 init_task_work(&event->pending_task, perf_pending_task); 12232 rcuwait_init(&event->pending_work_wait); 12233 12234 mutex_init(&event->mmap_mutex); 12235 raw_spin_lock_init(&event->addr_filters.lock); 12236 12237 atomic_long_set(&event->refcount, 1); 12238 event->cpu = cpu; 12239 event->attr = *attr; 12240 event->group_leader = group_leader; 12241 event->pmu = NULL; 12242 event->oncpu = -1; 12243 12244 event->parent = parent_event; 12245 12246 event->ns = get_pid_ns(task_active_pid_ns(current)); 12247 event->id = atomic64_inc_return(&perf_event_id); 12248 12249 event->state = PERF_EVENT_STATE_INACTIVE; 12250 12251 if (parent_event) 12252 event->event_caps = parent_event->event_caps; 12253 12254 if (task) { 12255 event->attach_state = PERF_ATTACH_TASK; 12256 /* 12257 * XXX pmu::event_init needs to know what task to account to 12258 * and we cannot use the ctx information because we need the 12259 * pmu before we get a ctx. 12260 */ 12261 event->hw.target = get_task_struct(task); 12262 } 12263 12264 event->clock = &local_clock; 12265 if (parent_event) 12266 event->clock = parent_event->clock; 12267 12268 if (!overflow_handler && parent_event) { 12269 overflow_handler = parent_event->overflow_handler; 12270 context = parent_event->overflow_handler_context; 12271 #if defined(CONFIG_BPF_SYSCALL) && defined(CONFIG_EVENT_TRACING) 12272 if (parent_event->prog) { 12273 struct bpf_prog *prog = parent_event->prog; 12274 12275 bpf_prog_inc(prog); 12276 event->prog = prog; 12277 } 12278 #endif 12279 } 12280 12281 if (overflow_handler) { 12282 event->overflow_handler = overflow_handler; 12283 event->overflow_handler_context = context; 12284 } else if (is_write_backward(event)){ 12285 event->overflow_handler = perf_event_output_backward; 12286 event->overflow_handler_context = NULL; 12287 } else { 12288 event->overflow_handler = perf_event_output_forward; 12289 event->overflow_handler_context = NULL; 12290 } 12291 12292 perf_event__state_init(event); 12293 12294 pmu = NULL; 12295 12296 hwc = &event->hw; 12297 hwc->sample_period = attr->sample_period; 12298 if (attr->freq && attr->sample_freq) 12299 hwc->sample_period = 1; 12300 hwc->last_period = hwc->sample_period; 12301 12302 local64_set(&hwc->period_left, hwc->sample_period); 12303 12304 /* 12305 * We do not support PERF_SAMPLE_READ on inherited events unless 12306 * PERF_SAMPLE_TID is also selected, which allows inherited events to 12307 * collect per-thread samples. 12308 * See perf_output_read(). 12309 */ 12310 if (has_inherit_and_sample_read(attr) && !(attr->sample_type & PERF_SAMPLE_TID)) 12311 goto err_ns; 12312 12313 if (!has_branch_stack(event)) 12314 event->attr.branch_sample_type = 0; 12315 12316 pmu = perf_init_event(event); 12317 if (IS_ERR(pmu)) { 12318 err = PTR_ERR(pmu); 12319 goto err_ns; 12320 } 12321 12322 /* 12323 * Disallow uncore-task events. Similarly, disallow uncore-cgroup 12324 * events (they don't make sense as the cgroup will be different 12325 * on other CPUs in the uncore mask). 12326 */ 12327 if (pmu->task_ctx_nr == perf_invalid_context && (task || cgroup_fd != -1)) { 12328 err = -EINVAL; 12329 goto err_pmu; 12330 } 12331 12332 if (event->attr.aux_output && 12333 (!(pmu->capabilities & PERF_PMU_CAP_AUX_OUTPUT) || 12334 event->attr.aux_pause || event->attr.aux_resume)) { 12335 err = -EOPNOTSUPP; 12336 goto err_pmu; 12337 } 12338 12339 if (event->attr.aux_pause && event->attr.aux_resume) { 12340 err = -EINVAL; 12341 goto err_pmu; 12342 } 12343 12344 if (event->attr.aux_start_paused) { 12345 if (!(pmu->capabilities & PERF_PMU_CAP_AUX_PAUSE)) { 12346 err = -EOPNOTSUPP; 12347 goto err_pmu; 12348 } 12349 event->hw.aux_paused = 1; 12350 } 12351 12352 if (cgroup_fd != -1) { 12353 err = perf_cgroup_connect(cgroup_fd, event, attr, group_leader); 12354 if (err) 12355 goto err_pmu; 12356 } 12357 12358 err = exclusive_event_init(event); 12359 if (err) 12360 goto err_pmu; 12361 12362 if (has_addr_filter(event)) { 12363 event->addr_filter_ranges = kcalloc(pmu->nr_addr_filters, 12364 sizeof(struct perf_addr_filter_range), 12365 GFP_KERNEL); 12366 if (!event->addr_filter_ranges) { 12367 err = -ENOMEM; 12368 goto err_per_task; 12369 } 12370 12371 /* 12372 * Clone the parent's vma offsets: they are valid until exec() 12373 * even if the mm is not shared with the parent. 12374 */ 12375 if (event->parent) { 12376 struct perf_addr_filters_head *ifh = perf_event_addr_filters(event); 12377 12378 raw_spin_lock_irq(&ifh->lock); 12379 memcpy(event->addr_filter_ranges, 12380 event->parent->addr_filter_ranges, 12381 pmu->nr_addr_filters * sizeof(struct perf_addr_filter_range)); 12382 raw_spin_unlock_irq(&ifh->lock); 12383 } 12384 12385 /* force hw sync on the address filters */ 12386 event->addr_filters_gen = 1; 12387 } 12388 12389 if (!event->parent) { 12390 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { 12391 err = get_callchain_buffers(attr->sample_max_stack); 12392 if (err) 12393 goto err_addr_filters; 12394 } 12395 } 12396 12397 err = security_perf_event_alloc(event); 12398 if (err) 12399 goto err_callchain_buffer; 12400 12401 /* symmetric to unaccount_event() in _free_event() */ 12402 account_event(event); 12403 12404 return event; 12405 12406 err_callchain_buffer: 12407 if (!event->parent) { 12408 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) 12409 put_callchain_buffers(); 12410 } 12411 err_addr_filters: 12412 kfree(event->addr_filter_ranges); 12413 12414 err_per_task: 12415 exclusive_event_destroy(event); 12416 12417 err_pmu: 12418 if (is_cgroup_event(event)) 12419 perf_detach_cgroup(event); 12420 if (event->destroy) 12421 event->destroy(event); 12422 module_put(pmu->module); 12423 err_ns: 12424 if (event->hw.target) 12425 put_task_struct(event->hw.target); 12426 call_rcu(&event->rcu_head, free_event_rcu); 12427 12428 return ERR_PTR(err); 12429 } 12430 12431 static int perf_copy_attr(struct perf_event_attr __user *uattr, 12432 struct perf_event_attr *attr) 12433 { 12434 u32 size; 12435 int ret; 12436 12437 /* Zero the full structure, so that a short copy will be nice. */ 12438 memset(attr, 0, sizeof(*attr)); 12439 12440 ret = get_user(size, &uattr->size); 12441 if (ret) 12442 return ret; 12443 12444 /* ABI compatibility quirk: */ 12445 if (!size) 12446 size = PERF_ATTR_SIZE_VER0; 12447 if (size < PERF_ATTR_SIZE_VER0 || size > PAGE_SIZE) 12448 goto err_size; 12449 12450 ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size); 12451 if (ret) { 12452 if (ret == -E2BIG) 12453 goto err_size; 12454 return ret; 12455 } 12456 12457 attr->size = size; 12458 12459 if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3) 12460 return -EINVAL; 12461 12462 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) 12463 return -EINVAL; 12464 12465 if (attr->read_format & ~(PERF_FORMAT_MAX-1)) 12466 return -EINVAL; 12467 12468 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { 12469 u64 mask = attr->branch_sample_type; 12470 12471 /* only using defined bits */ 12472 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) 12473 return -EINVAL; 12474 12475 /* at least one branch bit must be set */ 12476 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) 12477 return -EINVAL; 12478 12479 /* propagate priv level, when not set for branch */ 12480 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { 12481 12482 /* exclude_kernel checked on syscall entry */ 12483 if (!attr->exclude_kernel) 12484 mask |= PERF_SAMPLE_BRANCH_KERNEL; 12485 12486 if (!attr->exclude_user) 12487 mask |= PERF_SAMPLE_BRANCH_USER; 12488 12489 if (!attr->exclude_hv) 12490 mask |= PERF_SAMPLE_BRANCH_HV; 12491 /* 12492 * adjust user setting (for HW filter setup) 12493 */ 12494 attr->branch_sample_type = mask; 12495 } 12496 /* privileged levels capture (kernel, hv): check permissions */ 12497 if (mask & PERF_SAMPLE_BRANCH_PERM_PLM) { 12498 ret = perf_allow_kernel(attr); 12499 if (ret) 12500 return ret; 12501 } 12502 } 12503 12504 if (attr->sample_type & PERF_SAMPLE_REGS_USER) { 12505 ret = perf_reg_validate(attr->sample_regs_user); 12506 if (ret) 12507 return ret; 12508 } 12509 12510 if (attr->sample_type & PERF_SAMPLE_STACK_USER) { 12511 if (!arch_perf_have_user_stack_dump()) 12512 return -ENOSYS; 12513 12514 /* 12515 * We have __u32 type for the size, but so far 12516 * we can only use __u16 as maximum due to the 12517 * __u16 sample size limit. 12518 */ 12519 if (attr->sample_stack_user >= USHRT_MAX) 12520 return -EINVAL; 12521 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) 12522 return -EINVAL; 12523 } 12524 12525 if (!attr->sample_max_stack) 12526 attr->sample_max_stack = sysctl_perf_event_max_stack; 12527 12528 if (attr->sample_type & PERF_SAMPLE_REGS_INTR) 12529 ret = perf_reg_validate(attr->sample_regs_intr); 12530 12531 #ifndef CONFIG_CGROUP_PERF 12532 if (attr->sample_type & PERF_SAMPLE_CGROUP) 12533 return -EINVAL; 12534 #endif 12535 if ((attr->sample_type & PERF_SAMPLE_WEIGHT) && 12536 (attr->sample_type & PERF_SAMPLE_WEIGHT_STRUCT)) 12537 return -EINVAL; 12538 12539 if (!attr->inherit && attr->inherit_thread) 12540 return -EINVAL; 12541 12542 if (attr->remove_on_exec && attr->enable_on_exec) 12543 return -EINVAL; 12544 12545 if (attr->sigtrap && !attr->remove_on_exec) 12546 return -EINVAL; 12547 12548 out: 12549 return ret; 12550 12551 err_size: 12552 put_user(sizeof(*attr), &uattr->size); 12553 ret = -E2BIG; 12554 goto out; 12555 } 12556 12557 static void mutex_lock_double(struct mutex *a, struct mutex *b) 12558 { 12559 if (b < a) 12560 swap(a, b); 12561 12562 mutex_lock(a); 12563 mutex_lock_nested(b, SINGLE_DEPTH_NESTING); 12564 } 12565 12566 static int 12567 perf_event_set_output(struct perf_event *event, struct perf_event *output_event) 12568 { 12569 struct perf_buffer *rb = NULL; 12570 int ret = -EINVAL; 12571 12572 if (!output_event) { 12573 mutex_lock(&event->mmap_mutex); 12574 goto set; 12575 } 12576 12577 /* don't allow circular references */ 12578 if (event == output_event) 12579 goto out; 12580 12581 /* 12582 * Don't allow cross-cpu buffers 12583 */ 12584 if (output_event->cpu != event->cpu) 12585 goto out; 12586 12587 /* 12588 * If its not a per-cpu rb, it must be the same task. 12589 */ 12590 if (output_event->cpu == -1 && output_event->hw.target != event->hw.target) 12591 goto out; 12592 12593 /* 12594 * Mixing clocks in the same buffer is trouble you don't need. 12595 */ 12596 if (output_event->clock != event->clock) 12597 goto out; 12598 12599 /* 12600 * Either writing ring buffer from beginning or from end. 12601 * Mixing is not allowed. 12602 */ 12603 if (is_write_backward(output_event) != is_write_backward(event)) 12604 goto out; 12605 12606 /* 12607 * If both events generate aux data, they must be on the same PMU 12608 */ 12609 if (has_aux(event) && has_aux(output_event) && 12610 event->pmu != output_event->pmu) 12611 goto out; 12612 12613 /* 12614 * Hold both mmap_mutex to serialize against perf_mmap_close(). Since 12615 * output_event is already on rb->event_list, and the list iteration 12616 * restarts after every removal, it is guaranteed this new event is 12617 * observed *OR* if output_event is already removed, it's guaranteed we 12618 * observe !rb->mmap_count. 12619 */ 12620 mutex_lock_double(&event->mmap_mutex, &output_event->mmap_mutex); 12621 set: 12622 /* Can't redirect output if we've got an active mmap() */ 12623 if (atomic_read(&event->mmap_count)) 12624 goto unlock; 12625 12626 if (output_event) { 12627 /* get the rb we want to redirect to */ 12628 rb = ring_buffer_get(output_event); 12629 if (!rb) 12630 goto unlock; 12631 12632 /* did we race against perf_mmap_close() */ 12633 if (!atomic_read(&rb->mmap_count)) { 12634 ring_buffer_put(rb); 12635 goto unlock; 12636 } 12637 } 12638 12639 ring_buffer_attach(event, rb); 12640 12641 ret = 0; 12642 unlock: 12643 mutex_unlock(&event->mmap_mutex); 12644 if (output_event) 12645 mutex_unlock(&output_event->mmap_mutex); 12646 12647 out: 12648 return ret; 12649 } 12650 12651 static int perf_event_set_clock(struct perf_event *event, clockid_t clk_id) 12652 { 12653 bool nmi_safe = false; 12654 12655 switch (clk_id) { 12656 case CLOCK_MONOTONIC: 12657 event->clock = &ktime_get_mono_fast_ns; 12658 nmi_safe = true; 12659 break; 12660 12661 case CLOCK_MONOTONIC_RAW: 12662 event->clock = &ktime_get_raw_fast_ns; 12663 nmi_safe = true; 12664 break; 12665 12666 case CLOCK_REALTIME: 12667 event->clock = &ktime_get_real_ns; 12668 break; 12669 12670 case CLOCK_BOOTTIME: 12671 event->clock = &ktime_get_boottime_ns; 12672 break; 12673 12674 case CLOCK_TAI: 12675 event->clock = &ktime_get_clocktai_ns; 12676 break; 12677 12678 default: 12679 return -EINVAL; 12680 } 12681 12682 if (!nmi_safe && !(event->pmu->capabilities & PERF_PMU_CAP_NO_NMI)) 12683 return -EINVAL; 12684 12685 return 0; 12686 } 12687 12688 static bool 12689 perf_check_permission(struct perf_event_attr *attr, struct task_struct *task) 12690 { 12691 unsigned int ptrace_mode = PTRACE_MODE_READ_REALCREDS; 12692 bool is_capable = perfmon_capable(); 12693 12694 if (attr->sigtrap) { 12695 /* 12696 * perf_event_attr::sigtrap sends signals to the other task. 12697 * Require the current task to also have CAP_KILL. 12698 */ 12699 rcu_read_lock(); 12700 is_capable &= ns_capable(__task_cred(task)->user_ns, CAP_KILL); 12701 rcu_read_unlock(); 12702 12703 /* 12704 * If the required capabilities aren't available, checks for 12705 * ptrace permissions: upgrade to ATTACH, since sending signals 12706 * can effectively change the target task. 12707 */ 12708 ptrace_mode = PTRACE_MODE_ATTACH_REALCREDS; 12709 } 12710 12711 /* 12712 * Preserve ptrace permission check for backwards compatibility. The 12713 * ptrace check also includes checks that the current task and other 12714 * task have matching uids, and is therefore not done here explicitly. 12715 */ 12716 return is_capable || ptrace_may_access(task, ptrace_mode); 12717 } 12718 12719 /** 12720 * sys_perf_event_open - open a performance event, associate it to a task/cpu 12721 * 12722 * @attr_uptr: event_id type attributes for monitoring/sampling 12723 * @pid: target pid 12724 * @cpu: target cpu 12725 * @group_fd: group leader event fd 12726 * @flags: perf event open flags 12727 */ 12728 SYSCALL_DEFINE5(perf_event_open, 12729 struct perf_event_attr __user *, attr_uptr, 12730 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) 12731 { 12732 struct perf_event *group_leader = NULL, *output_event = NULL; 12733 struct perf_event_pmu_context *pmu_ctx; 12734 struct perf_event *event, *sibling; 12735 struct perf_event_attr attr; 12736 struct perf_event_context *ctx; 12737 struct file *event_file = NULL; 12738 struct task_struct *task = NULL; 12739 struct pmu *pmu; 12740 int event_fd; 12741 int move_group = 0; 12742 int err; 12743 int f_flags = O_RDWR; 12744 int cgroup_fd = -1; 12745 12746 /* for future expandability... */ 12747 if (flags & ~PERF_FLAG_ALL) 12748 return -EINVAL; 12749 12750 err = perf_copy_attr(attr_uptr, &attr); 12751 if (err) 12752 return err; 12753 12754 /* Do we allow access to perf_event_open(2) ? */ 12755 err = security_perf_event_open(&attr, PERF_SECURITY_OPEN); 12756 if (err) 12757 return err; 12758 12759 if (!attr.exclude_kernel) { 12760 err = perf_allow_kernel(&attr); 12761 if (err) 12762 return err; 12763 } 12764 12765 if (attr.namespaces) { 12766 if (!perfmon_capable()) 12767 return -EACCES; 12768 } 12769 12770 if (attr.freq) { 12771 if (attr.sample_freq > sysctl_perf_event_sample_rate) 12772 return -EINVAL; 12773 } else { 12774 if (attr.sample_period & (1ULL << 63)) 12775 return -EINVAL; 12776 } 12777 12778 /* Only privileged users can get physical addresses */ 12779 if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR)) { 12780 err = perf_allow_kernel(&attr); 12781 if (err) 12782 return err; 12783 } 12784 12785 /* REGS_INTR can leak data, lockdown must prevent this */ 12786 if (attr.sample_type & PERF_SAMPLE_REGS_INTR) { 12787 err = security_locked_down(LOCKDOWN_PERF); 12788 if (err) 12789 return err; 12790 } 12791 12792 /* 12793 * In cgroup mode, the pid argument is used to pass the fd 12794 * opened to the cgroup directory in cgroupfs. The cpu argument 12795 * designates the cpu on which to monitor threads from that 12796 * cgroup. 12797 */ 12798 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) 12799 return -EINVAL; 12800 12801 if (flags & PERF_FLAG_FD_CLOEXEC) 12802 f_flags |= O_CLOEXEC; 12803 12804 event_fd = get_unused_fd_flags(f_flags); 12805 if (event_fd < 0) 12806 return event_fd; 12807 12808 CLASS(fd, group)(group_fd); // group_fd == -1 => empty 12809 if (group_fd != -1) { 12810 if (!is_perf_file(group)) { 12811 err = -EBADF; 12812 goto err_fd; 12813 } 12814 group_leader = fd_file(group)->private_data; 12815 if (flags & PERF_FLAG_FD_OUTPUT) 12816 output_event = group_leader; 12817 if (flags & PERF_FLAG_FD_NO_GROUP) 12818 group_leader = NULL; 12819 } 12820 12821 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { 12822 task = find_lively_task_by_vpid(pid); 12823 if (IS_ERR(task)) { 12824 err = PTR_ERR(task); 12825 goto err_fd; 12826 } 12827 } 12828 12829 if (task && group_leader && 12830 group_leader->attr.inherit != attr.inherit) { 12831 err = -EINVAL; 12832 goto err_task; 12833 } 12834 12835 if (flags & PERF_FLAG_PID_CGROUP) 12836 cgroup_fd = pid; 12837 12838 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, 12839 NULL, NULL, cgroup_fd); 12840 if (IS_ERR(event)) { 12841 err = PTR_ERR(event); 12842 goto err_task; 12843 } 12844 12845 if (is_sampling_event(event)) { 12846 if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) { 12847 err = -EOPNOTSUPP; 12848 goto err_alloc; 12849 } 12850 } 12851 12852 /* 12853 * Special case software events and allow them to be part of 12854 * any hardware group. 12855 */ 12856 pmu = event->pmu; 12857 12858 if (attr.use_clockid) { 12859 err = perf_event_set_clock(event, attr.clockid); 12860 if (err) 12861 goto err_alloc; 12862 } 12863 12864 if (pmu->task_ctx_nr == perf_sw_context) 12865 event->event_caps |= PERF_EV_CAP_SOFTWARE; 12866 12867 if (task) { 12868 err = down_read_interruptible(&task->signal->exec_update_lock); 12869 if (err) 12870 goto err_alloc; 12871 12872 /* 12873 * We must hold exec_update_lock across this and any potential 12874 * perf_install_in_context() call for this new event to 12875 * serialize against exec() altering our credentials (and the 12876 * perf_event_exit_task() that could imply). 12877 */ 12878 err = -EACCES; 12879 if (!perf_check_permission(&attr, task)) 12880 goto err_cred; 12881 } 12882 12883 /* 12884 * Get the target context (task or percpu): 12885 */ 12886 ctx = find_get_context(task, event); 12887 if (IS_ERR(ctx)) { 12888 err = PTR_ERR(ctx); 12889 goto err_cred; 12890 } 12891 12892 mutex_lock(&ctx->mutex); 12893 12894 if (ctx->task == TASK_TOMBSTONE) { 12895 err = -ESRCH; 12896 goto err_locked; 12897 } 12898 12899 if (!task) { 12900 /* 12901 * Check if the @cpu we're creating an event for is online. 12902 * 12903 * We use the perf_cpu_context::ctx::mutex to serialize against 12904 * the hotplug notifiers. See perf_event_{init,exit}_cpu(). 12905 */ 12906 struct perf_cpu_context *cpuctx = per_cpu_ptr(&perf_cpu_context, event->cpu); 12907 12908 if (!cpuctx->online) { 12909 err = -ENODEV; 12910 goto err_locked; 12911 } 12912 } 12913 12914 if (group_leader) { 12915 err = -EINVAL; 12916 12917 /* 12918 * Do not allow a recursive hierarchy (this new sibling 12919 * becoming part of another group-sibling): 12920 */ 12921 if (group_leader->group_leader != group_leader) 12922 goto err_locked; 12923 12924 /* All events in a group should have the same clock */ 12925 if (group_leader->clock != event->clock) 12926 goto err_locked; 12927 12928 /* 12929 * Make sure we're both events for the same CPU; 12930 * grouping events for different CPUs is broken; since 12931 * you can never concurrently schedule them anyhow. 12932 */ 12933 if (group_leader->cpu != event->cpu) 12934 goto err_locked; 12935 12936 /* 12937 * Make sure we're both on the same context; either task or cpu. 12938 */ 12939 if (group_leader->ctx != ctx) 12940 goto err_locked; 12941 12942 /* 12943 * Only a group leader can be exclusive or pinned 12944 */ 12945 if (attr.exclusive || attr.pinned) 12946 goto err_locked; 12947 12948 if (is_software_event(event) && 12949 !in_software_context(group_leader)) { 12950 /* 12951 * If the event is a sw event, but the group_leader 12952 * is on hw context. 12953 * 12954 * Allow the addition of software events to hw 12955 * groups, this is safe because software events 12956 * never fail to schedule. 12957 * 12958 * Note the comment that goes with struct 12959 * perf_event_pmu_context. 12960 */ 12961 pmu = group_leader->pmu_ctx->pmu; 12962 } else if (!is_software_event(event)) { 12963 if (is_software_event(group_leader) && 12964 (group_leader->group_caps & PERF_EV_CAP_SOFTWARE)) { 12965 /* 12966 * In case the group is a pure software group, and we 12967 * try to add a hardware event, move the whole group to 12968 * the hardware context. 12969 */ 12970 move_group = 1; 12971 } 12972 12973 /* Don't allow group of multiple hw events from different pmus */ 12974 if (!in_software_context(group_leader) && 12975 group_leader->pmu_ctx->pmu != pmu) 12976 goto err_locked; 12977 } 12978 } 12979 12980 /* 12981 * Now that we're certain of the pmu; find the pmu_ctx. 12982 */ 12983 pmu_ctx = find_get_pmu_context(pmu, ctx, event); 12984 if (IS_ERR(pmu_ctx)) { 12985 err = PTR_ERR(pmu_ctx); 12986 goto err_locked; 12987 } 12988 event->pmu_ctx = pmu_ctx; 12989 12990 if (output_event) { 12991 err = perf_event_set_output(event, output_event); 12992 if (err) 12993 goto err_context; 12994 } 12995 12996 if (!perf_event_validate_size(event)) { 12997 err = -E2BIG; 12998 goto err_context; 12999 } 13000 13001 if (perf_need_aux_event(event) && !perf_get_aux_event(event, group_leader)) { 13002 err = -EINVAL; 13003 goto err_context; 13004 } 13005 13006 /* 13007 * Must be under the same ctx::mutex as perf_install_in_context(), 13008 * because we need to serialize with concurrent event creation. 13009 */ 13010 if (!exclusive_event_installable(event, ctx)) { 13011 err = -EBUSY; 13012 goto err_context; 13013 } 13014 13015 WARN_ON_ONCE(ctx->parent_ctx); 13016 13017 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, f_flags); 13018 if (IS_ERR(event_file)) { 13019 err = PTR_ERR(event_file); 13020 event_file = NULL; 13021 goto err_context; 13022 } 13023 13024 /* 13025 * This is the point on no return; we cannot fail hereafter. This is 13026 * where we start modifying current state. 13027 */ 13028 13029 if (move_group) { 13030 perf_remove_from_context(group_leader, 0); 13031 put_pmu_ctx(group_leader->pmu_ctx); 13032 13033 for_each_sibling_event(sibling, group_leader) { 13034 perf_remove_from_context(sibling, 0); 13035 put_pmu_ctx(sibling->pmu_ctx); 13036 } 13037 13038 /* 13039 * Install the group siblings before the group leader. 13040 * 13041 * Because a group leader will try and install the entire group 13042 * (through the sibling list, which is still in-tact), we can 13043 * end up with siblings installed in the wrong context. 13044 * 13045 * By installing siblings first we NO-OP because they're not 13046 * reachable through the group lists. 13047 */ 13048 for_each_sibling_event(sibling, group_leader) { 13049 sibling->pmu_ctx = pmu_ctx; 13050 get_pmu_ctx(pmu_ctx); 13051 perf_event__state_init(sibling); 13052 perf_install_in_context(ctx, sibling, sibling->cpu); 13053 } 13054 13055 /* 13056 * Removing from the context ends up with disabled 13057 * event. What we want here is event in the initial 13058 * startup state, ready to be add into new context. 13059 */ 13060 group_leader->pmu_ctx = pmu_ctx; 13061 get_pmu_ctx(pmu_ctx); 13062 perf_event__state_init(group_leader); 13063 perf_install_in_context(ctx, group_leader, group_leader->cpu); 13064 } 13065 13066 /* 13067 * Precalculate sample_data sizes; do while holding ctx::mutex such 13068 * that we're serialized against further additions and before 13069 * perf_install_in_context() which is the point the event is active and 13070 * can use these values. 13071 */ 13072 perf_event__header_size(event); 13073 perf_event__id_header_size(event); 13074 13075 event->owner = current; 13076 13077 perf_install_in_context(ctx, event, event->cpu); 13078 perf_unpin_context(ctx); 13079 13080 mutex_unlock(&ctx->mutex); 13081 13082 if (task) { 13083 up_read(&task->signal->exec_update_lock); 13084 put_task_struct(task); 13085 } 13086 13087 mutex_lock(¤t->perf_event_mutex); 13088 list_add_tail(&event->owner_entry, ¤t->perf_event_list); 13089 mutex_unlock(¤t->perf_event_mutex); 13090 13091 /* 13092 * File reference in group guarantees that group_leader has been 13093 * kept alive until we place the new event on the sibling_list. 13094 * This ensures destruction of the group leader will find 13095 * the pointer to itself in perf_group_detach(). 13096 */ 13097 fd_install(event_fd, event_file); 13098 return event_fd; 13099 13100 err_context: 13101 put_pmu_ctx(event->pmu_ctx); 13102 event->pmu_ctx = NULL; /* _free_event() */ 13103 err_locked: 13104 mutex_unlock(&ctx->mutex); 13105 perf_unpin_context(ctx); 13106 put_ctx(ctx); 13107 err_cred: 13108 if (task) 13109 up_read(&task->signal->exec_update_lock); 13110 err_alloc: 13111 free_event(event); 13112 err_task: 13113 if (task) 13114 put_task_struct(task); 13115 err_fd: 13116 put_unused_fd(event_fd); 13117 return err; 13118 } 13119 13120 /** 13121 * perf_event_create_kernel_counter 13122 * 13123 * @attr: attributes of the counter to create 13124 * @cpu: cpu in which the counter is bound 13125 * @task: task to profile (NULL for percpu) 13126 * @overflow_handler: callback to trigger when we hit the event 13127 * @context: context data could be used in overflow_handler callback 13128 */ 13129 struct perf_event * 13130 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, 13131 struct task_struct *task, 13132 perf_overflow_handler_t overflow_handler, 13133 void *context) 13134 { 13135 struct perf_event_pmu_context *pmu_ctx; 13136 struct perf_event_context *ctx; 13137 struct perf_event *event; 13138 struct pmu *pmu; 13139 int err; 13140 13141 /* 13142 * Grouping is not supported for kernel events, neither is 'AUX', 13143 * make sure the caller's intentions are adjusted. 13144 */ 13145 if (attr->aux_output || attr->aux_action) 13146 return ERR_PTR(-EINVAL); 13147 13148 event = perf_event_alloc(attr, cpu, task, NULL, NULL, 13149 overflow_handler, context, -1); 13150 if (IS_ERR(event)) { 13151 err = PTR_ERR(event); 13152 goto err; 13153 } 13154 13155 /* Mark owner so we could distinguish it from user events. */ 13156 event->owner = TASK_TOMBSTONE; 13157 pmu = event->pmu; 13158 13159 if (pmu->task_ctx_nr == perf_sw_context) 13160 event->event_caps |= PERF_EV_CAP_SOFTWARE; 13161 13162 /* 13163 * Get the target context (task or percpu): 13164 */ 13165 ctx = find_get_context(task, event); 13166 if (IS_ERR(ctx)) { 13167 err = PTR_ERR(ctx); 13168 goto err_alloc; 13169 } 13170 13171 WARN_ON_ONCE(ctx->parent_ctx); 13172 mutex_lock(&ctx->mutex); 13173 if (ctx->task == TASK_TOMBSTONE) { 13174 err = -ESRCH; 13175 goto err_unlock; 13176 } 13177 13178 pmu_ctx = find_get_pmu_context(pmu, ctx, event); 13179 if (IS_ERR(pmu_ctx)) { 13180 err = PTR_ERR(pmu_ctx); 13181 goto err_unlock; 13182 } 13183 event->pmu_ctx = pmu_ctx; 13184 13185 if (!task) { 13186 /* 13187 * Check if the @cpu we're creating an event for is online. 13188 * 13189 * We use the perf_cpu_context::ctx::mutex to serialize against 13190 * the hotplug notifiers. See perf_event_{init,exit}_cpu(). 13191 */ 13192 struct perf_cpu_context *cpuctx = 13193 container_of(ctx, struct perf_cpu_context, ctx); 13194 if (!cpuctx->online) { 13195 err = -ENODEV; 13196 goto err_pmu_ctx; 13197 } 13198 } 13199 13200 if (!exclusive_event_installable(event, ctx)) { 13201 err = -EBUSY; 13202 goto err_pmu_ctx; 13203 } 13204 13205 perf_install_in_context(ctx, event, event->cpu); 13206 perf_unpin_context(ctx); 13207 mutex_unlock(&ctx->mutex); 13208 13209 return event; 13210 13211 err_pmu_ctx: 13212 put_pmu_ctx(pmu_ctx); 13213 event->pmu_ctx = NULL; /* _free_event() */ 13214 err_unlock: 13215 mutex_unlock(&ctx->mutex); 13216 perf_unpin_context(ctx); 13217 put_ctx(ctx); 13218 err_alloc: 13219 free_event(event); 13220 err: 13221 return ERR_PTR(err); 13222 } 13223 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); 13224 13225 static void __perf_pmu_remove(struct perf_event_context *ctx, 13226 int cpu, struct pmu *pmu, 13227 struct perf_event_groups *groups, 13228 struct list_head *events) 13229 { 13230 struct perf_event *event, *sibling; 13231 13232 perf_event_groups_for_cpu_pmu(event, groups, cpu, pmu) { 13233 perf_remove_from_context(event, 0); 13234 put_pmu_ctx(event->pmu_ctx); 13235 list_add(&event->migrate_entry, events); 13236 13237 for_each_sibling_event(sibling, event) { 13238 perf_remove_from_context(sibling, 0); 13239 put_pmu_ctx(sibling->pmu_ctx); 13240 list_add(&sibling->migrate_entry, events); 13241 } 13242 } 13243 } 13244 13245 static void __perf_pmu_install_event(struct pmu *pmu, 13246 struct perf_event_context *ctx, 13247 int cpu, struct perf_event *event) 13248 { 13249 struct perf_event_pmu_context *epc; 13250 struct perf_event_context *old_ctx = event->ctx; 13251 13252 get_ctx(ctx); /* normally find_get_context() */ 13253 13254 event->cpu = cpu; 13255 epc = find_get_pmu_context(pmu, ctx, event); 13256 event->pmu_ctx = epc; 13257 13258 if (event->state >= PERF_EVENT_STATE_OFF) 13259 event->state = PERF_EVENT_STATE_INACTIVE; 13260 perf_install_in_context(ctx, event, cpu); 13261 13262 /* 13263 * Now that event->ctx is updated and visible, put the old ctx. 13264 */ 13265 put_ctx(old_ctx); 13266 } 13267 13268 static void __perf_pmu_install(struct perf_event_context *ctx, 13269 int cpu, struct pmu *pmu, struct list_head *events) 13270 { 13271 struct perf_event *event, *tmp; 13272 13273 /* 13274 * Re-instate events in 2 passes. 13275 * 13276 * Skip over group leaders and only install siblings on this first 13277 * pass, siblings will not get enabled without a leader, however a 13278 * leader will enable its siblings, even if those are still on the old 13279 * context. 13280 */ 13281 list_for_each_entry_safe(event, tmp, events, migrate_entry) { 13282 if (event->group_leader == event) 13283 continue; 13284 13285 list_del(&event->migrate_entry); 13286 __perf_pmu_install_event(pmu, ctx, cpu, event); 13287 } 13288 13289 /* 13290 * Once all the siblings are setup properly, install the group leaders 13291 * to make it go. 13292 */ 13293 list_for_each_entry_safe(event, tmp, events, migrate_entry) { 13294 list_del(&event->migrate_entry); 13295 __perf_pmu_install_event(pmu, ctx, cpu, event); 13296 } 13297 } 13298 13299 void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) 13300 { 13301 struct perf_event_context *src_ctx, *dst_ctx; 13302 LIST_HEAD(events); 13303 13304 /* 13305 * Since per-cpu context is persistent, no need to grab an extra 13306 * reference. 13307 */ 13308 src_ctx = &per_cpu_ptr(&perf_cpu_context, src_cpu)->ctx; 13309 dst_ctx = &per_cpu_ptr(&perf_cpu_context, dst_cpu)->ctx; 13310 13311 /* 13312 * See perf_event_ctx_lock() for comments on the details 13313 * of swizzling perf_event::ctx. 13314 */ 13315 mutex_lock_double(&src_ctx->mutex, &dst_ctx->mutex); 13316 13317 __perf_pmu_remove(src_ctx, src_cpu, pmu, &src_ctx->pinned_groups, &events); 13318 __perf_pmu_remove(src_ctx, src_cpu, pmu, &src_ctx->flexible_groups, &events); 13319 13320 if (!list_empty(&events)) { 13321 /* 13322 * Wait for the events to quiesce before re-instating them. 13323 */ 13324 synchronize_rcu(); 13325 13326 __perf_pmu_install(dst_ctx, dst_cpu, pmu, &events); 13327 } 13328 13329 mutex_unlock(&dst_ctx->mutex); 13330 mutex_unlock(&src_ctx->mutex); 13331 } 13332 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); 13333 13334 static void sync_child_event(struct perf_event *child_event) 13335 { 13336 struct perf_event *parent_event = child_event->parent; 13337 u64 child_val; 13338 13339 if (child_event->attr.inherit_stat) { 13340 struct task_struct *task = child_event->ctx->task; 13341 13342 if (task && task != TASK_TOMBSTONE) 13343 perf_event_read_event(child_event, task); 13344 } 13345 13346 child_val = perf_event_count(child_event, false); 13347 13348 /* 13349 * Add back the child's count to the parent's count: 13350 */ 13351 atomic64_add(child_val, &parent_event->child_count); 13352 atomic64_add(child_event->total_time_enabled, 13353 &parent_event->child_total_time_enabled); 13354 atomic64_add(child_event->total_time_running, 13355 &parent_event->child_total_time_running); 13356 } 13357 13358 static void 13359 perf_event_exit_event(struct perf_event *event, struct perf_event_context *ctx) 13360 { 13361 struct perf_event *parent_event = event->parent; 13362 unsigned long detach_flags = 0; 13363 13364 if (parent_event) { 13365 /* 13366 * Do not destroy the 'original' grouping; because of the 13367 * context switch optimization the original events could've 13368 * ended up in a random child task. 13369 * 13370 * If we were to destroy the original group, all group related 13371 * operations would cease to function properly after this 13372 * random child dies. 13373 * 13374 * Do destroy all inherited groups, we don't care about those 13375 * and being thorough is better. 13376 */ 13377 detach_flags = DETACH_GROUP | DETACH_CHILD; 13378 mutex_lock(&parent_event->child_mutex); 13379 } 13380 13381 perf_remove_from_context(event, detach_flags); 13382 13383 raw_spin_lock_irq(&ctx->lock); 13384 if (event->state > PERF_EVENT_STATE_EXIT) 13385 perf_event_set_state(event, PERF_EVENT_STATE_EXIT); 13386 raw_spin_unlock_irq(&ctx->lock); 13387 13388 /* 13389 * Child events can be freed. 13390 */ 13391 if (parent_event) { 13392 mutex_unlock(&parent_event->child_mutex); 13393 /* 13394 * Kick perf_poll() for is_event_hup(); 13395 */ 13396 perf_event_wakeup(parent_event); 13397 free_event(event); 13398 put_event(parent_event); 13399 return; 13400 } 13401 13402 /* 13403 * Parent events are governed by their filedesc, retain them. 13404 */ 13405 perf_event_wakeup(event); 13406 } 13407 13408 static void perf_event_exit_task_context(struct task_struct *child) 13409 { 13410 struct perf_event_context *child_ctx, *clone_ctx = NULL; 13411 struct perf_event *child_event, *next; 13412 13413 WARN_ON_ONCE(child != current); 13414 13415 child_ctx = perf_pin_task_context(child); 13416 if (!child_ctx) 13417 return; 13418 13419 /* 13420 * In order to reduce the amount of tricky in ctx tear-down, we hold 13421 * ctx::mutex over the entire thing. This serializes against almost 13422 * everything that wants to access the ctx. 13423 * 13424 * The exception is sys_perf_event_open() / 13425 * perf_event_create_kernel_count() which does find_get_context() 13426 * without ctx::mutex (it cannot because of the move_group double mutex 13427 * lock thing). See the comments in perf_install_in_context(). 13428 */ 13429 mutex_lock(&child_ctx->mutex); 13430 13431 /* 13432 * In a single ctx::lock section, de-schedule the events and detach the 13433 * context from the task such that we cannot ever get it scheduled back 13434 * in. 13435 */ 13436 raw_spin_lock_irq(&child_ctx->lock); 13437 task_ctx_sched_out(child_ctx, NULL, EVENT_ALL); 13438 13439 /* 13440 * Now that the context is inactive, destroy the task <-> ctx relation 13441 * and mark the context dead. 13442 */ 13443 RCU_INIT_POINTER(child->perf_event_ctxp, NULL); 13444 put_ctx(child_ctx); /* cannot be last */ 13445 WRITE_ONCE(child_ctx->task, TASK_TOMBSTONE); 13446 put_task_struct(current); /* cannot be last */ 13447 13448 clone_ctx = unclone_ctx(child_ctx); 13449 raw_spin_unlock_irq(&child_ctx->lock); 13450 13451 if (clone_ctx) 13452 put_ctx(clone_ctx); 13453 13454 /* 13455 * Report the task dead after unscheduling the events so that we 13456 * won't get any samples after PERF_RECORD_EXIT. We can however still 13457 * get a few PERF_RECORD_READ events. 13458 */ 13459 perf_event_task(child, child_ctx, 0); 13460 13461 list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry) 13462 perf_event_exit_event(child_event, child_ctx); 13463 13464 mutex_unlock(&child_ctx->mutex); 13465 13466 put_ctx(child_ctx); 13467 } 13468 13469 /* 13470 * When a child task exits, feed back event values to parent events. 13471 * 13472 * Can be called with exec_update_lock held when called from 13473 * setup_new_exec(). 13474 */ 13475 void perf_event_exit_task(struct task_struct *child) 13476 { 13477 struct perf_event *event, *tmp; 13478 13479 mutex_lock(&child->perf_event_mutex); 13480 list_for_each_entry_safe(event, tmp, &child->perf_event_list, 13481 owner_entry) { 13482 list_del_init(&event->owner_entry); 13483 13484 /* 13485 * Ensure the list deletion is visible before we clear 13486 * the owner, closes a race against perf_release() where 13487 * we need to serialize on the owner->perf_event_mutex. 13488 */ 13489 smp_store_release(&event->owner, NULL); 13490 } 13491 mutex_unlock(&child->perf_event_mutex); 13492 13493 perf_event_exit_task_context(child); 13494 13495 /* 13496 * The perf_event_exit_task_context calls perf_event_task 13497 * with child's task_ctx, which generates EXIT events for 13498 * child contexts and sets child->perf_event_ctxp[] to NULL. 13499 * At this point we need to send EXIT events to cpu contexts. 13500 */ 13501 perf_event_task(child, NULL, 0); 13502 } 13503 13504 static void perf_free_event(struct perf_event *event, 13505 struct perf_event_context *ctx) 13506 { 13507 struct perf_event *parent = event->parent; 13508 13509 if (WARN_ON_ONCE(!parent)) 13510 return; 13511 13512 mutex_lock(&parent->child_mutex); 13513 list_del_init(&event->child_list); 13514 mutex_unlock(&parent->child_mutex); 13515 13516 put_event(parent); 13517 13518 raw_spin_lock_irq(&ctx->lock); 13519 perf_group_detach(event); 13520 list_del_event(event, ctx); 13521 raw_spin_unlock_irq(&ctx->lock); 13522 free_event(event); 13523 } 13524 13525 /* 13526 * Free a context as created by inheritance by perf_event_init_task() below, 13527 * used by fork() in case of fail. 13528 * 13529 * Even though the task has never lived, the context and events have been 13530 * exposed through the child_list, so we must take care tearing it all down. 13531 */ 13532 void perf_event_free_task(struct task_struct *task) 13533 { 13534 struct perf_event_context *ctx; 13535 struct perf_event *event, *tmp; 13536 13537 ctx = rcu_access_pointer(task->perf_event_ctxp); 13538 if (!ctx) 13539 return; 13540 13541 mutex_lock(&ctx->mutex); 13542 raw_spin_lock_irq(&ctx->lock); 13543 /* 13544 * Destroy the task <-> ctx relation and mark the context dead. 13545 * 13546 * This is important because even though the task hasn't been 13547 * exposed yet the context has been (through child_list). 13548 */ 13549 RCU_INIT_POINTER(task->perf_event_ctxp, NULL); 13550 WRITE_ONCE(ctx->task, TASK_TOMBSTONE); 13551 put_task_struct(task); /* cannot be last */ 13552 raw_spin_unlock_irq(&ctx->lock); 13553 13554 13555 list_for_each_entry_safe(event, tmp, &ctx->event_list, event_entry) 13556 perf_free_event(event, ctx); 13557 13558 mutex_unlock(&ctx->mutex); 13559 13560 /* 13561 * perf_event_release_kernel() could've stolen some of our 13562 * child events and still have them on its free_list. In that 13563 * case we must wait for these events to have been freed (in 13564 * particular all their references to this task must've been 13565 * dropped). 13566 * 13567 * Without this copy_process() will unconditionally free this 13568 * task (irrespective of its reference count) and 13569 * _free_event()'s put_task_struct(event->hw.target) will be a 13570 * use-after-free. 13571 * 13572 * Wait for all events to drop their context reference. 13573 */ 13574 wait_var_event(&ctx->refcount, refcount_read(&ctx->refcount) == 1); 13575 put_ctx(ctx); /* must be last */ 13576 } 13577 13578 void perf_event_delayed_put(struct task_struct *task) 13579 { 13580 WARN_ON_ONCE(task->perf_event_ctxp); 13581 } 13582 13583 struct file *perf_event_get(unsigned int fd) 13584 { 13585 struct file *file = fget(fd); 13586 if (!file) 13587 return ERR_PTR(-EBADF); 13588 13589 if (file->f_op != &perf_fops) { 13590 fput(file); 13591 return ERR_PTR(-EBADF); 13592 } 13593 13594 return file; 13595 } 13596 13597 const struct perf_event *perf_get_event(struct file *file) 13598 { 13599 if (file->f_op != &perf_fops) 13600 return ERR_PTR(-EINVAL); 13601 13602 return file->private_data; 13603 } 13604 13605 const struct perf_event_attr *perf_event_attrs(struct perf_event *event) 13606 { 13607 if (!event) 13608 return ERR_PTR(-EINVAL); 13609 13610 return &event->attr; 13611 } 13612 13613 int perf_allow_kernel(struct perf_event_attr *attr) 13614 { 13615 if (sysctl_perf_event_paranoid > 1 && !perfmon_capable()) 13616 return -EACCES; 13617 13618 return security_perf_event_open(attr, PERF_SECURITY_KERNEL); 13619 } 13620 EXPORT_SYMBOL_GPL(perf_allow_kernel); 13621 13622 /* 13623 * Inherit an event from parent task to child task. 13624 * 13625 * Returns: 13626 * - valid pointer on success 13627 * - NULL for orphaned events 13628 * - IS_ERR() on error 13629 */ 13630 static struct perf_event * 13631 inherit_event(struct perf_event *parent_event, 13632 struct task_struct *parent, 13633 struct perf_event_context *parent_ctx, 13634 struct task_struct *child, 13635 struct perf_event *group_leader, 13636 struct perf_event_context *child_ctx) 13637 { 13638 enum perf_event_state parent_state = parent_event->state; 13639 struct perf_event_pmu_context *pmu_ctx; 13640 struct perf_event *child_event; 13641 unsigned long flags; 13642 13643 /* 13644 * Instead of creating recursive hierarchies of events, 13645 * we link inherited events back to the original parent, 13646 * which has a filp for sure, which we use as the reference 13647 * count: 13648 */ 13649 if (parent_event->parent) 13650 parent_event = parent_event->parent; 13651 13652 child_event = perf_event_alloc(&parent_event->attr, 13653 parent_event->cpu, 13654 child, 13655 group_leader, parent_event, 13656 NULL, NULL, -1); 13657 if (IS_ERR(child_event)) 13658 return child_event; 13659 13660 pmu_ctx = find_get_pmu_context(child_event->pmu, child_ctx, child_event); 13661 if (IS_ERR(pmu_ctx)) { 13662 free_event(child_event); 13663 return ERR_CAST(pmu_ctx); 13664 } 13665 child_event->pmu_ctx = pmu_ctx; 13666 13667 /* 13668 * is_orphaned_event() and list_add_tail(&parent_event->child_list) 13669 * must be under the same lock in order to serialize against 13670 * perf_event_release_kernel(), such that either we must observe 13671 * is_orphaned_event() or they will observe us on the child_list. 13672 */ 13673 mutex_lock(&parent_event->child_mutex); 13674 if (is_orphaned_event(parent_event) || 13675 !atomic_long_inc_not_zero(&parent_event->refcount)) { 13676 mutex_unlock(&parent_event->child_mutex); 13677 /* task_ctx_data is freed with child_ctx */ 13678 free_event(child_event); 13679 return NULL; 13680 } 13681 13682 get_ctx(child_ctx); 13683 13684 /* 13685 * Make the child state follow the state of the parent event, 13686 * not its attr.disabled bit. We hold the parent's mutex, 13687 * so we won't race with perf_event_{en, dis}able_family. 13688 */ 13689 if (parent_state >= PERF_EVENT_STATE_INACTIVE) 13690 child_event->state = PERF_EVENT_STATE_INACTIVE; 13691 else 13692 child_event->state = PERF_EVENT_STATE_OFF; 13693 13694 if (parent_event->attr.freq) { 13695 u64 sample_period = parent_event->hw.sample_period; 13696 struct hw_perf_event *hwc = &child_event->hw; 13697 13698 hwc->sample_period = sample_period; 13699 hwc->last_period = sample_period; 13700 13701 local64_set(&hwc->period_left, sample_period); 13702 } 13703 13704 child_event->ctx = child_ctx; 13705 child_event->overflow_handler = parent_event->overflow_handler; 13706 child_event->overflow_handler_context 13707 = parent_event->overflow_handler_context; 13708 13709 /* 13710 * Precalculate sample_data sizes 13711 */ 13712 perf_event__header_size(child_event); 13713 perf_event__id_header_size(child_event); 13714 13715 /* 13716 * Link it up in the child's context: 13717 */ 13718 raw_spin_lock_irqsave(&child_ctx->lock, flags); 13719 add_event_to_ctx(child_event, child_ctx); 13720 child_event->attach_state |= PERF_ATTACH_CHILD; 13721 raw_spin_unlock_irqrestore(&child_ctx->lock, flags); 13722 13723 /* 13724 * Link this into the parent event's child list 13725 */ 13726 list_add_tail(&child_event->child_list, &parent_event->child_list); 13727 mutex_unlock(&parent_event->child_mutex); 13728 13729 return child_event; 13730 } 13731 13732 /* 13733 * Inherits an event group. 13734 * 13735 * This will quietly suppress orphaned events; !inherit_event() is not an error. 13736 * This matches with perf_event_release_kernel() removing all child events. 13737 * 13738 * Returns: 13739 * - 0 on success 13740 * - <0 on error 13741 */ 13742 static int inherit_group(struct perf_event *parent_event, 13743 struct task_struct *parent, 13744 struct perf_event_context *parent_ctx, 13745 struct task_struct *child, 13746 struct perf_event_context *child_ctx) 13747 { 13748 struct perf_event *leader; 13749 struct perf_event *sub; 13750 struct perf_event *child_ctr; 13751 13752 leader = inherit_event(parent_event, parent, parent_ctx, 13753 child, NULL, child_ctx); 13754 if (IS_ERR(leader)) 13755 return PTR_ERR(leader); 13756 /* 13757 * @leader can be NULL here because of is_orphaned_event(). In this 13758 * case inherit_event() will create individual events, similar to what 13759 * perf_group_detach() would do anyway. 13760 */ 13761 for_each_sibling_event(sub, parent_event) { 13762 child_ctr = inherit_event(sub, parent, parent_ctx, 13763 child, leader, child_ctx); 13764 if (IS_ERR(child_ctr)) 13765 return PTR_ERR(child_ctr); 13766 13767 if (sub->aux_event == parent_event && child_ctr && 13768 !perf_get_aux_event(child_ctr, leader)) 13769 return -EINVAL; 13770 } 13771 if (leader) 13772 leader->group_generation = parent_event->group_generation; 13773 return 0; 13774 } 13775 13776 /* 13777 * Creates the child task context and tries to inherit the event-group. 13778 * 13779 * Clears @inherited_all on !attr.inherited or error. Note that we'll leave 13780 * inherited_all set when we 'fail' to inherit an orphaned event; this is 13781 * consistent with perf_event_release_kernel() removing all child events. 13782 * 13783 * Returns: 13784 * - 0 on success 13785 * - <0 on error 13786 */ 13787 static int 13788 inherit_task_group(struct perf_event *event, struct task_struct *parent, 13789 struct perf_event_context *parent_ctx, 13790 struct task_struct *child, 13791 u64 clone_flags, int *inherited_all) 13792 { 13793 struct perf_event_context *child_ctx; 13794 int ret; 13795 13796 if (!event->attr.inherit || 13797 (event->attr.inherit_thread && !(clone_flags & CLONE_THREAD)) || 13798 /* Do not inherit if sigtrap and signal handlers were cleared. */ 13799 (event->attr.sigtrap && (clone_flags & CLONE_CLEAR_SIGHAND))) { 13800 *inherited_all = 0; 13801 return 0; 13802 } 13803 13804 child_ctx = child->perf_event_ctxp; 13805 if (!child_ctx) { 13806 /* 13807 * This is executed from the parent task context, so 13808 * inherit events that have been marked for cloning. 13809 * First allocate and initialize a context for the 13810 * child. 13811 */ 13812 child_ctx = alloc_perf_context(child); 13813 if (!child_ctx) 13814 return -ENOMEM; 13815 13816 child->perf_event_ctxp = child_ctx; 13817 } 13818 13819 ret = inherit_group(event, parent, parent_ctx, child, child_ctx); 13820 if (ret) 13821 *inherited_all = 0; 13822 13823 return ret; 13824 } 13825 13826 /* 13827 * Initialize the perf_event context in task_struct 13828 */ 13829 static int perf_event_init_context(struct task_struct *child, u64 clone_flags) 13830 { 13831 struct perf_event_context *child_ctx, *parent_ctx; 13832 struct perf_event_context *cloned_ctx; 13833 struct perf_event *event; 13834 struct task_struct *parent = current; 13835 int inherited_all = 1; 13836 unsigned long flags; 13837 int ret = 0; 13838 13839 if (likely(!parent->perf_event_ctxp)) 13840 return 0; 13841 13842 /* 13843 * If the parent's context is a clone, pin it so it won't get 13844 * swapped under us. 13845 */ 13846 parent_ctx = perf_pin_task_context(parent); 13847 if (!parent_ctx) 13848 return 0; 13849 13850 /* 13851 * No need to check if parent_ctx != NULL here; since we saw 13852 * it non-NULL earlier, the only reason for it to become NULL 13853 * is if we exit, and since we're currently in the middle of 13854 * a fork we can't be exiting at the same time. 13855 */ 13856 13857 /* 13858 * Lock the parent list. No need to lock the child - not PID 13859 * hashed yet and not running, so nobody can access it. 13860 */ 13861 mutex_lock(&parent_ctx->mutex); 13862 13863 /* 13864 * We dont have to disable NMIs - we are only looking at 13865 * the list, not manipulating it: 13866 */ 13867 perf_event_groups_for_each(event, &parent_ctx->pinned_groups) { 13868 ret = inherit_task_group(event, parent, parent_ctx, 13869 child, clone_flags, &inherited_all); 13870 if (ret) 13871 goto out_unlock; 13872 } 13873 13874 /* 13875 * We can't hold ctx->lock when iterating the ->flexible_group list due 13876 * to allocations, but we need to prevent rotation because 13877 * rotate_ctx() will change the list from interrupt context. 13878 */ 13879 raw_spin_lock_irqsave(&parent_ctx->lock, flags); 13880 parent_ctx->rotate_disable = 1; 13881 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); 13882 13883 perf_event_groups_for_each(event, &parent_ctx->flexible_groups) { 13884 ret = inherit_task_group(event, parent, parent_ctx, 13885 child, clone_flags, &inherited_all); 13886 if (ret) 13887 goto out_unlock; 13888 } 13889 13890 raw_spin_lock_irqsave(&parent_ctx->lock, flags); 13891 parent_ctx->rotate_disable = 0; 13892 13893 child_ctx = child->perf_event_ctxp; 13894 13895 if (child_ctx && inherited_all) { 13896 /* 13897 * Mark the child context as a clone of the parent 13898 * context, or of whatever the parent is a clone of. 13899 * 13900 * Note that if the parent is a clone, the holding of 13901 * parent_ctx->lock avoids it from being uncloned. 13902 */ 13903 cloned_ctx = parent_ctx->parent_ctx; 13904 if (cloned_ctx) { 13905 child_ctx->parent_ctx = cloned_ctx; 13906 child_ctx->parent_gen = parent_ctx->parent_gen; 13907 } else { 13908 child_ctx->parent_ctx = parent_ctx; 13909 child_ctx->parent_gen = parent_ctx->generation; 13910 } 13911 get_ctx(child_ctx->parent_ctx); 13912 } 13913 13914 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); 13915 out_unlock: 13916 mutex_unlock(&parent_ctx->mutex); 13917 13918 perf_unpin_context(parent_ctx); 13919 put_ctx(parent_ctx); 13920 13921 return ret; 13922 } 13923 13924 /* 13925 * Initialize the perf_event context in task_struct 13926 */ 13927 int perf_event_init_task(struct task_struct *child, u64 clone_flags) 13928 { 13929 int ret; 13930 13931 memset(child->perf_recursion, 0, sizeof(child->perf_recursion)); 13932 child->perf_event_ctxp = NULL; 13933 mutex_init(&child->perf_event_mutex); 13934 INIT_LIST_HEAD(&child->perf_event_list); 13935 13936 ret = perf_event_init_context(child, clone_flags); 13937 if (ret) { 13938 perf_event_free_task(child); 13939 return ret; 13940 } 13941 13942 return 0; 13943 } 13944 13945 static void __init perf_event_init_all_cpus(void) 13946 { 13947 struct swevent_htable *swhash; 13948 struct perf_cpu_context *cpuctx; 13949 int cpu; 13950 13951 zalloc_cpumask_var(&perf_online_mask, GFP_KERNEL); 13952 zalloc_cpumask_var(&perf_online_core_mask, GFP_KERNEL); 13953 zalloc_cpumask_var(&perf_online_die_mask, GFP_KERNEL); 13954 zalloc_cpumask_var(&perf_online_cluster_mask, GFP_KERNEL); 13955 zalloc_cpumask_var(&perf_online_pkg_mask, GFP_KERNEL); 13956 zalloc_cpumask_var(&perf_online_sys_mask, GFP_KERNEL); 13957 13958 13959 for_each_possible_cpu(cpu) { 13960 swhash = &per_cpu(swevent_htable, cpu); 13961 mutex_init(&swhash->hlist_mutex); 13962 13963 INIT_LIST_HEAD(&per_cpu(pmu_sb_events.list, cpu)); 13964 raw_spin_lock_init(&per_cpu(pmu_sb_events.lock, cpu)); 13965 13966 INIT_LIST_HEAD(&per_cpu(sched_cb_list, cpu)); 13967 13968 cpuctx = per_cpu_ptr(&perf_cpu_context, cpu); 13969 __perf_event_init_context(&cpuctx->ctx); 13970 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); 13971 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); 13972 cpuctx->online = cpumask_test_cpu(cpu, perf_online_mask); 13973 cpuctx->heap_size = ARRAY_SIZE(cpuctx->heap_default); 13974 cpuctx->heap = cpuctx->heap_default; 13975 } 13976 } 13977 13978 static void perf_swevent_init_cpu(unsigned int cpu) 13979 { 13980 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); 13981 13982 mutex_lock(&swhash->hlist_mutex); 13983 if (swhash->hlist_refcount > 0 && !swevent_hlist_deref(swhash)) { 13984 struct swevent_hlist *hlist; 13985 13986 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); 13987 WARN_ON(!hlist); 13988 rcu_assign_pointer(swhash->swevent_hlist, hlist); 13989 } 13990 mutex_unlock(&swhash->hlist_mutex); 13991 } 13992 13993 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC_CORE 13994 static void __perf_event_exit_context(void *__info) 13995 { 13996 struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context); 13997 struct perf_event_context *ctx = __info; 13998 struct perf_event *event; 13999 14000 raw_spin_lock(&ctx->lock); 14001 ctx_sched_out(ctx, NULL, EVENT_TIME); 14002 list_for_each_entry(event, &ctx->event_list, event_entry) 14003 __perf_remove_from_context(event, cpuctx, ctx, (void *)DETACH_GROUP); 14004 raw_spin_unlock(&ctx->lock); 14005 } 14006 14007 static void perf_event_clear_cpumask(unsigned int cpu) 14008 { 14009 int target[PERF_PMU_MAX_SCOPE]; 14010 unsigned int scope; 14011 struct pmu *pmu; 14012 14013 cpumask_clear_cpu(cpu, perf_online_mask); 14014 14015 for (scope = PERF_PMU_SCOPE_NONE + 1; scope < PERF_PMU_MAX_SCOPE; scope++) { 14016 const struct cpumask *cpumask = perf_scope_cpu_topology_cpumask(scope, cpu); 14017 struct cpumask *pmu_cpumask = perf_scope_cpumask(scope); 14018 14019 target[scope] = -1; 14020 if (WARN_ON_ONCE(!pmu_cpumask || !cpumask)) 14021 continue; 14022 14023 if (!cpumask_test_and_clear_cpu(cpu, pmu_cpumask)) 14024 continue; 14025 target[scope] = cpumask_any_but(cpumask, cpu); 14026 if (target[scope] < nr_cpu_ids) 14027 cpumask_set_cpu(target[scope], pmu_cpumask); 14028 } 14029 14030 /* migrate */ 14031 list_for_each_entry(pmu, &pmus, entry) { 14032 if (pmu->scope == PERF_PMU_SCOPE_NONE || 14033 WARN_ON_ONCE(pmu->scope >= PERF_PMU_MAX_SCOPE)) 14034 continue; 14035 14036 if (target[pmu->scope] >= 0 && target[pmu->scope] < nr_cpu_ids) 14037 perf_pmu_migrate_context(pmu, cpu, target[pmu->scope]); 14038 } 14039 } 14040 14041 static void perf_event_exit_cpu_context(int cpu) 14042 { 14043 struct perf_cpu_context *cpuctx; 14044 struct perf_event_context *ctx; 14045 14046 // XXX simplify cpuctx->online 14047 mutex_lock(&pmus_lock); 14048 /* 14049 * Clear the cpumasks, and migrate to other CPUs if possible. 14050 * Must be invoked before the __perf_event_exit_context. 14051 */ 14052 perf_event_clear_cpumask(cpu); 14053 cpuctx = per_cpu_ptr(&perf_cpu_context, cpu); 14054 ctx = &cpuctx->ctx; 14055 14056 mutex_lock(&ctx->mutex); 14057 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); 14058 cpuctx->online = 0; 14059 mutex_unlock(&ctx->mutex); 14060 mutex_unlock(&pmus_lock); 14061 } 14062 #else 14063 14064 static void perf_event_exit_cpu_context(int cpu) { } 14065 14066 #endif 14067 14068 static void perf_event_setup_cpumask(unsigned int cpu) 14069 { 14070 struct cpumask *pmu_cpumask; 14071 unsigned int scope; 14072 14073 /* 14074 * Early boot stage, the cpumask hasn't been set yet. 14075 * The perf_online_<domain>_masks includes the first CPU of each domain. 14076 * Always unconditionally set the boot CPU for the perf_online_<domain>_masks. 14077 */ 14078 if (cpumask_empty(perf_online_mask)) { 14079 for (scope = PERF_PMU_SCOPE_NONE + 1; scope < PERF_PMU_MAX_SCOPE; scope++) { 14080 pmu_cpumask = perf_scope_cpumask(scope); 14081 if (WARN_ON_ONCE(!pmu_cpumask)) 14082 continue; 14083 cpumask_set_cpu(cpu, pmu_cpumask); 14084 } 14085 goto end; 14086 } 14087 14088 for (scope = PERF_PMU_SCOPE_NONE + 1; scope < PERF_PMU_MAX_SCOPE; scope++) { 14089 const struct cpumask *cpumask = perf_scope_cpu_topology_cpumask(scope, cpu); 14090 14091 pmu_cpumask = perf_scope_cpumask(scope); 14092 14093 if (WARN_ON_ONCE(!pmu_cpumask || !cpumask)) 14094 continue; 14095 14096 if (!cpumask_empty(cpumask) && 14097 cpumask_any_and(pmu_cpumask, cpumask) >= nr_cpu_ids) 14098 cpumask_set_cpu(cpu, pmu_cpumask); 14099 } 14100 end: 14101 cpumask_set_cpu(cpu, perf_online_mask); 14102 } 14103 14104 int perf_event_init_cpu(unsigned int cpu) 14105 { 14106 struct perf_cpu_context *cpuctx; 14107 struct perf_event_context *ctx; 14108 14109 perf_swevent_init_cpu(cpu); 14110 14111 mutex_lock(&pmus_lock); 14112 perf_event_setup_cpumask(cpu); 14113 cpuctx = per_cpu_ptr(&perf_cpu_context, cpu); 14114 ctx = &cpuctx->ctx; 14115 14116 mutex_lock(&ctx->mutex); 14117 cpuctx->online = 1; 14118 mutex_unlock(&ctx->mutex); 14119 mutex_unlock(&pmus_lock); 14120 14121 return 0; 14122 } 14123 14124 int perf_event_exit_cpu(unsigned int cpu) 14125 { 14126 perf_event_exit_cpu_context(cpu); 14127 return 0; 14128 } 14129 14130 static int 14131 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) 14132 { 14133 int cpu; 14134 14135 for_each_online_cpu(cpu) 14136 perf_event_exit_cpu(cpu); 14137 14138 return NOTIFY_OK; 14139 } 14140 14141 /* 14142 * Run the perf reboot notifier at the very last possible moment so that 14143 * the generic watchdog code runs as long as possible. 14144 */ 14145 static struct notifier_block perf_reboot_notifier = { 14146 .notifier_call = perf_reboot, 14147 .priority = INT_MIN, 14148 }; 14149 14150 void __init perf_event_init(void) 14151 { 14152 int ret; 14153 14154 idr_init(&pmu_idr); 14155 14156 perf_event_init_all_cpus(); 14157 init_srcu_struct(&pmus_srcu); 14158 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); 14159 perf_pmu_register(&perf_cpu_clock, "cpu_clock", -1); 14160 perf_pmu_register(&perf_task_clock, "task_clock", -1); 14161 perf_tp_register(); 14162 perf_event_init_cpu(smp_processor_id()); 14163 register_reboot_notifier(&perf_reboot_notifier); 14164 14165 ret = init_hw_breakpoint(); 14166 WARN(ret, "hw_breakpoint initialization failed with: %d", ret); 14167 14168 perf_event_cache = KMEM_CACHE(perf_event, SLAB_PANIC); 14169 14170 /* 14171 * Build time assertion that we keep the data_head at the intended 14172 * location. IOW, validation we got the __reserved[] size right. 14173 */ 14174 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) 14175 != 1024); 14176 } 14177 14178 ssize_t perf_event_sysfs_show(struct device *dev, struct device_attribute *attr, 14179 char *page) 14180 { 14181 struct perf_pmu_events_attr *pmu_attr = 14182 container_of(attr, struct perf_pmu_events_attr, attr); 14183 14184 if (pmu_attr->event_str) 14185 return sprintf(page, "%s\n", pmu_attr->event_str); 14186 14187 return 0; 14188 } 14189 EXPORT_SYMBOL_GPL(perf_event_sysfs_show); 14190 14191 static int __init perf_event_sysfs_init(void) 14192 { 14193 struct pmu *pmu; 14194 int ret; 14195 14196 mutex_lock(&pmus_lock); 14197 14198 ret = bus_register(&pmu_bus); 14199 if (ret) 14200 goto unlock; 14201 14202 list_for_each_entry(pmu, &pmus, entry) { 14203 if (pmu->dev) 14204 continue; 14205 14206 ret = pmu_dev_alloc(pmu); 14207 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); 14208 } 14209 pmu_bus_running = 1; 14210 ret = 0; 14211 14212 unlock: 14213 mutex_unlock(&pmus_lock); 14214 14215 return ret; 14216 } 14217 device_initcall(perf_event_sysfs_init); 14218 14219 #ifdef CONFIG_CGROUP_PERF 14220 static struct cgroup_subsys_state * 14221 perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css) 14222 { 14223 struct perf_cgroup *jc; 14224 14225 jc = kzalloc(sizeof(*jc), GFP_KERNEL); 14226 if (!jc) 14227 return ERR_PTR(-ENOMEM); 14228 14229 jc->info = alloc_percpu(struct perf_cgroup_info); 14230 if (!jc->info) { 14231 kfree(jc); 14232 return ERR_PTR(-ENOMEM); 14233 } 14234 14235 return &jc->css; 14236 } 14237 14238 static void perf_cgroup_css_free(struct cgroup_subsys_state *css) 14239 { 14240 struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css); 14241 14242 free_percpu(jc->info); 14243 kfree(jc); 14244 } 14245 14246 static int perf_cgroup_css_online(struct cgroup_subsys_state *css) 14247 { 14248 perf_event_cgroup(css->cgroup); 14249 return 0; 14250 } 14251 14252 static int __perf_cgroup_move(void *info) 14253 { 14254 struct task_struct *task = info; 14255 14256 preempt_disable(); 14257 perf_cgroup_switch(task); 14258 preempt_enable(); 14259 14260 return 0; 14261 } 14262 14263 static void perf_cgroup_attach(struct cgroup_taskset *tset) 14264 { 14265 struct task_struct *task; 14266 struct cgroup_subsys_state *css; 14267 14268 cgroup_taskset_for_each(task, css, tset) 14269 task_function_call(task, __perf_cgroup_move, task); 14270 } 14271 14272 struct cgroup_subsys perf_event_cgrp_subsys = { 14273 .css_alloc = perf_cgroup_css_alloc, 14274 .css_free = perf_cgroup_css_free, 14275 .css_online = perf_cgroup_css_online, 14276 .attach = perf_cgroup_attach, 14277 /* 14278 * Implicitly enable on dfl hierarchy so that perf events can 14279 * always be filtered by cgroup2 path as long as perf_event 14280 * controller is not mounted on a legacy hierarchy. 14281 */ 14282 .implicit_on_dfl = true, 14283 .threaded = true, 14284 }; 14285 #endif /* CONFIG_CGROUP_PERF */ 14286 14287 DEFINE_STATIC_CALL_RET0(perf_snapshot_branch_stack, perf_snapshot_branch_stack_t); 14288