From patchwork Mon Nov 11 20:54:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13871355 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C607CD3ABF6 for ; Mon, 11 Nov 2024 20:54:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 52FB96B00AB; Mon, 11 Nov 2024 15:54:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 43F2D6B00AC; Mon, 11 Nov 2024 15:54:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 241CC6B00B5; Mon, 11 Nov 2024 15:54:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 03C986B00AB for ; Mon, 11 Nov 2024 15:54:46 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A863BA1B1E for ; Mon, 11 Nov 2024 20:54:46 +0000 (UTC) X-FDA: 82775016912.10.9492882 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) by imf30.hostedemail.com (Postfix) with ESMTP id 8963E80004 for ; Mon, 11 Nov 2024 20:53:27 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0Cr45pA9; dmarc=none; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.48 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731358309; a=rsa-sha256; cv=none; b=A9rP7/Yg/iqiiOK3PnW1swYowTQeLTRajx08Z8IiOcIlZU3DVHECvvmd/kloQSgMRz7KCL u75+45Sco5y77lYYzXvV6Z6FhIF4gISOQlGUWcX+Ca37f/v5JeHVXMnEmqTPTQproFypwL xGGr92BswvayZ005HeUpZnX88cO+Skg= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=0Cr45pA9; dmarc=none; spf=pass (imf30.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.48 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731358309; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AiAoAt/atgDgHIo+7vUn0I6AlUMwVku2xnmdwPnM988=; b=zAIinxjDbVltPXwRnL308Kdm7NZt/NyeYZsPwMefDztt5sIgCTC83iOdFlfygDlLjgKBv9 ZbZhrUUCRZH0O+1RXaa/6x8q/rrDBX9rRW/fchp0gdcUjgkz9drW3xWckefT23VbAsTRvv YHLZZ0v5qiGtnreIOfjy0Z1TsEK8LtU= Received: by mail-pj1-f48.google.com with SMTP id 98e67ed59e1d1-2e2eb9dde40so3997794a91.0 for ; Mon, 11 Nov 2024 12:54:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1731358483; x=1731963283; darn=kvack.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=AiAoAt/atgDgHIo+7vUn0I6AlUMwVku2xnmdwPnM988=; b=0Cr45pA9ilQnHsXFWJLAkDwFDM3/JmBXavqKozq4RbnNcPOAbxW6OKPJLF3sS4sr+v kVK9rwTf1G2iK+mue1WGqy4dr70fH6YOeA541Yb7bbfuRO+4XaI9uaJcjWDKY8csSEj6 iK64TiuuADLLqaxYpcRSOB9xxDAlE4TsQBBt/ufUhyawqstGW7d+dOUeNzmtYo4Ecjc8 TVUHfdpJpaHy4EUe0UmBssXI/tuGWLp3UeOKWg/Hu59BIURY1gmiozQSXQLyH1sHRRvY pJw8fo/KJqIh7kJyvDONise8yBCUNVQzzlynxxfRah671lzIZtqEpqMCM/MDJAYGD0FT HOOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731358483; x=1731963283; 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=AiAoAt/atgDgHIo+7vUn0I6AlUMwVku2xnmdwPnM988=; b=enqI4TxZaYS1N6FhOzbRXLIXfj2isEhp9BB3kED9XNzo73gMJN3bZS8xYYkeOnGxfY a5VY6rqOR2RPF5bSW9OtHUu9JHZFlsh/iPSGGSnR8uGvTQvrI/gjyJ2JpGuODKSOB2fo m0ohMr7CKpbydEw9CNWPUygUkthoSaUC06ztYTMR5IbZYak78telKbrrhePrECa6180T BcMSsBsctct6OWJZRMgSovbQwMfBA2ocD4etpg6mhtyB4VsZ688S7r/PiJxfPFwU89JQ dh1TxvKfv4RDEKzGT7Z584zdakeqmSvSrA82BTDEtC9UJ6JNBdzq9/u87Qcn1Y6Y09j/ +q4g== X-Forwarded-Encrypted: i=1; AJvYcCXDxwRz2Wf/5Es5AfVtlJXDD6TOOmJVvEpfy93jIoLiiXdcTkL7hNLQYnmLtFsOL8j6f+/Lxly8AA==@kvack.org X-Gm-Message-State: AOJu0YwfHjekY9YK+2cR8sA5xKHhaSP+UorexERkEAOVuKK8TaXlXDXD NPDuX4U4q8OMcfgpsnlC6DsICKJbo1msEHHTWsUePOGn/t+MGfDIVi0VGPIY6NM= X-Google-Smtp-Source: AGHT+IG9l53EM31RiSyqGG37/k+bDpkQY0fPocWmR7TDVciLiq+WzkkxtUuwaAGl1gcAKVBTiyo/CQ== X-Received: by 2002:a17:90b:4b87:b0:2e2:cc47:ce1a with SMTP id 98e67ed59e1d1-2e9b1655632mr20237191a91.1.1731358483361; Mon, 11 Nov 2024 12:54:43 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e9a5fd1534sm9059974a91.42.2024.11.11.12.54.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Nov 2024 12:54:43 -0800 (PST) From: Deepak Gupta Date: Mon, 11 Nov 2024 12:54:03 -0800 Subject: [PATCH v8 18/29] riscv: signal: abstract header saving for setup_sigcontext MIME-Version: 1.0 Message-Id: <20241111-v5_user_cfi_series-v8-18-dce14aa30207@rivosinc.com> References: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@rivosinc.com> In-Reply-To: <20241111-v5_user_cfi_series-v8-0-dce14aa30207@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 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, Andy Chiu X-Mailer: b4 0.14.0 X-Stat-Signature: i1ysfsz47emkootn3g8cxbjjnd7ugue3 X-Rspamd-Queue-Id: 8963E80004 X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1731358407-877311 X-HE-Meta: U2FsdGVkX18ym8sl4zEdaKivbVaj/SqNWhreQiOeKngRZn8Jh7rFgAP2l1ekLtIYDViE+tNMSDr14yGq62i9pLgA3m5ibiLrsbFzYVscJ6t8HGUdVeMCzAokYjTqAUCj68sCKrKwtc1wCmSb3tQ+SEaidogmsf/rz9CsnJb3XRoSaO3dhppJsu87esNdiST71/4SeTE0ZQEbLeRWhprLZS1rzIu60P9gBG5VmeSv2nMDLeYylxnPTvhP1oXrDHZyV1/eLWjwgtnu1pj2y34tbv8nB1GcY96/3g3J8oXpDrF1u7cpSVI4iKsINSzPRc8wPur/E+LcjXvpgxIHAZeD2ducPY04GzoSdv2wSNTi9IqBrl0HJDdRYXXWrU/tX/I8NHWYrKv+JUfG+E/cAa/uowCCNuFqUzT56LGfu8dmqcef+lS/qFqJPRPRU50p+oQZXAsb6DX1KZg5vXm46P+aH4mmbEveAkYr4Fd13m3fkxDDwIDz9N7Xu/lhAf/PDyJpih+tHDQ4pj0lWQkHIj9dLiNCLMASExckYoZRDIJSGaWWYaGBP3FzjBam3gsXvuXFiaXRjvt/nX2BsRL6VDgSAUp3uoQuseedX8dF+7oG0hUqh+zg1/0TKuuXf9RUzTtKWmqMMnxBz2cnqPzoZsmFRdSW8HiT+Rshblhu0FqPW6JxWPgAVx1HiFdf5L743bqU0QGKTiKXTSavU+YiS6H2+N6Fczb57JL+MHeb5Z9NQQFVw9rotfSv7PUXylKy47ERpM/DzZvzbdPuhCLXVOyIp6IJZovge0GG67QeM2wyTto95SsLfWU9zyhQ2W7DjZu7YlG2J7LqUrLaTeTuY5emUy4MEUbIG5uQXfeVUHCu6KfVINOnEEUBuyEhzVNpL9pw5qG0ZlXSiaLhyZgfcERc08u0zaJPM3rVLjobW7Cgbqs2lkHrCpi0DG9oohPlO3YtdHI8xh+bSKn16uuC9Ua gejcDXVt hVWr90DwbpokQZdc0ufehxaLmTyTxpWudyAgpIqqdhNeW+c/F13h9n9n4y1vuT9AUhoE2HDDB/6nVoJlxPoNqJgdwDARdf9doLWtnqq7J6W2sMTPGvkm06csXIRNymmzQdhg9yoTVc9zN+uaKBL+R7ecfJGPI52UT1dd+w5WSBGiGppJsWPIlMbZ5CRPHS1FX9kgJBefkEIRQ6Jx/yi7j0lW4mHnHutzJFHNBdCZ1LmL9cjVTWztgFflk4EpmZ0767LryLtcVJlVXDPS+vxgINshVNjfI5MiRtH5RGxl0Xug0/Z0JDcxr9UYaEp74Gj5W3vexuZeZcYpQDQcEzxMU23g73zFWjlURU7apdW+v0v4FJ0mGEP2WD7LqjKVQGBSFesBJSQe2TqA9Yjx/OthDf/DgzhoG74t9ZcWgXFSoaqNdq4F4oV8skQI9lYBwU0TG/f+bbHBA5soO3q2gFX+Ay/COkgxnvlo2QDJgDmA8s5S27rJ+fDX05MZ7QdhIxV6HGCggn2q3U9ymzBahYRtl74TFgw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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 | 60 ++++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/arch/riscv/include/asm/vector.h b/arch/riscv/include/asm/vector.h index c7c023afbacd..921b0fdcc3fb 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -283,6 +283,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 dcd282419456..014ac1024b85 100644 --- a/arch/riscv/kernel/signal.c +++ b/arch/riscv/kernel/signal.c @@ -68,18 +68,18 @@ 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() && 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 +97,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 +138,19 @@ 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 +281,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 +290,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() && 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. */