From nobody Mon May 25 03:55:47 2026 Received: from mail-dy1-f171.google.com (mail-dy1-f171.google.com [74.125.82.171]) (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 44E1B309EF2 for ; Tue, 19 May 2026 07:14:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779174882; cv=none; b=jyotV9DCJLZLBDRBCiJ82NFEmKRJGmrOd956fGhAYb/w94cirssP0i2nhGj3byl+pCi62uIDABC4fHdVcCmYOtCB5ENrNd2nGYFnVSxwHcy7/FnaLhSZimWo7PmRFkVX4NpWbhDACQ4t4CPUn342kSZ06XqViMlQby35P0pO76Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779174882; c=relaxed/simple; bh=FZIzK1KSNJmse9uzYQL5b9Mx2I8FFd8c2MtcegxgcV8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=fBpVvQ+82Mj98/ch3KH+YWQCHFd+ImBKoBjxenXQB/kU0cVcBghY2+DQPAVQBwLRFD39Kv60wIGi+RCUBpHrIiztFoepgUDLNO3gN9rLWJjk8l153Pp6+TfoZnkLwVQkv+T2eSNFdntb3I2T6E0+KoUuZyAid3wIA06NuDR/6Jw= 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=J5kA8vgr; arc=none smtp.client-ip=74.125.82.171 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="J5kA8vgr" Received: by mail-dy1-f171.google.com with SMTP id 5a478bee46e88-2ef2a1cc06dso3019307eec.0 for ; Tue, 19 May 2026 00:14:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1779174879; x=1779779679; 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=J5kA8vgrftZWk/puWmN6T6dlwuUVmOmYRf7hJBleEBru6DtX6NRNj+hBEfbqpdZa5M MlvZYE0jkQxDefxBHs4PRO9TJvY30Y2bYLqnUPLj1gEfRVecR4U45ttULVC3rChugMgL 78DxBd5ABPM00zeeS3AORb7+ldkholN0WK3uCJGe8rN2g6VrZNVhs4sb70lnCu3KS4XW 4bgcWlTdEmvxSyjbBe3Gdn6WbLZ/ONz9TKmBV9den3yDvxnsMy8AzZ0rQo71Q+vcrKg0 TuS35yz4Pw2gdTPsDShua3sZjj35EwasYgpHLzB0yUCP+C6MnlxHl3Ky4LUCpJNzv7hz Suiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779174879; x=1779779679; 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=KJiEaHHfNSvBzSaM6Nr3nYoW80FW6SsOZ2sYUNrNr4SRffwP2dvnx0B/vxmmPoNw0H BbJeevZfKW3Zyu8XxUP0TwmaixxP6uvIQyG8X6dV7qa+IpOR3VcpFWtPOgPltL6bRGkz sQL4F9+8L0iwjKnFIFb3xHmFMm4SagarPjLC7ilJ8Ws3VBrS7ndjcneFM5bO9eTvdECK WOW7U/1+n6+aXykTNv910y0AroKFxfX82nsHtiX9bL8ceg4Ie7WgxOTwEWIdP22qYRuQ KejciTvLeLCP/xli03UdeN2CJeTjTyVvSIisee1GsdzD9mWLTHWGJO6EW0aATp0XpJuT QjMQ== X-Forwarded-Encrypted: i=1; AFNElJ8ur5nUeeT1u+5N4wqesBKKM9/DoBma5fiiXk+OupBFyistmE2jBEfTj2Ke4EEOhjNUvuiI12DOs05Ad5Q=@vger.kernel.org X-Gm-Message-State: AOJu0YxHlmmmIn6ogEtB2wQrScjKaenB4VpjP1a/K0RMqBzQS6uCqRN6 y99yXtxBpWP08cYUZ56SeYD5v9ptn0n0RKruzG2vqIgiWNOKjrwCHXQdnjigDr1ssJk= X-Gm-Gg: Acq92OEzsTOuAb73bndFmKO0OvByXhAFwf0DCBF5eV3a0QoFOZP+GtGBWlXgqEgb4bY g3KeEr5igtsGeXB6DreHjqyF357HWqxhheIVNZMtEh5VqI2vqXAtMpYpFJ9/yoXO5AxEcL4DGZa CP0u6mLHBxOxiqQywhh0n+I/fKsW54ke13BkxX/jSM3Xeus6iYGIFsW+UD0FMrbUm3iv73kiPf3 rWeqVwRGsAwAgEdlPXB+TVwZZtdw7S/AvVPz+dBlH/kn2LGFacoUo8l4rmAnF7mXpwPfKMSVvbl nI0NL5eRu1MDT3IjAld0Y9b07E3tYrkf9AFR0FBA1V7c/yENe5z0GKlmCS6Rjb396/tjnB2bVf2 oR9I2GAalIhKiTcyMuBlFOB13Mp720MeDEMkqiF9fMQfxVGZ+HNV/zg9gbo/LDVNR2mCJLNsz40 cSumrBbctzw5apIYiL2Q8hYCNoZrIlbSSRJZc= X-Received: by 2002:a05:7301:169b:b0:2d9:32c8:2b69 with SMTP id 5a478bee46e88-3039865fe40mr7744836eec.28.1779174879153; Tue, 19 May 2026 00:14:39 -0700 (PDT) Received: from sw04.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-30294500726sm14733801eec.10.2026.05.19.00.14.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 May 2026 00:14:38 -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 v4] riscv: cif: reduce shadow stack size limit from 4GB to 2GB Date: Tue, 19 May 2026 00:14:36 -0700 Message-ID: <20260519071436.3821558-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