19251f904SBorislav Petkov /* 29251f904SBorislav Petkov * Performance events callchain code, extracted from core.c: 39251f904SBorislav Petkov * 49251f904SBorislav Petkov * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de> 59251f904SBorislav Petkov * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar 690eec103SPeter Zijlstra * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra 79251f904SBorislav Petkov * Copyright � 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> 89251f904SBorislav Petkov * 99251f904SBorislav Petkov * For licensing details see kernel-base/COPYING 109251f904SBorislav Petkov */ 119251f904SBorislav Petkov 129251f904SBorislav Petkov #include <linux/perf_event.h> 139251f904SBorislav Petkov #include <linux/slab.h> 149251f904SBorislav Petkov #include "internal.h" 159251f904SBorislav Petkov 169251f904SBorislav Petkov struct callchain_cpus_entries { 179251f904SBorislav Petkov struct rcu_head rcu_head; 189251f904SBorislav Petkov struct perf_callchain_entry *cpu_entries[0]; 199251f904SBorislav Petkov }; 209251f904SBorislav Petkov 21c5dfd78eSArnaldo Carvalho de Melo int sysctl_perf_event_max_stack __read_mostly = PERF_MAX_STACK_DEPTH; 22c5dfd78eSArnaldo Carvalho de Melo 23c5dfd78eSArnaldo Carvalho de Melo static inline size_t perf_callchain_entry__sizeof(void) 24c5dfd78eSArnaldo Carvalho de Melo { 25c5dfd78eSArnaldo Carvalho de Melo return (sizeof(struct perf_callchain_entry) + 26c5dfd78eSArnaldo Carvalho de Melo sizeof(__u64) * sysctl_perf_event_max_stack); 27c5dfd78eSArnaldo Carvalho de Melo } 28c5dfd78eSArnaldo Carvalho de Melo 299251f904SBorislav Petkov static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]); 309251f904SBorislav Petkov static atomic_t nr_callchain_events; 319251f904SBorislav Petkov static DEFINE_MUTEX(callchain_mutex); 329251f904SBorislav Petkov static struct callchain_cpus_entries *callchain_cpus_entries; 339251f904SBorislav Petkov 349251f904SBorislav Petkov 35cfbcf468SArnaldo Carvalho de Melo __weak void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, 369251f904SBorislav Petkov struct pt_regs *regs) 379251f904SBorislav Petkov { 389251f904SBorislav Petkov } 399251f904SBorislav Petkov 40cfbcf468SArnaldo Carvalho de Melo __weak void perf_callchain_user(struct perf_callchain_entry_ctx *entry, 419251f904SBorislav Petkov struct pt_regs *regs) 429251f904SBorislav Petkov { 439251f904SBorislav Petkov } 449251f904SBorislav Petkov 459251f904SBorislav Petkov static void release_callchain_buffers_rcu(struct rcu_head *head) 469251f904SBorislav Petkov { 479251f904SBorislav Petkov struct callchain_cpus_entries *entries; 489251f904SBorislav Petkov int cpu; 499251f904SBorislav Petkov 509251f904SBorislav Petkov entries = container_of(head, struct callchain_cpus_entries, rcu_head); 519251f904SBorislav Petkov 529251f904SBorislav Petkov for_each_possible_cpu(cpu) 539251f904SBorislav Petkov kfree(entries->cpu_entries[cpu]); 549251f904SBorislav Petkov 559251f904SBorislav Petkov kfree(entries); 569251f904SBorislav Petkov } 579251f904SBorislav Petkov 589251f904SBorislav Petkov static void release_callchain_buffers(void) 599251f904SBorislav Petkov { 609251f904SBorislav Petkov struct callchain_cpus_entries *entries; 619251f904SBorislav Petkov 629251f904SBorislav Petkov entries = callchain_cpus_entries; 63e0455e19SAndreea-Cristina Bernat RCU_INIT_POINTER(callchain_cpus_entries, NULL); 649251f904SBorislav Petkov call_rcu(&entries->rcu_head, release_callchain_buffers_rcu); 659251f904SBorislav Petkov } 669251f904SBorislav Petkov 679251f904SBorislav Petkov static int alloc_callchain_buffers(void) 689251f904SBorislav Petkov { 699251f904SBorislav Petkov int cpu; 709251f904SBorislav Petkov int size; 719251f904SBorislav Petkov struct callchain_cpus_entries *entries; 729251f904SBorislav Petkov 739251f904SBorislav Petkov /* 749251f904SBorislav Petkov * We can't use the percpu allocation API for data that can be 759251f904SBorislav Petkov * accessed from NMI. Use a temporary manual per cpu allocation 769251f904SBorislav Petkov * until that gets sorted out. 779251f904SBorislav Petkov */ 789251f904SBorislav Petkov size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]); 799251f904SBorislav Petkov 809251f904SBorislav Petkov entries = kzalloc(size, GFP_KERNEL); 819251f904SBorislav Petkov if (!entries) 829251f904SBorislav Petkov return -ENOMEM; 839251f904SBorislav Petkov 84c5dfd78eSArnaldo Carvalho de Melo size = perf_callchain_entry__sizeof() * PERF_NR_CONTEXTS; 859251f904SBorislav Petkov 869251f904SBorislav Petkov for_each_possible_cpu(cpu) { 879251f904SBorislav Petkov entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL, 889251f904SBorislav Petkov cpu_to_node(cpu)); 899251f904SBorislav Petkov if (!entries->cpu_entries[cpu]) 909251f904SBorislav Petkov goto fail; 919251f904SBorislav Petkov } 929251f904SBorislav Petkov 939251f904SBorislav Petkov rcu_assign_pointer(callchain_cpus_entries, entries); 949251f904SBorislav Petkov 959251f904SBorislav Petkov return 0; 969251f904SBorislav Petkov 979251f904SBorislav Petkov fail: 989251f904SBorislav Petkov for_each_possible_cpu(cpu) 999251f904SBorislav Petkov kfree(entries->cpu_entries[cpu]); 1009251f904SBorislav Petkov kfree(entries); 1019251f904SBorislav Petkov 1029251f904SBorislav Petkov return -ENOMEM; 1039251f904SBorislav Petkov } 1049251f904SBorislav Petkov 1059251f904SBorislav Petkov int get_callchain_buffers(void) 1069251f904SBorislav Petkov { 1079251f904SBorislav Petkov int err = 0; 1089251f904SBorislav Petkov int count; 1099251f904SBorislav Petkov 1109251f904SBorislav Petkov mutex_lock(&callchain_mutex); 1119251f904SBorislav Petkov 1129251f904SBorislav Petkov count = atomic_inc_return(&nr_callchain_events); 1139251f904SBorislav Petkov if (WARN_ON_ONCE(count < 1)) { 1149251f904SBorislav Petkov err = -EINVAL; 1159251f904SBorislav Petkov goto exit; 1169251f904SBorislav Petkov } 1179251f904SBorislav Petkov 1189251f904SBorislav Petkov if (count > 1) { 1199251f904SBorislav Petkov /* If the allocation failed, give up */ 1209251f904SBorislav Petkov if (!callchain_cpus_entries) 1219251f904SBorislav Petkov err = -ENOMEM; 1229251f904SBorislav Petkov goto exit; 1239251f904SBorislav Petkov } 1249251f904SBorislav Petkov 1259251f904SBorislav Petkov err = alloc_callchain_buffers(); 1269251f904SBorislav Petkov exit: 12790983b16SFrederic Weisbecker if (err) 12890983b16SFrederic Weisbecker atomic_dec(&nr_callchain_events); 1299251f904SBorislav Petkov 130fc3b86d6SFrederic Weisbecker mutex_unlock(&callchain_mutex); 131fc3b86d6SFrederic Weisbecker 1329251f904SBorislav Petkov return err; 1339251f904SBorislav Petkov } 1349251f904SBorislav Petkov 1359251f904SBorislav Petkov void put_callchain_buffers(void) 1369251f904SBorislav Petkov { 1379251f904SBorislav Petkov if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) { 1389251f904SBorislav Petkov release_callchain_buffers(); 1399251f904SBorislav Petkov mutex_unlock(&callchain_mutex); 1409251f904SBorislav Petkov } 1419251f904SBorislav Petkov } 1429251f904SBorislav Petkov 1439251f904SBorislav Petkov static struct perf_callchain_entry *get_callchain_entry(int *rctx) 1449251f904SBorislav Petkov { 1459251f904SBorislav Petkov int cpu; 1469251f904SBorislav Petkov struct callchain_cpus_entries *entries; 1479251f904SBorislav Petkov 1484a32fea9SChristoph Lameter *rctx = get_recursion_context(this_cpu_ptr(callchain_recursion)); 1499251f904SBorislav Petkov if (*rctx == -1) 1509251f904SBorislav Petkov return NULL; 1519251f904SBorislav Petkov 1529251f904SBorislav Petkov entries = rcu_dereference(callchain_cpus_entries); 1539251f904SBorislav Petkov if (!entries) 1549251f904SBorislav Petkov return NULL; 1559251f904SBorislav Petkov 1569251f904SBorislav Petkov cpu = smp_processor_id(); 1579251f904SBorislav Petkov 158c5dfd78eSArnaldo Carvalho de Melo return (((void *)entries->cpu_entries[cpu]) + 159c5dfd78eSArnaldo Carvalho de Melo (*rctx * perf_callchain_entry__sizeof())); 1609251f904SBorislav Petkov } 1619251f904SBorislav Petkov 1629251f904SBorislav Petkov static void 1639251f904SBorislav Petkov put_callchain_entry(int rctx) 1649251f904SBorislav Petkov { 1654a32fea9SChristoph Lameter put_recursion_context(this_cpu_ptr(callchain_recursion), rctx); 1669251f904SBorislav Petkov } 1679251f904SBorislav Petkov 168e6dab5ffSAndrew Vagin struct perf_callchain_entry * 169e6dab5ffSAndrew Vagin perf_callchain(struct perf_event *event, struct pt_regs *regs) 1709251f904SBorislav Petkov { 171568b329aSAlexei Starovoitov bool kernel = !event->attr.exclude_callchain_kernel; 172568b329aSAlexei Starovoitov bool user = !event->attr.exclude_callchain_user; 173568b329aSAlexei Starovoitov /* Disallow cross-task user callchains. */ 174568b329aSAlexei Starovoitov bool crosstask = event->ctx->task && event->ctx->task != current; 175d0775264SFrederic Weisbecker 176d0775264SFrederic Weisbecker if (!kernel && !user) 177d0775264SFrederic Weisbecker return NULL; 1789251f904SBorislav Petkov 179cfbcf468SArnaldo Carvalho de Melo return get_perf_callchain(regs, 0, kernel, user, sysctl_perf_event_max_stack, crosstask, true); 180568b329aSAlexei Starovoitov } 181568b329aSAlexei Starovoitov 182568b329aSAlexei Starovoitov struct perf_callchain_entry * 183568b329aSAlexei Starovoitov get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user, 184cfbcf468SArnaldo Carvalho de Melo u32 max_stack, bool crosstask, bool add_mark) 185568b329aSAlexei Starovoitov { 186568b329aSAlexei Starovoitov struct perf_callchain_entry *entry; 187cfbcf468SArnaldo Carvalho de Melo struct perf_callchain_entry_ctx ctx; 188568b329aSAlexei Starovoitov int rctx; 189568b329aSAlexei Starovoitov 1909251f904SBorislav Petkov entry = get_callchain_entry(&rctx); 1919251f904SBorislav Petkov if (rctx == -1) 1929251f904SBorislav Petkov return NULL; 1939251f904SBorislav Petkov 1949251f904SBorislav Petkov if (!entry) 1959251f904SBorislav Petkov goto exit_put; 1969251f904SBorislav Petkov 197cfbcf468SArnaldo Carvalho de Melo ctx.entry = entry; 198cfbcf468SArnaldo Carvalho de Melo ctx.max_stack = max_stack; 199*3b1fff08SArnaldo Carvalho de Melo ctx.nr = entry->nr = init_nr; 2009251f904SBorislav Petkov 201d0775264SFrederic Weisbecker if (kernel && !user_mode(regs)) { 202568b329aSAlexei Starovoitov if (add_mark) 203cfbcf468SArnaldo Carvalho de Melo perf_callchain_store(&ctx, PERF_CONTEXT_KERNEL); 204cfbcf468SArnaldo Carvalho de Melo perf_callchain_kernel(&ctx, regs); 205d0775264SFrederic Weisbecker } 206d0775264SFrederic Weisbecker 207d0775264SFrederic Weisbecker if (user) { 208d0775264SFrederic Weisbecker if (!user_mode(regs)) { 2099251f904SBorislav Petkov if (current->mm) 2109251f904SBorislav Petkov regs = task_pt_regs(current); 2119251f904SBorislav Petkov else 2129251f904SBorislav Petkov regs = NULL; 2139251f904SBorislav Petkov } 2149251f904SBorislav Petkov 2159251f904SBorislav Petkov if (regs) { 216568b329aSAlexei Starovoitov if (crosstask) 217e6dab5ffSAndrew Vagin goto exit_put; 218e6dab5ffSAndrew Vagin 219568b329aSAlexei Starovoitov if (add_mark) 220cfbcf468SArnaldo Carvalho de Melo perf_callchain_store(&ctx, PERF_CONTEXT_USER); 221cfbcf468SArnaldo Carvalho de Melo perf_callchain_user(&ctx, regs); 2229251f904SBorislav Petkov } 223d0775264SFrederic Weisbecker } 2249251f904SBorislav Petkov 2259251f904SBorislav Petkov exit_put: 2269251f904SBorislav Petkov put_callchain_entry(rctx); 2279251f904SBorislav Petkov 2289251f904SBorislav Petkov return entry; 2299251f904SBorislav Petkov } 230c5dfd78eSArnaldo Carvalho de Melo 231c5dfd78eSArnaldo Carvalho de Melo int perf_event_max_stack_handler(struct ctl_table *table, int write, 232c5dfd78eSArnaldo Carvalho de Melo void __user *buffer, size_t *lenp, loff_t *ppos) 233c5dfd78eSArnaldo Carvalho de Melo { 234a831100aSArnaldo Carvalho de Melo int *value = table->data; 235a831100aSArnaldo Carvalho de Melo int new_value = *value, ret; 236c5dfd78eSArnaldo Carvalho de Melo struct ctl_table new_table = *table; 237c5dfd78eSArnaldo Carvalho de Melo 238c5dfd78eSArnaldo Carvalho de Melo new_table.data = &new_value; 239c5dfd78eSArnaldo Carvalho de Melo ret = proc_dointvec_minmax(&new_table, write, buffer, lenp, ppos); 240c5dfd78eSArnaldo Carvalho de Melo if (ret || !write) 241c5dfd78eSArnaldo Carvalho de Melo return ret; 242c5dfd78eSArnaldo Carvalho de Melo 243c5dfd78eSArnaldo Carvalho de Melo mutex_lock(&callchain_mutex); 244c5dfd78eSArnaldo Carvalho de Melo if (atomic_read(&nr_callchain_events)) 245c5dfd78eSArnaldo Carvalho de Melo ret = -EBUSY; 246c5dfd78eSArnaldo Carvalho de Melo else 247a831100aSArnaldo Carvalho de Melo *value = new_value; 248c5dfd78eSArnaldo Carvalho de Melo 249c5dfd78eSArnaldo Carvalho de Melo mutex_unlock(&callchain_mutex); 250c5dfd78eSArnaldo Carvalho de Melo 251c5dfd78eSArnaldo Carvalho de Melo return ret; 252c5dfd78eSArnaldo Carvalho de Melo } 253