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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 58AA6C4167B for ; Mon, 27 Nov 2023 13:33:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=v+FUoQPIq+YDZUbvyaLr5gvA7gSLdqkwdlIO3Cqw7JU=; b=nP67FQCwjw3PyZ TSp5OAhg+1xAh5rJCQU6HGlh67lHWSqlA5ge8qyubBT34uFWXiCh/a1qJQx0pcgcgoEUg9kbq7yyU NSkTc2oFbq4yyVMpdvMwWTFByrruujrYm5zXivRT7+L2vtkQz9o9rCY0+u9oDH4m2R1HakyIpuugx 0ow6oiwy174eMXFHrqESxU+8oleFcHrKMNTa5c+7zmHaK6DsNpo+nN8PDB1eTW436EhtJ338Qr1Dd KGFiULEjM+f2i5Fn7hZVhkQPAuC4p18Xe2yumV6txuwQhzcaAAcMeUncNVcN51Q4JvbuRvDCqRyrJ khoL3gn3vNPk7Apkgc3A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r7bjB-002aOn-1D; Mon, 27 Nov 2023 13:32:45 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r7bj8-002aOF-1N for linux-arm-kernel@lists.infradead.org; Mon, 27 Nov 2023 13:32:44 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id D069B2F4; Mon, 27 Nov 2023 05:33:24 -0800 (PST) Received: from FVFF77S0Q05N (unknown [10.57.43.171]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 7F3813F73F; Mon, 27 Nov 2023 05:32:35 -0800 (PST) Date: Mon, 27 Nov 2023 13:32:32 +0000 From: Mark Rutland To: Ard Biesheuvel Cc: linux-arm-kernel@lists.infradead.org, linux-crypto@vger.kernel.org, Ard Biesheuvel , Marc Zyngier , Will Deacon , Kees Cook , Catalin Marinas , Mark Brown , Eric Biggers , Sebastian Andrzej Siewior Subject: Re: [PATCH v3 3/5] arm64: fpsimd: Implement lazy restore for kernel mode FPSIMD Message-ID: References: <20231127122259.2265164-7-ardb@google.com> <20231127122259.2265164-10-ardb@google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20231127122259.2265164-10-ardb@google.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231127_053242_565336_4BCA0D02 X-CRM114-Status: GOOD ( 27.24 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Mon, Nov 27, 2023 at 01:23:03PM +0100, Ard Biesheuvel wrote: > From: Ard Biesheuvel > > Now that kernel mode FPSIMD state is context switched along with other > task state, we can enable the existing logic that keeps track of which > task's FPSIMD state the CPU is holding in its registers. If it is the > context of the task that we are switching to, we can elide the reload of > the FPSIMD state from memory. > > Note that we also need to check whether the FPSIMD state on this CPU is > the most recent: if a task gets migrated away and back again, the state > in memory may be more recent than the state in the CPU. So add another > CPU id field to task_struct to keep track of this. (We could reuse the > existing CPU id field used for user mode context, but that might result > in user state to be discarded unnecessarily, given that two distinct > CPUs could be holding the most recent user mode state and the most > recent kernel mode state) > > Signed-off-by: Ard Biesheuvel > Reviewed-by: Mark Brown > --- > arch/arm64/include/asm/processor.h | 1 + > arch/arm64/kernel/fpsimd.c | 18 ++++++++++++++++++ > 2 files changed, 19 insertions(+) > > diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h > index dcb51c0571af..332f15d0abcf 100644 > --- a/arch/arm64/include/asm/processor.h > +++ b/arch/arm64/include/asm/processor.h > @@ -169,6 +169,7 @@ struct thread_struct { > struct debug_info debug; /* debugging */ > > struct user_fpsimd_state kmode_fpsimd_state; > + unsigned int kmode_fpsimd_cpu; > #ifdef CONFIG_ARM64_PTR_AUTH > struct ptrauth_keys_user keys_user; > #ifdef CONFIG_ARM64_PTR_AUTH_KERNEL > diff --git a/arch/arm64/kernel/fpsimd.c b/arch/arm64/kernel/fpsimd.c > index 198918805bf6..112111a078b6 100644 > --- a/arch/arm64/kernel/fpsimd.c > +++ b/arch/arm64/kernel/fpsimd.c > @@ -1476,12 +1476,30 @@ void do_fpsimd_exc(unsigned long esr, struct pt_regs *regs) > > static void fpsimd_load_kernel_state(struct task_struct *task) > { > + struct cpu_fp_state *last = this_cpu_ptr(&fpsimd_last_state); > + > + /* > + * Elide the load if this CPU holds the most recent kernel mode > + * FPSIMD context of the current task. > + */ > + if (last->st == &task->thread.kmode_fpsimd_state && > + task->thread.kmode_fpsimd_cpu == smp_processor_id()) > + return; > + > fpsimd_load_state(&task->thread.kmode_fpsimd_state); > } > > static void fpsimd_save_kernel_state(struct task_struct *task) > { > + struct cpu_fp_state cpu_fp_state = { > + .st = &task->thread.kmode_fpsimd_state, > + .to_save = FP_STATE_FPSIMD, > + }; > + > fpsimd_save_state(&task->thread.kmode_fpsimd_state); > + fpsimd_bind_state_to_cpu(&cpu_fp_state); > + > + task->thread.kmode_fpsimd_cpu = smp_processor_id(); > } I was a little worried tha we might be missing a change to fpsimd_cpu_pm_notifier() to handle contesxt-destructive idle states correctly, but since that clears the fpsimd_last_state variable already, that should do the right thing as-is. Acked-by: Mark Rutland Mark. > > void fpsimd_thread_switch(struct task_struct *next) > -- > 2.43.0.rc1.413.gea7ed67945-goog > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel