[PATCH 07/18] cpu-common: use atomic access for interrupt_request

Paolo Bonzini posted 18 patches 1 week ago
Maintainers: Richard Henderson <richard.henderson@linaro.org>, Paolo Bonzini <pbonzini@redhat.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, Cameron Esfahani <dirty@apple.com>, Roman Bolshakov <rbolshakov@ddn.com>, Phil Dennis-Jordan <phil@philjordan.eu>, Mads Ynddal <mads@ynddal.dk>, Riku Voipio <riku.voipio@iki.fi>, Warner Losh <imp@bsdimp.com>, Kyle Evans <kevans@freebsd.org>, Eduardo Habkost <eduardo@habkost.net>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>, Yanan Wang <wangyanan55@huawei.com>, Zhao Liu <zhao1.liu@intel.com>, Nicholas Piggin <npiggin@gmail.com>, Chinmay Rath <rathc@linux.ibm.com>, Harsh Prateek Bora <harshpb@linux.ibm.com>, Laurent Vivier <laurent@vivier.eu>, Brian Cain <brian.cain@oss.qualcomm.com>, "Alex Bennée" <alex.bennee@linaro.org>, Peter Maydell <peter.maydell@linaro.org>, Michael Rolnik <mrolnik@gmail.com>, Marcelo Tosatti <mtosatti@redhat.com>, Reinoud Zandijk <reinoud@netbsd.org>, Sunil Muthuswamy <sunilmut@microsoft.com>, Stafford Horne <shorne@gmail.com>, Yoshinori Sato <yoshinori.sato@nifty.com>, David Hildenbrand <david@redhat.com>, Ilya Leoshkevich <iii@linux.ibm.com>, Thomas Huth <thuth@redhat.com>, Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>, Artyom Tarasenko <atar4qemu@gmail.com>
[PATCH 07/18] cpu-common: use atomic access for interrupt_request
Posted by Paolo Bonzini 1 week ago
Writes to interrupt_request used non-atomic accesses, but there are a
few cases where the access was not protected by the BQL.  Now that
there is a full set of helpers, it's easier to guarantee that
interrupt_request accesses are fully atomic, so just drop the
requirement instead of fixing them.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/hw/core/cpu.h |  1 -
 hw/core/cpu-common.c  | 12 +-----------
 system/cpus.c         |  3 +--
 3 files changed, 2 insertions(+), 14 deletions(-)

diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
index b01a0cffd64..23bd02277f4 100644
--- a/include/hw/core/cpu.h
+++ b/include/hw/core/cpu.h
@@ -495,7 +495,6 @@ struct CPUState {
     bool exit_request;
     int exclusive_context_count;
     uint32_t cflags_next_tb;
-    /* updates protected by BQL */
     uint32_t interrupt_request;
     int singlestep_enabled;
     int64_t icount_budget;
diff --git a/hw/core/cpu-common.c b/hw/core/cpu-common.c
index 39e674aca21..9ea1f3764a8 100644
--- a/hw/core/cpu-common.c
+++ b/hw/core/cpu-common.c
@@ -67,19 +67,9 @@ CPUState *cpu_create(const char *typename)
     return cpu;
 }
 
-/* Resetting the IRQ comes from across the code base so we take the
- * BQL here if we need to.  cpu_interrupt assumes it is held.*/
 void cpu_reset_interrupt(CPUState *cpu, int mask)
 {
-    bool need_lock = !bql_locked();
-
-    if (need_lock) {
-        bql_lock();
-    }
-    cpu->interrupt_request &= ~mask;
-    if (need_lock) {
-        bql_unlock();
-    }
+    qatomic_and(&cpu->interrupt_request, ~mask);
 }
 
 void cpu_exit(CPUState *cpu)
diff --git a/system/cpus.c b/system/cpus.c
index 437848b5eb4..9bfbe2b0607 100644
--- a/system/cpus.c
+++ b/system/cpus.c
@@ -257,8 +257,7 @@ int64_t cpus_get_elapsed_ticks(void)
 void cpu_set_interrupt(CPUState *cpu, int mask)
 {
     /* Pairs with cpu_test_interrupt(). */
-    qatomic_store_release(&cpu->interrupt_request,
-        cpu->interrupt_request | mask);
+    qatomic_or(&cpu->interrupt_request, mask);
 }
 
 void generic_handle_interrupt(CPUState *cpu, int mask)
-- 
2.51.0
Re: [PATCH 07/18] cpu-common: use atomic access for interrupt_request
Posted by Igor Mammedov 4 days, 14 hours ago
On Fri, 29 Aug 2025 17:31:04 +0200
Paolo Bonzini <pbonzini@redhat.com> wrote:

> Writes to interrupt_request used non-atomic accesses, but there are a
> few cases where the access was not protected by the BQL.  Now that
> there is a full set of helpers, it's easier to guarantee that
> interrupt_request accesses are fully atomic, so just drop the
> requirement instead of fixing them.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Reviewed-by: Igor Mammedov <imammedo@redhat.com>

> ---
>  include/hw/core/cpu.h |  1 -
>  hw/core/cpu-common.c  | 12 +-----------
>  system/cpus.c         |  3 +--
>  3 files changed, 2 insertions(+), 14 deletions(-)
> 
> diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
> index b01a0cffd64..23bd02277f4 100644
> --- a/include/hw/core/cpu.h
> +++ b/include/hw/core/cpu.h
> @@ -495,7 +495,6 @@ struct CPUState {
>      bool exit_request;
>      int exclusive_context_count;
>      uint32_t cflags_next_tb;
> -    /* updates protected by BQL */
>      uint32_t interrupt_request;
>      int singlestep_enabled;
>      int64_t icount_budget;
> diff --git a/hw/core/cpu-common.c b/hw/core/cpu-common.c
> index 39e674aca21..9ea1f3764a8 100644
> --- a/hw/core/cpu-common.c
> +++ b/hw/core/cpu-common.c
> @@ -67,19 +67,9 @@ CPUState *cpu_create(const char *typename)
>      return cpu;
>  }
>  
> -/* Resetting the IRQ comes from across the code base so we take the
> - * BQL here if we need to.  cpu_interrupt assumes it is held.*/
>  void cpu_reset_interrupt(CPUState *cpu, int mask)
>  {
> -    bool need_lock = !bql_locked();
> -
> -    if (need_lock) {
> -        bql_lock();
> -    }
> -    cpu->interrupt_request &= ~mask;
> -    if (need_lock) {
> -        bql_unlock();
> -    }
> +    qatomic_and(&cpu->interrupt_request, ~mask);
>  }
>  
>  void cpu_exit(CPUState *cpu)
> diff --git a/system/cpus.c b/system/cpus.c
> index 437848b5eb4..9bfbe2b0607 100644
> --- a/system/cpus.c
> +++ b/system/cpus.c
> @@ -257,8 +257,7 @@ int64_t cpus_get_elapsed_ticks(void)
>  void cpu_set_interrupt(CPUState *cpu, int mask)
>  {
>      /* Pairs with cpu_test_interrupt(). */
> -    qatomic_store_release(&cpu->interrupt_request,
> -        cpu->interrupt_request | mask);
> +    qatomic_or(&cpu->interrupt_request, mask);
>  }
>  
>  void generic_handle_interrupt(CPUState *cpu, int mask)
Re: [PATCH 07/18] cpu-common: use atomic access for interrupt_request
Posted by Philippe Mathieu-Daudé 4 days, 19 hours ago
On 29/8/25 17:31, Paolo Bonzini wrote:
> Writes to interrupt_request used non-atomic accesses, but there are a
> few cases where the access was not protected by the BQL.  Now that
> there is a full set of helpers, it's easier to guarantee that
> interrupt_request accesses are fully atomic, so just drop the
> requirement instead of fixing them.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>   include/hw/core/cpu.h |  1 -
>   hw/core/cpu-common.c  | 12 +-----------
>   system/cpus.c         |  3 +--
>   3 files changed, 2 insertions(+), 14 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>


Re: [PATCH 07/18] cpu-common: use atomic access for interrupt_request
Posted by Richard Henderson 1 week ago
On 8/30/25 01:31, Paolo Bonzini wrote:
> Writes to interrupt_request used non-atomic accesses, but there are a
> few cases where the access was not protected by the BQL.  Now that
> there is a full set of helpers, it's easier to guarantee that
> interrupt_request accesses are fully atomic, so just drop the
> requirement instead of fixing them.
> 
> Signed-off-by: Paolo Bonzini<pbonzini@redhat.com>
> ---
>   include/hw/core/cpu.h |  1 -
>   hw/core/cpu-common.c  | 12 +-----------
>   system/cpus.c         |  3 +--
>   3 files changed, 2 insertions(+), 14 deletions(-)

I guess we didn't need SEQ_CST, but since we don't choose to play with relaxed atomics 
elsewhere, it would simply complicate things to start.

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~