From nobody Mon May 25 03:55:48 2026 Received: from mail-dl1-f47.google.com (mail-dl1-f47.google.com [74.125.82.47]) (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 319CE3BD63B for ; Tue, 19 May 2026 07:18:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779175094; cv=none; b=n+M4Lcoxh+yykF6iSX/zMb3OUCtLP/xoci7+DbNOPVjVF2cicuJJIsFKCd3wlfEF8h8T28k4JhInnUzAUoiztQm9x4eyO8t28ppJmbutSeGUl518xvgRSTCbz8Vf6fn4+q7dSAaRSQNKcVjzITgb7wYFLAHUVMAgfbOWqrY1pRM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779175094; c=relaxed/simple; bh=XA4FS72kRelg15OP83i0VFm0ZHFcZKD9G4mUg5fXsik=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=V6GGTLm/HPP8NUAmUu+pTs+WlMenWxvZF1G4bIKI7OJui3aV4oWcBifvnV9SVhhUGJt6RQ1AiJnAtLGu8s2VZMqxtRBabGZcU0cXTzgnqrkrOwq1wQyjjx/bnu2CRlx3ujgSAq9UNOgYQTr9o+/n4Hp5DO5M7ZtNfZorW36bP9k= 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=dL/ObWQS; arc=none smtp.client-ip=74.125.82.47 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="dL/ObWQS" Received: by mail-dl1-f47.google.com with SMTP id a92af1059eb24-134ac81c445so11576352c88.1 for ; Tue, 19 May 2026 00:18:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1779175092; x=1779779892; 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=g7y0FJyTHq8D7gcxyr7jUIMKaRAyJaeBHi7Q0uKpFT8=; b=dL/ObWQSP3MevvETgh/Gney6i+n+ywbfd2xf7W6pUn4Lj53N2K1qc7KJPaj7zWYPz5 ZI5sXB1NoBWScmfehN/IgsQ+MqcMWymp15IVtrSSMYyw3k7brmnnFnn2y6gvwuP5f/6m TrKX4eul5njtiVPsmXdfd3w4RCq1x4avkdePtr3u+3gg6wQg9nMPAB09Z40FyGrmjCh9 dei4u8jdySxpqgcMNU9pNtuviotDXBWr6h9eeWZN3HIbTutO6OrM8AIRXbdGibwDjIa5 4TIi+eMmaBYS4SbbJ6JoXzO5zBBFhTqQEf5PtfgtBls3nPDCIlCgI4LDfY+K8teLLibB 2ZfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779175092; x=1779779892; 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=g7y0FJyTHq8D7gcxyr7jUIMKaRAyJaeBHi7Q0uKpFT8=; b=s8tOs2WHjy0/n7AdWCKg9FwNnVPXGmGbM9Le6ntvR/WzvmF90r3SWn/w4uoPo5cN3D cGMlDtJTN6zY9w4/2z8hgjzLbRz1HHcZww2Ik/xxyIxCZ2uBr3vb+VmYmlVOfWi0YH9j ooIPjN+3+9QuILMKURc/C3MZ6xwOqNZUMXGpn+iyv64q5EdQotkNT2f7oIJlaemNoW49 RYlS12F12Q756Ay+4kedN3wW5t2+juabHLByIWDJoVG0v1Zh5L95Wa+bcd+guJl3brW3 JyeyYc5ItP58usNmjiBbepjTk2UOnCJmffeRtIa2i/UXX5a0tzGQC/0Dp+nTTRgO0OEF v7/A== X-Forwarded-Encrypted: i=1; AFNElJ/kJpRqum1yyH8ce88sAdglgZyLsel8IDjmpqZVOU0W8W2p52+uhQZjInMc0uWkbdp/AQvjNJOSe6MmbIY=@vger.kernel.org X-Gm-Message-State: AOJu0Yzph9tpi5SIP0/4KOn0dQ5uP6W/7Qka8TgkNJKviV8R5HsK1TzU bsWoCFeewMIdMSxhOH1M7Pyzl+l9PvQb+ezvfNOFonpMwaGIKErl2nmkCn12Iu3L5Mo= X-Gm-Gg: Acq92OFMnbJIh6jPNXIzaLaiO4khyJOy7G89hhBb8/wYbTnF6/UmF3kRD4zYpkUU1VF Rb3XQQPty4uDxuSPaT3cgixyZirVtYbKZ5bhl3eAE+cZjnChS5CCaUfAX9e/wiUYxe2xjXbDhVf tD7kccPCDXddMVB31U1mdaeYAZI6QLF2jz6/FFzMi9MKuATtqoedBp/vgEW9w3qpew50kbLIVVc UWeHMr16Q4ZbZdnS8MTO35CvfQmA1ZJ5y75G+tbzhHHxxeoimosaL2eZJBvYkK4Cb8G50s7VIx7 rpxb0l2HdNYTDyTC98POlIgL1E2Mynrkc1zR2Jq4UHLqYE6A0lKpSkappb8cE62u+HKFVx18oug NTCU/8pSN7U8OIJ4xsft8tLGr0lMHpxpLPNjaPTl80CET8ihFcrBui7KnAx/FP+s0t2BAFPx6bi paWfe8XaiiCtUT8HFXH4BgdCp4133kcWweKDg5fpX2TuRqaw== X-Received: by 2002:a05:7022:43a4:b0:134:a701:de8 with SMTP id a92af1059eb24-1350441871fmr8092871c88.5.1779175092219; Tue, 19 May 2026 00:18:12 -0700 (PDT) Received: from sw04.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-134cbcb93f3sm23368188c88.3.2026.05.19.00.18.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 May 2026 00:18:11 -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 v5] riscv: cif: reduce shadow stack size limit from 4GB to 512MB Date: Tue, 19 May 2026 00:18:09 -0700 Message-ID: <20260519071809.3823470-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 v4: - Fix wrong subject. It is 512MB instead of 2GB 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