xref: /openbmc/linux/kernel/events/callchain.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
18e86e015SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
29251f904SBorislav Petkov /*
39251f904SBorislav Petkov  * Performance events callchain code, extracted from core.c:
49251f904SBorislav Petkov  *
59251f904SBorislav Petkov  *  Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
69251f904SBorislav Petkov  *  Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
790eec103SPeter Zijlstra  *  Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra
83723c632SArnd Bergmann  *  Copyright  ©  2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
99251f904SBorislav Petkov  */
109251f904SBorislav Petkov 
119251f904SBorislav Petkov #include <linux/perf_event.h>
129251f904SBorislav Petkov #include <linux/slab.h>
1368db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
1468db0cf1SIngo Molnar 
159251f904SBorislav Petkov #include "internal.h"
169251f904SBorislav Petkov 
179251f904SBorislav Petkov struct callchain_cpus_entries {
189251f904SBorislav Petkov 	struct rcu_head			rcu_head;
19c50c75e9SGustavo A. R. Silva 	struct perf_callchain_entry	*cpu_entries[];
209251f904SBorislav Petkov };
219251f904SBorislav Petkov 
22c5dfd78eSArnaldo Carvalho de Melo int sysctl_perf_event_max_stack __read_mostly = PERF_MAX_STACK_DEPTH;
23c85b0334SArnaldo Carvalho de Melo int sysctl_perf_event_max_contexts_per_stack __read_mostly = PERF_MAX_CONTEXTS_PER_STACK;
24c5dfd78eSArnaldo Carvalho de Melo 
perf_callchain_entry__sizeof(void)25c5dfd78eSArnaldo Carvalho de Melo static inline size_t perf_callchain_entry__sizeof(void)
26c5dfd78eSArnaldo Carvalho de Melo {
27c5dfd78eSArnaldo Carvalho de Melo 	return (sizeof(struct perf_callchain_entry) +
28c85b0334SArnaldo Carvalho de Melo 		sizeof(__u64) * (sysctl_perf_event_max_stack +
29c85b0334SArnaldo Carvalho de Melo 				 sysctl_perf_event_max_contexts_per_stack));
30c5dfd78eSArnaldo Carvalho de Melo }
31c5dfd78eSArnaldo Carvalho de Melo 
329251f904SBorislav Petkov static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
339251f904SBorislav Petkov static atomic_t nr_callchain_events;
349251f904SBorislav Petkov static DEFINE_MUTEX(callchain_mutex);
359251f904SBorislav Petkov static struct callchain_cpus_entries *callchain_cpus_entries;
369251f904SBorislav Petkov 
379251f904SBorislav Petkov 
perf_callchain_kernel(struct perf_callchain_entry_ctx * entry,struct pt_regs * regs)38cfbcf468SArnaldo Carvalho de Melo __weak void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
399251f904SBorislav Petkov 				  struct pt_regs *regs)
409251f904SBorislav Petkov {
419251f904SBorislav Petkov }
429251f904SBorislav Petkov 
perf_callchain_user(struct perf_callchain_entry_ctx * entry,struct pt_regs * regs)43cfbcf468SArnaldo Carvalho de Melo __weak void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
449251f904SBorislav Petkov 				struct pt_regs *regs)
459251f904SBorislav Petkov {
469251f904SBorislav Petkov }
479251f904SBorislav Petkov 
release_callchain_buffers_rcu(struct rcu_head * head)489251f904SBorislav Petkov static void release_callchain_buffers_rcu(struct rcu_head *head)
499251f904SBorislav Petkov {
509251f904SBorislav Petkov 	struct callchain_cpus_entries *entries;
519251f904SBorislav Petkov 	int cpu;
529251f904SBorislav Petkov 
539251f904SBorislav Petkov 	entries = container_of(head, struct callchain_cpus_entries, rcu_head);
549251f904SBorislav Petkov 
559251f904SBorislav Petkov 	for_each_possible_cpu(cpu)
569251f904SBorislav Petkov 		kfree(entries->cpu_entries[cpu]);
579251f904SBorislav Petkov 
589251f904SBorislav Petkov 	kfree(entries);
599251f904SBorislav Petkov }
609251f904SBorislav Petkov 
release_callchain_buffers(void)619251f904SBorislav Petkov static void release_callchain_buffers(void)
629251f904SBorislav Petkov {
639251f904SBorislav Petkov 	struct callchain_cpus_entries *entries;
649251f904SBorislav Petkov 
659251f904SBorislav Petkov 	entries = callchain_cpus_entries;
66e0455e19SAndreea-Cristina Bernat 	RCU_INIT_POINTER(callchain_cpus_entries, NULL);
679251f904SBorislav Petkov 	call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
689251f904SBorislav Petkov }
699251f904SBorislav Petkov 
alloc_callchain_buffers(void)709251f904SBorislav Petkov static int alloc_callchain_buffers(void)
719251f904SBorislav Petkov {
729251f904SBorislav Petkov 	int cpu;
739251f904SBorislav Petkov 	int size;
749251f904SBorislav Petkov 	struct callchain_cpus_entries *entries;
759251f904SBorislav Petkov 
769251f904SBorislav Petkov 	/*
779251f904SBorislav Petkov 	 * We can't use the percpu allocation API for data that can be
789251f904SBorislav Petkov 	 * accessed from NMI. Use a temporary manual per cpu allocation
799251f904SBorislav Petkov 	 * until that gets sorted out.
809251f904SBorislav Petkov 	 */
819251f904SBorislav Petkov 	size = offsetof(struct callchain_cpus_entries, cpu_entries[nr_cpu_ids]);
829251f904SBorislav Petkov 
839251f904SBorislav Petkov 	entries = kzalloc(size, GFP_KERNEL);
849251f904SBorislav Petkov 	if (!entries)
859251f904SBorislav Petkov 		return -ENOMEM;
869251f904SBorislav Petkov 
87c5dfd78eSArnaldo Carvalho de Melo 	size = perf_callchain_entry__sizeof() * PERF_NR_CONTEXTS;
889251f904SBorislav Petkov 
899251f904SBorislav Petkov 	for_each_possible_cpu(cpu) {
909251f904SBorislav Petkov 		entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
919251f904SBorislav Petkov 							 cpu_to_node(cpu));
929251f904SBorislav Petkov 		if (!entries->cpu_entries[cpu])
939251f904SBorislav Petkov 			goto fail;
949251f904SBorislav Petkov 	}
959251f904SBorislav Petkov 
969251f904SBorislav Petkov 	rcu_assign_pointer(callchain_cpus_entries, entries);
979251f904SBorislav Petkov 
989251f904SBorislav Petkov 	return 0;
999251f904SBorislav Petkov 
1009251f904SBorislav Petkov fail:
1019251f904SBorislav Petkov 	for_each_possible_cpu(cpu)
1029251f904SBorislav Petkov 		kfree(entries->cpu_entries[cpu]);
1039251f904SBorislav Petkov 	kfree(entries);
1049251f904SBorislav Petkov 
1059251f904SBorislav Petkov 	return -ENOMEM;
1069251f904SBorislav Petkov }
1079251f904SBorislav Petkov 
get_callchain_buffers(int event_max_stack)10897c79a38SArnaldo Carvalho de Melo int get_callchain_buffers(int event_max_stack)
1099251f904SBorislav Petkov {
1109251f904SBorislav Petkov 	int err = 0;
1119251f904SBorislav Petkov 	int count;
1129251f904SBorislav Petkov 
1139251f904SBorislav Petkov 	mutex_lock(&callchain_mutex);
1149251f904SBorislav Petkov 
1159251f904SBorislav Petkov 	count = atomic_inc_return(&nr_callchain_events);
1169251f904SBorislav Petkov 	if (WARN_ON_ONCE(count < 1)) {
1179251f904SBorislav Petkov 		err = -EINVAL;
1189251f904SBorislav Petkov 		goto exit;
1199251f904SBorislav Petkov 	}
1209251f904SBorislav Petkov 
12197c79a38SArnaldo Carvalho de Melo 	/*
12297c79a38SArnaldo Carvalho de Melo 	 * If requesting per event more than the global cap,
12397c79a38SArnaldo Carvalho de Melo 	 * return a different error to help userspace figure
12497c79a38SArnaldo Carvalho de Melo 	 * this out.
12597c79a38SArnaldo Carvalho de Melo 	 *
12697c79a38SArnaldo Carvalho de Melo 	 * And also do it here so that we have &callchain_mutex held.
12797c79a38SArnaldo Carvalho de Melo 	 */
1285af44ca5SJiri Olsa 	if (event_max_stack > sysctl_perf_event_max_stack) {
12997c79a38SArnaldo Carvalho de Melo 		err = -EOVERFLOW;
1309251f904SBorislav Petkov 		goto exit;
1319251f904SBorislav Petkov 	}
1329251f904SBorislav Petkov 
133bfb3d7b8SJiri Olsa 	if (count == 1)
1349251f904SBorislav Petkov 		err = alloc_callchain_buffers();
1359251f904SBorislav Petkov exit:
13690983b16SFrederic Weisbecker 	if (err)
13790983b16SFrederic Weisbecker 		atomic_dec(&nr_callchain_events);
1389251f904SBorislav Petkov 
139fc3b86d6SFrederic Weisbecker 	mutex_unlock(&callchain_mutex);
140fc3b86d6SFrederic Weisbecker 
1419251f904SBorislav Petkov 	return err;
1429251f904SBorislav Petkov }
1439251f904SBorislav Petkov 
put_callchain_buffers(void)1449251f904SBorislav Petkov void put_callchain_buffers(void)
1459251f904SBorislav Petkov {
1469251f904SBorislav Petkov 	if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
1479251f904SBorislav Petkov 		release_callchain_buffers();
1489251f904SBorislav Petkov 		mutex_unlock(&callchain_mutex);
1499251f904SBorislav Petkov 	}
1509251f904SBorislav Petkov }
1519251f904SBorislav Petkov 
get_callchain_entry(int * rctx)152*d141b8bcSSong Liu struct perf_callchain_entry *get_callchain_entry(int *rctx)
1539251f904SBorislav Petkov {
1549251f904SBorislav Petkov 	int cpu;
1559251f904SBorislav Petkov 	struct callchain_cpus_entries *entries;
1569251f904SBorislav Petkov 
1574a32fea9SChristoph Lameter 	*rctx = get_recursion_context(this_cpu_ptr(callchain_recursion));
1589251f904SBorislav Petkov 	if (*rctx == -1)
1599251f904SBorislav Petkov 		return NULL;
1609251f904SBorislav Petkov 
1619251f904SBorislav Petkov 	entries = rcu_dereference(callchain_cpus_entries);
162*d141b8bcSSong Liu 	if (!entries) {
163*d141b8bcSSong Liu 		put_recursion_context(this_cpu_ptr(callchain_recursion), *rctx);
1649251f904SBorislav Petkov 		return NULL;
165*d141b8bcSSong Liu 	}
1669251f904SBorislav Petkov 
1679251f904SBorislav Petkov 	cpu = smp_processor_id();
1689251f904SBorislav Petkov 
169c5dfd78eSArnaldo Carvalho de Melo 	return (((void *)entries->cpu_entries[cpu]) +
170c5dfd78eSArnaldo Carvalho de Melo 		(*rctx * perf_callchain_entry__sizeof()));
1719251f904SBorislav Petkov }
1729251f904SBorislav Petkov 
173*d141b8bcSSong Liu void
put_callchain_entry(int rctx)1749251f904SBorislav Petkov put_callchain_entry(int rctx)
1759251f904SBorislav Petkov {
1764a32fea9SChristoph Lameter 	put_recursion_context(this_cpu_ptr(callchain_recursion), rctx);
1779251f904SBorislav Petkov }
1789251f904SBorislav Petkov 
179e6dab5ffSAndrew Vagin struct perf_callchain_entry *
get_perf_callchain(struct pt_regs * regs,u32 init_nr,bool kernel,bool user,u32 max_stack,bool crosstask,bool add_mark)180568b329aSAlexei Starovoitov get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
181cfbcf468SArnaldo Carvalho de Melo 		   u32 max_stack, bool crosstask, bool add_mark)
182568b329aSAlexei Starovoitov {
183568b329aSAlexei Starovoitov 	struct perf_callchain_entry *entry;
184cfbcf468SArnaldo Carvalho de Melo 	struct perf_callchain_entry_ctx ctx;
185568b329aSAlexei Starovoitov 	int rctx;
186568b329aSAlexei Starovoitov 
1879251f904SBorislav Petkov 	entry = get_callchain_entry(&rctx);
1889251f904SBorislav Petkov 	if (!entry)
189*d141b8bcSSong Liu 		return NULL;
1909251f904SBorislav Petkov 
191cfbcf468SArnaldo Carvalho de Melo 	ctx.entry     = entry;
192cfbcf468SArnaldo Carvalho de Melo 	ctx.max_stack = max_stack;
1933b1fff08SArnaldo Carvalho de Melo 	ctx.nr	      = entry->nr = init_nr;
194c85b0334SArnaldo Carvalho de Melo 	ctx.contexts       = 0;
195c85b0334SArnaldo Carvalho de Melo 	ctx.contexts_maxed = false;
1969251f904SBorislav Petkov 
197d0775264SFrederic Weisbecker 	if (kernel && !user_mode(regs)) {
198568b329aSAlexei Starovoitov 		if (add_mark)
1993e4de4ecSArnaldo Carvalho de Melo 			perf_callchain_store_context(&ctx, PERF_CONTEXT_KERNEL);
200cfbcf468SArnaldo Carvalho de Melo 		perf_callchain_kernel(&ctx, regs);
201d0775264SFrederic Weisbecker 	}
202d0775264SFrederic Weisbecker 
203d0775264SFrederic Weisbecker 	if (user) {
204d0775264SFrederic Weisbecker 		if (!user_mode(regs)) {
2059251f904SBorislav Petkov 			if  (current->mm)
2069251f904SBorislav Petkov 				regs = task_pt_regs(current);
2079251f904SBorislav Petkov 			else
2089251f904SBorislav Petkov 				regs = NULL;
2099251f904SBorislav Petkov 		}
2109251f904SBorislav Petkov 
2119251f904SBorislav Petkov 		if (regs) {
212568b329aSAlexei Starovoitov 			if (crosstask)
213e6dab5ffSAndrew Vagin 				goto exit_put;
214e6dab5ffSAndrew Vagin 
215568b329aSAlexei Starovoitov 			if (add_mark)
2163e4de4ecSArnaldo Carvalho de Melo 				perf_callchain_store_context(&ctx, PERF_CONTEXT_USER);
21788b0193dSWill Deacon 
218cfbcf468SArnaldo Carvalho de Melo 			perf_callchain_user(&ctx, regs);
2199251f904SBorislav Petkov 		}
220d0775264SFrederic Weisbecker 	}
2219251f904SBorislav Petkov 
2229251f904SBorislav Petkov exit_put:
2239251f904SBorislav Petkov 	put_callchain_entry(rctx);
2249251f904SBorislav Petkov 
2259251f904SBorislav Petkov 	return entry;
2269251f904SBorislav Petkov }
227c5dfd78eSArnaldo Carvalho de Melo 
228c85b0334SArnaldo Carvalho de Melo /*
229c85b0334SArnaldo Carvalho de Melo  * Used for sysctl_perf_event_max_stack and
230c85b0334SArnaldo Carvalho de Melo  * sysctl_perf_event_max_contexts_per_stack.
231c85b0334SArnaldo Carvalho de Melo  */
perf_event_max_stack_handler(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)232c5dfd78eSArnaldo Carvalho de Melo int perf_event_max_stack_handler(struct ctl_table *table, int write,
23332927393SChristoph Hellwig 				 void *buffer, size_t *lenp, loff_t *ppos)
234c5dfd78eSArnaldo Carvalho de Melo {
235a831100aSArnaldo Carvalho de Melo 	int *value = table->data;
236a831100aSArnaldo Carvalho de Melo 	int new_value = *value, ret;
237c5dfd78eSArnaldo Carvalho de Melo 	struct ctl_table new_table = *table;
238c5dfd78eSArnaldo Carvalho de Melo 
239c5dfd78eSArnaldo Carvalho de Melo 	new_table.data = &new_value;
240c5dfd78eSArnaldo Carvalho de Melo 	ret = proc_dointvec_minmax(&new_table, write, buffer, lenp, ppos);
241c5dfd78eSArnaldo Carvalho de Melo 	if (ret || !write)
242c5dfd78eSArnaldo Carvalho de Melo 		return ret;
243c5dfd78eSArnaldo Carvalho de Melo 
244c5dfd78eSArnaldo Carvalho de Melo 	mutex_lock(&callchain_mutex);
245c5dfd78eSArnaldo Carvalho de Melo 	if (atomic_read(&nr_callchain_events))
246c5dfd78eSArnaldo Carvalho de Melo 		ret = -EBUSY;
247c5dfd78eSArnaldo Carvalho de Melo 	else
248a831100aSArnaldo Carvalho de Melo 		*value = new_value;
249c5dfd78eSArnaldo Carvalho de Melo 
250c5dfd78eSArnaldo Carvalho de Melo 	mutex_unlock(&callchain_mutex);
251c5dfd78eSArnaldo Carvalho de Melo 
252c5dfd78eSArnaldo Carvalho de Melo 	return ret;
253c5dfd78eSArnaldo Carvalho de Melo }
254