From nobody Mon May 25 03:55:48 2026 Received: from mail-dy1-f172.google.com (mail-dy1-f172.google.com [74.125.82.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 05DFF3B27C6 for ; Tue, 19 May 2026 07:13:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779174818; cv=none; b=fZj8TduzE/9IgInufDg3r+a2ZzQk81IVNS/HnwttXWdF4VHE5NYPgwBsdbWXZiFXWeLkiyN1c5Yqek2AOPgepHsWULIjCRdxtQ61pC1/OKwXkKyeYCU1SHZQjdE8ApeaoRW5zdKb8MNibs3j/NPDDDvPQZHp0waOzXoC84ezCQs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779174818; c=relaxed/simple; bh=FZIzK1KSNJmse9uzYQL5b9Mx2I8FFd8c2MtcegxgcV8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=an+UESe0cgYiZqgCcCt7QOrOYwlmv61jYE1T6lHPKrGFJi2qJgWGd9o6yh1BC4Dbq8CuB+cibi8Pij+Tk9jg519XY43b6bWU/gNjd7z+oVo3JC2rdj3dMPmRUcGxeCDvX08t7egWiJldfUafx1hdc8qikbAf2hzml1HsxqfX82Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com; spf=pass smtp.mailfrom=sifive.com; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b=TpcYFABm; arc=none smtp.client-ip=74.125.82.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="TpcYFABm" Received: by mail-dy1-f172.google.com with SMTP id 5a478bee46e88-2c156c4a9efso4123954eec.1 for ; Tue, 19 May 2026 00:13:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1779174816; x=1779779616; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=/v832jg02T0+h3BQvkKwAzJW+pkS6A9rgEMY10zaHwM=; b=TpcYFABmT8kbz9qd/fRfH5VCnGiLIvDeBgT4cnPoyePNLLxvkYA4MSyNPPGWLUAO77 OUBeWIdTBV7uI3tTmJ/J/27B1t1ALHNGy7tzgZeN2HI8LKInRp8tKN5TO5LyEkPGw0IJ s+1kUhfSXtge2l7KLSL5Ev5Iz+ge7ZBhSO2qrz/CZPn+Xtzg4dXKhoyMxgREsubyD7mD 2uCX7jS0RCgbLiaVgFRrtA2nxj96RrAq4V5T1KthdWyKDMuVwyqFBiegoUxo9oaoPuAk ZDOMpHHImVEbP1D8msIRdjEYl08Bfv4oHiboHCESrH1bP6SzUXQzsG+ENRGgpfqC/Aii B+Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779174816; x=1779779616; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=/v832jg02T0+h3BQvkKwAzJW+pkS6A9rgEMY10zaHwM=; b=CusTxrhOxpgRag1BEN9ope8z8wVsZnJYeew/dQiSFpQUbL/ep5PW6ZRm0U+Wh+DgGe L/xplxgi9imatgKol10w723d2VBfAX+4GvJt+IbcZJaY+PjUsfNUFHgF4cckvpB7qrfZ Oe1MvA6+dOipE0EhbeVKtKWQp6CFl5XwL8crkenuQheO6WZcZostjcXP5NVfkQta67/j /ds0pL/P8JeqA0/dwMxmV6DPo4XCn3uhk7R1uJHnPeTUiwIdLVqvFPDUTnShYZatlI5X 2PBauSGHFZLama6agpZpxkO2z3gTNK1nrFy8CHDaxke9yei0S0Xj/Gid2U7XF8VQXWpE sBkA== X-Forwarded-Encrypted: i=1; AFNElJ80mtVgtle/inzfwjacVUYWdU7FdON3rQuFNGgTvqddQX/vhWNYxad/ASnDy4Ip7JG4wL/sGaEsUtmr73U=@vger.kernel.org X-Gm-Message-State: AOJu0YyC9uSy9Ak9EZYZFbqqa2cF2JOdA4jOm9PjS3FUQl5w6+HDqmXk /zuF7sepuZko8PwLWwHAKg9C0q8C7FerD0EgvTXPU5zV1HwjjbuzcY03Ryi3RXYUSoA= X-Gm-Gg: Acq92OHwtRYp/+wq1TSit04PHMmOxW4p7LryDhe1BCxo7/BoptLJ3BBpvrfGzD6/7QM eukdZvApHu0UmP5Xr4EGFKvsSwanNkWA1ou9SALRuPqOTZJ8NqQUuap9AxyvJKEZmMecaWCAVbe 1A9+/tqUHhq9XddGlDoDmo3dRbLFL0Vf3y6yG2MfYRPZ2UOBdVShot5khDLQjk/TPNvnmg0vacG JT7n4Y0Jlg5KUlocStzo5oVn1UnKQUxiaYWl5C+n+6HuXUa3b+wu1htDb0PgPP1El3EdVKSfIAy uc3d2bU51zRadoP9NNI02RuLsWSuiqOfFLcozZAhxk/Z8552bUEtA0P/lZ+nT6cSgiDN17J/6FD KjXDJO7vYIZ9ru65eB7Zby7HGTa5dv6CUUbI07+qI8tFA+RpkGkKZCGnPsBjoJ5eQc4ihLMENEY r+sDKGLpP+/7eG737rnlad5psJ1eDXBr5hNbHXbOU3i3+3VQ== X-Received: by 2002:a05:7300:2152:b0:2f2:b544:2fca with SMTP id 5a478bee46e88-303986950a4mr8945348eec.33.1779174816039; Tue, 19 May 2026 00:13:36 -0700 (PDT) Received: from sw04.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-302977a9474sm15631673eec.25.2026.05.19.00.13.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 May 2026 00:13:35 -0700 (PDT) From: Zong Li To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, debug@rivosinc.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, david.laight.linux@gmail.com Cc: Zong Li Subject: [PATCH v3] riscv: cif: reduce shadow stack size limit from 4GB to 2GB Date: Tue, 19 May 2026 00:13:33 -0700 Message-ID: <20260519071333.3820429-1-zong.li@sifive.com> X-Mailer: git-send-email @GIT_VERSION@ Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rationale: 1. Shadow stacks only store return addresses (8 bytes per entry), not local variables, function parameters, or saved registers. A 512MB shadow stack is far more than sufficient for any practical application, even with extremely deep recursion. This 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" Suggested-by: David Laight Signed-off-by: Zong Li --- Changed in v3: - Remove max(). PAGE_ALIGN() already rounds up - Change stack size to RLIMIT_STACK/8 with SZ_512M cap. Suggested by David = Laight Changed in v2: - Add max() in case RLIMIT_STACK is smaller than PAGE_SIZE. Suggested by Paul Walmsley and Sashiko Changed in v1: - Use min() instead of min_t(). Suggested by David Laight arch/riscv/kernel/usercfi.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 6eaa0d94fdfe..2036918a77db 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -109,15 +109,15 @@ void set_indir_lp_lock(struct task_struct *task, bool= lock) task->thread_info.user_cfi_state.ufcfi_locked =3D 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 + * 512M should be more than sufficient for most applications. */ static unsigned long calc_shstk_size(unsigned long size) { if (size) return PAGE_ALIGN(size); =20 - return PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G)); + return PAGE_ALIGN(min(rlimit(RLIMIT_STACK) / 8, SZ_512M)); } =20 /* --=20 2.43.7