From nobody Fri Jun 12 15:50:43 2026 Received: from mail-dy1-f175.google.com (mail-dy1-f175.google.com [74.125.82.175]) (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 CC8973B5842 for ; Thu, 14 May 2026 07:50:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778745042; cv=none; b=kqWQGgu+mjVg9cjLEMw2Jx6uUWySRlxtvvbGn1Rx0ZtegvzmPl+hW8RC6z9a6sOn08NzkuZ19xxMO5ThHGKE5bZydD77ooOx6zEkl6wlkypJ5f4yAHVFR08283guHGYsNytrYOY1Hrei2bMfDcOZLwIP2y6QUsdXtyOkoJdQAxE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778745042; c=relaxed/simple; bh=6F5Sp9VPcyLipRitCT3ZvCzbR12TE1+WB33cMx+08AY=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Jwy2UO2PAwo2EFxGnTVd/lB5+DdLl6K9LJIIjfR0X7uW4kkjx/AOnT2bLRRs6Elqor1dSujvy3MDq38ETqXdhqFIEtN9gd3Ux1gjp2KTbhbQrtSaCYyAiW/t/eorgZfOGQhpsYVs+YMA9vUDhePRJ4zD4TUVjjg2/E4rpUvUPVY= 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=bSKFbNaj; arc=none smtp.client-ip=74.125.82.175 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="bSKFbNaj" Received: by mail-dy1-f175.google.com with SMTP id 5a478bee46e88-2f7020a928eso10730388eec.1 for ; Thu, 14 May 2026 00:50:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1778745040; x=1779349840; 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=RDKYjZJg/Bo3EjMAid7K8WutRS/dE3g6XX4B3GZEcbo=; b=bSKFbNaj2ug3UlAgZD53M+oLm5X55hIUX+g/y4J7u4MasQVb4mGsQTqUCLRjLizRw7 JYLMvJ483XdI/G2Zz3CcVTcJiwLG/swUyXsP5Sb+vxB+lf4XwvtulGRTC5tItf1S80Mk DxVPwJQtNG1ePysxC89Yyiji617ctq0WTOwgiQPkLpznwPl6V+K+xW1i754H7RmgcLn7 pKHv3p3cIAoZ5kGEjM4KziYj68JcHpzQzI+d6YMybhPt5gN/bvSAzKkXdebvHviXxd+2 DUcAzjjoDxd2/U8rt+epynZeTi43I1M0P6mE0X06C31mP9UniaYhuajnVxUgjE08xuvD GdGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778745040; x=1779349840; 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=RDKYjZJg/Bo3EjMAid7K8WutRS/dE3g6XX4B3GZEcbo=; b=R+bSpddVrBWfbn48+Uil0OiqaTgiN6K4HT2bHredVxI54Dep2Er3tl3PakAc8FH7bF TwULNqmkgDfk0ymJD6Pan+l9kUWULl3FeL0BAQm1XrvLE2U5CbsS4pE+epWLyFK0TCCz +bk76CeOqpySQtybT3ulnIII6Qq6dUpdzI+9l4YruJEQD3Unl44Fh2xUt2Gkny6rZmXj /9swDtBIsGMVAbd3FVeh3j7A1ELETZAG/FWMBqmXHkUmVM/0KxzrbKn38BN6jRd1Zc92 cJU1uHSrwfgWVZdCclwcnE/cJHKRyzxt7UxZiNSYefW7fXl4YF20Awxd+durb0RYvrLD a5RQ== X-Forwarded-Encrypted: i=1; AFNElJ/tX11u8g60pdYsTWDU7aTUqb2g/96LHQV5m9fWmvZjDv29ubcGyPFDq6vmMDxc0VtXgg2dVxDYtYK/iKQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzxPX016vnwaD7kWMO6ol31W/iGEzkb1ML2Fcf4oTZosPz23X64 5ZZjnEbybaGr5+llrCpymhN5KAAzz7+jVmCj3Zb611sfRE6pv2/ve0U9MKyuqLOAhb4= X-Gm-Gg: Acq92OGUySOXubPTHHtePqB2BiK4zUsvnLbhXwPqGvZZzR1A9AlxA7t9WCWIUWCS+oP tNN/rmcGLIKdzeLMjB71RfEA5suTfE2+ZsDHcI/BRQsfIeJwAzROopOKfZpILIPk7x+mhm1ecrA NxQAzQMRcKjpbPNpPeUcXiNsoD2Vz7du1tImlObKP80XHm1lBGLGtoF+3MZmMzZjYGZFtvIHFmQ 5EdTHzncU8a+igsaFN3ok43tlyV3N8JxtM5IvyQwDqMGHes14cyPhMezm0zJ+suPgQuPvapnxnh hGn0cL7mRlz/VfjyQuqQn3HbnJZdLpG+la3ELc1a4QQMh1tZoFXFI1GwEx3VaL0cA1mVCmoWZPL GeQm+nFb598ANXLOfsgn8r3on/ZOiODsw6thU0DMdzEJ//4jJeaY8EhBtGooa/gdgFIBz3EyNUk F5ZB5hynZSuOLAvoxyJVq+XO/RGEulsrNf76Tj6DQps+IibA== X-Received: by 2002:a05:7301:2901:b0:2f1:496c:94d4 with SMTP id 5a478bee46e88-30117c736aemr3622831eec.4.1778745039815; Thu, 14 May 2026 00:50:39 -0700 (PDT) Received: from sw04.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-30293e2ea78sm2090985eec.6.2026.05.14.00.50.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 May 2026 00:50:39 -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: Thu, 14 May 2026 00:50:35 -0700 Message-ID: <20260514075036.1432352-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. Additionally, add max(PAGE_SIZE, size) constraint, which covers the case where RLIMIT_STACK is smaller than PAGE_SIZE. Signed-off-by: Zong Li --- 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 | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index 6eaa0d94fdfe..0f75e8f5d0ec 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -109,15 +109,17 @@ 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 + * 2G 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)); + size =3D PAGE_ALIGN(min(rlimit(RLIMIT_STACK) / 2, SZ_2G)); + + return max(PAGE_SIZE, size); } =20 /* --=20 2.43.7