All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] powerpc/security: Use a mutex for interrupt exit code patching
@ 2021-10-27  7:24 Russell Currey
  2021-10-28  3:03 ` Nicholas Piggin
  2021-11-02 10:12 ` Michael Ellerman
  0 siblings, 2 replies; 3+ messages in thread
From: Russell Currey @ 2021-10-27  7:24 UTC (permalink / raw)
  To: linuxppc-dev; +Cc: npiggin, Russell Currey

The mitigation-patching.sh script in the powerpc selftests toggles
all mitigations on and off simultaneously, revealing that rfi_flush
and stf_barrier cannot safely operate at the same time due to races
in updating the static key.

On some systems, the static key code throws a warning and the kernel
remains functional.  On others, the kernel will hang or crash.

Fix this by slapping on a mutex.

Fixes: 13799748b957 ("powerpc/64: use interrupt restart table to speed up return from interrupt")
Signed-off-by: Russell Currey <ruscur@russell.cc>
---
 arch/powerpc/lib/feature-fixups.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/arch/powerpc/lib/feature-fixups.c b/arch/powerpc/lib/feature-fixups.c
index cda17bee5afe..9956277fbd88 100644
--- a/arch/powerpc/lib/feature-fixups.c
+++ b/arch/powerpc/lib/feature-fixups.c
@@ -228,6 +228,7 @@ static void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
 
 static bool stf_exit_reentrant = false;
 static bool rfi_exit_reentrant = false;
+static DEFINE_MUTEX(exit_flush_lock);
 
 static int __do_stf_barrier_fixups(void *data)
 {
@@ -253,6 +254,9 @@ void do_stf_barrier_fixups(enum stf_barrier_type types)
 	 * low level interrupt exit code before patching. After the patching,
 	 * if allowed, then flip the branch to allow fast exits.
 	 */
+
+	// Prevent static key update races with do_rfi_flush_fixups()
+	mutex_lock(&exit_flush_lock);
 	static_branch_enable(&interrupt_exit_not_reentrant);
 
 	stop_machine(__do_stf_barrier_fixups, &types, NULL);
@@ -264,6 +268,7 @@ void do_stf_barrier_fixups(enum stf_barrier_type types)
 
 	if (stf_exit_reentrant && rfi_exit_reentrant)
 		static_branch_disable(&interrupt_exit_not_reentrant);
+	mutex_unlock(&exit_flush_lock);
 }
 
 void do_uaccess_flush_fixups(enum l1d_flush_type types)
@@ -486,6 +491,9 @@ void do_rfi_flush_fixups(enum l1d_flush_type types)
 	 * without stop_machine, so this could be achieved with a broadcast
 	 * IPI instead, but this matches the stf sequence.
 	 */
+
+	// Prevent static key update races with do_stf_barrier_fixups()
+	mutex_lock(&exit_flush_lock);
 	static_branch_enable(&interrupt_exit_not_reentrant);
 
 	stop_machine(__do_rfi_flush_fixups, &types, NULL);
@@ -497,6 +505,7 @@ void do_rfi_flush_fixups(enum l1d_flush_type types)
 
 	if (stf_exit_reentrant && rfi_exit_reentrant)
 		static_branch_disable(&interrupt_exit_not_reentrant);
+	mutex_unlock(&exit_flush_lock);
 }
 
 void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
-- 
2.33.1


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

* Re: [PATCH] powerpc/security: Use a mutex for interrupt exit code patching
  2021-10-27  7:24 [PATCH] powerpc/security: Use a mutex for interrupt exit code patching Russell Currey
@ 2021-10-28  3:03 ` Nicholas Piggin
  2021-11-02 10:12 ` Michael Ellerman
  1 sibling, 0 replies; 3+ messages in thread
From: Nicholas Piggin @ 2021-10-28  3:03 UTC (permalink / raw)
  To: linuxppc-dev, Russell Currey

Excerpts from Russell Currey's message of October 27, 2021 5:24 pm:
> The mitigation-patching.sh script in the powerpc selftests toggles
> all mitigations on and off simultaneously, revealing that rfi_flush
> and stf_barrier cannot safely operate at the same time due to races
> in updating the static key.
> 
> On some systems, the static key code throws a warning and the kernel
> remains functional.  On others, the kernel will hang or crash.
> 
> Fix this by slapping on a mutex.
> 
> Fixes: 13799748b957 ("powerpc/64: use interrupt restart table to speed up return from interrupt")
> Signed-off-by: Russell Currey <ruscur@russell.cc>

Doh,

Acked-by: Nicholas Piggin <npiggin@gmail.com>

> ---
>  arch/powerpc/lib/feature-fixups.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/arch/powerpc/lib/feature-fixups.c b/arch/powerpc/lib/feature-fixups.c
> index cda17bee5afe..9956277fbd88 100644
> --- a/arch/powerpc/lib/feature-fixups.c
> +++ b/arch/powerpc/lib/feature-fixups.c
> @@ -228,6 +228,7 @@ static void do_stf_exit_barrier_fixups(enum stf_barrier_type types)
>  
>  static bool stf_exit_reentrant = false;
>  static bool rfi_exit_reentrant = false;
> +static DEFINE_MUTEX(exit_flush_lock);
>  
>  static int __do_stf_barrier_fixups(void *data)
>  {
> @@ -253,6 +254,9 @@ void do_stf_barrier_fixups(enum stf_barrier_type types)
>  	 * low level interrupt exit code before patching. After the patching,
>  	 * if allowed, then flip the branch to allow fast exits.
>  	 */
> +
> +	// Prevent static key update races with do_rfi_flush_fixups()
> +	mutex_lock(&exit_flush_lock);
>  	static_branch_enable(&interrupt_exit_not_reentrant);
>  
>  	stop_machine(__do_stf_barrier_fixups, &types, NULL);
> @@ -264,6 +268,7 @@ void do_stf_barrier_fixups(enum stf_barrier_type types)
>  
>  	if (stf_exit_reentrant && rfi_exit_reentrant)
>  		static_branch_disable(&interrupt_exit_not_reentrant);
> +	mutex_unlock(&exit_flush_lock);
>  }
>  
>  void do_uaccess_flush_fixups(enum l1d_flush_type types)
> @@ -486,6 +491,9 @@ void do_rfi_flush_fixups(enum l1d_flush_type types)
>  	 * without stop_machine, so this could be achieved with a broadcast
>  	 * IPI instead, but this matches the stf sequence.
>  	 */
> +
> +	// Prevent static key update races with do_stf_barrier_fixups()
> +	mutex_lock(&exit_flush_lock);
>  	static_branch_enable(&interrupt_exit_not_reentrant);
>  
>  	stop_machine(__do_rfi_flush_fixups, &types, NULL);
> @@ -497,6 +505,7 @@ void do_rfi_flush_fixups(enum l1d_flush_type types)
>  
>  	if (stf_exit_reentrant && rfi_exit_reentrant)
>  		static_branch_disable(&interrupt_exit_not_reentrant);
> +	mutex_unlock(&exit_flush_lock);
>  }
>  
>  void do_barrier_nospec_fixups_range(bool enable, void *fixup_start, void *fixup_end)
> -- 
> 2.33.1
> 
> 

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

* Re: [PATCH] powerpc/security: Use a mutex for interrupt exit code patching
  2021-10-27  7:24 [PATCH] powerpc/security: Use a mutex for interrupt exit code patching Russell Currey
  2021-10-28  3:03 ` Nicholas Piggin
@ 2021-11-02 10:12 ` Michael Ellerman
  1 sibling, 0 replies; 3+ messages in thread
From: Michael Ellerman @ 2021-11-02 10:12 UTC (permalink / raw)
  To: Russell Currey, linuxppc-dev; +Cc: npiggin

On Wed, 27 Oct 2021 17:24:10 +1000, Russell Currey wrote:
> The mitigation-patching.sh script in the powerpc selftests toggles
> all mitigations on and off simultaneously, revealing that rfi_flush
> and stf_barrier cannot safely operate at the same time due to races
> in updating the static key.
> 
> On some systems, the static key code throws a warning and the kernel
> remains functional.  On others, the kernel will hang or crash.
> 
> [...]

Applied to powerpc/next.

[1/1] powerpc/security: Use a mutex for interrupt exit code patching
      https://git.kernel.org/powerpc/c/3c12b4df8d5e026345a19886ae375b3ebc33c0b6

cheers

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

end of thread, other threads:[~2021-11-02 11:51 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-27  7:24 [PATCH] powerpc/security: Use a mutex for interrupt exit code patching Russell Currey
2021-10-28  3:03 ` Nicholas Piggin
2021-11-02 10:12 ` Michael Ellerman

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.