From mboxrd@z Thu Jan 1 00:00:00 1970 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 CEC82C54ED1 for ; Fri, 23 May 2025 05:32:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C4176B00AE; Fri, 23 May 2025 01:32:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 673E96B00AF; Fri, 23 May 2025 01:32:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 53D526B00B0; Fri, 23 May 2025 01:32:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 3941C6B00AE for ; Fri, 23 May 2025 01:32:09 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E98668160B for ; Fri, 23 May 2025 05:32:08 +0000 (UTC) X-FDA: 83473051536.05.CF0AA6A Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf09.hostedemail.com (Postfix) with ESMTP id 05CE9140004 for ; Fri, 23 May 2025 05:32:06 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rHD+nHCS; dmarc=none; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1747978327; a=rsa-sha256; cv=none; b=fMHIY7eYJQUzmkQo9EKyJ8kPCaAJwnahtQkSq98/Sb4/BV4H3nxIKx3dfVmH4u8tC+JMo7 GJtKqVW15Zxwz89kVKm6/um2SYYKdxrSph7/eXzFlWa5u0CNgY/EAQ8tThNA3tOXVF+n0m nS6ReKULhkTzYQpbwsMuuzOVf7RKLQM= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=rHD+nHCS; dmarc=none; spf=pass (imf09.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.182 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=1747978327; 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=+D4QltF1Nc7VEdA5n+UDPSlZueufWbEVbyRTdYC5Tzo=; b=Kyhpc3TJspnEaudIipJmeRT6+P/2iyzYjLi+3oTVRG2FM+AA6gcRJ1MBsM6z1xmAZw9kEJ wFryGWIcM+xtnwSQDhyphBaOmzDSJ0RcDOdptBEbTEPJN7mPmt02Py/ep4kB7ybNRknxGM HaPY+KtSrB8lH4CKVqneIDS8abr/KNs= Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-742af848148so5285891b3a.1 for ; Thu, 22 May 2025 22:32:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1747978326; x=1748583126; 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=+D4QltF1Nc7VEdA5n+UDPSlZueufWbEVbyRTdYC5Tzo=; b=rHD+nHCSR8JjDYLxlZkD0yKDiyhhhqF092t4ZT7oLoUXtEnZHHh11IYnuOWwZGgvbq CA5PMNz7HJMPZLMHBS79eHQ8ywKTQWdqS2h1mDkYW9VYLt2l/3i7WnujQcowqMePjpCa QP/Z6x5IfBwRvCl8lKqq6KRUhm+FoQaMifMC2+TAYFni/fWzNW2h4GWKZa83iYv1Uz/N I7qmD70/wVDTDttbYPib3JoxAKecittLhhCkWX6f5Q+ggih1V2LWCdptRW/3l65XE0rn 1v2i0BogLi4997I1URLsCQvbURRauCGyF4rpqHCnM/qPAiraxIK8jhcMPJ3JeB8kU7LL dOZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747978326; x=1748583126; 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=+D4QltF1Nc7VEdA5n+UDPSlZueufWbEVbyRTdYC5Tzo=; b=UlsmmvyxWglKx/eCldq0XuNLsOGNzbLAlpAjSDM14JueilysoVx74xfuIRMGADL6pk ZM2eBJwJcDpcXgvrMyoT1xZM1z8NTbRTjfr3nZyBAyP6t1z+fhsPVVQdUcKa4izIBHib WDQ9IMSD4emzCtpOP8Dk+kTB8LKnoljMy99xv/PzNqbBIGtyvi/Yt9bcaVzhMfuu9dca 0Bc1fyN5LjY+ucctzPRed/Z8sPE6kySGEB9Jd7KWl3CdHFTvcK1g/99OYYXsbIiO8SPZ 1UAMuLyWuX4ohT4NVVIjWgDl+dsufJRqnY8O+o8Xrqait8Sio31rcGd0D0mFp2RGADy3 oplA== X-Forwarded-Encrypted: i=1; AJvYcCUC7Qb3DGJnb+KSyb1PQYOPv1ILoUK8daxc6Yj2rE81q3YdKrsMb7bXD7nwH8wQ1aB1GI4HogKJiA==@kvack.org X-Gm-Message-State: AOJu0YxqUYpO1ap0j4AGzj4+HJoDxHwcDCIc3H0AF4xUB577HabNLFTE T7t45p34ONnoLbSv2NGobO+qmvKTlx0b8aPq7AGtsg1HQ5VnmtWShEWuEjy3zzETFf4= X-Gm-Gg: ASbGncsqxPlIwJUnL/7R8BTNk6ay1bwR4F2qzgMxylX3WKCtDNPnagTx8ilovaJZLzI HZMXb1S/a0G2uq0OOiB6y3CVACHKrP+xxEBs5nURUaOWlvqDp/0QupcU+XcHxmyoB0MWxsV5Nj0 GN33YMF+Ph+nTa5BvvIs4Fb8OEiSVgEfTbP9P9vUPdZn78R+dC93WoWdkJrxdw3lJFVcjGHxAX0 Aijcba0JsWdAvwdu3HcGnsyF1ANI2UBHlZsh30HkCRfUOVminQAF1ZT+lNqe7hfsK4r+w7Y/Nbm ZDfk7b7tf3QIwBLFPcHjk6Js5kxnAm2DwbX43k3S6OmuGeoscb0PokBdiikZUw== X-Google-Smtp-Source: AGHT+IFkWRRXBEoC4fH68pZ4ejtHWDND82oZ8A77Xo7x4brAaaOUpBlDtEDCsr/l37FKBQKC+8ipUg== X-Received: by 2002:a05:6a00:3e01:b0:742:aecc:c472 with SMTP id d2e1a72fcca58-745ed848e1emr2436294b3a.2.1747978325797; Thu, 22 May 2025 22:32:05 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-742a982a0a4sm12474336b3a.101.2025.05.22.22.32.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 22:32:05 -0700 (PDT) From: Deepak Gupta Date: Thu, 22 May 2025 22:31:19 -0700 Subject: [PATCH v16 16/27] riscv: signal: abstract header saving for setup_sigcontext MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20250522-v5_user_cfi_series-v16-16-64f61a35eee7@rivosinc.com> References: <20250522-v5_user_cfi_series-v16-0-64f61a35eee7@rivosinc.com> In-Reply-To: <20250522-v5_user_cfi_series-v16-0-64f61a35eee7@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 , Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross 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, rust-for-linux@vger.kernel.org X-Mailer: b4 0.13.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 05CE9140004 X-Stat-Signature: kqb419pu7epe5i4q5kz18bar9qni81e1 X-Rspam-User: X-HE-Tag: 1747978326-425261 X-HE-Meta: U2FsdGVkX1/pACehXCHsVbHaELJN4ZDLQSILK2nIdWl6A/ZSp7Z3UeIgqdIo6a+pYUm8TrVdwOxMPPXH6s6VZLq1zXLDp/Avs0m/JX2xGjLbxzM/8WRNtlXqX2x5i9fH/N6EPrVxukVdwC4wco3zn628bY4OfhtvtMwCmYMBO8JL3YT6vbA07uIM6jK8wAp5Ryd6yD5YCzlGNwkEoe6TG0WwR7t94/M29jxr9PmUIaBxTGcZ0yxxvK+qFmVK/lyESnlKFSHkfT1wZ8o6bF9h/xbdM0zJp0TjzofezewMV4qIgmA/o8SXRcMJRqy2/QHnPRwGOKnJ+DRbu0/DrIesByyxHC+Fxion2Q/E8n6ZfkjFAysdFpnn+7E1VWx++RiuVglZdNJjveEfNu5/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ity: 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 | 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 08378fea3a11..a5e3d54fe54b 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) @@ -270,7 +277,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)); @@ -278,8 +286,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. */ -- 2.43.0