From patchwork Wed Feb 5 01:22:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13960376 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 76D38C02193 for ; Wed, 5 Feb 2025 01:22:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 29AD328001B; Tue, 4 Feb 2025 20:22:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 222B5280016; Tue, 4 Feb 2025 20:22:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 001BC28001B; Tue, 4 Feb 2025 20:22:25 -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 D364D280016 for ; Tue, 4 Feb 2025 20:22:25 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 9BBE0B081A for ; Wed, 5 Feb 2025 01:22:25 +0000 (UTC) X-FDA: 83084140650.12.00A8143 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) by imf11.hostedemail.com (Postfix) with ESMTP id AA74C40008 for ; Wed, 5 Feb 2025 01:22:23 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=CSxLN+QZ; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.52 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=1738718543; 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=s4a4iCR11C3zMGjHst2+0HH0/G2/gzAuktcP+r9rICaT+414/f3vPrwRo0+WbD/03D4Wzh lLI256qudqx4TUFj1MxncQjbsEuyOp3lxWIIsywt0osBPUjjTmO9wOAmpnr4PMD/JFredQ wJDrRsvmWxOEC0s10fH/6/CHX9XhQ/4= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=CSxLN+QZ; spf=pass (imf11.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.52 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738718543; a=rsa-sha256; cv=none; b=dOIHbQaqYRKhtXJh+KuKwfsez6Nq8JNXGyyoRZrvbLMlNm5HP0/sJoTnLqRhykF6Bl2Im2 g/iH6MIBCCr2PgrF6cC038cWvFuw9Mljll1mN/zDp0oHwCQP5+EEEDB2SpGS5NiFi2xOVd Uq1bf79pwjUABH87cU7bNYwM4sQqiBE= Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-2ef87d24c2dso8264382a91.1 for ; Tue, 04 Feb 2025 17:22:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1738718542; x=1739323342; 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=CSxLN+QZy/vSz9daHREwzhgJgKvtMv8WiwXeCGDCfscMQxc//IvuuV+ZHMnBxHTR6E L+GLVbFgNP9VVcsK0G+oL4UOb8kQE8/KvtkvLF75WDpxs6Q1NT0EVIa2MNnxMy5xW1aF 3V+QXYEGHHtBvo8bWvtnqzcoZgz6LIdDHR2d+6Zr8Tv+3Lp0gWSF/Ybm7oaxIIQw8QSZ sCnHmsj0CNYRS0f24tLdp+CkEnEtDTVKdcYKBVqHA8eWPVLL7o1qn+rr4hkEsDRflaEj 1NUD38JSCpr07+cWxd5Zvq9vogt9k2yveeJnJPOvGWPd/8lbQTWyW9dS+Ai9nJRbHdmu QQLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738718542; x=1739323342; 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=kAHAT09Pund/06MQYSieaalhZDgFr9+Fn91RsuNM9QdBJrGl95PkjHDNmj3Y9fAlbr v+EPAnHaMZPOEqOfKjYevp3G2g4zRpnaJKHS67/d8XUwzg95rYUif2SpvTOTsV9AKJBF JpAwOcrJftMLiRoKL2HYuOOnygQbggTHHqUvte9eUDGRO2KeHrskPMXn6oVQagE1iNrh qhdMDSc7DZvfSvEQzoavRFsMiqBm/uGrCEE5p+1R8TNaHmCRhCM+jjcpnnBtlg0oeAP/ DpAdP48LTrvnfVuo0M6e9HDKfw+JliQEl+AB2kUD7BhrGcdPF4zD5YuinKBIg2e/yTcE zyzw== X-Forwarded-Encrypted: i=1; AJvYcCV/R4u6ubSHrPw6XwglYTbu8AvP1+3FkMTav5OeYhNM12g68krG9776+vV7PgkdBYikTmT2PgM0rw==@kvack.org X-Gm-Message-State: AOJu0Yxr19D7/sgUO4ZSjP3oOUfkCxvZbh8IMQrMgSE/b9TQspVLAjZb pfeTzFo/Opi3EW8xwmSg37+bP8dp+R0P0NetEdP6f+qpSDMKxwA9u9JTl2/xFyY= X-Gm-Gg: ASbGncteI3884VdU4KLq1Fasn3Zips6FDuV7OkIXq6dnUjWsPo8DPoJD+nuP2lnvph/ xt6jqDVBNPfFPHcax57HWReAR9TG/kOPA2EhQ0iY9btkt1yHNC7489c/kvElGIT51wx02iR3aj3 g4pNKAj6zDctOnIEKYwnMVJrnpPzlULzaB09tHZMLudjuSSERRTSVT35M3N0uOVYqmtnGilNE4k an7pHYr9ZXwta9/vlrvMOg2QW8nvi3rOcIhUUsw++WHcWtlXY0tAXJzzdxiKEOJubFEmt0kOp9/ rktuPzfAT07lFi0UC2GyilBSNA== X-Google-Smtp-Source: AGHT+IFP/nZiHgN8IdOqdmoR6h7NcsY1CSiQrUwXf/VaRJ07hdJ31km67MYlbRhpJdCkwehTOyokKQ== X-Received: by 2002:a05:6a00:3a1d:b0:725:f1b1:cb9f with SMTP id d2e1a72fcca58-7303520e7f3mr1721528b3a.20.1738718542562; Tue, 04 Feb 2025 17:22:22 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72fe69cec0fsm11457202b3a.137.2025.02.04.17.22.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Feb 2025 17:22:22 -0800 (PST) From: Deepak Gupta Date: Tue, 04 Feb 2025 17:22:02 -0800 Subject: [PATCH v9 15/26] riscv: signal: abstract header saving for setup_sigcontext MIME-Version: 1.0 Message-Id: <20250204-v5_user_cfi_series-v9-15-b37a49c5205c@rivosinc.com> References: <20250204-v5_user_cfi_series-v9-0-b37a49c5205c@rivosinc.com> In-Reply-To: <20250204-v5_user_cfi_series-v9-0-b37a49c5205c@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-Rspamd-Queue-Id: AA74C40008 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 9ma4cb5f9pkfysoupshb93rac95yahy5 X-HE-Tag: 1738718543-63724 X-HE-Meta: U2FsdGVkX19eQ4j1YaPDQI1qEG/Jepf/HD38DLd3sJkjiCTdweTjK5nsFhIhCokhCWtnBCqEsIN/2STH/6xJw88PxdKOTIfjx+ddliK+B6Aiw3+BtBf9LpexRl3hh9bEY+PgHghg30NdExIz5WPUpWDkV+FnnNuOAcZwBmzan3nOHPgMgmB5N3ADWT0zJ2LCbdOj/frGd4EexnM5NpKgpETTwkTVzzanxKgtowLumjsqLQEmGjNfWZJXQAWplvgOMwrQ2Y0kU5gtBXFOehBBtMAqu34p1Xa1c0aGIBqK0xj1G7YZ6xFYqBRyKrXtSmwr80S4ybWYsEhnKYchx8Lx3a/2N8eYsFo1m7/Pjv/NYo+d7LLvrGgwE5sQkyGm9JX1PlpPJT9MdSbqu6yXc0o5NOSd4iz0kcebGCXfUUMBxks6CO0wL12dbalsHlfYImgjMXlH488pqyKFYMYNFK6Tqq9UTA0zrLs8bRmmL4B+taoCCXRD3IlHAFcId1U9tkwqQYMc7rJJNOPyZ0wfBaAafIwujLqopOLBRpxwH9EzRnUme+7Rgzo2tKycwi75eHPGeUlXMhh/eKOkynUqkaVxGC9/uRFyYbS3CylPPbkUjlJtsD9dCRfwkEkUZJbkZop3tUcF30J/80K9CfUR1vjI9YGefUy0nD+0Hy7B74pQ2rPNyO0ExpgTUuPuOTx+9AE+UwbOG39fqUfhcsLBmCxnL9Dz6YUua0yyarhMcdh6uNNNmhFvNZprc0POVMhWdvUIIMfB8CHbha5vQD319ykVLLsdr+P/M6qH04a80pwYN9k8yHxSLyps25XQ96HJxYUDhl1G/frPTMd3PPRjlE9xBeLeTS+m5qlozOEO8go8FYxAs4YneCjP4KFiumMl+XPDauujBXvHdorTLQPoTevykeDiA8IWJZLii6uGEbqt6vMePBMcS7VLsP1linRVoLg06cqrZ0b1zLuILA3xmak BCSxM6N1 jWMYjId+ynUTueF5sNEzKuod7AoucBYW9BJhE5+pWEByXVCBbVfLRXORXLq45MlaBQ7BbnZ8PR2di76a+K15RnS+NExL534MnD7wHPve/LmyOOmtMmErJnSu8qbplkMpxHdUBm87wNeKOwImcTSbMra8qUJG6zd/kJTtflURpANRrlvLoFOcvSHzgCkZjWvBgQg4Mp0WLrVyEppZK5xMQgU1uM2kI6Sk9qhgXww1rUfWVZ9kD0HQo2M4kwEvgtb7vuNm1Y6TLg6BTBz8Nx75J0sP8/aTzgD5n+wJ5P2NNEXmNJRnSUZWHdesuWv/LC9CofdmnV7AaI2y/qdKeDMzHymMKwSf3oDcZTMLCMIWG2X8vLJDJ7oD0y2RfqqkUDOKpliYwc5o4l9h/GF+rCNtNfM7T2YNf/fg9QW7Na740MesQICkifR3pVZB0HEi5rGYrrouBXtjyvXYwUlh6VmjZKDpSfqItMvsrVh5u 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. */