From patchwork Mon Feb 10 20:26:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13969351 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 620D4C02198 for ; Mon, 10 Feb 2025 20:27:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4zqeylQw6K1XmeN+KmrsDIr9DHpPuxliJ1VDOvHF6kw=; b=Xe0CMvBoNlVx+p spCWZFU2hHc1wYG6GVtNzfLEB+qfn362mBcArjQiotEbvXacsi3RL98TdSzpfYnu2dw8TxtKdzi15 BwI9R/hEBTWGBRyjNjntndoDwZgQXhWKsMe8DNWJwbJ9n9p4ZpFlLfHHq0tYP1DlLfdEjQEqddCgf KkN2BSOXkfdujHCYltINx0PMk1jeJVGa1MXaOQLi5JxZiM1pAAuXMtdxgFICAzJhHeNgpWyYnqda5 +KtavupmwM/EMsMYxJX6ACpXlGGczHB3KarYzEgx+YvteZyGoDgHGExVKnlFqUVWE5dFJeLBjPYCU QjwqOnof6iUHTZrZg31A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1thaNL-00000001KOw-0neT; Mon, 10 Feb 2025 20:27:27 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNJ-00000001KMB-0UwL for linux-riscv@bombadil.infradead.org; Mon, 10 Feb 2025 20:27:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Sender:Reply-To:Content-ID:Content-Description; bh=N78rd5bu5L3Nb7UCylb6NhXzPkib+ej1wIZfC16ALRg=; b=nLBe0smp5z0fCktB7FwNphjJD2 gZLW7nbgR6hY/MUvCtzQB17aGpoY5yp3+bnuCNzQrwi2MpxmJABrRmjo9C7atp1093J/0pSIgYHnr cJWNOsAvlNPVxGweu3bGGUpPUeuQ+OdpLoMMR7OtpwQODIJcnzEZPtvDDUo5BLrV3/uUn/KTLiI0c TF700l2kFoI1CjfWPnm9iJBfcDKQfjss2sKu6Qemz+G4XQ7QrqOeuYqnIKjbazFzJ5p4Tsn7lM0gB pFc2hmDQvQxB20J+HPcM5FX/wfkCi73zM/6LdkM/ugxhTEs4cR2/EnfQ0XeYm6hpkOGrfurnJ9r52 2wCOTvfw==; Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by desiato.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1thaNG-00000000DgJ-0721 for linux-riscv@lists.infradead.org; Mon, 10 Feb 2025 20:27:24 +0000 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-21f6d264221so28882625ad.1 for ; Mon, 10 Feb 2025 12:27:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1739219239; x=1739824039; darn=lists.infradead.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=N78rd5bu5L3Nb7UCylb6NhXzPkib+ej1wIZfC16ALRg=; b=DOtTtJ0o4w2GV75fdbzTJuprmMaorjmm/DYYzkaD3BaZD8ZxC8naSQeVkgKO6MQzbS keD0eFi07+qUrJnhLVuqUoC1ivaCVx3yF6AliPsuHi7I3/S68ZwkHDy9NJbWFITvGIPQ gpIlOweo/lL1wd951RIJdRNHHWn5IXI3FL1fPhPBvsT7rsV+oOaqkaDzc954okC5yMza iE2dHsq5fmeOikdRIAdN2BGfzmUHbK0o5nF1Q5rQmi6oJZX4N8DB589yDgHkG6o5YqjE dOW6ClgT6O8gUju+6MCpFcNFOFNG7cpX0MsBl3QhCdTu5kRSkh/05R0f/U2M+ddxrLfq FAwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739219239; x=1739824039; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=N78rd5bu5L3Nb7UCylb6NhXzPkib+ej1wIZfC16ALRg=; b=BIyYYrKxcRqeytBizmKZtY17ltuZ4JPDpW/Avqkg1dZj6Ado+iHfzwwV/QMiTWoCUv j2/jGrOYwuSt/uD6F7MaolcYfde9RobToQcsi6e2zfVInDUk/oR5TStVD978UiW1Cy5u kMQQXgrNRqBawKOmHR+GgsdSo1YRaeYGqx24UT2SWsJeqz5uTX21u7rKxiwhxX2244Dg kpiemXW1hqTL/W2g843KvERg3JDFDS0GxMtkSL9v+ix43/mSS4DzgrBXMCsAnPTwi7BN 6v4wVC8oao5BlfvYZXBB9aUcrL1c/EIMqz8r3jyXtY2EmM1aeJk8kwFUkIgszY9wiNJO dKGA== X-Forwarded-Encrypted: i=1; AJvYcCU+0hKQ6qg7oNNbi1AR+TNSI2IBMqQYB9+9Kkz1O3kYx1BKKTwz0xdIBuHTwRTRxBQFDAoslvReJDI8Qw==@lists.infradead.org X-Gm-Message-State: AOJu0YyyuSGdSJL2n5BNMjZ0D/G1tYy3bYsaRPZFQ1elP9zmNQ0ggssE DNYGa42tnLyEuIWGt7Mzpi0+RT4qbN5tD4A9GYpHbCDHoqME8xuheGvVuJW1CA4= X-Gm-Gg: ASbGncuktSKyzCTHRmtYHJvIl2rVEmLFggcoSpw1B4WUtEcFXfqrin04FXeqdZ+kEX+ NgERV+vkb1pL+NIIofEJe4EuUCnJa3jRljKCUnhzM5o1hb5A6vg5K8gqong99qLDuCXu8xY6NtU xBjoNfzMnPqzh2VREReftqJsLpkDBVrQpi3hOIWYxEHqFDYe5Nv6B3EOPFtU5hDPQsRE+tOyTyA jkRX5f/WV83MVjBQpDREy/eBhtKsQ4P9SK1SAhD4PgyEqUn4YicAOL8iQEpdRBlD+IxPLeTBcP5 uSRLY8vKMYnyq0c33KDiwdlmjA== X-Google-Smtp-Source: AGHT+IEqr2w6ar9z8igWE37AnqdTZQAskGxOfpDyMik3dNCSypk77ZCJWfXC9IN3REVr6mH1hjulIA== X-Received: by 2002:a17:902:c942:b0:215:8809:b3b7 with SMTP id d9443c01a7336-21f4e1cf107mr224484445ad.7.1739219239596; Mon, 10 Feb 2025 12:27:19 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21f3687c187sm83711555ad.168.2025.02.10.12.27.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2025 12:27:19 -0800 (PST) From: Deepak Gupta Date: Mon, 10 Feb 2025 12:26:48 -0800 Subject: [PATCH v10 15/27] riscv: signal: abstract header saving for setup_sigcontext MIME-Version: 1.0 Message-Id: <20250210-v5_user_cfi_series-v10-15-163dcfa31c60@rivosinc.com> References: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> In-Reply-To: <20250210-v5_user_cfi_series-v10-0-163dcfa31c60@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , "Liam R. Howlett" , Vlastimil Babka , Lorenzo Stoakes , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , Rob Herring , Krzysztof Kozlowski , Arnd Bergmann , Christian Brauner , Peter Zijlstra , Oleg Nesterov , Eric Biederman , Kees Cook , Jonathan Corbet , Shuah Khan , Jann Horn , Conor Dooley Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com X-Mailer: b4 0.14.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250210_202722_190710_CC75359C X-CRM114-Status: GOOD ( 25.12 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org From: Andy Chiu The function save_v_state() served two purposes. First, it saved extension context into the signal stack. Then, it constructed the extension header if there was no fault. The second part is independent of the extension itself. As a result, we can pull that part out, so future extensions may reuse it. This patch adds arch_ext_list and makes setup_sigcontext() go through all possible extensions' save() callback. The callback returns a positive value indicating the size of the successfully saved extension. Then the kernel proceeds to construct the header for that extension. The kernel skips an extension if it does not exist, or if the saving fails for some reasons. The error code is propagated out on the later case. This patch does not introduce any functional changes. Signed-off-by: Andy Chiu --- arch/riscv/include/asm/vector.h | 3 ++ arch/riscv/kernel/signal.c | 62 +++++++++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index e8a83f55be2b..05390538ea8a 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -407,6 +407,9 @@ static inline bool riscv_v_vstate_ctrl_user_allowed(void) { return false; } #define riscv_v_thread_free(tsk) do {} while (0) #define riscv_v_setup_ctx_cache() do {} while (0) #define riscv_v_thread_alloc(tsk) do {} while (0) +#define get_cpu_vector_context() do {} while (0) +#define put_cpu_vector_context() do {} while (0) +#define riscv_v_vstate_set_restore(task, regs) do {} while (0) #endif /* CONFIG_RISCV_ISA_V */ diff --git a/arch/riscv/kernel/signal.c b/arch/riscv/kernel/signal.c index 94e905eea1de..80c70dccf09f 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -68,18 +68,19 @@ static long save_fp_state(struct pt_regs *regs, #define restore_fp_state(task, regs) (0) #endif -#ifdef CONFIG_RISCV_ISA_V - -static long save_v_state(struct pt_regs *regs, void __user **sc_vec) +static long save_v_state(struct pt_regs *regs, void __user *sc_vec) { - struct __riscv_ctx_hdr __user *hdr; struct __sc_riscv_v_state __user *state; void __user *datap; long err; - hdr = *sc_vec; - /* Place state to the user's signal context space after the hdr */ - state = (struct __sc_riscv_v_state __user *)(hdr + 1); + if (!IS_ENABLED(CONFIG_RISCV_ISA_V) || + !((has_vector() || has_xtheadvector()) && + riscv_v_vstate_query(regs))) + return 0; + + /* Place state to the user's signal context spac */ + state = (struct __sc_riscv_v_state __user *)sc_vec; /* Point datap right after the end of __sc_riscv_v_state */ datap = state + 1; @@ -97,15 +98,11 @@ static long save_v_state(struct pt_regs *regs, void __user **sc_vec) err |= __put_user((__force void *)datap, &state->v_state.datap); /* Copy the whole vector content to user space datap. */ err |= __copy_to_user(datap, current->thread.vstate.datap, riscv_v_vsize); - /* Copy magic to the user space after saving all vector conetext */ - err |= __put_user(RISCV_V_MAGIC, &hdr->magic); - err |= __put_user(riscv_v_sc_size, &hdr->size); if (unlikely(err)) - return err; + return -EFAULT; - /* Only progress the sv_vec if everything has done successfully */ - *sc_vec += riscv_v_sc_size; - return 0; + /* Only return the size if everything has done successfully */ + return riscv_v_sc_size; } /* @@ -142,10 +139,20 @@ static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec) */ return copy_from_user(current->thread.vstate.datap, datap, riscv_v_vsize); } -#else -#define save_v_state(task, regs) (0) -#define __restore_v_state(task, regs) (0) -#endif + +struct arch_ext_priv { + __u32 magic; + long (*save)(struct pt_regs *regs, void __user *sc_vec); +}; + +struct arch_ext_priv arch_ext_list[] = { + { + .magic = RISCV_V_MAGIC, + .save = &save_v_state, + }, +}; + +const size_t nr_arch_exts = ARRAY_SIZE(arch_ext_list); static long restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc) @@ -276,7 +283,8 @@ static long setup_sigcontext(struct rt_sigframe __user *frame, { struct sigcontext __user *sc = &frame->uc.uc_mcontext; struct __riscv_ctx_hdr __user *sc_ext_ptr = &sc->sc_extdesc.hdr; - long err; + struct arch_ext_priv *arch_ext; + long err, i, ext_size; /* sc_regs is structured the same as the start of pt_regs */ err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs)); @@ -284,8 +292,20 @@ static long setup_sigcontext(struct rt_sigframe __user *frame, if (has_fpu()) err |= save_fp_state(regs, &sc->sc_fpregs); /* Save the vector state. */ - if ((has_vector() || has_xtheadvector()) && riscv_v_vstate_query(regs)) - err |= save_v_state(regs, (void __user **)&sc_ext_ptr); + for (i = 0; i < nr_arch_exts; i++) { + arch_ext = &arch_ext_list[i]; + if (!arch_ext->save) + continue; + + ext_size = arch_ext->save(regs, sc_ext_ptr + 1); + if (ext_size <= 0) { + err |= ext_size; + } else { + err |= __put_user(arch_ext->magic, &sc_ext_ptr->magic); + err |= __put_user(ext_size, &sc_ext_ptr->size); + sc_ext_ptr = (void *)sc_ext_ptr + ext_size; + } + } /* Write zero to fp-reserved space and check it on restore_sigcontext */ err |= __put_user(0, &sc->sc_extdesc.reserved); /* And put END __riscv_ctx_hdr at the end. */