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: 13968624 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 1B10FC021A1 for ; Mon, 10 Feb 2025 20:28:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9FDF028002E; Mon, 10 Feb 2025 15:28:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 984FE28002F; Mon, 10 Feb 2025 15:28:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 761CF28002E; Mon, 10 Feb 2025 15:28:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 4DEDD28002C for ; Mon, 10 Feb 2025 15:28:08 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A6B981A06E7 for ; Mon, 10 Feb 2025 20:27:22 +0000 (UTC) X-FDA: 83105169966.18.9FFA08B Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by imf28.hostedemail.com (Postfix) with ESMTP id A11A7C000A for ; Mon, 10 Feb 2025 20:27:20 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=zO12bHNb; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1739219240; 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=N78rd5bu5L3Nb7UCylb6NhXzPkib+ej1wIZfC16ALRg=; b=mzk3WtYbkFbZVwv7zgDBHHFFAu2TfHJ/Tbu9ff7fSesufVyZ87VAkO2+BTLQqx+ExJ/hqL QImfbL0onr90QtBShzUFJjCx1pPcXbapRZOC4XdAeq1ZhlgxfR5RLkQxsmZ5pDMaD6ygLQ OolSx5zsPpyTBskBXI5f5rQoly7dny4= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=zO12bHNb; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1739219240; a=rsa-sha256; cv=none; b=Cy6CBGZ6aKIH1THRwB4SZ21b2h5PsiKTdct1uR0F6MGylYURKXB4phC8gwnB9Tv6g3Zv71 RNOqifR/VEcYwmpRthqm3TqV7a5lEL0LMZhl8+V0vRYG0+OIWGgbDSK4Teiwk+nXbB7pRS 5HEpM6rAZhCMSs1UnszYihmMQw+6/o0= Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-21f49bd087cso63024085ad.0 for ; Mon, 10 Feb 2025 12:27:20 -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=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=N78rd5bu5L3Nb7UCylb6NhXzPkib+ej1wIZfC16ALRg=; b=zO12bHNbjPgq5xDpe4fnJSLBdxQQANrkjScLWfdhkMOJfJ+ZLEWk6ZqTyaf7ZZfbMe RzKWMAG2wF4S39b+HNrRUqxzZPiivLGC7TQUP//ExN9oPhifIoZGWB3uFEE0M3atTMr7 dsjVJ3XpNDlvm7/SAHblMTLZN8yhqBvMaaeHNUamCgWb1Ofbah4hybEnBwUmWheMefNO l7MdhP4tPxvzrXxhTI/vPN7OUVky2x1tsy0wfKojQNodBbh1KXHaJ/FLU2pIly8XvfkU Bn4HM0ow2FywB4YQFywcPp7AUC6BRU/8HpQtFyFZNG4PNkT3xHfeCC9jbWu1I+lVnZ/C g+kQ== 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=IDnZ1OgfMolsxsOQwsJXa79BxZJ5VfNyDFNjcP+35gxor2ujGIo1chnqtUaTvnoKEG WpeyfyBNnFTORIyvsAkHdSXyE4kXoU5hgQFdDsuhlzop1ihuUseMqV07JM8aIVNYp5nz Ejff2WftT2Nm0ZDYqQflv4r70CSrFVeaPlSAgjK0psAAMRkHxger3iqq90MGSlx8uLRF 4bQpNeBfxFUjv4yD+PJnFVbMclLOLUahepm8dqlthvu5ykA7ceAR3tg9N99QLbcvND39 7IGhBWJv7MoGulWV141PszfY5dUiWfVM3Y7X7/Y7WLGMIth84D8Q5qN+a2P9daImEH8b 2w0Q== X-Forwarded-Encrypted: i=1; AJvYcCUXG5gyq3ptZ0/UqzG5uNd4mDo8lEE+pcSLnE8UWXHKCHjAHW62FjoTROLwvwzgZczmqln5payrJQ==@kvack.org X-Gm-Message-State: AOJu0Yy8bZT4KwP8H0QCiscXVSEj7RvAhKNq7hklCS+uZJ+lWvWt5RAY mGn3AwJNThJknk57f0RGDc3k9Pcfm6pjcyArt2hJMlUamEeE2YwO5yhnXiUI6yI= X-Gm-Gg: ASbGncsn6sV5Jzk1JfzlWJNdn74xOltFD+/B+R9ErY7J1JlKJsV/vrZQRnZf05HDbjW Ga0Z6Y84a6NV/E1+EvY7qd6qPGKZW4fuHTcNQeJoEzgQ03H1ilZOpDgWQwYQKJfb3KcmygBPQ/1 7PHv7+JKFB8U2V+G1lHO4rHxdXGSvc7UkhYH6CIxC1vaoGp53vp4fYkFw0lRl75sHgbac5yzeLE 4evprB2fJZn1lUAY9LIHD1Us94pMvfnn+//VzFv4QkykUbXBNtwgpZPb1NHNrySTLFi+hltCXIN 1ofpyOUqln+Tmyp16MqbkK8hrg== 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-Rspam-User: X-Rspamd-Queue-Id: A11A7C000A X-Stat-Signature: d6ijrf8mcumzxqk6m4inwes8qw7urzdr X-Rspamd-Server: rspam03 X-HE-Tag: 1739219240-765843 X-HE-Meta: U2FsdGVkX1+eaWuEi+bGYdjINRDmKIOk+iTxtg53DGiqbSBlOVhTG3VptMgqWaC1DnBMfk38AquOBU5cpuulE8zrQMRYhMNmuBXQTRbxXbPuuhBbOq1c0Moqr/cBA/Z1bnXDX9lZKQTPrzHHTvRa2qYJOQlMWpOH8/pEKrINjjMbDpFgxJrGY+FhPuVwzMX4SPJRO+GB/714S89VCkNUZPhystawXSxwmb22se4GTxejAzeYW2qIb8dkntLxydycCN5/t0nnf0uMVKJJL6UEHfPnTa2IaNXZRrS23cj8gG7Zh/GScdTJd/XSZEbaDJMTIu9R+Vx6fCPfnrfsQwdiKEC2XOLBJsX88GnguzONret4hkM+6CHsA4RkRvKJFVpesgfLQ0Lf7uw3VgaV4BSEp2Q0typ4NUYY8eXARBQbl+Z5twXU/3l8WmfpQ1M6N2K4jy6599fR4u+LYMiWjBsz3igjcslKgcPEegBn8QuC7b1mZaq1pE2w5nSPm5k/zLAyUH53ISKcixfb0QnJn8LIZMVewS6llIEdH3CCfRZ6aCDSZ/udQqpMYHSeuksSGgnvAyt/L8j+G3LF4bsDORWrNTpLAd4M0P5jv+7PpOba2z1JMY4d8xX3Pzcz3ELaCxkv5qLFE7aeVxweh7HRri9nRVO84QeshpZy0oIHvm8Y64oz+PUczCJw+JhUNohTAIZnVayOp696RwExYtfwbCoot6rRpE3lqFRo+McQbT7pboG1VGK6XOiV9uT8acwgbiD3zwydhOJglV9fdiNFV+ZmweSTdsNh18Tog4kGi1dXbkIzKR96fazYyXH/RYx/ZctfRQYj9cstBzCohCb6YmYI9Hjg1S0geQjhY07XPwNmjdm5lWP4VGh4KppDYbl8b6OZ313t8uU83AGM+Az88oV98wTfEzl6ufFPHpXsrP/TWwjjO8gt9DNqj8jjMUg0Ild8gBLlIQ04isBGNrfdA+/ qAiz9eqA J2j6qXWQt9ufFC3BJKLZQnqF6tJcAbRctGVrWzM8Bp/qdCIK+qBHnihseck92ClChzvpXkWyUiCBG3KC/5LAY7/X4t/XxGtiw/SLyvjqLI46cDISiZjIsvpmmU7S5KKDiU1Ghnn9uwzxmJk71LWLPvD12XshFObnn7Slct+pzUXsu6uJZUkaivIBmu61xi3G4c6dWSsOUuPdk6B8v0oQNUxveHxh00LfzPztQw1hvq6BA/yvxgQdK/sNUTvgajDwm7r2qum2/LKg7Ipr4fLhV5Pn0j5eov15sFvVHKjVisuZ2NxOb2QRF+OhIoWuHyAOiRIijBTmSb9Iza/xhsgT3iPuMwgHUKNy3RDBDOQRbBtAxwme6xrMgU7l3Ji5nCfWxjz8LYlfVcGZwgcFntatkorfQ+5cQEROW0rEZ3UGFzGTJOpAE+SW9AfFxpKd9/Ia/WQW6y7u10PWNzVler6UtXAZKAVS669Jhsl85 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 | 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. */