[PATCH v2] riscv: cif: reduce shadow stack size limit from 4GB to 2GB

Zong Li posted 1 patch 1 month, 2 weeks ago
There is a newer version of this series
arch/riscv/kernel/usercfi.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
[PATCH v2] riscv: cif: reduce shadow stack size limit from 4GB to 2GB
Posted by Zong Li 1 month, 2 weeks ago
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 '506496bcbb42 "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 while being more resource-efficient margin

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>
---

Changed in v1:
- Use min() instead of min_t(). Suggested by David Laight

 arch/riscv/kernel/usercfi.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c
index 6eaa0d94fdfe..cbfb4e495e9f 100644
--- a/arch/riscv/kernel/usercfi.c
+++ b/arch/riscv/kernel/usercfi.c
@@ -109,15 +109,16 @@ void set_indir_lp_lock(struct task_struct *task, bool lock)
 	task->thread_info.user_cfi_state.ufcfi_locked = lock;
 }
 /*
- * If size is 0, then to be compatible with regular stack we want it to be as big as
- * regular stack. Else PAGE_ALIGN it and return back
+ * The shadow stack only stores the return address and not any variables
+ * this should be more than sufficient for most applications.
+ * Else PAGE_ALIGN it and return back
  */
 static unsigned long calc_shstk_size(unsigned long size)
 {
 	if (size)
 		return PAGE_ALIGN(size);
 
-	return PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G));
+	return PAGE_ALIGN(min(rlimit(RLIMIT_STACK) / 2, SZ_2G));
 }
 
 /*
-- 
2.43.7
Re: [PATCH v2] riscv: cif: reduce shadow stack size limit from 4GB to 2GB
Posted by Paul Walmsley 1 month ago
Hi, 

On Mon, 27 Apr 2026, Zong Li wrote:

> 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 '506496bcbb42 "arm64/gcs: Ensure
> that new threads have a GCS")'

Sashiko caught that this patch doesn't exactly follow the ARM 
implementation, in that it doesn't include the max(PAGE_SIZE, size) 
constraint, which covers the case where someone sets RLIMIT_STACK to 
something absurdly small ( < PAGE_SIZE ).  Care to fix that and post a new 
version?


thanks

- Paul
Re: [PATCH v2] riscv: cif: reduce shadow stack size limit from 4GB to 2GB
Posted by Zong Li 1 month ago
On Thu, May 14, 2026 at 9:20 AM Paul Walmsley <pjw@kernel.org> wrote:
>
> Hi,
>
> On Mon, 27 Apr 2026, Zong Li wrote:
>
> > 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 '506496bcbb42 "arm64/gcs: Ensure
> > that new threads have a GCS")'
>
> Sashiko caught that this patch doesn't exactly follow the ARM
> implementation, in that it doesn't include the max(PAGE_SIZE, size)
> constraint, which covers the case where someone sets RLIMIT_STACK to
> something absurdly small ( < PAGE_SIZE ).  Care to fix that and post a new
> version?
>

Ok. I will send the next version to include that. Thanks for pointing it out.

>
> thanks
>
> - Paul