From mboxrd@z Thu Jan 1 00:00:00 1970 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-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 *vcpu, int port) } } +static void __kvm_xen_set_evtchn_fast(struct kvm_vcpu *vcpu, int port_word_bit) +{ + struct gfn_to_pfn_cache *gpc = &vcpu->arch.xen.vcpu_info_cache; + bool kick_vcpu = 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 = 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 = true; + goto out_unlock; + } + + if (IS_ENABLED(CONFIG_64BIT) && vcpu->kvm->arch.xen.long_mode) { + struct vcpu_info *vcpu_info = 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 = true; + } + } else { + struct compat_vcpu_info *vcpu_info = 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 = 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 = 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 = false; int vcpu_idx, rc; vcpu_idx = 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 = 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 = &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 = 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 = true; - goto out_unlock; - } - - if (IS_ENABLED(CONFIG_64BIT) && kvm->arch.xen.long_mode) { - struct vcpu_info *vcpu_info = 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 = true; - } - } else { - struct compat_vcpu_info *vcpu_info = 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 = 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 = false; - } } - out_unlock: +out_unlock: read_unlock(&gpc->lock); - out_kick: - if (kick_vcpu) { - kvm_make_request(KVM_REQ_UNBLOCK, vcpu); - kvm_vcpu_kick(vcpu); - } + if (rc == 1) + __kvm_xen_set_evtchn_fast(vcpu, port_word_bit); return rc; } -- 2.54.0.823.g6e5bcc1fc9-goog