From nobody Wed Jun 17 05:13:21 2026 Received: from mail-dy1-f182.google.com (mail-dy1-f182.google.com [74.125.82.182]) (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 77E0E175A74 for ; Tue, 28 Apr 2026 02:41:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777344070; cv=none; b=afU3B0nhc+0lMGvhJIEUFRJ0uY5u4r9L0uPmo0ezQKF+EbJJ7GI1dC7GjQlIx9boa7YMUae9RBa9j/RxdOI4IQYrPHdojjWpnG1H9cmq1XIip/spYFMTRHYG3zAiYyMy630Ikq5OzGWHFHxIk5wtC3V6b6IapKLgUI7LgxKVfXQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777344070; c=relaxed/simple; bh=/XGi10DyDugOnEchXzbzh0zRjo3s8n9p93nq3cRXBEg=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=dRYBIaoQ9Y/nSs8BothqmrCzPtnMproVnn1L5pgs+POkyNuNDB1cPHhIkXG0P9xJxqqWnm2/8i5rmuKBhB7iqCXzTAFu6d9Q7NrITLAaBDd2SDWdl+jE1B3Nlb/QmTY2L7FO/5p6hMh3agAvT1urGgzXenNxR7NSLX1C1RXWPKo= 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=CemhGQbE; arc=none smtp.client-ip=74.125.82.182 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="CemhGQbE" Received: by mail-dy1-f182.google.com with SMTP id 5a478bee46e88-2d96243c91fso17779577eec.1 for ; Mon, 27 Apr 2026 19:41:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1777344068; x=1777948868; 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=xBnMRKm4HOrWsem4bfxRVhKbbPsJU4q6tVVLOb0/SDc=; b=CemhGQbEewySAiYbMzKfCWhuwV8Mm8UIZvC4DDVR0FVJVR92LMzNGff+vCA2TN84/5 KBZMHqZEX4aT9ek0Trx3R2Tkyhye34s7S3QLOVjdzaggnZj1BYQ3A/+UXug0F5ml5yZS yEDw2cwsfydLwJbFpW1/SAnIxsuM/NA2UI+Gy5tkkfdtUbD5eXPE+FfE3ikOpBmXMPAp rYbPxnx0oy9S8nCelYCVMrBeI48wS0tXvwoIiwU8oejy/r46BJSIiHvDT94o1mb1+OfP tiNQlcDM6QSxt5O9wi8U+LpAhnCV+iA7kzS2FHo2er0fK996fs3xrfLfQxB2b+0oDYhR BkDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777344068; x=1777948868; 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=xBnMRKm4HOrWsem4bfxRVhKbbPsJU4q6tVVLOb0/SDc=; b=OxPqYkhUzyBBi9mBhIbTZuBFsZq0RlaxkvaStWSCu4q96/UQpRNoRDm5kVJYJeMI8d Dt5bxcPgDn2tXDteFDwYpdUyvgY/Wzk4da41TFOsrZk+Wxc3o2uGOtpg3en8GQVR9cpH lSGktLOhNGVtpMefd1lMjIOOxXqrnLbIUV6cln5UKmSsI5ou8bCG32ULjzR0uGuxxgXT AnkT84ii/BQpZGlhvVEdZuijfvIkzuNLMrHxjvTCwM4tuHszaL0Qy09rIT3bvoTpFy5F PcztEXM8aqR9Q66VrS6J4aKBMt5nwvCTrIkEwaXaKSAsVtaC52EWrYzgCTzfCZu2XaH8 +gdg== X-Forwarded-Encrypted: i=1; AFNElJ87YUzZ+fn4/n+VZQZuN7pJlC6z6Cz7Mej3MJsUE3P1neMNr8Fn08A1wm7598h8MjeguAylylFU68t3KQY=@vger.kernel.org X-Gm-Message-State: AOJu0Yyczl6uDa/2hQttrEb8H7SqwAmSpE65bnLA1iqUkqFzoz8YNCBk 3yFswYdcWkDGy01BCg9cHwCPpoXS7TcezoGVXKEmnyVCV23E7hgnFppl4xHyADEfhko= X-Gm-Gg: AeBDievBJY+M/F4oQmOpx0VfilP71MR75eqDSzCgLIeeFZNRUpfdeclJ4+zwz+8JPHX rd/6xIimloQ1GUX3y9CbgpKuUsxO0/fzDjDKnAcqyFBGOca3TPFbAsds245u367++8yAeSGiQhY ItEZNpv+ryqutxeQ6yuy3vt0nmlwjxqoxCBhCj6rmgRQN79tQ8l1KsWZBC0IdSZ8fxHS1plLKBe Dld7szGjX2yrJcFEVW/KPezFRWnualfK0tMWIh0NdJj8MyCZsCzaOADEjuhu5PE4m4+RzorwDbi sEmFehZfLkQyUwEhOeB/HNhOfOIsUCsmco9LizGTvTVbDTc0kbFHVh842SQI88zBgU6yaq+wVdo X8/jqbaqD4NsTiIu+lPceP/tFKhAA0pGkYTpSlY3bmPBVfg/vsA646ZWDuNJsJ3zOuSGJDeOJlk 5pLw/wzEoKrOIy14suMq0IYaH6Z4/CyeuXFykHoz1rytJ52zluCx5mYo7Z X-Received: by 2002:a05:7301:128d:b0:2e6:4d6e:2cbd with SMTP id 5a478bee46e88-2ed0a03bbbdmr685314eec.8.1777344068469; Mon, 27 Apr 2026 19:41:08 -0700 (PDT) Received: from sw04.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2ed0a10678csm1273397eec.24.2026.04.27.19.41.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Apr 2026 19:41:08 -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 v2] riscv: cif: reduce shadow stack size limit from 4GB to 2GB Date: Mon, 27 Apr 2026 19:41:05 -0700 Message-ID: <20260428024105.645162-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 --- Changed in v1: - Use min() instead of min_t(). Suggested by David Laight 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..cbfb4e495e9f 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(rlimit(RLIMIT_STACK) / 2, SZ_2G)); } =20 /* --=20 2.43.7