From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 025713F6C2C for ; Fri, 29 May 2026 16:51:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073488; cv=none; b=WUTKLWKvWMZ31zQ3NdhoBH707Nlu41OKuJKrvwcrzPPpQZyDOiLqUGEN4pcPVW/TU4bX50QIJQPCKHoSXfbPvikoj9PqRwVN1UYS6u0yZr1eIS944QKCAXceFrczas9hYOMl9QxBkOz3ZAl3X9oQpHfTcxw9Thq8IpP/c3JMi74= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073488; c=relaxed/simple; bh=PbZYMZRYLczdjt2P7xyS0U+UYKMH1FVH6F9kfPQzfB4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cP/rMV0y0Vlu/jmY3TzzGPaqC/aX/YlExJPZ0HDl69EjJ+M7p9Gr7riwwEqyCZ3JiFDZrPt7cMcyTVWOJ9NPfcYX5oyOWxUByW4YqcDD6nGkTnaE8h3TtO6bYnY4OdSn1HDp5GNEzzDImLmCRUNZrBOVjeFdjfIhobmTFWhDJ/M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=h0hO+NlS; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="h0hO+NlS" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-368b15eeb3bso24942984a91.2 for ; Fri, 29 May 2026 09:51:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073484; x=1780678284; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=6zDAoeEn8PGfgusvzyR+7E54OCJzqQJJs9HK0Wh5WQw=; b=h0hO+NlSgGq4KgqmSJCjndfLsaogBKwFMvXhwrd2mTGH7ljDWhhWho1QTnFqVezNsA H0nxr8pDvQPwZRkaFfayt8IT4f8uwCdUZoZLdelpah9uuINqaZY+1CBJVMOMvrLmSNMu x6pumHutNyRV9ESNlTcKlM+PFWMDkSKo0/chudi2OXnCGA0ShKyswO13UdsaxrBc0Hbr mmd1Jj50HAbnpD09oL+6DT6JimnDE5MhBjycWxnJ5KJctEAlUq7F7cha3ltjllJRHdxt 8cTGgRH7Xjal1QZ0mX8Oe2yWVUr96qQlgyJ5G4xRyhgxTts65mrEjAiTD5ZtyDqD8oXO Vt7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073484; x=1780678284; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6zDAoeEn8PGfgusvzyR+7E54OCJzqQJJs9HK0Wh5WQw=; b=Ynb9+LLFKAiKjPqQwTkiFfydXPzmHehHNDNvgU5YW3Eo8QVziZ3tN5PYFy7mzefjr5 6BZ/dY6ghKLaWIvkx4nNp6EORf5yblrdfC75W3Ac1BOxSHYFBMJ/xTg7lFTPNDg3leZW VMLm86HwstvBT4TDFwyCibHON9cJrgz0kX27ctK0O5uYmESDcnPHyMb6zDyD2VCpfp0Y Tt0So7TWa5lLY9Ge8RkCffl52NH5QnPeH3MIBMxTVo6dvSR8a0qtU5w/XhKD38P/576+ ed/WhcD1XTLqAyiDvGjlHreTYxTXgz6VxAn9QsAUmaQysCH15ThLAEc94FB/mdGqEROd lv4w== X-Forwarded-Encrypted: i=1; AFNElJ/HfOr2gTs612/JIZeJDmOxTbcdCw+xFbBalsOYY594KtcQNes8c1THqYwq0BiMQPuDTwpZ2PG7AV5LuBM=@vger.kernel.org X-Gm-Message-State: AOJu0Yyc5z1rM46jVrOUQUvhu40HC5jLdLjPTbzk3B0batnDLpleNzES mLVopV7EoYLZWJG9XVg6kGSSxTr+O18G4yvdZ7hFxT2d0teLLrV2NPvi6Zkm/wk9R7yW3WlQ6Uo NKmYO4Q== X-Received: from pgbeh15.prod.google.com ([2002:a05:6a02:256f:b0:c80:15fa:8bd5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:6f0e:b0:39f:a42:9247 with SMTP id adf61e73a8af0-3b4281db00bmr6956637.37.1780073483920; Fri, 29 May 2026 09:51:23 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:50:55 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-2-seanjc@google.com> Subject: [PATCH v2 01/20] locking/rt: Use raw_spin_lock_irqsave() in __rwbase_read_unlock() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse __rwbase_read_unlock() uses raw_spin_lock_irq()/raw_spin_unlock_irq() which unconditionally disables and re-enables interrupts. When read_unlock() is called from hardirq context (e.g. after a successful read_trylock() in a timer callback), the raw_spin_unlock_irq() incorrectly re-enables interrupts within the hardirq handler. This causes lockdep warnings ('hardirqs_on_prepare' from hardirq context) and can lead to IRQ state corruption. Using read_trylock() in hardirq context on PREEMPT_RT is safe because it does not record the lock owner. The read_unlock() acquires the wait_lock which is hardirq safe. This change additionally allows rwlock_t during early boot. Switch to raw_spin_lock_irqsave()/raw_spin_unlock_irqrestore() to preserve the caller's IRQ state. Signed-off-by: David Woodhouse Reviewed-by: Sebastian Andrzej Siewior Signed-off-by: Sean Christopherson --- kernel/locking/rwbase_rt.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kernel/locking/rwbase_rt.c b/kernel/locking/rwbase_rt.c index 82e078c0665a..25744862d627 100644 --- a/kernel/locking/rwbase_rt.c +++ b/kernel/locking/rwbase_rt.c @@ -153,8 +153,9 @@ static void __sched __rwbase_read_unlock(struct rwbase_= rt *rwb, struct rt_mutex_base *rtm =3D &rwb->rtmutex; struct task_struct *owner; DEFINE_RT_WAKE_Q(wqh); + unsigned long flags; =20 - raw_spin_lock_irq(&rtm->wait_lock); + raw_spin_lock_irqsave(&rtm->wait_lock, flags); /* * Wake the writer, i.e. the rtmutex owner. It might release the * rtmutex concurrently in the fast path (due to a signal), but to @@ -167,7 +168,7 @@ static void __sched __rwbase_read_unlock(struct rwbase_= rt *rwb, =20 /* Pairs with the preempt_enable in rt_mutex_wake_up_q() */ preempt_disable(); - raw_spin_unlock_irq(&rtm->wait_lock); + raw_spin_unlock_irqrestore(&rtm->wait_lock, flags); rt_mutex_wake_up_q(&wqh); } =20 --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1EABB3F787B for ; Fri, 29 May 2026 16:51:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073489; cv=none; b=dtfEY34NgBnzBqrE89bScwtwmVX9gHz5EZY0Z+YssZiIbEp55EF3jNFaVpD0F+o8EnEtFl0W6na1g2PRHpoJEeMTtvE/uyY3NtcFVrd6+leFE2M0aPk29l8a/yy35+bWV894xU1DjNPlEK/Fwzujn2cA6jtpanKWdNh8iAav0XQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073489; c=relaxed/simple; bh=WjoDBDihuoTsObsTRtzBi0FovEtka+bji/1FjjVQEiI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QlekwbXG0q+cN9RU6mhUnrHnmlxn/fawpBHID70iXzc2VYRRJJuMN/OyxmihJbGNNI09ijIkNNt8W5SA0NHX8T3tuiFE0loioc93y5DC5UsJA/Jco7TGEAr230jGdV+kNqqX8AzDpCffZXN2eMlhgMrh0HIPPm9GdJJrSJ+y8y4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=DniI1gUC; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DniI1gUC" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2b4530a90fdso106708005ad.1 for ; Fri, 29 May 2026 09:51:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073485; x=1780678285; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:from:to:cc:subject:date :message-id:reply-to; bh=fkSZAvSUuIZkLdKqbpwmfPEheEuKi4ON1NJkkf1RUcU=; b=DniI1gUCaOoqM1im4zrt2gs1XhJbyzYKvrnV86F/VNcEsBciIl+y0OBbZxdSJ0Dh23 HqEsuwXtZMNqRdoPfeTxy7DAjxVm9kmT/SbUhWMdLdjxJ+MZ90qPhKdyBqC+qS1RFaDZ 3VkU3pLkosyNZZL5IBApWX3V9CrqeVZtGkxGm3N0s4+DkzSUtVknC2UcrEHGAv/YSUZz Ob5Ww9ab8GtHTIWOlD6K/erDWFdbBF6bDhMCWr/bLvJkDVJrYTymWulyNCF5FH4JnKYv JAsz2QSQmnfDEZ4Eq2gmsetXrhZ12rznuinIuzTKq4Bo4EAtn2Ge+fXTaajJ3eGnOfSb FF4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073485; x=1780678285; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=fkSZAvSUuIZkLdKqbpwmfPEheEuKi4ON1NJkkf1RUcU=; b=E5Zf/2bFydwK5xdRqTLN4gGamBLnemNT91kX8z9xFfp5WDPMxM9joa5RTWDMPSQb+a h1Iyl1pfjV/PgVwunzX6h56Tr5kwVRiNMDA2hPENenRz4iINAismTb50QNhSvFhbDebh Lmx+Bfax5g1FmbT1AEujaBFlK8tc1+4xlMqN33FLJU34kkziTYMAaF6ctX6EiM6RYHSK 0mC3LuFGO+jPUPaUfPMqwn7iPXuuKw2jq03DDrvyFG6tBx3zrAbBmBd3VG1kwVHP+WA+ 7T2odDKPRAnQQ0aEaCVEtR3MHsLGLmS0OUPBdqBRXBR79ix8jBjG6xozaD4yJ+nWSyJE aJ9Q== X-Forwarded-Encrypted: i=1; AFNElJ+3v6kEQ762xAyZkVrb57/9mzj6dpyS+ZIuiLGpKgXR/ERI9A1sZx69cAPi0dekLo2/BsWHShI4EVBJUfA=@vger.kernel.org X-Gm-Message-State: AOJu0YxaPma+oOzFPLNe8ykJbmowkkP/mmQSlcbW+aUPGCJhEBkwDS+N 9gUfPMJCaKMTgdG28Pb7SMeMueDL1hqbAkjnV/aBd5mNk3GRjWv9VT9gg3DdrrvMbz6cBc5dya5 wgfyLZQ== X-Received: from pleu6.prod.google.com ([2002:a17:903:41c6:b0:2bf:1803:8eec]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2f8d:b0:2b0:4fb6:85ce with SMTP id d9443c01a7336-2bf3684d836mr6960405ad.21.1780073485069; Fri, 29 May 2026 09:51:25 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:50:56 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-3-seanjc@google.com> Subject: [PATCH v2 02/20] KVM: x86/xen: Use read_trylock() for GPC locks in hardirq/atomic paths From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse kvm_xen_set_evtchn_fast() is called from hardirq context (timer callback, kvm_arch_set_irq_inatomic()). On PREEMPT_RT, rwlock_t is a sleeping lock, so read_lock_irqsave() cannot be used in this context. Switch to read_trylock() and return -EWOULDBLOCK on contention, which is the designed fallback =E2=80=94 there is always a slow path for the case wh= ere the GPC is invalid and needs to be refreshed. Reported-by: syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=3D208f7f3e5f59c11aeb90 Fixes: 14243b387137 ("KVM: x86/xen: Add KVM_IRQ_ROUTING_XEN_EVTCHN and even= t channel delivery") Signed-off-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 91fd3673c09a..9bdb8e3cad58 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -697,6 +697,7 @@ void kvm_xen_inject_pending_events(struct kvm_vcpu *v) int __kvm_xen_has_interrupt(struct kvm_vcpu *v) { struct gfn_to_pfn_cache *gpc =3D &v->arch.xen.vcpu_info_cache; + bool atomic =3D in_atomic() || !task_is_running(current); unsigned long flags; u8 rc =3D 0; =20 @@ -713,7 +714,15 @@ int __kvm_xen_has_interrupt(struct kvm_vcpu *v) BUILD_BUG_ON(sizeof(rc) !=3D sizeof_field(struct compat_vcpu_info, evtchn_upcall_pending)); =20 - read_lock_irqsave(&gpc->lock, flags); + if (atomic) { + local_irq_save(flags); + if (!read_trylock(&gpc->lock)) { + local_irq_restore(flags); + return 1; + } + } else { + read_lock_irqsave(&gpc->lock, flags); + } while (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) { read_unlock_irqrestore(&gpc->lock, flags); =20 @@ -725,7 +734,7 @@ int __kvm_xen_has_interrupt(struct kvm_vcpu *v) * and we'll end up getting called again from a context where we *can* * fault in the page and wait for it. */ - if (in_atomic() || !task_is_running(current)) + if (atomic) return 1; =20 if (kvm_gpc_refresh(gpc, sizeof(struct vcpu_info))) { @@ -1794,7 +1803,6 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe= , struct kvm *kvm) struct gfn_to_pfn_cache *gpc =3D &kvm->arch.xen.shinfo_cache; struct kvm_vcpu *vcpu; unsigned long *pending_bits, *mask_bits; - unsigned long flags; int port_word_bit; bool kick_vcpu =3D false; int vcpu_idx, idx, rc; @@ -1816,9 +1824,10 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *x= e, struct kvm *kvm) =20 idx =3D srcu_read_lock(&kvm->srcu); =20 - read_lock_irqsave(&gpc->lock, flags); - if (!kvm_gpc_check(gpc, PAGE_SIZE)) + if (!read_trylock(&gpc->lock)) goto out_rcu; + if (!kvm_gpc_check(gpc, PAGE_SIZE)) + goto out_unlock; =20 if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) { struct shared_info *shinfo =3D gpc->khva; @@ -1847,11 +1856,10 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *= xe, struct kvm *kvm) } else { rc =3D 1; /* Delivered to the bitmap in shared_info. */ /* Now switch to the vCPU's vcpu_info to set the index and pending_sel */ - read_unlock_irqrestore(&gpc->lock, flags); + read_unlock(&gpc->lock); gpc =3D &vcpu->arch.xen.vcpu_info_cache; =20 - read_lock_irqsave(&gpc->lock, flags); - if (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) { + if (!read_trylock(&gpc->lock)) { /* * Could not access the vcpu_info. Set the bit in-kernel * and prod the vCPU to deliver it for itself. @@ -1860,6 +1868,11 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *x= e, struct kvm *kvm) kick_vcpu =3D true; goto out_rcu; } + if (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) { + if (!test_and_set_bit(port_word_bit, &vcpu->arch.xen.evtchn_pending_sel= )) + kick_vcpu =3D true; + goto out_unlock; + } =20 if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) { struct vcpu_info *vcpu_info =3D gpc->khva; @@ -1883,8 +1896,9 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe= , struct kvm *kvm) } } =20 + out_unlock: + read_unlock(&gpc->lock); out_rcu: - read_unlock_irqrestore(&gpc->lock, flags); srcu_read_unlock(&kvm->srcu, idx); =20 if (kick_vcpu) { --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56C483F4DF1 for ; Fri, 29 May 2026 16:51:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073492; cv=none; b=FioMgWJkqxj04oSHIMRkJovHeIITLs63lI/Zq6mgE/qqXQ/NqOy0WxeyLbnANyOxmibfOZc07OtOe1X1IH/qhJluJkS7KwiEh6mhfYUvT2OzkCDNRFL8X+Tnd4Cx4LugVzHZ2u5MLlxoBvu6cOVq2lwI4FriPN012+bYenlr/0k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073492; c=relaxed/simple; bh=0WKDTNo/N6mctTV85KTNp1xGVOCXUy7AHtFJOVuefGA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PwSo36swWEuykMz5KGaTiFOHxV+B+8FQaO9tFSCfrWyteUgPRWGfk3TeWqCikt1DwHq8JC/wZfo5j+ePOvfEtPXRD/jnYNab20l3D0BtcA3MJyj8tAm4zLH6x3bZEklvuTRwYILhBsAgrzmMA3+3EiXWwgJ885C6qk7KwFuFxBI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=XwMLMWoi; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="XwMLMWoi" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-368f2d76b04so13606735a91.3 for ; Fri, 29 May 2026 09:51:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073486; x=1780678286; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=6fy61ks8ZEC4lUkYGSB1laEeqHhcHSahxf6RJbhPdvs=; b=XwMLMWoiy2pz/ZG+UgPifv0hwJwDcLhxIDmUZOGr4RxMYQO8x1S58xFYHThkU2bdtQ XdY0WrgV/KxZS8XustlepVoZIE10KAKPxI65KRSF2/cRPlTMPBj4qxxP/Eew0VyEqmy1 D1phYooPxnd8w4fRBZCRjMrtm76AGzgxvhKQRG9R6SPavpIvyJ2K7CAl80V2KDktF726 ECOE5DioMnpL7vw1IFwQtK4LOlBGFCMB5g0TQONedJiR6Oq+i4+pRBtYHSXeTyyHlloQ xVUxRTtkIOltv7eyrSYdZBnJoqiAvZcSwvkeHULTSstYoWeeiusNjR+tKQ7ULeTbgh4o gTBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073486; x=1780678286; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6fy61ks8ZEC4lUkYGSB1laEeqHhcHSahxf6RJbhPdvs=; b=LAZJb57SuefI9lBOlHbn6iJ3vEfcPn1Ec0xlxkuN5OJSA6+drgp30fZ1T6otyTSZ1R VIEMwYcE14X3WdgDwfoDrrESh6ogZ8WhdzLKeMB4NlJ2kTzE0AYvyN7qQhOQoWcRgepF ALd3zF3VARVXh4SNWF4VEeWUm8/nimqOotOfFfcRLozW+sutQC6Ap2NuOuuHbwhD9Jnt aO8bTWnkoBGPA8dIAGoLNfExPN8AuYSuuxKaElnuYCg0P4rKJ0EkAF7CsQXmBL7VnDi0 AC19R18zdSUR/YBh5Q5FQQilB+AGIEQYG0E+zdbpL0DrzLkv3LD9oxZ7P0j1q9cdYyoI 5CjA== X-Forwarded-Encrypted: i=1; AFNElJ+W6DZ93qjfkIeANiT3vsmb/wA3oRwBWbWKmrqTYArOZpw6SLLi8MofdFC+54Ca1rZm7jYzD+MryWiAV28=@vger.kernel.org X-Gm-Message-State: AOJu0Yw8LKOY1RTPiOT74jdmOCt7tAxMty6vJ+B8ARmKuuxixjaLdSVf bTCfg89owL9BtCfCxgwSY95NDTih+95tPoAdyn9Xw9Pv2hEuKOa6xu14toFtCdujwkv4PlIWA0g gSZpufw== X-Received: from pllc4.prod.google.com ([2002:a17:902:7244:b0:2bd:4f83:e3df]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1207:b0:2bd:3bfd:7512 with SMTP id d9443c01a7336-2bf368468ddmr7071335ad.29.1780073486132; Fri, 29 May 2026 09:51:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:50:57 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-4-seanjc@google.com> Subject: [PATCH v2 03/20] KVM: x86/xen: Remove unnecessary irqsave from GPC lock usage in xen.c From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse Now that the hardirq path (xen_timer_callback and set_evtchn_fast) uses read_trylock() instead of read_lock_irqsave(), the remaining GPC lock users in xen.c are only called from process context (vcpu_run, ioctls). There is no need to disable interrupts to prevent concurrent access from a hardirq user, since the hardirq path no longer takes the lock. Convert read_lock_irqsave()/read_unlock_irqrestore() to plain read_lock()/read_unlock() in: - kvm_xen_update_runstate_guest() - kvm_xen_shared_info_init() - xen_get_guest_pvclock() - kvm_xen_inject_pending_events() - __kvm_xen_has_interrupt() - wait_pending_event() Signed-off-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 60 +++++++++++++++++++--------------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 9bdb8e3cad58..b1fae42bf295 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -45,15 +45,15 @@ static int kvm_xen_shared_info_init(struct kvm *kvm) int ret =3D 0; int idx =3D srcu_read_lock(&kvm->srcu); =20 - read_lock_irq(&gpc->lock); + read_lock(&gpc->lock); while (!kvm_gpc_check(gpc, PAGE_SIZE)) { - read_unlock_irq(&gpc->lock); + read_unlock(&gpc->lock); =20 ret =3D kvm_gpc_refresh(gpc, PAGE_SIZE); if (ret) goto out; =20 - read_lock_irq(&gpc->lock); + read_lock(&gpc->lock); } =20 /* @@ -96,7 +96,7 @@ static int kvm_xen_shared_info_init(struct kvm *kvm) smp_wmb(); =20 wc->version =3D wc_version + 1; - read_unlock_irq(&gpc->lock); + read_unlock(&gpc->lock); =20 kvm_make_all_cpus_request(kvm, KVM_REQ_MASTERCLOCK_UPDATE); =20 @@ -155,22 +155,21 @@ static int xen_get_guest_pvclock(struct kvm_vcpu *vcp= u, struct gfn_to_pfn_cache *gpc, unsigned int offset) { - unsigned long flags; int r; =20 - read_lock_irqsave(&gpc->lock, flags); + read_lock(&gpc->lock); while (!kvm_gpc_check(gpc, offset + sizeof(*hv_clock))) { - read_unlock_irqrestore(&gpc->lock, flags); + read_unlock(&gpc->lock); =20 r =3D kvm_gpc_refresh(gpc, offset + sizeof(*hv_clock)); if (r) return r; =20 - read_lock_irqsave(&gpc->lock, flags); + read_lock(&gpc->lock); } =20 memcpy(hv_clock, gpc->khva + offset, sizeof(*hv_clock)); - read_unlock_irqrestore(&gpc->lock, flags); + read_unlock(&gpc->lock); =20 /* * Sanity check TSC shift+multiplier to verify the guest's view of time @@ -325,7 +324,6 @@ static void kvm_xen_update_runstate_guest(struct kvm_vc= pu *v, bool atomic) struct gfn_to_pfn_cache *gpc2 =3D &vx->runstate2_cache; size_t user_len, user_len1, user_len2; struct vcpu_runstate_info rs; - unsigned long flags; size_t times_ofs; uint8_t *update_bit =3D NULL; uint64_t entry_time; @@ -421,16 +419,14 @@ static void kvm_xen_update_runstate_guest(struct kvm_= vcpu *v, bool atomic) * gfn_to_pfn caches that cover the region. */ if (atomic) { - local_irq_save(flags); if (!read_trylock(&gpc1->lock)) { - local_irq_restore(flags); return; } } else { - read_lock_irqsave(&gpc1->lock, flags); + read_lock(&gpc1->lock); } while (!kvm_gpc_check(gpc1, user_len1)) { - read_unlock_irqrestore(&gpc1->lock, flags); + read_unlock(&gpc1->lock); =20 /* When invoked from kvm_sched_out() we cannot sleep */ if (atomic) @@ -439,7 +435,7 @@ static void kvm_xen_update_runstate_guest(struct kvm_vc= pu *v, bool atomic) if (kvm_gpc_refresh(gpc1, user_len1)) return; =20 - read_lock_irqsave(&gpc1->lock, flags); + read_lock(&gpc1->lock); } =20 if (likely(!user_len2)) { @@ -467,7 +463,7 @@ static void kvm_xen_update_runstate_guest(struct kvm_vc= pu *v, bool atomic) lock_set_subclass(&gpc1->lock.dep_map, 1, _THIS_IP_); if (atomic) { if (!read_trylock(&gpc2->lock)) { - read_unlock_irqrestore(&gpc1->lock, flags); + read_unlock(&gpc1->lock); return; } } else { @@ -476,7 +472,7 @@ static void kvm_xen_update_runstate_guest(struct kvm_vc= pu *v, bool atomic) =20 if (!kvm_gpc_check(gpc2, user_len2)) { read_unlock(&gpc2->lock); - read_unlock_irqrestore(&gpc1->lock, flags); + read_unlock(&gpc1->lock); =20 /* When invoked from kvm_sched_out() we cannot sleep */ if (atomic) @@ -581,7 +577,7 @@ static void kvm_xen_update_runstate_guest(struct kvm_vc= pu *v, bool atomic) } =20 kvm_gpc_mark_dirty_in_slot(gpc1); - read_unlock_irqrestore(&gpc1->lock, flags); + read_unlock(&gpc1->lock); } =20 void kvm_xen_update_runstate(struct kvm_vcpu *v, int state) @@ -640,7 +636,6 @@ void kvm_xen_inject_pending_events(struct kvm_vcpu *v) { unsigned long evtchn_pending_sel =3D READ_ONCE(v->arch.xen.evtchn_pending= _sel); struct gfn_to_pfn_cache *gpc =3D &v->arch.xen.vcpu_info_cache; - unsigned long flags; =20 if (!evtchn_pending_sel) return; @@ -650,14 +645,14 @@ void kvm_xen_inject_pending_events(struct kvm_vcpu *v) * does anyway. Page it in and retry the instruction. We're just a * little more honest about it. */ - read_lock_irqsave(&gpc->lock, flags); + read_lock(&gpc->lock); while (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) { - read_unlock_irqrestore(&gpc->lock, flags); + read_unlock(&gpc->lock); =20 if (kvm_gpc_refresh(gpc, sizeof(struct vcpu_info))) return; =20 - read_lock_irqsave(&gpc->lock, flags); + read_lock(&gpc->lock); } =20 /* Now gpc->khva is a valid kernel address for the vcpu_info */ @@ -687,7 +682,7 @@ void kvm_xen_inject_pending_events(struct kvm_vcpu *v) } =20 kvm_gpc_mark_dirty_in_slot(gpc); - read_unlock_irqrestore(&gpc->lock, flags); + read_unlock(&gpc->lock); =20 /* For the per-vCPU lapic vector, deliver it as MSI. */ if (v->arch.xen.upcall_vector) @@ -698,7 +693,6 @@ int __kvm_xen_has_interrupt(struct kvm_vcpu *v) { struct gfn_to_pfn_cache *gpc =3D &v->arch.xen.vcpu_info_cache; bool atomic =3D in_atomic() || !task_is_running(current); - unsigned long flags; u8 rc =3D 0; =20 /* @@ -715,16 +709,13 @@ int __kvm_xen_has_interrupt(struct kvm_vcpu *v) sizeof_field(struct compat_vcpu_info, evtchn_upcall_pending)); =20 if (atomic) { - local_irq_save(flags); - if (!read_trylock(&gpc->lock)) { - local_irq_restore(flags); + if (!read_trylock(&gpc->lock)) return 1; - } } else { - read_lock_irqsave(&gpc->lock, flags); + read_lock(&gpc->lock); } while (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) { - read_unlock_irqrestore(&gpc->lock, flags); + read_unlock(&gpc->lock); =20 /* * This function gets called from kvm_vcpu_block() after setting the @@ -744,11 +735,11 @@ int __kvm_xen_has_interrupt(struct kvm_vcpu *v) */ return 0; } - read_lock_irqsave(&gpc->lock, flags); + read_lock(&gpc->lock); } =20 rc =3D ((struct vcpu_info *)gpc->khva)->evtchn_upcall_pending; - read_unlock_irqrestore(&gpc->lock, flags); + read_unlock(&gpc->lock); return rc; } =20 @@ -1445,12 +1436,11 @@ static bool wait_pending_event(struct kvm_vcpu *vcp= u, int nr_ports, struct kvm *kvm =3D vcpu->kvm; struct gfn_to_pfn_cache *gpc =3D &kvm->arch.xen.shinfo_cache; unsigned long *pending_bits; - unsigned long flags; bool ret =3D true; int idx, i; =20 idx =3D srcu_read_lock(&kvm->srcu); - read_lock_irqsave(&gpc->lock, flags); + read_lock(&gpc->lock); if (!kvm_gpc_check(gpc, PAGE_SIZE)) goto out_rcu; =20 @@ -1471,7 +1461,7 @@ static bool wait_pending_event(struct kvm_vcpu *vcpu,= int nr_ports, } =20 out_rcu: - read_unlock_irqrestore(&gpc->lock, flags); + read_unlock(&gpc->lock); srcu_read_unlock(&kvm->srcu, idx); =20 return ret; --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26FCB3F6C29 for ; Fri, 29 May 2026 16:51:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073491; cv=none; b=NBDqNVqRi/s/VQklUd5M0y/1dL2udW5/B+JV1Q1s7PnE42V2NxFm5bAmp7G1fcTJl9rbCw/MQnlbcE2yNZj3Qej6L+7ImYk/Q51ZJSNm4Q78EcEnlrvKGPBq2uQgIAPxEfCOqHgGmvmSPzmY1MDvgkN2xuXWELG6usQfu2KcZSo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073491; c=relaxed/simple; bh=oW/F801MDiaEkPjxmUiqZ3f0Bc8U/zEyrHl7/8D7RXU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VYEFer0GzQZMhF2QWRDc8dBgA7jw9F/uRAoWJR8Q7EtiNwkcnLkTFU86zHywTn8KFL0T5s3SEDibsvpMPZY0gtf44/mH8je9NeAMvBLpTk/jGIOZ0fZtFdWNh7xfclE0b5qS08Hk0XiJct+oa/+mulE5IQGgmqciZ3bfLDKShv0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=aE7LGYjv; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aE7LGYjv" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-368f2d76b04so13606752a91.3 for ; Fri, 29 May 2026 09:51:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073487; x=1780678287; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=TJnjdAhlmJESU3BFxz6VsHnLTiXOcrzZ2ZxCcyybtBc=; b=aE7LGYjvSM0/EX0jjg1p+tTwrefxxf/NJ0FMgxNZH7DF0FSjN5JpMsJySNhk4IH6Wc i8zKMN4wvo2CwKFTHBfA4eGrmSroWXyVt1Zt0Th3c/6chcl9W4qWgJWjZh93GE5xXEtu UEx+GU4pCsIZTxSXHAW7NfQxGKzWyeD7zrsvUJQcqHjEIz1yUS3o0WFPxdzH3V9ovp8l iQ+Ops9xgc7I91rtZFAQ18dqZug+9n6CZ3GNv5B3nIfqmDrwD55aFRuHwGvwo28uXJ01 jr2gv4FBhcriESBnV96J4KWJijRZNlj6u029Vb1sgkl3G3Q56v5UruMobk4wQTEgWqID ECuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073487; x=1780678287; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TJnjdAhlmJESU3BFxz6VsHnLTiXOcrzZ2ZxCcyybtBc=; b=S7PAL5KKqGdo83Mi9Cj7Hz93Beqet+M/ALClEJ1zzqXrwjTMQZSWgaqcvuKGC2BLSD GShWe1RqxzSY9ZWObDM7X5YDkyTd6DpRfmYPPjYkj3ytHhU+68OvqKZV4xDefoAeuckg uDpNROAPnRWEOqSi7AOb/pcfDgm/MgZV0jAEjXI94eG8bJjjscQfqetCwTGZoWsowm9I +zWsUTyPuMyjbwpVccTxmyk9o3MS+7A042DiI339dJHAofmNXz5fuHBU4ILgUOfj5gI7 LgC2xuOtsUBEkjsLzC+xvsSgNN299luxO12527i6OlMuO89bTeBF0nsRmO2KfYVzNg/y FekQ== X-Forwarded-Encrypted: i=1; AFNElJ85cJm7Z/Q7czk/glMCMJKG3kK4IGMU1kITfNl1GJYpJtWIoDfwe/eVFSNhc75qM/rJVRJgV490gMBCNv0=@vger.kernel.org X-Gm-Message-State: AOJu0YyObX2t4ZjP7iLtTHA5R33emaZYfYfiMI89tvkBZvq/JD8poqJB rqvCP6S9xM+qkw3Td6AL/nbObFYCzfnIvHjWWUE8wQZ0XzW69uxSLrsfE06cYek96VN1lU8jjL6 Nt8sdMg== X-Received: from pgbcz14.prod.google.com ([2002:a05:6a02:230e:b0:c74:664d:4080]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:cf12:b0:2bc:cf06:6835 with SMTP id d9443c01a7336-2bf36798747mr8362995ad.8.1780073487261; Fri, 29 May 2026 09:51:27 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:50:58 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-5-seanjc@google.com> Subject: [PATCH v2 04/20] KVM: x86: Remove unnecessary irqsave from kvm_setup_guest_pvclock() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse kvm_setup_guest_pvclock() is only called from kvm_guest_time_update() which runs in process context (vcpu_enter_guest or ioctl). There is no hardirq path that takes the GPC read lock for pvclock, so irqsave is unnecessary. Convert to plain read_lock()/read_unlock(). Signed-off-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e6f1dd84f22d..87e99756de0a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3265,18 +3265,17 @@ static void kvm_setup_guest_pvclock(struct pvclock_= vcpu_time_info *ref_hv_clock, { struct pvclock_vcpu_time_info *guest_hv_clock; struct pvclock_vcpu_time_info hv_clock; - unsigned long flags; =20 memcpy(&hv_clock, ref_hv_clock, sizeof(hv_clock)); =20 - read_lock_irqsave(&gpc->lock, flags); + read_lock(&gpc->lock); while (!kvm_gpc_check(gpc, offset + sizeof(*guest_hv_clock))) { - read_unlock_irqrestore(&gpc->lock, flags); + read_unlock(&gpc->lock); =20 if (kvm_gpc_refresh(gpc, offset + sizeof(*guest_hv_clock))) return; =20 - read_lock_irqsave(&gpc->lock, flags); + read_lock(&gpc->lock); } =20 guest_hv_clock =3D (void *)(gpc->khva + offset); @@ -3301,7 +3300,7 @@ static void kvm_setup_guest_pvclock(struct pvclock_vc= pu_time_info *ref_hv_clock, guest_hv_clock->version =3D ++hv_clock.version; =20 kvm_gpc_mark_dirty_in_slot(gpc); - read_unlock_irqrestore(&gpc->lock, flags); + read_unlock(&gpc->lock); =20 trace_kvm_pvclock_update(vcpu->vcpu_id, &hv_clock); } --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62CF13F88B8 for ; Fri, 29 May 2026 16:51:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073493; cv=none; b=V9Y8JDdqYWZpLxoFU/PQ8gaPf6soHA9oo6TEnQtnGqb9Q6d7hkewlUZrRIVT4hCkcDQ+4MYThlEkZYX8Aw/SJ4A+UkDVXrIDajuT+ZGBK3LvzDv0A/RfMoGG+v2y86rAzOGsBZHP0e+33sk2wMj24R2NlvPJT8vtXgy3p8TXWcU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073493; c=relaxed/simple; bh=7d+dI7BCOXsb/xg7q8MDnqPJ4olf9QKcYi3J2vHMi8c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oiWchKjVAPXRQzUF3cVVcF+wEslNMr4gV+wH6K8xKbXKKD4dhx0jCX1F+2BZeKNXzizXqRK19z5XBTGLVWWLrDxO64ZRAHvr34nzA8ZNELESgJdR3gCEtKsB9n4lGPKO6vhilvquKqns16QmQu++WfNCto2DsxgZQLD8Dy3xu4E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=mkxs2vGk; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="mkxs2vGk" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2b99eb06178so68097885ad.2 for ; Fri, 29 May 2026 09:51:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073488; x=1780678288; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=eAHT4lc4Hf6k7nHbKC1TYzljr82cl9daWTb4LEaL2yg=; b=mkxs2vGk7xCUFt9gHfBFeTVpD5PFjVtmX7ppOpXaysIwVfyJ240XtUp6AEIp2sEFQk GnyJJofrBTxCatInbpkj6nG/neHVQDiYnQXeG3D0YH2y6E5j7zVeQf5cTwHn7YwXlLAM Um2nRqseJzOnV8gARZm6ZJzdmskn9SMZJ3i6gJ/pPkHoWyEF1kafECcghiwZpFVGgw3i kk6G9SmLmKR+nl0B9aajQEDX137ABD6FzyhRdE1aEEQm6t3TUo2LMyUxxrCRzpwEZI12 YLfGUDAKHCt8ygu6TUC5C09fBrv7ii/1mEIQaGt+l+NTIHiOpP0Wtd8rbe541tYJFqUM vz1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073488; x=1780678288; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=eAHT4lc4Hf6k7nHbKC1TYzljr82cl9daWTb4LEaL2yg=; b=kg0kJL+YFxolchPtRLRNnuveRIo1DJOXctT41Hy9BQZ5MjizxpCYiwELWMVsMwyc5b JuBqM9G4qg3qmNJ7dhlRG7R0rOSqrFfox6HzDNycK+UIlfiQsBfuym8fsJZVz2V72GE9 tR1lhzKC8YtgNMWnlRfM3dtFWdoWsE7FqZd3ICn7EYIah6ExuFVDiG7EvVHzlfXepNMv o6o1NAIJxcbROViK5Ncvs8H3MwwFVBl8UtEJFf6hNBc8r7+JpGZSGEC6zZ/TVtk1iX8y B+IfBCsDWj0nOAV7iFfvAwtVNYz+IRNAuoXxa/ZhIHk/VfpiBPSrne6Q0LE/B5/pJULe 4mvw== X-Forwarded-Encrypted: i=1; AFNElJ+Rp1UQ9DOzrUOqOhXMlEK8NLbwE7RR/lby92C/gUMs3rcmYLPped0+yL6qz4BOvw6E+ltY6C5WxTZNEGA=@vger.kernel.org X-Gm-Message-State: AOJu0YyRSQf3OcFH+KbaZzEiZA29spuqiW3RbVCq1GXgS8REsrCdp0gX 5ibQqqMQ8vr5zQzmEMTS2C+p+p7iO94vTOBv+xT3rsBEfPLjqP6eFFfIxEKzvQYH24rbpIqEllB EL4nVsA== X-Received: from plblo7.prod.google.com ([2002:a17:903:4347:b0:2bf:8db:4516]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d58d:b0:2bf:21e6:baee with SMTP id d9443c01a7336-2bf36826736mr7842105ad.28.1780073488289; Fri, 29 May 2026 09:51:28 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:50:59 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-6-seanjc@google.com> Subject: [PATCH v2 05/20] KVM: Remove unnecessary IRQ disabling from GPC lock in pfncache.c From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: David Woodhouse Now that all hardirq/atomic GPC users (xen_timer_callback, kvm_xen_set_evtchn_fast) use read_trylock() instead of read_lock(), no hardirq path ever holds the GPC rwlock. There is therefore no risk of deadlock between the write side and a hardirq reader, and no need to disable interrupts when taking the lock. Convert all read_lock_irq()/write_lock_irq() and their unlock counterparts to plain read_lock()/write_lock() in pfncache.c. Signed-off-by: David Woodhouse Signed-off-by: Sean Christopherson --- virt/kvm/pfncache.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 728d2c1b488a..70b102095173 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -29,12 +29,12 @@ void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm,= unsigned long start, =20 spin_lock(&kvm->gpc_lock); list_for_each_entry(gpc, &kvm->gpc_list, list) { - read_lock_irq(&gpc->lock); + read_lock(&gpc->lock); =20 /* Only a single page so no need to care about length */ if (gpc->valid && !is_error_noslot_pfn(gpc->pfn) && gpc->uhva >=3D start && gpc->uhva < end) { - read_unlock_irq(&gpc->lock); + read_unlock(&gpc->lock); =20 /* * There is a small window here where the cache could @@ -44,15 +44,15 @@ void gfn_to_pfn_cache_invalidate_start(struct kvm *kvm,= unsigned long start, * acquired. */ =20 - write_lock_irq(&gpc->lock); + write_lock(&gpc->lock); if (gpc->valid && !is_error_noslot_pfn(gpc->pfn) && gpc->uhva >=3D start && gpc->uhva < end) gpc->valid =3D false; - write_unlock_irq(&gpc->lock); + write_unlock(&gpc->lock); continue; } =20 - read_unlock_irq(&gpc->lock); + read_unlock(&gpc->lock); } spin_unlock(&kvm->gpc_lock); } @@ -184,7 +184,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cac= he *gpc) mmu_seq =3D gpc->kvm->mmu_invalidate_seq; smp_rmb(); =20 - write_unlock_irq(&gpc->lock); + write_unlock(&gpc->lock); =20 /* * If the previous iteration "failed" due to an mmu_notifier @@ -225,7 +225,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cac= he *gpc) goto out_error; } =20 - write_lock_irq(&gpc->lock); + write_lock(&gpc->lock); =20 /* * Other tasks must wait for _this_ refresh to complete before @@ -248,7 +248,7 @@ static kvm_pfn_t hva_to_pfn_retry(struct gfn_to_pfn_cac= he *gpc) return 0; =20 out_error: - write_lock_irq(&gpc->lock); + write_lock(&gpc->lock); =20 return -EFAULT; } @@ -269,7 +269,7 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *g= pc, gpa_t gpa, unsigned l =20 lockdep_assert_held(&gpc->refresh_lock); =20 - write_lock_irq(&gpc->lock); + write_lock(&gpc->lock); =20 if (!gpc->active) { ret =3D -EINVAL; @@ -355,7 +355,7 @@ static int __kvm_gpc_refresh(struct gfn_to_pfn_cache *g= pc, gpa_t gpa, unsigned l unmap_old =3D (old_pfn !=3D gpc->pfn); =20 out_unlock: - write_unlock_irq(&gpc->lock); + write_unlock(&gpc->lock); =20 if (unmap_old) gpc_unmap(old_pfn, old_khva); @@ -417,9 +417,9 @@ static int __kvm_gpc_activate(struct gfn_to_pfn_cache *= gpc, gpa_t gpa, unsigned * refresh must not establish a mapping until the cache is * reachable by mmu_notifier events. */ - write_lock_irq(&gpc->lock); + write_lock(&gpc->lock); gpc->active =3D true; - write_unlock_irq(&gpc->lock); + write_unlock(&gpc->lock); } return __kvm_gpc_refresh(gpc, gpa, uhva); } @@ -458,7 +458,7 @@ void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc) * must stall mmu_notifier events until all users go away, i.e. * until gpc->lock is dropped and refresh is guaranteed to fail. */ - write_lock_irq(&gpc->lock); + write_lock(&gpc->lock); gpc->active =3D false; gpc->valid =3D false; =20 @@ -473,7 +473,7 @@ void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc) =20 old_pfn =3D gpc->pfn; gpc->pfn =3D KVM_PFN_ERR_FAULT; - write_unlock_irq(&gpc->lock); + write_unlock(&gpc->lock); =20 spin_lock(&kvm->gpc_lock); list_del(&gpc->list); --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D4173F9287 for ; Fri, 29 May 2026 16:51:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073495; cv=none; b=KlYOOWJjCKJ/+P1QcKjx46RjajHf8+AplGn/Jz42/TWFouAzejMPc513xJ08UbddwQC/zXHYgigZkH8/h0uKfm4I8CZBSuwuY5PmM2XJgpCIjZaJe2bSpUU7lJEGsZ2HLtgLFbMRbDJfdP7IsPOtCswqYD++8P2LiYRcko7jk3E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073495; c=relaxed/simple; bh=O4qpHyxDqbUyRC7xhAqgdYzXBuoRUCZbiNwyQ4FQt3U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uiRPO+LgVeMg86ErdS13ffSklxTyeCjTOw8Tjdnw+eBlZbwmLvd0nrzUL9e1zBxYd0FWn3YXSAnr2DDDfe+p7kyzOEicYtEeQAY+27kBed/t97rnb3szN9/PNKMO7C6j7CclKfOyXS+qTl3sMHJFRew8//dqTItgEYAoSscyvLo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=i6aQtMgI; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="i6aQtMgI" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2bf0453031aso28333725ad.2 for ; Fri, 29 May 2026 09:51:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073489; x=1780678289; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=V3YJTc4/phuSx/AsQ9kARrMuegtvoBaks7Ls8Sj689w=; b=i6aQtMgI2K8hiq95NeVfSxAhcfAf4oMNvLU+zzGS5XpCe1U+MBy4l5U7K7/HhKP+et FANMnobcZdpSp1rGMPk55B1KxBrzDMMdn30i/m3XZA4LXf2KH0in5eMDlb+o2TxmsjCV QoQD7taaNYzIQYgG/RKJ6gYmCFI2voKLskQXcnhzDU57hEoKIMl5yMOya58k+sIjw5Pr IDlnE6wytr6cM7gNi8c/kksYFtbzCTk3nhWxcq0Rx5G316MpFNMQMt4mRA0/C+qqn5AL JezJs5qk5cH8ANkGILCphoUWKlPxnVPFR6XCYIOqnPiFTiGjJRIbnoHMu1WM+YfpmemW tj2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073489; x=1780678289; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=V3YJTc4/phuSx/AsQ9kARrMuegtvoBaks7Ls8Sj689w=; b=duqVLbHHmBqaSKPyhrsi6fZ4DKW73SBWqNwK/7eX794qpMj7uO0omJcUn1FR75WVcW 9xVX9HYlPBk87ZW1Ch9o8oLZkmITUAhDaXK2CtCmTY+NioJU4fcQBqcmnLQhM+qjolsP niTNXnYbH6RsKvuxBdLbkx4nrx+clb+95o+1SJjsB1c30fimFL5y9EXv4BQvqW9NrTKF 2y+gWE1fPvzprHp21cG4bGNYW3p063v+Yc3KUOXXUKONfKi5tqgLU2PGr+H94LJ4v3bR diX+RypOLOkYbxqR/Yf/KGMijqJXgYa7EvZXc95cDLa7MraAYvn7Z8jNeBGLsQCBcbnh vNXg== X-Forwarded-Encrypted: i=1; AFNElJ9q6U0dWDUiAJfa7+Bmtaq2n3/XWct+K0KxAigKhtgL0lIQcw3IG5BY6ynqsbdgUMihCCsEaex+eAWsM/M=@vger.kernel.org X-Gm-Message-State: AOJu0YxsxqOLd9H21VMNisqm7qHydkuCJqLnfN8DKagn5SWWvN4cuQ0+ eth7xLbYQvLSR3jAZIJyyO+42BarIG349V3bEqnzVbxNuQ+3ICphPoGLrYK2a6YUBU6jnFGEtXQ G2h1/IQ== X-Received: from pln12.prod.google.com ([2002:a17:903:360c:b0:2bf:162a:acda]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f651:b0:2b2:4bf9:1766 with SMTP id d9443c01a7336-2bf36889aa0mr7584825ad.33.1780073489348; Fri, 29 May 2026 09:51:29 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:00 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-7-seanjc@google.com> Subject: [PATCH v2 06/20] KVM: x86/xen: Use guard() to grab kvm->srcu around gpc critical sections From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use guard() to acquire and release kvm->srcu protection around gpc critical sections, so that said critical sections can also use the fancy __cleanup() functionality. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index b1fae42bf295..0c6b74b97408 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -42,8 +42,9 @@ static int kvm_xen_shared_info_init(struct kvm *kvm) u32 *wc_sec_hi; u32 wc_version; u64 wall_nsec; - int ret =3D 0; - int idx =3D srcu_read_lock(&kvm->srcu); + int ret; + + guard(srcu)(&kvm->srcu); =20 read_lock(&gpc->lock); while (!kvm_gpc_check(gpc, PAGE_SIZE)) { @@ -51,7 +52,7 @@ static int kvm_xen_shared_info_init(struct kvm *kvm) =20 ret =3D kvm_gpc_refresh(gpc, PAGE_SIZE); if (ret) - goto out; + return ret; =20 read_lock(&gpc->lock); } @@ -99,10 +100,7 @@ static int kvm_xen_shared_info_init(struct kvm *kvm) read_unlock(&gpc->lock); =20 kvm_make_all_cpus_request(kvm, KVM_REQ_MASTERCLOCK_UPDATE); - -out: - srcu_read_unlock(&kvm->srcu, idx); - return ret; + return 0; } =20 void kvm_xen_inject_timer_irqs(struct kvm_vcpu *vcpu) @@ -1437,9 +1435,10 @@ static bool wait_pending_event(struct kvm_vcpu *vcpu= , int nr_ports, struct gfn_to_pfn_cache *gpc =3D &kvm->arch.xen.shinfo_cache; unsigned long *pending_bits; bool ret =3D true; - int idx, i; + int i; + + guard(srcu)(&kvm->srcu); =20 - idx =3D srcu_read_lock(&kvm->srcu); read_lock(&gpc->lock); if (!kvm_gpc_check(gpc, PAGE_SIZE)) goto out_rcu; @@ -1462,8 +1461,6 @@ static bool wait_pending_event(struct kvm_vcpu *vcpu,= int nr_ports, =20 out_rcu: read_unlock(&gpc->lock); - srcu_read_unlock(&kvm->srcu, idx); - return ret; } =20 @@ -1795,7 +1792,7 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe= , struct kvm *kvm) unsigned long *pending_bits, *mask_bits; int port_word_bit; bool kick_vcpu =3D false; - int vcpu_idx, idx, rc; + int vcpu_idx, rc; =20 vcpu_idx =3D READ_ONCE(xe->vcpu_idx); if (vcpu_idx >=3D 0) @@ -1812,10 +1809,11 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *= xe, struct kvm *kvm) =20 rc =3D -EWOULDBLOCK; =20 - idx =3D srcu_read_lock(&kvm->srcu); + guard(srcu)(&kvm->srcu); =20 if (!read_trylock(&gpc->lock)) - goto out_rcu; + return rc; + if (!kvm_gpc_check(gpc, PAGE_SIZE)) goto out_unlock; =20 @@ -1856,7 +1854,7 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe= , struct kvm *kvm) */ if (!test_and_set_bit(port_word_bit, &vcpu->arch.xen.evtchn_pending_sel= )) kick_vcpu =3D true; - goto out_rcu; + goto out_kick; } if (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) { if (!test_and_set_bit(port_word_bit, &vcpu->arch.xen.evtchn_pending_sel= )) @@ -1888,9 +1886,7 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe= , struct kvm *kvm) =20 out_unlock: read_unlock(&gpc->lock); - out_rcu: - srcu_read_unlock(&kvm->srcu, idx); - + out_kick: if (kick_vcpu) { kvm_make_request(KVM_REQ_UNBLOCK, vcpu); kvm_vcpu_kick(vcpu); --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C6FD43F9F52 for ; Fri, 29 May 2026 16:51:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073496; cv=none; b=J3V7HNYs9Rqu+DGBrXB7aMqjZrunHnohNDe+0DJRbHUwCRorNN7fO39ZpIUzETFSUiXZiqZ27+izkeau9RD91+CmfBcnklKCoRBg80VYO7KCIJnmoVI2qy+hwNjRW8OUMj7bwENmKAPH9uAmj6nz1/iP/HXJ3aQLqnK2sTFl6Pk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073496; c=relaxed/simple; bh=GoET2JKvJg3a0x9eCSCU+59BfoX2Vx7yF+LJx8bxDqM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MwQcrdTVoSh39sacc2w617mFmaCxTIPqyK3C1tPMNlUyKi1EmxoQY9m9VTjbHEtm7ddfr2x/nzZ8eF+4uqOW0XzmKoGbrynwFQTpYMGzWm9JuA7kHwGefS8hFjaZqxzU63SYpZiwJ5zaY2Y5HWHzL4vSxyld8UHJN9M5aCvdi6g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=UDZvnhQ6; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="UDZvnhQ6" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2bf11699875so20305915ad.1 for ; Fri, 29 May 2026 09:51:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073491; x=1780678291; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=i2QifqEk3Wa+x9NPrIkPMLqkVE5h8yODSs0wPMQc5rE=; b=UDZvnhQ6llmTpTa1CF4WrChHFnnJlK/WNVguJPoFr7b7Y1duOtI5JOxar9GkMgMEVo 1cImU24BB45yJjX+YsgJ24gHGS87MlpYcJtI/7Njxsf3DoNQD9QUm9amIL+lQ6CPiRCp SoOfZ5+SpZ9tf4m+BzucY5APA2LrV4OYFaCaC9IUueRWvo+R8upd12YfGMUbNWaZYqOP RQZPWIRGbRpFEeqxWLd6EjjpUGz0wlJ46t/nGmibJ/Npa9T0nEm3oyVgUUYpIENNjZBB QFSJQ5Fm7Coo/2W/OJXPFtz3ubvW8AAzveEe028xtYz2VGb5A0e5gNXiDrU0+zeC2xpn 8P6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073491; x=1780678291; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=i2QifqEk3Wa+x9NPrIkPMLqkVE5h8yODSs0wPMQc5rE=; b=Vw7ep9O3aLoVMG23JW0k7RdEg1BmEf/UnxHWifMHTnvQqGaql722I1PZTamCgF30Ar d8YBKwbPu6K5WzUR5h8KAaxEG5CBi0ngTOoHGgnZuOHCQ0dPd6zxUgaaiZ5ZrTvJtLtK CBkxJVfocHY39RFmYIouPU4SKVSqbAKmru31pYuAyKEI7zM2IWJsvEteAjFq6SqhYI+l 26N/PShes4ZYaEmzU1AKBqEfC5tWmmuJ0b9s9IPmNX/j2ia139hZdYyZkFhnwaVWG5VS e0Tg3MRcp0mURkgvSSgMOhZ29eZYDlafWcLqDZxlkvZedhdhFm4O5l/DA59nT2nzdw24 OJqg== X-Forwarded-Encrypted: i=1; AFNElJ850pGpq1ygeauvM0q2qN/esiLAGakLbHlWtJcU+bYUt3BN+6rAkW1c55kU2ZCY9iO04Q4RCwaekGdzmDs=@vger.kernel.org X-Gm-Message-State: AOJu0Yx6HEKuQXfU9RNQqZBEOPPob0WDZWviNhQoQYj61qqbYl2LQoZQ fY3hQxFvLrblny+5kHklBHvOvj7JjCclCSGWm8JQxpHfrBW8ilUMqr1ienV2RjWothfiy2yVzN9 GEL3Sig== X-Received: from play17.prod.google.com ([2002:a17:902:e191:b0:2bd:16cc:1966]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:94e:b0:2bd:73f4:8e4f with SMTP id d9443c01a7336-2bf372bcb77mr2964585ad.0.1780073490710; Fri, 29 May 2026 09:51:30 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:01 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-8-seanjc@google.com> Subject: [PATCH v2 07/20] KVM: x86/xen: Extract delivery of event to vCPU into a separate helper From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Hoist the actual fastpath delivery of an event to a vCPU into a separate helper so that CLASS()-based gpc locking+checking can be used without needing to implement scoped versions. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 100 ++++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 46 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 0c6b74b97408..020ef0ddab01 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -1775,6 +1775,57 @@ static void kvm_xen_check_poller(struct kvm_vcpu *vc= pu, int port) } } =20 +static void __kvm_xen_set_evtchn_fast(struct kvm_vcpu *vcpu, int port_word= _bit) +{ + struct gfn_to_pfn_cache *gpc =3D &vcpu->arch.xen.vcpu_info_cache; + bool kick_vcpu =3D false; + + /* Now switch to the vCPU's vcpu_info to set the index and pending_sel */ + if (!read_trylock(&gpc->lock)) { + /* + * Could not access the vcpu_info. Set the bit in-kernel and + * prod the vCPU to deliver it for itself. + */ + if (!test_and_set_bit(port_word_bit, &vcpu->arch.xen.evtchn_pending_sel)) + kick_vcpu =3D true; + goto out_kick; + } + if (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) { + if (!test_and_set_bit(port_word_bit, &vcpu->arch.xen.evtchn_pending_sel)) + kick_vcpu =3D true; + goto out_unlock; + } + + if (IS_ENABLED(CONFIG_64BIT) && vcpu->kvm->arch.xen.long_mode) { + struct vcpu_info *vcpu_info =3D gpc->khva; + if (!test_and_set_bit(port_word_bit, &vcpu_info->evtchn_pending_sel)) { + WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1); + kick_vcpu =3D true; + } + } else { + struct compat_vcpu_info *vcpu_info =3D gpc->khva; + if (!test_and_set_bit(port_word_bit, + (unsigned long *)&vcpu_info->evtchn_pending_sel)) { + WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1); + kick_vcpu =3D true; + } + } + + /* For the per-vCPU lapic vector, deliver it as MSI. */ + if (kick_vcpu && vcpu->arch.xen.upcall_vector) { + kvm_xen_inject_vcpu_vector(vcpu); + kick_vcpu =3D false; + } + +out_unlock: + read_unlock(&gpc->lock); +out_kick: + if (kick_vcpu) { + kvm_make_request(KVM_REQ_UNBLOCK, vcpu); + kvm_vcpu_kick(vcpu); + } +} + /* * The return value from this function is propagated to kvm_set_irq() API, * so it returns: @@ -1791,7 +1842,6 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe= , struct kvm *kvm) struct kvm_vcpu *vcpu; unsigned long *pending_bits, *mask_bits; int port_word_bit; - bool kick_vcpu =3D false; int vcpu_idx, rc; =20 vcpu_idx =3D READ_ONCE(xe->vcpu_idx); @@ -1843,55 +1893,13 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *= xe, struct kvm *kvm) kvm_xen_check_poller(vcpu, xe->port); } else { rc =3D 1; /* Delivered to the bitmap in shared_info. */ - /* Now switch to the vCPU's vcpu_info to set the index and pending_sel */ - read_unlock(&gpc->lock); - gpc =3D &vcpu->arch.xen.vcpu_info_cache; - - if (!read_trylock(&gpc->lock)) { - /* - * Could not access the vcpu_info. Set the bit in-kernel - * and prod the vCPU to deliver it for itself. - */ - if (!test_and_set_bit(port_word_bit, &vcpu->arch.xen.evtchn_pending_sel= )) - kick_vcpu =3D true; - goto out_kick; - } - if (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) { - if (!test_and_set_bit(port_word_bit, &vcpu->arch.xen.evtchn_pending_sel= )) - kick_vcpu =3D true; - goto out_unlock; - } - - if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) { - struct vcpu_info *vcpu_info =3D gpc->khva; - if (!test_and_set_bit(port_word_bit, &vcpu_info->evtchn_pending_sel)) { - WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1); - kick_vcpu =3D true; - } - } else { - struct compat_vcpu_info *vcpu_info =3D gpc->khva; - if (!test_and_set_bit(port_word_bit, - (unsigned long *)&vcpu_info->evtchn_pending_sel)) { - WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1); - kick_vcpu =3D true; - } - } - - /* For the per-vCPU lapic vector, deliver it as MSI. */ - if (kick_vcpu && vcpu->arch.xen.upcall_vector) { - kvm_xen_inject_vcpu_vector(vcpu); - kick_vcpu =3D false; - } } =20 - out_unlock: +out_unlock: read_unlock(&gpc->lock); - out_kick: - if (kick_vcpu) { - kvm_make_request(KVM_REQ_UNBLOCK, vcpu); - kvm_vcpu_kick(vcpu); - } =20 + if (rc =3D=3D 1) + __kvm_xen_set_evtchn_fast(vcpu, port_word_bit); return rc; } =20 --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0514F3FB040 for ; Fri, 29 May 2026 16:51:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073496; cv=none; b=sWdNKQpR+dugjFLdM4OWyoOdW1wzjFhl/gNDur+Qx1N68eQtEJ0PDStoYibeVG/tLcDxyMN14ht/WcRcXn4WhfHajucK8Ukn9I6Y6UyFPlsZdij0div0J7RFtzrexWZThLSBLh1BaQR1znWCXXPL2tfOGn0oID4XF+iz+L/rgSM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073496; c=relaxed/simple; bh=0lsKgKDWAG6ilUiGyVqdXWwZKr2qSgHhst802vkoXjo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PqMsm7EIaGy+BNMgZV0qckJVUTn3cwqBUTpW/yStpvoLJPq5ZC6qUIEFAprqKoTWAGR0Uc6L8MSTRM9LkdVt28Eu7PF5C7mixWND7JGpkIX0C4bKy0JkhWYQEa01Y709T3ePiJnZDHfZV0npKG/YDtfMp0N4rskns4XvpcMoc0o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=R6Mq6zu6; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="R6Mq6zu6" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-36bc02d28b6so823596a91.3 for ; Fri, 29 May 2026 09:51:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073492; x=1780678292; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=yJlZ0p8w2Zt6JF+kmSoduaX7LUoYSExgSIwQfbW32Qc=; b=R6Mq6zu6p5QqeYVkcrUTTo+km9ImVC9ITKBZehmt7a52jmcR2dp3+ZJgPgYoca6uwp pPuTS1nKeBb3wZlzUogFBEPow5FjCJuO2QICnSv4aWhXWe+8pQBjpKLc9UlemKjmvrZR +R0Th4IHko+qlzkzhPrcoegVll7VFtNTUKEPMDnMCppqFg8Tdx5HqO/Bz6qYIM91I6TK w58OWYiKGscm7o3RHECSJqYQNbSt3GqSUET097XItDFTAlx7ronc9tqFXWZgJGnIX2bb Aw9ZUpgwQ4xaCzcLZqGEx/eepb1DCHYH+Ag7w4UZRe4aHlcUUwR0ejaw+Xyugjl4kkJd A9HA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073492; x=1780678292; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yJlZ0p8w2Zt6JF+kmSoduaX7LUoYSExgSIwQfbW32Qc=; b=FkTg4SUFNvM/qg0+UNNhWd3ITaGcr9bLZ8+m6ofM/NIhHepgbX/fEOYtz4k3EYQfy8 PYrX8KkAv9k+PqX3dK8jOdaKhKirOvUoYHgQPlxtDw+kE5o3sr9H0aigMQc7+LDsLyxv Y8lalcy+WZnXXGQDynx6Ynq3Qttvr/YExo3UrYQbNMz45alRnOhF437ZfsqivhRQvL9m TZ8s4CvE7So9FpiFEPinGXKuPWkeDJjlsosnp0aBtzVoq+aGekk9N224SdieQLUulh2E jGC0qjETQHnxD7IdB8poDqa+tW3Bug2WnOAAxwuCS+9GL0gn26eJSDTeqGpzr870W/PS eVvQ== X-Forwarded-Encrypted: i=1; AFNElJ+EXUrNg3fXIU/sE5M6iQrU4ABmFu70wwaUtxo/VwWPxCXoUOna4yr40GCMXAMl7TOVTsOBPvUORD28PNo=@vger.kernel.org X-Gm-Message-State: AOJu0YyzcMm2DF1B8s6r/Iy64VlkP7FefV5WmOqMnX9aVzdOPO598IC2 J8c7h6uGCCC8MWMZjSGnn5NDo9VDc5/VuAyq5iseEvu9vRboaa/orFCFh3Jwi/JTMgWGKGxwq8C 6cd/kTg== X-Received: from pghk24.prod.google.com ([2002:a63:f018:0:b0:c82:3956:ab7c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:ce4a:b0:3aa:f9cb:d438 with SMTP id adf61e73a8af0-3b427ecf1bcmr72868637.21.1780073491805; Fri, 29 May 2026 09:51:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:02 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-9-seanjc@google.com> Subject: [PATCH v2 08/20] KVM: x86/xen: Explicitly tag "shared info" page as never being dirty tracked From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Explicitly mark the Xen shared info page as never being dirty tracked so that higher-level gpc APIs can be added to automatically take care of things like dirty tracking, without reintroducing the bug fixed by commit 55749769fe60 ("KVM: x86: Fix wall clock writes in Xen shared_info not to mark page dirty"). And because the code _looks_ buggy. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 2 +- include/linux/kvm_host.h | 13 ++++++++++--- include/linux/kvm_types.h | 1 + virt/kvm/pfncache.c | 4 +++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 020ef0ddab01..ab8e95647406 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -2334,7 +2334,7 @@ void kvm_xen_init_vm(struct kvm *kvm) { mutex_init(&kvm->arch.xen.xen_lock); idr_init(&kvm->arch.xen.evtchn_ports); - kvm_gpc_init(&kvm->arch.xen.shinfo_cache, kvm); + __kvm_gpc_init(&kvm->arch.xen.shinfo_cache, kvm, true); } =20 void kvm_xen_destroy_vm(struct kvm *kvm) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 27498e990dff..0dc4eb78b6d9 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1427,16 +1427,23 @@ int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa= _t gpa, const void *data, unsigned long len); =20 /** - * kvm_gpc_init - initialize gfn_to_pfn_cache. + * __kvm_gpc_init - initialize gfn_to_pfn_cache. * * @gpc: struct gfn_to_pfn_cache object. * @kvm: pointer to kvm instance. + * @never_dirty: %true if the associated gfn should never be marked dirty * * This sets up a gfn_to_pfn_cache by initializing locks and assigning the * immutable attributes. Note, the cache must be zero-allocated (or zeroe= d by * the caller before init). */ -void kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct kvm *kvm); +void __kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct kvm *kvm, + bool never_dirty); + +static inline void kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct kvm *= kvm) +{ + __kvm_gpc_init(gpc, kvm, false); +} =20 /** * kvm_gpc_activate - prepare a cached kernel mapping and HPA for a given = guest @@ -1942,7 +1949,7 @@ static inline void kvm_gpc_mark_dirty_in_slot(struct = gfn_to_pfn_cache *gpc) { lockdep_assert_held(&gpc->lock); =20 - if (!gpc->memslot) + if (!gpc->memslot || gpc->never_dirty) return; =20 mark_page_dirty_in_slot(gpc->kvm, gpc->memslot, gpa_to_gfn(gpc->gpa)); diff --git a/include/linux/kvm_types.h b/include/linux/kvm_types.h index a568d8e6f4e8..e850adc3f47e 100644 --- a/include/linux/kvm_types.h +++ b/include/linux/kvm_types.h @@ -94,6 +94,7 @@ struct gfn_to_pfn_cache { kvm_pfn_t pfn; bool active; bool valid; + bool never_dirty; }; =20 #ifdef KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 70b102095173..9209f06c46b4 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -382,7 +382,8 @@ int kvm_gpc_refresh(struct gfn_to_pfn_cache *gpc, unsig= ned long len) return __kvm_gpc_refresh(gpc, gpc->gpa, uhva); } =20 -void kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct kvm *kvm) +void __kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct kvm *kvm, + bool never_dirty) { rwlock_init(&gpc->lock); mutex_init(&gpc->refresh_lock); @@ -392,6 +393,7 @@ void kvm_gpc_init(struct gfn_to_pfn_cache *gpc, struct = kvm *kvm) gpc->gpa =3D INVALID_GPA; gpc->uhva =3D KVM_HVA_ERR_BAD; gpc->active =3D gpc->valid =3D false; + gpc->never_dirty =3D never_dirty; } =20 static int __kvm_gpc_activate(struct gfn_to_pfn_cache *gpc, gpa_t gpa, uns= igned long uhva, --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D153F3FBB5E for ; Fri, 29 May 2026 16:51:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073498; cv=none; b=ANBmRINeJcjZ6AX1NboKYpbGwojYRf1mjlHWPI/pFCQfgSpYaIn7fD83FipKKmVLFhqpYyyyxyo1OwhRz21t7qFk/50M6ke8x0+oy1dnFNtaB2I22vNC/Qe6YZpUWsn7QJrPQQMHwDobbpnhOHMfdA7v8Wo4rtQislDV25ruPeE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073498; c=relaxed/simple; bh=sxiylftm2Xrx+BcnKKefzVgCGCX1y+gcVBkU3gV1EsI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mjFrMeXIaQdUhyrfnZ4IVaOcECJzUn2PWP7jLEAEz+5S8IL+5Qcy9uCjdHt8SJ46V7V1Mn1p+j5biyg5m4sx62EexRVWwpB9KPHeMa+qiJA+pV6H+AYXowcATdqBsYVgZoiDFFBFzOS1MsCBgP/9QbLR+38ccT6DKfFB8W9ljzE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PHDNgvdz; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PHDNgvdz" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-c8582b167f7so99331a12.0 for ; Fri, 29 May 2026 09:51:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073493; x=1780678293; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=6jFRO5B5gIjR7rsGbbPkzABaqNE8Q13YLBC9Ee7R2t4=; b=PHDNgvdzhEz6j+GjOJE2sfVGto1xoIsb29B9+KJXeyrOkcz3T4TCMRRMNgpREqh9MN 7vU7K8uNKIfKyiTpRux8n/oN42HcGHW6cM+/JbgxcZixYBJb2CjSt+PFTlkj0EluvGia KEleUMohIoduKh4Nd8Oyd8r9A0zXMCRGLeK2rFi44xRpDlAvOUlMuZ+sPslcOyCGfu0k TVhVDvl/P1zK6gWbLi6tK8gsToqTCZbHqk/Tr7pvxSe28ggXF4AGxGCYuLt1d4IjANcC TAT/91YuiIsOjZlCEyy08RHPjo0dFGKTlRyLh68zX4/n/sfQ6DojJxrCxn8Vd1G7l7uU C9Ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073493; x=1780678293; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6jFRO5B5gIjR7rsGbbPkzABaqNE8Q13YLBC9Ee7R2t4=; b=SrcY0piZwgkBaP9auIQYY64RkQyzQEF6qrUBSqparLAI/z/xi7/BCa4Eh4ZhnE9/W+ UvpU6ZSVGcaeIXQ5GBto5yO2Yjkuaoi6G6ahiMNri9XRAxMaoQW9uNlS6RWGQC3epBYU DgFaa332ZwBf5L/gWv4f4tNBWKyGs11uj1T3axvY+Xa8RnGMKhiRJUHZ6NjKNVpRkKpt L78/vw7ayim+5SaPuxpzbnsLswnnsVZOoESGiA/qcPH/att7MI75KedNwXP3SzasZXpm 0gzo34PGdi4OncZFfoqHm6Whz5HlL69CDIMpkLVEV7ABPXmWqcn8oh8DT3GLjHKnW4Cy JTTw== X-Forwarded-Encrypted: i=1; AFNElJ+CO0MMIW7foUn+InhPqmLPmBXo636541MM/BWSAZBKfcwDT+VT3dr6uB5FSPrRlDdShzwlOR2rJv5Gjec=@vger.kernel.org X-Gm-Message-State: AOJu0Yw6mXpdc0YLCD3nAwwU/0zU5uhCqmN0Kzp63uMfXaxF4MieeOBQ 0XyrDqpx5/wTo//PgBKK2K7rjSMdIeC/w3iYcBX7SSGVCflmTeGBd4r2bCfstA47oerSt9+Yz22 gBxnyFw== X-Received: from pgbct3.prod.google.com ([2002:a05:6a02:2103:b0:c73:fc44:8baf]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3a44:b0:3b3:1951:489b with SMTP id adf61e73a8af0-3b427f5bd55mr45061637.45.1780073492919; Fri, 29 May 2026 09:51:32 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:03 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-10-seanjc@google.com> Subject: [PATCH v2 09/20] KVM: x86/xen: Don't dirty track "vCPU info" page From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Give the Xen per-vCPU info page the same treatment as the per-VM shared info page, and never mark it dirty, as KVM clearly relies on userspace to assume the page is always dirty. While the page is marked dirty on writes via kvm_xen_inject_pending_events(), it's not marked dirty when written by __kvm_xen_set_evtchn_fast(). Furthermore, as was the case with the shared info page, writes in the event channel fastpath may be done without an active vCPU, e.g. when called via timer callback or irqfd injection. I.e. attempting to fix the faspath would run afoul of same issue that was fixed by commit ("KVM: x86: Fix wall clock writes in Xen shared_info not to mark page dirty"). Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index ab8e95647406..7b527a983cfc 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -679,7 +679,6 @@ void kvm_xen_inject_pending_events(struct kvm_vcpu *v) WRITE_ONCE(vi->evtchn_upcall_pending, 1); } =20 - kvm_gpc_mark_dirty_in_slot(gpc); read_unlock(&gpc->lock); =20 /* For the per-vCPU lapic vector, deliver it as MSI. */ @@ -2313,7 +2312,7 @@ void kvm_xen_init_vcpu(struct kvm_vcpu *vcpu) =20 kvm_gpc_init(&vcpu->arch.xen.runstate_cache, vcpu->kvm); kvm_gpc_init(&vcpu->arch.xen.runstate2_cache, vcpu->kvm); - kvm_gpc_init(&vcpu->arch.xen.vcpu_info_cache, vcpu->kvm); + __kvm_gpc_init(&vcpu->arch.xen.vcpu_info_cache, vcpu->kvm, true); kvm_gpc_init(&vcpu->arch.xen.vcpu_time_info_cache, vcpu->kvm); } =20 --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 288763FC5C4 for ; Fri, 29 May 2026 16:51:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073499; cv=none; b=LgEV5K8GlYXJ5dD2DFC+triVd7gxy2xB2q9gUWXTqPq0jsY/+tO2FIYU2y8UNJ43pReHLQi78kp632FpKOMF8vHP+FxY+Psiy/ig8i0SCwsgjyMbtVQzK2HUtGtkxc2IiF89WhqoDzdUvBFYCJyQlbetA8XCpZFsONcb8zwhtew= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073499; c=relaxed/simple; bh=NVsUxQn7yEnpEgTJFXM8nf5/FDwzPHZmaJDH/It9iwA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Hw+t8evpX4e7vNywz0dUj/Wf5YvjQRZ/hjQnK03U9vrv9n16pUfbVW//4j/uzfdRg8pUS9NnWD5RFQUhBpPIJlMKtZp0Vprcdzu9VPwKNZDxxYZxj4PuPaICry9H4uj9b1ofvqDmvN+czgkDp3cnS4UAUv61bwSi+b1WWU1QI+s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=IHhngFZF; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="IHhngFZF" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-82f756ebd0dso8399960b3a.1 for ; Fri, 29 May 2026 09:51:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073494; x=1780678294; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=nlnbyJQj2RcPXqGfb4WvTtzInmRvbkDj8NcLEcQU12w=; b=IHhngFZFEeRZdfh1lXlTH4H34+X2CA6zXtNAzqL9S+QaJ01RnmtMAetjuPPOsSxT0V OxQWKyk1sOTMhZrjzTE9B027iOvcg9d/n+e1G4EWUzywiwPUXJZRpiRGWQzZ4DlGLdqb HanI1cQn4UmHOy9SRh4vp1YNinRZ3d7+IrPlWipLZ9v53hvoKUxSQmy9qbE4BTJa2V0l Pqh6/F2gE8QcrLqPwPMGBPDwTB3xJSlf8lqwHHQfdm4LyViqJif8DVMMEqKA1X29Elfd bCy9jJiLvSiQc0Kf20C/CMdWUrF3u1hE7Pk5XXso84o5lQzw23dCWcN3ThzdI+kRZY+l Q7wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073494; x=1780678294; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nlnbyJQj2RcPXqGfb4WvTtzInmRvbkDj8NcLEcQU12w=; b=S2KqodsoDjAOkNUNpwmX5iD1MI16M6QQ4PMczBxHmxxpTQzq6B6AviFEu1gGRuPDSD doMXLxXTpKM0HRgh59sl2/f6cEWQPgsZeqp0z4QnMs8/cJbNkpQYtV2fQefWXjBwz7Vz 9rNr2CI6jyTZy7bRqYDz5ge1oj09haKrwiqrdEc0OPVRgtBDqkAGGO9+4P6MeJShmQk+ KGKvrdeSmwxdQcoXj+gBcU/h3NtPTXqNL3YFr+RyZxpmS+zN3iu5nLeTQMw0Q2fhbq20 X/NumezzKozCKlTBU9Lu+fL3dR/MPU9TJWGuqIF9BGwELRGNDWveqtOhIDEZDH7VhWpE dugw== X-Forwarded-Encrypted: i=1; AFNElJ93MmTqObjcfknj+qwGLmTdzKbAvp+mwaUZ294iWYvXWCGIwlt++oEccQqrpOMpU673BzflsNMTbPBrMS4=@vger.kernel.org X-Gm-Message-State: AOJu0YzsySm3hfMkEE5L9bw1Sig2xnwYSy2vl//8b0Ee19XKIzrMO27j jtUTY4S795rP7JY137nq71ZC10/T+fTTj/A4lh1bXAORqcawCto/iJPWu0BECQtcOcivGiMQX7h RYxhbvQ== X-Received: from pfbjs9.prod.google.com ([2002:a05:6a00:9189:b0:835:62a8:bbc]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:32cb:b0:82f:38:a5b2 with SMTP id d2e1a72fcca58-842255ce40emr302011b3a.40.1780073493982; Fri, 29 May 2026 09:51:33 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:04 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-11-seanjc@google.com> Subject: [PATCH v2 10/20] KVM: Move {g,p}fn <=> {g,h}pa conversion helpers to kvm_types.h From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the helpers to convert between pfns and physical addresses (for host and guest) into kvm_types.h so that they can be used throughout kvm_host.h (and any other KVM-related header) without having to worry about ordering. No functional change intended. Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 15 --------------- include/linux/kvm_types.h | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 0dc4eb78b6d9..ffbae1e6e84e 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1923,21 +1923,6 @@ hva_to_gfn_memslot(unsigned long hva, struct kvm_mem= ory_slot *slot) return slot->base_gfn + gfn_offset; } =20 -static inline gpa_t gfn_to_gpa(gfn_t gfn) -{ - return (gpa_t)gfn << PAGE_SHIFT; -} - -static inline gfn_t gpa_to_gfn(gpa_t gpa) -{ - return (gfn_t)(gpa >> PAGE_SHIFT); -} - -static inline hpa_t pfn_to_hpa(kvm_pfn_t pfn) -{ - return (hpa_t)pfn << PAGE_SHIFT; -} - static inline bool kvm_is_gpa_in_memslot(struct kvm *kvm, gpa_t gpa) { unsigned long hva =3D gfn_to_hva(kvm, gpa_to_gfn(gpa)); diff --git a/include/linux/kvm_types.h b/include/linux/kvm_types.h index e850adc3f47e..961572e102f0 100644 --- a/include/linux/kvm_types.h +++ b/include/linux/kvm_types.h @@ -6,6 +6,7 @@ #include #include #include +#include #include =20 #ifdef KVM_SUB_MODULES @@ -73,6 +74,21 @@ typedef u64 hfn_t; =20 typedef hfn_t kvm_pfn_t; =20 +static inline gpa_t gfn_to_gpa(gfn_t gfn) +{ + return (gpa_t)gfn << PAGE_SHIFT; +} + +static inline gfn_t gpa_to_gfn(gpa_t gpa) +{ + return (gfn_t)(gpa >> PAGE_SHIFT); +} + +static inline hpa_t pfn_to_hpa(kvm_pfn_t pfn) +{ + return (hpa_t)pfn << PAGE_SHIFT; +} + struct gfn_to_hva_cache { u64 generation; gpa_t gpa; --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1E2713FCB29 for ; Fri, 29 May 2026 16:51:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073500; cv=none; b=Ge5TWUmcRtWB2O7CdP0Tyg2d1Gn+dfAXfzyztk/WfPLfKWDRkxZVcdleMqmhL3k2IBOu1w1/7CyLod+VXkQ+FFlO2KaFfPK8AzX2llbfehC+igtAMoNcyk7E8Zptd57MzG2XUIH78zC1fgxLjDjT7LiWDSm9s/uCwlnmoQFKcN4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073500; c=relaxed/simple; bh=TbGTG//BRfZyO3EO5/l2ZyduEsQQgNZBpVmpI8VznlM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jz0am+r141u+AVfp7iQAJgcjFU6Aw1GI1Zyoy/ZQB4Uc8Ced6fBDofQIMUzole9RFR++JPsPHIJP72jG3hUHOGblkPkMgdiGz2uW9rAbOXX67YZGrJRyOypyQKSN5zbk1g+CUtdgSP2RPQKfxYjDiWh8A8+Bv0b8ijC9FQdzZn0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=smuRiX6C; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="smuRiX6C" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-36642d2f4deso26742855a91.3 for ; Fri, 29 May 2026 09:51:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073495; x=1780678295; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=9LTJsLO4iBomCJPsSYYSa3MedSO/a0SsIY1gx3EsAfg=; b=smuRiX6C97vCywRVBO/MV6SF2lpAXPTVJe0JePBZMIvbrNXsuD8uLKIOVQuhxzaPrq KwJjT8maYQ6JLQ1xx1kcxQ6uhenghrN/iVBChjg/NOG01fMkJCkW0h2vcL3fwjNw+Cmw TK/r1Z48YQ0MISjOShdvtPOzdXyr6WLTj2B0lhx9eUDzHQUWC8d+GqpDopO7iX7jEBlj XbF5cVAcEmIpX7wfO44P/ArFYSz48La9TT8FxNpZ0Jaw+qtRj5XRw7eMAWXimDv7FKwX WkaXdu0UmuERtIDgeTc8Z2RpEKWliA3YsPRVf8Z3s+KemnajKdkVfBV169RLvEowEu/p 7nSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073495; x=1780678295; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=9LTJsLO4iBomCJPsSYYSa3MedSO/a0SsIY1gx3EsAfg=; b=c79/TIEoBNC4nw5sUpjZxRljxNVFVsL8mxbtT6nQOPeuo50kz1B6f2+8kBb710peQC 1gNCjvp0cCVxBXUeJuA4oyebFAYxgyjiEH8yufmZ+6GNRx3NVGnSB4LYVWvu+1AUFrBl eDeXYcXF48tpFEUnv4vB/xlo5fZTlZc2cyMUB+/nPn0KUnqqay8bdVWnUGikBgeq/0l8 Z31t3s4EMd0QjJ9w/tCBiMnD1OHyuuhlgN0gkSDZQKwiA/UXkNhZidUBLPueF9FNDU4n 32vB27Vg2yYIKm5gf42zfDg3ANZ8p1SPXHejCAmxmEpxgSuvwZw0Ttf+2hzfW2VIdjiA ptjA== X-Forwarded-Encrypted: i=1; AFNElJ+bfPLPkcp/j2+9GEPY8r1atJJIU/8ejQyVdI+FR9DqJ5RR1IUnt8iyzopXGuGF0wyn7NUGA4gvVJMO8+s=@vger.kernel.org X-Gm-Message-State: AOJu0YzYtdpNk2ncHXXwcHnHEPgrFqgACky4bZ0QK+x/3282oI2ME09b ix3cbPeuHmrwvaatlN7fOCPdrTzwdbz7e8Mh8HATL9QF/sVKh7q00RGl/sVTYmiKHyTTku2hZu8 b/Z+6hw== X-Received: from pgbdr2.prod.google.com ([2002:a05:6a02:fc2:b0:c79:599a:d8e3]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:d8b:b0:3aa:c9d8:d795 with SMTP id adf61e73a8af0-3b427c15601mr88062637.10.1780073495059; Fri, 29 May 2026 09:51:35 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:05 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-12-seanjc@google.com> Subject: [PATCH v2 11/20] KVM: Add CLASS() constructs to automagically handle lock+check of gpc From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add CLASS() definitions for locally mapping a PFN given a gpc (gfn_to_pfn cache), to eventually deduplicate a number of users that do: lock(); while (!check()) { unlock(); if (refresh()) return err; lock() } ... mark_dirty(); unlock(); Implement read-only (for cases where KVM is only reading) and "try" (for use in atomic code where rwlock might sleep due to PREEMPT_RT) variations. Use "map local" as the primary terminology as the basic concept is more or less the same as kmap_local(): ensure the current CPU has a kernel mapping to the underlying memory. Convert the pvclock code as the first user, as it is straightforward and thus easier to audit for correctness. For all intents and purposes, no functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 17 ++++---------- include/linux/kvm_host.h | 48 +++++++++++++++++++++++++++++++--------- virt/kvm/pfncache.c | 36 ++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 23 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 87e99756de0a..ea10ed4ab06f 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3268,17 +3268,11 @@ static void kvm_setup_guest_pvclock(struct pvclock_= vcpu_time_info *ref_hv_clock, =20 memcpy(&hv_clock, ref_hv_clock, sizeof(hv_clock)); =20 - read_lock(&gpc->lock); - while (!kvm_gpc_check(gpc, offset + sizeof(*guest_hv_clock))) { - read_unlock(&gpc->lock); + CLASS(gpc_map_local, clock_map)(gpc, offset + sizeof(*guest_hv_clock)); + if (IS_ERR(clock_map)) + return; =20 - if (kvm_gpc_refresh(gpc, offset + sizeof(*guest_hv_clock))) - return; - - read_lock(&gpc->lock); - } - - guest_hv_clock =3D (void *)(gpc->khva + offset); + guest_hv_clock =3D *clock_map + offset; =20 /* * This VCPU is paused, but it's legal for a guest to read another @@ -3299,9 +3293,6 @@ static void kvm_setup_guest_pvclock(struct pvclock_vc= pu_time_info *ref_hv_clock, =20 guest_hv_clock->version =3D ++hv_clock.version; =20 - kvm_gpc_mark_dirty_in_slot(gpc); - read_unlock(&gpc->lock); - trace_kvm_pvclock_update(vcpu->vcpu_id, &hv_clock); } =20 diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index ffbae1e6e84e..d70fa91cda0c 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1535,6 +1535,44 @@ static inline bool kvm_gpc_is_hva_active(struct gfn_= to_pfn_cache *gpc) return gpc->active && kvm_is_error_gpa(gpc->gpa); } =20 +static inline void kvm_gpc_mark_dirty_in_slot(struct gfn_to_pfn_cache *gpc) +{ + lockdep_assert_held(&gpc->lock); + + if (!gpc->memslot || gpc->never_dirty) + return; + + mark_page_dirty_in_slot(gpc->kvm, gpc->memslot, gpa_to_gfn(gpc->gpa)); +} + +void **gpc_map_local_lock(struct gfn_to_pfn_cache *gpc, unsigned long len); +void **gpc_try_map_local_lock(struct gfn_to_pfn_cache *gpc, unsigned long = len); + +static inline void gpc_map_local_unlock(void **khva) +{ + struct gfn_to_pfn_cache *gpc =3D container_of(khva, struct gfn_to_pfn_cac= he, khva); + + kvm_gpc_mark_dirty_in_slot(gpc); + + read_unlock(&gpc->lock); +} + +static inline void gpc_map_local_unlock_ro(void **khva) +{ + read_unlock(&container_of(khva, struct gfn_to_pfn_cache, khva)->lock); +} + +#define DEFINE_GPC_CLASS(try, ro) \ +DEFINE_CLASS(gpc##try##_map_local##ro, void **, \ + if (!IS_ERR(_T)) gpc_map_local_unlock##ro(_T), \ + gpc##try##_map_local_lock(gpc, len), \ + struct gfn_to_pfn_cache *gpc, unsigned long len) \ + +DEFINE_GPC_CLASS(,); +DEFINE_GPC_CLASS(_try,); +DEFINE_GPC_CLASS(, _ro); +DEFINE_GPC_CLASS(_try, _ro); + void kvm_sigset_activate(struct kvm_vcpu *vcpu); void kvm_sigset_deactivate(struct kvm_vcpu *vcpu); =20 @@ -1930,16 +1968,6 @@ static inline bool kvm_is_gpa_in_memslot(struct kvm = *kvm, gpa_t gpa) return !kvm_is_error_hva(hva); } =20 -static inline void kvm_gpc_mark_dirty_in_slot(struct gfn_to_pfn_cache *gpc) -{ - lockdep_assert_held(&gpc->lock); - - if (!gpc->memslot || gpc->never_dirty) - return; - - mark_page_dirty_in_slot(gpc->kvm, gpc->memslot, gpa_to_gfn(gpc->gpa)); -} - enum kvm_stat_kind { KVM_STAT_VM, KVM_STAT_VCPU, diff --git a/virt/kvm/pfncache.c b/virt/kvm/pfncache.c index 9209f06c46b4..d3e02a2bac38 100644 --- a/virt/kvm/pfncache.c +++ b/virt/kvm/pfncache.c @@ -484,3 +484,39 @@ void kvm_gpc_deactivate(struct gfn_to_pfn_cache *gpc) gpc_unmap(old_pfn, old_khva); } } + +void **gpc_try_map_local_lock(struct gfn_to_pfn_cache *gpc, unsigned long = len) +{ + if (!read_trylock(&gpc->lock)) + return ERR_PTR(-EWOULDBLOCK); + + if (!kvm_gpc_check(gpc, len)) { + read_unlock(&gpc->lock); + return ERR_PTR(-EWOULDBLOCK); + } + + return &gpc->khva; +} + +void **gpc_map_local_lock(struct gfn_to_pfn_cache *gpc, unsigned long len) +{ + /* + * Yes, this is an open-coded loop. But that's just what put_user() + * does anyway. Page it in and retry the instruction. We're just a + * little more honest about it. + */ + for (;;) { + int r; + + read_lock(&gpc->lock); + + if (kvm_gpc_check(gpc, len)) + return &gpc->khva; + + read_unlock(&gpc->lock); + + r =3D kvm_gpc_refresh(gpc, len); + if (r) + return ERR_PTR(r); + } +} --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3FD953F0777 for ; Fri, 29 May 2026 16:51:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073501; cv=none; b=lVSmJ4FKZegNJRqWpcYR8/Yepwgp/VrZ1BDorDaaMnHu66wTjibG86HIJKtuZgJwIIxy3AAhC4XQkFo6TH/eL5+nJPRqYptrExJwIYbeR1mPyElHBEX2MTJUwrTqSzcIggrVvl+wHZ2nJuIFvMJux0XbxXp0qu5xslr/2sDyE3I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073501; c=relaxed/simple; bh=SeVlNuknHErmqDKUXqZSVrCJUPPszJUqPGMo15fPV5k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=O82gbFUGmgnNT/1MxA4RDThmxlV0HQ00C0ztA6izmv/O9v7XV3gos1Pu+1PBx/xJmZd4E3C5MIykgtbLz8/7sdxo+x6WYAG3FS1GCoz1Gfu3lSFhH6ypOfEVfVqGARMY7vCCIFH6g2QgHbeNmnw/JaE0lYdcStyFDn2SaIwRPCQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=qcfWfII0; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="qcfWfII0" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-36642d2f4deso26742909a91.3 for ; Fri, 29 May 2026 09:51:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073496; x=1780678296; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=HgTdIrRk7scNPvBPwk7WshkWagXictFKa90Nqw8RxOc=; b=qcfWfII0Qnhb5CcJH8mgiRabHsQcWKOR2S18uiPbLchOA8t3F/Le0rXKJK5DWJ4Hf5 RFjK/YZj5KOQTZz8mF7O6gqfnNN7JvtUZ/7rxE2KpuOgOWqx9WkN9/fhaTFXJSXki7Lu saDKOgIwlrJ/QANWc5v9xGsbbCBcDeH4/GH9EkLdwQ9/R35PO7LeXV1/2rFyVeR+b1Ef lMm/vhzedJf9CvknBZhIFSgc6fxrkR/hF1x1czZg9Fjrh8DL20zZQVQrq/JPmBawPhmc rSqNUueQRi0qIBN3hcCvHvKyLqZnhTpCyYeMuJEnn+PuBsJrM8XMoQV9srbcJ2uxVALR 4bpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073496; x=1780678296; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=HgTdIrRk7scNPvBPwk7WshkWagXictFKa90Nqw8RxOc=; b=QhJ4YnVckFdoTXtCQAGp/reuusKZ6n2tnP3W8MmXhDtZtAo9mf+s/c2z8yvRlNZkyL vgsQufnHdro60qVExlmP6zsquF/Kon6kwkvj0WSH5QrIRa0hXWej+TG7FB3dFkE2WG+t JikTfVvvZulUIiaNhoEFVE/9x4L8lO2OeUkZil9VDObjhd5U2eLtGer82Hjs7vO6X4dr S1hXE5tPZVgcaTSu//1P+AEiYUkbtgg9z4ybXC0I1q2ACr2RG26LyHrKlwi4P1xI5vA/ WWDjTLYfxblrs8rMQuDXYc6IJ5mi4ur8yqvlI3+5ZcRv/nsKupvqWAe/4TTja1TEaTnA W1fQ== X-Forwarded-Encrypted: i=1; AFNElJ88B+51I1leGU8CNz97OwBgteQWC2g7wmrd4EOopdgFoXoODdwr1HPqK1Fv+46MK0fyo3OhIFBiMzjjTek=@vger.kernel.org X-Gm-Message-State: AOJu0YxnoVVC08Fhh6l8NOREsxihHMGjLO+GA/cZng/4DeNgLMCQszH2 I83dS2RxIrrWBVlfB5MlAx/ut051btNSoOv2AekAFTAFbKDRMNOVwx1N73eQeLdBv4mpsAMnDbu lu3wp2Q== X-Received: from pgaf27.prod.google.com ([2002:a63:381b:0:b0:c79:65ab:b3b4]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:d8b:b0:3aa:c9d8:d795 with SMTP id adf61e73a8af0-3b427c15601mr88135637.10.1780073496201; Fri, 29 May 2026 09:51:36 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:06 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-13-seanjc@google.com> Subject: [PATCH v2 12/20] KVM: x86/xen: Convert kvm_xen_shared_info_init() to gpc's CLASS() APIs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use the newfangled "map local" CLASS() APIs to access the Xen shared info page via its gpc. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 7b527a983cfc..065b4c92f7ed 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -35,27 +35,19 @@ static bool kvm_xen_hcall_evtchn_send(struct kvm_vcpu *= vcpu, u64 param, u64 *r); =20 DEFINE_STATIC_KEY_DEFERRED_FALSE(kvm_xen_enabled, HZ); =20 -static int kvm_xen_shared_info_init(struct kvm *kvm) +static int __kvm_xen_shared_info_init(struct kvm *kvm) { struct gfn_to_pfn_cache *gpc =3D &kvm->arch.xen.shinfo_cache; struct pvclock_wall_clock *wc; u32 *wc_sec_hi; u32 wc_version; u64 wall_nsec; - int ret; =20 guard(srcu)(&kvm->srcu); =20 - read_lock(&gpc->lock); - while (!kvm_gpc_check(gpc, PAGE_SIZE)) { - read_unlock(&gpc->lock); - - ret =3D kvm_gpc_refresh(gpc, PAGE_SIZE); - if (ret) - return ret; - - read_lock(&gpc->lock); - } + CLASS(gpc_map_local, shinfo_map)(gpc, PAGE_SIZE); + if (IS_ERR(shinfo_map)) + return PTR_ERR(shinfo_map); =20 /* * This code mirrors kvm_write_wall_clock() except that it writes @@ -74,14 +66,14 @@ static int kvm_xen_shared_info_init(struct kvm *kvm) BUILD_BUG_ON(offsetof(struct shared_info, wc_sec_hi) !=3D 0xc0c); =20 if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) { - struct shared_info *shinfo =3D gpc->khva; + struct shared_info *shinfo =3D *shinfo_map; =20 wc_sec_hi =3D &shinfo->wc_sec_hi; wc =3D &shinfo->wc; } else #endif { - struct compat_shared_info *shinfo =3D gpc->khva; + struct compat_shared_info *shinfo =3D *shinfo_map; =20 wc_sec_hi =3D &shinfo->arch.wc_sec_hi; wc =3D &shinfo->wc; @@ -97,12 +89,20 @@ static int kvm_xen_shared_info_init(struct kvm *kvm) smp_wmb(); =20 wc->version =3D wc_version + 1; - read_unlock(&gpc->lock); - - kvm_make_all_cpus_request(kvm, KVM_REQ_MASTERCLOCK_UPDATE); return 0; } =20 +static int kvm_xen_shared_info_init(struct kvm *kvm) +{ + int r; + + r =3D __kvm_xen_shared_info_init(kvm); + if (!r) + kvm_make_all_cpus_request(kvm, KVM_REQ_MASTERCLOCK_UPDATE); + + return r; +} + void kvm_xen_inject_timer_irqs(struct kvm_vcpu *vcpu) { if (atomic_read(&vcpu->arch.xen.timer_pending) > 0) { --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 50B213FE369 for ; Fri, 29 May 2026 16:51:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073504; cv=none; b=cJBGjuR26RFG/JmZ08XE3uR4xnrOm0FoRAN/Ff6Lpx2zZCUXAwDKY3SuuedZYJpxEVOGY0h0mDPsMg/XEMKCN0G11ADFgrrkiPwUa2ChbSLZsQGoyrpZ0tkBJ3t6QrMk50Rngt9OSwHXlzlnvSYm0+P70MldpznRuu65dCIpXw8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073504; c=relaxed/simple; bh=Po2rTZ6jokeiPCNXljM27Dn/+9t8hD9odLXpV/XvtJc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hwvOj1FKGR4xo7GqMz6uWc1vlUzS6g4qY1DnY+3DypQhc6DAWJDiOIMDof/frJY6zADqVBcGpQgru/1O70I35i/DSpY3sD5GkChYKQ+n3o5qjEI9EaQz3TSrP80kcWcqPcqHCK1v5ZQAbRDrQxSNjvpxFXVr5H+L2rMKOn4vIco= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=LexjU8cz; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LexjU8cz" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2b99eb06178so68099865ad.2 for ; Fri, 29 May 2026 09:51:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073497; x=1780678297; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=d6AdMaZgWz2WUUzV0Lty2KxxxU18TOhDWUp2QLry3VI=; b=LexjU8czifiWQY7nrGRSb22ddREo7elUCSYuWWnwGM4jwkHnCk3M4/U1uccr6HPI/n apZ9auctrNAbg5W6SWHoyapsY7FKqmaxdrgzKp+TRItWi1lmeN4CnL8aHpjlTTcrzLDF DLeUPdFJU2S/op29e9lbahOauWja9ATTQr3xRByMc+bWOwkdrDPWB6gq6bZsjMs12Fq7 pC+U78gekdgmKBOpEqMDdrB2tJ32ytmPqxw7Jz3VHaQ5KxeSSXHcKJZK8mF4hVIAdAiQ 4gNLf71Q67W+9XbmHLMoE44awm8BsKgVcWNg7NNVQaccTdkQLCYAaGCKpXT4aawO6E+N +E6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073497; x=1780678297; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=d6AdMaZgWz2WUUzV0Lty2KxxxU18TOhDWUp2QLry3VI=; b=hoq8n4k8RMc+D5irIbB8Vke6Y9qovzYrnw3qngPYHEVs+UUMdIoCHSwOtRS+obvnNv r2MDWQAcN5r7CLEN6brRLaujq5X4fCFBRpmxNGUCMCkTQAPKZAIUngOcqvlTf3lDTL0i TrzcsPsf8L2aEEjQkFMuvrZJfc5F8Ujtj790Md+ICx1pGdWwFnfZ3NTVbUA5OvQ9Rnqr JGIeIfhjknODqvvUDKZhjWvxXCPeM1RxgImZSUnHVRvVRLUN4//MPfg6Wb3HxdRyJMsm jQBlQXOu9KwwQq2oW5rRudB2DnQmD2GA7xdjxlnhE1QBN/yUYoYzbt5ty1m2pnr92LIJ IeLg== X-Forwarded-Encrypted: i=1; AFNElJ+q6Ht7xyaQPbtfSRsEmlzM8qJ1Lc6SPqA9+YSo7IxGsGdKUP4XvMJvqu7YCYOrFgMR7kdeGK00jx5dLN4=@vger.kernel.org X-Gm-Message-State: AOJu0YzsraQm4DUgjZIpW3vu81w4aZe+dr7INVRaQUvB1nZ+wg+pVT6U lnwnWnUzUJoLQ9m4CSXxROlBzeDPbuT5RmTi0q3T60D4idpPjIuh11oF/u0wkK8AXERNYA5Lxw/ GuWF8qg== X-Received: from plae3.prod.google.com ([2002:a17:902:e0c3:b0:2b0:46bd:4fe5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d48b:b0:2bf:2114:ecbe with SMTP id d9443c01a7336-2bf36809f57mr7505715ad.23.1780073497347; Fri, 29 May 2026 09:51:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:07 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-14-seanjc@google.com> Subject: [PATCH v2 13/20] KVM: x86/xen: Don't bother waiting on gpc->lock in SCHEDOP_poll From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use trylock instead of waiting on the gpc->lock when locking and checking the shared info page for SCHEDOP_poll, as odds are very good that if the lock is contended, then the check will fail. This will allow using the new CLASS() APIs for local gpc mappings, without having to add a version that waits on the lock, but doesn't refresh on failure. Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 065b4c92f7ed..d9b09809e243 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -1438,7 +1438,9 @@ static bool wait_pending_event(struct kvm_vcpu *vcpu,= int nr_ports, =20 guard(srcu)(&kvm->srcu); =20 - read_lock(&gpc->lock); + if (!read_trylock(&gpc->lock)) + return ret; + if (!kvm_gpc_check(gpc, PAGE_SIZE)) goto out_rcu; =20 --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B8C9A3FF893 for ; Fri, 29 May 2026 16:51:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073503; cv=none; b=dkaYfOuhd0cqtqelq36PNtDbfr2dPevjJQvAHXXoo66tpVnuB46B/mx/FDFa5C+GkQAiXyB1y5q3sy7LHKMzrbTv12sUqR1TolVjP1wwgcHYaZX1vMLJudqXQpYAOpI55RnZVgY/LeLPjgPohYrbIX76bXTdE+3Lrjy8QS82akA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073503; c=relaxed/simple; bh=/9vK7oqgWkxOq5hWyTgPFsyB6Rd5CA1wT66CgweGhZs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=N2r5wbBpc03SoiHSXnToLubWxW8ONSbS7mD9R6MVOXHJlmm3Yc+kZzOKzxgQ0HMXaTCYlyZLfRzsb0a5lkvKwIJqdwGqVNVf8VhbhPjvatxBJ9NXcfJ888DhBBd8UHs76T5JBjwkDp2FtFqXjR+Lz1gXmoIQJJ1lZX9dLA4uwEM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=jDwaP4Ym; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="jDwaP4Ym" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2b4530a90fdso106711725ad.1 for ; Fri, 29 May 2026 09:51:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073499; x=1780678299; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=M1JXo6LdkJRwGuJhKDA/Rf+G/NM5xlKF3M5/hZnE0Xg=; b=jDwaP4Ym9fmlCJygZM8QD52oGKKp0EswUayfuL7eCbq9vAkeLuo+wH5ZLjDzVKC49k p8kYFLo39qYkeg7YVLu2PvghCYUcXUynlxfZdQDh0Ng1Wc0c0HbVg5fPzKUw4NF6KVAD 9fJMAbfP4hPbWm8jmLoQHAgX1o6lgfNgndN7RAEV01b4JKjr4oqjuQJmzBoZxSEUKlqN Ac0fwDqMDvwHfv7w8/AWA87Cv/257ER6uNHMX3i9nrzcSg/svT2tXrc8lNsfDymXuyQp SEUTfR/jE/k8XzdDTgOaFGrHSRjAS+doGFXhXZwlKFw8cIOmSNOZUfZ3MAiqUAInItNA UZ6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073499; x=1780678299; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=M1JXo6LdkJRwGuJhKDA/Rf+G/NM5xlKF3M5/hZnE0Xg=; b=UgPrMiTrHv8YW0sQqJOURz2qdx5l+8Gbanbt6Jl6L79vN7XBWtUbyA10ST9JmFfLoN IyuNj1s1veVOMyTTVNe+63tUUoPb3iOSlpRXtI8acwMGQOBmeXzEh8iewzJ/vFoh9aLQ RDSb8kBNFb+VWqN56c7XrmIgjkBT2su5q+L+JwCkgVUYRJCrdx0Oo8hp9Gv5XsIavApi AVghzPN8w9Zhnn3imN7i3jX0zeUmrxdL5AA8U+O5EIxrlIn+lY8DMYzPFMG973NduVd8 UU8Men5Npp4gP386BJfJovL7LOxBvnP1PhXunSwBHERJOu/R9MRM48bMU+R7v9ByauHe xtaw== X-Forwarded-Encrypted: i=1; AFNElJ8XBgd20JyHwoktP6KBgoUXKSS8POOi8XDpbxasn9mzUTwcKFq0uxbbYyaLykLNRe2J9AOULcIoKfOVd5U=@vger.kernel.org X-Gm-Message-State: AOJu0YyG4ttTHuoijSCoYUT28xWBHpqBNmS0l8f7t+j9tD5t3IAblDkp y4sXrGaMDAhvw46RStS/LIRfM/tTdTInpix9RoYkNYxNtqJ5RGH6UhrRcbHI9n+mMT7jq3zIuKW oEMdGDg== X-Received: from plbmi12.prod.google.com ([2002:a17:902:fccc:b0:2b0:51f0:272d]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1c6:b0:2ba:4ad9:70f6 with SMTP id d9443c01a7336-2bf36875e60mr7672905ad.31.1780073498742; Fri, 29 May 2026 09:51:38 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:08 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-15-seanjc@google.com> Subject: [PATCH v2 14/20] KVM: x86/xen: Convert wait_pending_event() to gpc's CLASS() APIs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use gpc's CLASS() interface to lock and check the shared info page when processing a SCHEDOP_poll hypercall. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index d9b09809e243..8f822acb11a4 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -1433,36 +1433,28 @@ static bool wait_pending_event(struct kvm_vcpu *vcp= u, int nr_ports, struct kvm *kvm =3D vcpu->kvm; struct gfn_to_pfn_cache *gpc =3D &kvm->arch.xen.shinfo_cache; unsigned long *pending_bits; - bool ret =3D true; int i; =20 guard(srcu)(&kvm->srcu); =20 - if (!read_trylock(&gpc->lock)) - return ret; + CLASS(gpc_try_map_local_ro, shinfo_map)(gpc, PAGE_SIZE); + if (IS_ERR(shinfo_map)) + return true; =20 - if (!kvm_gpc_check(gpc, PAGE_SIZE)) - goto out_rcu; - - ret =3D false; if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) { - struct shared_info *shinfo =3D gpc->khva; + struct shared_info *shinfo =3D *shinfo_map; pending_bits =3D (unsigned long *)&shinfo->evtchn_pending; } else { - struct compat_shared_info *shinfo =3D gpc->khva; + struct compat_shared_info *shinfo =3D *shinfo_map; pending_bits =3D (unsigned long *)&shinfo->evtchn_pending; } =20 for (i =3D 0; i < nr_ports; i++) { - if (test_bit(ports[i], pending_bits)) { - ret =3D true; - break; - } + if (test_bit(ports[i], pending_bits)) + return true; } =20 - out_rcu: - read_unlock(&gpc->lock); - return ret; + return false; } =20 static bool kvm_xen_schedop_poll(struct kvm_vcpu *vcpu, bool longmode, --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 62343400E09 for ; Fri, 29 May 2026 16:51:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073504; cv=none; b=BkpcFX8WwILEQFesJH0G3OPl9YSTgqi9a1ghJfOjXcxjJlpfjLQTt6CDM1qV/Pw69XsrsejdRsNEHEfwAJkpi8gHt/syvKEmut4a1e5tRB76lNopvv2OuXp4uMxeAiMsB5hnkZBeeeNWUOGoq2ZJAtiPpLlkO50SwM/vk1onxl4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073504; c=relaxed/simple; bh=awAqPvTjCGvtOfeH1l05uKfVpQ07Yx7AEqRp/zyuH+0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=B9HNBOUc4ZpFL67Oihat/mWi2vlhgbOqbsAu0OQolhX8VPPPAKZqCRENvXqBQ9Zaz3e/PG9k/9vnlJo/6DmuWHOeR0qC/nrvojEfHRBMDWxDPN4P5Kdyn82ZBTrs9KrrTt199Hs0Ye5SyVi2EkiMVed/fBaheL+LrAXJ4Bn0w5M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Gq/PnM/u; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Gq/PnM/u" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-c857fa2706aso170230a12.2 for ; Fri, 29 May 2026 09:51:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073500; x=1780678300; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=L2n+V0FsqyljLxYaD1QETdWONVMaFI3jjVM3JkMpLz4=; b=Gq/PnM/uii80Y9krtJYYAvxTK/6GFJJdINFVpUv/gd8iUACCznnrZHcPB6Qd2gMN8R oKNCmSSKchadZIWIkrLY1sN3bWymkvVZhOEdGhtlnTIP0+DNAbs4odlHCYTk7tWYjgqu R6EBYqxXM3PRgWdapZwF3L0/HGpzNK9HQpmB4DkZ5aVIhBepTQAlZ9DUjgX+EZGDUgAG gu00JxTNKw68uTM96YldWcatkd25UcW36ZvlyrZz/GrK8XNaTTXPaIRfcLzRsTlgc60z slc5Qa3m64IzXLRZOwsfkTOe941jndEgWPPhKNnwMtlYddxTn4QskbLooRSkmpL2mPk0 p4qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073500; x=1780678300; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=L2n+V0FsqyljLxYaD1QETdWONVMaFI3jjVM3JkMpLz4=; b=eYSBFc9TuHTDgq6Ot64+l/hALqmaIeRy1b7rMwCGiqJoHVFT3soTw8MwhnSzhse/mJ 4OUoPLw7XdNISjm+o8MCogJj961NwxW7pvkkUZW3CO7dWxM3/oHzj/yMLscEAts/aY6X rAoZFndP7qbjMg+MiYtZlcHtIdn2a+wXCwr8NKzs9JpEtyFQLUAnXvhnc2myxdjQJN2e 88k2oJBbuYWMDIhmg2YH+tq4QDo76ehwCTPN8kVwET308fJvNqCco4RwrJhFOKXaTebj 13dn3IyTtw08lO228TnolHRMMcLuRXs4ecG2WNfsD8Q9h7Hvr7ArDL/6A2napUQStDNE sC2Q== X-Forwarded-Encrypted: i=1; AFNElJ+5sWhuJceGuuqlEdHAtvfhBBU3R4sZami71CsnRjgg9uDcsZtff+I4nZMP1vRDZB/2z8kAoT6pqB29mYg=@vger.kernel.org X-Gm-Message-State: AOJu0Ywv+C021J68X+YiK2qoE2bBa5q+q+fQp2bSQkxLtOfUSl9bUQ6F NcSTzHTxd7aaxg28+dec6ilziVyURhVOu6tA1kIeG+676PHkLxjBYN8Ky8TfgkjOvEcPRXIRzEp AcIlOzg== X-Received: from pfki6.prod.google.com ([2002:a05:6a00:46:b0:82f:790c:3922]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:198f:b0:837:80a:5ab4 with SMTP id d2e1a72fcca58-8422571af4fmr275744b3a.44.1780073500104; Fri, 29 May 2026 09:51:40 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:09 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-16-seanjc@google.com> Subject: [PATCH v2 15/20] KVM: x86/xen: Convert kvm_xen_set_evtchn_fast() to gpc's CLASS() APIs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Convert the event channel fastpath to the "map local" CLASS() APIs, using the "try" variants as the faspath can't block. Note! The vcpu_info mapping is read/write, even though there is no existing call to mark the page dirty. Like Xen's shared info page, the vCPU info page is assumed to be dirty at all times, and so isn't marked dirty after every write. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 8f822acb11a4..47750316f132 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -1774,29 +1774,28 @@ static void __kvm_xen_set_evtchn_fast(struct kvm_vc= pu *vcpu, int port_word_bit) bool kick_vcpu =3D false; =20 /* Now switch to the vCPU's vcpu_info to set the index and pending_sel */ - if (!read_trylock(&gpc->lock)) { - /* - * Could not access the vcpu_info. Set the bit in-kernel and - * prod the vCPU to deliver it for itself. - */ + CLASS(gpc_try_map_local, vcpu_info_map)(gpc, sizeof(struct vcpu_info)); + + /* + * If the vcpu_info is inaccessible, set the bit in-kernel and prod the + * vCPU to deliver it for itself. + */ + if (IS_ERR(vcpu_info_map)) { if (!test_and_set_bit(port_word_bit, &vcpu->arch.xen.evtchn_pending_sel)) kick_vcpu =3D true; goto out_kick; } - if (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) { - if (!test_and_set_bit(port_word_bit, &vcpu->arch.xen.evtchn_pending_sel)) - kick_vcpu =3D true; - goto out_unlock; - } =20 if (IS_ENABLED(CONFIG_64BIT) && vcpu->kvm->arch.xen.long_mode) { - struct vcpu_info *vcpu_info =3D gpc->khva; + struct vcpu_info *vcpu_info =3D *vcpu_info_map; + if (!test_and_set_bit(port_word_bit, &vcpu_info->evtchn_pending_sel)) { WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1); kick_vcpu =3D true; } } else { - struct compat_vcpu_info *vcpu_info =3D gpc->khva; + struct compat_vcpu_info *vcpu_info =3D *vcpu_info_map; + if (!test_and_set_bit(port_word_bit, (unsigned long *)&vcpu_info->evtchn_pending_sel)) { WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1); @@ -1810,8 +1809,6 @@ static void __kvm_xen_set_evtchn_fast(struct kvm_vcpu= *vcpu, int port_word_bit) kick_vcpu =3D false; } =20 -out_unlock: - read_unlock(&gpc->lock); out_kick: if (kick_vcpu) { kvm_make_request(KVM_REQ_UNBLOCK, vcpu); @@ -1850,23 +1847,19 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *= xe, struct kvm *kvm) if (xe->port >=3D max_evtchn_port(kvm)) return -EINVAL; =20 - rc =3D -EWOULDBLOCK; - guard(srcu)(&kvm->srcu); =20 - if (!read_trylock(&gpc->lock)) - return rc; - - if (!kvm_gpc_check(gpc, PAGE_SIZE)) - goto out_unlock; + CLASS(gpc_try_map_local, shinfo_map)(gpc, PAGE_SIZE); + if (IS_ERR(shinfo_map)) + return -EWOULDBLOCK; =20 if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) { - struct shared_info *shinfo =3D gpc->khva; + struct shared_info *shinfo =3D *shinfo_map; pending_bits =3D (unsigned long *)&shinfo->evtchn_pending; mask_bits =3D (unsigned long *)&shinfo->evtchn_mask; port_word_bit =3D xe->port / 64; } else { - struct compat_shared_info *shinfo =3D gpc->khva; + struct compat_shared_info *shinfo =3D *shinfo_map; pending_bits =3D (unsigned long *)&shinfo->evtchn_pending; mask_bits =3D (unsigned long *)&shinfo->evtchn_mask; port_word_bit =3D xe->port / 32; @@ -1888,9 +1881,6 @@ int kvm_xen_set_evtchn_fast(struct kvm_xen_evtchn *xe= , struct kvm *kvm) rc =3D 1; /* Delivered to the bitmap in shared_info. */ } =20 -out_unlock: - read_unlock(&gpc->lock); - if (rc =3D=3D 1) __kvm_xen_set_evtchn_fast(vcpu, port_word_bit); return rc; --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CDEB34028D0 for ; Fri, 29 May 2026 16:51:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073506; cv=none; b=RwEc68oGwHUPBEVEu3jMGllRSn4by9OqOXXc5AYU9iBUFDp3aAt8aGsV6/L6I68klNNbKjqMLpnfjhNKFLtdwYySITiw2WonWOctBxmfj2R/ZFhwGc7zqrA/Kha8hpBrsYNdlmj8bIfmZCaieeDzVccy4KLR4OWbqvbMPhwuFeM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073506; c=relaxed/simple; bh=0HhtByV5rsxXuTBsxvldTKAWnLNz94TzjlFuF8ru/xU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MYJPE66Lt7P8H0JFSQ/+99/sLH1NE5bpxNLP+oaPcP+mWQXymePqzQowinBNg6wEIV8EQB+Twviz8W1BRof9RQms2qkTHabOaTRiTKg+sMh+0pn/17HqKOVqdqUzJ0NX4Dyh4tLx3b2TSP+hW8mAse+MtJlVbr77Y/mK6xICBgQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=YeyDWed9; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YeyDWed9" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c85759a8f33so574140a12.0 for ; Fri, 29 May 2026 09:51:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073502; x=1780678302; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=ewXhP3520EhD386XfwBnuZGOAW/5b7EnRAjGjx/ciMY=; b=YeyDWed9Wuze4XVAE7zex6WcKqMWyW4rYNB+l6jzQMi/LvzoH7NjZ3P943KiVcY36Y ZCJosSaNlHmboDWryzvkD1d/jKVh7Uuyxmpf1F+klYe2V0AHWDQ4pQ1oL5/D9sGeSTYG aKiik4Zua5/4MHREiwqKThnRnAka2KNWDGUOx5Blh2pfE+6xcXcn8VTPRt0IfEEHzX9S V7NpX59+Mnd7wcQhisetOskN08CeABeQaeuBjCXkh/eamck71BqHjWjOuQwUI+lBc0O5 AZ2+O9NoFpxNUoj+a6TrjX0+zJLQnnG282ryNmu8fMdpbWMu0PAd2QyRzk7trLu4Up8A OMsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073502; x=1780678302; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ewXhP3520EhD386XfwBnuZGOAW/5b7EnRAjGjx/ciMY=; b=s6+RushohtNzGHGLGefx44owV1vAHpd5ORpTXWx871zw61zJCyKaK0gM4V0RgZ2qSL AeY7yiScOA55mQiTvGF07S7039b7ZV1mPifUxn2dhByq5E3zCJRJ20MzG66fkw2hUzWR Ek12dtf4dCXT+ZTAw8LNIiDvERNUiixYMv0NRaz/EtLJzw1UXsh1G97J9DiL6cdx2QBd 536oAFixrtA3zcXJKVAw1QSl16EomsFGdVa6r3+DDy6gUtSNztiySFb6NGazzb901S6H LdEr012/5XEr9YE8Y3ohnNj86EhUpiL1WUmdGakF64PpruduULKhavGI2d4Pn5EzjWBj h2nw== X-Forwarded-Encrypted: i=1; AFNElJ9CLSRrO68FLLvLAkg+FCU4R1BxAgbxsgI3YnYPbrWBTBZ6HzQL9MQFob/veaNOeEQ/XvlYSV4giL/wu3g=@vger.kernel.org X-Gm-Message-State: AOJu0YxDxE4HBSSDt/urmf5LEi74rH0FbzY40+5PcKsNUUITZ+jPKsFe 11X/1MCZdIFxCopAcdDxKT1BtN5Qmf0kuw6tHsBD5jjO1QW4YiNQKFIySSfILti1ia4l1os6Ltq Dq0wb5Q== X-Received: from pgka18.prod.google.com ([2002:a63:e852:0:b0:c85:7fac:dcbd]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6492:b0:3ab:30d:c7a0 with SMTP id adf61e73a8af0-3b40fd781abmr3906703637.32.1780073501523; Fri, 29 May 2026 09:51:41 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:10 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-17-seanjc@google.com> Subject: [PATCH v2 16/20] KVM: x86/xen: Convert xen_get_guest_pvclock() to gpc's CLASS() APIs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Convert the Xen pvclock reads to the "map local" CLASS() APIs, using a read-only variants as KVM simply copying data from the pvclock. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 47750316f132..89daad3fe712 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -153,21 +153,11 @@ static int xen_get_guest_pvclock(struct kvm_vcpu *vcp= u, struct gfn_to_pfn_cache *gpc, unsigned int offset) { - int r; + CLASS(gpc_map_local_ro, pvclock_map)(gpc, offset + sizeof(*hv_clock)); + if (IS_ERR(pvclock_map)) + return PTR_ERR(pvclock_map); =20 - read_lock(&gpc->lock); - while (!kvm_gpc_check(gpc, offset + sizeof(*hv_clock))) { - read_unlock(&gpc->lock); - - r =3D kvm_gpc_refresh(gpc, offset + sizeof(*hv_clock)); - if (r) - return r; - - read_lock(&gpc->lock); - } - - memcpy(hv_clock, gpc->khva + offset, sizeof(*hv_clock)); - read_unlock(&gpc->lock); + memcpy(hv_clock, *pvclock_map + offset, sizeof(*hv_clock)); =20 /* * Sanity check TSC shift+multiplier to verify the guest's view of time --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3F98403E8B for ; Fri, 29 May 2026 16:51:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073508; cv=none; b=SfsvsaNVlbAGlZyBxSCI8Srnj3iuq+cg9tXjduPbjHXNhDri0jukrcK7DiQmzzq/YGpGSZ0VkFl6lw3JiwmPCSkfAItdxnCYm3jsmwwi9f+nadZIDLDU5ovYCtct6GR6db4fS4TN6oqd5rHYjz1kOPxAUCBfPxo1kcF2T8lzppE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073508; c=relaxed/simple; bh=eRpqhsXGVulDHWpEG+Es7s7b63lWnDs9riy9U+fNOtA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jfDVnTe8ul3gxRTbs4CzUTMhwsmDLsCIgOFC7/Fe/j8sEdrk0JZDL6jRGmOe0gSqyU98VGHusYeaCgoQqGyyuQew1GBF2MaRb81C2FWWC4b4UYudUovH7IBVAWnZr4/pyIAJoc/eZyPsXnoAbN98Y5JRYHURRMlnSIoFtChDyXg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=FlPeygS6; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FlPeygS6" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-36b9106b0fbso1452398a91.0 for ; Fri, 29 May 2026 09:51:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073503; x=1780678303; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=YdeRkiZ514PCwhPFqIiDSwP2lVsh3k3Oe44aCTYNQts=; b=FlPeygS6YtJfpApB9Hs2GkFNN2KTW57S5AVqSiAEN9UIFllasFuWNj3aD/p7o/8khX iZfS/6SEVVxJHQtKvul4kThlKo4WB4Dz+5oxpSgeDeUbeTdTXFqCes/41u43FBQD8Znx bovP9y9vJbvZXQp+6LCVsOAXaITvlIwgQWDTjtgWXgz5a2GwWGYSgJLuzBoBsyO1nbxY bgriXPkclN8d7+MZ4MWTPZ0xYKQm70ivB0QnFdMG87ZZpQlxcmjZiFEHIrNa3iuUyDgq r2athTjDbPDqZBAC0gjd5JrmLDbfB/r0wsxL1MaAuy0xgSDD0Aqo8a49RA/1QivKFh7c hT5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073503; x=1780678303; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=YdeRkiZ514PCwhPFqIiDSwP2lVsh3k3Oe44aCTYNQts=; b=ZKvRqXmj8ugOoX+pi3gVBOu6ZbFO+UM+acnnfBWCXCeWl2c09dgIr5SdnRNKFTIldE lvuLA8WErUM3blGvSEWJys077m49eEyNnRbmW+yPkKhgrwTfotz2o2IBw325ZV1fjpHm fGQKlTMeQR/YjTjVQCJubxdzM6Rk2fJ5WLglSf0b3puYFLrDlUNTqc9+If+faUvvUK23 qtr8Fd0DArcYdJfuzzafH5maLeZfJsoceTqOAFQwKQXD+OYjLU76oCtbxLAFwOzmCl27 egvaeg3mnz+n5QWbhSiuDthnVaxwlV3CcfROpzfqnir3qLSOeJ06UER34Bv6CicR627j SNfA== X-Forwarded-Encrypted: i=1; AFNElJ95yIVx8fu74RN3rofIIQoNgEs/mz1NKxTgAaOvuZc9Re71hPcNndvgYT4vCfloIPmGLt9qmOOesGiFOoo=@vger.kernel.org X-Gm-Message-State: AOJu0YwyyQeRaw+3t359mBFJLrBAoQgE2oQXmdA9KWR5Rtq4C3QGumV+ 94biNXDHAzcYPwELSzMOYrw9PAHjMsRsjTnqYSXkUuYyfKJoR7/8o7d2zc/o6hFJph5iG/Lq6g+ jZlEXGg== X-Received: from pjbem5.prod.google.com ([2002:a17:90b:145:b0:36b:d802:9795]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1fc5:b0:369:3c3d:f899 with SMTP id 98e67ed59e1d1-36beb2ad6ddmr232947a91.2.1780073502851; Fri, 29 May 2026 09:51:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:11 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-18-seanjc@google.com> Subject: [PATCH v2 17/20] KVM: x86/xen: Drop local "kick_vcpu" from __kvm_xen_set_evtchn_fast() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that the CLASS()-based gpc mappings allow for early returns, drop the local "kick_vcpu" from the event channel fastpath, and simply return early if the vCPU doesn't need to be kicked. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 89daad3fe712..2c776e475a4f 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -1761,7 +1761,6 @@ static void kvm_xen_check_poller(struct kvm_vcpu *vcp= u, int port) static void __kvm_xen_set_evtchn_fast(struct kvm_vcpu *vcpu, int port_word= _bit) { struct gfn_to_pfn_cache *gpc =3D &vcpu->arch.xen.vcpu_info_cache; - bool kick_vcpu =3D false; =20 /* Now switch to the vCPU's vcpu_info to set the index and pending_sel */ CLASS(gpc_try_map_local, vcpu_info_map)(gpc, sizeof(struct vcpu_info)); @@ -1771,39 +1770,37 @@ static void __kvm_xen_set_evtchn_fast(struct kvm_vc= pu *vcpu, int port_word_bit) * vCPU to deliver it for itself. */ if (IS_ERR(vcpu_info_map)) { - if (!test_and_set_bit(port_word_bit, &vcpu->arch.xen.evtchn_pending_sel)) - kick_vcpu =3D true; + if (test_and_set_bit(port_word_bit, &vcpu->arch.xen.evtchn_pending_sel)) + return; goto out_kick; } =20 if (IS_ENABLED(CONFIG_64BIT) && vcpu->kvm->arch.xen.long_mode) { struct vcpu_info *vcpu_info =3D *vcpu_info_map; =20 - if (!test_and_set_bit(port_word_bit, &vcpu_info->evtchn_pending_sel)) { - WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1); - kick_vcpu =3D true; - } + if (test_and_set_bit(port_word_bit, &vcpu_info->evtchn_pending_sel)) + return; + + WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1); } else { struct compat_vcpu_info *vcpu_info =3D *vcpu_info_map; =20 - if (!test_and_set_bit(port_word_bit, - (unsigned long *)&vcpu_info->evtchn_pending_sel)) { - WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1); - kick_vcpu =3D true; - } + if (test_and_set_bit(port_word_bit, + (unsigned long *)&vcpu_info->evtchn_pending_sel)) + return; + + WRITE_ONCE(vcpu_info->evtchn_upcall_pending, 1); } =20 /* For the per-vCPU lapic vector, deliver it as MSI. */ - if (kick_vcpu && vcpu->arch.xen.upcall_vector) { + if (vcpu->arch.xen.upcall_vector) { kvm_xen_inject_vcpu_vector(vcpu); - kick_vcpu =3D false; + return; } =20 out_kick: - if (kick_vcpu) { - kvm_make_request(KVM_REQ_UNBLOCK, vcpu); - kvm_vcpu_kick(vcpu); - } + kvm_make_request(KVM_REQ_UNBLOCK, vcpu); + kvm_vcpu_kick(vcpu); } =20 /* --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 43B99400E0A for ; Fri, 29 May 2026 16:51:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073510; cv=none; b=Jx2JJhsxV1FwzKV++CR34SYHnXUMB90eJsrKAI0tkM3KBptce6lFFwk0FG6R5ScNWNzH/k24285ghDBQewzKF2+m1Z7j4GV5qxnYq+liVQ8vBprE3oui2/mvXC18k0iU0cfo2+ORedZTtWoThfcLJBSHvvCAb+tieUVt5EdlYRk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073510; c=relaxed/simple; bh=mox19TzrslAZs1BtaxxWeJ01fL8LSqdy5s10jatghS4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RPfCRu/FoEbX8A2H6qY04ZPQGV3EsUgjZK7jCfVOAsK0HscboSmafioQVoX5PYQmB/nDYoipDsq4ioQXZ4On/ELLkuCrccbfq80fJ/qmajbqBhj+AJoPbvROMgAXZyoAKnO39Q11AOWM6JFdgzyNKKMs84aYLm3M3vP7jhIi3E4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PF/2jcjN; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PF/2jcjN" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-367bb9caa54so12965031a91.2 for ; Fri, 29 May 2026 09:51:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073504; x=1780678304; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=TD4h/NKvbeZX0uke90rA2Hq9F8m/UJMqfSOKDUlllno=; b=PF/2jcjNX6j2qfawaxYVMArg3OSt+0gMUMLyiOwZygm62z+pC83U5DeP/OS0YL6Nq9 h8lGXLiM+MmylwD8mQQV/o3dMwS9k4fzpy70R3J8IvD8ximrksLN1Gk6j/fLhauIDAgR 8Oueoy+yENSFEHEmf0Zrl0Y81LFBQX+dl+F4sETXGlaQyWf6qHpsyouY6doJHF13rQ58 Dg99MyEc9fK4J3kzY82LxryWX/rXef8zfHULJ/Ti4mx4ecfpNKb9vuDJAyfCuSfwI6B2 Wre621aB1Ed2pJtMkryu2v3S2fSjSWd8fs6mzcOSNDnYK0JCp0UaKInudrVBuXGrN03t Jrxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073504; x=1780678304; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TD4h/NKvbeZX0uke90rA2Hq9F8m/UJMqfSOKDUlllno=; b=fE/EE7VvkPD+BjM2s1v5dDrxNIH5wMzB7NFCXNtC10RaY7nxJetdHO5AlJeKpnWJYY AoHl4rbRKYbs6+PgF0sY2QDQjxyvEXJ3XlvNl/Z1ZCv+3ePyeip3aZzgJnxHl9/iv+jO 2NND+S9CdR5527/wcaFrhohHNxQpxqj/m8XWA1DIN2Q4+Bqca1/ckCgvkdbPrgtdvsMZ GOa524V2cTfvvODFiBQW46LKtaKfQJGluNB7uYeXAo5qE5y9YnQTAkTPOe3Z0HcqX1eq l4G1sI0WhqrYJZcawUOtFLCz5DhZ2ii06KPRmbKvOOC1+zSu4jm2nicBNFUvlalpnihu 7Qfw== X-Forwarded-Encrypted: i=1; AFNElJ/seygAuj1cPbempaC5fvTwrKkExg4BE55jW3TwGAGd6bQGRmSYrojXTcEnENk6X0uWQ8kNITLi4/48Vbk=@vger.kernel.org X-Gm-Message-State: AOJu0YzWtue5NC4dG4Nq1Dr2VEybTe+Q0JCDFmE98AQT9vS7TrL5JM0R X8ykto2u0DC8laUnXdxxdpRrWfKy8QFnYKE+C+64KBsS1TV/h2fpgtMrvnBD5PNvSX0mHt0fol0 gF9D/AA== X-Received: from pgjn13.prod.google.com ([2002:a63:e04d:0:b0:c85:1092:c503]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:35c2:b0:368:78da:803 with SMTP id 98e67ed59e1d1-36bbcaf76f7mr4327305a91.12.1780073503994; Fri, 29 May 2026 09:51:43 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:12 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-19-seanjc@google.com> Subject: [PATCH v2 18/20] KVM: x86/xen: Convert event injection to gpc's CLASS() APIs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Convert Xen event injection to the "map local" CLASS() APIs. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 2c776e475a4f..3ebde7ba5558 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -628,24 +628,12 @@ void kvm_xen_inject_pending_events(struct kvm_vcpu *v) if (!evtchn_pending_sel) return; =20 - /* - * Yes, this is an open-coded loop. But that's just what put_user() - * does anyway. Page it in and retry the instruction. We're just a - * little more honest about it. - */ - read_lock(&gpc->lock); - while (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) { - read_unlock(&gpc->lock); + CLASS(gpc_map_local, vcpu_info_map)(gpc, sizeof(struct vcpu_info)); + if (IS_ERR(vcpu_info_map)) + return; =20 - if (kvm_gpc_refresh(gpc, sizeof(struct vcpu_info))) - return; - - read_lock(&gpc->lock); - } - - /* Now gpc->khva is a valid kernel address for the vcpu_info */ if (IS_ENABLED(CONFIG_64BIT) && v->kvm->arch.xen.long_mode) { - struct vcpu_info *vi =3D gpc->khva; + struct vcpu_info *vi =3D *vcpu_info_map; =20 asm volatile(LOCK_PREFIX "orq %0, %1\n" "notq %0\n" @@ -657,7 +645,7 @@ void kvm_xen_inject_pending_events(struct kvm_vcpu *v) WRITE_ONCE(vi->evtchn_upcall_pending, 1); } else { u32 evtchn_pending_sel32 =3D evtchn_pending_sel; - struct compat_vcpu_info *vi =3D gpc->khva; + struct compat_vcpu_info *vi =3D *vcpu_info_map; =20 asm volatile(LOCK_PREFIX "orl %0, %1\n" "notl %0\n" @@ -669,8 +657,6 @@ void kvm_xen_inject_pending_events(struct kvm_vcpu *v) WRITE_ONCE(vi->evtchn_upcall_pending, 1); } =20 - read_unlock(&gpc->lock); - /* For the per-vCPU lapic vector, deliver it as MSI. */ if (v->arch.xen.upcall_vector) kvm_xen_inject_vcpu_vector(v); --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 55DB940801D for ; Fri, 29 May 2026 16:51:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073512; cv=none; b=DyRGHSeDdEtIhDHbveUQwgQ0nz1SnlJvVZzVvYPePYe3fAwz4zfj5QFGU2nO6qD+PSicY2FD7IpLzhFRPYHR/MzedA88YcvmdhGFjuvTUw/rXzb5mQky/g4McGRAZjCcm3nlcSe3DuchwoDarvXcTpBkJipKIezdQpvsp2fJ2ZE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073512; c=relaxed/simple; bh=iJIJe1fMii+CIS5HHndViwT1rBLgSNUAciekKF3V8cY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=F7SQav4ujRsjdnFH9NX+s7L1ZRhpIf7i9jVe20V58L97oj3OkBsmANdr8nPMuMiWSPH1mph6GrQD9vlzs0TV4vCBu/GVPqGdEfyUurHTqD+1vLlh/KnElskcgVelJB2974SmrJp10Md+O++0HVLfRPo1ZP2Q3Nzh1HAu9Ie1EnQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Nw7cKi6o; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Nw7cKi6o" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2bf004bf8beso29517445ad.3 for ; Fri, 29 May 2026 09:51:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073505; x=1780678305; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=K5pvfWIqzQDNhPL3FNzz/qK/cOT6WUwNj0yu/46Hxps=; b=Nw7cKi6orUrmKqtVR3RD7jOrZ8ror1TYmfGQWQad/RD2SDXVrLXO54MTMPzvqJt692 LjdVFxabSA7v88E/FUPIYT5Bacc/ngitHFzeKGZDf9VaAi4jz2i/+DKSVFuRifM8G0UR rQcny8iybyOb/0CKpd15lxdaoP39+hQ0ye6OW3eRBGNouuYbxQHu3qZpe5+J4n25V+FW D9bLDU/GWCF29y9FwjVY0MJFixjpVnE8j9LQBh1toMmmQ5AJn45g1fz6ctAhsW+bI87P 6JHnKZIGSN/tEdAmVqRJ96f1QOvzDfsr4j/bRHO1qf2qGoG8mU+HL2GsHqPFGg5+Ssw5 R44Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073505; x=1780678305; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=K5pvfWIqzQDNhPL3FNzz/qK/cOT6WUwNj0yu/46Hxps=; b=tTY7Z2ZpEhMjpCR8QcDLGsCWMfoY6K1XAY0ZSCSaIjglRLTDy/kZ/l4yb4flNN9hfp 76WfQ/PB4vah1RTYvVgW6Z5fr764cjHfbeSoQkgRZxInsD81f4z9tZqKIoy7OoZp+9v+ ehbv2HtvhLIqzBng08EKil5FTIkw7twzxDeebl9TSTSUrVrz6In0eUjWmpOFq+4BsR6m esMN0NgbbqXuAWn700MIveEthR2wMMKQib+N/liq8QUub7xn9E0rcCr4PS2Wx+u75BUu ovnJBOoa00oIN/X3GjyuixvzqcSN54qgjoKqHtTqWyuWeTsQEK/aBxOeg7FV6Kt0jT7F dJHQ== X-Forwarded-Encrypted: i=1; AFNElJ8dNVBlgHIWn1dVxf/BI0dxTK9K28GXVS5CzT3YPIJUT0YWocfjYbfi2oXS7VCYPZzYxjU7lma4UAAS2Ig=@vger.kernel.org X-Gm-Message-State: AOJu0YzPHgBJPh/zBJkUAus0jFMhebDxLJ1q4uLScqJaEiERocBssJfi bC7qLZPfA3r7GeffC9tSB4ARni8mgVQvBah1X6fSy9eN5gwBzPI0eexNW4G50RebUOr52ZWEytV uUk4WTg== X-Received: from plbks11.prod.google.com ([2002:a17:903:84b:b0:2bf:bf7:6205]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2f8d:b0:2bd:2de3:51a4 with SMTP id d9443c01a7336-2bf3682502dmr7103865ad.27.1780073505343; Fri, 29 May 2026 09:51:45 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:13 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-20-seanjc@google.com> Subject: [PATCH v2 19/20] KVM: Add "extended" gpc CLASS() APIs for sometimes-atomic cases From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend the CLASS() APIs for gpcs to allow choosing between the "normal" and the "try" versions at runtime, depending on whether or not the caller is running in atomic context. Convert the "has interrupt" helper as the first user, as it is called from IRQ context, but also needs to wait when called from non-atomic context, i.e. can't tolerate false negatives in that case. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/xen.c | 52 +++++++++++++--------------------------- include/linux/kvm_host.h | 10 ++++++++ 2 files changed, 26 insertions(+), 36 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 3ebde7ba5558..a2e88a76e8d9 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -666,7 +666,6 @@ int __kvm_xen_has_interrupt(struct kvm_vcpu *v) { struct gfn_to_pfn_cache *gpc =3D &v->arch.xen.vcpu_info_cache; bool atomic =3D in_atomic() || !task_is_running(current); - u8 rc =3D 0; =20 /* * If the global upcall vector (HVMIRQ_callback_vector) is set and @@ -676,44 +675,25 @@ int __kvm_xen_has_interrupt(struct kvm_vcpu *v) /* No need for compat handling here */ BUILD_BUG_ON(offsetof(struct vcpu_info, evtchn_upcall_pending) !=3D offsetof(struct compat_vcpu_info, evtchn_upcall_pending)); - BUILD_BUG_ON(sizeof(rc) !=3D - sizeof_field(struct vcpu_info, evtchn_upcall_pending)); - BUILD_BUG_ON(sizeof(rc) !=3D + BUILD_BUG_ON(sizeof_field(struct vcpu_info, evtchn_upcall_pending) !=3D sizeof_field(struct compat_vcpu_info, evtchn_upcall_pending)); =20 - if (atomic) { - if (!read_trylock(&gpc->lock)) - return 1; - } else { - read_lock(&gpc->lock); - } - while (!kvm_gpc_check(gpc, sizeof(struct vcpu_info))) { - read_unlock(&gpc->lock); + /* + * This function gets called from kvm_vcpu_block() after setting the + * task to TASK_INTERRUPTIBLE, to see if it needs to wake immediately + * from a HLT. So we really mustn't sleep. If the page ended up absent + * at that point, just return 1 in order to trigger an immediate wake, + * and we'll end up getting called again from a context where we *can* + * fault in the page and wait for it. + * + * For normal, non-atomic usage, nothing can be done if userspace has + * screwed up the vcpu_info mapping. No interrupts for you. + */ + CLASS(gpc_map_local_ro_ex, info_map)(gpc, sizeof(struct vcpu_info), atomi= c); + if (IS_ERR(info_map)) + return atomic ? 1 : 0; =20 - /* - * This function gets called from kvm_vcpu_block() after setting the - * task to TASK_INTERRUPTIBLE, to see if it needs to wake immediately - * from a HLT. So we really mustn't sleep. If the page ended up absent - * at that point, just return 1 in order to trigger an immediate wake, - * and we'll end up getting called again from a context where we *can* - * fault in the page and wait for it. - */ - if (atomic) - return 1; - - if (kvm_gpc_refresh(gpc, sizeof(struct vcpu_info))) { - /* - * If this failed, userspace has screwed up the - * vcpu_info mapping. No interrupts for you. - */ - return 0; - } - read_lock(&gpc->lock); - } - - rc =3D ((struct vcpu_info *)gpc->khva)->evtchn_upcall_pending; - read_unlock(&gpc->lock); - return rc; + return ((struct vcpu_info *)*info_map)->evtchn_upcall_pending; } =20 int kvm_xen_hvm_set_attr(struct kvm *kvm, struct kvm_xen_hvm_attr *data) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index d70fa91cda0c..0602d0ca731c 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1573,6 +1573,16 @@ DEFINE_GPC_CLASS(_try,); DEFINE_GPC_CLASS(, _ro); DEFINE_GPC_CLASS(_try, _ro); =20 +#define DEFINE_GPC_CLASS_EX(ro) \ +DEFINE_CLASS(gpc_map_local##ro##_ex, void **, \ + if (!IS_ERR(_T)) gpc_map_local_unlock##ro(_T), \ + atomic ? gpc_try_map_local_lock(gpc, len) : \ + gpc_map_local_lock(gpc, len), \ + struct gfn_to_pfn_cache *gpc, unsigned long len, bool atomic) + +DEFINE_GPC_CLASS_EX(); +DEFINE_GPC_CLASS_EX(_ro); + void kvm_sigset_activate(struct kvm_vcpu *vcpu); void kvm_sigset_deactivate(struct kvm_vcpu *vcpu); =20 --=20 2.54.0.823.g6e5bcc1fc9-goog From nobody Mon Jun 8 10:56:50 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F86C40960D for ; Fri, 29 May 2026 16:51:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073512; cv=none; b=lH12fWxAXEU2ALnRhhncjqSZq805pnZ70GoSxVg1+kQ8t1MUTaetYOitEpOjXD+qBHnqU4W2fmf/PpXOIguU++AkpxYFPOcMlO0AJ2aMFytE84fBGyf/ZOFiHbcgTLnfISTyppl0fl/SZc2iUxdkj9UwlIUk8ZAEJV5TIcIukmo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780073512; c=relaxed/simple; bh=CuWNGtalLog0SQTqa1JAed3YyRpBsui1oqSsQSYcxxI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=S9gBjtAioNbneyIjiVEcUwlHxFIOuUMPwaJs7tY4pdY22x1MHD8S/BEphvCQNQoNTJYcvQSBttHQQdIuywmByDSr4SjVJBgeYWpBqGiDundzWh9zyfaZK3XQRKdIUmtWpS/QAOJ1pVdJ/5XVUF/lpCem7VsUo0poKhwIhUNjUZo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cx2vjLMS; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cx2vjLMS" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2bf0453031aso28337955ad.2 for ; Fri, 29 May 2026 09:51:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780073507; x=1780678307; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:from:to:cc:subject:date :message-id:reply-to; bh=9477XMVZOaF3Qg3+6qo6AFeB6OJQib8BfZbzYj/EN3k=; b=cx2vjLMS4rahiqlOkW70cfb0GWPjduyT2SL6ZIt/2J3a7dmLhGJGV9ou/uR5P2K9so NSyI6ZH0VzTCqBqs/BLmMRazkUDx+oOjILIk/N9fcd1BUt311wFtwSdAVhfJJcmSaB7D v2wsstyYZ47LPUyruuRA740C1pxHprBcxYux/QHRuCdUm/20BYmikc6lhgh8+iqavijW rXzmKAS0VRN3wxVDir68M4A2+/O23tq6KSa0Rkn5W9SG+KPCB6EBqvLQCMytMUTQMNZr v5fd0Jd2tpi/wQYjpaixYeyf3jByBCxKi6mqIQfTjgZ3QSMIO6BJgcUTrv9qZZIvN6JD arWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780073507; x=1780678307; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=9477XMVZOaF3Qg3+6qo6AFeB6OJQib8BfZbzYj/EN3k=; b=a68v8RsSe3y+aOtnpOxBcjP+w8W93ZxjKHKnkTo0dx456oXmfA2ZR3Kuhsu3gNHfkP W8kDUHTiSw006OZ8tvYsL64/h3y4rpfJolJIu9pUsRiqNlIqsd9mFG4hx8QaGNlDk8LD nO1io00XwaC35w3QJeN6Y2i/0mqfhjJ+EZMzjrAvncWyWtK25PhDRR1kGHPFG2gXJZgB KQ24veB39HBOnLXbPNAG1HQ4UAcU4RLjbkHH7LAtMXkxmCiY7DRggkuYdO6IaTZAEvYd zhTsM+a2sBtcXOV67orOhH9IBLcEV2yNG4utk6XwQdwWZjMwP/s7BwL1j8hZYdEgcUoI R6Mw== X-Forwarded-Encrypted: i=1; AFNElJ/LUbqwWD/rxZeI/zsj2R4fUUI9mx7Ur5iEbQYAzwdKk0vi9yyZNVimuJSO2cBz8CuBKGucUKs7F4hA08E=@vger.kernel.org X-Gm-Message-State: AOJu0Yyxrr6x8tb2EZgGl2KnPqdNRYeGXtPEkcpZrjOKnLqw6yd4en/E fSZ610I4LOI1gzaMzBXMnayML2TmrMJWIxtzO8VuH9PhzXygs/H/4Hp62GwMlj/PL1U9FB8Jf+y MHzROoQ== X-Received: from plbjy14.prod.google.com ([2002:a17:903:42ce:b0:2bf:3684:64d2]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:e785:b0:2bd:d7b5:83ed with SMTP id d9443c01a7336-2bf368d10d5mr7822785ad.39.1780073506459; Fri, 29 May 2026 09:51:46 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 29 May 2026 09:51:14 -0700 In-Reply-To: <20260529165114.748639-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260529165114.748639-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.823.g6e5bcc1fc9-goog Message-ID: <20260529165114.748639-21-seanjc@google.com> Subject: [PATCH v2 20/20] KVM: x86: Use gfn_to_pfn_cache for record_steal_time From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , David Woodhouse , Paul Durrant , Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng Cc: Waiman Long , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, David Woodhouse , Sebastian Andrzej Siewior , syzbot+208f7f3e5f59c11aeb90@syzkaller.appspotmail.com, Carsten Stollmaier Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Carsten Stollmaier This largely reverts commit 7e2175ebd695 ("KVM: x86: Fix recording of guest steal time / preempted status"), which dropped the use of the gfn_to_pfn_cache because it was not integrated with the MMU notifiers at the time. That shortcoming has long since been addressed, making the GPC work correctly for this use case. Aside from cleaning up the last open-coded assembler access to user addresses and associated explicit asm exception fixups, moving back to the now-functional GPC also resolves an issue with contention on the mmap_lock with userfaultfd. The contention issue is as follows: On vcpu_run, before entering the guest, the update of the steal time information causes a page-fault if the page is not present. In our scenario, this gets handled by do_user_addr_fault() and successively handle_userfault() because the region is registered to that. Since handle_userfault() uses TASK_INTERRUPTIBLE, it is interruptible by signals. But do_user_addr_fault() then busy-retries if the pending signal is non-fatal, which leads to heavy contention of the mmap_lock. By restoring the use of GPC for accessing the guest steal time, the contention is avoided and refreshing the GPC happens when the vCPU is next scheduled. Since the gfn_to_pfn_cache gives a kernel mapping rather than a userspace HVA, accesses are now plain C instead of unsafe_put_user() et al. Use READ_ONCE()/WRITE_ONCE() to prevent the compiler from reordering or tearing the accesses, and add an smp_wmb() before the final version increment to ensure the data writes are ordered before the seqcount update =E2=80=94 the old unsafe_put_user() inline assembly act= ed as an implicit compiler barrier. In kvm_steal_time_set_preempted(), use read_trylock() instead of read_lock_irqsave() since this is called from the scheduler path where rwlock_t is not safe on PREEMPT_RT (it becomes sleepable). Since we only trylock and bail on failure, there is no risk of deadlock with an interrupt handler, so no need to disable interrupts at all. Setting the preempted flag is best-effort anyway. Signed-off-by: Carsten Stollmaier Co-developed-by: David Woodhouse Signed-off-by: David Woodhouse Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/x86.c | 122 ++++++++++++++------------------ 2 files changed, 54 insertions(+), 70 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 6ae7d539af90..9f652dcdda93 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -983,7 +983,7 @@ struct kvm_vcpu_arch { u8 preempted; u64 msr_val; u64 last_steal; - struct gfn_to_hva_cache cache; + struct gfn_to_pfn_cache cache; } st; =20 u64 l1_tsc_offset; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ea10ed4ab06f..1b27dd9ba0aa 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3728,10 +3728,8 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_service_local_tlb= _flush_requests); =20 static void record_steal_time(struct kvm_vcpu *vcpu) { - struct gfn_to_hva_cache *ghc =3D &vcpu->arch.st.cache; - struct kvm_steal_time __user *st; - struct kvm_memslots *slots; - gpa_t gpa =3D vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; + struct gfn_to_pfn_cache *gpc =3D &vcpu->arch.st.cache; + struct kvm_steal_time *st; u64 steal; u32 version; =20 @@ -3746,42 +3744,20 @@ static void record_steal_time(struct kvm_vcpu *vcpu) if (WARN_ON_ONCE(current->mm !=3D vcpu->kvm->mm)) return; =20 - slots =3D kvm_memslots(vcpu->kvm); + /* We rely on the fact that it fits in a single page. */ + BUILD_BUG_ON((sizeof(*st) - 1) & KVM_STEAL_VALID_BITS); =20 - if (unlikely(slots->generation !=3D ghc->generation || - gpa !=3D ghc->gpa || - kvm_is_error_hva(ghc->hva) || !ghc->memslot)) { - /* We rely on the fact that it fits in a single page. */ - BUILD_BUG_ON((sizeof(*st) - 1) & KVM_STEAL_VALID_BITS); + CLASS(gpc_map_local, st_map)(gpc, sizeof(*st)); + if (IS_ERR(st_map)) + return; =20 - if (kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, gpa, sizeof(*st)) || - kvm_is_error_hva(ghc->hva) || !ghc->memslot) - return; - } - - st =3D (struct kvm_steal_time __user *)ghc->hva; + st =3D *st_map; /* * Doing a TLB flush here, on the guest's behalf, can avoid * expensive IPIs. */ if (guest_pv_has(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) { - u8 st_preempted =3D 0; - int err =3D -EFAULT; - - if (!user_access_begin(st, sizeof(*st))) - return; - - asm volatile("1: xchgb %0, %2\n" - "xor %1, %1\n" - "2:\n" - _ASM_EXTABLE_UA(1b, 2b) - : "+q" (st_preempted), - "+&r" (err), - "+m" (st->preempted)); - if (err) - goto out; - - user_access_end(); + u8 st_preempted =3D xchg(&st->preempted, 0); =20 vcpu->arch.st.preempted =3D 0; =20 @@ -3789,39 +3765,30 @@ static void record_steal_time(struct kvm_vcpu *vcpu) st_preempted & KVM_VCPU_FLUSH_TLB); if (st_preempted & KVM_VCPU_FLUSH_TLB) kvm_vcpu_flush_tlb_guest(vcpu); - - if (!user_access_begin(st, sizeof(*st))) - goto dirty; } else { - if (!user_access_begin(st, sizeof(*st))) - return; - - unsafe_put_user(0, &st->preempted, out); + WRITE_ONCE(st->preempted, 0); vcpu->arch.st.preempted =3D 0; } =20 - unsafe_get_user(version, &st->version, out); + version =3D READ_ONCE(st->version); if (version & 1) version +=3D 1; /* first time write, random junk */ =20 version +=3D 1; - unsafe_put_user(version, &st->version, out); + WRITE_ONCE(st->version, version); =20 smp_wmb(); =20 - unsafe_get_user(steal, &st->steal, out); + steal =3D READ_ONCE(st->steal); steal +=3D current->sched_info.run_delay - vcpu->arch.st.last_steal; vcpu->arch.st.last_steal =3D current->sched_info.run_delay; - unsafe_put_user(steal, &st->steal, out); + WRITE_ONCE(st->steal, steal); + + smp_wmb(); =20 version +=3D 1; - unsafe_put_user(version, &st->version, out); - - out: - user_access_end(); - dirty: - mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); + WRITE_ONCE(st->version, version); } =20 /* @@ -4162,8 +4129,11 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct= msr_data *msr_info) =20 vcpu->arch.st.msr_val =3D data; =20 - if (!(data & KVM_MSR_ENABLED)) - break; + if (data & KVM_MSR_ENABLED) + kvm_gpc_activate(&vcpu->arch.st.cache, data & ~KVM_MSR_ENABLED, + sizeof(struct kvm_steal_time)); + else + kvm_gpc_deactivate(&vcpu->arch.st.cache); =20 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); =20 @@ -5231,11 +5201,8 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int c= pu) =20 static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) { - struct gfn_to_hva_cache *ghc =3D &vcpu->arch.st.cache; - struct kvm_steal_time __user *st; - struct kvm_memslots *slots; - static const u8 preempted =3D KVM_VCPU_PREEMPTED; - gpa_t gpa =3D vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS; + struct gfn_to_pfn_cache *gpc =3D &vcpu->arch.st.cache; + struct kvm_steal_time *st; =20 /* * The vCPU can be marked preempted if and only if the VM-Exit was on @@ -5260,20 +5227,32 @@ static void kvm_steal_time_set_preempted(struct kvm= _vcpu *vcpu) if (unlikely(current->mm !=3D vcpu->kvm->mm)) return; =20 - slots =3D kvm_memslots(vcpu->kvm); - - if (unlikely(slots->generation !=3D ghc->generation || - gpa !=3D ghc->gpa || - kvm_is_error_hva(ghc->hva) || !ghc->memslot)) + /* + * Use a trylock as this is called from the scheduler path (via + * kvm_sched_out), where rwlock_t is not safe on PREEMPT_RT (it + * becomes sleepable). Setting preempted is best-effort anyway; + * the old HVA-based code used copy_to_user_nofault() which could + * also silently fail. + * + * Since we only trylock and bail on failure, there is no risk of + * deadlock with an interrupt handler, so no need to disable + * interrupts. + */ + CLASS(gpc_try_map_local, st_map)(gpc, sizeof(st->preempted)); + if (IS_ERR(st_map)) return; =20 - st =3D (struct kvm_steal_time __user *)ghc->hva; - BUILD_BUG_ON(sizeof(st->preempted) !=3D sizeof(preempted)); + st =3D *st_map; + WRITE_ONCE(st->preempted, KVM_VCPU_PREEMPTED); + vcpu->arch.st.preempted =3D KVM_VCPU_PREEMPTED; +} =20 - if (!copy_to_user_nofault(&st->preempted, &preempted, sizeof(preempted))) - vcpu->arch.st.preempted =3D KVM_VCPU_PREEMPTED; - - mark_page_dirty_in_slot(vcpu->kvm, ghc->memslot, gpa_to_gfn(ghc->gpa)); +static void kvm_steal_time_reset(struct kvm_vcpu *vcpu) +{ + kvm_gpc_deactivate(&vcpu->arch.st.cache); + vcpu->arch.st.preempted =3D 0; + vcpu->arch.st.msr_val =3D 0; + vcpu->arch.st.last_steal =3D 0; } =20 void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) @@ -12819,6 +12798,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) =20 kvm_gpc_init(&vcpu->arch.pv_time, vcpu->kvm); =20 + kvm_gpc_init(&vcpu->arch.st.cache, vcpu->kvm); + if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) kvm_set_mp_state(vcpu, KVM_MP_STATE_RUNNABLE); else @@ -12926,6 +12907,8 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) kvm_clear_async_pf_completion_queue(vcpu); kvm_mmu_unload(vcpu); =20 + kvm_steal_time_reset(vcpu); + kvmclock_reset(vcpu); =20 for_each_possible_cpu(cpu) @@ -13046,7 +13029,8 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool ini= t_event) kvm_make_request(KVM_REQ_EVENT, vcpu); vcpu->arch.apf.msr_en_val =3D 0; vcpu->arch.apf.msr_int_val =3D 0; - vcpu->arch.st.msr_val =3D 0; + + kvm_steal_time_reset(vcpu); =20 kvmclock_reset(vcpu); =20 --=20 2.54.0.823.g6e5bcc1fc9-goog