linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: <gregkh@linuxfoundation.org>
To: anshuman.khandual@arm.com,ardb@kernel.org,broonie@kernel.org,catalin.marinas@arm.com,ebiederm@xmission.com,f.fainelli@gmail.com,gregkh@linuxfoundation.org,haibinzhang@tencent.com,hewenliang4@huawei.com,james.morse@arm.com,joey.gouly@arm.com,linux-arm-kernel@lists.infradead.org,mark.rutland@arm.com,peterz@infradead.org,ruanjinjie@huawei.com,sashal@kernel.org,scott@os.amperecomputing.com,stable@kernel.org,will@kernel.org,youngmin.nam@samsung.com,yuzenghui@huawei.com
Cc: <stable-commits@vger.kernel.org>
Subject: Patch "arm64: factor out EL1 SSBS emulation hook" has been added to the 5.10-stable tree
Date: Mon, 16 Oct 2023 10:02:24 +0200	[thread overview]
Message-ID: <2023101624-marital-emphasize-d5cd@gregkh> (raw)
In-Reply-To: <20231011100545.979577-9-ruanjinjie@huawei.com>


This is a note to let you know that I've just added the patch titled

    arm64: factor out EL1 SSBS emulation hook

to the 5.10-stable tree which can be found at:
    http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
     arm64-factor-out-el1-ssbs-emulation-hook.patch
and it can be found in the queue-5.10 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree,
please let <stable@vger.kernel.org> know about it.


From ruanjinjie@huawei.com Wed Oct 11 12:07:07 2023
From: Jinjie Ruan <ruanjinjie@huawei.com>
Date: Wed, 11 Oct 2023 10:05:38 +0000
Subject: arm64: factor out EL1 SSBS emulation hook
To: <catalin.marinas@arm.com>, <will@kernel.org>, <yuzenghui@huawei.com>, <anshuman.khandual@arm.com>, <gregkh@linuxfoundation.org>, <mark.rutland@arm.com>, <broonie@kernel.org>, <youngmin.nam@samsung.com>, <ardb@kernel.org>, <f.fainelli@gmail.com>, <james.morse@arm.com>, <sashal@kernel.org>, <scott@os.amperecomputing.com>, <ebiederm@xmission.com>, <haibinzhang@tencent.com>, <hewenliang4@huawei.com>, <linux-arm-kernel@lists.infradead.org>, <linux-kernel@vger.kernel.org>, <stable@kernel.org>
Cc: <ruanjinjie@huawei.com>
Message-ID: <20231011100545.979577-9-ruanjinjie@huawei.com>

From: Mark Rutland <mark.rutland@arm.com>

commit bff8f413c71ffc3cb679dbd9a5632b33af563f9f upstream.

Currently call_undef_hook() is used to handle UNDEFINED exceptions from
EL0 and EL1. As support for deprecated instructions may be enabled
independently, the handlers for individual instructions are organised as
a linked list of struct undef_hook which can be manipulated dynamically.
As this can be manipulated dynamically, the list is protected with a
raw_spinlock which must be acquired when handling UNDEFINED exceptions
or when manipulating the list of handlers.

This locking is unfortunate as it serialises handling of UNDEFINED
exceptions, and requires RCU to be enabled for lockdep, requiring the
use of RCU_NONIDLE() in resume path of cpu_suspend() since commit:

  a2c42bbabbe260b7 ("arm64: spectre: Prevent lockdep splat on v4 mitigation enable path")

The list of UNDEFINED handlers largely consist of handlers for
exceptions taken from EL0, and the only handler for exceptions taken
from EL1 handles `MSR SSBS, #imm` on CPUs which feature PSTATE.SSBS but
lack the corresponding MSR (Immediate) instruction. Other than this we
never expect to take an UNDEFINED exception from EL1 in normal
operation.

This patch reworks do_el0_undef() to invoke the EL1 SSBS handler
directly, relegating call_undef_hook() to only handle EL0 UNDEFs. This
removes redundant work to iterate the list for EL1 UNDEFs, and removes
the need for locking, permitting EL1 UNDEFs to be handled in parallel
without contention.

The RCU_NONIDLE() call in cpu_suspend() will be removed in a subsequent
patch, as there are other potential issues with the use of
instrumentable code and RCU in the CPU suspend code.

I've tested this by forcing the detection of SSBS on a CPU that doesn't
have it, and verifying that the try_emulate_el1_ssbs() callback is
invoked.

Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: James Morse <james.morse@arm.com>
Cc: Joey Gouly <joey.gouly@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Will Deacon <will@kernel.org>
Link: https://lore.kernel.org/r/20221019144123.612388-4-mark.rutland@arm.com
Signed-off-by: Will Deacon <will@kernel.org>
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 arch/arm64/include/asm/spectre.h |    2 ++
 arch/arm64/kernel/proton-pack.c  |   26 +++++++-------------------
 arch/arm64/kernel/traps.c        |   15 ++++++++-------
 3 files changed, 17 insertions(+), 26 deletions(-)

--- a/arch/arm64/include/asm/spectre.h
+++ b/arch/arm64/include/asm/spectre.h
@@ -18,6 +18,7 @@ enum mitigation_state {
 	SPECTRE_VULNERABLE,
 };
 
+struct pt_regs;
 struct task_struct;
 
 enum mitigation_state arm64_get_spectre_v2_state(void);
@@ -33,4 +34,5 @@ enum mitigation_state arm64_get_spectre_
 bool is_spectre_bhb_affected(const struct arm64_cpu_capabilities *entry, int scope);
 u8 spectre_bhb_loop_affected(int scope);
 void spectre_bhb_enable_mitigation(const struct arm64_cpu_capabilities *__unused);
+bool try_emulate_el1_ssbs(struct pt_regs *regs, u32 instr);
 #endif	/* __ASM_SPECTRE_H */
--- a/arch/arm64/kernel/proton-pack.c
+++ b/arch/arm64/kernel/proton-pack.c
@@ -537,10 +537,13 @@ bool has_spectre_v4(const struct arm64_c
 	return state != SPECTRE_UNAFFECTED;
 }
 
-static int ssbs_emulation_handler(struct pt_regs *regs, u32 instr)
+bool try_emulate_el1_ssbs(struct pt_regs *regs, u32 instr)
 {
-	if (user_mode(regs))
-		return 1;
+	const u32 instr_mask = ~(1U << PSTATE_Imm_shift);
+	const u32 instr_val = 0xd500401f | PSTATE_SSBS;
+
+	if ((instr & instr_mask) != instr_val)
+		return false;
 
 	if (instr & BIT(PSTATE_Imm_shift))
 		regs->pstate |= PSR_SSBS_BIT;
@@ -548,19 +551,11 @@ static int ssbs_emulation_handler(struct
 		regs->pstate &= ~PSR_SSBS_BIT;
 
 	arm64_skip_faulting_instruction(regs, 4);
-	return 0;
+	return true;
 }
 
-static struct undef_hook ssbs_emulation_hook = {
-	.instr_mask	= ~(1U << PSTATE_Imm_shift),
-	.instr_val	= 0xd500401f | PSTATE_SSBS,
-	.fn		= ssbs_emulation_handler,
-};
-
 static enum mitigation_state spectre_v4_enable_hw_mitigation(void)
 {
-	static bool undef_hook_registered = false;
-	static DEFINE_RAW_SPINLOCK(hook_lock);
 	enum mitigation_state state;
 
 	/*
@@ -571,13 +566,6 @@ static enum mitigation_state spectre_v4_
 	if (state != SPECTRE_MITIGATED || !this_cpu_has_cap(ARM64_SSBS))
 		return state;
 
-	raw_spin_lock(&hook_lock);
-	if (!undef_hook_registered) {
-		register_undef_hook(&ssbs_emulation_hook);
-		undef_hook_registered = true;
-	}
-	raw_spin_unlock(&hook_lock);
-
 	if (spectre_v4_mitigations_off()) {
 		sysreg_clear_set(sctlr_el1, 0, SCTLR_ELx_DSSBS);
 		asm volatile(SET_PSTATE_SSBS(1));
--- a/arch/arm64/kernel/traps.c
+++ b/arch/arm64/kernel/traps.c
@@ -311,12 +311,7 @@ static int call_undef_hook(struct pt_reg
 	int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
 	void __user *pc = (void __user *)instruction_pointer(regs);
 
-	if (!user_mode(regs)) {
-		__le32 instr_le;
-		if (get_kernel_nofault(instr_le, (__force __le32 *)pc))
-			goto exit;
-		instr = le32_to_cpu(instr_le);
-	} else if (compat_thumb_mode(regs)) {
+	if (compat_thumb_mode(regs)) {
 		/* 16-bit Thumb instruction */
 		__le16 instr_le;
 		if (get_user(instr_le, (__le16 __user *)pc))
@@ -409,9 +404,15 @@ void do_el0_undef(struct pt_regs *regs,
 
 void do_el1_undef(struct pt_regs *regs, unsigned long esr)
 {
-	if (call_undef_hook(regs) == 0)
+	u32 insn;
+
+	if (aarch64_insn_read((void *)regs->pc, &insn))
+		goto out_err;
+
+	if (try_emulate_el1_ssbs(regs, insn))
 		return;
 
+out_err:
 	die("Oops - Undefined instruction", regs, esr);
 }
 


Patches currently in stable-queue which might be from ruanjinjie@huawei.com are

queue-5.10/arm64-factor-insn-read-out-of-call_undef_hook.patch
queue-5.10/arm64-rework-el0-mrs-emulation.patch
queue-5.10/arm64-die-pass-err-as-long.patch
queue-5.10/arm64-armv8_deprecated-rework-deprected-instruction-handling.patch
queue-5.10/arm64-armv8_deprecated-fix-unused-function-error.patch
queue-5.10/arm64-armv8_deprecated-move-aarch32-helper-earlier.patch
queue-5.10/arm64-consistently-pass-esr_elx-to-die.patch
queue-5.10/arm64-factor-out-el1-ssbs-emulation-hook.patch
queue-5.10/arm64-report-el1-undefs-better.patch
queue-5.10/arm64-armv8_deprecated-fold-ops-into-insn_emulation.patch
queue-5.10/arm64-rework-bti-exception-handling.patch
queue-5.10/arm64-rework-fpac-exception-handling.patch
queue-5.10/arm64-split-el0-el1-undef-handlers.patch
queue-5.10/arm64-allow-kprobes-on-el0-handlers.patch
queue-5.10/arm64-armv8_deprecated-move-emulation-functions.patch

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  reply	other threads:[~2023-10-16  8:03 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-11 10:05 [PATCH v5.10 RESEND 00/15] arm64: Fix a concurrency issue in emulation_proc_handler() Jinjie Ruan
2023-10-11 10:05 ` [PATCH v5.10 RESEND 01/15] arm64: report EL1 UNDEFs better Jinjie Ruan
2023-10-16  8:02   ` Patch "arm64: report EL1 UNDEFs better" has been added to the 5.10-stable tree gregkh
2023-10-11 10:05 ` [PATCH v5.10 RESEND 02/15] arm64: die(): pass 'err' as long Jinjie Ruan
2023-10-16  8:02   ` Patch "arm64: die(): pass 'err' as long" has been added to the 5.10-stable tree gregkh
2023-10-11 10:05 ` [PATCH v5.10 RESEND 03/15] arm64: consistently pass ESR_ELx to die() Jinjie Ruan
2023-10-16  8:02   ` Patch "arm64: consistently pass ESR_ELx to die()" has been added to the 5.10-stable tree gregkh
2023-10-11 10:05 ` [PATCH v5.10 RESEND 04/15] arm64: rework FPAC exception handling Jinjie Ruan
2023-10-16  8:02   ` Patch "arm64: rework FPAC exception handling" has been added to the 5.10-stable tree gregkh
2023-10-11 10:05 ` [PATCH v5.10 RESEND 05/15] arm64: rework BTI exception handling Jinjie Ruan
2023-10-16  8:02   ` Patch "arm64: rework BTI exception handling" has been added to the 5.10-stable tree gregkh
2023-10-11 10:05 ` [PATCH v5.10 RESEND 06/15] arm64: allow kprobes on EL0 handlers Jinjie Ruan
2023-10-16  8:02   ` Patch "arm64: allow kprobes on EL0 handlers" has been added to the 5.10-stable tree gregkh
2023-10-11 10:05 ` [PATCH v5.10 RESEND 07/15] arm64: split EL0/EL1 UNDEF handlers Jinjie Ruan
2023-10-16  8:02   ` Patch "arm64: split EL0/EL1 UNDEF handlers" has been added to the 5.10-stable tree gregkh
2023-10-11 10:05 ` [PATCH v5.10 RESEND 08/15] arm64: factor out EL1 SSBS emulation hook Jinjie Ruan
2023-10-16  8:02   ` gregkh [this message]
2023-10-11 10:05 ` [PATCH v5.10 RESEND 09/15] arm64: factor insn read out of call_undef_hook() Jinjie Ruan
2023-10-16  8:02   ` Patch "arm64: factor insn read out of call_undef_hook()" has been added to the 5.10-stable tree gregkh
2023-10-11 10:05 ` [PATCH v5.10 RESEND 10/15] arm64: rework EL0 MRS emulation Jinjie Ruan
2023-10-16  8:02   ` Patch "arm64: rework EL0 MRS emulation" has been added to the 5.10-stable tree gregkh
2023-10-11 10:05 ` [PATCH v5.10 RESEND 11/15] arm64: armv8_deprecated: fold ops into insn_emulation Jinjie Ruan
2023-10-16  8:02   ` Patch "arm64: armv8_deprecated: fold ops into insn_emulation" has been added to the 5.10-stable tree gregkh
2023-10-11 10:05 ` [PATCH v5.10 RESEND 12/15] arm64: armv8_deprecated move emulation functions Jinjie Ruan
2023-10-16  8:02   ` Patch "arm64: armv8_deprecated move emulation functions" has been added to the 5.10-stable tree gregkh
2023-10-11 10:05 ` [PATCH v5.10 RESEND 13/15] arm64: armv8_deprecated: move aarch32 helper earlier Jinjie Ruan
2023-10-16  8:02   ` Patch "arm64: armv8_deprecated: move aarch32 helper earlier" has been added to the 5.10-stable tree gregkh
2023-10-11 10:05 ` [PATCH v5.10 RESEND 14/15] arm64: armv8_deprecated: rework deprected instruction handling Jinjie Ruan
2023-10-16  8:02   ` Patch "arm64: armv8_deprecated: rework deprected instruction handling" has been added to the 5.10-stable tree gregkh
2023-10-11 10:05 ` [PATCH v5.10 RESEND 15/15] arm64: armv8_deprecated: fix unused-function error Jinjie Ruan
2023-10-16  8:02   ` Patch "arm64: armv8_deprecated: fix unused-function error" has been added to the 5.10-stable tree gregkh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2023101624-marital-emphasize-d5cd@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=anshuman.khandual@arm.com \
    --cc=ardb@kernel.org \
    --cc=broonie@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=ebiederm@xmission.com \
    --cc=f.fainelli@gmail.com \
    --cc=haibinzhang@tencent.com \
    --cc=hewenliang4@huawei.com \
    --cc=james.morse@arm.com \
    --cc=joey.gouly@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=mark.rutland@arm.com \
    --cc=peterz@infradead.org \
    --cc=ruanjinjie@huawei.com \
    --cc=sashal@kernel.org \
    --cc=scott@os.amperecomputing.com \
    --cc=stable-commits@vger.kernel.org \
    --cc=stable@kernel.org \
    --cc=will@kernel.org \
    --cc=youngmin.nam@samsung.com \
    --cc=yuzenghui@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).