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 <pzijlstr@redhat.com> 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/sysfs.h> 22 #include <linux/dcache.h> 23 #include <linux/percpu.h> 24 #include <linux/ptrace.h> 25 #include <linux/reboot.h> 26 #include <linux/vmstat.h> 27 #include <linux/device.h> 28 #include <linux/export.h> 29 #include <linux/vmalloc.h> 30 #include <linux/hardirq.h> 31 #include <linux/rculist.h> 32 #include <linux/uaccess.h> 33 #include <linux/syscalls.h> 34 #include <linux/anon_inodes.h> 35 #include <linux/kernel_stat.h> 36 #include <linux/perf_event.h> 37 #include <linux/ftrace_event.h> 38 #include <linux/hw_breakpoint.h> 39 #include <linux/mm_types.h> 40 #include <linux/cgroup.h> 41 42 #include "internal.h" 43 44 #include <asm/irq_regs.h> 45 46 struct remote_function_call { 47 struct task_struct *p; 48 int (*func)(void *info); 49 void *info; 50 int ret; 51 }; 52 53 static void remote_function(void *data) 54 { 55 struct remote_function_call *tfc = data; 56 struct task_struct *p = tfc->p; 57 58 if (p) { 59 tfc->ret = -EAGAIN; 60 if (task_cpu(p) != smp_processor_id() || !task_curr(p)) 61 return; 62 } 63 64 tfc->ret = tfc->func(tfc->info); 65 } 66 67 /** 68 * task_function_call - call a function on the cpu on which a task runs 69 * @p: the task to evaluate 70 * @func: the function to be called 71 * @info: the function call argument 72 * 73 * Calls the function @func when the task is currently running. This might 74 * be on the current CPU, which just calls the function directly 75 * 76 * returns: @func return value, or 77 * -ESRCH - when the process isn't running 78 * -EAGAIN - when the process moved away 79 */ 80 static int 81 task_function_call(struct task_struct *p, int (*func) (void *info), void *info) 82 { 83 struct remote_function_call data = { 84 .p = p, 85 .func = func, 86 .info = info, 87 .ret = -ESRCH, /* No such (running) process */ 88 }; 89 90 if (task_curr(p)) 91 smp_call_function_single(task_cpu(p), remote_function, &data, 1); 92 93 return data.ret; 94 } 95 96 /** 97 * cpu_function_call - call a function on the cpu 98 * @func: the function to be called 99 * @info: the function call argument 100 * 101 * Calls the function @func on the remote cpu. 102 * 103 * returns: @func return value or -ENXIO when the cpu is offline 104 */ 105 static int cpu_function_call(int cpu, int (*func) (void *info), void *info) 106 { 107 struct remote_function_call data = { 108 .p = NULL, 109 .func = func, 110 .info = info, 111 .ret = -ENXIO, /* No such CPU */ 112 }; 113 114 smp_call_function_single(cpu, remote_function, &data, 1); 115 116 return data.ret; 117 } 118 119 #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\ 120 PERF_FLAG_FD_OUTPUT |\ 121 PERF_FLAG_PID_CGROUP) 122 123 /* 124 * branch priv levels that need permission checks 125 */ 126 #define PERF_SAMPLE_BRANCH_PERM_PLM \ 127 (PERF_SAMPLE_BRANCH_KERNEL |\ 128 PERF_SAMPLE_BRANCH_HV) 129 130 enum event_type_t { 131 EVENT_FLEXIBLE = 0x1, 132 EVENT_PINNED = 0x2, 133 EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED, 134 }; 135 136 /* 137 * perf_sched_events : >0 events exist 138 * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu 139 */ 140 struct static_key_deferred perf_sched_events __read_mostly; 141 static DEFINE_PER_CPU(atomic_t, perf_cgroup_events); 142 static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events); 143 144 static atomic_t nr_mmap_events __read_mostly; 145 static atomic_t nr_comm_events __read_mostly; 146 static atomic_t nr_task_events __read_mostly; 147 148 static LIST_HEAD(pmus); 149 static DEFINE_MUTEX(pmus_lock); 150 static struct srcu_struct pmus_srcu; 151 152 /* 153 * perf event paranoia level: 154 * -1 - not paranoid at all 155 * 0 - disallow raw tracepoint access for unpriv 156 * 1 - disallow cpu events for unpriv 157 * 2 - disallow kernel profiling for unpriv 158 */ 159 int sysctl_perf_event_paranoid __read_mostly = 1; 160 161 /* Minimum for 512 kiB + 1 user control page */ 162 int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */ 163 164 /* 165 * max perf event sample rate 166 */ 167 #define DEFAULT_MAX_SAMPLE_RATE 100000 168 int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE; 169 static int max_samples_per_tick __read_mostly = 170 DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ); 171 172 int perf_proc_update_handler(struct ctl_table *table, int write, 173 void __user *buffer, size_t *lenp, 174 loff_t *ppos) 175 { 176 int ret = proc_dointvec(table, write, buffer, lenp, ppos); 177 178 if (ret || !write) 179 return ret; 180 181 max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ); 182 183 return 0; 184 } 185 186 static atomic64_t perf_event_id; 187 188 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, 189 enum event_type_t event_type); 190 191 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, 192 enum event_type_t event_type, 193 struct task_struct *task); 194 195 static void update_context_time(struct perf_event_context *ctx); 196 static u64 perf_event_time(struct perf_event *event); 197 198 static void ring_buffer_attach(struct perf_event *event, 199 struct ring_buffer *rb); 200 201 void __weak perf_event_print_debug(void) { } 202 203 extern __weak const char *perf_pmu_name(void) 204 { 205 return "pmu"; 206 } 207 208 static inline u64 perf_clock(void) 209 { 210 return local_clock(); 211 } 212 213 static inline struct perf_cpu_context * 214 __get_cpu_context(struct perf_event_context *ctx) 215 { 216 return this_cpu_ptr(ctx->pmu->pmu_cpu_context); 217 } 218 219 static void perf_ctx_lock(struct perf_cpu_context *cpuctx, 220 struct perf_event_context *ctx) 221 { 222 raw_spin_lock(&cpuctx->ctx.lock); 223 if (ctx) 224 raw_spin_lock(&ctx->lock); 225 } 226 227 static void perf_ctx_unlock(struct perf_cpu_context *cpuctx, 228 struct perf_event_context *ctx) 229 { 230 if (ctx) 231 raw_spin_unlock(&ctx->lock); 232 raw_spin_unlock(&cpuctx->ctx.lock); 233 } 234 235 #ifdef CONFIG_CGROUP_PERF 236 237 /* 238 * perf_cgroup_info keeps track of time_enabled for a cgroup. 239 * This is a per-cpu dynamically allocated data structure. 240 */ 241 struct perf_cgroup_info { 242 u64 time; 243 u64 timestamp; 244 }; 245 246 struct perf_cgroup { 247 struct cgroup_subsys_state css; 248 struct perf_cgroup_info __percpu *info; 249 }; 250 251 /* 252 * Must ensure cgroup is pinned (css_get) before calling 253 * this function. In other words, we cannot call this function 254 * if there is no cgroup event for the current CPU context. 255 */ 256 static inline struct perf_cgroup * 257 perf_cgroup_from_task(struct task_struct *task) 258 { 259 return container_of(task_subsys_state(task, perf_subsys_id), 260 struct perf_cgroup, css); 261 } 262 263 static inline bool 264 perf_cgroup_match(struct perf_event *event) 265 { 266 struct perf_event_context *ctx = event->ctx; 267 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); 268 269 /* @event doesn't care about cgroup */ 270 if (!event->cgrp) 271 return true; 272 273 /* wants specific cgroup scope but @cpuctx isn't associated with any */ 274 if (!cpuctx->cgrp) 275 return false; 276 277 /* 278 * Cgroup scoping is recursive. An event enabled for a cgroup is 279 * also enabled for all its descendant cgroups. If @cpuctx's 280 * cgroup is a descendant of @event's (the test covers identity 281 * case), it's a match. 282 */ 283 return cgroup_is_descendant(cpuctx->cgrp->css.cgroup, 284 event->cgrp->css.cgroup); 285 } 286 287 static inline bool perf_tryget_cgroup(struct perf_event *event) 288 { 289 return css_tryget(&event->cgrp->css); 290 } 291 292 static inline void perf_put_cgroup(struct perf_event *event) 293 { 294 css_put(&event->cgrp->css); 295 } 296 297 static inline void perf_detach_cgroup(struct perf_event *event) 298 { 299 perf_put_cgroup(event); 300 event->cgrp = NULL; 301 } 302 303 static inline int is_cgroup_event(struct perf_event *event) 304 { 305 return event->cgrp != NULL; 306 } 307 308 static inline u64 perf_cgroup_event_time(struct perf_event *event) 309 { 310 struct perf_cgroup_info *t; 311 312 t = per_cpu_ptr(event->cgrp->info, event->cpu); 313 return t->time; 314 } 315 316 static inline void __update_cgrp_time(struct perf_cgroup *cgrp) 317 { 318 struct perf_cgroup_info *info; 319 u64 now; 320 321 now = perf_clock(); 322 323 info = this_cpu_ptr(cgrp->info); 324 325 info->time += now - info->timestamp; 326 info->timestamp = now; 327 } 328 329 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) 330 { 331 struct perf_cgroup *cgrp_out = cpuctx->cgrp; 332 if (cgrp_out) 333 __update_cgrp_time(cgrp_out); 334 } 335 336 static inline void update_cgrp_time_from_event(struct perf_event *event) 337 { 338 struct perf_cgroup *cgrp; 339 340 /* 341 * ensure we access cgroup data only when needed and 342 * when we know the cgroup is pinned (css_get) 343 */ 344 if (!is_cgroup_event(event)) 345 return; 346 347 cgrp = perf_cgroup_from_task(current); 348 /* 349 * Do not update time when cgroup is not active 350 */ 351 if (cgrp == event->cgrp) 352 __update_cgrp_time(event->cgrp); 353 } 354 355 static inline void 356 perf_cgroup_set_timestamp(struct task_struct *task, 357 struct perf_event_context *ctx) 358 { 359 struct perf_cgroup *cgrp; 360 struct perf_cgroup_info *info; 361 362 /* 363 * ctx->lock held by caller 364 * ensure we do not access cgroup data 365 * unless we have the cgroup pinned (css_get) 366 */ 367 if (!task || !ctx->nr_cgroups) 368 return; 369 370 cgrp = perf_cgroup_from_task(task); 371 info = this_cpu_ptr(cgrp->info); 372 info->timestamp = ctx->timestamp; 373 } 374 375 #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */ 376 #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */ 377 378 /* 379 * reschedule events based on the cgroup constraint of task. 380 * 381 * mode SWOUT : schedule out everything 382 * mode SWIN : schedule in based on cgroup for next 383 */ 384 void perf_cgroup_switch(struct task_struct *task, int mode) 385 { 386 struct perf_cpu_context *cpuctx; 387 struct pmu *pmu; 388 unsigned long flags; 389 390 /* 391 * disable interrupts to avoid geting nr_cgroup 392 * changes via __perf_event_disable(). Also 393 * avoids preemption. 394 */ 395 local_irq_save(flags); 396 397 /* 398 * we reschedule only in the presence of cgroup 399 * constrained events. 400 */ 401 rcu_read_lock(); 402 403 list_for_each_entry_rcu(pmu, &pmus, entry) { 404 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); 405 if (cpuctx->unique_pmu != pmu) 406 continue; /* ensure we process each cpuctx once */ 407 408 /* 409 * perf_cgroup_events says at least one 410 * context on this CPU has cgroup events. 411 * 412 * ctx->nr_cgroups reports the number of cgroup 413 * events for a context. 414 */ 415 if (cpuctx->ctx.nr_cgroups > 0) { 416 perf_ctx_lock(cpuctx, cpuctx->task_ctx); 417 perf_pmu_disable(cpuctx->ctx.pmu); 418 419 if (mode & PERF_CGROUP_SWOUT) { 420 cpu_ctx_sched_out(cpuctx, EVENT_ALL); 421 /* 422 * must not be done before ctxswout due 423 * to event_filter_match() in event_sched_out() 424 */ 425 cpuctx->cgrp = NULL; 426 } 427 428 if (mode & PERF_CGROUP_SWIN) { 429 WARN_ON_ONCE(cpuctx->cgrp); 430 /* 431 * set cgrp before ctxsw in to allow 432 * event_filter_match() to not have to pass 433 * task around 434 */ 435 cpuctx->cgrp = perf_cgroup_from_task(task); 436 cpu_ctx_sched_in(cpuctx, EVENT_ALL, task); 437 } 438 perf_pmu_enable(cpuctx->ctx.pmu); 439 perf_ctx_unlock(cpuctx, cpuctx->task_ctx); 440 } 441 } 442 443 rcu_read_unlock(); 444 445 local_irq_restore(flags); 446 } 447 448 static inline void perf_cgroup_sched_out(struct task_struct *task, 449 struct task_struct *next) 450 { 451 struct perf_cgroup *cgrp1; 452 struct perf_cgroup *cgrp2 = NULL; 453 454 /* 455 * we come here when we know perf_cgroup_events > 0 456 */ 457 cgrp1 = perf_cgroup_from_task(task); 458 459 /* 460 * next is NULL when called from perf_event_enable_on_exec() 461 * that will systematically cause a cgroup_switch() 462 */ 463 if (next) 464 cgrp2 = perf_cgroup_from_task(next); 465 466 /* 467 * only schedule out current cgroup events if we know 468 * that we are switching to a different cgroup. Otherwise, 469 * do no touch the cgroup events. 470 */ 471 if (cgrp1 != cgrp2) 472 perf_cgroup_switch(task, PERF_CGROUP_SWOUT); 473 } 474 475 static inline void perf_cgroup_sched_in(struct task_struct *prev, 476 struct task_struct *task) 477 { 478 struct perf_cgroup *cgrp1; 479 struct perf_cgroup *cgrp2 = NULL; 480 481 /* 482 * we come here when we know perf_cgroup_events > 0 483 */ 484 cgrp1 = perf_cgroup_from_task(task); 485 486 /* prev can never be NULL */ 487 cgrp2 = perf_cgroup_from_task(prev); 488 489 /* 490 * only need to schedule in cgroup events if we are changing 491 * cgroup during ctxsw. Cgroup events were not scheduled 492 * out of ctxsw out if that was not the case. 493 */ 494 if (cgrp1 != cgrp2) 495 perf_cgroup_switch(task, PERF_CGROUP_SWIN); 496 } 497 498 static inline int perf_cgroup_connect(int fd, struct perf_event *event, 499 struct perf_event_attr *attr, 500 struct perf_event *group_leader) 501 { 502 struct perf_cgroup *cgrp; 503 struct cgroup_subsys_state *css; 504 struct fd f = fdget(fd); 505 int ret = 0; 506 507 if (!f.file) 508 return -EBADF; 509 510 css = cgroup_css_from_dir(f.file, perf_subsys_id); 511 if (IS_ERR(css)) { 512 ret = PTR_ERR(css); 513 goto out; 514 } 515 516 cgrp = container_of(css, struct perf_cgroup, css); 517 event->cgrp = cgrp; 518 519 /* must be done before we fput() the file */ 520 if (!perf_tryget_cgroup(event)) { 521 event->cgrp = NULL; 522 ret = -ENOENT; 523 goto out; 524 } 525 526 /* 527 * all events in a group must monitor 528 * the same cgroup because a task belongs 529 * to only one perf cgroup at a time 530 */ 531 if (group_leader && group_leader->cgrp != cgrp) { 532 perf_detach_cgroup(event); 533 ret = -EINVAL; 534 } 535 out: 536 fdput(f); 537 return ret; 538 } 539 540 static inline void 541 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) 542 { 543 struct perf_cgroup_info *t; 544 t = per_cpu_ptr(event->cgrp->info, event->cpu); 545 event->shadow_ctx_time = now - t->timestamp; 546 } 547 548 static inline void 549 perf_cgroup_defer_enabled(struct perf_event *event) 550 { 551 /* 552 * when the current task's perf cgroup does not match 553 * the event's, we need to remember to call the 554 * perf_mark_enable() function the first time a task with 555 * a matching perf cgroup is scheduled in. 556 */ 557 if (is_cgroup_event(event) && !perf_cgroup_match(event)) 558 event->cgrp_defer_enabled = 1; 559 } 560 561 static inline void 562 perf_cgroup_mark_enabled(struct perf_event *event, 563 struct perf_event_context *ctx) 564 { 565 struct perf_event *sub; 566 u64 tstamp = perf_event_time(event); 567 568 if (!event->cgrp_defer_enabled) 569 return; 570 571 event->cgrp_defer_enabled = 0; 572 573 event->tstamp_enabled = tstamp - event->total_time_enabled; 574 list_for_each_entry(sub, &event->sibling_list, group_entry) { 575 if (sub->state >= PERF_EVENT_STATE_INACTIVE) { 576 sub->tstamp_enabled = tstamp - sub->total_time_enabled; 577 sub->cgrp_defer_enabled = 0; 578 } 579 } 580 } 581 #else /* !CONFIG_CGROUP_PERF */ 582 583 static inline bool 584 perf_cgroup_match(struct perf_event *event) 585 { 586 return true; 587 } 588 589 static inline void perf_detach_cgroup(struct perf_event *event) 590 {} 591 592 static inline int is_cgroup_event(struct perf_event *event) 593 { 594 return 0; 595 } 596 597 static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event) 598 { 599 return 0; 600 } 601 602 static inline void update_cgrp_time_from_event(struct perf_event *event) 603 { 604 } 605 606 static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx) 607 { 608 } 609 610 static inline void perf_cgroup_sched_out(struct task_struct *task, 611 struct task_struct *next) 612 { 613 } 614 615 static inline void perf_cgroup_sched_in(struct task_struct *prev, 616 struct task_struct *task) 617 { 618 } 619 620 static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event, 621 struct perf_event_attr *attr, 622 struct perf_event *group_leader) 623 { 624 return -EINVAL; 625 } 626 627 static inline void 628 perf_cgroup_set_timestamp(struct task_struct *task, 629 struct perf_event_context *ctx) 630 { 631 } 632 633 void 634 perf_cgroup_switch(struct task_struct *task, struct task_struct *next) 635 { 636 } 637 638 static inline void 639 perf_cgroup_set_shadow_time(struct perf_event *event, u64 now) 640 { 641 } 642 643 static inline u64 perf_cgroup_event_time(struct perf_event *event) 644 { 645 return 0; 646 } 647 648 static inline void 649 perf_cgroup_defer_enabled(struct perf_event *event) 650 { 651 } 652 653 static inline void 654 perf_cgroup_mark_enabled(struct perf_event *event, 655 struct perf_event_context *ctx) 656 { 657 } 658 #endif 659 660 void perf_pmu_disable(struct pmu *pmu) 661 { 662 int *count = this_cpu_ptr(pmu->pmu_disable_count); 663 if (!(*count)++) 664 pmu->pmu_disable(pmu); 665 } 666 667 void perf_pmu_enable(struct pmu *pmu) 668 { 669 int *count = this_cpu_ptr(pmu->pmu_disable_count); 670 if (!--(*count)) 671 pmu->pmu_enable(pmu); 672 } 673 674 static DEFINE_PER_CPU(struct list_head, rotation_list); 675 676 /* 677 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized 678 * because they're strictly cpu affine and rotate_start is called with IRQs 679 * disabled, while rotate_context is called from IRQ context. 680 */ 681 static void perf_pmu_rotate_start(struct pmu *pmu) 682 { 683 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); 684 struct list_head *head = &__get_cpu_var(rotation_list); 685 686 WARN_ON(!irqs_disabled()); 687 688 if (list_empty(&cpuctx->rotation_list)) 689 list_add(&cpuctx->rotation_list, head); 690 } 691 692 static void get_ctx(struct perf_event_context *ctx) 693 { 694 WARN_ON(!atomic_inc_not_zero(&ctx->refcount)); 695 } 696 697 static void put_ctx(struct perf_event_context *ctx) 698 { 699 if (atomic_dec_and_test(&ctx->refcount)) { 700 if (ctx->parent_ctx) 701 put_ctx(ctx->parent_ctx); 702 if (ctx->task) 703 put_task_struct(ctx->task); 704 kfree_rcu(ctx, rcu_head); 705 } 706 } 707 708 static void unclone_ctx(struct perf_event_context *ctx) 709 { 710 if (ctx->parent_ctx) { 711 put_ctx(ctx->parent_ctx); 712 ctx->parent_ctx = NULL; 713 } 714 } 715 716 static u32 perf_event_pid(struct perf_event *event, struct task_struct *p) 717 { 718 /* 719 * only top level events have the pid namespace they were created in 720 */ 721 if (event->parent) 722 event = event->parent; 723 724 return task_tgid_nr_ns(p, event->ns); 725 } 726 727 static u32 perf_event_tid(struct perf_event *event, struct task_struct *p) 728 { 729 /* 730 * only top level events have the pid namespace they were created in 731 */ 732 if (event->parent) 733 event = event->parent; 734 735 return task_pid_nr_ns(p, event->ns); 736 } 737 738 /* 739 * If we inherit events we want to return the parent event id 740 * to userspace. 741 */ 742 static u64 primary_event_id(struct perf_event *event) 743 { 744 u64 id = event->id; 745 746 if (event->parent) 747 id = event->parent->id; 748 749 return id; 750 } 751 752 /* 753 * Get the perf_event_context for a task and lock it. 754 * This has to cope with with the fact that until it is locked, 755 * the context could get moved to another task. 756 */ 757 static struct perf_event_context * 758 perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags) 759 { 760 struct perf_event_context *ctx; 761 762 rcu_read_lock(); 763 retry: 764 ctx = rcu_dereference(task->perf_event_ctxp[ctxn]); 765 if (ctx) { 766 /* 767 * If this context is a clone of another, it might 768 * get swapped for another underneath us by 769 * perf_event_task_sched_out, though the 770 * rcu_read_lock() protects us from any context 771 * getting freed. Lock the context and check if it 772 * got swapped before we could get the lock, and retry 773 * if so. If we locked the right context, then it 774 * can't get swapped on us any more. 775 */ 776 raw_spin_lock_irqsave(&ctx->lock, *flags); 777 if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) { 778 raw_spin_unlock_irqrestore(&ctx->lock, *flags); 779 goto retry; 780 } 781 782 if (!atomic_inc_not_zero(&ctx->refcount)) { 783 raw_spin_unlock_irqrestore(&ctx->lock, *flags); 784 ctx = NULL; 785 } 786 } 787 rcu_read_unlock(); 788 return ctx; 789 } 790 791 /* 792 * Get the context for a task and increment its pin_count so it 793 * can't get swapped to another task. This also increments its 794 * reference count so that the context can't get freed. 795 */ 796 static struct perf_event_context * 797 perf_pin_task_context(struct task_struct *task, int ctxn) 798 { 799 struct perf_event_context *ctx; 800 unsigned long flags; 801 802 ctx = perf_lock_task_context(task, ctxn, &flags); 803 if (ctx) { 804 ++ctx->pin_count; 805 raw_spin_unlock_irqrestore(&ctx->lock, flags); 806 } 807 return ctx; 808 } 809 810 static void perf_unpin_context(struct perf_event_context *ctx) 811 { 812 unsigned long flags; 813 814 raw_spin_lock_irqsave(&ctx->lock, flags); 815 --ctx->pin_count; 816 raw_spin_unlock_irqrestore(&ctx->lock, flags); 817 } 818 819 /* 820 * Update the record of the current time in a context. 821 */ 822 static void update_context_time(struct perf_event_context *ctx) 823 { 824 u64 now = perf_clock(); 825 826 ctx->time += now - ctx->timestamp; 827 ctx->timestamp = now; 828 } 829 830 static u64 perf_event_time(struct perf_event *event) 831 { 832 struct perf_event_context *ctx = event->ctx; 833 834 if (is_cgroup_event(event)) 835 return perf_cgroup_event_time(event); 836 837 return ctx ? ctx->time : 0; 838 } 839 840 /* 841 * Update the total_time_enabled and total_time_running fields for a event. 842 * The caller of this function needs to hold the ctx->lock. 843 */ 844 static void update_event_times(struct perf_event *event) 845 { 846 struct perf_event_context *ctx = event->ctx; 847 u64 run_end; 848 849 if (event->state < PERF_EVENT_STATE_INACTIVE || 850 event->group_leader->state < PERF_EVENT_STATE_INACTIVE) 851 return; 852 /* 853 * in cgroup mode, time_enabled represents 854 * the time the event was enabled AND active 855 * tasks were in the monitored cgroup. This is 856 * independent of the activity of the context as 857 * there may be a mix of cgroup and non-cgroup events. 858 * 859 * That is why we treat cgroup events differently 860 * here. 861 */ 862 if (is_cgroup_event(event)) 863 run_end = perf_cgroup_event_time(event); 864 else if (ctx->is_active) 865 run_end = ctx->time; 866 else 867 run_end = event->tstamp_stopped; 868 869 event->total_time_enabled = run_end - event->tstamp_enabled; 870 871 if (event->state == PERF_EVENT_STATE_INACTIVE) 872 run_end = event->tstamp_stopped; 873 else 874 run_end = perf_event_time(event); 875 876 event->total_time_running = run_end - event->tstamp_running; 877 878 } 879 880 /* 881 * Update total_time_enabled and total_time_running for all events in a group. 882 */ 883 static void update_group_times(struct perf_event *leader) 884 { 885 struct perf_event *event; 886 887 update_event_times(leader); 888 list_for_each_entry(event, &leader->sibling_list, group_entry) 889 update_event_times(event); 890 } 891 892 static struct list_head * 893 ctx_group_list(struct perf_event *event, struct perf_event_context *ctx) 894 { 895 if (event->attr.pinned) 896 return &ctx->pinned_groups; 897 else 898 return &ctx->flexible_groups; 899 } 900 901 /* 902 * Add a event from the lists for its context. 903 * Must be called with ctx->mutex and ctx->lock held. 904 */ 905 static void 906 list_add_event(struct perf_event *event, struct perf_event_context *ctx) 907 { 908 WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT); 909 event->attach_state |= PERF_ATTACH_CONTEXT; 910 911 /* 912 * If we're a stand alone event or group leader, we go to the context 913 * list, group events are kept attached to the group so that 914 * perf_group_detach can, at all times, locate all siblings. 915 */ 916 if (event->group_leader == event) { 917 struct list_head *list; 918 919 if (is_software_event(event)) 920 event->group_flags |= PERF_GROUP_SOFTWARE; 921 922 list = ctx_group_list(event, ctx); 923 list_add_tail(&event->group_entry, list); 924 } 925 926 if (is_cgroup_event(event)) 927 ctx->nr_cgroups++; 928 929 if (has_branch_stack(event)) 930 ctx->nr_branch_stack++; 931 932 list_add_rcu(&event->event_entry, &ctx->event_list); 933 if (!ctx->nr_events) 934 perf_pmu_rotate_start(ctx->pmu); 935 ctx->nr_events++; 936 if (event->attr.inherit_stat) 937 ctx->nr_stat++; 938 } 939 940 /* 941 * Initialize event state based on the perf_event_attr::disabled. 942 */ 943 static inline void perf_event__state_init(struct perf_event *event) 944 { 945 event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF : 946 PERF_EVENT_STATE_INACTIVE; 947 } 948 949 /* 950 * Called at perf_event creation and when events are attached/detached from a 951 * group. 952 */ 953 static void perf_event__read_size(struct perf_event *event) 954 { 955 int entry = sizeof(u64); /* value */ 956 int size = 0; 957 int nr = 1; 958 959 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 960 size += sizeof(u64); 961 962 if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 963 size += sizeof(u64); 964 965 if (event->attr.read_format & PERF_FORMAT_ID) 966 entry += sizeof(u64); 967 968 if (event->attr.read_format & PERF_FORMAT_GROUP) { 969 nr += event->group_leader->nr_siblings; 970 size += sizeof(u64); 971 } 972 973 size += entry * nr; 974 event->read_size = size; 975 } 976 977 static void perf_event__header_size(struct perf_event *event) 978 { 979 struct perf_sample_data *data; 980 u64 sample_type = event->attr.sample_type; 981 u16 size = 0; 982 983 perf_event__read_size(event); 984 985 if (sample_type & PERF_SAMPLE_IP) 986 size += sizeof(data->ip); 987 988 if (sample_type & PERF_SAMPLE_ADDR) 989 size += sizeof(data->addr); 990 991 if (sample_type & PERF_SAMPLE_PERIOD) 992 size += sizeof(data->period); 993 994 if (sample_type & PERF_SAMPLE_WEIGHT) 995 size += sizeof(data->weight); 996 997 if (sample_type & PERF_SAMPLE_READ) 998 size += event->read_size; 999 1000 if (sample_type & PERF_SAMPLE_DATA_SRC) 1001 size += sizeof(data->data_src.val); 1002 1003 event->header_size = size; 1004 } 1005 1006 static void perf_event__id_header_size(struct perf_event *event) 1007 { 1008 struct perf_sample_data *data; 1009 u64 sample_type = event->attr.sample_type; 1010 u16 size = 0; 1011 1012 if (sample_type & PERF_SAMPLE_TID) 1013 size += sizeof(data->tid_entry); 1014 1015 if (sample_type & PERF_SAMPLE_TIME) 1016 size += sizeof(data->time); 1017 1018 if (sample_type & PERF_SAMPLE_ID) 1019 size += sizeof(data->id); 1020 1021 if (sample_type & PERF_SAMPLE_STREAM_ID) 1022 size += sizeof(data->stream_id); 1023 1024 if (sample_type & PERF_SAMPLE_CPU) 1025 size += sizeof(data->cpu_entry); 1026 1027 event->id_header_size = size; 1028 } 1029 1030 static void perf_group_attach(struct perf_event *event) 1031 { 1032 struct perf_event *group_leader = event->group_leader, *pos; 1033 1034 /* 1035 * We can have double attach due to group movement in perf_event_open. 1036 */ 1037 if (event->attach_state & PERF_ATTACH_GROUP) 1038 return; 1039 1040 event->attach_state |= PERF_ATTACH_GROUP; 1041 1042 if (group_leader == event) 1043 return; 1044 1045 if (group_leader->group_flags & PERF_GROUP_SOFTWARE && 1046 !is_software_event(event)) 1047 group_leader->group_flags &= ~PERF_GROUP_SOFTWARE; 1048 1049 list_add_tail(&event->group_entry, &group_leader->sibling_list); 1050 group_leader->nr_siblings++; 1051 1052 perf_event__header_size(group_leader); 1053 1054 list_for_each_entry(pos, &group_leader->sibling_list, group_entry) 1055 perf_event__header_size(pos); 1056 } 1057 1058 /* 1059 * Remove a event from the lists for its context. 1060 * Must be called with ctx->mutex and ctx->lock held. 1061 */ 1062 static void 1063 list_del_event(struct perf_event *event, struct perf_event_context *ctx) 1064 { 1065 struct perf_cpu_context *cpuctx; 1066 /* 1067 * We can have double detach due to exit/hot-unplug + close. 1068 */ 1069 if (!(event->attach_state & PERF_ATTACH_CONTEXT)) 1070 return; 1071 1072 event->attach_state &= ~PERF_ATTACH_CONTEXT; 1073 1074 if (is_cgroup_event(event)) { 1075 ctx->nr_cgroups--; 1076 cpuctx = __get_cpu_context(ctx); 1077 /* 1078 * if there are no more cgroup events 1079 * then cler cgrp to avoid stale pointer 1080 * in update_cgrp_time_from_cpuctx() 1081 */ 1082 if (!ctx->nr_cgroups) 1083 cpuctx->cgrp = NULL; 1084 } 1085 1086 if (has_branch_stack(event)) 1087 ctx->nr_branch_stack--; 1088 1089 ctx->nr_events--; 1090 if (event->attr.inherit_stat) 1091 ctx->nr_stat--; 1092 1093 list_del_rcu(&event->event_entry); 1094 1095 if (event->group_leader == event) 1096 list_del_init(&event->group_entry); 1097 1098 update_group_times(event); 1099 1100 /* 1101 * If event was in error state, then keep it 1102 * that way, otherwise bogus counts will be 1103 * returned on read(). The only way to get out 1104 * of error state is by explicit re-enabling 1105 * of the event 1106 */ 1107 if (event->state > PERF_EVENT_STATE_OFF) 1108 event->state = PERF_EVENT_STATE_OFF; 1109 } 1110 1111 static void perf_group_detach(struct perf_event *event) 1112 { 1113 struct perf_event *sibling, *tmp; 1114 struct list_head *list = NULL; 1115 1116 /* 1117 * We can have double detach due to exit/hot-unplug + close. 1118 */ 1119 if (!(event->attach_state & PERF_ATTACH_GROUP)) 1120 return; 1121 1122 event->attach_state &= ~PERF_ATTACH_GROUP; 1123 1124 /* 1125 * If this is a sibling, remove it from its group. 1126 */ 1127 if (event->group_leader != event) { 1128 list_del_init(&event->group_entry); 1129 event->group_leader->nr_siblings--; 1130 goto out; 1131 } 1132 1133 if (!list_empty(&event->group_entry)) 1134 list = &event->group_entry; 1135 1136 /* 1137 * If this was a group event with sibling events then 1138 * upgrade the siblings to singleton events by adding them 1139 * to whatever list we are on. 1140 */ 1141 list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) { 1142 if (list) 1143 list_move_tail(&sibling->group_entry, list); 1144 sibling->group_leader = sibling; 1145 1146 /* Inherit group flags from the previous leader */ 1147 sibling->group_flags = event->group_flags; 1148 } 1149 1150 out: 1151 perf_event__header_size(event->group_leader); 1152 1153 list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry) 1154 perf_event__header_size(tmp); 1155 } 1156 1157 static inline int 1158 event_filter_match(struct perf_event *event) 1159 { 1160 return (event->cpu == -1 || event->cpu == smp_processor_id()) 1161 && perf_cgroup_match(event); 1162 } 1163 1164 static void 1165 event_sched_out(struct perf_event *event, 1166 struct perf_cpu_context *cpuctx, 1167 struct perf_event_context *ctx) 1168 { 1169 u64 tstamp = perf_event_time(event); 1170 u64 delta; 1171 /* 1172 * An event which could not be activated because of 1173 * filter mismatch still needs to have its timings 1174 * maintained, otherwise bogus information is return 1175 * via read() for time_enabled, time_running: 1176 */ 1177 if (event->state == PERF_EVENT_STATE_INACTIVE 1178 && !event_filter_match(event)) { 1179 delta = tstamp - event->tstamp_stopped; 1180 event->tstamp_running += delta; 1181 event->tstamp_stopped = tstamp; 1182 } 1183 1184 if (event->state != PERF_EVENT_STATE_ACTIVE) 1185 return; 1186 1187 event->state = PERF_EVENT_STATE_INACTIVE; 1188 if (event->pending_disable) { 1189 event->pending_disable = 0; 1190 event->state = PERF_EVENT_STATE_OFF; 1191 } 1192 event->tstamp_stopped = tstamp; 1193 event->pmu->del(event, 0); 1194 event->oncpu = -1; 1195 1196 if (!is_software_event(event)) 1197 cpuctx->active_oncpu--; 1198 ctx->nr_active--; 1199 if (event->attr.freq && event->attr.sample_freq) 1200 ctx->nr_freq--; 1201 if (event->attr.exclusive || !cpuctx->active_oncpu) 1202 cpuctx->exclusive = 0; 1203 } 1204 1205 static void 1206 group_sched_out(struct perf_event *group_event, 1207 struct perf_cpu_context *cpuctx, 1208 struct perf_event_context *ctx) 1209 { 1210 struct perf_event *event; 1211 int state = group_event->state; 1212 1213 event_sched_out(group_event, cpuctx, ctx); 1214 1215 /* 1216 * Schedule out siblings (if any): 1217 */ 1218 list_for_each_entry(event, &group_event->sibling_list, group_entry) 1219 event_sched_out(event, cpuctx, ctx); 1220 1221 if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive) 1222 cpuctx->exclusive = 0; 1223 } 1224 1225 /* 1226 * Cross CPU call to remove a performance event 1227 * 1228 * We disable the event on the hardware level first. After that we 1229 * remove it from the context list. 1230 */ 1231 static int __perf_remove_from_context(void *info) 1232 { 1233 struct perf_event *event = info; 1234 struct perf_event_context *ctx = event->ctx; 1235 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); 1236 1237 raw_spin_lock(&ctx->lock); 1238 event_sched_out(event, cpuctx, ctx); 1239 list_del_event(event, ctx); 1240 if (!ctx->nr_events && cpuctx->task_ctx == ctx) { 1241 ctx->is_active = 0; 1242 cpuctx->task_ctx = NULL; 1243 } 1244 raw_spin_unlock(&ctx->lock); 1245 1246 return 0; 1247 } 1248 1249 1250 /* 1251 * Remove the event from a task's (or a CPU's) list of events. 1252 * 1253 * CPU events are removed with a smp call. For task events we only 1254 * call when the task is on a CPU. 1255 * 1256 * If event->ctx is a cloned context, callers must make sure that 1257 * every task struct that event->ctx->task could possibly point to 1258 * remains valid. This is OK when called from perf_release since 1259 * that only calls us on the top-level context, which can't be a clone. 1260 * When called from perf_event_exit_task, it's OK because the 1261 * context has been detached from its task. 1262 */ 1263 static void perf_remove_from_context(struct perf_event *event) 1264 { 1265 struct perf_event_context *ctx = event->ctx; 1266 struct task_struct *task = ctx->task; 1267 1268 lockdep_assert_held(&ctx->mutex); 1269 1270 if (!task) { 1271 /* 1272 * Per cpu events are removed via an smp call and 1273 * the removal is always successful. 1274 */ 1275 cpu_function_call(event->cpu, __perf_remove_from_context, event); 1276 return; 1277 } 1278 1279 retry: 1280 if (!task_function_call(task, __perf_remove_from_context, event)) 1281 return; 1282 1283 raw_spin_lock_irq(&ctx->lock); 1284 /* 1285 * If we failed to find a running task, but find the context active now 1286 * that we've acquired the ctx->lock, retry. 1287 */ 1288 if (ctx->is_active) { 1289 raw_spin_unlock_irq(&ctx->lock); 1290 goto retry; 1291 } 1292 1293 /* 1294 * Since the task isn't running, its safe to remove the event, us 1295 * holding the ctx->lock ensures the task won't get scheduled in. 1296 */ 1297 list_del_event(event, ctx); 1298 raw_spin_unlock_irq(&ctx->lock); 1299 } 1300 1301 /* 1302 * Cross CPU call to disable a performance event 1303 */ 1304 int __perf_event_disable(void *info) 1305 { 1306 struct perf_event *event = info; 1307 struct perf_event_context *ctx = event->ctx; 1308 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); 1309 1310 /* 1311 * If this is a per-task event, need to check whether this 1312 * event's task is the current task on this cpu. 1313 * 1314 * Can trigger due to concurrent perf_event_context_sched_out() 1315 * flipping contexts around. 1316 */ 1317 if (ctx->task && cpuctx->task_ctx != ctx) 1318 return -EINVAL; 1319 1320 raw_spin_lock(&ctx->lock); 1321 1322 /* 1323 * If the event is on, turn it off. 1324 * If it is in error state, leave it in error state. 1325 */ 1326 if (event->state >= PERF_EVENT_STATE_INACTIVE) { 1327 update_context_time(ctx); 1328 update_cgrp_time_from_event(event); 1329 update_group_times(event); 1330 if (event == event->group_leader) 1331 group_sched_out(event, cpuctx, ctx); 1332 else 1333 event_sched_out(event, cpuctx, ctx); 1334 event->state = PERF_EVENT_STATE_OFF; 1335 } 1336 1337 raw_spin_unlock(&ctx->lock); 1338 1339 return 0; 1340 } 1341 1342 /* 1343 * Disable a event. 1344 * 1345 * If event->ctx is a cloned context, callers must make sure that 1346 * every task struct that event->ctx->task could possibly point to 1347 * remains valid. This condition is satisifed when called through 1348 * perf_event_for_each_child or perf_event_for_each because they 1349 * hold the top-level event's child_mutex, so any descendant that 1350 * goes to exit will block in sync_child_event. 1351 * When called from perf_pending_event it's OK because event->ctx 1352 * is the current context on this CPU and preemption is disabled, 1353 * hence we can't get into perf_event_task_sched_out for this context. 1354 */ 1355 void perf_event_disable(struct perf_event *event) 1356 { 1357 struct perf_event_context *ctx = event->ctx; 1358 struct task_struct *task = ctx->task; 1359 1360 if (!task) { 1361 /* 1362 * Disable the event on the cpu that it's on 1363 */ 1364 cpu_function_call(event->cpu, __perf_event_disable, event); 1365 return; 1366 } 1367 1368 retry: 1369 if (!task_function_call(task, __perf_event_disable, event)) 1370 return; 1371 1372 raw_spin_lock_irq(&ctx->lock); 1373 /* 1374 * If the event is still active, we need to retry the cross-call. 1375 */ 1376 if (event->state == PERF_EVENT_STATE_ACTIVE) { 1377 raw_spin_unlock_irq(&ctx->lock); 1378 /* 1379 * Reload the task pointer, it might have been changed by 1380 * a concurrent perf_event_context_sched_out(). 1381 */ 1382 task = ctx->task; 1383 goto retry; 1384 } 1385 1386 /* 1387 * Since we have the lock this context can't be scheduled 1388 * in, so we can change the state safely. 1389 */ 1390 if (event->state == PERF_EVENT_STATE_INACTIVE) { 1391 update_group_times(event); 1392 event->state = PERF_EVENT_STATE_OFF; 1393 } 1394 raw_spin_unlock_irq(&ctx->lock); 1395 } 1396 EXPORT_SYMBOL_GPL(perf_event_disable); 1397 1398 static void perf_set_shadow_time(struct perf_event *event, 1399 struct perf_event_context *ctx, 1400 u64 tstamp) 1401 { 1402 /* 1403 * use the correct time source for the time snapshot 1404 * 1405 * We could get by without this by leveraging the 1406 * fact that to get to this function, the caller 1407 * has most likely already called update_context_time() 1408 * and update_cgrp_time_xx() and thus both timestamp 1409 * are identical (or very close). Given that tstamp is, 1410 * already adjusted for cgroup, we could say that: 1411 * tstamp - ctx->timestamp 1412 * is equivalent to 1413 * tstamp - cgrp->timestamp. 1414 * 1415 * Then, in perf_output_read(), the calculation would 1416 * work with no changes because: 1417 * - event is guaranteed scheduled in 1418 * - no scheduled out in between 1419 * - thus the timestamp would be the same 1420 * 1421 * But this is a bit hairy. 1422 * 1423 * So instead, we have an explicit cgroup call to remain 1424 * within the time time source all along. We believe it 1425 * is cleaner and simpler to understand. 1426 */ 1427 if (is_cgroup_event(event)) 1428 perf_cgroup_set_shadow_time(event, tstamp); 1429 else 1430 event->shadow_ctx_time = tstamp - ctx->timestamp; 1431 } 1432 1433 #define MAX_INTERRUPTS (~0ULL) 1434 1435 static void perf_log_throttle(struct perf_event *event, int enable); 1436 1437 static int 1438 event_sched_in(struct perf_event *event, 1439 struct perf_cpu_context *cpuctx, 1440 struct perf_event_context *ctx) 1441 { 1442 u64 tstamp = perf_event_time(event); 1443 1444 if (event->state <= PERF_EVENT_STATE_OFF) 1445 return 0; 1446 1447 event->state = PERF_EVENT_STATE_ACTIVE; 1448 event->oncpu = smp_processor_id(); 1449 1450 /* 1451 * Unthrottle events, since we scheduled we might have missed several 1452 * ticks already, also for a heavily scheduling task there is little 1453 * guarantee it'll get a tick in a timely manner. 1454 */ 1455 if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) { 1456 perf_log_throttle(event, 1); 1457 event->hw.interrupts = 0; 1458 } 1459 1460 /* 1461 * The new state must be visible before we turn it on in the hardware: 1462 */ 1463 smp_wmb(); 1464 1465 if (event->pmu->add(event, PERF_EF_START)) { 1466 event->state = PERF_EVENT_STATE_INACTIVE; 1467 event->oncpu = -1; 1468 return -EAGAIN; 1469 } 1470 1471 event->tstamp_running += tstamp - event->tstamp_stopped; 1472 1473 perf_set_shadow_time(event, ctx, tstamp); 1474 1475 if (!is_software_event(event)) 1476 cpuctx->active_oncpu++; 1477 ctx->nr_active++; 1478 if (event->attr.freq && event->attr.sample_freq) 1479 ctx->nr_freq++; 1480 1481 if (event->attr.exclusive) 1482 cpuctx->exclusive = 1; 1483 1484 return 0; 1485 } 1486 1487 static int 1488 group_sched_in(struct perf_event *group_event, 1489 struct perf_cpu_context *cpuctx, 1490 struct perf_event_context *ctx) 1491 { 1492 struct perf_event *event, *partial_group = NULL; 1493 struct pmu *pmu = group_event->pmu; 1494 u64 now = ctx->time; 1495 bool simulate = false; 1496 1497 if (group_event->state == PERF_EVENT_STATE_OFF) 1498 return 0; 1499 1500 pmu->start_txn(pmu); 1501 1502 if (event_sched_in(group_event, cpuctx, ctx)) { 1503 pmu->cancel_txn(pmu); 1504 return -EAGAIN; 1505 } 1506 1507 /* 1508 * Schedule in siblings as one group (if any): 1509 */ 1510 list_for_each_entry(event, &group_event->sibling_list, group_entry) { 1511 if (event_sched_in(event, cpuctx, ctx)) { 1512 partial_group = event; 1513 goto group_error; 1514 } 1515 } 1516 1517 if (!pmu->commit_txn(pmu)) 1518 return 0; 1519 1520 group_error: 1521 /* 1522 * Groups can be scheduled in as one unit only, so undo any 1523 * partial group before returning: 1524 * The events up to the failed event are scheduled out normally, 1525 * tstamp_stopped will be updated. 1526 * 1527 * The failed events and the remaining siblings need to have 1528 * their timings updated as if they had gone thru event_sched_in() 1529 * and event_sched_out(). This is required to get consistent timings 1530 * across the group. This also takes care of the case where the group 1531 * could never be scheduled by ensuring tstamp_stopped is set to mark 1532 * the time the event was actually stopped, such that time delta 1533 * calculation in update_event_times() is correct. 1534 */ 1535 list_for_each_entry(event, &group_event->sibling_list, group_entry) { 1536 if (event == partial_group) 1537 simulate = true; 1538 1539 if (simulate) { 1540 event->tstamp_running += now - event->tstamp_stopped; 1541 event->tstamp_stopped = now; 1542 } else { 1543 event_sched_out(event, cpuctx, ctx); 1544 } 1545 } 1546 event_sched_out(group_event, cpuctx, ctx); 1547 1548 pmu->cancel_txn(pmu); 1549 1550 return -EAGAIN; 1551 } 1552 1553 /* 1554 * Work out whether we can put this event group on the CPU now. 1555 */ 1556 static int group_can_go_on(struct perf_event *event, 1557 struct perf_cpu_context *cpuctx, 1558 int can_add_hw) 1559 { 1560 /* 1561 * Groups consisting entirely of software events can always go on. 1562 */ 1563 if (event->group_flags & PERF_GROUP_SOFTWARE) 1564 return 1; 1565 /* 1566 * If an exclusive group is already on, no other hardware 1567 * events can go on. 1568 */ 1569 if (cpuctx->exclusive) 1570 return 0; 1571 /* 1572 * If this group is exclusive and there are already 1573 * events on the CPU, it can't go on. 1574 */ 1575 if (event->attr.exclusive && cpuctx->active_oncpu) 1576 return 0; 1577 /* 1578 * Otherwise, try to add it if all previous groups were able 1579 * to go on. 1580 */ 1581 return can_add_hw; 1582 } 1583 1584 static void add_event_to_ctx(struct perf_event *event, 1585 struct perf_event_context *ctx) 1586 { 1587 u64 tstamp = perf_event_time(event); 1588 1589 list_add_event(event, ctx); 1590 perf_group_attach(event); 1591 event->tstamp_enabled = tstamp; 1592 event->tstamp_running = tstamp; 1593 event->tstamp_stopped = tstamp; 1594 } 1595 1596 static void task_ctx_sched_out(struct perf_event_context *ctx); 1597 static void 1598 ctx_sched_in(struct perf_event_context *ctx, 1599 struct perf_cpu_context *cpuctx, 1600 enum event_type_t event_type, 1601 struct task_struct *task); 1602 1603 static void perf_event_sched_in(struct perf_cpu_context *cpuctx, 1604 struct perf_event_context *ctx, 1605 struct task_struct *task) 1606 { 1607 cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task); 1608 if (ctx) 1609 ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task); 1610 cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task); 1611 if (ctx) 1612 ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task); 1613 } 1614 1615 /* 1616 * Cross CPU call to install and enable a performance event 1617 * 1618 * Must be called with ctx->mutex held 1619 */ 1620 static int __perf_install_in_context(void *info) 1621 { 1622 struct perf_event *event = info; 1623 struct perf_event_context *ctx = event->ctx; 1624 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); 1625 struct perf_event_context *task_ctx = cpuctx->task_ctx; 1626 struct task_struct *task = current; 1627 1628 perf_ctx_lock(cpuctx, task_ctx); 1629 perf_pmu_disable(cpuctx->ctx.pmu); 1630 1631 /* 1632 * If there was an active task_ctx schedule it out. 1633 */ 1634 if (task_ctx) 1635 task_ctx_sched_out(task_ctx); 1636 1637 /* 1638 * If the context we're installing events in is not the 1639 * active task_ctx, flip them. 1640 */ 1641 if (ctx->task && task_ctx != ctx) { 1642 if (task_ctx) 1643 raw_spin_unlock(&task_ctx->lock); 1644 raw_spin_lock(&ctx->lock); 1645 task_ctx = ctx; 1646 } 1647 1648 if (task_ctx) { 1649 cpuctx->task_ctx = task_ctx; 1650 task = task_ctx->task; 1651 } 1652 1653 cpu_ctx_sched_out(cpuctx, EVENT_ALL); 1654 1655 update_context_time(ctx); 1656 /* 1657 * update cgrp time only if current cgrp 1658 * matches event->cgrp. Must be done before 1659 * calling add_event_to_ctx() 1660 */ 1661 update_cgrp_time_from_event(event); 1662 1663 add_event_to_ctx(event, ctx); 1664 1665 /* 1666 * Schedule everything back in 1667 */ 1668 perf_event_sched_in(cpuctx, task_ctx, task); 1669 1670 perf_pmu_enable(cpuctx->ctx.pmu); 1671 perf_ctx_unlock(cpuctx, task_ctx); 1672 1673 return 0; 1674 } 1675 1676 /* 1677 * Attach a performance event to a context 1678 * 1679 * First we add the event to the list with the hardware enable bit 1680 * in event->hw_config cleared. 1681 * 1682 * If the event is attached to a task which is on a CPU we use a smp 1683 * call to enable it in the task context. The task might have been 1684 * scheduled away, but we check this in the smp call again. 1685 */ 1686 static void 1687 perf_install_in_context(struct perf_event_context *ctx, 1688 struct perf_event *event, 1689 int cpu) 1690 { 1691 struct task_struct *task = ctx->task; 1692 1693 lockdep_assert_held(&ctx->mutex); 1694 1695 event->ctx = ctx; 1696 if (event->cpu != -1) 1697 event->cpu = cpu; 1698 1699 if (!task) { 1700 /* 1701 * Per cpu events are installed via an smp call and 1702 * the install is always successful. 1703 */ 1704 cpu_function_call(cpu, __perf_install_in_context, event); 1705 return; 1706 } 1707 1708 retry: 1709 if (!task_function_call(task, __perf_install_in_context, event)) 1710 return; 1711 1712 raw_spin_lock_irq(&ctx->lock); 1713 /* 1714 * If we failed to find a running task, but find the context active now 1715 * that we've acquired the ctx->lock, retry. 1716 */ 1717 if (ctx->is_active) { 1718 raw_spin_unlock_irq(&ctx->lock); 1719 goto retry; 1720 } 1721 1722 /* 1723 * Since the task isn't running, its safe to add the event, us holding 1724 * the ctx->lock ensures the task won't get scheduled in. 1725 */ 1726 add_event_to_ctx(event, ctx); 1727 raw_spin_unlock_irq(&ctx->lock); 1728 } 1729 1730 /* 1731 * Put a event into inactive state and update time fields. 1732 * Enabling the leader of a group effectively enables all 1733 * the group members that aren't explicitly disabled, so we 1734 * have to update their ->tstamp_enabled also. 1735 * Note: this works for group members as well as group leaders 1736 * since the non-leader members' sibling_lists will be empty. 1737 */ 1738 static void __perf_event_mark_enabled(struct perf_event *event) 1739 { 1740 struct perf_event *sub; 1741 u64 tstamp = perf_event_time(event); 1742 1743 event->state = PERF_EVENT_STATE_INACTIVE; 1744 event->tstamp_enabled = tstamp - event->total_time_enabled; 1745 list_for_each_entry(sub, &event->sibling_list, group_entry) { 1746 if (sub->state >= PERF_EVENT_STATE_INACTIVE) 1747 sub->tstamp_enabled = tstamp - sub->total_time_enabled; 1748 } 1749 } 1750 1751 /* 1752 * Cross CPU call to enable a performance event 1753 */ 1754 static int __perf_event_enable(void *info) 1755 { 1756 struct perf_event *event = info; 1757 struct perf_event_context *ctx = event->ctx; 1758 struct perf_event *leader = event->group_leader; 1759 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); 1760 int err; 1761 1762 if (WARN_ON_ONCE(!ctx->is_active)) 1763 return -EINVAL; 1764 1765 raw_spin_lock(&ctx->lock); 1766 update_context_time(ctx); 1767 1768 if (event->state >= PERF_EVENT_STATE_INACTIVE) 1769 goto unlock; 1770 1771 /* 1772 * set current task's cgroup time reference point 1773 */ 1774 perf_cgroup_set_timestamp(current, ctx); 1775 1776 __perf_event_mark_enabled(event); 1777 1778 if (!event_filter_match(event)) { 1779 if (is_cgroup_event(event)) 1780 perf_cgroup_defer_enabled(event); 1781 goto unlock; 1782 } 1783 1784 /* 1785 * If the event is in a group and isn't the group leader, 1786 * then don't put it on unless the group is on. 1787 */ 1788 if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE) 1789 goto unlock; 1790 1791 if (!group_can_go_on(event, cpuctx, 1)) { 1792 err = -EEXIST; 1793 } else { 1794 if (event == leader) 1795 err = group_sched_in(event, cpuctx, ctx); 1796 else 1797 err = event_sched_in(event, cpuctx, ctx); 1798 } 1799 1800 if (err) { 1801 /* 1802 * If this event can't go on and it's part of a 1803 * group, then the whole group has to come off. 1804 */ 1805 if (leader != event) 1806 group_sched_out(leader, cpuctx, ctx); 1807 if (leader->attr.pinned) { 1808 update_group_times(leader); 1809 leader->state = PERF_EVENT_STATE_ERROR; 1810 } 1811 } 1812 1813 unlock: 1814 raw_spin_unlock(&ctx->lock); 1815 1816 return 0; 1817 } 1818 1819 /* 1820 * Enable a event. 1821 * 1822 * If event->ctx is a cloned context, callers must make sure that 1823 * every task struct that event->ctx->task could possibly point to 1824 * remains valid. This condition is satisfied when called through 1825 * perf_event_for_each_child or perf_event_for_each as described 1826 * for perf_event_disable. 1827 */ 1828 void perf_event_enable(struct perf_event *event) 1829 { 1830 struct perf_event_context *ctx = event->ctx; 1831 struct task_struct *task = ctx->task; 1832 1833 if (!task) { 1834 /* 1835 * Enable the event on the cpu that it's on 1836 */ 1837 cpu_function_call(event->cpu, __perf_event_enable, event); 1838 return; 1839 } 1840 1841 raw_spin_lock_irq(&ctx->lock); 1842 if (event->state >= PERF_EVENT_STATE_INACTIVE) 1843 goto out; 1844 1845 /* 1846 * If the event is in error state, clear that first. 1847 * That way, if we see the event in error state below, we 1848 * know that it has gone back into error state, as distinct 1849 * from the task having been scheduled away before the 1850 * cross-call arrived. 1851 */ 1852 if (event->state == PERF_EVENT_STATE_ERROR) 1853 event->state = PERF_EVENT_STATE_OFF; 1854 1855 retry: 1856 if (!ctx->is_active) { 1857 __perf_event_mark_enabled(event); 1858 goto out; 1859 } 1860 1861 raw_spin_unlock_irq(&ctx->lock); 1862 1863 if (!task_function_call(task, __perf_event_enable, event)) 1864 return; 1865 1866 raw_spin_lock_irq(&ctx->lock); 1867 1868 /* 1869 * If the context is active and the event is still off, 1870 * we need to retry the cross-call. 1871 */ 1872 if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) { 1873 /* 1874 * task could have been flipped by a concurrent 1875 * perf_event_context_sched_out() 1876 */ 1877 task = ctx->task; 1878 goto retry; 1879 } 1880 1881 out: 1882 raw_spin_unlock_irq(&ctx->lock); 1883 } 1884 EXPORT_SYMBOL_GPL(perf_event_enable); 1885 1886 int perf_event_refresh(struct perf_event *event, int refresh) 1887 { 1888 /* 1889 * not supported on inherited events 1890 */ 1891 if (event->attr.inherit || !is_sampling_event(event)) 1892 return -EINVAL; 1893 1894 atomic_add(refresh, &event->event_limit); 1895 perf_event_enable(event); 1896 1897 return 0; 1898 } 1899 EXPORT_SYMBOL_GPL(perf_event_refresh); 1900 1901 static void ctx_sched_out(struct perf_event_context *ctx, 1902 struct perf_cpu_context *cpuctx, 1903 enum event_type_t event_type) 1904 { 1905 struct perf_event *event; 1906 int is_active = ctx->is_active; 1907 1908 ctx->is_active &= ~event_type; 1909 if (likely(!ctx->nr_events)) 1910 return; 1911 1912 update_context_time(ctx); 1913 update_cgrp_time_from_cpuctx(cpuctx); 1914 if (!ctx->nr_active) 1915 return; 1916 1917 perf_pmu_disable(ctx->pmu); 1918 if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) { 1919 list_for_each_entry(event, &ctx->pinned_groups, group_entry) 1920 group_sched_out(event, cpuctx, ctx); 1921 } 1922 1923 if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) { 1924 list_for_each_entry(event, &ctx->flexible_groups, group_entry) 1925 group_sched_out(event, cpuctx, ctx); 1926 } 1927 perf_pmu_enable(ctx->pmu); 1928 } 1929 1930 /* 1931 * Test whether two contexts are equivalent, i.e. whether they 1932 * have both been cloned from the same version of the same context 1933 * and they both have the same number of enabled events. 1934 * If the number of enabled events is the same, then the set 1935 * of enabled events should be the same, because these are both 1936 * inherited contexts, therefore we can't access individual events 1937 * in them directly with an fd; we can only enable/disable all 1938 * events via prctl, or enable/disable all events in a family 1939 * via ioctl, which will have the same effect on both contexts. 1940 */ 1941 static int context_equiv(struct perf_event_context *ctx1, 1942 struct perf_event_context *ctx2) 1943 { 1944 return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx 1945 && ctx1->parent_gen == ctx2->parent_gen 1946 && !ctx1->pin_count && !ctx2->pin_count; 1947 } 1948 1949 static void __perf_event_sync_stat(struct perf_event *event, 1950 struct perf_event *next_event) 1951 { 1952 u64 value; 1953 1954 if (!event->attr.inherit_stat) 1955 return; 1956 1957 /* 1958 * Update the event value, we cannot use perf_event_read() 1959 * because we're in the middle of a context switch and have IRQs 1960 * disabled, which upsets smp_call_function_single(), however 1961 * we know the event must be on the current CPU, therefore we 1962 * don't need to use it. 1963 */ 1964 switch (event->state) { 1965 case PERF_EVENT_STATE_ACTIVE: 1966 event->pmu->read(event); 1967 /* fall-through */ 1968 1969 case PERF_EVENT_STATE_INACTIVE: 1970 update_event_times(event); 1971 break; 1972 1973 default: 1974 break; 1975 } 1976 1977 /* 1978 * In order to keep per-task stats reliable we need to flip the event 1979 * values when we flip the contexts. 1980 */ 1981 value = local64_read(&next_event->count); 1982 value = local64_xchg(&event->count, value); 1983 local64_set(&next_event->count, value); 1984 1985 swap(event->total_time_enabled, next_event->total_time_enabled); 1986 swap(event->total_time_running, next_event->total_time_running); 1987 1988 /* 1989 * Since we swizzled the values, update the user visible data too. 1990 */ 1991 perf_event_update_userpage(event); 1992 perf_event_update_userpage(next_event); 1993 } 1994 1995 #define list_next_entry(pos, member) \ 1996 list_entry(pos->member.next, typeof(*pos), member) 1997 1998 static void perf_event_sync_stat(struct perf_event_context *ctx, 1999 struct perf_event_context *next_ctx) 2000 { 2001 struct perf_event *event, *next_event; 2002 2003 if (!ctx->nr_stat) 2004 return; 2005 2006 update_context_time(ctx); 2007 2008 event = list_first_entry(&ctx->event_list, 2009 struct perf_event, event_entry); 2010 2011 next_event = list_first_entry(&next_ctx->event_list, 2012 struct perf_event, event_entry); 2013 2014 while (&event->event_entry != &ctx->event_list && 2015 &next_event->event_entry != &next_ctx->event_list) { 2016 2017 __perf_event_sync_stat(event, next_event); 2018 2019 event = list_next_entry(event, event_entry); 2020 next_event = list_next_entry(next_event, event_entry); 2021 } 2022 } 2023 2024 static void perf_event_context_sched_out(struct task_struct *task, int ctxn, 2025 struct task_struct *next) 2026 { 2027 struct perf_event_context *ctx = task->perf_event_ctxp[ctxn]; 2028 struct perf_event_context *next_ctx; 2029 struct perf_event_context *parent; 2030 struct perf_cpu_context *cpuctx; 2031 int do_switch = 1; 2032 2033 if (likely(!ctx)) 2034 return; 2035 2036 cpuctx = __get_cpu_context(ctx); 2037 if (!cpuctx->task_ctx) 2038 return; 2039 2040 rcu_read_lock(); 2041 parent = rcu_dereference(ctx->parent_ctx); 2042 next_ctx = next->perf_event_ctxp[ctxn]; 2043 if (parent && next_ctx && 2044 rcu_dereference(next_ctx->parent_ctx) == parent) { 2045 /* 2046 * Looks like the two contexts are clones, so we might be 2047 * able to optimize the context switch. We lock both 2048 * contexts and check that they are clones under the 2049 * lock (including re-checking that neither has been 2050 * uncloned in the meantime). It doesn't matter which 2051 * order we take the locks because no other cpu could 2052 * be trying to lock both of these tasks. 2053 */ 2054 raw_spin_lock(&ctx->lock); 2055 raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING); 2056 if (context_equiv(ctx, next_ctx)) { 2057 /* 2058 * XXX do we need a memory barrier of sorts 2059 * wrt to rcu_dereference() of perf_event_ctxp 2060 */ 2061 task->perf_event_ctxp[ctxn] = next_ctx; 2062 next->perf_event_ctxp[ctxn] = ctx; 2063 ctx->task = next; 2064 next_ctx->task = task; 2065 do_switch = 0; 2066 2067 perf_event_sync_stat(ctx, next_ctx); 2068 } 2069 raw_spin_unlock(&next_ctx->lock); 2070 raw_spin_unlock(&ctx->lock); 2071 } 2072 rcu_read_unlock(); 2073 2074 if (do_switch) { 2075 raw_spin_lock(&ctx->lock); 2076 ctx_sched_out(ctx, cpuctx, EVENT_ALL); 2077 cpuctx->task_ctx = NULL; 2078 raw_spin_unlock(&ctx->lock); 2079 } 2080 } 2081 2082 #define for_each_task_context_nr(ctxn) \ 2083 for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++) 2084 2085 /* 2086 * Called from scheduler to remove the events of the current task, 2087 * with interrupts disabled. 2088 * 2089 * We stop each event and update the event value in event->count. 2090 * 2091 * This does not protect us against NMI, but disable() 2092 * sets the disabled bit in the control field of event _before_ 2093 * accessing the event control register. If a NMI hits, then it will 2094 * not restart the event. 2095 */ 2096 void __perf_event_task_sched_out(struct task_struct *task, 2097 struct task_struct *next) 2098 { 2099 int ctxn; 2100 2101 for_each_task_context_nr(ctxn) 2102 perf_event_context_sched_out(task, ctxn, next); 2103 2104 /* 2105 * if cgroup events exist on this CPU, then we need 2106 * to check if we have to switch out PMU state. 2107 * cgroup event are system-wide mode only 2108 */ 2109 if (atomic_read(&__get_cpu_var(perf_cgroup_events))) 2110 perf_cgroup_sched_out(task, next); 2111 } 2112 2113 static void task_ctx_sched_out(struct perf_event_context *ctx) 2114 { 2115 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); 2116 2117 if (!cpuctx->task_ctx) 2118 return; 2119 2120 if (WARN_ON_ONCE(ctx != cpuctx->task_ctx)) 2121 return; 2122 2123 ctx_sched_out(ctx, cpuctx, EVENT_ALL); 2124 cpuctx->task_ctx = NULL; 2125 } 2126 2127 /* 2128 * Called with IRQs disabled 2129 */ 2130 static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx, 2131 enum event_type_t event_type) 2132 { 2133 ctx_sched_out(&cpuctx->ctx, cpuctx, event_type); 2134 } 2135 2136 static void 2137 ctx_pinned_sched_in(struct perf_event_context *ctx, 2138 struct perf_cpu_context *cpuctx) 2139 { 2140 struct perf_event *event; 2141 2142 list_for_each_entry(event, &ctx->pinned_groups, group_entry) { 2143 if (event->state <= PERF_EVENT_STATE_OFF) 2144 continue; 2145 if (!event_filter_match(event)) 2146 continue; 2147 2148 /* may need to reset tstamp_enabled */ 2149 if (is_cgroup_event(event)) 2150 perf_cgroup_mark_enabled(event, ctx); 2151 2152 if (group_can_go_on(event, cpuctx, 1)) 2153 group_sched_in(event, cpuctx, ctx); 2154 2155 /* 2156 * If this pinned group hasn't been scheduled, 2157 * put it in error state. 2158 */ 2159 if (event->state == PERF_EVENT_STATE_INACTIVE) { 2160 update_group_times(event); 2161 event->state = PERF_EVENT_STATE_ERROR; 2162 } 2163 } 2164 } 2165 2166 static void 2167 ctx_flexible_sched_in(struct perf_event_context *ctx, 2168 struct perf_cpu_context *cpuctx) 2169 { 2170 struct perf_event *event; 2171 int can_add_hw = 1; 2172 2173 list_for_each_entry(event, &ctx->flexible_groups, group_entry) { 2174 /* Ignore events in OFF or ERROR state */ 2175 if (event->state <= PERF_EVENT_STATE_OFF) 2176 continue; 2177 /* 2178 * Listen to the 'cpu' scheduling filter constraint 2179 * of events: 2180 */ 2181 if (!event_filter_match(event)) 2182 continue; 2183 2184 /* may need to reset tstamp_enabled */ 2185 if (is_cgroup_event(event)) 2186 perf_cgroup_mark_enabled(event, ctx); 2187 2188 if (group_can_go_on(event, cpuctx, can_add_hw)) { 2189 if (group_sched_in(event, cpuctx, ctx)) 2190 can_add_hw = 0; 2191 } 2192 } 2193 } 2194 2195 static void 2196 ctx_sched_in(struct perf_event_context *ctx, 2197 struct perf_cpu_context *cpuctx, 2198 enum event_type_t event_type, 2199 struct task_struct *task) 2200 { 2201 u64 now; 2202 int is_active = ctx->is_active; 2203 2204 ctx->is_active |= event_type; 2205 if (likely(!ctx->nr_events)) 2206 return; 2207 2208 now = perf_clock(); 2209 ctx->timestamp = now; 2210 perf_cgroup_set_timestamp(task, ctx); 2211 /* 2212 * First go through the list and put on any pinned groups 2213 * in order to give them the best chance of going on. 2214 */ 2215 if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) 2216 ctx_pinned_sched_in(ctx, cpuctx); 2217 2218 /* Then walk through the lower prio flexible groups */ 2219 if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) 2220 ctx_flexible_sched_in(ctx, cpuctx); 2221 } 2222 2223 static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx, 2224 enum event_type_t event_type, 2225 struct task_struct *task) 2226 { 2227 struct perf_event_context *ctx = &cpuctx->ctx; 2228 2229 ctx_sched_in(ctx, cpuctx, event_type, task); 2230 } 2231 2232 static void perf_event_context_sched_in(struct perf_event_context *ctx, 2233 struct task_struct *task) 2234 { 2235 struct perf_cpu_context *cpuctx; 2236 2237 cpuctx = __get_cpu_context(ctx); 2238 if (cpuctx->task_ctx == ctx) 2239 return; 2240 2241 perf_ctx_lock(cpuctx, ctx); 2242 perf_pmu_disable(ctx->pmu); 2243 /* 2244 * We want to keep the following priority order: 2245 * cpu pinned (that don't need to move), task pinned, 2246 * cpu flexible, task flexible. 2247 */ 2248 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); 2249 2250 if (ctx->nr_events) 2251 cpuctx->task_ctx = ctx; 2252 2253 perf_event_sched_in(cpuctx, cpuctx->task_ctx, task); 2254 2255 perf_pmu_enable(ctx->pmu); 2256 perf_ctx_unlock(cpuctx, ctx); 2257 2258 /* 2259 * Since these rotations are per-cpu, we need to ensure the 2260 * cpu-context we got scheduled on is actually rotating. 2261 */ 2262 perf_pmu_rotate_start(ctx->pmu); 2263 } 2264 2265 /* 2266 * When sampling the branck stack in system-wide, it may be necessary 2267 * to flush the stack on context switch. This happens when the branch 2268 * stack does not tag its entries with the pid of the current task. 2269 * Otherwise it becomes impossible to associate a branch entry with a 2270 * task. This ambiguity is more likely to appear when the branch stack 2271 * supports priv level filtering and the user sets it to monitor only 2272 * at the user level (which could be a useful measurement in system-wide 2273 * mode). In that case, the risk is high of having a branch stack with 2274 * branch from multiple tasks. Flushing may mean dropping the existing 2275 * entries or stashing them somewhere in the PMU specific code layer. 2276 * 2277 * This function provides the context switch callback to the lower code 2278 * layer. It is invoked ONLY when there is at least one system-wide context 2279 * with at least one active event using taken branch sampling. 2280 */ 2281 static void perf_branch_stack_sched_in(struct task_struct *prev, 2282 struct task_struct *task) 2283 { 2284 struct perf_cpu_context *cpuctx; 2285 struct pmu *pmu; 2286 unsigned long flags; 2287 2288 /* no need to flush branch stack if not changing task */ 2289 if (prev == task) 2290 return; 2291 2292 local_irq_save(flags); 2293 2294 rcu_read_lock(); 2295 2296 list_for_each_entry_rcu(pmu, &pmus, entry) { 2297 cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); 2298 2299 /* 2300 * check if the context has at least one 2301 * event using PERF_SAMPLE_BRANCH_STACK 2302 */ 2303 if (cpuctx->ctx.nr_branch_stack > 0 2304 && pmu->flush_branch_stack) { 2305 2306 pmu = cpuctx->ctx.pmu; 2307 2308 perf_ctx_lock(cpuctx, cpuctx->task_ctx); 2309 2310 perf_pmu_disable(pmu); 2311 2312 pmu->flush_branch_stack(); 2313 2314 perf_pmu_enable(pmu); 2315 2316 perf_ctx_unlock(cpuctx, cpuctx->task_ctx); 2317 } 2318 } 2319 2320 rcu_read_unlock(); 2321 2322 local_irq_restore(flags); 2323 } 2324 2325 /* 2326 * Called from scheduler to add the events of the current task 2327 * with interrupts disabled. 2328 * 2329 * We restore the event value and then enable it. 2330 * 2331 * This does not protect us against NMI, but enable() 2332 * sets the enabled bit in the control field of event _before_ 2333 * accessing the event control register. If a NMI hits, then it will 2334 * keep the event running. 2335 */ 2336 void __perf_event_task_sched_in(struct task_struct *prev, 2337 struct task_struct *task) 2338 { 2339 struct perf_event_context *ctx; 2340 int ctxn; 2341 2342 for_each_task_context_nr(ctxn) { 2343 ctx = task->perf_event_ctxp[ctxn]; 2344 if (likely(!ctx)) 2345 continue; 2346 2347 perf_event_context_sched_in(ctx, task); 2348 } 2349 /* 2350 * if cgroup events exist on this CPU, then we need 2351 * to check if we have to switch in PMU state. 2352 * cgroup event are system-wide mode only 2353 */ 2354 if (atomic_read(&__get_cpu_var(perf_cgroup_events))) 2355 perf_cgroup_sched_in(prev, task); 2356 2357 /* check for system-wide branch_stack events */ 2358 if (atomic_read(&__get_cpu_var(perf_branch_stack_events))) 2359 perf_branch_stack_sched_in(prev, task); 2360 } 2361 2362 static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count) 2363 { 2364 u64 frequency = event->attr.sample_freq; 2365 u64 sec = NSEC_PER_SEC; 2366 u64 divisor, dividend; 2367 2368 int count_fls, nsec_fls, frequency_fls, sec_fls; 2369 2370 count_fls = fls64(count); 2371 nsec_fls = fls64(nsec); 2372 frequency_fls = fls64(frequency); 2373 sec_fls = 30; 2374 2375 /* 2376 * We got @count in @nsec, with a target of sample_freq HZ 2377 * the target period becomes: 2378 * 2379 * @count * 10^9 2380 * period = ------------------- 2381 * @nsec * sample_freq 2382 * 2383 */ 2384 2385 /* 2386 * Reduce accuracy by one bit such that @a and @b converge 2387 * to a similar magnitude. 2388 */ 2389 #define REDUCE_FLS(a, b) \ 2390 do { \ 2391 if (a##_fls > b##_fls) { \ 2392 a >>= 1; \ 2393 a##_fls--; \ 2394 } else { \ 2395 b >>= 1; \ 2396 b##_fls--; \ 2397 } \ 2398 } while (0) 2399 2400 /* 2401 * Reduce accuracy until either term fits in a u64, then proceed with 2402 * the other, so that finally we can do a u64/u64 division. 2403 */ 2404 while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) { 2405 REDUCE_FLS(nsec, frequency); 2406 REDUCE_FLS(sec, count); 2407 } 2408 2409 if (count_fls + sec_fls > 64) { 2410 divisor = nsec * frequency; 2411 2412 while (count_fls + sec_fls > 64) { 2413 REDUCE_FLS(count, sec); 2414 divisor >>= 1; 2415 } 2416 2417 dividend = count * sec; 2418 } else { 2419 dividend = count * sec; 2420 2421 while (nsec_fls + frequency_fls > 64) { 2422 REDUCE_FLS(nsec, frequency); 2423 dividend >>= 1; 2424 } 2425 2426 divisor = nsec * frequency; 2427 } 2428 2429 if (!divisor) 2430 return dividend; 2431 2432 return div64_u64(dividend, divisor); 2433 } 2434 2435 static DEFINE_PER_CPU(int, perf_throttled_count); 2436 static DEFINE_PER_CPU(u64, perf_throttled_seq); 2437 2438 static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable) 2439 { 2440 struct hw_perf_event *hwc = &event->hw; 2441 s64 period, sample_period; 2442 s64 delta; 2443 2444 period = perf_calculate_period(event, nsec, count); 2445 2446 delta = (s64)(period - hwc->sample_period); 2447 delta = (delta + 7) / 8; /* low pass filter */ 2448 2449 sample_period = hwc->sample_period + delta; 2450 2451 if (!sample_period) 2452 sample_period = 1; 2453 2454 hwc->sample_period = sample_period; 2455 2456 if (local64_read(&hwc->period_left) > 8*sample_period) { 2457 if (disable) 2458 event->pmu->stop(event, PERF_EF_UPDATE); 2459 2460 local64_set(&hwc->period_left, 0); 2461 2462 if (disable) 2463 event->pmu->start(event, PERF_EF_RELOAD); 2464 } 2465 } 2466 2467 /* 2468 * combine freq adjustment with unthrottling to avoid two passes over the 2469 * events. At the same time, make sure, having freq events does not change 2470 * the rate of unthrottling as that would introduce bias. 2471 */ 2472 static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx, 2473 int needs_unthr) 2474 { 2475 struct perf_event *event; 2476 struct hw_perf_event *hwc; 2477 u64 now, period = TICK_NSEC; 2478 s64 delta; 2479 2480 /* 2481 * only need to iterate over all events iff: 2482 * - context have events in frequency mode (needs freq adjust) 2483 * - there are events to unthrottle on this cpu 2484 */ 2485 if (!(ctx->nr_freq || needs_unthr)) 2486 return; 2487 2488 raw_spin_lock(&ctx->lock); 2489 perf_pmu_disable(ctx->pmu); 2490 2491 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { 2492 if (event->state != PERF_EVENT_STATE_ACTIVE) 2493 continue; 2494 2495 if (!event_filter_match(event)) 2496 continue; 2497 2498 hwc = &event->hw; 2499 2500 if (needs_unthr && hwc->interrupts == MAX_INTERRUPTS) { 2501 hwc->interrupts = 0; 2502 perf_log_throttle(event, 1); 2503 event->pmu->start(event, 0); 2504 } 2505 2506 if (!event->attr.freq || !event->attr.sample_freq) 2507 continue; 2508 2509 /* 2510 * stop the event and update event->count 2511 */ 2512 event->pmu->stop(event, PERF_EF_UPDATE); 2513 2514 now = local64_read(&event->count); 2515 delta = now - hwc->freq_count_stamp; 2516 hwc->freq_count_stamp = now; 2517 2518 /* 2519 * restart the event 2520 * reload only if value has changed 2521 * we have stopped the event so tell that 2522 * to perf_adjust_period() to avoid stopping it 2523 * twice. 2524 */ 2525 if (delta > 0) 2526 perf_adjust_period(event, period, delta, false); 2527 2528 event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0); 2529 } 2530 2531 perf_pmu_enable(ctx->pmu); 2532 raw_spin_unlock(&ctx->lock); 2533 } 2534 2535 /* 2536 * Round-robin a context's events: 2537 */ 2538 static void rotate_ctx(struct perf_event_context *ctx) 2539 { 2540 /* 2541 * Rotate the first entry last of non-pinned groups. Rotation might be 2542 * disabled by the inheritance code. 2543 */ 2544 if (!ctx->rotate_disable) 2545 list_rotate_left(&ctx->flexible_groups); 2546 } 2547 2548 /* 2549 * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized 2550 * because they're strictly cpu affine and rotate_start is called with IRQs 2551 * disabled, while rotate_context is called from IRQ context. 2552 */ 2553 static void perf_rotate_context(struct perf_cpu_context *cpuctx) 2554 { 2555 struct perf_event_context *ctx = NULL; 2556 int rotate = 0, remove = 1; 2557 2558 if (cpuctx->ctx.nr_events) { 2559 remove = 0; 2560 if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active) 2561 rotate = 1; 2562 } 2563 2564 ctx = cpuctx->task_ctx; 2565 if (ctx && ctx->nr_events) { 2566 remove = 0; 2567 if (ctx->nr_events != ctx->nr_active) 2568 rotate = 1; 2569 } 2570 2571 if (!rotate) 2572 goto done; 2573 2574 perf_ctx_lock(cpuctx, cpuctx->task_ctx); 2575 perf_pmu_disable(cpuctx->ctx.pmu); 2576 2577 cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE); 2578 if (ctx) 2579 ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE); 2580 2581 rotate_ctx(&cpuctx->ctx); 2582 if (ctx) 2583 rotate_ctx(ctx); 2584 2585 perf_event_sched_in(cpuctx, ctx, current); 2586 2587 perf_pmu_enable(cpuctx->ctx.pmu); 2588 perf_ctx_unlock(cpuctx, cpuctx->task_ctx); 2589 done: 2590 if (remove) 2591 list_del_init(&cpuctx->rotation_list); 2592 } 2593 2594 void perf_event_task_tick(void) 2595 { 2596 struct list_head *head = &__get_cpu_var(rotation_list); 2597 struct perf_cpu_context *cpuctx, *tmp; 2598 struct perf_event_context *ctx; 2599 int throttled; 2600 2601 WARN_ON(!irqs_disabled()); 2602 2603 __this_cpu_inc(perf_throttled_seq); 2604 throttled = __this_cpu_xchg(perf_throttled_count, 0); 2605 2606 list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) { 2607 ctx = &cpuctx->ctx; 2608 perf_adjust_freq_unthr_context(ctx, throttled); 2609 2610 ctx = cpuctx->task_ctx; 2611 if (ctx) 2612 perf_adjust_freq_unthr_context(ctx, throttled); 2613 2614 if (cpuctx->jiffies_interval == 1 || 2615 !(jiffies % cpuctx->jiffies_interval)) 2616 perf_rotate_context(cpuctx); 2617 } 2618 } 2619 2620 static int event_enable_on_exec(struct perf_event *event, 2621 struct perf_event_context *ctx) 2622 { 2623 if (!event->attr.enable_on_exec) 2624 return 0; 2625 2626 event->attr.enable_on_exec = 0; 2627 if (event->state >= PERF_EVENT_STATE_INACTIVE) 2628 return 0; 2629 2630 __perf_event_mark_enabled(event); 2631 2632 return 1; 2633 } 2634 2635 /* 2636 * Enable all of a task's events that have been marked enable-on-exec. 2637 * This expects task == current. 2638 */ 2639 static void perf_event_enable_on_exec(struct perf_event_context *ctx) 2640 { 2641 struct perf_event *event; 2642 unsigned long flags; 2643 int enabled = 0; 2644 int ret; 2645 2646 local_irq_save(flags); 2647 if (!ctx || !ctx->nr_events) 2648 goto out; 2649 2650 /* 2651 * We must ctxsw out cgroup events to avoid conflict 2652 * when invoking perf_task_event_sched_in() later on 2653 * in this function. Otherwise we end up trying to 2654 * ctxswin cgroup events which are already scheduled 2655 * in. 2656 */ 2657 perf_cgroup_sched_out(current, NULL); 2658 2659 raw_spin_lock(&ctx->lock); 2660 task_ctx_sched_out(ctx); 2661 2662 list_for_each_entry(event, &ctx->event_list, event_entry) { 2663 ret = event_enable_on_exec(event, ctx); 2664 if (ret) 2665 enabled = 1; 2666 } 2667 2668 /* 2669 * Unclone this context if we enabled any event. 2670 */ 2671 if (enabled) 2672 unclone_ctx(ctx); 2673 2674 raw_spin_unlock(&ctx->lock); 2675 2676 /* 2677 * Also calls ctxswin for cgroup events, if any: 2678 */ 2679 perf_event_context_sched_in(ctx, ctx->task); 2680 out: 2681 local_irq_restore(flags); 2682 } 2683 2684 /* 2685 * Cross CPU call to read the hardware event 2686 */ 2687 static void __perf_event_read(void *info) 2688 { 2689 struct perf_event *event = info; 2690 struct perf_event_context *ctx = event->ctx; 2691 struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); 2692 2693 /* 2694 * If this is a task context, we need to check whether it is 2695 * the current task context of this cpu. If not it has been 2696 * scheduled out before the smp call arrived. In that case 2697 * event->count would have been updated to a recent sample 2698 * when the event was scheduled out. 2699 */ 2700 if (ctx->task && cpuctx->task_ctx != ctx) 2701 return; 2702 2703 raw_spin_lock(&ctx->lock); 2704 if (ctx->is_active) { 2705 update_context_time(ctx); 2706 update_cgrp_time_from_event(event); 2707 } 2708 update_event_times(event); 2709 if (event->state == PERF_EVENT_STATE_ACTIVE) 2710 event->pmu->read(event); 2711 raw_spin_unlock(&ctx->lock); 2712 } 2713 2714 static inline u64 perf_event_count(struct perf_event *event) 2715 { 2716 return local64_read(&event->count) + atomic64_read(&event->child_count); 2717 } 2718 2719 static u64 perf_event_read(struct perf_event *event) 2720 { 2721 /* 2722 * If event is enabled and currently active on a CPU, update the 2723 * value in the event structure: 2724 */ 2725 if (event->state == PERF_EVENT_STATE_ACTIVE) { 2726 smp_call_function_single(event->oncpu, 2727 __perf_event_read, event, 1); 2728 } else if (event->state == PERF_EVENT_STATE_INACTIVE) { 2729 struct perf_event_context *ctx = event->ctx; 2730 unsigned long flags; 2731 2732 raw_spin_lock_irqsave(&ctx->lock, flags); 2733 /* 2734 * may read while context is not active 2735 * (e.g., thread is blocked), in that case 2736 * we cannot update context time 2737 */ 2738 if (ctx->is_active) { 2739 update_context_time(ctx); 2740 update_cgrp_time_from_event(event); 2741 } 2742 update_event_times(event); 2743 raw_spin_unlock_irqrestore(&ctx->lock, flags); 2744 } 2745 2746 return perf_event_count(event); 2747 } 2748 2749 /* 2750 * Initialize the perf_event context in a task_struct: 2751 */ 2752 static void __perf_event_init_context(struct perf_event_context *ctx) 2753 { 2754 raw_spin_lock_init(&ctx->lock); 2755 mutex_init(&ctx->mutex); 2756 INIT_LIST_HEAD(&ctx->pinned_groups); 2757 INIT_LIST_HEAD(&ctx->flexible_groups); 2758 INIT_LIST_HEAD(&ctx->event_list); 2759 atomic_set(&ctx->refcount, 1); 2760 } 2761 2762 static struct perf_event_context * 2763 alloc_perf_context(struct pmu *pmu, struct task_struct *task) 2764 { 2765 struct perf_event_context *ctx; 2766 2767 ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL); 2768 if (!ctx) 2769 return NULL; 2770 2771 __perf_event_init_context(ctx); 2772 if (task) { 2773 ctx->task = task; 2774 get_task_struct(task); 2775 } 2776 ctx->pmu = pmu; 2777 2778 return ctx; 2779 } 2780 2781 static struct task_struct * 2782 find_lively_task_by_vpid(pid_t vpid) 2783 { 2784 struct task_struct *task; 2785 int err; 2786 2787 rcu_read_lock(); 2788 if (!vpid) 2789 task = current; 2790 else 2791 task = find_task_by_vpid(vpid); 2792 if (task) 2793 get_task_struct(task); 2794 rcu_read_unlock(); 2795 2796 if (!task) 2797 return ERR_PTR(-ESRCH); 2798 2799 /* Reuse ptrace permission checks for now. */ 2800 err = -EACCES; 2801 if (!ptrace_may_access(task, PTRACE_MODE_READ)) 2802 goto errout; 2803 2804 return task; 2805 errout: 2806 put_task_struct(task); 2807 return ERR_PTR(err); 2808 2809 } 2810 2811 /* 2812 * Returns a matching context with refcount and pincount. 2813 */ 2814 static struct perf_event_context * 2815 find_get_context(struct pmu *pmu, struct task_struct *task, int cpu) 2816 { 2817 struct perf_event_context *ctx; 2818 struct perf_cpu_context *cpuctx; 2819 unsigned long flags; 2820 int ctxn, err; 2821 2822 if (!task) { 2823 /* Must be root to operate on a CPU event: */ 2824 if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN)) 2825 return ERR_PTR(-EACCES); 2826 2827 /* 2828 * We could be clever and allow to attach a event to an 2829 * offline CPU and activate it when the CPU comes up, but 2830 * that's for later. 2831 */ 2832 if (!cpu_online(cpu)) 2833 return ERR_PTR(-ENODEV); 2834 2835 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); 2836 ctx = &cpuctx->ctx; 2837 get_ctx(ctx); 2838 ++ctx->pin_count; 2839 2840 return ctx; 2841 } 2842 2843 err = -EINVAL; 2844 ctxn = pmu->task_ctx_nr; 2845 if (ctxn < 0) 2846 goto errout; 2847 2848 retry: 2849 ctx = perf_lock_task_context(task, ctxn, &flags); 2850 if (ctx) { 2851 unclone_ctx(ctx); 2852 ++ctx->pin_count; 2853 raw_spin_unlock_irqrestore(&ctx->lock, flags); 2854 } else { 2855 ctx = alloc_perf_context(pmu, task); 2856 err = -ENOMEM; 2857 if (!ctx) 2858 goto errout; 2859 2860 err = 0; 2861 mutex_lock(&task->perf_event_mutex); 2862 /* 2863 * If it has already passed perf_event_exit_task(). 2864 * we must see PF_EXITING, it takes this mutex too. 2865 */ 2866 if (task->flags & PF_EXITING) 2867 err = -ESRCH; 2868 else if (task->perf_event_ctxp[ctxn]) 2869 err = -EAGAIN; 2870 else { 2871 get_ctx(ctx); 2872 ++ctx->pin_count; 2873 rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx); 2874 } 2875 mutex_unlock(&task->perf_event_mutex); 2876 2877 if (unlikely(err)) { 2878 put_ctx(ctx); 2879 2880 if (err == -EAGAIN) 2881 goto retry; 2882 goto errout; 2883 } 2884 } 2885 2886 return ctx; 2887 2888 errout: 2889 return ERR_PTR(err); 2890 } 2891 2892 static void perf_event_free_filter(struct perf_event *event); 2893 2894 static void free_event_rcu(struct rcu_head *head) 2895 { 2896 struct perf_event *event; 2897 2898 event = container_of(head, struct perf_event, rcu_head); 2899 if (event->ns) 2900 put_pid_ns(event->ns); 2901 perf_event_free_filter(event); 2902 kfree(event); 2903 } 2904 2905 static void ring_buffer_put(struct ring_buffer *rb); 2906 2907 static void free_event(struct perf_event *event) 2908 { 2909 irq_work_sync(&event->pending); 2910 2911 if (!event->parent) { 2912 if (event->attach_state & PERF_ATTACH_TASK) 2913 static_key_slow_dec_deferred(&perf_sched_events); 2914 if (event->attr.mmap || event->attr.mmap_data) 2915 atomic_dec(&nr_mmap_events); 2916 if (event->attr.comm) 2917 atomic_dec(&nr_comm_events); 2918 if (event->attr.task) 2919 atomic_dec(&nr_task_events); 2920 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) 2921 put_callchain_buffers(); 2922 if (is_cgroup_event(event)) { 2923 atomic_dec(&per_cpu(perf_cgroup_events, event->cpu)); 2924 static_key_slow_dec_deferred(&perf_sched_events); 2925 } 2926 2927 if (has_branch_stack(event)) { 2928 static_key_slow_dec_deferred(&perf_sched_events); 2929 /* is system-wide event */ 2930 if (!(event->attach_state & PERF_ATTACH_TASK)) 2931 atomic_dec(&per_cpu(perf_branch_stack_events, 2932 event->cpu)); 2933 } 2934 } 2935 2936 if (event->rb) { 2937 ring_buffer_put(event->rb); 2938 event->rb = NULL; 2939 } 2940 2941 if (is_cgroup_event(event)) 2942 perf_detach_cgroup(event); 2943 2944 if (event->destroy) 2945 event->destroy(event); 2946 2947 if (event->ctx) 2948 put_ctx(event->ctx); 2949 2950 call_rcu(&event->rcu_head, free_event_rcu); 2951 } 2952 2953 int perf_event_release_kernel(struct perf_event *event) 2954 { 2955 struct perf_event_context *ctx = event->ctx; 2956 2957 WARN_ON_ONCE(ctx->parent_ctx); 2958 /* 2959 * There are two ways this annotation is useful: 2960 * 2961 * 1) there is a lock recursion from perf_event_exit_task 2962 * see the comment there. 2963 * 2964 * 2) there is a lock-inversion with mmap_sem through 2965 * perf_event_read_group(), which takes faults while 2966 * holding ctx->mutex, however this is called after 2967 * the last filedesc died, so there is no possibility 2968 * to trigger the AB-BA case. 2969 */ 2970 mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING); 2971 raw_spin_lock_irq(&ctx->lock); 2972 perf_group_detach(event); 2973 raw_spin_unlock_irq(&ctx->lock); 2974 perf_remove_from_context(event); 2975 mutex_unlock(&ctx->mutex); 2976 2977 free_event(event); 2978 2979 return 0; 2980 } 2981 EXPORT_SYMBOL_GPL(perf_event_release_kernel); 2982 2983 /* 2984 * Called when the last reference to the file is gone. 2985 */ 2986 static void put_event(struct perf_event *event) 2987 { 2988 struct task_struct *owner; 2989 2990 if (!atomic_long_dec_and_test(&event->refcount)) 2991 return; 2992 2993 rcu_read_lock(); 2994 owner = ACCESS_ONCE(event->owner); 2995 /* 2996 * Matches the smp_wmb() in perf_event_exit_task(). If we observe 2997 * !owner it means the list deletion is complete and we can indeed 2998 * free this event, otherwise we need to serialize on 2999 * owner->perf_event_mutex. 3000 */ 3001 smp_read_barrier_depends(); 3002 if (owner) { 3003 /* 3004 * Since delayed_put_task_struct() also drops the last 3005 * task reference we can safely take a new reference 3006 * while holding the rcu_read_lock(). 3007 */ 3008 get_task_struct(owner); 3009 } 3010 rcu_read_unlock(); 3011 3012 if (owner) { 3013 mutex_lock(&owner->perf_event_mutex); 3014 /* 3015 * We have to re-check the event->owner field, if it is cleared 3016 * we raced with perf_event_exit_task(), acquiring the mutex 3017 * ensured they're done, and we can proceed with freeing the 3018 * event. 3019 */ 3020 if (event->owner) 3021 list_del_init(&event->owner_entry); 3022 mutex_unlock(&owner->perf_event_mutex); 3023 put_task_struct(owner); 3024 } 3025 3026 perf_event_release_kernel(event); 3027 } 3028 3029 static int perf_release(struct inode *inode, struct file *file) 3030 { 3031 put_event(file->private_data); 3032 return 0; 3033 } 3034 3035 u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running) 3036 { 3037 struct perf_event *child; 3038 u64 total = 0; 3039 3040 *enabled = 0; 3041 *running = 0; 3042 3043 mutex_lock(&event->child_mutex); 3044 total += perf_event_read(event); 3045 *enabled += event->total_time_enabled + 3046 atomic64_read(&event->child_total_time_enabled); 3047 *running += event->total_time_running + 3048 atomic64_read(&event->child_total_time_running); 3049 3050 list_for_each_entry(child, &event->child_list, child_list) { 3051 total += perf_event_read(child); 3052 *enabled += child->total_time_enabled; 3053 *running += child->total_time_running; 3054 } 3055 mutex_unlock(&event->child_mutex); 3056 3057 return total; 3058 } 3059 EXPORT_SYMBOL_GPL(perf_event_read_value); 3060 3061 static int perf_event_read_group(struct perf_event *event, 3062 u64 read_format, char __user *buf) 3063 { 3064 struct perf_event *leader = event->group_leader, *sub; 3065 int n = 0, size = 0, ret = -EFAULT; 3066 struct perf_event_context *ctx = leader->ctx; 3067 u64 values[5]; 3068 u64 count, enabled, running; 3069 3070 mutex_lock(&ctx->mutex); 3071 count = perf_event_read_value(leader, &enabled, &running); 3072 3073 values[n++] = 1 + leader->nr_siblings; 3074 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 3075 values[n++] = enabled; 3076 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 3077 values[n++] = running; 3078 values[n++] = count; 3079 if (read_format & PERF_FORMAT_ID) 3080 values[n++] = primary_event_id(leader); 3081 3082 size = n * sizeof(u64); 3083 3084 if (copy_to_user(buf, values, size)) 3085 goto unlock; 3086 3087 ret = size; 3088 3089 list_for_each_entry(sub, &leader->sibling_list, group_entry) { 3090 n = 0; 3091 3092 values[n++] = perf_event_read_value(sub, &enabled, &running); 3093 if (read_format & PERF_FORMAT_ID) 3094 values[n++] = primary_event_id(sub); 3095 3096 size = n * sizeof(u64); 3097 3098 if (copy_to_user(buf + ret, values, size)) { 3099 ret = -EFAULT; 3100 goto unlock; 3101 } 3102 3103 ret += size; 3104 } 3105 unlock: 3106 mutex_unlock(&ctx->mutex); 3107 3108 return ret; 3109 } 3110 3111 static int perf_event_read_one(struct perf_event *event, 3112 u64 read_format, char __user *buf) 3113 { 3114 u64 enabled, running; 3115 u64 values[4]; 3116 int n = 0; 3117 3118 values[n++] = perf_event_read_value(event, &enabled, &running); 3119 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 3120 values[n++] = enabled; 3121 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 3122 values[n++] = running; 3123 if (read_format & PERF_FORMAT_ID) 3124 values[n++] = primary_event_id(event); 3125 3126 if (copy_to_user(buf, values, n * sizeof(u64))) 3127 return -EFAULT; 3128 3129 return n * sizeof(u64); 3130 } 3131 3132 /* 3133 * Read the performance event - simple non blocking version for now 3134 */ 3135 static ssize_t 3136 perf_read_hw(struct perf_event *event, char __user *buf, size_t count) 3137 { 3138 u64 read_format = event->attr.read_format; 3139 int ret; 3140 3141 /* 3142 * Return end-of-file for a read on a event that is in 3143 * error state (i.e. because it was pinned but it couldn't be 3144 * scheduled on to the CPU at some point). 3145 */ 3146 if (event->state == PERF_EVENT_STATE_ERROR) 3147 return 0; 3148 3149 if (count < event->read_size) 3150 return -ENOSPC; 3151 3152 WARN_ON_ONCE(event->ctx->parent_ctx); 3153 if (read_format & PERF_FORMAT_GROUP) 3154 ret = perf_event_read_group(event, read_format, buf); 3155 else 3156 ret = perf_event_read_one(event, read_format, buf); 3157 3158 return ret; 3159 } 3160 3161 static ssize_t 3162 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) 3163 { 3164 struct perf_event *event = file->private_data; 3165 3166 return perf_read_hw(event, buf, count); 3167 } 3168 3169 static unsigned int perf_poll(struct file *file, poll_table *wait) 3170 { 3171 struct perf_event *event = file->private_data; 3172 struct ring_buffer *rb; 3173 unsigned int events = POLL_HUP; 3174 3175 /* 3176 * Race between perf_event_set_output() and perf_poll(): perf_poll() 3177 * grabs the rb reference but perf_event_set_output() overrides it. 3178 * Here is the timeline for two threads T1, T2: 3179 * t0: T1, rb = rcu_dereference(event->rb) 3180 * t1: T2, old_rb = event->rb 3181 * t2: T2, event->rb = new rb 3182 * t3: T2, ring_buffer_detach(old_rb) 3183 * t4: T1, ring_buffer_attach(rb1) 3184 * t5: T1, poll_wait(event->waitq) 3185 * 3186 * To avoid this problem, we grab mmap_mutex in perf_poll() 3187 * thereby ensuring that the assignment of the new ring buffer 3188 * and the detachment of the old buffer appear atomic to perf_poll() 3189 */ 3190 mutex_lock(&event->mmap_mutex); 3191 3192 rcu_read_lock(); 3193 rb = rcu_dereference(event->rb); 3194 if (rb) { 3195 ring_buffer_attach(event, rb); 3196 events = atomic_xchg(&rb->poll, 0); 3197 } 3198 rcu_read_unlock(); 3199 3200 mutex_unlock(&event->mmap_mutex); 3201 3202 poll_wait(file, &event->waitq, wait); 3203 3204 return events; 3205 } 3206 3207 static void perf_event_reset(struct perf_event *event) 3208 { 3209 (void)perf_event_read(event); 3210 local64_set(&event->count, 0); 3211 perf_event_update_userpage(event); 3212 } 3213 3214 /* 3215 * Holding the top-level event's child_mutex means that any 3216 * descendant process that has inherited this event will block 3217 * in sync_child_event if it goes to exit, thus satisfying the 3218 * task existence requirements of perf_event_enable/disable. 3219 */ 3220 static void perf_event_for_each_child(struct perf_event *event, 3221 void (*func)(struct perf_event *)) 3222 { 3223 struct perf_event *child; 3224 3225 WARN_ON_ONCE(event->ctx->parent_ctx); 3226 mutex_lock(&event->child_mutex); 3227 func(event); 3228 list_for_each_entry(child, &event->child_list, child_list) 3229 func(child); 3230 mutex_unlock(&event->child_mutex); 3231 } 3232 3233 static void perf_event_for_each(struct perf_event *event, 3234 void (*func)(struct perf_event *)) 3235 { 3236 struct perf_event_context *ctx = event->ctx; 3237 struct perf_event *sibling; 3238 3239 WARN_ON_ONCE(ctx->parent_ctx); 3240 mutex_lock(&ctx->mutex); 3241 event = event->group_leader; 3242 3243 perf_event_for_each_child(event, func); 3244 list_for_each_entry(sibling, &event->sibling_list, group_entry) 3245 perf_event_for_each_child(sibling, func); 3246 mutex_unlock(&ctx->mutex); 3247 } 3248 3249 static int perf_event_period(struct perf_event *event, u64 __user *arg) 3250 { 3251 struct perf_event_context *ctx = event->ctx; 3252 int ret = 0; 3253 u64 value; 3254 3255 if (!is_sampling_event(event)) 3256 return -EINVAL; 3257 3258 if (copy_from_user(&value, arg, sizeof(value))) 3259 return -EFAULT; 3260 3261 if (!value) 3262 return -EINVAL; 3263 3264 raw_spin_lock_irq(&ctx->lock); 3265 if (event->attr.freq) { 3266 if (value > sysctl_perf_event_sample_rate) { 3267 ret = -EINVAL; 3268 goto unlock; 3269 } 3270 3271 event->attr.sample_freq = value; 3272 } else { 3273 event->attr.sample_period = value; 3274 event->hw.sample_period = value; 3275 } 3276 unlock: 3277 raw_spin_unlock_irq(&ctx->lock); 3278 3279 return ret; 3280 } 3281 3282 static const struct file_operations perf_fops; 3283 3284 static inline int perf_fget_light(int fd, struct fd *p) 3285 { 3286 struct fd f = fdget(fd); 3287 if (!f.file) 3288 return -EBADF; 3289 3290 if (f.file->f_op != &perf_fops) { 3291 fdput(f); 3292 return -EBADF; 3293 } 3294 *p = f; 3295 return 0; 3296 } 3297 3298 static int perf_event_set_output(struct perf_event *event, 3299 struct perf_event *output_event); 3300 static int perf_event_set_filter(struct perf_event *event, void __user *arg); 3301 3302 static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 3303 { 3304 struct perf_event *event = file->private_data; 3305 void (*func)(struct perf_event *); 3306 u32 flags = arg; 3307 3308 switch (cmd) { 3309 case PERF_EVENT_IOC_ENABLE: 3310 func = perf_event_enable; 3311 break; 3312 case PERF_EVENT_IOC_DISABLE: 3313 func = perf_event_disable; 3314 break; 3315 case PERF_EVENT_IOC_RESET: 3316 func = perf_event_reset; 3317 break; 3318 3319 case PERF_EVENT_IOC_REFRESH: 3320 return perf_event_refresh(event, arg); 3321 3322 case PERF_EVENT_IOC_PERIOD: 3323 return perf_event_period(event, (u64 __user *)arg); 3324 3325 case PERF_EVENT_IOC_SET_OUTPUT: 3326 { 3327 int ret; 3328 if (arg != -1) { 3329 struct perf_event *output_event; 3330 struct fd output; 3331 ret = perf_fget_light(arg, &output); 3332 if (ret) 3333 return ret; 3334 output_event = output.file->private_data; 3335 ret = perf_event_set_output(event, output_event); 3336 fdput(output); 3337 } else { 3338 ret = perf_event_set_output(event, NULL); 3339 } 3340 return ret; 3341 } 3342 3343 case PERF_EVENT_IOC_SET_FILTER: 3344 return perf_event_set_filter(event, (void __user *)arg); 3345 3346 default: 3347 return -ENOTTY; 3348 } 3349 3350 if (flags & PERF_IOC_FLAG_GROUP) 3351 perf_event_for_each(event, func); 3352 else 3353 perf_event_for_each_child(event, func); 3354 3355 return 0; 3356 } 3357 3358 int perf_event_task_enable(void) 3359 { 3360 struct perf_event *event; 3361 3362 mutex_lock(¤t->perf_event_mutex); 3363 list_for_each_entry(event, ¤t->perf_event_list, owner_entry) 3364 perf_event_for_each_child(event, perf_event_enable); 3365 mutex_unlock(¤t->perf_event_mutex); 3366 3367 return 0; 3368 } 3369 3370 int perf_event_task_disable(void) 3371 { 3372 struct perf_event *event; 3373 3374 mutex_lock(¤t->perf_event_mutex); 3375 list_for_each_entry(event, ¤t->perf_event_list, owner_entry) 3376 perf_event_for_each_child(event, perf_event_disable); 3377 mutex_unlock(¤t->perf_event_mutex); 3378 3379 return 0; 3380 } 3381 3382 static int perf_event_index(struct perf_event *event) 3383 { 3384 if (event->hw.state & PERF_HES_STOPPED) 3385 return 0; 3386 3387 if (event->state != PERF_EVENT_STATE_ACTIVE) 3388 return 0; 3389 3390 return event->pmu->event_idx(event); 3391 } 3392 3393 static void calc_timer_values(struct perf_event *event, 3394 u64 *now, 3395 u64 *enabled, 3396 u64 *running) 3397 { 3398 u64 ctx_time; 3399 3400 *now = perf_clock(); 3401 ctx_time = event->shadow_ctx_time + *now; 3402 *enabled = ctx_time - event->tstamp_enabled; 3403 *running = ctx_time - event->tstamp_running; 3404 } 3405 3406 void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now) 3407 { 3408 } 3409 3410 /* 3411 * Callers need to ensure there can be no nesting of this function, otherwise 3412 * the seqlock logic goes bad. We can not serialize this because the arch 3413 * code calls this from NMI context. 3414 */ 3415 void perf_event_update_userpage(struct perf_event *event) 3416 { 3417 struct perf_event_mmap_page *userpg; 3418 struct ring_buffer *rb; 3419 u64 enabled, running, now; 3420 3421 rcu_read_lock(); 3422 /* 3423 * compute total_time_enabled, total_time_running 3424 * based on snapshot values taken when the event 3425 * was last scheduled in. 3426 * 3427 * we cannot simply called update_context_time() 3428 * because of locking issue as we can be called in 3429 * NMI context 3430 */ 3431 calc_timer_values(event, &now, &enabled, &running); 3432 rb = rcu_dereference(event->rb); 3433 if (!rb) 3434 goto unlock; 3435 3436 userpg = rb->user_page; 3437 3438 /* 3439 * Disable preemption so as to not let the corresponding user-space 3440 * spin too long if we get preempted. 3441 */ 3442 preempt_disable(); 3443 ++userpg->lock; 3444 barrier(); 3445 userpg->index = perf_event_index(event); 3446 userpg->offset = perf_event_count(event); 3447 if (userpg->index) 3448 userpg->offset -= local64_read(&event->hw.prev_count); 3449 3450 userpg->time_enabled = enabled + 3451 atomic64_read(&event->child_total_time_enabled); 3452 3453 userpg->time_running = running + 3454 atomic64_read(&event->child_total_time_running); 3455 3456 arch_perf_update_userpage(userpg, now); 3457 3458 barrier(); 3459 ++userpg->lock; 3460 preempt_enable(); 3461 unlock: 3462 rcu_read_unlock(); 3463 } 3464 3465 static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 3466 { 3467 struct perf_event *event = vma->vm_file->private_data; 3468 struct ring_buffer *rb; 3469 int ret = VM_FAULT_SIGBUS; 3470 3471 if (vmf->flags & FAULT_FLAG_MKWRITE) { 3472 if (vmf->pgoff == 0) 3473 ret = 0; 3474 return ret; 3475 } 3476 3477 rcu_read_lock(); 3478 rb = rcu_dereference(event->rb); 3479 if (!rb) 3480 goto unlock; 3481 3482 if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE)) 3483 goto unlock; 3484 3485 vmf->page = perf_mmap_to_page(rb, vmf->pgoff); 3486 if (!vmf->page) 3487 goto unlock; 3488 3489 get_page(vmf->page); 3490 vmf->page->mapping = vma->vm_file->f_mapping; 3491 vmf->page->index = vmf->pgoff; 3492 3493 ret = 0; 3494 unlock: 3495 rcu_read_unlock(); 3496 3497 return ret; 3498 } 3499 3500 static void ring_buffer_attach(struct perf_event *event, 3501 struct ring_buffer *rb) 3502 { 3503 unsigned long flags; 3504 3505 if (!list_empty(&event->rb_entry)) 3506 return; 3507 3508 spin_lock_irqsave(&rb->event_lock, flags); 3509 if (!list_empty(&event->rb_entry)) 3510 goto unlock; 3511 3512 list_add(&event->rb_entry, &rb->event_list); 3513 unlock: 3514 spin_unlock_irqrestore(&rb->event_lock, flags); 3515 } 3516 3517 static void ring_buffer_detach(struct perf_event *event, 3518 struct ring_buffer *rb) 3519 { 3520 unsigned long flags; 3521 3522 if (list_empty(&event->rb_entry)) 3523 return; 3524 3525 spin_lock_irqsave(&rb->event_lock, flags); 3526 list_del_init(&event->rb_entry); 3527 wake_up_all(&event->waitq); 3528 spin_unlock_irqrestore(&rb->event_lock, flags); 3529 } 3530 3531 static void ring_buffer_wakeup(struct perf_event *event) 3532 { 3533 struct ring_buffer *rb; 3534 3535 rcu_read_lock(); 3536 rb = rcu_dereference(event->rb); 3537 if (!rb) 3538 goto unlock; 3539 3540 list_for_each_entry_rcu(event, &rb->event_list, rb_entry) 3541 wake_up_all(&event->waitq); 3542 3543 unlock: 3544 rcu_read_unlock(); 3545 } 3546 3547 static void rb_free_rcu(struct rcu_head *rcu_head) 3548 { 3549 struct ring_buffer *rb; 3550 3551 rb = container_of(rcu_head, struct ring_buffer, rcu_head); 3552 rb_free(rb); 3553 } 3554 3555 static struct ring_buffer *ring_buffer_get(struct perf_event *event) 3556 { 3557 struct ring_buffer *rb; 3558 3559 rcu_read_lock(); 3560 rb = rcu_dereference(event->rb); 3561 if (rb) { 3562 if (!atomic_inc_not_zero(&rb->refcount)) 3563 rb = NULL; 3564 } 3565 rcu_read_unlock(); 3566 3567 return rb; 3568 } 3569 3570 static void ring_buffer_put(struct ring_buffer *rb) 3571 { 3572 struct perf_event *event, *n; 3573 unsigned long flags; 3574 3575 if (!atomic_dec_and_test(&rb->refcount)) 3576 return; 3577 3578 spin_lock_irqsave(&rb->event_lock, flags); 3579 list_for_each_entry_safe(event, n, &rb->event_list, rb_entry) { 3580 list_del_init(&event->rb_entry); 3581 wake_up_all(&event->waitq); 3582 } 3583 spin_unlock_irqrestore(&rb->event_lock, flags); 3584 3585 call_rcu(&rb->rcu_head, rb_free_rcu); 3586 } 3587 3588 static void perf_mmap_open(struct vm_area_struct *vma) 3589 { 3590 struct perf_event *event = vma->vm_file->private_data; 3591 3592 atomic_inc(&event->mmap_count); 3593 } 3594 3595 static void perf_mmap_close(struct vm_area_struct *vma) 3596 { 3597 struct perf_event *event = vma->vm_file->private_data; 3598 3599 if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) { 3600 unsigned long size = perf_data_size(event->rb); 3601 struct user_struct *user = event->mmap_user; 3602 struct ring_buffer *rb = event->rb; 3603 3604 atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm); 3605 vma->vm_mm->pinned_vm -= event->mmap_locked; 3606 rcu_assign_pointer(event->rb, NULL); 3607 ring_buffer_detach(event, rb); 3608 mutex_unlock(&event->mmap_mutex); 3609 3610 ring_buffer_put(rb); 3611 free_uid(user); 3612 } 3613 } 3614 3615 static const struct vm_operations_struct perf_mmap_vmops = { 3616 .open = perf_mmap_open, 3617 .close = perf_mmap_close, 3618 .fault = perf_mmap_fault, 3619 .page_mkwrite = perf_mmap_fault, 3620 }; 3621 3622 static int perf_mmap(struct file *file, struct vm_area_struct *vma) 3623 { 3624 struct perf_event *event = file->private_data; 3625 unsigned long user_locked, user_lock_limit; 3626 struct user_struct *user = current_user(); 3627 unsigned long locked, lock_limit; 3628 struct ring_buffer *rb; 3629 unsigned long vma_size; 3630 unsigned long nr_pages; 3631 long user_extra, extra; 3632 int ret = 0, flags = 0; 3633 3634 /* 3635 * Don't allow mmap() of inherited per-task counters. This would 3636 * create a performance issue due to all children writing to the 3637 * same rb. 3638 */ 3639 if (event->cpu == -1 && event->attr.inherit) 3640 return -EINVAL; 3641 3642 if (!(vma->vm_flags & VM_SHARED)) 3643 return -EINVAL; 3644 3645 vma_size = vma->vm_end - vma->vm_start; 3646 nr_pages = (vma_size / PAGE_SIZE) - 1; 3647 3648 /* 3649 * If we have rb pages ensure they're a power-of-two number, so we 3650 * can do bitmasks instead of modulo. 3651 */ 3652 if (nr_pages != 0 && !is_power_of_2(nr_pages)) 3653 return -EINVAL; 3654 3655 if (vma_size != PAGE_SIZE * (1 + nr_pages)) 3656 return -EINVAL; 3657 3658 if (vma->vm_pgoff != 0) 3659 return -EINVAL; 3660 3661 WARN_ON_ONCE(event->ctx->parent_ctx); 3662 mutex_lock(&event->mmap_mutex); 3663 if (event->rb) { 3664 if (event->rb->nr_pages == nr_pages) 3665 atomic_inc(&event->rb->refcount); 3666 else 3667 ret = -EINVAL; 3668 goto unlock; 3669 } 3670 3671 user_extra = nr_pages + 1; 3672 user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10); 3673 3674 /* 3675 * Increase the limit linearly with more CPUs: 3676 */ 3677 user_lock_limit *= num_online_cpus(); 3678 3679 user_locked = atomic_long_read(&user->locked_vm) + user_extra; 3680 3681 extra = 0; 3682 if (user_locked > user_lock_limit) 3683 extra = user_locked - user_lock_limit; 3684 3685 lock_limit = rlimit(RLIMIT_MEMLOCK); 3686 lock_limit >>= PAGE_SHIFT; 3687 locked = vma->vm_mm->pinned_vm + extra; 3688 3689 if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() && 3690 !capable(CAP_IPC_LOCK)) { 3691 ret = -EPERM; 3692 goto unlock; 3693 } 3694 3695 WARN_ON(event->rb); 3696 3697 if (vma->vm_flags & VM_WRITE) 3698 flags |= RING_BUFFER_WRITABLE; 3699 3700 rb = rb_alloc(nr_pages, 3701 event->attr.watermark ? event->attr.wakeup_watermark : 0, 3702 event->cpu, flags); 3703 3704 if (!rb) { 3705 ret = -ENOMEM; 3706 goto unlock; 3707 } 3708 rcu_assign_pointer(event->rb, rb); 3709 3710 atomic_long_add(user_extra, &user->locked_vm); 3711 event->mmap_locked = extra; 3712 event->mmap_user = get_current_user(); 3713 vma->vm_mm->pinned_vm += event->mmap_locked; 3714 3715 perf_event_update_userpage(event); 3716 3717 unlock: 3718 if (!ret) 3719 atomic_inc(&event->mmap_count); 3720 mutex_unlock(&event->mmap_mutex); 3721 3722 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; 3723 vma->vm_ops = &perf_mmap_vmops; 3724 3725 return ret; 3726 } 3727 3728 static int perf_fasync(int fd, struct file *filp, int on) 3729 { 3730 struct inode *inode = file_inode(filp); 3731 struct perf_event *event = filp->private_data; 3732 int retval; 3733 3734 mutex_lock(&inode->i_mutex); 3735 retval = fasync_helper(fd, filp, on, &event->fasync); 3736 mutex_unlock(&inode->i_mutex); 3737 3738 if (retval < 0) 3739 return retval; 3740 3741 return 0; 3742 } 3743 3744 static const struct file_operations perf_fops = { 3745 .llseek = no_llseek, 3746 .release = perf_release, 3747 .read = perf_read, 3748 .poll = perf_poll, 3749 .unlocked_ioctl = perf_ioctl, 3750 .compat_ioctl = perf_ioctl, 3751 .mmap = perf_mmap, 3752 .fasync = perf_fasync, 3753 }; 3754 3755 /* 3756 * Perf event wakeup 3757 * 3758 * If there's data, ensure we set the poll() state and publish everything 3759 * to user-space before waking everybody up. 3760 */ 3761 3762 void perf_event_wakeup(struct perf_event *event) 3763 { 3764 ring_buffer_wakeup(event); 3765 3766 if (event->pending_kill) { 3767 kill_fasync(&event->fasync, SIGIO, event->pending_kill); 3768 event->pending_kill = 0; 3769 } 3770 } 3771 3772 static void perf_pending_event(struct irq_work *entry) 3773 { 3774 struct perf_event *event = container_of(entry, 3775 struct perf_event, pending); 3776 3777 if (event->pending_disable) { 3778 event->pending_disable = 0; 3779 __perf_event_disable(event); 3780 } 3781 3782 if (event->pending_wakeup) { 3783 event->pending_wakeup = 0; 3784 perf_event_wakeup(event); 3785 } 3786 } 3787 3788 /* 3789 * We assume there is only KVM supporting the callbacks. 3790 * Later on, we might change it to a list if there is 3791 * another virtualization implementation supporting the callbacks. 3792 */ 3793 struct perf_guest_info_callbacks *perf_guest_cbs; 3794 3795 int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) 3796 { 3797 perf_guest_cbs = cbs; 3798 return 0; 3799 } 3800 EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks); 3801 3802 int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) 3803 { 3804 perf_guest_cbs = NULL; 3805 return 0; 3806 } 3807 EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks); 3808 3809 static void 3810 perf_output_sample_regs(struct perf_output_handle *handle, 3811 struct pt_regs *regs, u64 mask) 3812 { 3813 int bit; 3814 3815 for_each_set_bit(bit, (const unsigned long *) &mask, 3816 sizeof(mask) * BITS_PER_BYTE) { 3817 u64 val; 3818 3819 val = perf_reg_value(regs, bit); 3820 perf_output_put(handle, val); 3821 } 3822 } 3823 3824 static void perf_sample_regs_user(struct perf_regs_user *regs_user, 3825 struct pt_regs *regs) 3826 { 3827 if (!user_mode(regs)) { 3828 if (current->mm) 3829 regs = task_pt_regs(current); 3830 else 3831 regs = NULL; 3832 } 3833 3834 if (regs) { 3835 regs_user->regs = regs; 3836 regs_user->abi = perf_reg_abi(current); 3837 } 3838 } 3839 3840 /* 3841 * Get remaining task size from user stack pointer. 3842 * 3843 * It'd be better to take stack vma map and limit this more 3844 * precisly, but there's no way to get it safely under interrupt, 3845 * so using TASK_SIZE as limit. 3846 */ 3847 static u64 perf_ustack_task_size(struct pt_regs *regs) 3848 { 3849 unsigned long addr = perf_user_stack_pointer(regs); 3850 3851 if (!addr || addr >= TASK_SIZE) 3852 return 0; 3853 3854 return TASK_SIZE - addr; 3855 } 3856 3857 static u16 3858 perf_sample_ustack_size(u16 stack_size, u16 header_size, 3859 struct pt_regs *regs) 3860 { 3861 u64 task_size; 3862 3863 /* No regs, no stack pointer, no dump. */ 3864 if (!regs) 3865 return 0; 3866 3867 /* 3868 * Check if we fit in with the requested stack size into the: 3869 * - TASK_SIZE 3870 * If we don't, we limit the size to the TASK_SIZE. 3871 * 3872 * - remaining sample size 3873 * If we don't, we customize the stack size to 3874 * fit in to the remaining sample size. 3875 */ 3876 3877 task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs)); 3878 stack_size = min(stack_size, (u16) task_size); 3879 3880 /* Current header size plus static size and dynamic size. */ 3881 header_size += 2 * sizeof(u64); 3882 3883 /* Do we fit in with the current stack dump size? */ 3884 if ((u16) (header_size + stack_size) < header_size) { 3885 /* 3886 * If we overflow the maximum size for the sample, 3887 * we customize the stack dump size to fit in. 3888 */ 3889 stack_size = USHRT_MAX - header_size - sizeof(u64); 3890 stack_size = round_up(stack_size, sizeof(u64)); 3891 } 3892 3893 return stack_size; 3894 } 3895 3896 static void 3897 perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size, 3898 struct pt_regs *regs) 3899 { 3900 /* Case of a kernel thread, nothing to dump */ 3901 if (!regs) { 3902 u64 size = 0; 3903 perf_output_put(handle, size); 3904 } else { 3905 unsigned long sp; 3906 unsigned int rem; 3907 u64 dyn_size; 3908 3909 /* 3910 * We dump: 3911 * static size 3912 * - the size requested by user or the best one we can fit 3913 * in to the sample max size 3914 * data 3915 * - user stack dump data 3916 * dynamic size 3917 * - the actual dumped size 3918 */ 3919 3920 /* Static size. */ 3921 perf_output_put(handle, dump_size); 3922 3923 /* Data. */ 3924 sp = perf_user_stack_pointer(regs); 3925 rem = __output_copy_user(handle, (void *) sp, dump_size); 3926 dyn_size = dump_size - rem; 3927 3928 perf_output_skip(handle, rem); 3929 3930 /* Dynamic size. */ 3931 perf_output_put(handle, dyn_size); 3932 } 3933 } 3934 3935 static void __perf_event_header__init_id(struct perf_event_header *header, 3936 struct perf_sample_data *data, 3937 struct perf_event *event) 3938 { 3939 u64 sample_type = event->attr.sample_type; 3940 3941 data->type = sample_type; 3942 header->size += event->id_header_size; 3943 3944 if (sample_type & PERF_SAMPLE_TID) { 3945 /* namespace issues */ 3946 data->tid_entry.pid = perf_event_pid(event, current); 3947 data->tid_entry.tid = perf_event_tid(event, current); 3948 } 3949 3950 if (sample_type & PERF_SAMPLE_TIME) 3951 data->time = perf_clock(); 3952 3953 if (sample_type & PERF_SAMPLE_ID) 3954 data->id = primary_event_id(event); 3955 3956 if (sample_type & PERF_SAMPLE_STREAM_ID) 3957 data->stream_id = event->id; 3958 3959 if (sample_type & PERF_SAMPLE_CPU) { 3960 data->cpu_entry.cpu = raw_smp_processor_id(); 3961 data->cpu_entry.reserved = 0; 3962 } 3963 } 3964 3965 void perf_event_header__init_id(struct perf_event_header *header, 3966 struct perf_sample_data *data, 3967 struct perf_event *event) 3968 { 3969 if (event->attr.sample_id_all) 3970 __perf_event_header__init_id(header, data, event); 3971 } 3972 3973 static void __perf_event__output_id_sample(struct perf_output_handle *handle, 3974 struct perf_sample_data *data) 3975 { 3976 u64 sample_type = data->type; 3977 3978 if (sample_type & PERF_SAMPLE_TID) 3979 perf_output_put(handle, data->tid_entry); 3980 3981 if (sample_type & PERF_SAMPLE_TIME) 3982 perf_output_put(handle, data->time); 3983 3984 if (sample_type & PERF_SAMPLE_ID) 3985 perf_output_put(handle, data->id); 3986 3987 if (sample_type & PERF_SAMPLE_STREAM_ID) 3988 perf_output_put(handle, data->stream_id); 3989 3990 if (sample_type & PERF_SAMPLE_CPU) 3991 perf_output_put(handle, data->cpu_entry); 3992 } 3993 3994 void perf_event__output_id_sample(struct perf_event *event, 3995 struct perf_output_handle *handle, 3996 struct perf_sample_data *sample) 3997 { 3998 if (event->attr.sample_id_all) 3999 __perf_event__output_id_sample(handle, sample); 4000 } 4001 4002 static void perf_output_read_one(struct perf_output_handle *handle, 4003 struct perf_event *event, 4004 u64 enabled, u64 running) 4005 { 4006 u64 read_format = event->attr.read_format; 4007 u64 values[4]; 4008 int n = 0; 4009 4010 values[n++] = perf_event_count(event); 4011 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) { 4012 values[n++] = enabled + 4013 atomic64_read(&event->child_total_time_enabled); 4014 } 4015 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) { 4016 values[n++] = running + 4017 atomic64_read(&event->child_total_time_running); 4018 } 4019 if (read_format & PERF_FORMAT_ID) 4020 values[n++] = primary_event_id(event); 4021 4022 __output_copy(handle, values, n * sizeof(u64)); 4023 } 4024 4025 /* 4026 * XXX PERF_FORMAT_GROUP vs inherited events seems difficult. 4027 */ 4028 static void perf_output_read_group(struct perf_output_handle *handle, 4029 struct perf_event *event, 4030 u64 enabled, u64 running) 4031 { 4032 struct perf_event *leader = event->group_leader, *sub; 4033 u64 read_format = event->attr.read_format; 4034 u64 values[5]; 4035 int n = 0; 4036 4037 values[n++] = 1 + leader->nr_siblings; 4038 4039 if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) 4040 values[n++] = enabled; 4041 4042 if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) 4043 values[n++] = running; 4044 4045 if (leader != event) 4046 leader->pmu->read(leader); 4047 4048 values[n++] = perf_event_count(leader); 4049 if (read_format & PERF_FORMAT_ID) 4050 values[n++] = primary_event_id(leader); 4051 4052 __output_copy(handle, values, n * sizeof(u64)); 4053 4054 list_for_each_entry(sub, &leader->sibling_list, group_entry) { 4055 n = 0; 4056 4057 if (sub != event) 4058 sub->pmu->read(sub); 4059 4060 values[n++] = perf_event_count(sub); 4061 if (read_format & PERF_FORMAT_ID) 4062 values[n++] = primary_event_id(sub); 4063 4064 __output_copy(handle, values, n * sizeof(u64)); 4065 } 4066 } 4067 4068 #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\ 4069 PERF_FORMAT_TOTAL_TIME_RUNNING) 4070 4071 static void perf_output_read(struct perf_output_handle *handle, 4072 struct perf_event *event) 4073 { 4074 u64 enabled = 0, running = 0, now; 4075 u64 read_format = event->attr.read_format; 4076 4077 /* 4078 * compute total_time_enabled, total_time_running 4079 * based on snapshot values taken when the event 4080 * was last scheduled in. 4081 * 4082 * we cannot simply called update_context_time() 4083 * because of locking issue as we are called in 4084 * NMI context 4085 */ 4086 if (read_format & PERF_FORMAT_TOTAL_TIMES) 4087 calc_timer_values(event, &now, &enabled, &running); 4088 4089 if (event->attr.read_format & PERF_FORMAT_GROUP) 4090 perf_output_read_group(handle, event, enabled, running); 4091 else 4092 perf_output_read_one(handle, event, enabled, running); 4093 } 4094 4095 void perf_output_sample(struct perf_output_handle *handle, 4096 struct perf_event_header *header, 4097 struct perf_sample_data *data, 4098 struct perf_event *event) 4099 { 4100 u64 sample_type = data->type; 4101 4102 perf_output_put(handle, *header); 4103 4104 if (sample_type & PERF_SAMPLE_IP) 4105 perf_output_put(handle, data->ip); 4106 4107 if (sample_type & PERF_SAMPLE_TID) 4108 perf_output_put(handle, data->tid_entry); 4109 4110 if (sample_type & PERF_SAMPLE_TIME) 4111 perf_output_put(handle, data->time); 4112 4113 if (sample_type & PERF_SAMPLE_ADDR) 4114 perf_output_put(handle, data->addr); 4115 4116 if (sample_type & PERF_SAMPLE_ID) 4117 perf_output_put(handle, data->id); 4118 4119 if (sample_type & PERF_SAMPLE_STREAM_ID) 4120 perf_output_put(handle, data->stream_id); 4121 4122 if (sample_type & PERF_SAMPLE_CPU) 4123 perf_output_put(handle, data->cpu_entry); 4124 4125 if (sample_type & PERF_SAMPLE_PERIOD) 4126 perf_output_put(handle, data->period); 4127 4128 if (sample_type & PERF_SAMPLE_READ) 4129 perf_output_read(handle, event); 4130 4131 if (sample_type & PERF_SAMPLE_CALLCHAIN) { 4132 if (data->callchain) { 4133 int size = 1; 4134 4135 if (data->callchain) 4136 size += data->callchain->nr; 4137 4138 size *= sizeof(u64); 4139 4140 __output_copy(handle, data->callchain, size); 4141 } else { 4142 u64 nr = 0; 4143 perf_output_put(handle, nr); 4144 } 4145 } 4146 4147 if (sample_type & PERF_SAMPLE_RAW) { 4148 if (data->raw) { 4149 perf_output_put(handle, data->raw->size); 4150 __output_copy(handle, data->raw->data, 4151 data->raw->size); 4152 } else { 4153 struct { 4154 u32 size; 4155 u32 data; 4156 } raw = { 4157 .size = sizeof(u32), 4158 .data = 0, 4159 }; 4160 perf_output_put(handle, raw); 4161 } 4162 } 4163 4164 if (!event->attr.watermark) { 4165 int wakeup_events = event->attr.wakeup_events; 4166 4167 if (wakeup_events) { 4168 struct ring_buffer *rb = handle->rb; 4169 int events = local_inc_return(&rb->events); 4170 4171 if (events >= wakeup_events) { 4172 local_sub(wakeup_events, &rb->events); 4173 local_inc(&rb->wakeup); 4174 } 4175 } 4176 } 4177 4178 if (sample_type & PERF_SAMPLE_BRANCH_STACK) { 4179 if (data->br_stack) { 4180 size_t size; 4181 4182 size = data->br_stack->nr 4183 * sizeof(struct perf_branch_entry); 4184 4185 perf_output_put(handle, data->br_stack->nr); 4186 perf_output_copy(handle, data->br_stack->entries, size); 4187 } else { 4188 /* 4189 * we always store at least the value of nr 4190 */ 4191 u64 nr = 0; 4192 perf_output_put(handle, nr); 4193 } 4194 } 4195 4196 if (sample_type & PERF_SAMPLE_REGS_USER) { 4197 u64 abi = data->regs_user.abi; 4198 4199 /* 4200 * If there are no regs to dump, notice it through 4201 * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE). 4202 */ 4203 perf_output_put(handle, abi); 4204 4205 if (abi) { 4206 u64 mask = event->attr.sample_regs_user; 4207 perf_output_sample_regs(handle, 4208 data->regs_user.regs, 4209 mask); 4210 } 4211 } 4212 4213 if (sample_type & PERF_SAMPLE_STACK_USER) 4214 perf_output_sample_ustack(handle, 4215 data->stack_user_size, 4216 data->regs_user.regs); 4217 4218 if (sample_type & PERF_SAMPLE_WEIGHT) 4219 perf_output_put(handle, data->weight); 4220 4221 if (sample_type & PERF_SAMPLE_DATA_SRC) 4222 perf_output_put(handle, data->data_src.val); 4223 } 4224 4225 void perf_prepare_sample(struct perf_event_header *header, 4226 struct perf_sample_data *data, 4227 struct perf_event *event, 4228 struct pt_regs *regs) 4229 { 4230 u64 sample_type = event->attr.sample_type; 4231 4232 header->type = PERF_RECORD_SAMPLE; 4233 header->size = sizeof(*header) + event->header_size; 4234 4235 header->misc = 0; 4236 header->misc |= perf_misc_flags(regs); 4237 4238 __perf_event_header__init_id(header, data, event); 4239 4240 if (sample_type & PERF_SAMPLE_IP) 4241 data->ip = perf_instruction_pointer(regs); 4242 4243 if (sample_type & PERF_SAMPLE_CALLCHAIN) { 4244 int size = 1; 4245 4246 data->callchain = perf_callchain(event, regs); 4247 4248 if (data->callchain) 4249 size += data->callchain->nr; 4250 4251 header->size += size * sizeof(u64); 4252 } 4253 4254 if (sample_type & PERF_SAMPLE_RAW) { 4255 int size = sizeof(u32); 4256 4257 if (data->raw) 4258 size += data->raw->size; 4259 else 4260 size += sizeof(u32); 4261 4262 WARN_ON_ONCE(size & (sizeof(u64)-1)); 4263 header->size += size; 4264 } 4265 4266 if (sample_type & PERF_SAMPLE_BRANCH_STACK) { 4267 int size = sizeof(u64); /* nr */ 4268 if (data->br_stack) { 4269 size += data->br_stack->nr 4270 * sizeof(struct perf_branch_entry); 4271 } 4272 header->size += size; 4273 } 4274 4275 if (sample_type & PERF_SAMPLE_REGS_USER) { 4276 /* regs dump ABI info */ 4277 int size = sizeof(u64); 4278 4279 perf_sample_regs_user(&data->regs_user, regs); 4280 4281 if (data->regs_user.regs) { 4282 u64 mask = event->attr.sample_regs_user; 4283 size += hweight64(mask) * sizeof(u64); 4284 } 4285 4286 header->size += size; 4287 } 4288 4289 if (sample_type & PERF_SAMPLE_STACK_USER) { 4290 /* 4291 * Either we need PERF_SAMPLE_STACK_USER bit to be allways 4292 * processed as the last one or have additional check added 4293 * in case new sample type is added, because we could eat 4294 * up the rest of the sample size. 4295 */ 4296 struct perf_regs_user *uregs = &data->regs_user; 4297 u16 stack_size = event->attr.sample_stack_user; 4298 u16 size = sizeof(u64); 4299 4300 if (!uregs->abi) 4301 perf_sample_regs_user(uregs, regs); 4302 4303 stack_size = perf_sample_ustack_size(stack_size, header->size, 4304 uregs->regs); 4305 4306 /* 4307 * If there is something to dump, add space for the dump 4308 * itself and for the field that tells the dynamic size, 4309 * which is how many have been actually dumped. 4310 */ 4311 if (stack_size) 4312 size += sizeof(u64) + stack_size; 4313 4314 data->stack_user_size = stack_size; 4315 header->size += size; 4316 } 4317 } 4318 4319 static void perf_event_output(struct perf_event *event, 4320 struct perf_sample_data *data, 4321 struct pt_regs *regs) 4322 { 4323 struct perf_output_handle handle; 4324 struct perf_event_header header; 4325 4326 /* protect the callchain buffers */ 4327 rcu_read_lock(); 4328 4329 perf_prepare_sample(&header, data, event, regs); 4330 4331 if (perf_output_begin(&handle, event, header.size)) 4332 goto exit; 4333 4334 perf_output_sample(&handle, &header, data, event); 4335 4336 perf_output_end(&handle); 4337 4338 exit: 4339 rcu_read_unlock(); 4340 } 4341 4342 /* 4343 * read event_id 4344 */ 4345 4346 struct perf_read_event { 4347 struct perf_event_header header; 4348 4349 u32 pid; 4350 u32 tid; 4351 }; 4352 4353 static void 4354 perf_event_read_event(struct perf_event *event, 4355 struct task_struct *task) 4356 { 4357 struct perf_output_handle handle; 4358 struct perf_sample_data sample; 4359 struct perf_read_event read_event = { 4360 .header = { 4361 .type = PERF_RECORD_READ, 4362 .misc = 0, 4363 .size = sizeof(read_event) + event->read_size, 4364 }, 4365 .pid = perf_event_pid(event, task), 4366 .tid = perf_event_tid(event, task), 4367 }; 4368 int ret; 4369 4370 perf_event_header__init_id(&read_event.header, &sample, event); 4371 ret = perf_output_begin(&handle, event, read_event.header.size); 4372 if (ret) 4373 return; 4374 4375 perf_output_put(&handle, read_event); 4376 perf_output_read(&handle, event); 4377 perf_event__output_id_sample(event, &handle, &sample); 4378 4379 perf_output_end(&handle); 4380 } 4381 4382 /* 4383 * task tracking -- fork/exit 4384 * 4385 * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task 4386 */ 4387 4388 struct perf_task_event { 4389 struct task_struct *task; 4390 struct perf_event_context *task_ctx; 4391 4392 struct { 4393 struct perf_event_header header; 4394 4395 u32 pid; 4396 u32 ppid; 4397 u32 tid; 4398 u32 ptid; 4399 u64 time; 4400 } event_id; 4401 }; 4402 4403 static void perf_event_task_output(struct perf_event *event, 4404 struct perf_task_event *task_event) 4405 { 4406 struct perf_output_handle handle; 4407 struct perf_sample_data sample; 4408 struct task_struct *task = task_event->task; 4409 int ret, size = task_event->event_id.header.size; 4410 4411 perf_event_header__init_id(&task_event->event_id.header, &sample, event); 4412 4413 ret = perf_output_begin(&handle, event, 4414 task_event->event_id.header.size); 4415 if (ret) 4416 goto out; 4417 4418 task_event->event_id.pid = perf_event_pid(event, task); 4419 task_event->event_id.ppid = perf_event_pid(event, current); 4420 4421 task_event->event_id.tid = perf_event_tid(event, task); 4422 task_event->event_id.ptid = perf_event_tid(event, current); 4423 4424 perf_output_put(&handle, task_event->event_id); 4425 4426 perf_event__output_id_sample(event, &handle, &sample); 4427 4428 perf_output_end(&handle); 4429 out: 4430 task_event->event_id.header.size = size; 4431 } 4432 4433 static int perf_event_task_match(struct perf_event *event) 4434 { 4435 if (event->state < PERF_EVENT_STATE_INACTIVE) 4436 return 0; 4437 4438 if (!event_filter_match(event)) 4439 return 0; 4440 4441 if (event->attr.comm || event->attr.mmap || 4442 event->attr.mmap_data || event->attr.task) 4443 return 1; 4444 4445 return 0; 4446 } 4447 4448 static void perf_event_task_ctx(struct perf_event_context *ctx, 4449 struct perf_task_event *task_event) 4450 { 4451 struct perf_event *event; 4452 4453 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { 4454 if (perf_event_task_match(event)) 4455 perf_event_task_output(event, task_event); 4456 } 4457 } 4458 4459 static void perf_event_task_event(struct perf_task_event *task_event) 4460 { 4461 struct perf_cpu_context *cpuctx; 4462 struct perf_event_context *ctx; 4463 struct pmu *pmu; 4464 int ctxn; 4465 4466 rcu_read_lock(); 4467 list_for_each_entry_rcu(pmu, &pmus, entry) { 4468 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); 4469 if (cpuctx->unique_pmu != pmu) 4470 goto next; 4471 perf_event_task_ctx(&cpuctx->ctx, task_event); 4472 4473 ctx = task_event->task_ctx; 4474 if (!ctx) { 4475 ctxn = pmu->task_ctx_nr; 4476 if (ctxn < 0) 4477 goto next; 4478 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); 4479 if (ctx) 4480 perf_event_task_ctx(ctx, task_event); 4481 } 4482 next: 4483 put_cpu_ptr(pmu->pmu_cpu_context); 4484 } 4485 if (task_event->task_ctx) 4486 perf_event_task_ctx(task_event->task_ctx, task_event); 4487 4488 rcu_read_unlock(); 4489 } 4490 4491 static void perf_event_task(struct task_struct *task, 4492 struct perf_event_context *task_ctx, 4493 int new) 4494 { 4495 struct perf_task_event task_event; 4496 4497 if (!atomic_read(&nr_comm_events) && 4498 !atomic_read(&nr_mmap_events) && 4499 !atomic_read(&nr_task_events)) 4500 return; 4501 4502 task_event = (struct perf_task_event){ 4503 .task = task, 4504 .task_ctx = task_ctx, 4505 .event_id = { 4506 .header = { 4507 .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT, 4508 .misc = 0, 4509 .size = sizeof(task_event.event_id), 4510 }, 4511 /* .pid */ 4512 /* .ppid */ 4513 /* .tid */ 4514 /* .ptid */ 4515 .time = perf_clock(), 4516 }, 4517 }; 4518 4519 perf_event_task_event(&task_event); 4520 } 4521 4522 void perf_event_fork(struct task_struct *task) 4523 { 4524 perf_event_task(task, NULL, 1); 4525 } 4526 4527 /* 4528 * comm tracking 4529 */ 4530 4531 struct perf_comm_event { 4532 struct task_struct *task; 4533 char *comm; 4534 int comm_size; 4535 4536 struct { 4537 struct perf_event_header header; 4538 4539 u32 pid; 4540 u32 tid; 4541 } event_id; 4542 }; 4543 4544 static void perf_event_comm_output(struct perf_event *event, 4545 struct perf_comm_event *comm_event) 4546 { 4547 struct perf_output_handle handle; 4548 struct perf_sample_data sample; 4549 int size = comm_event->event_id.header.size; 4550 int ret; 4551 4552 perf_event_header__init_id(&comm_event->event_id.header, &sample, event); 4553 ret = perf_output_begin(&handle, event, 4554 comm_event->event_id.header.size); 4555 4556 if (ret) 4557 goto out; 4558 4559 comm_event->event_id.pid = perf_event_pid(event, comm_event->task); 4560 comm_event->event_id.tid = perf_event_tid(event, comm_event->task); 4561 4562 perf_output_put(&handle, comm_event->event_id); 4563 __output_copy(&handle, comm_event->comm, 4564 comm_event->comm_size); 4565 4566 perf_event__output_id_sample(event, &handle, &sample); 4567 4568 perf_output_end(&handle); 4569 out: 4570 comm_event->event_id.header.size = size; 4571 } 4572 4573 static int perf_event_comm_match(struct perf_event *event) 4574 { 4575 if (event->state < PERF_EVENT_STATE_INACTIVE) 4576 return 0; 4577 4578 if (!event_filter_match(event)) 4579 return 0; 4580 4581 if (event->attr.comm) 4582 return 1; 4583 4584 return 0; 4585 } 4586 4587 static void perf_event_comm_ctx(struct perf_event_context *ctx, 4588 struct perf_comm_event *comm_event) 4589 { 4590 struct perf_event *event; 4591 4592 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { 4593 if (perf_event_comm_match(event)) 4594 perf_event_comm_output(event, comm_event); 4595 } 4596 } 4597 4598 static void perf_event_comm_event(struct perf_comm_event *comm_event) 4599 { 4600 struct perf_cpu_context *cpuctx; 4601 struct perf_event_context *ctx; 4602 char comm[TASK_COMM_LEN]; 4603 unsigned int size; 4604 struct pmu *pmu; 4605 int ctxn; 4606 4607 memset(comm, 0, sizeof(comm)); 4608 strlcpy(comm, comm_event->task->comm, sizeof(comm)); 4609 size = ALIGN(strlen(comm)+1, sizeof(u64)); 4610 4611 comm_event->comm = comm; 4612 comm_event->comm_size = size; 4613 4614 comm_event->event_id.header.size = sizeof(comm_event->event_id) + size; 4615 rcu_read_lock(); 4616 list_for_each_entry_rcu(pmu, &pmus, entry) { 4617 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); 4618 if (cpuctx->unique_pmu != pmu) 4619 goto next; 4620 perf_event_comm_ctx(&cpuctx->ctx, comm_event); 4621 4622 ctxn = pmu->task_ctx_nr; 4623 if (ctxn < 0) 4624 goto next; 4625 4626 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); 4627 if (ctx) 4628 perf_event_comm_ctx(ctx, comm_event); 4629 next: 4630 put_cpu_ptr(pmu->pmu_cpu_context); 4631 } 4632 rcu_read_unlock(); 4633 } 4634 4635 void perf_event_comm(struct task_struct *task) 4636 { 4637 struct perf_comm_event comm_event; 4638 struct perf_event_context *ctx; 4639 int ctxn; 4640 4641 rcu_read_lock(); 4642 for_each_task_context_nr(ctxn) { 4643 ctx = task->perf_event_ctxp[ctxn]; 4644 if (!ctx) 4645 continue; 4646 4647 perf_event_enable_on_exec(ctx); 4648 } 4649 rcu_read_unlock(); 4650 4651 if (!atomic_read(&nr_comm_events)) 4652 return; 4653 4654 comm_event = (struct perf_comm_event){ 4655 .task = task, 4656 /* .comm */ 4657 /* .comm_size */ 4658 .event_id = { 4659 .header = { 4660 .type = PERF_RECORD_COMM, 4661 .misc = 0, 4662 /* .size */ 4663 }, 4664 /* .pid */ 4665 /* .tid */ 4666 }, 4667 }; 4668 4669 perf_event_comm_event(&comm_event); 4670 } 4671 4672 /* 4673 * mmap tracking 4674 */ 4675 4676 struct perf_mmap_event { 4677 struct vm_area_struct *vma; 4678 4679 const char *file_name; 4680 int file_size; 4681 4682 struct { 4683 struct perf_event_header header; 4684 4685 u32 pid; 4686 u32 tid; 4687 u64 start; 4688 u64 len; 4689 u64 pgoff; 4690 } event_id; 4691 }; 4692 4693 static void perf_event_mmap_output(struct perf_event *event, 4694 struct perf_mmap_event *mmap_event) 4695 { 4696 struct perf_output_handle handle; 4697 struct perf_sample_data sample; 4698 int size = mmap_event->event_id.header.size; 4699 int ret; 4700 4701 perf_event_header__init_id(&mmap_event->event_id.header, &sample, event); 4702 ret = perf_output_begin(&handle, event, 4703 mmap_event->event_id.header.size); 4704 if (ret) 4705 goto out; 4706 4707 mmap_event->event_id.pid = perf_event_pid(event, current); 4708 mmap_event->event_id.tid = perf_event_tid(event, current); 4709 4710 perf_output_put(&handle, mmap_event->event_id); 4711 __output_copy(&handle, mmap_event->file_name, 4712 mmap_event->file_size); 4713 4714 perf_event__output_id_sample(event, &handle, &sample); 4715 4716 perf_output_end(&handle); 4717 out: 4718 mmap_event->event_id.header.size = size; 4719 } 4720 4721 static int perf_event_mmap_match(struct perf_event *event, 4722 struct perf_mmap_event *mmap_event, 4723 int executable) 4724 { 4725 if (event->state < PERF_EVENT_STATE_INACTIVE) 4726 return 0; 4727 4728 if (!event_filter_match(event)) 4729 return 0; 4730 4731 if ((!executable && event->attr.mmap_data) || 4732 (executable && event->attr.mmap)) 4733 return 1; 4734 4735 return 0; 4736 } 4737 4738 static void perf_event_mmap_ctx(struct perf_event_context *ctx, 4739 struct perf_mmap_event *mmap_event, 4740 int executable) 4741 { 4742 struct perf_event *event; 4743 4744 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { 4745 if (perf_event_mmap_match(event, mmap_event, executable)) 4746 perf_event_mmap_output(event, mmap_event); 4747 } 4748 } 4749 4750 static void perf_event_mmap_event(struct perf_mmap_event *mmap_event) 4751 { 4752 struct perf_cpu_context *cpuctx; 4753 struct perf_event_context *ctx; 4754 struct vm_area_struct *vma = mmap_event->vma; 4755 struct file *file = vma->vm_file; 4756 unsigned int size; 4757 char tmp[16]; 4758 char *buf = NULL; 4759 const char *name; 4760 struct pmu *pmu; 4761 int ctxn; 4762 4763 memset(tmp, 0, sizeof(tmp)); 4764 4765 if (file) { 4766 /* 4767 * d_path works from the end of the rb backwards, so we 4768 * need to add enough zero bytes after the string to handle 4769 * the 64bit alignment we do later. 4770 */ 4771 buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL); 4772 if (!buf) { 4773 name = strncpy(tmp, "//enomem", sizeof(tmp)); 4774 goto got_name; 4775 } 4776 name = d_path(&file->f_path, buf, PATH_MAX); 4777 if (IS_ERR(name)) { 4778 name = strncpy(tmp, "//toolong", sizeof(tmp)); 4779 goto got_name; 4780 } 4781 } else { 4782 if (arch_vma_name(mmap_event->vma)) { 4783 name = strncpy(tmp, arch_vma_name(mmap_event->vma), 4784 sizeof(tmp) - 1); 4785 tmp[sizeof(tmp) - 1] = '\0'; 4786 goto got_name; 4787 } 4788 4789 if (!vma->vm_mm) { 4790 name = strncpy(tmp, "[vdso]", sizeof(tmp)); 4791 goto got_name; 4792 } else if (vma->vm_start <= vma->vm_mm->start_brk && 4793 vma->vm_end >= vma->vm_mm->brk) { 4794 name = strncpy(tmp, "[heap]", sizeof(tmp)); 4795 goto got_name; 4796 } else if (vma->vm_start <= vma->vm_mm->start_stack && 4797 vma->vm_end >= vma->vm_mm->start_stack) { 4798 name = strncpy(tmp, "[stack]", sizeof(tmp)); 4799 goto got_name; 4800 } 4801 4802 name = strncpy(tmp, "//anon", sizeof(tmp)); 4803 goto got_name; 4804 } 4805 4806 got_name: 4807 size = ALIGN(strlen(name)+1, sizeof(u64)); 4808 4809 mmap_event->file_name = name; 4810 mmap_event->file_size = size; 4811 4812 if (!(vma->vm_flags & VM_EXEC)) 4813 mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA; 4814 4815 mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size; 4816 4817 rcu_read_lock(); 4818 list_for_each_entry_rcu(pmu, &pmus, entry) { 4819 cpuctx = get_cpu_ptr(pmu->pmu_cpu_context); 4820 if (cpuctx->unique_pmu != pmu) 4821 goto next; 4822 perf_event_mmap_ctx(&cpuctx->ctx, mmap_event, 4823 vma->vm_flags & VM_EXEC); 4824 4825 ctxn = pmu->task_ctx_nr; 4826 if (ctxn < 0) 4827 goto next; 4828 4829 ctx = rcu_dereference(current->perf_event_ctxp[ctxn]); 4830 if (ctx) { 4831 perf_event_mmap_ctx(ctx, mmap_event, 4832 vma->vm_flags & VM_EXEC); 4833 } 4834 next: 4835 put_cpu_ptr(pmu->pmu_cpu_context); 4836 } 4837 rcu_read_unlock(); 4838 4839 kfree(buf); 4840 } 4841 4842 void perf_event_mmap(struct vm_area_struct *vma) 4843 { 4844 struct perf_mmap_event mmap_event; 4845 4846 if (!atomic_read(&nr_mmap_events)) 4847 return; 4848 4849 mmap_event = (struct perf_mmap_event){ 4850 .vma = vma, 4851 /* .file_name */ 4852 /* .file_size */ 4853 .event_id = { 4854 .header = { 4855 .type = PERF_RECORD_MMAP, 4856 .misc = PERF_RECORD_MISC_USER, 4857 /* .size */ 4858 }, 4859 /* .pid */ 4860 /* .tid */ 4861 .start = vma->vm_start, 4862 .len = vma->vm_end - vma->vm_start, 4863 .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT, 4864 }, 4865 }; 4866 4867 perf_event_mmap_event(&mmap_event); 4868 } 4869 4870 /* 4871 * IRQ throttle logging 4872 */ 4873 4874 static void perf_log_throttle(struct perf_event *event, int enable) 4875 { 4876 struct perf_output_handle handle; 4877 struct perf_sample_data sample; 4878 int ret; 4879 4880 struct { 4881 struct perf_event_header header; 4882 u64 time; 4883 u64 id; 4884 u64 stream_id; 4885 } throttle_event = { 4886 .header = { 4887 .type = PERF_RECORD_THROTTLE, 4888 .misc = 0, 4889 .size = sizeof(throttle_event), 4890 }, 4891 .time = perf_clock(), 4892 .id = primary_event_id(event), 4893 .stream_id = event->id, 4894 }; 4895 4896 if (enable) 4897 throttle_event.header.type = PERF_RECORD_UNTHROTTLE; 4898 4899 perf_event_header__init_id(&throttle_event.header, &sample, event); 4900 4901 ret = perf_output_begin(&handle, event, 4902 throttle_event.header.size); 4903 if (ret) 4904 return; 4905 4906 perf_output_put(&handle, throttle_event); 4907 perf_event__output_id_sample(event, &handle, &sample); 4908 perf_output_end(&handle); 4909 } 4910 4911 /* 4912 * Generic event overflow handling, sampling. 4913 */ 4914 4915 static int __perf_event_overflow(struct perf_event *event, 4916 int throttle, struct perf_sample_data *data, 4917 struct pt_regs *regs) 4918 { 4919 int events = atomic_read(&event->event_limit); 4920 struct hw_perf_event *hwc = &event->hw; 4921 u64 seq; 4922 int ret = 0; 4923 4924 /* 4925 * Non-sampling counters might still use the PMI to fold short 4926 * hardware counters, ignore those. 4927 */ 4928 if (unlikely(!is_sampling_event(event))) 4929 return 0; 4930 4931 seq = __this_cpu_read(perf_throttled_seq); 4932 if (seq != hwc->interrupts_seq) { 4933 hwc->interrupts_seq = seq; 4934 hwc->interrupts = 1; 4935 } else { 4936 hwc->interrupts++; 4937 if (unlikely(throttle 4938 && hwc->interrupts >= max_samples_per_tick)) { 4939 __this_cpu_inc(perf_throttled_count); 4940 hwc->interrupts = MAX_INTERRUPTS; 4941 perf_log_throttle(event, 0); 4942 ret = 1; 4943 } 4944 } 4945 4946 if (event->attr.freq) { 4947 u64 now = perf_clock(); 4948 s64 delta = now - hwc->freq_time_stamp; 4949 4950 hwc->freq_time_stamp = now; 4951 4952 if (delta > 0 && delta < 2*TICK_NSEC) 4953 perf_adjust_period(event, delta, hwc->last_period, true); 4954 } 4955 4956 /* 4957 * XXX event_limit might not quite work as expected on inherited 4958 * events 4959 */ 4960 4961 event->pending_kill = POLL_IN; 4962 if (events && atomic_dec_and_test(&event->event_limit)) { 4963 ret = 1; 4964 event->pending_kill = POLL_HUP; 4965 event->pending_disable = 1; 4966 irq_work_queue(&event->pending); 4967 } 4968 4969 if (event->overflow_handler) 4970 event->overflow_handler(event, data, regs); 4971 else 4972 perf_event_output(event, data, regs); 4973 4974 if (event->fasync && event->pending_kill) { 4975 event->pending_wakeup = 1; 4976 irq_work_queue(&event->pending); 4977 } 4978 4979 return ret; 4980 } 4981 4982 int perf_event_overflow(struct perf_event *event, 4983 struct perf_sample_data *data, 4984 struct pt_regs *regs) 4985 { 4986 return __perf_event_overflow(event, 1, data, regs); 4987 } 4988 4989 /* 4990 * Generic software event infrastructure 4991 */ 4992 4993 struct swevent_htable { 4994 struct swevent_hlist *swevent_hlist; 4995 struct mutex hlist_mutex; 4996 int hlist_refcount; 4997 4998 /* Recursion avoidance in each contexts */ 4999 int recursion[PERF_NR_CONTEXTS]; 5000 }; 5001 5002 static DEFINE_PER_CPU(struct swevent_htable, swevent_htable); 5003 5004 /* 5005 * We directly increment event->count and keep a second value in 5006 * event->hw.period_left to count intervals. This period event 5007 * is kept in the range [-sample_period, 0] so that we can use the 5008 * sign as trigger. 5009 */ 5010 5011 static u64 perf_swevent_set_period(struct perf_event *event) 5012 { 5013 struct hw_perf_event *hwc = &event->hw; 5014 u64 period = hwc->last_period; 5015 u64 nr, offset; 5016 s64 old, val; 5017 5018 hwc->last_period = hwc->sample_period; 5019 5020 again: 5021 old = val = local64_read(&hwc->period_left); 5022 if (val < 0) 5023 return 0; 5024 5025 nr = div64_u64(period + val, period); 5026 offset = nr * period; 5027 val -= offset; 5028 if (local64_cmpxchg(&hwc->period_left, old, val) != old) 5029 goto again; 5030 5031 return nr; 5032 } 5033 5034 static void perf_swevent_overflow(struct perf_event *event, u64 overflow, 5035 struct perf_sample_data *data, 5036 struct pt_regs *regs) 5037 { 5038 struct hw_perf_event *hwc = &event->hw; 5039 int throttle = 0; 5040 5041 if (!overflow) 5042 overflow = perf_swevent_set_period(event); 5043 5044 if (hwc->interrupts == MAX_INTERRUPTS) 5045 return; 5046 5047 for (; overflow; overflow--) { 5048 if (__perf_event_overflow(event, throttle, 5049 data, regs)) { 5050 /* 5051 * We inhibit the overflow from happening when 5052 * hwc->interrupts == MAX_INTERRUPTS. 5053 */ 5054 break; 5055 } 5056 throttle = 1; 5057 } 5058 } 5059 5060 static void perf_swevent_event(struct perf_event *event, u64 nr, 5061 struct perf_sample_data *data, 5062 struct pt_regs *regs) 5063 { 5064 struct hw_perf_event *hwc = &event->hw; 5065 5066 local64_add(nr, &event->count); 5067 5068 if (!regs) 5069 return; 5070 5071 if (!is_sampling_event(event)) 5072 return; 5073 5074 if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) { 5075 data->period = nr; 5076 return perf_swevent_overflow(event, 1, data, regs); 5077 } else 5078 data->period = event->hw.last_period; 5079 5080 if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq) 5081 return perf_swevent_overflow(event, 1, data, regs); 5082 5083 if (local64_add_negative(nr, &hwc->period_left)) 5084 return; 5085 5086 perf_swevent_overflow(event, 0, data, regs); 5087 } 5088 5089 static int perf_exclude_event(struct perf_event *event, 5090 struct pt_regs *regs) 5091 { 5092 if (event->hw.state & PERF_HES_STOPPED) 5093 return 1; 5094 5095 if (regs) { 5096 if (event->attr.exclude_user && user_mode(regs)) 5097 return 1; 5098 5099 if (event->attr.exclude_kernel && !user_mode(regs)) 5100 return 1; 5101 } 5102 5103 return 0; 5104 } 5105 5106 static int perf_swevent_match(struct perf_event *event, 5107 enum perf_type_id type, 5108 u32 event_id, 5109 struct perf_sample_data *data, 5110 struct pt_regs *regs) 5111 { 5112 if (event->attr.type != type) 5113 return 0; 5114 5115 if (event->attr.config != event_id) 5116 return 0; 5117 5118 if (perf_exclude_event(event, regs)) 5119 return 0; 5120 5121 return 1; 5122 } 5123 5124 static inline u64 swevent_hash(u64 type, u32 event_id) 5125 { 5126 u64 val = event_id | (type << 32); 5127 5128 return hash_64(val, SWEVENT_HLIST_BITS); 5129 } 5130 5131 static inline struct hlist_head * 5132 __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id) 5133 { 5134 u64 hash = swevent_hash(type, event_id); 5135 5136 return &hlist->heads[hash]; 5137 } 5138 5139 /* For the read side: events when they trigger */ 5140 static inline struct hlist_head * 5141 find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id) 5142 { 5143 struct swevent_hlist *hlist; 5144 5145 hlist = rcu_dereference(swhash->swevent_hlist); 5146 if (!hlist) 5147 return NULL; 5148 5149 return __find_swevent_head(hlist, type, event_id); 5150 } 5151 5152 /* For the event head insertion and removal in the hlist */ 5153 static inline struct hlist_head * 5154 find_swevent_head(struct swevent_htable *swhash, struct perf_event *event) 5155 { 5156 struct swevent_hlist *hlist; 5157 u32 event_id = event->attr.config; 5158 u64 type = event->attr.type; 5159 5160 /* 5161 * Event scheduling is always serialized against hlist allocation 5162 * and release. Which makes the protected version suitable here. 5163 * The context lock guarantees that. 5164 */ 5165 hlist = rcu_dereference_protected(swhash->swevent_hlist, 5166 lockdep_is_held(&event->ctx->lock)); 5167 if (!hlist) 5168 return NULL; 5169 5170 return __find_swevent_head(hlist, type, event_id); 5171 } 5172 5173 static void do_perf_sw_event(enum perf_type_id type, u32 event_id, 5174 u64 nr, 5175 struct perf_sample_data *data, 5176 struct pt_regs *regs) 5177 { 5178 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); 5179 struct perf_event *event; 5180 struct hlist_head *head; 5181 5182 rcu_read_lock(); 5183 head = find_swevent_head_rcu(swhash, type, event_id); 5184 if (!head) 5185 goto end; 5186 5187 hlist_for_each_entry_rcu(event, head, hlist_entry) { 5188 if (perf_swevent_match(event, type, event_id, data, regs)) 5189 perf_swevent_event(event, nr, data, regs); 5190 } 5191 end: 5192 rcu_read_unlock(); 5193 } 5194 5195 int perf_swevent_get_recursion_context(void) 5196 { 5197 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); 5198 5199 return get_recursion_context(swhash->recursion); 5200 } 5201 EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context); 5202 5203 inline void perf_swevent_put_recursion_context(int rctx) 5204 { 5205 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); 5206 5207 put_recursion_context(swhash->recursion, rctx); 5208 } 5209 5210 void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) 5211 { 5212 struct perf_sample_data data; 5213 int rctx; 5214 5215 preempt_disable_notrace(); 5216 rctx = perf_swevent_get_recursion_context(); 5217 if (rctx < 0) 5218 return; 5219 5220 perf_sample_data_init(&data, addr, 0); 5221 5222 do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs); 5223 5224 perf_swevent_put_recursion_context(rctx); 5225 preempt_enable_notrace(); 5226 } 5227 5228 static void perf_swevent_read(struct perf_event *event) 5229 { 5230 } 5231 5232 static int perf_swevent_add(struct perf_event *event, int flags) 5233 { 5234 struct swevent_htable *swhash = &__get_cpu_var(swevent_htable); 5235 struct hw_perf_event *hwc = &event->hw; 5236 struct hlist_head *head; 5237 5238 if (is_sampling_event(event)) { 5239 hwc->last_period = hwc->sample_period; 5240 perf_swevent_set_period(event); 5241 } 5242 5243 hwc->state = !(flags & PERF_EF_START); 5244 5245 head = find_swevent_head(swhash, event); 5246 if (WARN_ON_ONCE(!head)) 5247 return -EINVAL; 5248 5249 hlist_add_head_rcu(&event->hlist_entry, head); 5250 5251 return 0; 5252 } 5253 5254 static void perf_swevent_del(struct perf_event *event, int flags) 5255 { 5256 hlist_del_rcu(&event->hlist_entry); 5257 } 5258 5259 static void perf_swevent_start(struct perf_event *event, int flags) 5260 { 5261 event->hw.state = 0; 5262 } 5263 5264 static void perf_swevent_stop(struct perf_event *event, int flags) 5265 { 5266 event->hw.state = PERF_HES_STOPPED; 5267 } 5268 5269 /* Deref the hlist from the update side */ 5270 static inline struct swevent_hlist * 5271 swevent_hlist_deref(struct swevent_htable *swhash) 5272 { 5273 return rcu_dereference_protected(swhash->swevent_hlist, 5274 lockdep_is_held(&swhash->hlist_mutex)); 5275 } 5276 5277 static void swevent_hlist_release(struct swevent_htable *swhash) 5278 { 5279 struct swevent_hlist *hlist = swevent_hlist_deref(swhash); 5280 5281 if (!hlist) 5282 return; 5283 5284 rcu_assign_pointer(swhash->swevent_hlist, NULL); 5285 kfree_rcu(hlist, rcu_head); 5286 } 5287 5288 static void swevent_hlist_put_cpu(struct perf_event *event, int cpu) 5289 { 5290 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); 5291 5292 mutex_lock(&swhash->hlist_mutex); 5293 5294 if (!--swhash->hlist_refcount) 5295 swevent_hlist_release(swhash); 5296 5297 mutex_unlock(&swhash->hlist_mutex); 5298 } 5299 5300 static void swevent_hlist_put(struct perf_event *event) 5301 { 5302 int cpu; 5303 5304 if (event->cpu != -1) { 5305 swevent_hlist_put_cpu(event, event->cpu); 5306 return; 5307 } 5308 5309 for_each_possible_cpu(cpu) 5310 swevent_hlist_put_cpu(event, cpu); 5311 } 5312 5313 static int swevent_hlist_get_cpu(struct perf_event *event, int cpu) 5314 { 5315 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); 5316 int err = 0; 5317 5318 mutex_lock(&swhash->hlist_mutex); 5319 5320 if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) { 5321 struct swevent_hlist *hlist; 5322 5323 hlist = kzalloc(sizeof(*hlist), GFP_KERNEL); 5324 if (!hlist) { 5325 err = -ENOMEM; 5326 goto exit; 5327 } 5328 rcu_assign_pointer(swhash->swevent_hlist, hlist); 5329 } 5330 swhash->hlist_refcount++; 5331 exit: 5332 mutex_unlock(&swhash->hlist_mutex); 5333 5334 return err; 5335 } 5336 5337 static int swevent_hlist_get(struct perf_event *event) 5338 { 5339 int err; 5340 int cpu, failed_cpu; 5341 5342 if (event->cpu != -1) 5343 return swevent_hlist_get_cpu(event, event->cpu); 5344 5345 get_online_cpus(); 5346 for_each_possible_cpu(cpu) { 5347 err = swevent_hlist_get_cpu(event, cpu); 5348 if (err) { 5349 failed_cpu = cpu; 5350 goto fail; 5351 } 5352 } 5353 put_online_cpus(); 5354 5355 return 0; 5356 fail: 5357 for_each_possible_cpu(cpu) { 5358 if (cpu == failed_cpu) 5359 break; 5360 swevent_hlist_put_cpu(event, cpu); 5361 } 5362 5363 put_online_cpus(); 5364 return err; 5365 } 5366 5367 struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX]; 5368 5369 static void sw_perf_event_destroy(struct perf_event *event) 5370 { 5371 u64 event_id = event->attr.config; 5372 5373 WARN_ON(event->parent); 5374 5375 static_key_slow_dec(&perf_swevent_enabled[event_id]); 5376 swevent_hlist_put(event); 5377 } 5378 5379 static int perf_swevent_init(struct perf_event *event) 5380 { 5381 u64 event_id = event->attr.config; 5382 5383 if (event->attr.type != PERF_TYPE_SOFTWARE) 5384 return -ENOENT; 5385 5386 /* 5387 * no branch sampling for software events 5388 */ 5389 if (has_branch_stack(event)) 5390 return -EOPNOTSUPP; 5391 5392 switch (event_id) { 5393 case PERF_COUNT_SW_CPU_CLOCK: 5394 case PERF_COUNT_SW_TASK_CLOCK: 5395 return -ENOENT; 5396 5397 default: 5398 break; 5399 } 5400 5401 if (event_id >= PERF_COUNT_SW_MAX) 5402 return -ENOENT; 5403 5404 if (!event->parent) { 5405 int err; 5406 5407 err = swevent_hlist_get(event); 5408 if (err) 5409 return err; 5410 5411 static_key_slow_inc(&perf_swevent_enabled[event_id]); 5412 event->destroy = sw_perf_event_destroy; 5413 } 5414 5415 return 0; 5416 } 5417 5418 static int perf_swevent_event_idx(struct perf_event *event) 5419 { 5420 return 0; 5421 } 5422 5423 static struct pmu perf_swevent = { 5424 .task_ctx_nr = perf_sw_context, 5425 5426 .event_init = perf_swevent_init, 5427 .add = perf_swevent_add, 5428 .del = perf_swevent_del, 5429 .start = perf_swevent_start, 5430 .stop = perf_swevent_stop, 5431 .read = perf_swevent_read, 5432 5433 .event_idx = perf_swevent_event_idx, 5434 }; 5435 5436 #ifdef CONFIG_EVENT_TRACING 5437 5438 static int perf_tp_filter_match(struct perf_event *event, 5439 struct perf_sample_data *data) 5440 { 5441 void *record = data->raw->data; 5442 5443 if (likely(!event->filter) || filter_match_preds(event->filter, record)) 5444 return 1; 5445 return 0; 5446 } 5447 5448 static int perf_tp_event_match(struct perf_event *event, 5449 struct perf_sample_data *data, 5450 struct pt_regs *regs) 5451 { 5452 if (event->hw.state & PERF_HES_STOPPED) 5453 return 0; 5454 /* 5455 * All tracepoints are from kernel-space. 5456 */ 5457 if (event->attr.exclude_kernel) 5458 return 0; 5459 5460 if (!perf_tp_filter_match(event, data)) 5461 return 0; 5462 5463 return 1; 5464 } 5465 5466 void perf_tp_event(u64 addr, u64 count, void *record, int entry_size, 5467 struct pt_regs *regs, struct hlist_head *head, int rctx, 5468 struct task_struct *task) 5469 { 5470 struct perf_sample_data data; 5471 struct perf_event *event; 5472 5473 struct perf_raw_record raw = { 5474 .size = entry_size, 5475 .data = record, 5476 }; 5477 5478 perf_sample_data_init(&data, addr, 0); 5479 data.raw = &raw; 5480 5481 hlist_for_each_entry_rcu(event, head, hlist_entry) { 5482 if (perf_tp_event_match(event, &data, regs)) 5483 perf_swevent_event(event, count, &data, regs); 5484 } 5485 5486 /* 5487 * If we got specified a target task, also iterate its context and 5488 * deliver this event there too. 5489 */ 5490 if (task && task != current) { 5491 struct perf_event_context *ctx; 5492 struct trace_entry *entry = record; 5493 5494 rcu_read_lock(); 5495 ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]); 5496 if (!ctx) 5497 goto unlock; 5498 5499 list_for_each_entry_rcu(event, &ctx->event_list, event_entry) { 5500 if (event->attr.type != PERF_TYPE_TRACEPOINT) 5501 continue; 5502 if (event->attr.config != entry->type) 5503 continue; 5504 if (perf_tp_event_match(event, &data, regs)) 5505 perf_swevent_event(event, count, &data, regs); 5506 } 5507 unlock: 5508 rcu_read_unlock(); 5509 } 5510 5511 perf_swevent_put_recursion_context(rctx); 5512 } 5513 EXPORT_SYMBOL_GPL(perf_tp_event); 5514 5515 static void tp_perf_event_destroy(struct perf_event *event) 5516 { 5517 perf_trace_destroy(event); 5518 } 5519 5520 static int perf_tp_event_init(struct perf_event *event) 5521 { 5522 int err; 5523 5524 if (event->attr.type != PERF_TYPE_TRACEPOINT) 5525 return -ENOENT; 5526 5527 /* 5528 * no branch sampling for tracepoint events 5529 */ 5530 if (has_branch_stack(event)) 5531 return -EOPNOTSUPP; 5532 5533 err = perf_trace_init(event); 5534 if (err) 5535 return err; 5536 5537 event->destroy = tp_perf_event_destroy; 5538 5539 return 0; 5540 } 5541 5542 static struct pmu perf_tracepoint = { 5543 .task_ctx_nr = perf_sw_context, 5544 5545 .event_init = perf_tp_event_init, 5546 .add = perf_trace_add, 5547 .del = perf_trace_del, 5548 .start = perf_swevent_start, 5549 .stop = perf_swevent_stop, 5550 .read = perf_swevent_read, 5551 5552 .event_idx = perf_swevent_event_idx, 5553 }; 5554 5555 static inline void perf_tp_register(void) 5556 { 5557 perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT); 5558 } 5559 5560 static int perf_event_set_filter(struct perf_event *event, void __user *arg) 5561 { 5562 char *filter_str; 5563 int ret; 5564 5565 if (event->attr.type != PERF_TYPE_TRACEPOINT) 5566 return -EINVAL; 5567 5568 filter_str = strndup_user(arg, PAGE_SIZE); 5569 if (IS_ERR(filter_str)) 5570 return PTR_ERR(filter_str); 5571 5572 ret = ftrace_profile_set_filter(event, event->attr.config, filter_str); 5573 5574 kfree(filter_str); 5575 return ret; 5576 } 5577 5578 static void perf_event_free_filter(struct perf_event *event) 5579 { 5580 ftrace_profile_free_filter(event); 5581 } 5582 5583 #else 5584 5585 static inline void perf_tp_register(void) 5586 { 5587 } 5588 5589 static int perf_event_set_filter(struct perf_event *event, void __user *arg) 5590 { 5591 return -ENOENT; 5592 } 5593 5594 static void perf_event_free_filter(struct perf_event *event) 5595 { 5596 } 5597 5598 #endif /* CONFIG_EVENT_TRACING */ 5599 5600 #ifdef CONFIG_HAVE_HW_BREAKPOINT 5601 void perf_bp_event(struct perf_event *bp, void *data) 5602 { 5603 struct perf_sample_data sample; 5604 struct pt_regs *regs = data; 5605 5606 perf_sample_data_init(&sample, bp->attr.bp_addr, 0); 5607 5608 if (!bp->hw.state && !perf_exclude_event(bp, regs)) 5609 perf_swevent_event(bp, 1, &sample, regs); 5610 } 5611 #endif 5612 5613 /* 5614 * hrtimer based swevent callback 5615 */ 5616 5617 static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer) 5618 { 5619 enum hrtimer_restart ret = HRTIMER_RESTART; 5620 struct perf_sample_data data; 5621 struct pt_regs *regs; 5622 struct perf_event *event; 5623 u64 period; 5624 5625 event = container_of(hrtimer, struct perf_event, hw.hrtimer); 5626 5627 if (event->state != PERF_EVENT_STATE_ACTIVE) 5628 return HRTIMER_NORESTART; 5629 5630 event->pmu->read(event); 5631 5632 perf_sample_data_init(&data, 0, event->hw.last_period); 5633 regs = get_irq_regs(); 5634 5635 if (regs && !perf_exclude_event(event, regs)) { 5636 if (!(event->attr.exclude_idle && is_idle_task(current))) 5637 if (__perf_event_overflow(event, 1, &data, regs)) 5638 ret = HRTIMER_NORESTART; 5639 } 5640 5641 period = max_t(u64, 10000, event->hw.sample_period); 5642 hrtimer_forward_now(hrtimer, ns_to_ktime(period)); 5643 5644 return ret; 5645 } 5646 5647 static void perf_swevent_start_hrtimer(struct perf_event *event) 5648 { 5649 struct hw_perf_event *hwc = &event->hw; 5650 s64 period; 5651 5652 if (!is_sampling_event(event)) 5653 return; 5654 5655 period = local64_read(&hwc->period_left); 5656 if (period) { 5657 if (period < 0) 5658 period = 10000; 5659 5660 local64_set(&hwc->period_left, 0); 5661 } else { 5662 period = max_t(u64, 10000, hwc->sample_period); 5663 } 5664 __hrtimer_start_range_ns(&hwc->hrtimer, 5665 ns_to_ktime(period), 0, 5666 HRTIMER_MODE_REL_PINNED, 0); 5667 } 5668 5669 static void perf_swevent_cancel_hrtimer(struct perf_event *event) 5670 { 5671 struct hw_perf_event *hwc = &event->hw; 5672 5673 if (is_sampling_event(event)) { 5674 ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer); 5675 local64_set(&hwc->period_left, ktime_to_ns(remaining)); 5676 5677 hrtimer_cancel(&hwc->hrtimer); 5678 } 5679 } 5680 5681 static void perf_swevent_init_hrtimer(struct perf_event *event) 5682 { 5683 struct hw_perf_event *hwc = &event->hw; 5684 5685 if (!is_sampling_event(event)) 5686 return; 5687 5688 hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 5689 hwc->hrtimer.function = perf_swevent_hrtimer; 5690 5691 /* 5692 * Since hrtimers have a fixed rate, we can do a static freq->period 5693 * mapping and avoid the whole period adjust feedback stuff. 5694 */ 5695 if (event->attr.freq) { 5696 long freq = event->attr.sample_freq; 5697 5698 event->attr.sample_period = NSEC_PER_SEC / freq; 5699 hwc->sample_period = event->attr.sample_period; 5700 local64_set(&hwc->period_left, hwc->sample_period); 5701 hwc->last_period = hwc->sample_period; 5702 event->attr.freq = 0; 5703 } 5704 } 5705 5706 /* 5707 * Software event: cpu wall time clock 5708 */ 5709 5710 static void cpu_clock_event_update(struct perf_event *event) 5711 { 5712 s64 prev; 5713 u64 now; 5714 5715 now = local_clock(); 5716 prev = local64_xchg(&event->hw.prev_count, now); 5717 local64_add(now - prev, &event->count); 5718 } 5719 5720 static void cpu_clock_event_start(struct perf_event *event, int flags) 5721 { 5722 local64_set(&event->hw.prev_count, local_clock()); 5723 perf_swevent_start_hrtimer(event); 5724 } 5725 5726 static void cpu_clock_event_stop(struct perf_event *event, int flags) 5727 { 5728 perf_swevent_cancel_hrtimer(event); 5729 cpu_clock_event_update(event); 5730 } 5731 5732 static int cpu_clock_event_add(struct perf_event *event, int flags) 5733 { 5734 if (flags & PERF_EF_START) 5735 cpu_clock_event_start(event, flags); 5736 5737 return 0; 5738 } 5739 5740 static void cpu_clock_event_del(struct perf_event *event, int flags) 5741 { 5742 cpu_clock_event_stop(event, flags); 5743 } 5744 5745 static void cpu_clock_event_read(struct perf_event *event) 5746 { 5747 cpu_clock_event_update(event); 5748 } 5749 5750 static int cpu_clock_event_init(struct perf_event *event) 5751 { 5752 if (event->attr.type != PERF_TYPE_SOFTWARE) 5753 return -ENOENT; 5754 5755 if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK) 5756 return -ENOENT; 5757 5758 /* 5759 * no branch sampling for software events 5760 */ 5761 if (has_branch_stack(event)) 5762 return -EOPNOTSUPP; 5763 5764 perf_swevent_init_hrtimer(event); 5765 5766 return 0; 5767 } 5768 5769 static struct pmu perf_cpu_clock = { 5770 .task_ctx_nr = perf_sw_context, 5771 5772 .event_init = cpu_clock_event_init, 5773 .add = cpu_clock_event_add, 5774 .del = cpu_clock_event_del, 5775 .start = cpu_clock_event_start, 5776 .stop = cpu_clock_event_stop, 5777 .read = cpu_clock_event_read, 5778 5779 .event_idx = perf_swevent_event_idx, 5780 }; 5781 5782 /* 5783 * Software event: task time clock 5784 */ 5785 5786 static void task_clock_event_update(struct perf_event *event, u64 now) 5787 { 5788 u64 prev; 5789 s64 delta; 5790 5791 prev = local64_xchg(&event->hw.prev_count, now); 5792 delta = now - prev; 5793 local64_add(delta, &event->count); 5794 } 5795 5796 static void task_clock_event_start(struct perf_event *event, int flags) 5797 { 5798 local64_set(&event->hw.prev_count, event->ctx->time); 5799 perf_swevent_start_hrtimer(event); 5800 } 5801 5802 static void task_clock_event_stop(struct perf_event *event, int flags) 5803 { 5804 perf_swevent_cancel_hrtimer(event); 5805 task_clock_event_update(event, event->ctx->time); 5806 } 5807 5808 static int task_clock_event_add(struct perf_event *event, int flags) 5809 { 5810 if (flags & PERF_EF_START) 5811 task_clock_event_start(event, flags); 5812 5813 return 0; 5814 } 5815 5816 static void task_clock_event_del(struct perf_event *event, int flags) 5817 { 5818 task_clock_event_stop(event, PERF_EF_UPDATE); 5819 } 5820 5821 static void task_clock_event_read(struct perf_event *event) 5822 { 5823 u64 now = perf_clock(); 5824 u64 delta = now - event->ctx->timestamp; 5825 u64 time = event->ctx->time + delta; 5826 5827 task_clock_event_update(event, time); 5828 } 5829 5830 static int task_clock_event_init(struct perf_event *event) 5831 { 5832 if (event->attr.type != PERF_TYPE_SOFTWARE) 5833 return -ENOENT; 5834 5835 if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK) 5836 return -ENOENT; 5837 5838 /* 5839 * no branch sampling for software events 5840 */ 5841 if (has_branch_stack(event)) 5842 return -EOPNOTSUPP; 5843 5844 perf_swevent_init_hrtimer(event); 5845 5846 return 0; 5847 } 5848 5849 static struct pmu perf_task_clock = { 5850 .task_ctx_nr = perf_sw_context, 5851 5852 .event_init = task_clock_event_init, 5853 .add = task_clock_event_add, 5854 .del = task_clock_event_del, 5855 .start = task_clock_event_start, 5856 .stop = task_clock_event_stop, 5857 .read = task_clock_event_read, 5858 5859 .event_idx = perf_swevent_event_idx, 5860 }; 5861 5862 static void perf_pmu_nop_void(struct pmu *pmu) 5863 { 5864 } 5865 5866 static int perf_pmu_nop_int(struct pmu *pmu) 5867 { 5868 return 0; 5869 } 5870 5871 static void perf_pmu_start_txn(struct pmu *pmu) 5872 { 5873 perf_pmu_disable(pmu); 5874 } 5875 5876 static int perf_pmu_commit_txn(struct pmu *pmu) 5877 { 5878 perf_pmu_enable(pmu); 5879 return 0; 5880 } 5881 5882 static void perf_pmu_cancel_txn(struct pmu *pmu) 5883 { 5884 perf_pmu_enable(pmu); 5885 } 5886 5887 static int perf_event_idx_default(struct perf_event *event) 5888 { 5889 return event->hw.idx + 1; 5890 } 5891 5892 /* 5893 * Ensures all contexts with the same task_ctx_nr have the same 5894 * pmu_cpu_context too. 5895 */ 5896 static void *find_pmu_context(int ctxn) 5897 { 5898 struct pmu *pmu; 5899 5900 if (ctxn < 0) 5901 return NULL; 5902 5903 list_for_each_entry(pmu, &pmus, entry) { 5904 if (pmu->task_ctx_nr == ctxn) 5905 return pmu->pmu_cpu_context; 5906 } 5907 5908 return NULL; 5909 } 5910 5911 static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu) 5912 { 5913 int cpu; 5914 5915 for_each_possible_cpu(cpu) { 5916 struct perf_cpu_context *cpuctx; 5917 5918 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); 5919 5920 if (cpuctx->unique_pmu == old_pmu) 5921 cpuctx->unique_pmu = pmu; 5922 } 5923 } 5924 5925 static void free_pmu_context(struct pmu *pmu) 5926 { 5927 struct pmu *i; 5928 5929 mutex_lock(&pmus_lock); 5930 /* 5931 * Like a real lame refcount. 5932 */ 5933 list_for_each_entry(i, &pmus, entry) { 5934 if (i->pmu_cpu_context == pmu->pmu_cpu_context) { 5935 update_pmu_context(i, pmu); 5936 goto out; 5937 } 5938 } 5939 5940 free_percpu(pmu->pmu_cpu_context); 5941 out: 5942 mutex_unlock(&pmus_lock); 5943 } 5944 static struct idr pmu_idr; 5945 5946 static ssize_t 5947 type_show(struct device *dev, struct device_attribute *attr, char *page) 5948 { 5949 struct pmu *pmu = dev_get_drvdata(dev); 5950 5951 return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type); 5952 } 5953 5954 static struct device_attribute pmu_dev_attrs[] = { 5955 __ATTR_RO(type), 5956 __ATTR_NULL, 5957 }; 5958 5959 static int pmu_bus_running; 5960 static struct bus_type pmu_bus = { 5961 .name = "event_source", 5962 .dev_attrs = pmu_dev_attrs, 5963 }; 5964 5965 static void pmu_dev_release(struct device *dev) 5966 { 5967 kfree(dev); 5968 } 5969 5970 static int pmu_dev_alloc(struct pmu *pmu) 5971 { 5972 int ret = -ENOMEM; 5973 5974 pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL); 5975 if (!pmu->dev) 5976 goto out; 5977 5978 pmu->dev->groups = pmu->attr_groups; 5979 device_initialize(pmu->dev); 5980 ret = dev_set_name(pmu->dev, "%s", pmu->name); 5981 if (ret) 5982 goto free_dev; 5983 5984 dev_set_drvdata(pmu->dev, pmu); 5985 pmu->dev->bus = &pmu_bus; 5986 pmu->dev->release = pmu_dev_release; 5987 ret = device_add(pmu->dev); 5988 if (ret) 5989 goto free_dev; 5990 5991 out: 5992 return ret; 5993 5994 free_dev: 5995 put_device(pmu->dev); 5996 goto out; 5997 } 5998 5999 static struct lock_class_key cpuctx_mutex; 6000 static struct lock_class_key cpuctx_lock; 6001 6002 int perf_pmu_register(struct pmu *pmu, char *name, int type) 6003 { 6004 int cpu, ret; 6005 6006 mutex_lock(&pmus_lock); 6007 ret = -ENOMEM; 6008 pmu->pmu_disable_count = alloc_percpu(int); 6009 if (!pmu->pmu_disable_count) 6010 goto unlock; 6011 6012 pmu->type = -1; 6013 if (!name) 6014 goto skip_type; 6015 pmu->name = name; 6016 6017 if (type < 0) { 6018 type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL); 6019 if (type < 0) { 6020 ret = type; 6021 goto free_pdc; 6022 } 6023 } 6024 pmu->type = type; 6025 6026 if (pmu_bus_running) { 6027 ret = pmu_dev_alloc(pmu); 6028 if (ret) 6029 goto free_idr; 6030 } 6031 6032 skip_type: 6033 pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr); 6034 if (pmu->pmu_cpu_context) 6035 goto got_cpu_context; 6036 6037 ret = -ENOMEM; 6038 pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context); 6039 if (!pmu->pmu_cpu_context) 6040 goto free_dev; 6041 6042 for_each_possible_cpu(cpu) { 6043 struct perf_cpu_context *cpuctx; 6044 6045 cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu); 6046 __perf_event_init_context(&cpuctx->ctx); 6047 lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex); 6048 lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock); 6049 cpuctx->ctx.type = cpu_context; 6050 cpuctx->ctx.pmu = pmu; 6051 cpuctx->jiffies_interval = 1; 6052 INIT_LIST_HEAD(&cpuctx->rotation_list); 6053 cpuctx->unique_pmu = pmu; 6054 } 6055 6056 got_cpu_context: 6057 if (!pmu->start_txn) { 6058 if (pmu->pmu_enable) { 6059 /* 6060 * If we have pmu_enable/pmu_disable calls, install 6061 * transaction stubs that use that to try and batch 6062 * hardware accesses. 6063 */ 6064 pmu->start_txn = perf_pmu_start_txn; 6065 pmu->commit_txn = perf_pmu_commit_txn; 6066 pmu->cancel_txn = perf_pmu_cancel_txn; 6067 } else { 6068 pmu->start_txn = perf_pmu_nop_void; 6069 pmu->commit_txn = perf_pmu_nop_int; 6070 pmu->cancel_txn = perf_pmu_nop_void; 6071 } 6072 } 6073 6074 if (!pmu->pmu_enable) { 6075 pmu->pmu_enable = perf_pmu_nop_void; 6076 pmu->pmu_disable = perf_pmu_nop_void; 6077 } 6078 6079 if (!pmu->event_idx) 6080 pmu->event_idx = perf_event_idx_default; 6081 6082 list_add_rcu(&pmu->entry, &pmus); 6083 ret = 0; 6084 unlock: 6085 mutex_unlock(&pmus_lock); 6086 6087 return ret; 6088 6089 free_dev: 6090 device_del(pmu->dev); 6091 put_device(pmu->dev); 6092 6093 free_idr: 6094 if (pmu->type >= PERF_TYPE_MAX) 6095 idr_remove(&pmu_idr, pmu->type); 6096 6097 free_pdc: 6098 free_percpu(pmu->pmu_disable_count); 6099 goto unlock; 6100 } 6101 6102 void perf_pmu_unregister(struct pmu *pmu) 6103 { 6104 mutex_lock(&pmus_lock); 6105 list_del_rcu(&pmu->entry); 6106 mutex_unlock(&pmus_lock); 6107 6108 /* 6109 * We dereference the pmu list under both SRCU and regular RCU, so 6110 * synchronize against both of those. 6111 */ 6112 synchronize_srcu(&pmus_srcu); 6113 synchronize_rcu(); 6114 6115 free_percpu(pmu->pmu_disable_count); 6116 if (pmu->type >= PERF_TYPE_MAX) 6117 idr_remove(&pmu_idr, pmu->type); 6118 device_del(pmu->dev); 6119 put_device(pmu->dev); 6120 free_pmu_context(pmu); 6121 } 6122 6123 struct pmu *perf_init_event(struct perf_event *event) 6124 { 6125 struct pmu *pmu = NULL; 6126 int idx; 6127 int ret; 6128 6129 idx = srcu_read_lock(&pmus_srcu); 6130 6131 rcu_read_lock(); 6132 pmu = idr_find(&pmu_idr, event->attr.type); 6133 rcu_read_unlock(); 6134 if (pmu) { 6135 event->pmu = pmu; 6136 ret = pmu->event_init(event); 6137 if (ret) 6138 pmu = ERR_PTR(ret); 6139 goto unlock; 6140 } 6141 6142 list_for_each_entry_rcu(pmu, &pmus, entry) { 6143 event->pmu = pmu; 6144 ret = pmu->event_init(event); 6145 if (!ret) 6146 goto unlock; 6147 6148 if (ret != -ENOENT) { 6149 pmu = ERR_PTR(ret); 6150 goto unlock; 6151 } 6152 } 6153 pmu = ERR_PTR(-ENOENT); 6154 unlock: 6155 srcu_read_unlock(&pmus_srcu, idx); 6156 6157 return pmu; 6158 } 6159 6160 /* 6161 * Allocate and initialize a event structure 6162 */ 6163 static struct perf_event * 6164 perf_event_alloc(struct perf_event_attr *attr, int cpu, 6165 struct task_struct *task, 6166 struct perf_event *group_leader, 6167 struct perf_event *parent_event, 6168 perf_overflow_handler_t overflow_handler, 6169 void *context) 6170 { 6171 struct pmu *pmu; 6172 struct perf_event *event; 6173 struct hw_perf_event *hwc; 6174 long err; 6175 6176 if ((unsigned)cpu >= nr_cpu_ids) { 6177 if (!task || cpu != -1) 6178 return ERR_PTR(-EINVAL); 6179 } 6180 6181 event = kzalloc(sizeof(*event), GFP_KERNEL); 6182 if (!event) 6183 return ERR_PTR(-ENOMEM); 6184 6185 /* 6186 * Single events are their own group leaders, with an 6187 * empty sibling list: 6188 */ 6189 if (!group_leader) 6190 group_leader = event; 6191 6192 mutex_init(&event->child_mutex); 6193 INIT_LIST_HEAD(&event->child_list); 6194 6195 INIT_LIST_HEAD(&event->group_entry); 6196 INIT_LIST_HEAD(&event->event_entry); 6197 INIT_LIST_HEAD(&event->sibling_list); 6198 INIT_LIST_HEAD(&event->rb_entry); 6199 6200 init_waitqueue_head(&event->waitq); 6201 init_irq_work(&event->pending, perf_pending_event); 6202 6203 mutex_init(&event->mmap_mutex); 6204 6205 atomic_long_set(&event->refcount, 1); 6206 event->cpu = cpu; 6207 event->attr = *attr; 6208 event->group_leader = group_leader; 6209 event->pmu = NULL; 6210 event->oncpu = -1; 6211 6212 event->parent = parent_event; 6213 6214 event->ns = get_pid_ns(task_active_pid_ns(current)); 6215 event->id = atomic64_inc_return(&perf_event_id); 6216 6217 event->state = PERF_EVENT_STATE_INACTIVE; 6218 6219 if (task) { 6220 event->attach_state = PERF_ATTACH_TASK; 6221 6222 if (attr->type == PERF_TYPE_TRACEPOINT) 6223 event->hw.tp_target = task; 6224 #ifdef CONFIG_HAVE_HW_BREAKPOINT 6225 /* 6226 * hw_breakpoint is a bit difficult here.. 6227 */ 6228 else if (attr->type == PERF_TYPE_BREAKPOINT) 6229 event->hw.bp_target = task; 6230 #endif 6231 } 6232 6233 if (!overflow_handler && parent_event) { 6234 overflow_handler = parent_event->overflow_handler; 6235 context = parent_event->overflow_handler_context; 6236 } 6237 6238 event->overflow_handler = overflow_handler; 6239 event->overflow_handler_context = context; 6240 6241 perf_event__state_init(event); 6242 6243 pmu = NULL; 6244 6245 hwc = &event->hw; 6246 hwc->sample_period = attr->sample_period; 6247 if (attr->freq && attr->sample_freq) 6248 hwc->sample_period = 1; 6249 hwc->last_period = hwc->sample_period; 6250 6251 local64_set(&hwc->period_left, hwc->sample_period); 6252 6253 /* 6254 * we currently do not support PERF_FORMAT_GROUP on inherited events 6255 */ 6256 if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP)) 6257 goto done; 6258 6259 pmu = perf_init_event(event); 6260 6261 done: 6262 err = 0; 6263 if (!pmu) 6264 err = -EINVAL; 6265 else if (IS_ERR(pmu)) 6266 err = PTR_ERR(pmu); 6267 6268 if (err) { 6269 if (event->ns) 6270 put_pid_ns(event->ns); 6271 kfree(event); 6272 return ERR_PTR(err); 6273 } 6274 6275 if (!event->parent) { 6276 if (event->attach_state & PERF_ATTACH_TASK) 6277 static_key_slow_inc(&perf_sched_events.key); 6278 if (event->attr.mmap || event->attr.mmap_data) 6279 atomic_inc(&nr_mmap_events); 6280 if (event->attr.comm) 6281 atomic_inc(&nr_comm_events); 6282 if (event->attr.task) 6283 atomic_inc(&nr_task_events); 6284 if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) { 6285 err = get_callchain_buffers(); 6286 if (err) { 6287 free_event(event); 6288 return ERR_PTR(err); 6289 } 6290 } 6291 if (has_branch_stack(event)) { 6292 static_key_slow_inc(&perf_sched_events.key); 6293 if (!(event->attach_state & PERF_ATTACH_TASK)) 6294 atomic_inc(&per_cpu(perf_branch_stack_events, 6295 event->cpu)); 6296 } 6297 } 6298 6299 return event; 6300 } 6301 6302 static int perf_copy_attr(struct perf_event_attr __user *uattr, 6303 struct perf_event_attr *attr) 6304 { 6305 u32 size; 6306 int ret; 6307 6308 if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0)) 6309 return -EFAULT; 6310 6311 /* 6312 * zero the full structure, so that a short copy will be nice. 6313 */ 6314 memset(attr, 0, sizeof(*attr)); 6315 6316 ret = get_user(size, &uattr->size); 6317 if (ret) 6318 return ret; 6319 6320 if (size > PAGE_SIZE) /* silly large */ 6321 goto err_size; 6322 6323 if (!size) /* abi compat */ 6324 size = PERF_ATTR_SIZE_VER0; 6325 6326 if (size < PERF_ATTR_SIZE_VER0) 6327 goto err_size; 6328 6329 /* 6330 * If we're handed a bigger struct than we know of, 6331 * ensure all the unknown bits are 0 - i.e. new 6332 * user-space does not rely on any kernel feature 6333 * extensions we dont know about yet. 6334 */ 6335 if (size > sizeof(*attr)) { 6336 unsigned char __user *addr; 6337 unsigned char __user *end; 6338 unsigned char val; 6339 6340 addr = (void __user *)uattr + sizeof(*attr); 6341 end = (void __user *)uattr + size; 6342 6343 for (; addr < end; addr++) { 6344 ret = get_user(val, addr); 6345 if (ret) 6346 return ret; 6347 if (val) 6348 goto err_size; 6349 } 6350 size = sizeof(*attr); 6351 } 6352 6353 ret = copy_from_user(attr, uattr, size); 6354 if (ret) 6355 return -EFAULT; 6356 6357 if (attr->__reserved_1) 6358 return -EINVAL; 6359 6360 if (attr->sample_type & ~(PERF_SAMPLE_MAX-1)) 6361 return -EINVAL; 6362 6363 if (attr->read_format & ~(PERF_FORMAT_MAX-1)) 6364 return -EINVAL; 6365 6366 if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) { 6367 u64 mask = attr->branch_sample_type; 6368 6369 /* only using defined bits */ 6370 if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1)) 6371 return -EINVAL; 6372 6373 /* at least one branch bit must be set */ 6374 if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL)) 6375 return -EINVAL; 6376 6377 /* kernel level capture: check permissions */ 6378 if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM) 6379 && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) 6380 return -EACCES; 6381 6382 /* propagate priv level, when not set for branch */ 6383 if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) { 6384 6385 /* exclude_kernel checked on syscall entry */ 6386 if (!attr->exclude_kernel) 6387 mask |= PERF_SAMPLE_BRANCH_KERNEL; 6388 6389 if (!attr->exclude_user) 6390 mask |= PERF_SAMPLE_BRANCH_USER; 6391 6392 if (!attr->exclude_hv) 6393 mask |= PERF_SAMPLE_BRANCH_HV; 6394 /* 6395 * adjust user setting (for HW filter setup) 6396 */ 6397 attr->branch_sample_type = mask; 6398 } 6399 } 6400 6401 if (attr->sample_type & PERF_SAMPLE_REGS_USER) { 6402 ret = perf_reg_validate(attr->sample_regs_user); 6403 if (ret) 6404 return ret; 6405 } 6406 6407 if (attr->sample_type & PERF_SAMPLE_STACK_USER) { 6408 if (!arch_perf_have_user_stack_dump()) 6409 return -ENOSYS; 6410 6411 /* 6412 * We have __u32 type for the size, but so far 6413 * we can only use __u16 as maximum due to the 6414 * __u16 sample size limit. 6415 */ 6416 if (attr->sample_stack_user >= USHRT_MAX) 6417 ret = -EINVAL; 6418 else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64))) 6419 ret = -EINVAL; 6420 } 6421 6422 out: 6423 return ret; 6424 6425 err_size: 6426 put_user(sizeof(*attr), &uattr->size); 6427 ret = -E2BIG; 6428 goto out; 6429 } 6430 6431 static int 6432 perf_event_set_output(struct perf_event *event, struct perf_event *output_event) 6433 { 6434 struct ring_buffer *rb = NULL, *old_rb = NULL; 6435 int ret = -EINVAL; 6436 6437 if (!output_event) 6438 goto set; 6439 6440 /* don't allow circular references */ 6441 if (event == output_event) 6442 goto out; 6443 6444 /* 6445 * Don't allow cross-cpu buffers 6446 */ 6447 if (output_event->cpu != event->cpu) 6448 goto out; 6449 6450 /* 6451 * If its not a per-cpu rb, it must be the same task. 6452 */ 6453 if (output_event->cpu == -1 && output_event->ctx != event->ctx) 6454 goto out; 6455 6456 set: 6457 mutex_lock(&event->mmap_mutex); 6458 /* Can't redirect output if we've got an active mmap() */ 6459 if (atomic_read(&event->mmap_count)) 6460 goto unlock; 6461 6462 if (output_event) { 6463 /* get the rb we want to redirect to */ 6464 rb = ring_buffer_get(output_event); 6465 if (!rb) 6466 goto unlock; 6467 } 6468 6469 old_rb = event->rb; 6470 rcu_assign_pointer(event->rb, rb); 6471 if (old_rb) 6472 ring_buffer_detach(event, old_rb); 6473 ret = 0; 6474 unlock: 6475 mutex_unlock(&event->mmap_mutex); 6476 6477 if (old_rb) 6478 ring_buffer_put(old_rb); 6479 out: 6480 return ret; 6481 } 6482 6483 /** 6484 * sys_perf_event_open - open a performance event, associate it to a task/cpu 6485 * 6486 * @attr_uptr: event_id type attributes for monitoring/sampling 6487 * @pid: target pid 6488 * @cpu: target cpu 6489 * @group_fd: group leader event fd 6490 */ 6491 SYSCALL_DEFINE5(perf_event_open, 6492 struct perf_event_attr __user *, attr_uptr, 6493 pid_t, pid, int, cpu, int, group_fd, unsigned long, flags) 6494 { 6495 struct perf_event *group_leader = NULL, *output_event = NULL; 6496 struct perf_event *event, *sibling; 6497 struct perf_event_attr attr; 6498 struct perf_event_context *ctx; 6499 struct file *event_file = NULL; 6500 struct fd group = {NULL, 0}; 6501 struct task_struct *task = NULL; 6502 struct pmu *pmu; 6503 int event_fd; 6504 int move_group = 0; 6505 int err; 6506 6507 /* for future expandability... */ 6508 if (flags & ~PERF_FLAG_ALL) 6509 return -EINVAL; 6510 6511 err = perf_copy_attr(attr_uptr, &attr); 6512 if (err) 6513 return err; 6514 6515 if (!attr.exclude_kernel) { 6516 if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN)) 6517 return -EACCES; 6518 } 6519 6520 if (attr.freq) { 6521 if (attr.sample_freq > sysctl_perf_event_sample_rate) 6522 return -EINVAL; 6523 } 6524 6525 /* 6526 * In cgroup mode, the pid argument is used to pass the fd 6527 * opened to the cgroup directory in cgroupfs. The cpu argument 6528 * designates the cpu on which to monitor threads from that 6529 * cgroup. 6530 */ 6531 if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1)) 6532 return -EINVAL; 6533 6534 event_fd = get_unused_fd(); 6535 if (event_fd < 0) 6536 return event_fd; 6537 6538 if (group_fd != -1) { 6539 err = perf_fget_light(group_fd, &group); 6540 if (err) 6541 goto err_fd; 6542 group_leader = group.file->private_data; 6543 if (flags & PERF_FLAG_FD_OUTPUT) 6544 output_event = group_leader; 6545 if (flags & PERF_FLAG_FD_NO_GROUP) 6546 group_leader = NULL; 6547 } 6548 6549 if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) { 6550 task = find_lively_task_by_vpid(pid); 6551 if (IS_ERR(task)) { 6552 err = PTR_ERR(task); 6553 goto err_group_fd; 6554 } 6555 } 6556 6557 get_online_cpus(); 6558 6559 event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, 6560 NULL, NULL); 6561 if (IS_ERR(event)) { 6562 err = PTR_ERR(event); 6563 goto err_task; 6564 } 6565 6566 if (flags & PERF_FLAG_PID_CGROUP) { 6567 err = perf_cgroup_connect(pid, event, &attr, group_leader); 6568 if (err) 6569 goto err_alloc; 6570 /* 6571 * one more event: 6572 * - that has cgroup constraint on event->cpu 6573 * - that may need work on context switch 6574 */ 6575 atomic_inc(&per_cpu(perf_cgroup_events, event->cpu)); 6576 static_key_slow_inc(&perf_sched_events.key); 6577 } 6578 6579 /* 6580 * Special case software events and allow them to be part of 6581 * any hardware group. 6582 */ 6583 pmu = event->pmu; 6584 6585 if (group_leader && 6586 (is_software_event(event) != is_software_event(group_leader))) { 6587 if (is_software_event(event)) { 6588 /* 6589 * If event and group_leader are not both a software 6590 * event, and event is, then group leader is not. 6591 * 6592 * Allow the addition of software events to !software 6593 * groups, this is safe because software events never 6594 * fail to schedule. 6595 */ 6596 pmu = group_leader->pmu; 6597 } else if (is_software_event(group_leader) && 6598 (group_leader->group_flags & PERF_GROUP_SOFTWARE)) { 6599 /* 6600 * In case the group is a pure software group, and we 6601 * try to add a hardware event, move the whole group to 6602 * the hardware context. 6603 */ 6604 move_group = 1; 6605 } 6606 } 6607 6608 /* 6609 * Get the target context (task or percpu): 6610 */ 6611 ctx = find_get_context(pmu, task, event->cpu); 6612 if (IS_ERR(ctx)) { 6613 err = PTR_ERR(ctx); 6614 goto err_alloc; 6615 } 6616 6617 if (task) { 6618 put_task_struct(task); 6619 task = NULL; 6620 } 6621 6622 /* 6623 * Look up the group leader (we will attach this event to it): 6624 */ 6625 if (group_leader) { 6626 err = -EINVAL; 6627 6628 /* 6629 * Do not allow a recursive hierarchy (this new sibling 6630 * becoming part of another group-sibling): 6631 */ 6632 if (group_leader->group_leader != group_leader) 6633 goto err_context; 6634 /* 6635 * Do not allow to attach to a group in a different 6636 * task or CPU context: 6637 */ 6638 if (move_group) { 6639 if (group_leader->ctx->type != ctx->type) 6640 goto err_context; 6641 } else { 6642 if (group_leader->ctx != ctx) 6643 goto err_context; 6644 } 6645 6646 /* 6647 * Only a group leader can be exclusive or pinned 6648 */ 6649 if (attr.exclusive || attr.pinned) 6650 goto err_context; 6651 } 6652 6653 if (output_event) { 6654 err = perf_event_set_output(event, output_event); 6655 if (err) 6656 goto err_context; 6657 } 6658 6659 event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR); 6660 if (IS_ERR(event_file)) { 6661 err = PTR_ERR(event_file); 6662 goto err_context; 6663 } 6664 6665 if (move_group) { 6666 struct perf_event_context *gctx = group_leader->ctx; 6667 6668 mutex_lock(&gctx->mutex); 6669 perf_remove_from_context(group_leader); 6670 6671 /* 6672 * Removing from the context ends up with disabled 6673 * event. What we want here is event in the initial 6674 * startup state, ready to be add into new context. 6675 */ 6676 perf_event__state_init(group_leader); 6677 list_for_each_entry(sibling, &group_leader->sibling_list, 6678 group_entry) { 6679 perf_remove_from_context(sibling); 6680 perf_event__state_init(sibling); 6681 put_ctx(gctx); 6682 } 6683 mutex_unlock(&gctx->mutex); 6684 put_ctx(gctx); 6685 } 6686 6687 WARN_ON_ONCE(ctx->parent_ctx); 6688 mutex_lock(&ctx->mutex); 6689 6690 if (move_group) { 6691 synchronize_rcu(); 6692 perf_install_in_context(ctx, group_leader, event->cpu); 6693 get_ctx(ctx); 6694 list_for_each_entry(sibling, &group_leader->sibling_list, 6695 group_entry) { 6696 perf_install_in_context(ctx, sibling, event->cpu); 6697 get_ctx(ctx); 6698 } 6699 } 6700 6701 perf_install_in_context(ctx, event, event->cpu); 6702 ++ctx->generation; 6703 perf_unpin_context(ctx); 6704 mutex_unlock(&ctx->mutex); 6705 6706 put_online_cpus(); 6707 6708 event->owner = current; 6709 6710 mutex_lock(¤t->perf_event_mutex); 6711 list_add_tail(&event->owner_entry, ¤t->perf_event_list); 6712 mutex_unlock(¤t->perf_event_mutex); 6713 6714 /* 6715 * Precalculate sample_data sizes 6716 */ 6717 perf_event__header_size(event); 6718 perf_event__id_header_size(event); 6719 6720 /* 6721 * Drop the reference on the group_event after placing the 6722 * new event on the sibling_list. This ensures destruction 6723 * of the group leader will find the pointer to itself in 6724 * perf_group_detach(). 6725 */ 6726 fdput(group); 6727 fd_install(event_fd, event_file); 6728 return event_fd; 6729 6730 err_context: 6731 perf_unpin_context(ctx); 6732 put_ctx(ctx); 6733 err_alloc: 6734 free_event(event); 6735 err_task: 6736 put_online_cpus(); 6737 if (task) 6738 put_task_struct(task); 6739 err_group_fd: 6740 fdput(group); 6741 err_fd: 6742 put_unused_fd(event_fd); 6743 return err; 6744 } 6745 6746 /** 6747 * perf_event_create_kernel_counter 6748 * 6749 * @attr: attributes of the counter to create 6750 * @cpu: cpu in which the counter is bound 6751 * @task: task to profile (NULL for percpu) 6752 */ 6753 struct perf_event * 6754 perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu, 6755 struct task_struct *task, 6756 perf_overflow_handler_t overflow_handler, 6757 void *context) 6758 { 6759 struct perf_event_context *ctx; 6760 struct perf_event *event; 6761 int err; 6762 6763 /* 6764 * Get the target context (task or percpu): 6765 */ 6766 6767 event = perf_event_alloc(attr, cpu, task, NULL, NULL, 6768 overflow_handler, context); 6769 if (IS_ERR(event)) { 6770 err = PTR_ERR(event); 6771 goto err; 6772 } 6773 6774 ctx = find_get_context(event->pmu, task, cpu); 6775 if (IS_ERR(ctx)) { 6776 err = PTR_ERR(ctx); 6777 goto err_free; 6778 } 6779 6780 WARN_ON_ONCE(ctx->parent_ctx); 6781 mutex_lock(&ctx->mutex); 6782 perf_install_in_context(ctx, event, cpu); 6783 ++ctx->generation; 6784 perf_unpin_context(ctx); 6785 mutex_unlock(&ctx->mutex); 6786 6787 return event; 6788 6789 err_free: 6790 free_event(event); 6791 err: 6792 return ERR_PTR(err); 6793 } 6794 EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter); 6795 6796 void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu) 6797 { 6798 struct perf_event_context *src_ctx; 6799 struct perf_event_context *dst_ctx; 6800 struct perf_event *event, *tmp; 6801 LIST_HEAD(events); 6802 6803 src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx; 6804 dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx; 6805 6806 mutex_lock(&src_ctx->mutex); 6807 list_for_each_entry_safe(event, tmp, &src_ctx->event_list, 6808 event_entry) { 6809 perf_remove_from_context(event); 6810 put_ctx(src_ctx); 6811 list_add(&event->event_entry, &events); 6812 } 6813 mutex_unlock(&src_ctx->mutex); 6814 6815 synchronize_rcu(); 6816 6817 mutex_lock(&dst_ctx->mutex); 6818 list_for_each_entry_safe(event, tmp, &events, event_entry) { 6819 list_del(&event->event_entry); 6820 if (event->state >= PERF_EVENT_STATE_OFF) 6821 event->state = PERF_EVENT_STATE_INACTIVE; 6822 perf_install_in_context(dst_ctx, event, dst_cpu); 6823 get_ctx(dst_ctx); 6824 } 6825 mutex_unlock(&dst_ctx->mutex); 6826 } 6827 EXPORT_SYMBOL_GPL(perf_pmu_migrate_context); 6828 6829 static void sync_child_event(struct perf_event *child_event, 6830 struct task_struct *child) 6831 { 6832 struct perf_event *parent_event = child_event->parent; 6833 u64 child_val; 6834 6835 if (child_event->attr.inherit_stat) 6836 perf_event_read_event(child_event, child); 6837 6838 child_val = perf_event_count(child_event); 6839 6840 /* 6841 * Add back the child's count to the parent's count: 6842 */ 6843 atomic64_add(child_val, &parent_event->child_count); 6844 atomic64_add(child_event->total_time_enabled, 6845 &parent_event->child_total_time_enabled); 6846 atomic64_add(child_event->total_time_running, 6847 &parent_event->child_total_time_running); 6848 6849 /* 6850 * Remove this event from the parent's list 6851 */ 6852 WARN_ON_ONCE(parent_event->ctx->parent_ctx); 6853 mutex_lock(&parent_event->child_mutex); 6854 list_del_init(&child_event->child_list); 6855 mutex_unlock(&parent_event->child_mutex); 6856 6857 /* 6858 * Release the parent event, if this was the last 6859 * reference to it. 6860 */ 6861 put_event(parent_event); 6862 } 6863 6864 static void 6865 __perf_event_exit_task(struct perf_event *child_event, 6866 struct perf_event_context *child_ctx, 6867 struct task_struct *child) 6868 { 6869 if (child_event->parent) { 6870 raw_spin_lock_irq(&child_ctx->lock); 6871 perf_group_detach(child_event); 6872 raw_spin_unlock_irq(&child_ctx->lock); 6873 } 6874 6875 perf_remove_from_context(child_event); 6876 6877 /* 6878 * It can happen that the parent exits first, and has events 6879 * that are still around due to the child reference. These 6880 * events need to be zapped. 6881 */ 6882 if (child_event->parent) { 6883 sync_child_event(child_event, child); 6884 free_event(child_event); 6885 } 6886 } 6887 6888 static void perf_event_exit_task_context(struct task_struct *child, int ctxn) 6889 { 6890 struct perf_event *child_event, *tmp; 6891 struct perf_event_context *child_ctx; 6892 unsigned long flags; 6893 6894 if (likely(!child->perf_event_ctxp[ctxn])) { 6895 perf_event_task(child, NULL, 0); 6896 return; 6897 } 6898 6899 local_irq_save(flags); 6900 /* 6901 * We can't reschedule here because interrupts are disabled, 6902 * and either child is current or it is a task that can't be 6903 * scheduled, so we are now safe from rescheduling changing 6904 * our context. 6905 */ 6906 child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]); 6907 6908 /* 6909 * Take the context lock here so that if find_get_context is 6910 * reading child->perf_event_ctxp, we wait until it has 6911 * incremented the context's refcount before we do put_ctx below. 6912 */ 6913 raw_spin_lock(&child_ctx->lock); 6914 task_ctx_sched_out(child_ctx); 6915 child->perf_event_ctxp[ctxn] = NULL; 6916 /* 6917 * If this context is a clone; unclone it so it can't get 6918 * swapped to another process while we're removing all 6919 * the events from it. 6920 */ 6921 unclone_ctx(child_ctx); 6922 update_context_time(child_ctx); 6923 raw_spin_unlock_irqrestore(&child_ctx->lock, flags); 6924 6925 /* 6926 * Report the task dead after unscheduling the events so that we 6927 * won't get any samples after PERF_RECORD_EXIT. We can however still 6928 * get a few PERF_RECORD_READ events. 6929 */ 6930 perf_event_task(child, child_ctx, 0); 6931 6932 /* 6933 * We can recurse on the same lock type through: 6934 * 6935 * __perf_event_exit_task() 6936 * sync_child_event() 6937 * put_event() 6938 * mutex_lock(&ctx->mutex) 6939 * 6940 * But since its the parent context it won't be the same instance. 6941 */ 6942 mutex_lock(&child_ctx->mutex); 6943 6944 again: 6945 list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups, 6946 group_entry) 6947 __perf_event_exit_task(child_event, child_ctx, child); 6948 6949 list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups, 6950 group_entry) 6951 __perf_event_exit_task(child_event, child_ctx, child); 6952 6953 /* 6954 * If the last event was a group event, it will have appended all 6955 * its siblings to the list, but we obtained 'tmp' before that which 6956 * will still point to the list head terminating the iteration. 6957 */ 6958 if (!list_empty(&child_ctx->pinned_groups) || 6959 !list_empty(&child_ctx->flexible_groups)) 6960 goto again; 6961 6962 mutex_unlock(&child_ctx->mutex); 6963 6964 put_ctx(child_ctx); 6965 } 6966 6967 /* 6968 * When a child task exits, feed back event values to parent events. 6969 */ 6970 void perf_event_exit_task(struct task_struct *child) 6971 { 6972 struct perf_event *event, *tmp; 6973 int ctxn; 6974 6975 mutex_lock(&child->perf_event_mutex); 6976 list_for_each_entry_safe(event, tmp, &child->perf_event_list, 6977 owner_entry) { 6978 list_del_init(&event->owner_entry); 6979 6980 /* 6981 * Ensure the list deletion is visible before we clear 6982 * the owner, closes a race against perf_release() where 6983 * we need to serialize on the owner->perf_event_mutex. 6984 */ 6985 smp_wmb(); 6986 event->owner = NULL; 6987 } 6988 mutex_unlock(&child->perf_event_mutex); 6989 6990 for_each_task_context_nr(ctxn) 6991 perf_event_exit_task_context(child, ctxn); 6992 } 6993 6994 static void perf_free_event(struct perf_event *event, 6995 struct perf_event_context *ctx) 6996 { 6997 struct perf_event *parent = event->parent; 6998 6999 if (WARN_ON_ONCE(!parent)) 7000 return; 7001 7002 mutex_lock(&parent->child_mutex); 7003 list_del_init(&event->child_list); 7004 mutex_unlock(&parent->child_mutex); 7005 7006 put_event(parent); 7007 7008 perf_group_detach(event); 7009 list_del_event(event, ctx); 7010 free_event(event); 7011 } 7012 7013 /* 7014 * free an unexposed, unused context as created by inheritance by 7015 * perf_event_init_task below, used by fork() in case of fail. 7016 */ 7017 void perf_event_free_task(struct task_struct *task) 7018 { 7019 struct perf_event_context *ctx; 7020 struct perf_event *event, *tmp; 7021 int ctxn; 7022 7023 for_each_task_context_nr(ctxn) { 7024 ctx = task->perf_event_ctxp[ctxn]; 7025 if (!ctx) 7026 continue; 7027 7028 mutex_lock(&ctx->mutex); 7029 again: 7030 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, 7031 group_entry) 7032 perf_free_event(event, ctx); 7033 7034 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, 7035 group_entry) 7036 perf_free_event(event, ctx); 7037 7038 if (!list_empty(&ctx->pinned_groups) || 7039 !list_empty(&ctx->flexible_groups)) 7040 goto again; 7041 7042 mutex_unlock(&ctx->mutex); 7043 7044 put_ctx(ctx); 7045 } 7046 } 7047 7048 void perf_event_delayed_put(struct task_struct *task) 7049 { 7050 int ctxn; 7051 7052 for_each_task_context_nr(ctxn) 7053 WARN_ON_ONCE(task->perf_event_ctxp[ctxn]); 7054 } 7055 7056 /* 7057 * inherit a event from parent task to child task: 7058 */ 7059 static struct perf_event * 7060 inherit_event(struct perf_event *parent_event, 7061 struct task_struct *parent, 7062 struct perf_event_context *parent_ctx, 7063 struct task_struct *child, 7064 struct perf_event *group_leader, 7065 struct perf_event_context *child_ctx) 7066 { 7067 struct perf_event *child_event; 7068 unsigned long flags; 7069 7070 /* 7071 * Instead of creating recursive hierarchies of events, 7072 * we link inherited events back to the original parent, 7073 * which has a filp for sure, which we use as the reference 7074 * count: 7075 */ 7076 if (parent_event->parent) 7077 parent_event = parent_event->parent; 7078 7079 child_event = perf_event_alloc(&parent_event->attr, 7080 parent_event->cpu, 7081 child, 7082 group_leader, parent_event, 7083 NULL, NULL); 7084 if (IS_ERR(child_event)) 7085 return child_event; 7086 7087 if (!atomic_long_inc_not_zero(&parent_event->refcount)) { 7088 free_event(child_event); 7089 return NULL; 7090 } 7091 7092 get_ctx(child_ctx); 7093 7094 /* 7095 * Make the child state follow the state of the parent event, 7096 * not its attr.disabled bit. We hold the parent's mutex, 7097 * so we won't race with perf_event_{en, dis}able_family. 7098 */ 7099 if (parent_event->state >= PERF_EVENT_STATE_INACTIVE) 7100 child_event->state = PERF_EVENT_STATE_INACTIVE; 7101 else 7102 child_event->state = PERF_EVENT_STATE_OFF; 7103 7104 if (parent_event->attr.freq) { 7105 u64 sample_period = parent_event->hw.sample_period; 7106 struct hw_perf_event *hwc = &child_event->hw; 7107 7108 hwc->sample_period = sample_period; 7109 hwc->last_period = sample_period; 7110 7111 local64_set(&hwc->period_left, sample_period); 7112 } 7113 7114 child_event->ctx = child_ctx; 7115 child_event->overflow_handler = parent_event->overflow_handler; 7116 child_event->overflow_handler_context 7117 = parent_event->overflow_handler_context; 7118 7119 /* 7120 * Precalculate sample_data sizes 7121 */ 7122 perf_event__header_size(child_event); 7123 perf_event__id_header_size(child_event); 7124 7125 /* 7126 * Link it up in the child's context: 7127 */ 7128 raw_spin_lock_irqsave(&child_ctx->lock, flags); 7129 add_event_to_ctx(child_event, child_ctx); 7130 raw_spin_unlock_irqrestore(&child_ctx->lock, flags); 7131 7132 /* 7133 * Link this into the parent event's child list 7134 */ 7135 WARN_ON_ONCE(parent_event->ctx->parent_ctx); 7136 mutex_lock(&parent_event->child_mutex); 7137 list_add_tail(&child_event->child_list, &parent_event->child_list); 7138 mutex_unlock(&parent_event->child_mutex); 7139 7140 return child_event; 7141 } 7142 7143 static int inherit_group(struct perf_event *parent_event, 7144 struct task_struct *parent, 7145 struct perf_event_context *parent_ctx, 7146 struct task_struct *child, 7147 struct perf_event_context *child_ctx) 7148 { 7149 struct perf_event *leader; 7150 struct perf_event *sub; 7151 struct perf_event *child_ctr; 7152 7153 leader = inherit_event(parent_event, parent, parent_ctx, 7154 child, NULL, child_ctx); 7155 if (IS_ERR(leader)) 7156 return PTR_ERR(leader); 7157 list_for_each_entry(sub, &parent_event->sibling_list, group_entry) { 7158 child_ctr = inherit_event(sub, parent, parent_ctx, 7159 child, leader, child_ctx); 7160 if (IS_ERR(child_ctr)) 7161 return PTR_ERR(child_ctr); 7162 } 7163 return 0; 7164 } 7165 7166 static int 7167 inherit_task_group(struct perf_event *event, struct task_struct *parent, 7168 struct perf_event_context *parent_ctx, 7169 struct task_struct *child, int ctxn, 7170 int *inherited_all) 7171 { 7172 int ret; 7173 struct perf_event_context *child_ctx; 7174 7175 if (!event->attr.inherit) { 7176 *inherited_all = 0; 7177 return 0; 7178 } 7179 7180 child_ctx = child->perf_event_ctxp[ctxn]; 7181 if (!child_ctx) { 7182 /* 7183 * This is executed from the parent task context, so 7184 * inherit events that have been marked for cloning. 7185 * First allocate and initialize a context for the 7186 * child. 7187 */ 7188 7189 child_ctx = alloc_perf_context(event->pmu, child); 7190 if (!child_ctx) 7191 return -ENOMEM; 7192 7193 child->perf_event_ctxp[ctxn] = child_ctx; 7194 } 7195 7196 ret = inherit_group(event, parent, parent_ctx, 7197 child, child_ctx); 7198 7199 if (ret) 7200 *inherited_all = 0; 7201 7202 return ret; 7203 } 7204 7205 /* 7206 * Initialize the perf_event context in task_struct 7207 */ 7208 int perf_event_init_context(struct task_struct *child, int ctxn) 7209 { 7210 struct perf_event_context *child_ctx, *parent_ctx; 7211 struct perf_event_context *cloned_ctx; 7212 struct perf_event *event; 7213 struct task_struct *parent = current; 7214 int inherited_all = 1; 7215 unsigned long flags; 7216 int ret = 0; 7217 7218 if (likely(!parent->perf_event_ctxp[ctxn])) 7219 return 0; 7220 7221 /* 7222 * If the parent's context is a clone, pin it so it won't get 7223 * swapped under us. 7224 */ 7225 parent_ctx = perf_pin_task_context(parent, ctxn); 7226 7227 /* 7228 * No need to check if parent_ctx != NULL here; since we saw 7229 * it non-NULL earlier, the only reason for it to become NULL 7230 * is if we exit, and since we're currently in the middle of 7231 * a fork we can't be exiting at the same time. 7232 */ 7233 7234 /* 7235 * Lock the parent list. No need to lock the child - not PID 7236 * hashed yet and not running, so nobody can access it. 7237 */ 7238 mutex_lock(&parent_ctx->mutex); 7239 7240 /* 7241 * We dont have to disable NMIs - we are only looking at 7242 * the list, not manipulating it: 7243 */ 7244 list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) { 7245 ret = inherit_task_group(event, parent, parent_ctx, 7246 child, ctxn, &inherited_all); 7247 if (ret) 7248 break; 7249 } 7250 7251 /* 7252 * We can't hold ctx->lock when iterating the ->flexible_group list due 7253 * to allocations, but we need to prevent rotation because 7254 * rotate_ctx() will change the list from interrupt context. 7255 */ 7256 raw_spin_lock_irqsave(&parent_ctx->lock, flags); 7257 parent_ctx->rotate_disable = 1; 7258 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); 7259 7260 list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) { 7261 ret = inherit_task_group(event, parent, parent_ctx, 7262 child, ctxn, &inherited_all); 7263 if (ret) 7264 break; 7265 } 7266 7267 raw_spin_lock_irqsave(&parent_ctx->lock, flags); 7268 parent_ctx->rotate_disable = 0; 7269 7270 child_ctx = child->perf_event_ctxp[ctxn]; 7271 7272 if (child_ctx && inherited_all) { 7273 /* 7274 * Mark the child context as a clone of the parent 7275 * context, or of whatever the parent is a clone of. 7276 * 7277 * Note that if the parent is a clone, the holding of 7278 * parent_ctx->lock avoids it from being uncloned. 7279 */ 7280 cloned_ctx = parent_ctx->parent_ctx; 7281 if (cloned_ctx) { 7282 child_ctx->parent_ctx = cloned_ctx; 7283 child_ctx->parent_gen = parent_ctx->parent_gen; 7284 } else { 7285 child_ctx->parent_ctx = parent_ctx; 7286 child_ctx->parent_gen = parent_ctx->generation; 7287 } 7288 get_ctx(child_ctx->parent_ctx); 7289 } 7290 7291 raw_spin_unlock_irqrestore(&parent_ctx->lock, flags); 7292 mutex_unlock(&parent_ctx->mutex); 7293 7294 perf_unpin_context(parent_ctx); 7295 put_ctx(parent_ctx); 7296 7297 return ret; 7298 } 7299 7300 /* 7301 * Initialize the perf_event context in task_struct 7302 */ 7303 int perf_event_init_task(struct task_struct *child) 7304 { 7305 int ctxn, ret; 7306 7307 memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp)); 7308 mutex_init(&child->perf_event_mutex); 7309 INIT_LIST_HEAD(&child->perf_event_list); 7310 7311 for_each_task_context_nr(ctxn) { 7312 ret = perf_event_init_context(child, ctxn); 7313 if (ret) 7314 return ret; 7315 } 7316 7317 return 0; 7318 } 7319 7320 static void __init perf_event_init_all_cpus(void) 7321 { 7322 struct swevent_htable *swhash; 7323 int cpu; 7324 7325 for_each_possible_cpu(cpu) { 7326 swhash = &per_cpu(swevent_htable, cpu); 7327 mutex_init(&swhash->hlist_mutex); 7328 INIT_LIST_HEAD(&per_cpu(rotation_list, cpu)); 7329 } 7330 } 7331 7332 static void __cpuinit perf_event_init_cpu(int cpu) 7333 { 7334 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); 7335 7336 mutex_lock(&swhash->hlist_mutex); 7337 if (swhash->hlist_refcount > 0) { 7338 struct swevent_hlist *hlist; 7339 7340 hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu)); 7341 WARN_ON(!hlist); 7342 rcu_assign_pointer(swhash->swevent_hlist, hlist); 7343 } 7344 mutex_unlock(&swhash->hlist_mutex); 7345 } 7346 7347 #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC 7348 static void perf_pmu_rotate_stop(struct pmu *pmu) 7349 { 7350 struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context); 7351 7352 WARN_ON(!irqs_disabled()); 7353 7354 list_del_init(&cpuctx->rotation_list); 7355 } 7356 7357 static void __perf_event_exit_context(void *__info) 7358 { 7359 struct perf_event_context *ctx = __info; 7360 struct perf_event *event, *tmp; 7361 7362 perf_pmu_rotate_stop(ctx->pmu); 7363 7364 list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry) 7365 __perf_remove_from_context(event); 7366 list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry) 7367 __perf_remove_from_context(event); 7368 } 7369 7370 static void perf_event_exit_cpu_context(int cpu) 7371 { 7372 struct perf_event_context *ctx; 7373 struct pmu *pmu; 7374 int idx; 7375 7376 idx = srcu_read_lock(&pmus_srcu); 7377 list_for_each_entry_rcu(pmu, &pmus, entry) { 7378 ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx; 7379 7380 mutex_lock(&ctx->mutex); 7381 smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1); 7382 mutex_unlock(&ctx->mutex); 7383 } 7384 srcu_read_unlock(&pmus_srcu, idx); 7385 } 7386 7387 static void perf_event_exit_cpu(int cpu) 7388 { 7389 struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu); 7390 7391 mutex_lock(&swhash->hlist_mutex); 7392 swevent_hlist_release(swhash); 7393 mutex_unlock(&swhash->hlist_mutex); 7394 7395 perf_event_exit_cpu_context(cpu); 7396 } 7397 #else 7398 static inline void perf_event_exit_cpu(int cpu) { } 7399 #endif 7400 7401 static int 7402 perf_reboot(struct notifier_block *notifier, unsigned long val, void *v) 7403 { 7404 int cpu; 7405 7406 for_each_online_cpu(cpu) 7407 perf_event_exit_cpu(cpu); 7408 7409 return NOTIFY_OK; 7410 } 7411 7412 /* 7413 * Run the perf reboot notifier at the very last possible moment so that 7414 * the generic watchdog code runs as long as possible. 7415 */ 7416 static struct notifier_block perf_reboot_notifier = { 7417 .notifier_call = perf_reboot, 7418 .priority = INT_MIN, 7419 }; 7420 7421 static int __cpuinit 7422 perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu) 7423 { 7424 unsigned int cpu = (long)hcpu; 7425 7426 switch (action & ~CPU_TASKS_FROZEN) { 7427 7428 case CPU_UP_PREPARE: 7429 case CPU_DOWN_FAILED: 7430 perf_event_init_cpu(cpu); 7431 break; 7432 7433 case CPU_UP_CANCELED: 7434 case CPU_DOWN_PREPARE: 7435 perf_event_exit_cpu(cpu); 7436 break; 7437 7438 default: 7439 break; 7440 } 7441 7442 return NOTIFY_OK; 7443 } 7444 7445 void __init perf_event_init(void) 7446 { 7447 int ret; 7448 7449 idr_init(&pmu_idr); 7450 7451 perf_event_init_all_cpus(); 7452 init_srcu_struct(&pmus_srcu); 7453 perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE); 7454 perf_pmu_register(&perf_cpu_clock, NULL, -1); 7455 perf_pmu_register(&perf_task_clock, NULL, -1); 7456 perf_tp_register(); 7457 perf_cpu_notifier(perf_cpu_notify); 7458 register_reboot_notifier(&perf_reboot_notifier); 7459 7460 ret = init_hw_breakpoint(); 7461 WARN(ret, "hw_breakpoint initialization failed with: %d", ret); 7462 7463 /* do not patch jump label more than once per second */ 7464 jump_label_rate_limit(&perf_sched_events, HZ); 7465 7466 /* 7467 * Build time assertion that we keep the data_head at the intended 7468 * location. IOW, validation we got the __reserved[] size right. 7469 */ 7470 BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head)) 7471 != 1024); 7472 } 7473 7474 static int __init perf_event_sysfs_init(void) 7475 { 7476 struct pmu *pmu; 7477 int ret; 7478 7479 mutex_lock(&pmus_lock); 7480 7481 ret = bus_register(&pmu_bus); 7482 if (ret) 7483 goto unlock; 7484 7485 list_for_each_entry(pmu, &pmus, entry) { 7486 if (!pmu->name || pmu->type < 0) 7487 continue; 7488 7489 ret = pmu_dev_alloc(pmu); 7490 WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret); 7491 } 7492 pmu_bus_running = 1; 7493 ret = 0; 7494 7495 unlock: 7496 mutex_unlock(&pmus_lock); 7497 7498 return ret; 7499 } 7500 device_initcall(perf_event_sysfs_init); 7501 7502 #ifdef CONFIG_CGROUP_PERF 7503 static struct cgroup_subsys_state *perf_cgroup_css_alloc(struct cgroup *cont) 7504 { 7505 struct perf_cgroup *jc; 7506 7507 jc = kzalloc(sizeof(*jc), GFP_KERNEL); 7508 if (!jc) 7509 return ERR_PTR(-ENOMEM); 7510 7511 jc->info = alloc_percpu(struct perf_cgroup_info); 7512 if (!jc->info) { 7513 kfree(jc); 7514 return ERR_PTR(-ENOMEM); 7515 } 7516 7517 return &jc->css; 7518 } 7519 7520 static void perf_cgroup_css_free(struct cgroup *cont) 7521 { 7522 struct perf_cgroup *jc; 7523 jc = container_of(cgroup_subsys_state(cont, perf_subsys_id), 7524 struct perf_cgroup, css); 7525 free_percpu(jc->info); 7526 kfree(jc); 7527 } 7528 7529 static int __perf_cgroup_move(void *info) 7530 { 7531 struct task_struct *task = info; 7532 perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN); 7533 return 0; 7534 } 7535 7536 static void perf_cgroup_attach(struct cgroup *cgrp, struct cgroup_taskset *tset) 7537 { 7538 struct task_struct *task; 7539 7540 cgroup_taskset_for_each(task, cgrp, tset) 7541 task_function_call(task, __perf_cgroup_move, task); 7542 } 7543 7544 static void perf_cgroup_exit(struct cgroup *cgrp, struct cgroup *old_cgrp, 7545 struct task_struct *task) 7546 { 7547 /* 7548 * cgroup_exit() is called in the copy_process() failure path. 7549 * Ignore this case since the task hasn't ran yet, this avoids 7550 * trying to poke a half freed task state from generic code. 7551 */ 7552 if (!(task->flags & PF_EXITING)) 7553 return; 7554 7555 task_function_call(task, __perf_cgroup_move, task); 7556 } 7557 7558 struct cgroup_subsys perf_subsys = { 7559 .name = "perf_event", 7560 .subsys_id = perf_subsys_id, 7561 .css_alloc = perf_cgroup_css_alloc, 7562 .css_free = perf_cgroup_css_free, 7563 .exit = perf_cgroup_exit, 7564 .attach = perf_cgroup_attach, 7565 }; 7566 #endif /* CONFIG_CGROUP_PERF */ 7567