From nobody Sat Sep 6 03:35:54 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1754679710; cv=none; d=zohomail.com; s=zohoarc; b=JJGNZANu/m+hVVSiJGCRnQAbhvBM/3clUtNoczQg/NqrUjAIi3VNZaMzCwVaHaUhPOplC2jTeOIiYdw6/F+6UyhkOCYHbZMgXOYT0YCaT/llIUBDItEY9cu7KsLXwmhzXJkxjuDjZkZLv8wXyKRpHcTK5IpQLYkM9mEHQHUOWCQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1754679710; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=sKBBDyOhm3KB0EsnURIQG+Cs3th/O37nyJmS8J3TRyU=; b=fM8YqKp/ntBWA8inw9z9zCTADlKPpmZZ3Nal7KFUsi3y3Lytr5rA2HKoIj4iRcT3oRjmSDwe0qvVuulD3pRxEyccrpXwz2EMvM69srYq8lUYZWPWMvKvXYk1oxQlcL6zyEjSL7JcTYaPJmlYidcH/HV5KnxcsCiEOKZomjPhpRY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1754679710735324.5031098548583; Fri, 8 Aug 2025 12:01:50 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ukSJD-0006Cf-9z; Fri, 08 Aug 2025 14:59:19 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ukSJA-0006C6-Ub for qemu-devel@nongnu.org; Fri, 08 Aug 2025 14:59:17 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ukSJ9-0006fX-6T for qemu-devel@nongnu.org; Fri, 08 Aug 2025 14:59:16 -0400 Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-37-2TJqDGNgMoixtesoVMjI9A-1; Fri, 08 Aug 2025 14:59:13 -0400 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-3b793f76a46so1652398f8f.2 for ; Fri, 08 Aug 2025 11:59:13 -0700 (PDT) Received: from [192.168.10.48] ([151.49.234.144]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3b8e054036bsm21594609f8f.31.2025.08.08.11.59.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Aug 2025 11:59:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1754679554; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sKBBDyOhm3KB0EsnURIQG+Cs3th/O37nyJmS8J3TRyU=; b=Ky89HJTz4RxlneOpJoKMztUK+MdLkmzJvocPPu0v03ig2UvfpCVndRBqQOums76cG/GU8v BlB7S7eZPj6enKbONrxLUQkWCcppaqNUbrZiO3pQqKKhS5fFixccRTHNUbINEzHobIS617 3EQRh5wczY5JvIzgJo+CDDxScuKCgGc= X-MC-Unique: 2TJqDGNgMoixtesoVMjI9A-1 X-Mimecast-MFC-AGG-ID: 2TJqDGNgMoixtesoVMjI9A_1754679552 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754679551; x=1755284351; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sKBBDyOhm3KB0EsnURIQG+Cs3th/O37nyJmS8J3TRyU=; b=jM+ZvOGqH11zhZGnE7LFc9r1DuOjBRG/1sOuSkaEOCYys8LqzXmZQMakGITAsWR8MG 8CH8Oh6E6THWy1eaolLkbz8Sd5ji/4CZkyW82oJszuu3JLL++l+AWaEKdjEFoLqpl6S5 JDgIkWv6UB3pA5m6tiWTY12u/1JldsCu+8R95Q8AdSd/NNaa+nQS62IcXjG4fUMouya5 2/P1mW1UJc7gXJQSBnH6Y1bo3OE4zkFU8/oriyimER+33vcEx0jlK4y0qziFWe6azzml IC2mrtsdo7HPfjQ/FzfAvlIOqP97ZKAduA9RApAfNNKhfOigp4cMlpUTm/ILI1KM/Ajk jp/A== X-Gm-Message-State: AOJu0YwPXLfYfg4DyRKTyKNKOEMmm5TKRC0bOesH5Q+ID893wc4WSS3u l3xprxlbRgJnsMAdNSjgu/s3FAbxFbwIPIi9n69B3aQADEdH5vRrbR88cc7pF5htSm2gp4zriLv 8XGy39eZTWnZfuvKMhscKpIrQZSeYz4d+Kw8XguV4TWIU1cP9rDjRl7A3MVXp98MUpMqAyGQldX aT+/uMyUq07Bz5urdFz0cIW+UDV+2JomFfZnCHgWkL X-Gm-Gg: ASbGnctcE3iBm3tNoK48jdRjhS4xg+wCg8nhZUnBKNJJrv6VsU6s9bzoJaFPdyg6JSI /lnRw8z+T0qJe1jQhouXAWaRnvLBKDoq0DReXYl49jkeF2icJEZMIaGMjARyB06bjtBrS78+VuA 36pWUVAPhMfRNudf4X95VBcOwJCGf4vkqXUl6lYDzqtqu9D5IBCFqqlTdhP0kYh8lM16VV3xBTJ TW6Y95S1uZf9y005hwmpkdLFjEvSdpapU7z6OV1TvmikiubQEfKbTJiI5tbwN0M3mnPh5asNMD1 6dfcfGMD8JKBbUMlYOf2ANzPWf4GIhxABrUNoKQJqLmU X-Received: by 2002:a05:6000:4382:b0:3b8:f863:672c with SMTP id ffacd0b85a97d-3b900b7af6amr3729168f8f.33.1754679550822; Fri, 08 Aug 2025 11:59:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEF7NUjpOHQx3OSeNP9ENS9vtmkk/K7diUejXGwPcUJxdwJK5fbuDPzGf2XY2kVDBe5teeaeA== X-Received: by 2002:a05:6000:4382:b0:3b8:f863:672c with SMTP id ffacd0b85a97d-3b900b7af6amr3729143f8f.33.1754679550205; Fri, 08 Aug 2025 11:59:10 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: imammedo@redhat.com, richard.henderson@linaro.org, peterx@redhat.com Subject: [PATCH 1/8] accel: use store_release/load_acquire for cross-thread exit_request Date: Fri, 8 Aug 2025 20:58:58 +0200 Message-ID: <20250808185905.62776-2-pbonzini@redhat.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250808185905.62776-1-pbonzini@redhat.com> References: <20250808185905.62776-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.129.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1754679711643116600 Content-Type: text/plain; charset="utf-8" cpu->exit_request do not use a load-acquire/store-release pair right now, but this means that cpu_exit() does not store it after any flags that are read in the slow path. Probably everything is protected one way or the other by the BQL, because after reading cpu->exit_request the CPU thread often goes to sleep (by waiting on the BQL-protected cpu->halt_cond), but it's not clear. Use load-acquire/store-release consistently. Signed-off-by: Paolo Bonzini Reviewed-by: Peter Xu Reviewed-by: Richard Henderson --- accel/kvm/kvm-all.c | 19 +++++++++---------- accel/tcg/cpu-exec.c | 7 +++++-- accel/tcg/tcg-accel-ops-rr.c | 2 +- hw/core/cpu-common.c | 3 ++- target/i386/nvmm/nvmm-all.c | 5 ++--- target/i386/whpx/whpx-all.c | 3 ++- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/accel/kvm/kvm-all.c b/accel/kvm/kvm-all.c index 890d5ea9f86..57e35960125 100644 --- a/accel/kvm/kvm-all.c +++ b/accel/kvm/kvm-all.c @@ -3029,10 +3029,6 @@ static void kvm_eat_signals(CPUState *cpu) =20 if (kvm_immediate_exit) { qatomic_set(&cpu->kvm_run->immediate_exit, 0); - /* Write kvm_run->immediate_exit before the cpu->exit_request - * write in kvm_cpu_exec. - */ - smp_wmb(); return; } =20 @@ -3187,7 +3183,8 @@ int kvm_cpu_exec(CPUState *cpu) } =20 kvm_arch_pre_run(cpu, run); - if (qatomic_read(&cpu->exit_request)) { + /* Corresponding store-release is in cpu_exit. */ + if (qatomic_load_acquire(&cpu->exit_request)) { trace_kvm_interrupt_exit_request(); /* * KVM requires us to reenter the kernel after IO exits to com= plete @@ -3197,13 +3194,15 @@ int kvm_cpu_exec(CPUState *cpu) kvm_cpu_kick_self(); } =20 - /* Read cpu->exit_request before KVM_RUN reads run->immediate_exit. - * Matching barrier in kvm_eat_signals. - */ - smp_rmb(); - run_ret =3D kvm_vcpu_ioctl(cpu, KVM_RUN, 0); =20 + /* + * After writing cpu->exit_request, cpu_exit() sends a signal that= writes + * kvm->run->immediate_exit. The signal is always happening after= the + * write to cpu->exit_request so, if KVM read kvm->run->immediate_= exit + * as true, cpu->exit_request will always read as true. + */ + attrs =3D kvm_arch_post_run(cpu, run); =20 #ifdef KVM_HAVE_MCE_INJECTION diff --git a/accel/tcg/cpu-exec.c b/accel/tcg/cpu-exec.c index 82867f456c1..4bd9ee01c19 100644 --- a/accel/tcg/cpu-exec.c +++ b/accel/tcg/cpu-exec.c @@ -849,8 +849,11 @@ static inline bool cpu_handle_interrupt(CPUState *cpu, bql_unlock(); } =20 - /* Finally, check if we need to exit to the main loop. */ - if (unlikely(qatomic_read(&cpu->exit_request)) || icount_exit_request(= cpu)) { + /* + * Finally, check if we need to exit to the main loop. + * The corresponding store-release is in cpu_exit. + */ + if (unlikely(qatomic_load_acquire(&cpu->exit_request)) || icount_exit_= request(cpu)) { qatomic_set(&cpu->exit_request, 0); if (cpu->exception_index =3D=3D -1) { cpu->exception_index =3D EXCP_INTERRUPT; diff --git a/accel/tcg/tcg-accel-ops-rr.c b/accel/tcg/tcg-accel-ops-rr.c index 6eec5c9eee9..1e551e92d6d 100644 --- a/accel/tcg/tcg-accel-ops-rr.c +++ b/accel/tcg/tcg-accel-ops-rr.c @@ -242,7 +242,7 @@ static void *rr_cpu_thread_fn(void *arg) cpu =3D first_cpu; } =20 - while (cpu && cpu_work_list_empty(cpu) && !cpu->exit_request) { + while (cpu && cpu_work_list_empty(cpu) && !qatomic_load_acquire(&c= pu->exit_request)) { /* Store rr_current_cpu before evaluating cpu_can_run(). */ qatomic_set_mb(&rr_current_cpu, cpu); =20 diff --git a/hw/core/cpu-common.c b/hw/core/cpu-common.c index 39e674aca21..f189ce861c9 100644 --- a/hw/core/cpu-common.c +++ b/hw/core/cpu-common.c @@ -84,7 +84,8 @@ void cpu_reset_interrupt(CPUState *cpu, int mask) =20 void cpu_exit(CPUState *cpu) { - qatomic_set(&cpu->exit_request, 1); + /* Ensure cpu_exec will see the reason why the exit request was set. = */ + qatomic_store_release(&cpu->exit_request, 1); /* Ensure cpu_exec will see the exit request after TCG has exited. */ smp_wmb(); qatomic_set(&cpu->neg.icount_decr.u16.high, -1); diff --git a/target/i386/nvmm/nvmm-all.c b/target/i386/nvmm/nvmm-all.c index c1ac74c4f04..a5e3485c1f8 100644 --- a/target/i386/nvmm/nvmm-all.c +++ b/target/i386/nvmm/nvmm-all.c @@ -743,7 +743,8 @@ nvmm_vcpu_loop(CPUState *cpu) =20 nvmm_vcpu_pre_run(cpu); =20 - if (qatomic_read(&cpu->exit_request)) { + /* Corresponding store-release is in cpu_exit. */ + if (qatomic_load_acquire(&cpu->exit_request)) { #if NVMM_USER_VERSION >=3D 2 nvmm_vcpu_stop(vcpu); #else @@ -751,8 +752,6 @@ nvmm_vcpu_loop(CPUState *cpu) #endif } =20 - /* Read exit_request before the kernel reads the immediate exit fl= ag */ - smp_rmb(); ret =3D nvmm_vcpu_run(mach, vcpu); if (ret =3D=3D -1) { error_report("NVMM: Failed to exec a virtual processor," diff --git a/target/i386/whpx/whpx-all.c b/target/i386/whpx/whpx-all.c index 878cdd1668c..9f88e368d4d 100644 --- a/target/i386/whpx/whpx-all.c +++ b/target/i386/whpx/whpx-all.c @@ -1714,7 +1714,8 @@ static int whpx_vcpu_run(CPUState *cpu) if (exclusive_step_mode =3D=3D WHPX_STEP_NONE) { whpx_vcpu_pre_run(cpu); =20 - if (qatomic_read(&cpu->exit_request)) { + /* Corresponding store-release is in cpu_exit. */ + if (qatomic_load_acquire(&cpu->exit_request)) { whpx_vcpu_kick(cpu); } } --=20 2.50.1