From patchwork Tue Oct 1 16:06:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13818425 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 47EBBCEACE2 for ; Tue, 1 Oct 2024 16:08:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0ECF32800B1; Tue, 1 Oct 2024 12:08:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EF913280068; Tue, 1 Oct 2024 12:08:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB70F2800B1; Tue, 1 Oct 2024 12:08:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B46B7280068 for ; Tue, 1 Oct 2024 12:08:02 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 56E23AAD30 for ; Tue, 1 Oct 2024 16:08:02 +0000 (UTC) X-FDA: 82625514804.04.13C3A9D Received: from mail-pj1-f46.google.com (mail-pj1-f46.google.com [209.85.216.46]) by imf08.hostedemail.com (Postfix) with ESMTP id 5089F16001E for ; Tue, 1 Oct 2024 16:08:00 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=DAtCH0z4; spf=pass (imf08.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.46 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=1727798711; 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=OkiNy+EZPp+q5jaB7Z/R/b/GFoNISfEsqKrE23PeN4g=; b=BPDkvGNKIn206yIQi7usWklraoef9no2r4vsqGWydjqNJZCZaMVv4QTAbfvSB6eB4lZj2Z QGV2ULNEhgzQx70GxWSXTH6IX2+u3Ig8WEYIIHwnS5CCiAq0s+iPTRyN0uaiW8zDVo2EZE 76dseGCjSSRSyFFAjeLXr/Ebzwm9IoI= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=DAtCH0z4; spf=pass (imf08.hostedemail.com: domain of debug@rivosinc.com designates 209.85.216.46 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727798711; a=rsa-sha256; cv=none; b=XIE2b8lBfcqKTKfaim4PRn9fRvcUSp2OiIeVj6J6hPZJPiO/zncpfJtzSQVpVKOsnAGFhF FtPsPHBcRwFPStRk86xtkRPBQyk/N5HArIDMTTK4Sjip5hKrFRkRX5Z6YKQi9dAFJWc3u0 oXADhSp/dV8MPhx7N5lR/D2Ox2vfy2A= Received: by mail-pj1-f46.google.com with SMTP id 98e67ed59e1d1-2e137183587so1803386a91.3 for ; Tue, 01 Oct 2024 09:07:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1727798879; x=1728403679; 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=OkiNy+EZPp+q5jaB7Z/R/b/GFoNISfEsqKrE23PeN4g=; b=DAtCH0z4tUxcZzAj5/NLkdDHMrMUn6mxQNW2253Wfr9DVyZFEAEtQowPsODzeGQtUD yDWeasssoHslG0tmdbxHIg451fsRAQ1K1lV2A3S44kU3E1R1RYP3gSVP3PlDBn/Scwba 47dNUD+Q2vJVZYW8auDdHlVSzedFtS8ZQQEhHki8oDiPLzN5cZcPrlayxyLwIl7u6Trl ZAQzXf3GNff4yQEkP8uVAhGHwMnoR8YV3L4xKQT6TQn1kfDZQtIBkZKKu9I9v19+Zko4 Bj2yeyQsg806qu9UpQ2VmQxmZ+4eJW2F8H5UAGu8NgW25fgUg4e9Pu61Nq+ecBiCKypp 5ySg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727798879; x=1728403679; 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=OkiNy+EZPp+q5jaB7Z/R/b/GFoNISfEsqKrE23PeN4g=; b=KT0qjGeFMWTtlHt0NcWc9c15OfYvqaoIkttKAh0kuHaxJNTiYQ1ACaEk3fmONLZRIZ k/N4A63XrD8AdBQy2xrf11hVkXCyGBQXqM6NLhLs4FIVLiivJkz1piFZgyDRlbmWKIIs AZolTH5q/S4K7FQr1iYkXPFJVzdZeafji3RUXJXIcGcY32JSEphJNn1GUcX93KTC3tNn 2fKtzlBfxFGrAmiFsuszQvzlzo/Dhu8uCnLgcppVYY8mFo5DYCQ6MooypZc0M4M8kTo/ rdeZ65JEaKz40UGO27at8uZphif5VgWD6H/n8F7sBZJ2PFtSyCn/Y5NuP84R/cpmUCPX XzIQ== X-Forwarded-Encrypted: i=1; AJvYcCWJZQfueAI7301y+08TyttK3b7NIhRtOwjObHrFVC6Rh7KYQh2E5WYyMy8bn76qO2OQq2Co/3FF7g==@kvack.org X-Gm-Message-State: AOJu0Yy8y1PFGV1Dsaf29YPf0my1d6Eb0nJg4yES4sr/g1PdU+TvWoNK i5LJcX+LEVMY3FOrNlugwyJsnKavlHGDQJdfD4jalT8WUzsIcBAz60YuXR6mu3g= X-Google-Smtp-Source: AGHT+IG9H3vqP1KHn4K0qfXaJ6IuDbzDnjoMak6/wit/TRjvWRcwc+7EsVFnUBIlfw7mh2KN2DwXTg== X-Received: by 2002:a17:90b:1084:b0:2d3:ccaa:453 with SMTP id 98e67ed59e1d1-2e1849e8681mr204463a91.35.1727798878935; Tue, 01 Oct 2024 09:07:58 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e06e1d7d47sm13843973a91.28.2024.10.01.09.07.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Oct 2024 09:07:58 -0700 (PDT) From: Deepak Gupta Date: Tue, 01 Oct 2024 09:06:27 -0700 Subject: [PATCH 22/33] riscv: signal: abstract header saving for setup_sigcontext MIME-Version: 1.0 Message-Id: <20241001-v5_user_cfi_series-v1-22-3ba65b6e550f@rivosinc.com> References: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@rivosinc.com> In-Reply-To: <20241001-v5_user_cfi_series-v1-0-3ba65b6e550f@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, Deepak Gupta , Andy Chiu X-Mailer: b4 0.14.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 5089F16001E X-Stat-Signature: 5fj9z3p4xh1cyszg6ttzac8aqs4za3nm X-Rspam-User: X-HE-Tag: 1727798880-142817 X-HE-Meta: U2FsdGVkX1+mD+nilCEsmdeAdxSGXFlt/ee2hllSmpZ2hEWPYJhZeN3BTDjf/iPH1hLXs17geSZ6m+gv1Qcfy/LX4T7ABJRe2qyKbV30VnE8pW+o5RMbaxjnpZ55g32OZhe9/bvQxV4EI6f753CyJC4AkWGXch+H6Cn/t6/MVTsULQNKp313YT7gqTWTsgteQyzD+z23B+OvpX1/vAoHJksX613O01bMgfd5Qyybbh6rZiKNPfockIWsxvXJ0OT/odumku1eoKNedjlalN7QXNqgjgMl0Et4at6ZCMBlkAtHt75oHJWgGExsqT3lmppHOKlAE+ILjZEk1CQM+HmgcdPIZiKRPnYaEHr3DeTrJtibdZWJpxcNjeVIAOw+UcOrkbGCv7EzXphxRX0YxfQ+Hpa+nrzhom4rq41bHNZGwXWhft8giTYrrT/I3hO07ySmqQXR6N3ey+2TqWKix0e8Ho6lLEajSNb7Xa17ZqdOJmJu9jW1gRvRh8nJvKldY3VkuSZK6O9T6hL43xA1qUD6Ql1CptZ/JUrpH6Bfu85CiL++eMdnmm4naMhdj8eb1nGDRDdoNdHm34aPZrP8P5Me2qU7u4By7R73GB2Kia6d6tTfcxWGNbwxTZ3lJDK3nnv21R8FToYuNQoJ5JY5wbHEY2APmcAsKDdDGllqvH/F1H4Q3UMdHk0u29QRImmS5BHNBafY/2cXTm/2cL1pGqAbaGGZ0smKgPzmw7DtAXU4ZijRmoo0XNpZRnvPXmn4ye6BYLz308xdKYhTU2MtyQFWXD40nRu43s2HLO1QaI/tr2mIHUbaHsz2Goeb9bzvoeWxlJ7S150So9mVDYlhFrdRhgmGWal0EhRQ5tlvyDA/AQPwXrp19Qm8zi6LQP9WkdavYR4JJ69oMGZVTxxKDk4JdcFPYxrktLAX9vS3dnFYQS0CC4Mjiv4Zk3WbEnIxozJ6wWhzxV0Z3qG9AoePVtP vOxbdZT6 twjZeQ7X7egha4De9ljfYI/kuOsWyQuRbrh3pfvbGbtCIYUsE5yNh162ZpggORuGbDkwhRT25pBCBsm4g0HHgNBd+2zcy1Qlp2xAyp9t+/B+cnl+QQZpnDQMJQX2xn+gDQocUs23FZC9HG+ieaSPKaHW7tVcOvjHUK9JoKg+m6vANPY5XIVxluEHrRmFRgWygKB4+Fy+kkox5TjSdoReqVMKEg+GmHRj7IhfTjRJrhXHuP6v/ZXu8UagKQGb7pB/ZdCLgDC7TNvN45my+lMNU3dZd4YUOI1vpasDfJChqbYhvZyXcLDsUXBrNPmjy0DsC6HKrx48BRBhYAxt0hmJOQySvmkQRbUKTiFbZq6TmtKPkuJvE9AY+dLjiqVBu7nE09UcP1E+opZ5cBR610L+y/kkqYsWKu6O8HtrMj9U1FMUQq6dBCqu53h3zdqIAKZbKCczLaOqnTAois3+r4dq7ET3Zu7hjtB2FuBzbBAoMktBsFHNIXDsFByeNAHXFyKvSmltp 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/kernel/signal.c | 60 ++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 21 deletions(-) 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. */