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 66378C32771 for ; Wed, 21 Sep 2022 21:56:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F204194000B; Wed, 21 Sep 2022 17:56:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EA8AB940008; Wed, 21 Sep 2022 17:56:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD4CB94000B; Wed, 21 Sep 2022 17:56:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BC84D940008 for ; Wed, 21 Sep 2022 17:56:20 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 917A6AA044 for ; Wed, 21 Sep 2022 21:56:20 +0000 (UTC) X-FDA: 79937451720.07.CF1D215 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf01.hostedemail.com (Postfix) with ESMTP id 2389840010 for ; Wed, 21 Sep 2022 21:56:19 +0000 (UTC) Received: by mail-pg1-f169.google.com with SMTP id v4so7232796pgi.10 for ; Wed, 21 Sep 2022 14:56:19 -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=nS6duVJdRpy7VzEDbeuUiz7BRgby2LGBHTgBEvvIt40=; b=tjpF60fiDMo8Hp79sblUDwhbNESEOQ84S6zPjYO6+5DyJUVJ/QopYsgVRSL8uJ2n4c ZPT2Qw3dhXdov9tlFNovScCJpiwpUU86XiclidfdW1njjtkAk931axbm2bs1oOM6M1Iz zGf9sFOrCYS+LsE5K72TRh8c8F6rKRoisDV1iGUb5YpBiCvatrSBCV1vifRVPv/vV+iV i6eRKB9Mh6ebx0CSewJVuIgZBNscXgv8u7XeaqFfGrxRPe/swmXV96anwn24NmhzkqL+ oSv7H/uWDSmLvFLrjvVjbPf4bprpIcdSHC69Yjb8B/GskqoDB5LaX0qNK/WMzK3l3oGE F/CA== 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=nS6duVJdRpy7VzEDbeuUiz7BRgby2LGBHTgBEvvIt40=; b=aBKlGwBJybj35ZO8aLBG4yDECvZcaFJ7aRbLMcwL14grcJvGddH1yUnp/bVKLFaaij m2ZpCrU+wuWqbeOlb6bAs1SoJIf45TqhcUBZbZJyl7OvS3Dd1b7AwImUcBuPIzG3R+xK vN6gnT0gvL8GuQ7+Hg083HAAuhdQiWvNlDRtn7H/6xPYtvfCU31ntOEV3ZotpeGPgzrD SissWtSmJ7Ftfycv3NLxell/GUdx2duErkaDz9YgOAX3XHcI5EdfZmDJ14aMdoraPeHS wFffuFgmR/wZWUqtFCUB1FKmcQMZSMEg0Lq7pLX8Dk89vT/emLzWyfziyoomuKeG/UuU jDBg== X-Gm-Message-State: ACrzQf14/Y0wPEhneciGzAF1xPkLt/acFyu0EJbWgJWcvNemCNHe3LcU Us1Fk2F/1OzfNkhecwcpQ26MJw== X-Google-Smtp-Source: AMsMyM4eI1QZHxWSIPvoJijVfop0chBMt0kkLqJBs6N8ucJKxntXUdb9sZC80mjW3BJ/veY4quk7Rw== X-Received: by 2002:a05:6a02:104:b0:430:93ec:776f with SMTP id bg4-20020a056a02010400b0043093ec776fmr236548pgb.544.1663797379221; Wed, 21 Sep 2022 14:56:19 -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.56.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Sep 2022 14:56:18 -0700 (PDT) From: Chris Stillson To: Cc: Chris Stillson , Paul Walmsley , Palmer Dabbelt , Albert Ou , Eric Biederman , Kees Cook , Anup Patel , Atish Patra , Oleg Nesterov , Guo Ren , Heinrich Schuchardt , Mayuresh Chitale , Conor Dooley , Paolo Bonzini , Qinglin Pan , Alexandre Ghiti , Greentime Hu , Arnd Bergmann , Vincent Chen , Heiko Stuebner , Jisheng Zhang , Dao Lu , Sunil V L , Han-Kuan Chen , Changbin Du , Li Zhengyu , Alexander Graf , Ard Biesheuvel , Tsukasa OI , Yury Norov , Mark Rutland , "Paul E. McKenney" , Frederic Weisbecker , Chen Lu <181250012@smail.nju.edu.cn>, Vitaly Wool , Myrtle Shah , Mathieu Desnoyers , Catalin Marinas , Mark Brown , Will Deacon , Luis Machado , Janosch Frank , Huacai Chen , Alexey Dobriyan , Christian Brauner , Peter Collingbourne , Colin Cross , Eugene Syromiatnikov , Andrew Morton , Barret Rhoden , Suren Baghdasaryan , 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 17/17] riscv: prctl to enable vector commands Date: Wed, 21 Sep 2022 14:43:59 -0700 Message-Id: <20220921214439.1491510-17-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=1663797380; 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=nS6duVJdRpy7VzEDbeuUiz7BRgby2LGBHTgBEvvIt40=; b=FvAs8zqNBofTbdTUd2YnKTbGLOnTir+TsqpevXnxUZy4qXMPtJxCuPLOIxnCWYpaGUvrQd CuT+Tz0ryNgucJdyca9c968ycTrvihiVsk5hq7HdDIbrkBikNhIiTXKYdNyyhfCCtRR0zn tHHzqVXClve77KKEhlH+rciI7NYsxbs= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=tjpF60fi; dmarc=none; spf=pass (imf01.hostedemail.com: domain of stillson@rivosinc.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=stillson@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663797380; a=rsa-sha256; cv=none; b=DMjpziNbcTKqEyf7xxTA6Az/Wwgy5gVBdnspLn3uG1esLgZt+2OE0xBQJMX2JzXX6nFAp2 75CWT9eIV80GEJZULNIOnJEVGzY9iEonbZY/crUvMuCidBYubZ8Yx6iaCZWwVNmnGvKMkz whPVoDc9aRmhppI/B+1bfPiCt+PkpKg= X-Rspam-User: X-Stat-Signature: sf6bggyjqfa5f4q67frxqot3mizri79h X-Rspamd-Queue-Id: 2389840010 Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=tjpF60fi; dmarc=none; spf=pass (imf01.hostedemail.com: domain of stillson@rivosinc.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=stillson@rivosinc.com X-Rspamd-Server: rspam08 X-HE-Tag: 1663797379-438491 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: This code makes enabling the vector extension on a riscv manchine optional by adding an option to prctl() to allow a process to enable, disable or query its vector context state. -added prctl to enable/disable/query current vector state -added actual function in riscv specific code to change/query the process state - Fixed problem with initial set of patches (missing some EXPORT_SYMBOL() macro calls) - rebased to 6.0-rc1 --- arch/riscv/configs/defconfig | 6 ++++++ arch/riscv/include/asm/kvm_vcpu_vector.h | 8 ++++---- arch/riscv/include/asm/processor.h | 6 ++++++ arch/riscv/include/asm/switch_to.h | 11 +++++++++++ arch/riscv/kernel/cpufeature.c | 3 ++- arch/riscv/kernel/process.c | 20 +++++++++++++++++++- arch/riscv/kvm/vcpu_vector.c | 14 +++++++------- include/uapi/linux/prctl.h | 6 ++++++ kernel/sys.c | 7 +++++++ 9 files changed, 68 insertions(+), 13 deletions(-) diff --git a/arch/riscv/configs/defconfig b/arch/riscv/configs/defconfig index aed332a9d4ea..fce054286b1f 100644 --- a/arch/riscv/configs/defconfig +++ b/arch/riscv/configs/defconfig @@ -209,3 +209,9 @@ CONFIG_RCU_EQS_DEBUG=y # CONFIG_FTRACE is not set # CONFIG_RUNTIME_TESTING_MENU is not set CONFIG_MEMTEST=y +CONFIG_ARCH_RV64I=y +CONFIG_64BIT=y +CONFIG_VECTOR=y +CONFIG_ARCH_RV64I=y +CONFIG_64BIT=y +CONFIG_VECTOR=y diff --git a/arch/riscv/include/asm/kvm_vcpu_vector.h b/arch/riscv/include/asm/kvm_vcpu_vector.h index 1dcc1b2e05bb..c7101ff943a0 100644 --- a/arch/riscv/include/asm/kvm_vcpu_vector.h +++ b/arch/riscv/include/asm/kvm_vcpu_vector.h @@ -22,9 +22,9 @@ void __kvm_riscv_vector_save(struct kvm_cpu_context *context); void __kvm_riscv_vector_restore(struct kvm_cpu_context *context); void kvm_riscv_vcpu_vector_reset(struct kvm_vcpu *vcpu); void kvm_riscv_vcpu_guest_vector_save(struct kvm_cpu_context *cntx, - unsigned long isa); + unsigned long *isa); void kvm_riscv_vcpu_guest_vector_restore(struct kvm_cpu_context *cntx, - unsigned long isa); + unsigned long *isa); void kvm_riscv_vcpu_host_vector_save(struct kvm_cpu_context *cntx); void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cntx); void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu); @@ -34,12 +34,12 @@ static inline void kvm_riscv_vcpu_vector_reset(struct kvm_vcpu *vcpu) } static inline void kvm_riscv_vcpu_guest_vector_save(struct kvm_cpu_context *cntx, - unsigned long isa) + unsigned long *isa) { } static inline void kvm_riscv_vcpu_guest_vector_restore(struct kvm_cpu_context *cntx, - unsigned long isa) + unsigned long *isa) { } diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index a09141ecf6aa..f2d0a91ce174 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -88,6 +88,12 @@ extern void riscv_fill_hwcap(void); extern int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src); extern unsigned long signal_minsigstksz __ro_after_init; + +#ifdef CONFIG_VECTOR +extern int rvv_proc_enable(unsigned long x); +#define RVV_PROC_ENABLE(x) rvv_proc_enable(x) +#endif + #endif /* __ASSEMBLY__ */ #endif /* _ASM_RISCV_PROCESSOR_H */ diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 527951c033d4..d9747450311c 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -80,6 +80,17 @@ 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 bool vstate_query(struct pt_regs *regs) +{ + return (regs->status & SR_VS) != 0; +} + +static inline void vstate_on(struct task_struct *task, + struct pt_regs *regs) +{ + regs->status = (regs->status & ~(SR_VS)) | SR_VS_INITIAL; +} + static inline void __vstate_clean(struct pt_regs *regs) { regs->status = (regs->status & ~(SR_VS)) | SR_VS_CLEAN; diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c index 0487ab19b234..3be469cb9266 100644 --- a/arch/riscv/kernel/cpufeature.c +++ b/arch/riscv/kernel/cpufeature.c @@ -37,6 +37,8 @@ __ro_after_init DEFINE_STATIC_KEY_FALSE(cpu_hwcap_fpu); #include __ro_after_init DEFINE_STATIC_KEY_FALSE(cpu_hwcap_vector); unsigned long riscv_vsize __read_mostly; +EXPORT_SYMBOL(cpu_hwcap_vector); +EXPORT_SYMBOL(riscv_vsize); #endif /** @@ -346,4 +348,3 @@ void __init_or_module riscv_cpufeature_patch_func(struct alt_entry *begin, } } #endif -} diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index e88a37fc77ed..a5a76d1374ec 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -134,7 +135,6 @@ void start_thread(struct pt_regs *regs, unsigned long pc, if (WARN_ON(!vstate->datap)) return; } - regs->status |= SR_VS_INITIAL; /* * Restore the initial value to the vector register @@ -230,3 +229,22 @@ int copy_thread(struct task_struct *p, const struct kernel_clone_args *args) p->thread.sp = (unsigned long)childregs; /* kernel sp */ return 0; } + +#ifdef CONFIG_VECTOR +int rvv_proc_enable(unsigned long x) +{ + switch (x) { + case PR_RVV_DISABLE: + vstate_off(current, task_pt_regs(current)); + return 0; + case PR_RVV_ENABLE: + vstate_on(current, task_pt_regs(current)); + return 0; + case PR_RVV_QUERY: + return vstate_query(task_pt_regs(current)); + default: + return -(EINVAL); + + } +} +#endif diff --git a/arch/riscv/kvm/vcpu_vector.c b/arch/riscv/kvm/vcpu_vector.c index 37bf4ffd47dd..9d1613da561a 100644 --- a/arch/riscv/kvm/vcpu_vector.c +++ b/arch/riscv/kvm/vcpu_vector.c @@ -20,7 +20,7 @@ extern unsigned long riscv_vsize; void kvm_riscv_vcpu_vector_reset(struct kvm_vcpu *vcpu) { - unsigned long isa = vcpu->arch.isa; + unsigned long isa = *vcpu->arch.isa; struct kvm_cpu_context *cntx = &vcpu->arch.guest_context; cntx->sstatus &= ~SR_VS; @@ -39,20 +39,20 @@ static void kvm_riscv_vcpu_vector_clean(struct kvm_cpu_context *cntx) } void kvm_riscv_vcpu_guest_vector_save(struct kvm_cpu_context *cntx, - unsigned long isa) + unsigned long *isa) { if ((cntx->sstatus & SR_VS) == SR_VS_DIRTY) { - if (riscv_isa_extension_available(&isa, v)) + if (riscv_isa_extension_available(isa, v)) __kvm_riscv_vector_save(cntx); kvm_riscv_vcpu_vector_clean(cntx); } } void kvm_riscv_vcpu_guest_vector_restore(struct kvm_cpu_context *cntx, - unsigned long isa) + unsigned long *isa) { if ((cntx->sstatus & SR_VS) != SR_VS_OFF) { - if (riscv_isa_extension_available(&isa, v)) + if (riscv_isa_extension_available(isa, v)) __kvm_riscv_vector_restore(cntx); kvm_riscv_vcpu_vector_clean(cntx); } @@ -122,7 +122,7 @@ int kvm_riscv_vcpu_get_reg_vector(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg, unsigned long rtype) { - unsigned long isa = vcpu->arch.isa; + unsigned long isa = *vcpu->arch.isa; unsigned long __user *uaddr = (unsigned long __user *)(unsigned long)reg->addr; unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | @@ -149,7 +149,7 @@ int kvm_riscv_vcpu_set_reg_vector(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg, unsigned long rtype) { - unsigned long isa = vcpu->arch.isa; + unsigned long isa = *vcpu->arch.isa; unsigned long __user *uaddr = (unsigned long __user *)(unsigned long)reg->addr; unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK | diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index a5e06dcbba13..8ea56e4c48f8 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -281,6 +281,12 @@ struct prctl_mm_map { # define PR_SME_VL_LEN_MASK 0xffff # define PR_SME_VL_INHERIT (1 << 17) /* inherit across exec */ +/* RISC-V V vector extension */ +#define PR_RVV_STATE 65 +# define PR_RVV_DISABLE 0 +# define PR_RVV_ENABLE 1 +# define PR_RVV_QUERY 2 + #define PR_SET_VMA 0x53564d41 # define PR_SET_VMA_ANON_NAME 0 diff --git a/kernel/sys.c b/kernel/sys.c index b911fa6d81ab..3049b1823273 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -138,6 +138,9 @@ #ifndef GET_TAGGED_ADDR_CTRL # define GET_TAGGED_ADDR_CTRL() (-EINVAL) #endif +#ifndef RVV_PROC_ENABLE +# define RVV_PROC_ENABLE(x) (-EINVAL) +#endif /* * this is where the system-wide overflow UID and GID are defined, for @@ -2620,6 +2623,10 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, error = sched_core_share_pid(arg2, arg3, arg4, arg5); break; #endif + case PR_RVV_STATE: + error = RVV_PROC_ENABLE(arg2); + break; + case PR_SET_VMA: error = prctl_set_vma(arg2, arg3, arg4, arg5); break; -- 2.25.1