From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.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 0737438D401 for ; Thu, 14 May 2026 08:56:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778748970; cv=none; b=o7KxmVyVTLyDot9IyaFfv0tq+3DhVcDHeNDMJkRdNIU00YQMrGnQUb40oIiTTmd0PNcLONSrsk3yvUvRfCeDHoGyM5txI5Hg8amUY4pNwbgTOKr3wLUYmLLcTha2b7/uVgR0vKP8BcJfw7atdQQPWNgxsH9kzOyGnZlNdByLgjw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778748970; c=relaxed/simple; bh=f5oa743mNKochon8Nc9IAH5vDm+S52QZbkUG0ONsSms=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=j2bHRtDB4AJSxvn/zkFtBpS0uI1PNxtnPYsZHmX9Q1cPiTyS4idp21rzXMTCLg1bmQyqwWFD85V37pSkU9Phxt126Dl6kl3KTgDk4nn9wOOm4KhLApU/5DH157EH24uI9GyqXiQ2VZc6PgmoL9PM1JkRsny+tA0w/YXDFE9Ol/Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=oBk2hgsd; arc=none smtp.client-ip=209.85.221.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="oBk2hgsd" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-452169ae568so5927840f8f.3 for ; Thu, 14 May 2026 01:56:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778748967; x=1779353767; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=t3LT0hR23Ad91sR/2WKkYMfAOBkKoFQtEPupdXVlcoY=; b=oBk2hgsdSXu5wTlcDKtEjBbKmhjNhCzsbhmyL5RzHBlb/mHihpN0J7f6HXBQWU92ou e8Hi1E/iOrlFLziD9Rncz9dJfCZC7YVF2bFmxP72c80E4zaS2VgiCkhoTxED7bYQof3I Mj/yCJPY5BhrrStv3KHf9Oub51Mk7ovJRnAmxWs6gkSaTQT//bvFaY/GdtLdeWPaXXKz QZuPUL2yAnIdscSwOmv4ZLauFmfTVZGz6btKrt3oBwJ4e3/bvwrKbzy1Xj1wrGd92Sb0 QaLbXQ6y0NQ8GgRmG8iczXBk3dHHOf9zZf4e/Le69136nlrSzPlJJ180OAl2NtinXLcK U79Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778748967; x=1779353767; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=t3LT0hR23Ad91sR/2WKkYMfAOBkKoFQtEPupdXVlcoY=; b=p5DEBs2MHyV4Ie+A6nNMCOVuLpGh2zlKlnGTvOm7PsbxGf86Fp0NXH4A01IYkYQ4fk bSFzFOANVAGgBfDBsiz3E9Xv6OWFZaUk2donjRPgCoQK473B291tSJGF1/E8rc8+a6Ye 8gRB9pkrzfFtiErvYgaj4GpofAI6VSTZAfMHBbJRmI82dsdit8ksjVeOsl6xkYEKSWu8 65nu/cV6sRgyn7u7yFeWOPzA1JChnrkfFSEoG6UVSfcORK8XHbcvK+cEGo+t35pLtRnF RBD8prfL171w7WlL3YftMXZVg4L1+vylSMxuy63oS5Nwq4C/KTVs9xtRCyp9esvEHm5a yROw== X-Forwarded-Encrypted: i=1; AFNElJ8RmPLMpnaFPh7/Cn/7dnB9GhFvfx0Kjo1BwszLEVJFjqWI90yZGawMEpIJw/OcQ8DXgRcVg8u2G1CwZPk=@vger.kernel.org X-Gm-Message-State: AOJu0YyiY570TW8xfwqafffX4HPt/z7zQeZAVjGNGkgmQY8adu6i5ie5 aZvoepO4wwes084PiqcfweXh5w1MhsHtq9GBUAMWJSWbbYRN8tF19I0g X-Gm-Gg: Acq92OGztnj3oLGtMzXoi1mNrnF+whHK3V92u68fku/EM5U9TtK9lfQF0zjLO9jeAAg RBIrtTJU5G3vCYFKc1zTmZ/df9Ofdz52wT1MbIT/OjdTK4Ww/ybjQjmgZVf/nhin2Bt2dByGJE0 h+9O0Bpe1bxPF9G2dNJefBMYWtjwXdvNs9FL7huT+KZRGTM+HsYhLQmQNMfSObUIcKBT9MxbA3C LK714W87spGcj2RIHku6mv3osM3hMVg3RTUhaTyvVVHw3cnrlhhHFWhuW2TPmuYfmEK1I38YMqV beU7vDmnSKyPuYhlmERDFehqZdR+PeWO9i3P87AkiPAt6Vuoqkan2dQgVSZpuAx3qBLz1lB4tgm DIM6mkGeXTZ0qpGXJMfbRZ/MuoIYu4rBo165BP1jxFHK0a03SQ7fl5WTiNGhWvqNRmSiC2jREXV PYMGwUr9dD3Wl60HuJAGP9vQG5aBZsXDO4IOJ/R+KuYde1qLGQG3tEXuzY1Sq0 X-Received: by 2002:a05:6000:2dc3:b0:45b:d2ff:4d4f with SMTP id ffacd0b85a97d-45c7a8d4c68mr10226319f8f.40.1778748967200; Thu, 14 May 2026 01:56:07 -0700 (PDT) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-45d9e768072sm5236440f8f.5.2026.05.14.01.56.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 May 2026 01:56:06 -0700 (PDT) Date: Thu, 14 May 2026 09:56:05 +0100 From: David Laight To: Zong Li Cc: 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 Subject: Re: [PATCH v3] riscv: cif: reduce shadow stack size limit from 4GB to 2GB Message-ID: <20260514095605.2c7d8761@pumpkin> In-Reply-To: <20260514075036.1432352-1-zong.li@sifive.com> References: <20260514075036.1432352-1-zong.li@sifive.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 14 May 2026 00:50:35 -0700 Zong Li wrote: > 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 = 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); > > - return PAGE_ALIGN(min_t(unsigned long long, rlimit(RLIMIT_STACK), SZ_4G)); > + size = PAGE_ALIGN(min(rlimit(RLIMIT_STACK) / 2, SZ_2G)); PAGE_ALIGN() already rounds up, so the only problem would be if rlimit(STACK) were 0 or 1, I'm sure that would fail earlier (or not be allowed). I also don't understand the rational for just /2 and the 2G upper limit. You need 512 nested function calls to even use 4k. That would have to be quite deep recursion. -- David > + > + return max(PAGE_SIZE, size); > } > > /*