From nobody Fri Jun 19 08:41:20 2026 Received: from mail-dl1-f51.google.com (mail-dl1-f51.google.com [74.125.82.51]) (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 76DDF35F199 for ; Fri, 24 Apr 2026 06:55:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777013746; cv=none; b=YyktL3ORZUKKWFo84TS+asGS5CLa3jDMiaVsrIlf7dDjURNLAh9mJQZn3P5FtdancvaR3R7z0NSSwpt2lrj/kNRfNVnv0Fey3M6cWzQed/X/Jn3Ewxp4TyJFYd1nA3l4wJF0TtNGQyxXHcToapXey5XxuGYmGlpdYHts80KVVIM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777013746; c=relaxed/simple; bh=1qKeCOQOWS8R5C86kinWFJTytiPCRBL2mFpNDpribS8=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=acGtPo8pVWSFsI5v2pPR6ipD+cDt6bpUN+BUE81HE0U9G1SlqWhwc0aTL0d4UUZKRkWczl8JKqiFgAraoojEcPuV24rKCT5ledjtW29xCu3Gcj77hq7tRe0nq7pcjGigJyd6O5vr73GFxJVazj6D/Ac8F/XzOqqbQioiR5YGMic= 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=L4tg3JpM; arc=none smtp.client-ip=74.125.82.51 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="L4tg3JpM" Received: by mail-dl1-f51.google.com with SMTP id a92af1059eb24-12c19d23b19so9711212c88.0 for ; Thu, 23 Apr 2026 23:55:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1777013744; x=1777618544; 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=wlChXpy0nN0oSk3hgPhIo49rDQ+oIcHvjI/gnEjTyQ4=; b=L4tg3JpM0TNNWJbRb1s5j+orZptuZz2npe/Qj5oiG36E1jpd/ZbBCpvVdJrKan0cA7 RuRdhYBIOBAKesjgKBrZUzt5gGw/sxZ9fwkdv80KGG9ug3qUhzlsL2+o2GdDK6TvY3Km KCMzwYZz9HkckZnHuEI4vVIGAX4QSl/mxVPc6h15alleTff/FRRbm8QiYah5mJ8uQul1 EPTD6lEK8wZpa86OTQxr2mbBOo9Brr+A98OV+NXT2+jpK/KFQ/4/K30w8LH7d0vATP7R w4LnbgAG5taP7NcLxDDtbyYCBrFNTZg35D8wVZTxHymRDpFD16ol0irq7KIktjDNRjSh lCtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777013744; x=1777618544; 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=wlChXpy0nN0oSk3hgPhIo49rDQ+oIcHvjI/gnEjTyQ4=; b=hhvn8zouOCjflJKTCiZQsCj0rOs9/7r1a31PT9LKHHpIH8Ur2QwAkzaB7OS2EIQVBf SD0cbeY64Kmoizz6rRRdQDOfse2S25up5w0Rk3hwFH8IPdfrlKVPTM41g/JamFiszc7O JQJD9K5PEjNlKQYcad/i5loV7Et2fzdNUUIj01njMZXMWbdMvdXAoNJLFAa46aAmwWid /ct5fJOEXbgEiswOP2o/Anb4DtdSMubHpJDO7fbMfDdC+uu8PwLaaDesQ/YBsMcbfP6l Rz8AlSmQbHCJx9xKD1G1hVK0CLjtIfFU8UDQRIUoLb+tETWQgk7ylg1AEOGktz++BbcJ S7cw== X-Forwarded-Encrypted: i=1; AFNElJ8n6s7Pbzc/69b0P1s5nwRU1BwTsnQVW5TwBa/vjcLlTKPsVZINYQmBmWYU61f2BufaznhOL9h4Qp7bqWI=@vger.kernel.org X-Gm-Message-State: AOJu0YzZFUAKUeNhVIXtT8SC3ONWME7wWOZ493H+cqHQE8Y4Ygigdk/M 7hpyrvKxRqe4kOU8I6Hw/IQPd+7xtQ+dADoJ6lTjTilZ/t36xpNRd8WFjQtWX6jirJA= X-Gm-Gg: AeBDies5R9zplqjyByyTw1iejk+kU1Ij0wGCImULyUign99HDNUMY8jkLTSD4fLEmv+ jcCAyZXbX2xjDdiJjaG5wpBERRX9HWuSbIabUwMfiVoxwkdKLLgbPaiZfiYaesWCgprAKK9mWWX 1/ogVlkr5QXNTjnC3+lZ1CwRynR/nr7paiGnOG7yYvC8XQC4e03m9ONlIEfyqqJmpAVnRON4OTl LS/NbKk5uWrye47USm5GA8OWMjRwFiq6vMh0KLv+pMTN/LLHJh17dMnMaeCln4AwK/3q2AzbGWu yAj+9SMQ6YCP/MPxJHiTUiHynRfO/ZjJnSZ46GKpCs+kSIcgHVU3KQQYJZljMUwYfF+yfVhdYCp Ej7ZBNG3uQNywini5ICKiLnMlK3shR3sfwY/lGxZ3WVsYfBwQH1R/Q3xnEocjsRpUdBhs/zB2mh CgYu+vf5Q4FrOXrtTGr8myscaO7uAqcJ2ajRG9iPRrg8Q4LbWGqVsCuYyW X-Received: by 2002:a05:7022:79e:b0:12b:ec15:69d3 with SMTP id a92af1059eb24-12c73f99c8amr16945900c88.19.1777013744478; Thu, 23 Apr 2026 23:55:44 -0700 (PDT) Received: from sw04.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-12c74a20eb5sm39271101c88.14.2026.04.23.23.55.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Apr 2026 23:55:43 -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 Cc: Zong Li Subject: [PATCH] riscv: cif: reduce shadow stack size limit from 4GB to 2GB Date: Thu, 23 Apr 2026 23:55:40 -0700 Message-ID: <20260424065540.3480755-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" 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 --- 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..4a48dd28d113 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 =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 + * 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); =20 - return PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G)); + return PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK) / 2, SZ_= 2G)); } =20 /* --=20 2.43.7