Skip to content

Commit 86038c5

Browse files
Peter Zijlstra (Intel)ingomolnar
authored andcommitted
perf: Avoid horrible stack usage
Both Linus (most recent) and Steve (a while ago) reported that perf related callbacks have massive stack bloat. The problem is that software events need a pt_regs in order to properly report the event location and unwind stack. And because we could not assume one was present we allocated one on stack and filled it with minimal bits required for operation. Now, pt_regs is quite large, so this is undesirable. Furthermore it turns out that most sites actually have a pt_regs pointer available, making this even more onerous, as the stack space is pointless waste. This patch addresses the problem by observing that software events have well defined nesting semantics, therefore we can use static per-cpu storage instead of on-stack. Linus made the further observation that all but the scheduler callers of perf_sw_event() have a pt_regs available, so we change the regular perf_sw_event() to require a valid pt_regs (where it used to be optional) and add perf_sw_event_sched() for the scheduler. We have a scheduler specific call instead of a more generic _noregs() like construct because we can assume non-recursion from the scheduler and thereby simplify the code further (_noregs would have to put the recursion context call inline in order to assertain which __perf_regs element to use). One last note on the implementation of perf_trace_buf_prepare(); we allow .regs = NULL for those cases where we already have a pt_regs pointer available and do not need another. Reported-by: Linus Torvalds <torvalds@linux-foundation.org> Reported-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Arnaldo Carvalho de Melo <acme@kernel.org> Cc: Javi Merino <javi.merino@arm.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Paul Mackerras <paulus@samba.org> Cc: Petr Mladek <pmladek@suse.cz> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Tom Zanussi <tom.zanussi@linux.intel.com> Cc: Vaibhav Nagarnaik <vnagarnaik@google.com> Link: http://lkml.kernel.org/r/20141216115041.GW3337@twins.programming.kicks-ass.net Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 188c901 commit 86038c5

9 files changed

Lines changed: 52 additions & 24 deletions

File tree

‎include/linux/ftrace_event.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ extern int ftrace_profile_set_filter(struct perf_event *event, int event_id,
595595
char *filter_str);
596596
extern void ftrace_profile_free_filter(struct perf_event *event);
597597
extern void *perf_trace_buf_prepare(int size, unsigned short type,
598-
struct pt_regs *regs, int *rctxp);
598+
struct pt_regs **regs, int *rctxp);
599599

600600
static inline void
601601
perf_trace_buf_submit(void *raw_data, int size, int rctx, u64 addr,

‎include/linux/perf_event.h‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@ static inline int is_software_event(struct perf_event *event)
665665

666666
extern struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
667667

668+
extern void ___perf_sw_event(u32, u64, struct pt_regs *, u64);
668669
extern void __perf_sw_event(u32, u64, struct pt_regs *, u64);
669670

670671
#ifndef perf_arch_fetch_caller_regs
@@ -689,14 +690,25 @@ static inline void perf_fetch_caller_regs(struct pt_regs *regs)
689690
static __always_inline void
690691
perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
691692
{
692-
struct pt_regs hot_regs;
693+
if (static_key_false(&perf_swevent_enabled[event_id]))
694+
__perf_sw_event(event_id, nr, regs, addr);
695+
}
696+
697+
DECLARE_PER_CPU(struct pt_regs, __perf_regs[4]);
693698

699+
/*
700+
* 'Special' version for the scheduler, it hard assumes no recursion,
701+
* which is guaranteed by us not actually scheduling inside other swevents
702+
* because those disable preemption.
703+
*/
704+
static __always_inline void
705+
perf_sw_event_sched(u32 event_id, u64 nr, u64 addr)
706+
{
694707
if (static_key_false(&perf_swevent_enabled[event_id])) {
695-
if (!regs) {
696-
perf_fetch_caller_regs(&hot_regs);
697-
regs = &hot_regs;
698-
}
699-
__perf_sw_event(event_id, nr, regs, addr);
708+
struct pt_regs *regs = this_cpu_ptr(&__perf_regs[0]);
709+
710+
perf_fetch_caller_regs(regs);
711+
___perf_sw_event(event_id, nr, regs, addr);
700712
}
701713
}
702714

@@ -712,7 +724,7 @@ static inline void perf_event_task_sched_in(struct task_struct *prev,
712724
static inline void perf_event_task_sched_out(struct task_struct *prev,
713725
struct task_struct *next)
714726
{
715-
perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, NULL, 0);
727+
perf_sw_event_sched(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 0);
716728

717729
if (static_key_false(&perf_sched_events.key))
718730
__perf_event_task_sched_out(prev, next);
@@ -823,6 +835,8 @@ static inline int perf_event_refresh(struct perf_event *event, int refresh)
823835
static inline void
824836
perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr) { }
825837
static inline void
838+
perf_sw_event_sched(u32 event_id, u64 nr, u64 addr) { }
839+
static inline void
826840
perf_bp_event(struct perf_event *event, void *data) { }
827841

828842
static inline int perf_register_guest_info_callbacks

‎include/trace/ftrace.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ perf_trace_##call(void *__data, proto) \
763763
struct ftrace_event_call *event_call = __data; \
764764
struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
765765
struct ftrace_raw_##call *entry; \
766-
struct pt_regs __regs; \
766+
struct pt_regs *__regs; \
767767
u64 __addr = 0, __count = 1; \
768768
struct task_struct *__task = NULL; \
769769
struct hlist_head *head; \
@@ -782,18 +782,19 @@ perf_trace_##call(void *__data, proto) \
782782
sizeof(u64)); \
783783
__entry_size -= sizeof(u32); \
784784
\
785-
perf_fetch_caller_regs(&__regs); \
786785
entry = perf_trace_buf_prepare(__entry_size, \
787786
event_call->event.type, &__regs, &rctx); \
788787
if (!entry) \
789788
return; \
790789
\
790+
perf_fetch_caller_regs(__regs); \
791+
\
791792
tstruct \
792793
\
793794
{ assign; } \
794795
\
795796
perf_trace_buf_submit(entry, __entry_size, rctx, __addr, \
796-
__count, &__regs, head, __task); \
797+
__count, __regs, head, __task); \
797798
}
798799

799800
/*

‎kernel/events/core.c‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5889,6 +5889,8 @@ static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
58895889
rcu_read_unlock();
58905890
}
58915891

5892+
DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
5893+
58925894
int perf_swevent_get_recursion_context(void)
58935895
{
58945896
struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
@@ -5904,21 +5906,30 @@ inline void perf_swevent_put_recursion_context(int rctx)
59045906
put_recursion_context(swhash->recursion, rctx);
59055907
}
59065908

5907-
void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
5909+
void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
59085910
{
59095911
struct perf_sample_data data;
5910-
int rctx;
59115912

5912-
preempt_disable_notrace();
5913-
rctx = perf_swevent_get_recursion_context();
5914-
if (rctx < 0)
5913+
if (WARN_ON_ONCE(!regs))
59155914
return;
59165915

59175916
perf_sample_data_init(&data, addr, 0);
5918-
59195917
do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
5918+
}
5919+
5920+
void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
5921+
{
5922+
int rctx;
5923+
5924+
preempt_disable_notrace();
5925+
rctx = perf_swevent_get_recursion_context();
5926+
if (unlikely(rctx < 0))
5927+
goto fail;
5928+
5929+
___perf_sw_event(event_id, nr, regs, addr);
59205930

59215931
perf_swevent_put_recursion_context(rctx);
5932+
fail:
59225933
preempt_enable_notrace();
59235934
}
59245935

‎kernel/sched/core.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
10821082
if (p->sched_class->migrate_task_rq)
10831083
p->sched_class->migrate_task_rq(p, new_cpu);
10841084
p->se.nr_migrations++;
1085-
perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, NULL, 0);
1085+
perf_sw_event_sched(PERF_COUNT_SW_CPU_MIGRATIONS, 1, 0);
10861086
}
10871087

10881088
__set_task_cpu(p, new_cpu);

‎kernel/trace/trace_event_perf.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ void perf_trace_del(struct perf_event *p_event, int flags)
261261
}
262262

263263
void *perf_trace_buf_prepare(int size, unsigned short type,
264-
struct pt_regs *regs, int *rctxp)
264+
struct pt_regs **regs, int *rctxp)
265265
{
266266
struct trace_entry *entry;
267267
unsigned long flags;
@@ -280,6 +280,8 @@ void *perf_trace_buf_prepare(int size, unsigned short type,
280280
if (*rctxp < 0)
281281
return NULL;
282282

283+
if (regs)
284+
*regs = this_cpu_ptr(&__perf_regs[*rctxp]);
283285
raw_data = this_cpu_ptr(perf_trace_buf[*rctxp]);
284286

285287
/* zero the dead bytes from align to not leak stack to user */

‎kernel/trace/trace_kprobe.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
11481148
size = ALIGN(__size + sizeof(u32), sizeof(u64));
11491149
size -= sizeof(u32);
11501150

1151-
entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
1151+
entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
11521152
if (!entry)
11531153
return;
11541154

@@ -1179,7 +1179,7 @@ kretprobe_perf_func(struct trace_kprobe *tk, struct kretprobe_instance *ri,
11791179
size = ALIGN(__size + sizeof(u32), sizeof(u64));
11801180
size -= sizeof(u32);
11811181

1182-
entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
1182+
entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
11831183
if (!entry)
11841184
return;
11851185

‎kernel/trace/trace_syscalls.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id)
574574
size -= sizeof(u32);
575575

576576
rec = (struct syscall_trace_enter *)perf_trace_buf_prepare(size,
577-
sys_data->enter_event->event.type, regs, &rctx);
577+
sys_data->enter_event->event.type, NULL, &rctx);
578578
if (!rec)
579579
return;
580580

@@ -647,7 +647,7 @@ static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret)
647647
size -= sizeof(u32);
648648

649649
rec = (struct syscall_trace_exit *)perf_trace_buf_prepare(size,
650-
sys_data->exit_event->event.type, regs, &rctx);
650+
sys_data->exit_event->event.type, NULL, &rctx);
651651
if (!rec)
652652
return;
653653

‎kernel/trace/trace_uprobe.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ static void __uprobe_perf_func(struct trace_uprobe *tu,
11111111
if (hlist_empty(head))
11121112
goto out;
11131113

1114-
entry = perf_trace_buf_prepare(size, call->event.type, regs, &rctx);
1114+
entry = perf_trace_buf_prepare(size, call->event.type, NULL, &rctx);
11151115
if (!entry)
11161116
goto out;
11171117

0 commit comments

Comments
 (0)