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