Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp4661082rwb; Mon, 31 Jul 2023 10:07:44 -0700 (PDT) X-Google-Smtp-Source: APBJJlEaynQqiTxCwG8U6DoSVj1y6CP/AztpHKWc+Fx3N7pV/KxlK1nYJLdwDdtGX8qHJUfXOvz5 X-Received: by 2002:a17:902:8305:b0:1b8:a812:7bc2 with SMTP id bd5-20020a170902830500b001b8a8127bc2mr9078757plb.8.1690823264453; Mon, 31 Jul 2023 10:07:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690823264; cv=none; d=google.com; s=arc-20160816; b=uF8K9PQqUPMQG3mQeP7WM5ruG6niHNCZ6AsN8ZN14N9p/xjGISlaoAIk9BOVnsdhc+ 5qsU7ei6Mu/v6w24kZ7twAvJHwGlskvAD5f9HYiZpLqqKfE1K3lt222dQNLlqwWL+hjF cgxwnfkD2/DjfZI20g0R+gB8PHry5forZPYwEA1Ncy8B2+eNmiG+LXE55Y+m+IX7atnV Us9x2IzvvugA7PKAQ29m1t8gEek8xVhmfF6LGh3i5Et6Pn47932jZXCMX+Hbho+L6HU2 Zmbw2GIx16v55H6YZPTXs3BWRCyKL6LCaSVF0AGACogtcdHzDk7EcDtKCp/IyCexntTA yrdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=VQLReHfqXw4IQFsKXR7Z66ViTgnZ/htZGrJG42Wb++0=; fh=REndtsOkTI3NRNVX/abcwQpwVkmbJ3QgkNzoNLHC+TQ=; b=vAIvU5OnZcnwgobKcDGTGrMi0xOCP59WR8Lba0ln2SfeQTz7r//XW6xSGSFnaxcxJo VXEefnAvJYgB3WxY+6+7SYZyGJHV+vO9XkP3/5ZlO47v1CVNs/3Zdh9uNvsnoTBnsm5B q+KxTh9PZKdXw+XuU470Nhpzyx8h3chVy7ORHaJs3NXBmrFCvqLlsSBYBOmjzcSU7OJK S9/UjFR31G0FKQS4ArHyb46fdVhV77PCWxpKFvWm7IqPQ7e3lToeSLmPz2ovyJataQCP /WctnJhK7W6iEEvZhfYp1pVW1PTLXHYQMG5Z6XgeTloF9kyULXfUCkLrmIluOJmCagBU ui9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@xen0n.name header.s=mail header.b=ZTlXul5Q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ky6-20020a170902f98600b001b9e970e961si7492457plb.277.2023.07.31.10.07.31; Mon, 31 Jul 2023 10:07:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@xen0n.name header.s=mail header.b=ZTlXul5Q; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229971AbjGaQWp (ORCPT + 99 others); Mon, 31 Jul 2023 12:22:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233055AbjGaQWk (ORCPT ); Mon, 31 Jul 2023 12:22:40 -0400 Received: from mailbox.box.xen0n.name (mail.xen0n.name [115.28.160.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E688E78 for ; Mon, 31 Jul 2023 09:22:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xen0n.name; s=mail; t=1690820551; bh=KXu0pOSJ+Yi7H0N8jumb2xkFoohQyaUvR6lhW0va5Ts=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=ZTlXul5QjkJDbwZKFvqqsa2YhpTRwjsyKvlrXBT9TrlOFnZdDUjvMZ4/katyWQ6/D htWKfeZ7hMQ/hnFhOI3nn+xCtHgsKjspOHeSq7YFP8agvzvgVdZonO1L3afJmNt6Bd bprSnmRBShnjws/v6MguTHhSyqC2sI7e6+c6vgtY= Received: from [192.168.9.172] (unknown [101.88.28.229]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mailbox.box.xen0n.name (Postfix) with ESMTPSA id E063C600A6; Tue, 1 Aug 2023 00:22:30 +0800 (CST) Message-ID: <94bd0300-a920-a0d7-244b-6caeba5fff26@xen0n.name> Date: Tue, 1 Aug 2023 00:22:30 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH] LoongArch: Allow usage of LSX/LASX in the kernel Content-Language: en-US To: Huacai Chen , Huacai Chen Cc: loongarch@lists.linux.dev, Xuefeng Li , Guo Ren , Jiaxun Yang , linux-kernel@vger.kernel.org, loongson-kernel@lists.loongnix.cn References: <20230722072201.2677516-1-chenhuacai@loongson.cn> From: WANG Xuerui In-Reply-To: <20230722072201.2677516-1-chenhuacai@loongson.cn> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 7/22/23 15:22, Huacai Chen wrote: > Allow usage of LSX/LASX in the kernel by extending kernel_fpu_begin() > and kernel_fpu_end(). > > Signed-off-by: Huacai Chen > --- > arch/loongarch/kernel/kfpu.c | 55 +++++++++++++++++++++++++++++++++--- > 1 file changed, 51 insertions(+), 4 deletions(-) > > diff --git a/arch/loongarch/kernel/kfpu.c b/arch/loongarch/kernel/kfpu.c > index 5c46ae8c6cac..ec5b28e570c9 100644 > --- a/arch/loongarch/kernel/kfpu.c > +++ b/arch/loongarch/kernel/kfpu.c > @@ -8,19 +8,40 @@ > #include > #include > > +static unsigned int euen_mask = CSR_EUEN_FPEN; > + > +/* > + * The critical section between kernel_fpu_begin() and kernel_fpu_end() > + * is non-reentrant. It is the caller's responsibility to avoid reentrance. > + * See drivers/gpu/drm/amd/display/amdgpu_dm/dc_fpu.c as an example. > + */ > static DEFINE_PER_CPU(bool, in_kernel_fpu); > +static DEFINE_PER_CPU(unsigned int, euen_current); > > void kernel_fpu_begin(void) > { > + unsigned int *euen_curr; > + > preempt_disable(); > > WARN_ON(this_cpu_read(in_kernel_fpu)); > > this_cpu_write(in_kernel_fpu, true); > + euen_curr = this_cpu_ptr(&euen_current); > > - if (!is_fpu_owner()) > - enable_fpu(); > + *euen_curr = csr_xchg32(euen_mask, euen_mask, LOONGARCH_CSR_EUEN); > + > +#ifdef CONFIG_CPU_HAS_LASX > + if (*euen_curr & CSR_EUEN_LASXEN) > + _save_lasx(¤t->thread.fpu); > + else > +#endif > +#ifdef CONFIG_CPU_HAS_LSX > + if (*euen_curr & CSR_EUEN_LSXEN) > + _save_lsx(¤t->thread.fpu); > else > +#endif > + if (*euen_curr & CSR_EUEN_FPEN) > _save_fp(¤t->thread.fpu); > > write_fcsr(LOONGARCH_FCSR0, 0); > @@ -29,15 +50,41 @@ EXPORT_SYMBOL_GPL(kernel_fpu_begin); > > void kernel_fpu_end(void) > { > + unsigned int *euen_curr; > + > WARN_ON(!this_cpu_read(in_kernel_fpu)); > > - if (!is_fpu_owner()) > - disable_fpu(); > + euen_curr = this_cpu_ptr(&euen_current); > + > +#ifdef CONFIG_CPU_HAS_LASX > + if (*euen_curr & CSR_EUEN_LASXEN) > + _restore_lasx(¤t->thread.fpu); > else > +#endif > +#ifdef CONFIG_CPU_HAS_LSX > + if (*euen_curr & CSR_EUEN_LSXEN) > + _restore_lsx(¤t->thread.fpu); > + else > +#endif > + if (*euen_curr & CSR_EUEN_FPEN) > _restore_fp(¤t->thread.fpu); > > + *euen_curr = csr_xchg32(*euen_curr, euen_mask, LOONGARCH_CSR_EUEN); > + > this_cpu_write(in_kernel_fpu, false); > > preempt_enable(); > } > EXPORT_SYMBOL_GPL(kernel_fpu_end); > + > +static int __init init_euen_mask(void) > +{ > + if (cpu_has_lsx) > + euen_mask |= CSR_EUEN_LSXEN; > + > + if (cpu_has_lasx) > + euen_mask |= CSR_EUEN_LASXEN; > + > + return 0; > +} > +arch_initcall(init_euen_mask); I've stressed this code a bit with my RAID5/6 LASX patch applied, then running $(nproc) copies of vector workload while letting the kernel repeatedly scrub a RAID6 array created from a bunch of loop devices, and there wasn't signs of context corruption either side. Although I only tested for a few minutes (so a Tested-by isn't appropriate yet), the code LGTM nevertheless, so... Reviewed-by: WANG Xuerui -- WANG "xen0n" Xuerui Linux/LoongArch mailing list: https://lore.kernel.org/loongarch/