From patchwork Tue Oct 29 23:44:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855737 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 77610D7494F for ; Tue, 29 Oct 2024 23:45:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A9826B00BA; Tue, 29 Oct 2024 19:45:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 85A896B00BC; Tue, 29 Oct 2024 19:45:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 689636B00BD; Tue, 29 Oct 2024 19:45:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 42B636B00BA for ; Tue, 29 Oct 2024 19:45:14 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id F219F4079C for ; Tue, 29 Oct 2024 23:45:13 +0000 (UTC) X-FDA: 82728272718.15.C443931 Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf04.hostedemail.com (Postfix) with ESMTP id 2673A4000B for ; Tue, 29 Oct 2024 23:44:39 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=bnNEt6C8; dmarc=none; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245457; a=rsa-sha256; cv=none; b=ee5vHjZgug9j12ej5U/AbfHNxIj4qM5vYoGJ9DTceyt7xeb6HjZVeogYj2iUi3V0sUYN0a G/bezpdaao7HB+YhZbAaKTyZcMv/xx2W1TM9T/1ydqnDSgxPhJctqxc3InerG4ZPfPlVKW bJv1+UqZxB/LWe/n8njdPSnmDL0SpA0= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=bnNEt6C8; dmarc=none; spf=pass (imf04.hostedemail.com: domain of debug@rivosinc.com designates 209.85.215.172 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=1730245457; 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=yi9c4PfgxIrsXFqGtUWAjsswGeaf0ww2Eq0Goi40tzg=; b=LfmVz60fQsQR65LfMGDKSPUcNWnMdLSe5iVYj3IjViRIA2H2mC9HKWlOmYI6rRPZmoABen Z4DzCUVCdGCoxUN1G/e9GnCdTJDUS4n3F160ncU0m/fO7I5EOouV71fcFnlEG/334ve42P bOVbQDscX7jg8iZL78F7pqkGDbbWDIo= Received: by mail-pg1-f172.google.com with SMTP id 41be03b00d2f7-7ae3d7222d4so4663609a12.3 for ; Tue, 29 Oct 2024 16:45:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245511; x=1730850311; 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=yi9c4PfgxIrsXFqGtUWAjsswGeaf0ww2Eq0Goi40tzg=; b=bnNEt6C8SNqedlh0m0aHTX9MnyuxB4pwKXVJGCnPsAQTAHED8/O59y1B2RBgRrQl2y mvjU0G2P3yH0AQv+zl/39qgPEZDeNtoWj7KDirGKK9Yd4rHlgchvZF+uSIIF0nizrSAQ lNdPgfM47g4FnaVer05IMyQzpkd/ivoi41D/U89NCZBgHaNJ36jGWb8fZhsuU3F1rC0i s9cBzz6KT8xbXKCf92xpXhN3aqqqWzMNDiE2T9ClJyH/6x6p6ikkmwSPqG9i5fnZQsOT 0xEpVqj0o3jJIbeiGszcd14+5I17lks6XLXY4i652JplUeSnXEBA7axBSmRXZ1PvXR26 DXog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245511; x=1730850311; 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=yi9c4PfgxIrsXFqGtUWAjsswGeaf0ww2Eq0Goi40tzg=; b=TQVpQCX08EwolHg20IjskpTzn6JRr7nMa/ZNQqYz+N+Mu7P6yBuMWVd+nwV+2pN8L6 qYz/AaKbcIsOVXlgXMtsu39N42uM7pZiYA/9AxuxfEBNG27rDl5MVSvUsk0xgeZ/Mu+P xyINLv1nn7tWo505ReCBqCaPhYW7ypZyIwnNmDOZE/UcXCmHbTN+mrw0DhSvGzagazMY BJSqHhIaOij5/yFaup4N9DZIhXY7IRyUKNuq+8VLmwwkgOttdX68tmQvO7Ofe7IDibfy 5l6ZIGxgplGIGTi7oRkj/WVckpCYCT96wjLOqaD000FOREzDE1KuJGUXKp7T2e5PhYIV 4Wrw== X-Forwarded-Encrypted: i=1; AJvYcCUijNS3PMZ5Ehkpjv+6jTmJELnQTSiYoUjgcP7jPIvsYzjnP3Y3LzCdrCj4GKtg3/3LCV+Abx1IJA==@kvack.org X-Gm-Message-State: AOJu0Yy+RapHsHiu2X4AUIaSqTQ+XYTXQs8ESuV3ChtSZvphc1qduWXw 2qsPm8r3XY4QuZhoi1i3JNJWElPG4WLhr1A21Bkdn+BrUkJWJYIWl7CIt33bHUY= X-Google-Smtp-Source: AGHT+IF51lB1bvJMVNfZsGgqLWarH+JYdED//undtRt22viNiy18bh6cEWI+2sRnPRU3+1w1VPg/Hg== X-Received: by 2002:a05:6a21:3384:b0:1d9:c569:15f2 with SMTP id adf61e73a8af0-1d9c56916aamr12728492637.2.1730245510969; Tue, 29 Oct 2024 16:45:10 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:10 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:21 -0700 Subject: [PATCH v7 21/32] riscv: signal: abstract header saving for setup_sigcontext MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-21-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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-Rspam-User: X-Rspamd-Queue-Id: 2673A4000B X-Rspamd-Server: rspam01 X-Stat-Signature: d758kg9krrzjk3aqxbxhtinmoco6odh1 X-HE-Tag: 1730245479-281494 X-HE-Meta: U2FsdGVkX1+2WkfPw/5NzI6URVPhyUiHk5j6Tlc2XiJxB/qhXFu64c22rdeBxL18SE7Ac2vqk2G0Drd5ZcAFijDVSAOnu7zvY043VlfRs6n4ZBVqDV4w4BNWFtlETlEeQa2m9ADXpwrdYpFrQo3oZtqqJ0jB+CxAC/yK9EYqk8fnuZ2mfwYUJsrYuqCBloFKvyCR3WTNNaLCAqsxzxABHBHhUc/W7CKo3H4PBzzWCyG81bk+PMddr5ZW7q9VbvM6X5YapZvDuC0WSqPNvSp4HpR8M/zNLldjDY54KNSkJHAGvqKT47gbcKL0sHm/28rB9ZiGUc0mj9KECiZuwKYcljE9F9xEZrQd+RH28WyD4F7a6//ic+g06hYSlH3UpNEiEUQyT+cFE9KSAn1lMO1dbjPiTBlIwDNvwX430nRt58VtIKQX7KRjIHpwfzbtKkXNJPm9lNA2sdCHV+FJYsaLj76DCmLW4iWT9C8mpaTOEv0xFE9LslnOA99EARFrODdef0yp6JYKIvvRGT47qN9MnEqY3dXOGgq5vtdawfETGYQTfrl8I7lo9CyEfncJL1M4WKdaw0HYbi3GZen/0L8cTL8duJa5M65V5YKZG38jOWta922uG2cGhueu7pHAFMjIO6689xH6MPtQhOKjyoT71TeRFqS/FngGlixeM7xq0VPqnPSp22Fpf96fTe/8jDlkjRcAVzMmhzkO3BsrKhniq9WKn4SRl5i8SL1xzLpuD1pUwttjpwtfV5lGjS9EiCWf0hjceHrjCfQHkya1GDDTkQIm+wiIW703wvZk5mi33RWaaZZ0YvKpdRzl2zdsPFwZir9qLayK+e//2Cmd0iba6BrvvZhiPlwAO80US03ZESdFZII7dwArKRSfRK+627mP5sEHbe2eVY2r64yz6GRYbY9hN7RUag7Akmw0CtWeX5XYB1LsbvgDFt595N29ku1RLx6Hgk7upaq+McSwiG+ xzNApYjb HKrp6D6pxGCHrjAc0NnvLNFZXbI7kZqrZrS+RO2GpE5UGdZDKg1VfcJp5heGsD+WS4i6DqoENL6dEHp+AJq4oMdCRKt2oADGlNpcLPRhSI92qGo3iVJvYduL2MX6ucVRLTZtOyXs6nNZGK4/PzbTpjiNfm0EsWXCdjhc0flmPo5SYV+XTPPJemhHQJqAeJw2lSmjlRDQemAa2MkWuduBWxeon/39AEwDySuUfqE8R/5O0cTlDphhNV25tgvz4nkylWqIvXyjy6VzVMXxxfeRZFzLiAg7U7znkijbWg5T3YRD8EHnDFUSALs2jHF2t5SbBpWrA02iFghx6ogoKUt/yYFS7hBYP4N1Dby1n+6izK0qSbTx4Tt3fx6RyO4w4aSqyKuJQJEsUmwlbKOT+H8SU4Ow6gv0q4ZBl9gydKMCRxIGTh/CwKglFMGI2G745VpEwd+mkAGvf1gOaVP0i43JDaS0orUGF7DB68A8NJiizETeP9hvHbxdZE06eFupbCZXSOpWr 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 be7d309cca8a..2d2ec6ca3abb 100644 --- a/arch/riscv/include/asm/vector.h +++ b/arch/riscv/include/asm/vector.h @@ -281,6 +281,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. */