From mboxrd@z Thu Jan 1 00:00:00 1970 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 CB08BECAAD8 for ; Wed, 21 Sep 2022 21:50:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6BA2694000C; Wed, 21 Sep 2022 17:50:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6691A940008; Wed, 21 Sep 2022 17:50:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4BCF394000C; Wed, 21 Sep 2022 17:50:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 3F9A4940008 for ; Wed, 21 Sep 2022 17:50:04 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 13A2DC0D01 for ; Wed, 21 Sep 2022 21:50:04 +0000 (UTC) X-FDA: 79937435928.08.9AE8814 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf20.hostedemail.com (Postfix) with ESMTP id BB5DB1C0020 for ; Wed, 21 Sep 2022 21:50:03 +0000 (UTC) Received: by mail-pl1-f181.google.com with SMTP id w20so6955612ply.12 for ; Wed, 21 Sep 2022 14:50:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=ACCvZsq03esbed4JJPHdCD5d2fctZP9IZFCRSwcsBSU=; b=1kUW2EH0zK6y6bRETPrx2VpMVPcRSNWGXFdI6ecaRofL16XjZUmKBHVLy7VElAZ/2N yv9JJZFrtvYaBqpfeA4VQfimtPC5GTVcRGFY0nt2YTuQoNjgJuS2OYnwKEBm7xjs8IbW M8mt7S1vEuU+UNx5eVizaYFKMUDQIMkk46HcPN2B3003Zfg3UyZgIv2TWMGPrTmmMmvo QMtE3lIhJMmPu9cw4dDRNK1gLMawiSz+sgSsrNHwlmYe+ys554gnRN03euy+H/LpMX8+ uAmlQXt8XGOH5jkg56NDgjYEpt/Eeq+Agskl4i/xDAMeakVEU+Zgth2ITynfqqywVkzY BkVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date; bh=ACCvZsq03esbed4JJPHdCD5d2fctZP9IZFCRSwcsBSU=; b=gY8lBYQaloHdMHQe/8JjYr5IJuM/kOrrPoZn+WItaIZiGcxFwvMybUJR6mHcP2Li4i fexG5ZM562DGxkcC9dLlWahle7nrk8HnCsZEF6oe4pdMIkOonci8HhdkFF1Y5a1+vBQh DE2tWrDHq6PVpObi6Dzs3xGLOIfFaJ9Ie2+FdS/4Cn+pRISULm+QrXgbV4Ih5l9aTN/b h7E5eS/7/tRNpiqlCY8aYPxJaKaVMZu2p3rPO9AhKR0y2XhblhL+LbASfMa03qOSAnQV MSVkuhWuerlsygF3Jnw8PKU6yblBP21f6skfZ92c3/Z4fAlbi/ZwzKeyaXHSSY/ZywaM 0ZoA== X-Gm-Message-State: ACrzQf1bNndebkRbSujFQ+mCwsliRKmRBw2RkeEa/aMef9RVf6AJCDzH GUobi85SWY65n20rQl+BJqttLg== X-Google-Smtp-Source: AMsMyM50Fs9HxIrYMFhDXwQ+y7mPFNqB4ZsPmSEUnLUjyv6cMj+iv+OQosafgvTzgbqY2mK/Sds5Aw== X-Received: by 2002:a17:90b:1905:b0:202:cbf9:cd76 with SMTP id mp5-20020a17090b190500b00202cbf9cd76mr193820pjb.171.1663797002769; Wed, 21 Sep 2022 14:50:02 -0700 (PDT) Received: from stillson.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id k7-20020aa79727000000b005484d133127sm2634536pfg.129.2022.09.21.14.50.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Sep 2022 14:50:02 -0700 (PDT) From: Chris Stillson To: Cc: Greentime Hu , Andrew Waterman , Nick Knight , Guo Ren , Vincent Chen , Ruinland Tsai , kernel test robot , Paul Walmsley , Palmer Dabbelt , Albert Ou , Eric Biederman , Kees Cook , Anup Patel , Atish Patra , Oleg Nesterov , Guo Ren , Heinrich Schuchardt , Chris Stillson , Conor Dooley , Paolo Bonzini , Qinglin Pan , Alexandre Ghiti , Arnd Bergmann , Heiko Stuebner , Dao Lu , Jisheng Zhang , "Peter Zijlstra (Intel)" , Sunil V L , Han-Kuan Chen , Li Zhengyu , Changbin Du , Alexander Graf , Ard Biesheuvel , Tsukasa OI , Yury Norov , Nicolas Saenz Julienne , Mark Rutland , Frederic Weisbecker , Vitaly Wool , Myrtle Shah , Catalin Marinas , Will Deacon , Mark Brown , Jiaxun Yang , Huacai Chen , Janosch Frank , Alexey Dobriyan , Christian Brauner , Peter Collingbourne , Eugene Syromiatnikov , Colin Cross , Andrew Morton , Suren Baghdasaryan , Barret Rhoden , Davidlohr Bueso , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Subject: [PATCH v12 08/17] riscv: Add task switch support for vector Date: Wed, 21 Sep 2022 14:43:50 -0700 Message-Id: <20220921214439.1491510-8-stillson@rivosinc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220921214439.1491510-1-stillson@rivosinc.com> References: <20220921214439.1491510-1-stillson@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663797003; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ACCvZsq03esbed4JJPHdCD5d2fctZP9IZFCRSwcsBSU=; b=3PG/6X9sW/qhqxx9D+P7IztsDiOuesbX/V63e9q8tvYVeTuYRKRMY6nqScCoBsJioMsX7C 35Essl9ozA8r4w2pQxfp5zVFKyamDXI4G0Do61vUUkV+C1HMTJXYahqG5Nt2bjBtvROpPO ftSmMpBf0pS70OTQ7atf8d2a5NUOoEg= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=1kUW2EH0; spf=pass (imf20.hostedemail.com: domain of stillson@rivosinc.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=stillson@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663797003; a=rsa-sha256; cv=none; b=pI+sfIgKHOSg6GGoyCDtae6ogYHuvN+6DDl7IO+8faGfKSltnFnEsq8NZN1skfNZMM0zYu 4nlrVVFKVMlpnvwYGIyAZsR50HD/c2gW0fvmM39gaaf6R7Iud194dhzKpSAABO9IcJWUkW h4k5nS+EpUEnupETikhJ2VaCD3/8Kx8= X-Stat-Signature: isexd6eyhe5i9fpubh5pfp8hd7yz8bff X-Rspamd-Queue-Id: BB5DB1C0020 X-Rspam-User: Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=1kUW2EH0; spf=pass (imf20.hostedemail.com: domain of stillson@rivosinc.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=stillson@rivosinc.com; dmarc=none X-Rspamd-Server: rspam09 X-HE-Tag: 1663797003-389985 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: From: Greentime Hu This patch adds task switch support for vector. It supports partial lazy save and restore mechanism. It also supports all lengths of vlen. [guoren@linux.alibaba.com: First available porting to support vector context switching] [nick.knight@sifive.com: Rewrite vector.S to support dynamic vlen, xlen and code refine] [vincent.chen@sifive.com: Fix the might_sleep issue in vstate_save, vstate_restore] [andrew@sifive.com: Optimize task switch codes of vector] [ruinland.tsai@sifive.com: Fix the arch_release_task_struct free wrong datap issue] Suggested-by: Andrew Waterman Co-developed-by: Nick Knight Signed-off-by: Nick Knight Co-developed-by: Guo Ren Signed-off-by: Guo Ren Co-developed-by: Vincent Chen Signed-off-by: Vincent Chen Co-developed-by: Ruinland Tsai Signed-off-by: Ruinland Tsai Signed-off-by: Greentime Hu Reported-by: kernel test robot Reported-by: kernel test robot --- arch/riscv/include/asm/switch_to.h | 66 ++++++++++++++++++++++++++++++ arch/riscv/kernel/Makefile | 1 + arch/riscv/kernel/process.c | 43 +++++++++++++++++++ 3 files changed, 110 insertions(+) diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index df1aa589b7fd..527951c033d4 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -7,11 +7,13 @@ #define _ASM_RISCV_SWITCH_TO_H #include +#include #include #include #include #include #include +#include #ifdef CONFIG_FPU extern void __fstate_save(struct task_struct *save_to); @@ -68,6 +70,68 @@ static __always_inline bool has_fpu(void) { return false; } #define __switch_to_fpu(__prev, __next) do { } while (0) #endif +#ifdef CONFIG_VECTOR +extern struct static_key_false cpu_hwcap_vector; +static __always_inline bool has_vector(void) +{ + return static_branch_likely(&cpu_hwcap_vector); +} +extern unsigned long riscv_vsize; +extern void __vstate_save(struct __riscv_v_state *save_to, void *datap); +extern void __vstate_restore(struct __riscv_v_state *restore_from, void *datap); + +static inline void __vstate_clean(struct pt_regs *regs) +{ + regs->status = (regs->status & ~(SR_VS)) | SR_VS_CLEAN; +} + +static inline void vstate_off(struct task_struct *task, + struct pt_regs *regs) +{ + regs->status = (regs->status & ~SR_VS) | SR_VS_OFF; +} + +static inline void vstate_save(struct task_struct *task, + struct pt_regs *regs) +{ + if ((regs->status & SR_VS) == SR_VS_DIRTY) { + struct __riscv_v_state *vstate = &(task->thread.vstate); + + __vstate_save(vstate, vstate->datap); + __vstate_clean(regs); + } +} + +static inline void vstate_restore(struct task_struct *task, + struct pt_regs *regs) +{ + if ((regs->status & SR_VS) != SR_VS_OFF) { + struct __riscv_v_state *vstate = &(task->thread.vstate); + + __vstate_restore(vstate, vstate->datap); + __vstate_clean(regs); + } +} + +static inline void __switch_to_vector(struct task_struct *prev, + struct task_struct *next) +{ + struct pt_regs *regs; + + regs = task_pt_regs(prev); + if (unlikely(regs->status & SR_SD)) + vstate_save(prev, regs); + vstate_restore(next, task_pt_regs(next)); +} + +#else +static __always_inline bool has_vector(void) { return false; } +#define riscv_vsize (0) +#define vstate_save(task, regs) do { } while (0) +#define vstate_restore(task, regs) do { } while (0) +#define __switch_to_vector(__prev, __next) do { } while (0) +#endif + extern struct task_struct *__switch_to(struct task_struct *, struct task_struct *); @@ -77,6 +141,8 @@ do { \ struct task_struct *__next = (next); \ if (has_fpu()) \ __switch_to_fpu(__prev, __next); \ + if (has_vector()) \ + __switch_to_vector(__prev, __next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 33bb60a354cd..35752fb6d145 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -55,6 +55,7 @@ obj-$(CONFIG_MMU) += vdso.o vdso/ obj-$(CONFIG_RISCV_M_MODE) += traps_misaligned.o obj-$(CONFIG_FPU) += fpu.o +obj-$(CONFIG_VECTOR) += vector.o obj-$(CONFIG_SMP) += smpboot.o obj-$(CONFIG_SMP) += smp.o obj-$(CONFIG_SMP) += cpu_ops.o diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index ceb9ebab6558..e88a37fc77ed 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -124,6 +124,25 @@ void start_thread(struct pt_regs *regs, unsigned long pc, */ fstate_restore(current, regs); } + + if (has_vector()) { + struct __riscv_v_state *vstate = &(current->thread.vstate); + + /* Enable vector and allocate memory for vector registers. */ + if (!vstate->datap) { + vstate->datap = kzalloc(riscv_vsize, GFP_KERNEL); + if (WARN_ON(!vstate->datap)) + return; + } + regs->status |= SR_VS_INITIAL; + + /* + * Restore the initial value to the vector register + * before starting the user program. + */ + vstate_restore(current, regs); + } + regs->epc = pc; regs->sp = sp; @@ -148,15 +167,29 @@ void flush_thread(void) fstate_off(current, task_pt_regs(current)); memset(¤t->thread.fstate, 0, sizeof(current->thread.fstate)); #endif +#ifdef CONFIG_VECTOR + /* Reset vector state */ + vstate_off(current, task_pt_regs(current)); + memset(¤t->thread.vstate, 0, RISCV_V_STATE_DATAP); +#endif } int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) { fstate_save(src, task_pt_regs(src)); *dst = *src; + dst->thread.vstate.datap = NULL; + return 0; } +void arch_release_task_struct(struct task_struct *tsk) +{ + /* Free the vector context of datap. */ + if (has_vector() && tsk->thread.vstate.datap) + kfree(tsk->thread.vstate.datap); +} + int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) { unsigned long clone_flags = args->flags; @@ -175,7 +208,17 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.ra = (unsigned long)ret_from_kernel_thread; p->thread.s[0] = (unsigned long)args->fn; p->thread.s[1] = (unsigned long)args->fn_arg; + p->thread.vstate.datap = NULL; } else { + /* Allocate the datap for the user process if datap is NULL */ + if (has_vector() && !p->thread.vstate.datap) { + void *datap = kzalloc(riscv_vsize, GFP_KERNEL); + /* Failed to allocate memory. */ + if (!datap) + return -ENOMEM; + p->thread.vstate.datap = datap; + memset(&p->thread.vstate, 0, RISCV_V_STATE_DATAP); + } *childregs = *(current_pt_regs()); if (usp) /* User fork */ childregs->sp = usp; -- 2.25.1