Skip to content

Commit 6c7674b

Browse files
zongboxPaul Walmsley
authored andcommitted
riscv: cfi: reduce shadow stack size limit from 4GB to 2GB
Follow the ARM64 GCS (Guarded Control Stack) implementation approach by reducing the shadow stack size allocation from min(RLIMIT_STACK, 4GB) to min(RLIMIT_STACK/2, 2GB). See commit 506496b ("arm64/gcs: Ensure that new threads have a GCS") Rationale: 1. Shadow stacks only store return addresses (8 bytes per entry), not local variables, function parameters, or saved registers. A 2GB shadow stack is far more than sufficient for any practical application, even with extremely deep recursion. Using half the size maintains adequate margin while being more resource-efficient. 2. On memory-constrained systems (e.g., platforms with only 4GB of physical memory, which is a common configuration), allocating 4GB of virtual address space for shadow stack per process/thread can lead to virtual memory allocation failures when the overcommit mode is set to OVERCOMMIT_GUESS or OVERCOMMIT_NEVER: Error: "__vm_enough_memory: not enough memory for the allocation" This reduces virtual address space consumption by 50% while maintaining more than adequate space for return address storage. Signed-off-by: Zong Li <zong.li@sifive.com> Link: https://patch.msgid.link/20260428024105.645162-1-zong.li@sifive.com [pjw@kernel.org: clean up patch description] Signed-off-by: Paul Walmsley <pjw@kernel.org>
1 parent 4133709 commit 6c7674b

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

arch/riscv/kernel/usercfi.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,16 @@ void set_indir_lp_lock(struct task_struct *task, bool lock)
109109
task->thread_info.user_cfi_state.ufcfi_locked = lock;
110110
}
111111
/*
112-
* If size is 0, then to be compatible with regular stack we want it to be as big as
113-
* regular stack. Else PAGE_ALIGN it and return back
112+
* The shadow stack only stores the return address and not any variables
113+
* this should be more than sufficient for most applications.
114+
* Else PAGE_ALIGN it and return back
114115
*/
115116
static unsigned long calc_shstk_size(unsigned long size)
116117
{
117118
if (size)
118119
return PAGE_ALIGN(size);
119120

120-
return PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G));
121+
return PAGE_ALIGN(min(rlimit(RLIMIT_STACK) / 2, SZ_2G));
121122
}
122123

123124
/*

0 commit comments

Comments
 (0)