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: 13818391 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD6A11D016F for ; Tue, 1 Oct 2024 16:07:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727798881; cv=none; b=Yfggsk3rpV9BdwVdZCmFCDR6kidPjosNDBd2o4HXUN076XWB9nyU+HpfiyKU/qme7Shi+SWBctJXvWw0EWojUCXF9fgfhVgMNwvQAs7Y0n/5bBZ5Ip2Dsi8sIF8/QEHYe1cUZ4q5SOOj3RelY0mJbnAT25TZxv+NM2IUfMOlPiU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727798881; c=relaxed/simple; bh=0g243EdkLJFmzZUrBC3ZUkhosp1Dh3BgUCRmrQVYUx4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=c0ji1buhZdaE0J4c22JKpMwh59BcfMbfPJ6FAmfWR9QsGiyqWsY9BP8CdMqgQF9VIh1rWBEAhLSDT86M4qaDdmVR3ViVhViFeJPT0XF81fbFBxKJHcGq44Lrwtz2ANMU8dN99+msQHJfgzCB2akXEaAVXkPhKZwOvJKgVizgl14= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=qqajZMbz; arc=none smtp.client-ip=209.85.215.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="qqajZMbz" Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-7e6afa8baeaso4884341a12.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=vger.kernel.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=qqajZMbztkW//xbSEFHLSCCcH3mcu0QeygmzQTyXZ2Khc73ZDE8ufG73j58BmR3akB 51ruA/rUbI0549SGfSQha8TAYaAYS+R+1CO2rdjxSl8Gq/UGnUVbbeDrGCpvFOw38svH LFIirPNgko4aUmP9xmBhMQTvImDB27P881oLpg6GMZPpvv7rFvkGn7dDUxxREPcnrrqq 6gQ3tHg6QHrQQMhJSRBTIGRbWmD2Q/45DzWQMYwTdfaj2FzeURM1ar8SnulelP7ZCMJP +hyDs+jEa6Xmp7vcwkp4sBGesAu9qvZEoYnR62c2MZa6R92wtTRDCC38gtnSSoIvRskZ 9RMQ== 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=h6wLnDz09VccOa+deSCJuxdhPHDv0ZMrsYDRRWLVvnSCYT0GfG4ozVaesGuuTvDMor S1nKsxyWmLtvDzv1qJlLzNoZRPcZMtOvpv0WECy4zo8Do1YqqKv/+TC2pXbZbosYOjuL gR5tlW/I9UDFk9WewqlvCZG5+FTGAZaNxYpAtuWVXiY+7kbuYltw1hzI1IwZwkA0YjIu tJMrovVtM4mII4JmT7lj3YMIdQpIf0lm7Bpiv+F5IzW1bHUMgQvhb96xiD0hwLZHvzLO IY2rA0z8B6Oh2czzKiFO5QrpHl6OhYtYks/f2jDviFEZzfEY0sjdQUfULN+nSm83P9Qs YsvQ== X-Forwarded-Encrypted: i=1; AJvYcCWy5cyHHqh06wEfDwKuEx63jzDi7LeGjp5HzNBu0oERXjyFP4iZIviC7Mj0Yt/d0owrBdkJ/g0n6Tm3eKRg@vger.kernel.org X-Gm-Message-State: AOJu0YzY1ydK0i8o4hWEWHH3sBAXYAkPck9snHLL4qz2yk8wLPPEMrK6 jEWlnIeArA8hrJ6w01fFFqK0NTTFOF8ZdN7YvGKRnHWnMnLFCkRSLQpWyL9mAcE= 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 Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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. */