linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 0/2] arm64/asm: Remove DAIF save/restore macros
@ 2022-11-23 18:02 Mark Brown
  2022-11-23 18:02 ` [PATCH v1 1/2] arm64/kpti: Move DAIF masking to C code Mark Brown
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Mark Brown @ 2022-11-23 18:02 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: Mark Rutland, Ard Biesheuvel, linux-arm-kernel, Mark Brown

As part of an effort to improve the maintainability of the code for
manipulating DAIF let's reduce the number of places where it is
implemented by lifting the masking done during TTBR1 replacement for
KPTI up into C code and removing the assembler macros it was using.

Due to a textual collision with the removal of enable_da this is based
on for-next/trivial in the arm64 tree.

Mark Brown (2):
  arm64/kpti: Move DAIF masking to C code
  arm64/asm: Remove unused assembler DAIF save/restore macros

 arch/arm64/include/asm/assembler.h   |  9 ---------
 arch/arm64/include/asm/mmu_context.h | 10 ++++++++++
 arch/arm64/mm/proc.S                 |  4 ----
 3 files changed, 10 insertions(+), 13 deletions(-)


base-commit: 32d495b0c3305546f4773b9aafcd4e90188ddb9e
-- 
2.30.2


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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v1 1/2] arm64/kpti: Move DAIF masking to C code
  2022-11-23 18:02 [PATCH v1 0/2] arm64/asm: Remove DAIF save/restore macros Mark Brown
@ 2022-11-23 18:02 ` Mark Brown
  2022-11-24 10:58   ` Mark Rutland
  2022-11-23 18:02 ` [PATCH v1 2/2] arm64/asm: Remove unused assembler DAIF save/restore macros Mark Brown
  2022-11-25 13:24 ` [PATCH v1 0/2] arm64/asm: Remove " Will Deacon
  2 siblings, 1 reply; 6+ messages in thread
From: Mark Brown @ 2022-11-23 18:02 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: Mark Rutland, Ard Biesheuvel, linux-arm-kernel, Mark Brown

We really don't want to take an exception while replacing TTBR1 so we mask
DAIF during the actual update. Currently this is done in the assembly
function idmap_cpu_replace_ttbr1() but it could equally be done in the only
caller of that function, cpu_replace_ttbr1(). This simplifies the assembly
code slightly and means that when working with the code around masking DAIF
flags there is one less piece of assembly code which needs to be considered.

While we're at it add a comment which makes explicit why we are masking
DAIF in this code.

There should be no functional effect.

Signed-off-by: Mark Brown <broonie@kernel.org>
---
 arch/arm64/include/asm/mmu_context.h | 10 ++++++++++
 arch/arm64/mm/proc.S                 |  4 ----
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/arch/arm64/include/asm/mmu_context.h b/arch/arm64/include/asm/mmu_context.h
index d3f8b5df0c1f..72dbd6400549 100644
--- a/arch/arm64/include/asm/mmu_context.h
+++ b/arch/arm64/include/asm/mmu_context.h
@@ -18,6 +18,7 @@
 
 #include <asm/cacheflush.h>
 #include <asm/cpufeature.h>
+#include <asm/daifflags.h>
 #include <asm/proc-fns.h>
 #include <asm-generic/mm_hooks.h>
 #include <asm/cputype.h>
@@ -152,6 +153,7 @@ static inline void cpu_replace_ttbr1(pgd_t *pgdp, pgd_t *idmap)
 	typedef void (ttbr_replace_func)(phys_addr_t);
 	extern ttbr_replace_func idmap_cpu_replace_ttbr1;
 	ttbr_replace_func *replace_phys;
+	unsigned long daif;
 
 	/* phys_to_ttbr() zeros lower 2 bits of ttbr with 52-bit PA */
 	phys_addr_t ttbr1 = phys_to_ttbr(virt_to_phys(pgdp));
@@ -171,7 +173,15 @@ static inline void cpu_replace_ttbr1(pgd_t *pgdp, pgd_t *idmap)
 	replace_phys = (void *)__pa_symbol(idmap_cpu_replace_ttbr1);
 
 	__cpu_install_idmap(idmap);
+
+	/*
+	 * We really don't want to take *any* exceptions while TTBR1 is
+	 * in the process of being replaced so mask everything.
+	 */
+	daif = local_daif_save();
 	replace_phys(ttbr1);
+	local_daif_restore(daif);
+
 	cpu_uninstall_idmap();
 }
 
diff --git a/arch/arm64/mm/proc.S b/arch/arm64/mm/proc.S
index b9ecbbae1e1a..066fa60b93d2 100644
--- a/arch/arm64/mm/proc.S
+++ b/arch/arm64/mm/proc.S
@@ -189,16 +189,12 @@ SYM_FUNC_END(cpu_do_resume)
  * called by anything else. It can only be executed from a TTBR0 mapping.
  */
 SYM_TYPED_FUNC_START(idmap_cpu_replace_ttbr1)
-	save_and_disable_daif flags=x2
-
 	__idmap_cpu_set_reserved_ttbr1 x1, x3
 
 	offset_ttbr1 x0, x3
 	msr	ttbr1_el1, x0
 	isb
 
-	restore_daif x2
-
 	ret
 SYM_FUNC_END(idmap_cpu_replace_ttbr1)
 	.popsection
-- 
2.30.2


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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v1 2/2] arm64/asm: Remove unused assembler DAIF save/restore macros
  2022-11-23 18:02 [PATCH v1 0/2] arm64/asm: Remove DAIF save/restore macros Mark Brown
  2022-11-23 18:02 ` [PATCH v1 1/2] arm64/kpti: Move DAIF masking to C code Mark Brown
@ 2022-11-23 18:02 ` Mark Brown
  2022-11-24 10:59   ` Mark Rutland
  2022-11-25 13:24 ` [PATCH v1 0/2] arm64/asm: Remove " Will Deacon
  2 siblings, 1 reply; 6+ messages in thread
From: Mark Brown @ 2022-11-23 18:02 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon
  Cc: Mark Rutland, Ard Biesheuvel, linux-arm-kernel, Mark Brown

There are no longer any users of the assembler macros for saving and
restoring DAIF so remove them to prevent further users being added, there
are C equivalents available.

Signed-off-by: Mark Brown <broonie@kernel.org>
---
 arch/arm64/include/asm/assembler.h | 9 ---------
 1 file changed, 9 deletions(-)

diff --git a/arch/arm64/include/asm/assembler.h b/arch/arm64/include/asm/assembler.h
index 88175551b401..d8d6627be0f6 100644
--- a/arch/arm64/include/asm/assembler.h
+++ b/arch/arm64/include/asm/assembler.h
@@ -34,11 +34,6 @@
 	wx\n	.req	w\n
 	.endr
 
-	.macro save_and_disable_daif, flags
-	mrs	\flags, daif
-	msr	daifset, #0xf
-	.endm
-
 	.macro disable_daif
 	msr	daifset, #0xf
 	.endm
@@ -47,10 +42,6 @@
 	msr	daifclr, #0xf
 	.endm
 
-	.macro	restore_daif, flags:req
-	msr	daif, \flags
-	.endm
-
 /*
  * Save/restore interrupts.
  */
-- 
2.30.2


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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v1 1/2] arm64/kpti: Move DAIF masking to C code
  2022-11-23 18:02 ` [PATCH v1 1/2] arm64/kpti: Move DAIF masking to C code Mark Brown
@ 2022-11-24 10:58   ` Mark Rutland
  0 siblings, 0 replies; 6+ messages in thread
From: Mark Rutland @ 2022-11-24 10:58 UTC (permalink / raw)
  To: Mark Brown; +Cc: Catalin Marinas, Will Deacon, Ard Biesheuvel, linux-arm-kernel

On Wed, Nov 23, 2022 at 06:02:08PM +0000, Mark Brown wrote:
> We really don't want to take an exception while replacing TTBR1 so we mask
> DAIF during the actual update. Currently this is done in the assembly
> function idmap_cpu_replace_ttbr1() but it could equally be done in the only
> caller of that function, cpu_replace_ttbr1(). This simplifies the assembly
> code slightly and means that when working with the code around masking DAIF
> flags there is one less piece of assembly code which needs to be considered.
> 
> While we're at it add a comment which makes explicit why we are masking
> DAIF in this code.
> 
> There should be no functional effect.
> 
> Signed-off-by: Mark Brown <broonie@kernel.org>

When using GIC priority masking this means we'll also poke the PMR, but other
than that this is identical, and getting rid of the asm variation will make it
easier to clean up the DAIF / PMR manipulation for (p)NMI support.

So this makes sense to me; I don't see a problem with the additional PMR poking
in that case (it's consistent with other DAIF masking in C code), and we'll
still correctly mask and restore all the relevant DAIF bits.

FWIW:

  Reviewed-by: Mark Rutland <mark.rutland@arm.com>

Mark.


> ---
>  arch/arm64/include/asm/mmu_context.h | 10 ++++++++++
>  arch/arm64/mm/proc.S                 |  4 ----
>  2 files changed, 10 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/arm64/include/asm/mmu_context.h b/arch/arm64/include/asm/mmu_context.h
> index d3f8b5df0c1f..72dbd6400549 100644
> --- a/arch/arm64/include/asm/mmu_context.h
> +++ b/arch/arm64/include/asm/mmu_context.h
> @@ -18,6 +18,7 @@
>  
>  #include <asm/cacheflush.h>
>  #include <asm/cpufeature.h>
> +#include <asm/daifflags.h>
>  #include <asm/proc-fns.h>
>  #include <asm-generic/mm_hooks.h>
>  #include <asm/cputype.h>
> @@ -152,6 +153,7 @@ static inline void cpu_replace_ttbr1(pgd_t *pgdp, pgd_t *idmap)
>  	typedef void (ttbr_replace_func)(phys_addr_t);
>  	extern ttbr_replace_func idmap_cpu_replace_ttbr1;
>  	ttbr_replace_func *replace_phys;
> +	unsigned long daif;
>  
>  	/* phys_to_ttbr() zeros lower 2 bits of ttbr with 52-bit PA */
>  	phys_addr_t ttbr1 = phys_to_ttbr(virt_to_phys(pgdp));
> @@ -171,7 +173,15 @@ static inline void cpu_replace_ttbr1(pgd_t *pgdp, pgd_t *idmap)
>  	replace_phys = (void *)__pa_symbol(idmap_cpu_replace_ttbr1);
>  
>  	__cpu_install_idmap(idmap);
> +
> +	/*
> +	 * We really don't want to take *any* exceptions while TTBR1 is
> +	 * in the process of being replaced so mask everything.
> +	 */
> +	daif = local_daif_save();
>  	replace_phys(ttbr1);
> +	local_daif_restore(daif);
> +
>  	cpu_uninstall_idmap();
>  }
>  
> diff --git a/arch/arm64/mm/proc.S b/arch/arm64/mm/proc.S
> index b9ecbbae1e1a..066fa60b93d2 100644
> --- a/arch/arm64/mm/proc.S
> +++ b/arch/arm64/mm/proc.S
> @@ -189,16 +189,12 @@ SYM_FUNC_END(cpu_do_resume)
>   * called by anything else. It can only be executed from a TTBR0 mapping.
>   */
>  SYM_TYPED_FUNC_START(idmap_cpu_replace_ttbr1)
> -	save_and_disable_daif flags=x2
> -
>  	__idmap_cpu_set_reserved_ttbr1 x1, x3
>  
>  	offset_ttbr1 x0, x3
>  	msr	ttbr1_el1, x0
>  	isb
>  
> -	restore_daif x2
> -
>  	ret
>  SYM_FUNC_END(idmap_cpu_replace_ttbr1)
>  	.popsection
> -- 
> 2.30.2
> 

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1 2/2] arm64/asm: Remove unused assembler DAIF save/restore macros
  2022-11-23 18:02 ` [PATCH v1 2/2] arm64/asm: Remove unused assembler DAIF save/restore macros Mark Brown
@ 2022-11-24 10:59   ` Mark Rutland
  0 siblings, 0 replies; 6+ messages in thread
From: Mark Rutland @ 2022-11-24 10:59 UTC (permalink / raw)
  To: Mark Brown; +Cc: Catalin Marinas, Will Deacon, Ard Biesheuvel, linux-arm-kernel

On Wed, Nov 23, 2022 at 06:02:09PM +0000, Mark Brown wrote:
> There are no longer any users of the assembler macros for saving and
> restoring DAIF so remove them to prevent further users being added, there
> are C equivalents available.
> 
> Signed-off-by: Mark Brown <broonie@kernel.org>

Getting rid of these will make it easier to clean up DAIF / PMR manipulation
generally, so FWIW:

  Acked-by: Mark Rutland <mark.rutland@arm.com>

Mark.

> ---
>  arch/arm64/include/asm/assembler.h | 9 ---------
>  1 file changed, 9 deletions(-)
> 
> diff --git a/arch/arm64/include/asm/assembler.h b/arch/arm64/include/asm/assembler.h
> index 88175551b401..d8d6627be0f6 100644
> --- a/arch/arm64/include/asm/assembler.h
> +++ b/arch/arm64/include/asm/assembler.h
> @@ -34,11 +34,6 @@
>  	wx\n	.req	w\n
>  	.endr
>  
> -	.macro save_and_disable_daif, flags
> -	mrs	\flags, daif
> -	msr	daifset, #0xf
> -	.endm
> -
>  	.macro disable_daif
>  	msr	daifset, #0xf
>  	.endm
> @@ -47,10 +42,6 @@
>  	msr	daifclr, #0xf
>  	.endm
>  
> -	.macro	restore_daif, flags:req
> -	msr	daif, \flags
> -	.endm
> -
>  /*
>   * Save/restore interrupts.
>   */
> -- 
> 2.30.2
> 

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1 0/2] arm64/asm: Remove DAIF save/restore macros
  2022-11-23 18:02 [PATCH v1 0/2] arm64/asm: Remove DAIF save/restore macros Mark Brown
  2022-11-23 18:02 ` [PATCH v1 1/2] arm64/kpti: Move DAIF masking to C code Mark Brown
  2022-11-23 18:02 ` [PATCH v1 2/2] arm64/asm: Remove unused assembler DAIF save/restore macros Mark Brown
@ 2022-11-25 13:24 ` Will Deacon
  2 siblings, 0 replies; 6+ messages in thread
From: Will Deacon @ 2022-11-25 13:24 UTC (permalink / raw)
  To: Catalin Marinas, Mark Brown
  Cc: kernel-team, Will Deacon, Mark Rutland, linux-arm-kernel,
	Ard Biesheuvel

On Wed, 23 Nov 2022 18:02:07 +0000, Mark Brown wrote:
> As part of an effort to improve the maintainability of the code for
> manipulating DAIF let's reduce the number of places where it is
> implemented by lifting the masking done during TTBR1 replacement for
> KPTI up into C code and removing the assembler macros it was using.
> 
> Due to a textual collision with the removal of enable_da this is based
> on for-next/trivial in the arm64 tree.
> 
> [...]

Applied to arm64 (for-next/trivial), thanks!

[1/2] arm64/kpti: Move DAIF masking to C code
      https://git.kernel.org/arm64/c/a8bf2fc43fc6
[2/2] arm64/asm: Remove unused assembler DAIF save/restore macros
      https://git.kernel.org/arm64/c/d503d01e5016

Cheers,
-- 
Will

https://fixes.arm64.dev
https://next.arm64.dev
https://will.arm64.dev

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-11-25 13:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-11-23 18:02 [PATCH v1 0/2] arm64/asm: Remove DAIF save/restore macros Mark Brown
2022-11-23 18:02 ` [PATCH v1 1/2] arm64/kpti: Move DAIF masking to C code Mark Brown
2022-11-24 10:58   ` Mark Rutland
2022-11-23 18:02 ` [PATCH v1 2/2] arm64/asm: Remove unused assembler DAIF save/restore macros Mark Brown
2022-11-24 10:59   ` Mark Rutland
2022-11-25 13:24 ` [PATCH v1 0/2] arm64/asm: Remove " Will Deacon

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).