Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9A028606A322 for ; Fri, 4 Dec 2026 07:15:46 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.1513750.1705216 (Exim 4.92) (envelope-from ) id 1xZNW3-00018L-TJ; Fri, 04 Dec 2026 07:15:35 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1513750.1705216; Fri, 04 Dec 2026 07:15:35 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xZNW3-00018E-Pq; Fri, 04 Dec 2026 07:15:35 +0000 Received: by outflank-mailman (input) for mailman id 1513750; Fri, 04 Dec 2026 07:15:35 +0000 Received: from mx.expurgate.net ([194.145.224.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1xZNW2-00017n-Vt for xen-devel@lists.xenproject.org; Fri, 04 Dec 2026 07:15:34 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1xZNW2-005usv-5Q for xen-devel@lists.xenproject.org; Fri, 04 Dec 2026 08:15:34 +0100 Received: from [10.42.69.11] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6b126892-e002-0a2a0a5209dd-0a2a450bd882-20 for ; Fri, 04 Dec 2026 08:15:34 +0100 Received: from [195.135.223.130] (helo=smtp-out1.suse.de) by tlsNG-42698a.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6b126895-b7e8-0a2a450b0019-c387df82bde0-3 for ; Fri, 04 Dec 2026 08:15:34 +0100 Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id A6280373EF; Fri, 4 Dec 2026 07:15:33 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 3B4361352B; Fri, 4 Dec 2026 07:15:33 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id Sh5BNZRoEmuOCwAAD6G6ig (envelope-from ); Fri, 04 Dec 2026 07:15:33 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Post: List-Help: List-Subscribe: , Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; none Authentication-Results: smtp-out1.suse.de; none From: Juergen Gross To: xen-devel@lists.xenproject.org Cc: Juergen Gross , Andrew Cooper , Anthony PERARD , Michal Orzel , Jan Beulich , Julien Grall , =?UTF-8?q?Roger=20Pau=20Monn=C3=A9?= , Stefano Stabellini Subject: [PATCH v3 1/2] xen/domain: fix domain_pause() and domain_unpause() Date: Fri, 4 Dec 2026 08:15:22 +0100 Message-ID: <20261204071523.369318-2-jgross@suse.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261204071523.369318-1-jgross@suse.com> References: <20261204071523.369318-1-jgross@suse.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Queue-Id: A6280373EF X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Action: no action X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Spam-Level: X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[] X-Spam-Score: -4.00 X-purgate-ID: tlsNG-42698a/1796368534-AB2DC9EA-4287603F/0/0 X-purgate-type: clean X-purgate-size: 3816 X-Spam-Status: No, score=-17.0 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS,UNPARSEABLE_RELAY, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 Calls of domain_pause() can be nested. So there should be no action other than handling the paused counter if a nested call is detected. Especially calling arch_domain_unpause() unconditionally in domain_unpause() is wrong, but the arch_domain_pause() call in domain_pause() can be skipped, too, if the domain was paused already. This requires to drop the scheme of the pause counter being modified just via atomic updates, as the calling of the arch_domain_[un]pause() functions needs to be guarded against races, too. Introduce a pause_lock instead, which is being held across the update of the pause counter AND the potential call of arch_domain_[un]pause(). While there might still be races with concurrent pause/unpause calls, the vcpu_sleep() and vcpu_wake() calls can happen outside the lock, as especially vcpu_wake() is looking at the pause count, too. Note that a race of domain_pause() seeing an initial count of 0 and domain_unpause() concurrently setting it back to 0 can't happen, as the domain_unpause() can be valid only, if the initial count was 1 already. This means, that there is no risk of the count being 0 at the end AND vcpus having been put to sleep by an unpause action. Fixes: f6a07643e1cc ("x86/viridian: freeze time reference counter when domain is paused") Signed-off-by: Juergen Gross --- V3: - switch to lock --- xen/common/domain.c | 30 ++++++++++++++++++++++++------ xen/include/xen/sched.h | 1 + 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/xen/common/domain.c b/xen/common/domain.c index 0bef436ef3..5f183a0d83 100644 --- a/xen/common/domain.c +++ b/xen/common/domain.c @@ -943,6 +943,7 @@ struct domain *domain_create(domid_t domid, rspin_lock_init_prof(d, domain_lock); rspin_lock_init_prof(d, page_alloc_lock); spin_lock_init(&d->hypercall_deadlock_mutex); + spin_lock_init(&d->pause_lock); rwlock_init(&d->caps_lock); INIT_PAGE_LIST_HEAD(&d->page_list); INIT_PAGE_LIST_HEAD(&d->extra_page_list); @@ -1624,17 +1625,25 @@ int vcpu_unpause_by_systemcontroller(struct vcpu *v) static void _domain_pause(struct domain *d, bool sync) { struct vcpu *v; + bool pause; + spin_lock(&d->pause_lock); + + pause = !atomic_read(&d->pause_count); atomic_inc(&d->pause_count); + if ( pause ) + arch_domain_pause(d); - for_each_vcpu ( d, v ) - vcpu_sleep_nosync(v); + spin_unlock(&d->pause_lock); + + if ( pause ) + for_each_vcpu ( d, v ) + vcpu_sleep_nosync(v); + /* Wait might be needed even if already paused, could have been nosync. */ if ( sync ) for_each_vcpu ( d, v ) vcpu_wait_sleep(v); - - arch_domain_pause(d); } void domain_pause(struct domain *d) @@ -1651,10 +1660,19 @@ void domain_pause_nosync(struct domain *d) void domain_unpause(struct domain *d) { struct vcpu *v; + bool unpause; + + spin_lock(&d->pause_lock); + + ASSERT(atomic_read(&d->pause_count)); + atomic_dec(&d->pause_count); + unpause = !atomic_read(&d->pause_count); + if ( unpause ) + arch_domain_unpause(d); - arch_domain_unpause(d); + spin_unlock(&d->pause_lock); - if ( atomic_dec_and_test(&d->pause_count) ) + if ( unpause ) for_each_vcpu( d, v ) vcpu_wake(v); } diff --git a/xen/include/xen/sched.h b/xen/include/xen/sched.h index 29ecc97c11..628521b9ab 100644 --- a/xen/include/xen/sched.h +++ b/xen/include/xen/sched.h @@ -573,6 +573,7 @@ struct domain * raising DOM_EXC */ evtchn_port_t suspend_evtchn; + spinlock_t pause_lock; atomic_t pause_count; atomic_t refcnt; -- 2.56.0