Skip to content

Commit 8a1ccfb

Browse files
labbottwildea01
authored andcommitted
arm64: Add stack information to on_accessible_stack
In preparation for enabling the stackleak plugin on arm64, we need a way to get the bounds of the current stack. Extend on_accessible_stack to get this information. Acked-by: Alexander Popov <alex.popov@linux.com> Reviewed-by: Mark Rutland <mark.rutland@arm.com> Signed-off-by: Laura Abbott <labbott@redhat.com> [will: folded in fix for allmodconfig build breakage w/ sdei] Signed-off-by: Will Deacon <will.deacon@arm.com>
1 parent 2c870e6 commit 8a1ccfb

5 files changed

Lines changed: 111 additions & 26 deletions

File tree

‎arch/arm64/include/asm/sdei.h‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,18 @@ asmlinkage unsigned long __sdei_handler(struct pt_regs *regs,
4040
unsigned long sdei_arch_get_entry_point(int conduit);
4141
#define sdei_arch_get_entry_point(x) sdei_arch_get_entry_point(x)
4242

43-
bool _on_sdei_stack(unsigned long sp);
44-
static inline bool on_sdei_stack(unsigned long sp)
43+
struct stack_info;
44+
45+
bool _on_sdei_stack(unsigned long sp, struct stack_info *info);
46+
static inline bool on_sdei_stack(unsigned long sp,
47+
struct stack_info *info)
4548
{
4649
if (!IS_ENABLED(CONFIG_VMAP_STACK))
4750
return false;
4851
if (!IS_ENABLED(CONFIG_ARM_SDE_INTERFACE))
4952
return false;
5053
if (in_nmi())
51-
return _on_sdei_stack(sp);
54+
return _on_sdei_stack(sp, info);
5255

5356
return false;
5457
}

‎arch/arm64/include/asm/stacktrace.h‎

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,61 +32,110 @@ struct stackframe {
3232
#endif
3333
};
3434

35+
enum stack_type {
36+
STACK_TYPE_UNKNOWN,
37+
STACK_TYPE_TASK,
38+
STACK_TYPE_IRQ,
39+
STACK_TYPE_OVERFLOW,
40+
STACK_TYPE_SDEI_NORMAL,
41+
STACK_TYPE_SDEI_CRITICAL,
42+
};
43+
44+
struct stack_info {
45+
unsigned long low;
46+
unsigned long high;
47+
enum stack_type type;
48+
};
49+
3550
extern int unwind_frame(struct task_struct *tsk, struct stackframe *frame);
3651
extern void walk_stackframe(struct task_struct *tsk, struct stackframe *frame,
3752
int (*fn)(struct stackframe *, void *), void *data);
3853
extern void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk);
3954

4055
DECLARE_PER_CPU(unsigned long *, irq_stack_ptr);
4156

42-
static inline bool on_irq_stack(unsigned long sp)
57+
static inline bool on_irq_stack(unsigned long sp,
58+
struct stack_info *info)
4359
{
4460
unsigned long low = (unsigned long)raw_cpu_read(irq_stack_ptr);
4561
unsigned long high = low + IRQ_STACK_SIZE;
4662

4763
if (!low)
4864
return false;
4965

50-
return (low <= sp && sp < high);
66+
if (sp < low || sp >= high)
67+
return false;
68+
69+
if (info) {
70+
info->low = low;
71+
info->high = high;
72+
info->type = STACK_TYPE_IRQ;
73+
}
74+
75+
return true;
5176
}
5277

53-
static inline bool on_task_stack(struct task_struct *tsk, unsigned long sp)
78+
static inline bool on_task_stack(struct task_struct *tsk, unsigned long sp,
79+
struct stack_info *info)
5480
{
5581
unsigned long low = (unsigned long)task_stack_page(tsk);
5682
unsigned long high = low + THREAD_SIZE;
5783

58-
return (low <= sp && sp < high);
84+
if (sp < low || sp >= high)
85+
return false;
86+
87+
if (info) {
88+
info->low = low;
89+
info->high = high;
90+
info->type = STACK_TYPE_TASK;
91+
}
92+
93+
return true;
5994
}
6095

6196
#ifdef CONFIG_VMAP_STACK
6297
DECLARE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack);
6398

64-
static inline bool on_overflow_stack(unsigned long sp)
99+
static inline bool on_overflow_stack(unsigned long sp,
100+
struct stack_info *info)
65101
{
66102
unsigned long low = (unsigned long)raw_cpu_ptr(overflow_stack);
67103
unsigned long high = low + OVERFLOW_STACK_SIZE;
68104

69-
return (low <= sp && sp < high);
105+
if (sp < low || sp >= high)
106+
return false;
107+
108+
if (info) {
109+
info->low = low;
110+
info->high = high;
111+
info->type = STACK_TYPE_OVERFLOW;
112+
}
113+
114+
return true;
70115
}
71116
#else
72-
static inline bool on_overflow_stack(unsigned long sp) { return false; }
117+
static inline bool on_overflow_stack(unsigned long sp,
118+
struct stack_info *info) { return false; }
73119
#endif
74120

121+
75122
/*
76123
* We can only safely access per-cpu stacks from current in a non-preemptible
77124
* context.
78125
*/
79-
static inline bool on_accessible_stack(struct task_struct *tsk, unsigned long sp)
126+
static inline bool on_accessible_stack(struct task_struct *tsk,
127+
unsigned long sp,
128+
struct stack_info *info)
80129
{
81-
if (on_task_stack(tsk, sp))
130+
if (on_task_stack(tsk, sp, info))
82131
return true;
83132
if (tsk != current || preemptible())
84133
return false;
85-
if (on_irq_stack(sp))
134+
if (on_irq_stack(sp, info))
86135
return true;
87-
if (on_overflow_stack(sp))
136+
if (on_overflow_stack(sp, info))
88137
return true;
89-
if (on_sdei_stack(sp))
138+
if (on_sdei_stack(sp, info))
90139
return true;
91140

92141
return false;

‎arch/arm64/kernel/ptrace.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
132132
{
133133
return ((addr & ~(THREAD_SIZE - 1)) ==
134134
(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))) ||
135-
on_irq_stack(addr);
135+
on_irq_stack(addr, NULL);
136136
}
137137

138138
/**

‎arch/arm64/kernel/sdei.c‎

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <asm/mmu.h>
1414
#include <asm/ptrace.h>
1515
#include <asm/sections.h>
16+
#include <asm/stacktrace.h>
1617
#include <asm/sysreg.h>
1718
#include <asm/vmap_stack.h>
1819

@@ -88,23 +89,55 @@ static int init_sdei_stacks(void)
8889
return err;
8990
}
9091

91-
bool _on_sdei_stack(unsigned long sp)
92+
bool on_sdei_normal_stack(unsigned long sp,
93+
struct stack_info *info)
9294
{
93-
unsigned long low, high;
95+
unsigned long low = (unsigned long)raw_cpu_read(sdei_stack_normal_ptr);
96+
unsigned long high = low + SDEI_STACK_SIZE;
9497

95-
if (!IS_ENABLED(CONFIG_VMAP_STACK))
98+
if (sp < low || sp >= high)
9699
return false;
97100

98-
low = (unsigned long)raw_cpu_read(sdei_stack_critical_ptr);
99-
high = low + SDEI_STACK_SIZE;
101+
if (info) {
102+
info->low = low;
103+
info->high = high;
104+
info->type = STACK_TYPE_SDEI_NORMAL;
105+
}
100106

101-
if (low <= sp && sp < high)
107+
return true;
108+
}
109+
110+
bool on_sdei_critical_stack(unsigned long sp,
111+
struct stack_info *info)
112+
{
113+
unsigned long low = (unsigned long)raw_cpu_read(sdei_stack_critical_ptr);
114+
unsigned long high = low + SDEI_STACK_SIZE;
115+
116+
if (sp < low || sp >= high)
117+
return false;
118+
119+
if (info) {
120+
info->low = low;
121+
info->high = high;
122+
info->type = STACK_TYPE_SDEI_CRITICAL;
123+
}
124+
125+
return true;
126+
}
127+
128+
bool _on_sdei_stack(unsigned long sp,
129+
struct stack_info *info)
130+
{
131+
if (!IS_ENABLED(CONFIG_VMAP_STACK))
132+
return false;
133+
134+
if (on_sdei_critical_stack(sp, info))
102135
return true;
103136

104-
low = (unsigned long)raw_cpu_read(sdei_stack_normal_ptr);
105-
high = low + SDEI_STACK_SIZE;
137+
if (on_sdei_normal_stack(sp, info))
138+
return true;
106139

107-
return (low <= sp && sp < high);
140+
return false;
108141
}
109142

110143
unsigned long sdei_arch_get_entry_point(int conduit)

‎arch/arm64/kernel/stacktrace.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int notrace unwind_frame(struct task_struct *tsk, struct stackframe *frame)
5050
if (!tsk)
5151
tsk = current;
5252

53-
if (!on_accessible_stack(tsk, fp))
53+
if (!on_accessible_stack(tsk, fp, NULL))
5454
return -EINVAL;
5555

5656
frame->fp = READ_ONCE_NOCHECK(*(unsigned long *)(fp));

0 commit comments

Comments
 (0)