Skip to content

Commit c85b033

Browse files
committed
perf core: Separate accounting of contexts and real addresses in a stack trace
The perf_sample->ip_callchain->nr value includes all the entries in the ip_callchain->ip[] array, real addresses and PERF_CONTEXT_{KERNEL,USER,etc}, while what the user expects is that what is in the kernel.perf_event_max_stack sysctl or in the upcoming per event perf_event_attr.sample_max_stack knob be honoured in terms of IP addresses in the stack trace. So allocate a bunch of extra entries for contexts, and do the accounting via perf_callchain_entry_ctx struct members. A new sysctl, kernel.perf_event_max_contexts_per_stack is also introduced for investigating possible bugs in the callchain implementation by some arch. Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: Brendan Gregg <[email protected]> Cc: David Ahern <[email protected]> Cc: Frederic Weisbecker <[email protected]> Cc: He Kuang <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Milian Wolff <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Vince Weaver <[email protected]> Cc: Wang Nan <[email protected]> Cc: Zefan Li <[email protected]> Link: http://lkml.kernel.org/n/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 3e4de4e commit c85b033

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

Documentation/sysctl/kernel.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ show up in /proc/sys/kernel:
6161
- perf_cpu_time_max_percent
6262
- perf_event_paranoid
6363
- perf_event_max_stack
64+
- perf_event_max_contexts_per_stack
6465
- pid_max
6566
- powersave-nap [ PPC only ]
6667
- printk
@@ -668,6 +669,19 @@ The default value is 127.
668669

669670
==============================================================
670671

672+
perf_event_max_contexts_per_stack:
673+
674+
Controls maximum number of stack frame context entries for
675+
(attr.sample_type & PERF_SAMPLE_CALLCHAIN) configured events, for
676+
instance, when using 'perf record -g' or 'perf trace --call-graph fp'.
677+
678+
This can only be done when no events are in use that have callchains
679+
enabled, otherwise writing to this file will return -EBUSY.
680+
681+
The default value is 8.
682+
683+
==============================================================
684+
671685
pid_max:
672686

673687
PID allocation wrap value. When the kernel's next PID value

include/linux/perf_event.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ struct perf_callchain_entry_ctx {
6565
struct perf_callchain_entry *entry;
6666
u32 max_stack;
6767
u32 nr;
68+
short contexts;
69+
bool contexts_maxed;
6870
};
6971

7072
struct perf_raw_record {
@@ -1078,12 +1080,24 @@ extern int get_callchain_buffers(void);
10781080
extern void put_callchain_buffers(void);
10791081

10801082
extern int sysctl_perf_event_max_stack;
1083+
extern int sysctl_perf_event_max_contexts_per_stack;
10811084

1082-
#define perf_callchain_store_context(ctx, context) perf_callchain_store(ctx, context)
1085+
static inline int perf_callchain_store_context(struct perf_callchain_entry_ctx *ctx, u64 ip)
1086+
{
1087+
if (ctx->contexts < sysctl_perf_event_max_contexts_per_stack) {
1088+
struct perf_callchain_entry *entry = ctx->entry;
1089+
entry->ip[entry->nr++] = ip;
1090+
++ctx->contexts;
1091+
return 0;
1092+
} else {
1093+
ctx->contexts_maxed = true;
1094+
return -1; /* no more room, stop walking the stack */
1095+
}
1096+
}
10831097

10841098
static inline int perf_callchain_store(struct perf_callchain_entry_ctx *ctx, u64 ip)
10851099
{
1086-
if (ctx->nr < ctx->max_stack) {
1100+
if (ctx->nr < ctx->max_stack && !ctx->contexts_maxed) {
10871101
struct perf_callchain_entry *entry = ctx->entry;
10881102
entry->ip[entry->nr++] = ip;
10891103
++ctx->nr;

include/uapi/linux/perf_event.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ enum perf_event_type {
862862
};
863863

864864
#define PERF_MAX_STACK_DEPTH 127
865+
#define PERF_MAX_CONTEXTS_PER_STACK 8
865866

866867
enum perf_callchain_context {
867868
PERF_CONTEXT_HV = (__u64)-32,

kernel/events/callchain.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ struct callchain_cpus_entries {
1919
};
2020

2121
int sysctl_perf_event_max_stack __read_mostly = PERF_MAX_STACK_DEPTH;
22+
int sysctl_perf_event_max_contexts_per_stack __read_mostly = PERF_MAX_CONTEXTS_PER_STACK;
2223

2324
static inline size_t perf_callchain_entry__sizeof(void)
2425
{
2526
return (sizeof(struct perf_callchain_entry) +
26-
sizeof(__u64) * sysctl_perf_event_max_stack);
27+
sizeof(__u64) * (sysctl_perf_event_max_stack +
28+
sysctl_perf_event_max_contexts_per_stack));
2729
}
2830

2931
static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
@@ -197,6 +199,8 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
197199
ctx.entry = entry;
198200
ctx.max_stack = max_stack;
199201
ctx.nr = entry->nr = init_nr;
202+
ctx.contexts = 0;
203+
ctx.contexts_maxed = false;
200204

201205
if (kernel && !user_mode(regs)) {
202206
if (add_mark)
@@ -228,6 +232,10 @@ get_perf_callchain(struct pt_regs *regs, u32 init_nr, bool kernel, bool user,
228232
return entry;
229233
}
230234

235+
/*
236+
* Used for sysctl_perf_event_max_stack and
237+
* sysctl_perf_event_max_contexts_per_stack.
238+
*/
231239
int perf_event_max_stack_handler(struct ctl_table *table, int write,
232240
void __user *buffer, size_t *lenp, loff_t *ppos)
233241
{

kernel/sysctl.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,15 @@ static struct ctl_table kern_table[] = {
11561156
.extra1 = &zero,
11571157
.extra2 = &six_hundred_forty_kb,
11581158
},
1159+
{
1160+
.procname = "perf_event_max_contexts_per_stack",
1161+
.data = &sysctl_perf_event_max_contexts_per_stack,
1162+
.maxlen = sizeof(sysctl_perf_event_max_contexts_per_stack),
1163+
.mode = 0644,
1164+
.proc_handler = perf_event_max_stack_handler,
1165+
.extra1 = &zero,
1166+
.extra2 = &one_thousand,
1167+
},
11591168
#endif
11601169
#ifdef CONFIG_KMEMCHECK
11611170
{

0 commit comments

Comments
 (0)