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 EB2D5C61D92 for ; Tue, 21 Nov 2023 13:54:58 +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:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=Z+4oI49ngPPPF6RMYHv49/lxSxHM3yByKzo0rraRcME=; b=t/nnPCsC8+9TCVXb/0fFVEXnaK xpJw3xKDSGdUSpHaRQCJMXboXZCysDtGsEIsxvaAwSdmUfp6pflM9j94ePRNQ6WpDxmIdZC/bv3GQ C3p/fCg2Uo/TKcW3lI2sohu+nhJT/2getoXPspr1jijkUcwM0L1mTOxKje1Drrs0Vyh2rhDsj1Foc g+yTC/PpSRLj4TX2uiaQy5fB7YliuXHspNirKM8H7727o3awua36CyBDAyxtcpvDW7GpXLI8uq5dL TXSvJIZ1+1syoTWdxMjW/tWEHTf2juFhsJ/q6pLhMjuLlruohkOADna6u+b51Cc/9rxzaXt1t+++E l2C8d7ZA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r5RCw-00Gwlx-1z; Tue, 21 Nov 2023 13:54:30 +0000 Received: from mail-wr1-x44a.google.com ([2a00:1450:4864:20::44a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r5RB3-00GupX-35 for linux-arm-kernel@lists.infradead.org; Tue, 21 Nov 2023 13:52:38 +0000 Received: by mail-wr1-x44a.google.com with SMTP id ffacd0b85a97d-332cb6be78fso805282f8f.0 for ; Tue, 21 Nov 2023 05:52:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700574749; x=1701179549; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=6nh+L/THzFuFl4YIcCggLg4JDn2dxugge/pgZIj3Gkk=; b=CTGb6fvgYeZis6anwa6CAmwRd/YBtxhe5VkMw6nqbMw0pXMWsOyIOw5lrVN//ybFe4 PFvqKOt1kiEM4KBibtLbXANYumlR/Xq1HuxDn7+GcnTL9S8nC6dHSRdre4SfryWGBrXc WnWG/FpnXWm2Kqur0RqLV3L7K0k/38QSe0DlNrl6POOaVGj0korBNKODHgAcUxAX6v9K edisOe7SUma7zp/lpSw4ziEmUlrJdMkPyAE11qoqP3PTiwc6pP7VGaWuBU2UMiIReLFJ CsxNXocz3i5vXgAPyZKrCCno02vRcjuXBC4hw/qsuMDuKAL38rJLGyTHyy7+uTrsMe1x wLnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700574749; x=1701179549; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6nh+L/THzFuFl4YIcCggLg4JDn2dxugge/pgZIj3Gkk=; b=sALSFdBCqcD9uMmTMvw7tat3GVyUm2BvIQ+LL8uvbLr6h4tPzjaEVQUpt+8ApXLHnN EOCvkpON3vf+EUVFbOnFBgkrHsDq5v9U2VoWyHjx5v91DeH/v44D6nj6a8DmMBUFVK5a aipTQABy2G+SB/4VIWLOyA7lM/zARmQNCAIYAaky+TuQpaciZolDWWkq+NrENi0DPuh+ w0fMr6K8tYBSXugA2Vce2br0sqfxdhrpCGxttMqn3ynCv9KfTLB9TN8YNXE72OrDL25E hpAcBVJOhOCrXc3ctxcAgqnEIPebVeYxlZGWiJjPOfIBFyyHeIzcWtueAuaH808hJnRo 92fA== X-Gm-Message-State: AOJu0Yw9Wec1CAGUcMCduSiib1XNwzsGgfQFXwecIxtxD7fVhtN2Qtss SfDzJd2d3IafKTEc4WnoTNk7+H3QZwWzOCUGkfmUZRMh0n95UKPwIMkHsZdzhzZykwMB/ai1L5n LZjgDWvKW4ErJnKNjMtbY6mAziUEH0BiXm6Qb9OTU/tndzV0HsG0JG4j3A6vDzr4N9HLvuI4Ocu c= X-Google-Smtp-Source: AGHT+IH3lQcZ4VNoRnTTfTZQZTzfl2wdnv3MkV41RLgKFoyQihCjasRiMXWm8PEVO4n6LKhbpBoGixY4 X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a5d:694a:0:b0:332:d00f:2599 with SMTP id r10-20020a5d694a000000b00332d00f2599mr6384wrw.9.1700574749350; Tue, 21 Nov 2023 05:52:29 -0800 (PST) Date: Tue, 21 Nov 2023 14:52:15 +0100 In-Reply-To: <20231121135212.2235251-5-ardb@google.com> Mime-Version: 1.0 References: <20231121135212.2235251-5-ardb@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=7194; i=ardb@kernel.org; h=from:subject; bh=/U2csTETLyCBvPEuyyXO9UmmYpr/OXhC/Z7P6O7IHzo=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JITVmG3+ozNJIlxm2pjGHrdN1vCee3bBfft2q7G1fX+9aI SnQqLuqo5SFQYyDQVZMkUVg9t93O09PlKp1niULM4eVCWQIAxenAEwkJZSR4Zp68/nast11525u SdlyK0xEnr9J3at2YsUCV719cZ56/xgZruTffDYtr2eB/RO+O25/f0tx2Dfp8THef/X9lvO81Rs a2AE= X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Message-ID: <20231121135212.2235251-7-ardb@google.com> Subject: [PATCH 2/3] arm64: fpsimd: preserve/restore kernel mode NEON at context switch From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , Marc Zyngier , Will Deacon , Mark Rutland , Kees Cook , Catalin Marinas , Mark Brown X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231121_055234_027506_A2FC65E5 X-CRM114-Status: GOOD ( 28.98 ) 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 From: Ard Biesheuvel Currently, the SIMD register file is not preserved and restored along with the general registers on exception entry/exit or context switch. For this reason, we disable preemption when enabling the NEON for kernel mode use in task context, and suspend the processing of softirqs so that there are no concurrent uses in the kernel. (Kernel mode NEON may not be used at all in other contexts). Disabling preemption while doing CPU intensive work on inputs of potentially unbounded size is bad for real-time performance, which is why we try and ensure that SIMD crypto code does not operate on more than ~4k at a time, which is an arbitrary value and requires assembler code to implement efficiently. We can avoid the need for disabling preemption if we can ensure that any in-kernel users of the NEON will not lose the FPSIMD register state across a context switch. And given that disabling softirqs implicitly disables preemption as well, we will also have to ensure that a softirq that uses the NEON can safely interrupt an in-kernel user. So introduce a thread_info flag TIF_USING_KMODE_NEON, and modify the context switch hook for FPSIMD to preserve resp. restore the kernel mode FPSIMD to/from struct thread_struct when it is set. This avoids any scheduling blackouts due to prolonged use of the NEON in kernel mode, without the need for manual yielding. In order to support softirq processing while the NEON is being used in kernel task context, use the same flag to decide whether the kernel mode FPSIMD state needs to be preserved and restored before allowing the NEON to be used in softirq context. Signed-off-by: Ard Biesheuvel --- arch/arm64/include/asm/processor.h | 2 + arch/arm64/include/asm/thread_info.h | 1 + arch/arm64/kernel/fpsimd.c | 75 ++++++++++++++++---- 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h index e5bc54522e71..dcb51c0571af 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -167,6 +167,8 @@ struct thread_struct { unsigned long fault_address; /* fault info */ unsigned long fault_code; /* ESR_EL1 value */ struct debug_info debug; /* debugging */ + + struct user_fpsimd_state kmode_fpsimd_state; #ifdef CONFIG_ARM64_PTR_AUTH struct ptrauth_keys_user keys_user; #ifdef CONFIG_ARM64_PTR_AUTH_KERNEL diff --git a/arch/arm64/include/asm/thread_info.h b/arch/arm64/include/asm/thread_info.h index 553d1bc559c6..b65324d1ffc8 100644 --- a/arch/arm64/include/asm/thread_info.h +++ b/arch/arm64/include/asm/thread_info.h @@ -80,6 +80,7 @@ void arch_setup_new_exec(void); #define TIF_TAGGED_ADDR 26 /* Allow tagged user addresses */ #define TIF_SME 27 /* SME in use */ #define TIF_SME_VL_INHERIT 28 /* Inherit SME vl_onexec across exec */ +#define TIF_USING_KMODE_NEON 29 /* Task is in a kernel mode NEON section */ #define _TIF_SIGPENDING (1 << TIF_SIGPENDING) #define _TIF_NEED_RESCHED (1 << TIF_NEED_RESCHED) diff --git a/arch/arm64/kernel/fpsimd.c b/arch/arm64/kernel/fpsimd.c index ccc4a78a70e4..326c1613091e 100644 --- a/arch/arm64/kernel/fpsimd.c +++ b/arch/arm64/kernel/fpsimd.c @@ -357,6 +357,7 @@ static void task_fpsimd_load(void) WARN_ON(!system_supports_fpsimd()); WARN_ON(preemptible()); + WARN_ON(test_thread_flag(TIF_USING_KMODE_NEON)); if (system_supports_sve() || system_supports_sme()) { switch (current->thread.fp_type) { @@ -1482,20 +1483,29 @@ void fpsimd_thread_switch(struct task_struct *next) WARN_ON_ONCE(!irqs_disabled()); - /* Save unsaved fpsimd state, if any: */ - fpsimd_save(); + if (!test_thread_flag(TIF_USING_KMODE_NEON)) { + /* Save unsaved user mode fpsimd state, if any: */ + fpsimd_save(); + } else { + fpsimd_save_state(¤t->thread.kmode_fpsimd_state); + } - /* - * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's - * state. For kernel threads, FPSIMD registers are never loaded - * and wrong_task and wrong_cpu will always be true. - */ - wrong_task = __this_cpu_read(fpsimd_last_state.st) != - &next->thread.uw.fpsimd_state; - wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id(); + if (test_tsk_thread_flag(next, TIF_USING_KMODE_NEON)) { + fpsimd_load_state(&next->thread.kmode_fpsimd_state); + set_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE); + } else { + /* + * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's + * state. For kernel threads, FPSIMD registers are never loaded + * and wrong_task and wrong_cpu will always be true. + */ + wrong_task = __this_cpu_read(fpsimd_last_state.st) != + &next->thread.uw.fpsimd_state; + wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id(); - update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE, - wrong_task || wrong_cpu); + update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE, + wrong_task || wrong_cpu); + } } static void fpsimd_flush_thread_vl(enum vec_type type) @@ -1834,11 +1844,37 @@ void kernel_neon_begin(void) get_cpu_fpsimd_context(); - /* Save unsaved fpsimd state, if any: */ - fpsimd_save(); + if (!test_thread_flag(TIF_USING_KMODE_NEON)) { + /* Save unsaved user mode fpsimd state, if any: */ + fpsimd_save(); + + /* + * Set the thread flag so that the kernel mode NEON state will + * be context switched along with the rest of the task state. + * + * On non-PREEMPT_RT, softirqs may interrupt task level kernel + * mode NEON, but the task will not be preemptible so setting + * TIF_USING_KMODE_NEON for those would be both wrong (as it + * would mark the task context NEON state as requiring a + * context switch) and unnecessary. + * + * On PREEMPT_RT, softirqs are serviced from a separate thread, + * which is scheduled as usual, and this guarantees that these + * softirqs are not interrupting use of the NEON in kernel mode + * in task context. So in this case, setting the flag here is + * always appropriate. + */ + if (IS_ENABLED(CONFIG_PREEMPT_RT) || !in_serving_softirq()) + set_thread_flag(TIF_USING_KMODE_NEON); + } else { + BUG_ON(IS_ENABLED(CONFIG_PREEMPT_RT) || !in_serving_softirq()); + fpsimd_save_state(¤t->thread.kmode_fpsimd_state); + } /* Invalidate any task state remaining in the fpsimd regs: */ fpsimd_flush_cpu_state(); + + put_cpu_fpsimd_context(); } EXPORT_SYMBOL_GPL(kernel_neon_begin); @@ -1856,7 +1892,16 @@ void kernel_neon_end(void) if (!system_supports_fpsimd()) return; - put_cpu_fpsimd_context(); + /* + * If we are returning from a nested use of the kernel mode NEON, + * restore the task context kernel mode NEON state. This can only + * happen when running in softirq context on non-PREEMPT_RT. + */ + if (!IS_ENABLED(CONFIG_PREEMPT_RT) && in_serving_softirq() && + test_thread_flag(TIF_USING_KMODE_NEON)) + fpsimd_load_state(¤t->thread.kmode_fpsimd_state); + else + clear_thread_flag(TIF_USING_KMODE_NEON); } EXPORT_SYMBOL_GPL(kernel_neon_end); -- 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