From nobody Fri Dec 19 14:23:30 2025 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 543402206BB for ; Mon, 19 May 2025 23:28:17 +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=1747697299; cv=none; b=KUK6YMmNrPNLJhUy6vB3joAX31OtKLC+98112nndqNvg/OQyjazYE5HQQOpfbLUcTmjzXUqEj7zR83u8ydOMDgqN/zVGDajFrSlnVDygCZRYlH+G22UIYinLHVm05NLP2M567Uo0owm1IaEBPW5/WtoK6rJS3wPoGbkyFpxjfRQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697299; c=relaxed/simple; bh=7XLMy6k3euXlqZoZIk6NOl+uE5XTQOe7T5Piz8sEjIE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=I2CDBI86qJgAGAbXZnhXNyJRhq4Lb3jgPiK8uWitOl6HixUVZywTx1VNeNGTpiYh8LGDkc1yBRvjSmfWkTgE7m1QizClb2radpzyVIM5QvZhfERgJXgJlsqMsIjAHqWLPLgNv8fvsKLdYITNVaIwtooexxDZEwIJdS+gRTeDjFE= 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=CgI3bHOw; 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="CgI3bHOw" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30ed376a927so1554477a91.0 for ; Mon, 19 May 2025 16:28:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697296; x=1748302096; 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=ZFZL/kH4WRlINTL3eamUJbEcDFBeTVdE3ERRtxKHz5s=; b=CgI3bHOwwC5ZzIt4N+CUiQ49COZPzu0fzTbaGfoVi4ePAw7JHmMjPGZHFsu+ndv6LA cCv0RSBq3zkkW5p8yp9YvtU3ddIXrBhFFTNZNPAu3uvecZdswLFTzDIqj7NKxoibwClp 9rwUuFF1aZbhCaVZ5yfuWps4kviQHMcXdRI9MwCYgDRAt0en7MMDr2vpKzaEQPo9R58P zKVgzzl/Rolwj1IlQfHOFlGcTd3er8Y2Oo50IRXKJFHJaCi4XTih+U7VNI/mdXas1zgj ++qTeriAf38RQf2XAPRX1M8n7uS/wiAyctqF5V5Q1FAD4EfjkOkydMfllE9YI7WCGk/0 ZPIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697296; x=1748302096; 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=ZFZL/kH4WRlINTL3eamUJbEcDFBeTVdE3ERRtxKHz5s=; b=hT3BtZyC4D1cETdKxP2XOjAhymg1kRyL1PqX2uoKp7U9z/DLuXXK/4k0SrdiFdMQWc FbPXbny3RaWZEEAsSsXLxM0anYwL1ojMgu/iThYDR8wf906Z75D6RcFh8GEQQeam0YWV 3S/q3OrxfWh7OlavSOwAPUGzdQZL0Qx7GF3To4VS4Q3QSCD0Y2Q+wqIDtOqwWcpGMsLq HjANwwTuCgy32r64nIxnJnf8/pAHuQhq42f/0ZUtFmpNW2pjcBk8xo/YQyQ/LkA3xae8 O2fuX6OUT83dIs1M96HfymPZ23hLn9i8tz0Z843/GV/c597HFIYliFTQEvLBshdGKyWt z5JA== X-Forwarded-Encrypted: i=1; AJvYcCWJIqhiM04ZMR/sWrTneBkBgUWMN/wyF5Eh9eNvbTVMHOUbnnjY+yHU+r1OVGbLvqcH2fm+JThM7E2EIb4=@vger.kernel.org X-Gm-Message-State: AOJu0YyIwXFLHhsxreRkKTJUKsg3iV10DLz9m5YVGiSiWe3GDItzfJ0i C2SqwLZWkz6Tpi4n/7SJ+80MGMZnxdZXAYum5VSAF2nUQc3JgtxL07WEKt7arp7AGILmgN2JzBS /tgFVmA== X-Google-Smtp-Source: AGHT+IHQHbcXZtaoWAtgRshiy3d8CBLPCDH18KzA2veAqY8K4gctlw03Pifc9NLOszqBQATvWqPErDxNwGg= X-Received: from pjk3.prod.google.com ([2002:a17:90b:5583:b0:30a:4874:5389]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2e85:b0:2ee:c30f:33c9 with SMTP id 98e67ed59e1d1-30e7e770523mr18211368a91.14.1747697296528; Mon, 19 May 2025 16:28:16 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:27:54 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-2-seanjc@google.com> Subject: [PATCH 01/15] KVM: x86: Trigger I/O APIC route rescan in kvm_arch_irq_routing_update() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Trigger the I/O APIC route rescan that's performed for a split IRQ chip after userspace updates IRQ routes in kvm_arch_irq_routing_update(), i.e. before dropping kvm->irq_lock. Calling kvm_make_all_cpus_request() under a mutex is perfectly safe, and the smp_wmb()+smp_mb__after_atomic() pair in __kvm_make_request()+kvm_check_request() ensures the new routing is visible to vCPUs prior to the request being visible to vCPUs. In all likelihood, commit b053b2aef25d ("KVM: x86: Add EOI exit bitmap inference") somewhat arbitrarily made the request outside of irq_lock to avoid holding irq_lock any longer than is strictly necessary. And then commit abdb080f7ac8 ("kvm/irqchip: kvm_arch_irq_routing_update renaming split") took the easy route of adding another arch hook instead of risking a functional change. Note, the call to synchronize_srcu_expedited() does NOT provide ordering guarantees with respect to vCPUs scanning the new routing; as above, the request infrastructure provides the necessary ordering. I.e. there's no need to wait for kvm_scan_ioapic_routes() to complete if it's actively running, because regardless of whether it grabs the old or new table, the vCPU will have another KVM_REQ_SCAN_IOAPIC pending, i.e. will rescan again and see the new mappings. Signed-off-by: Sean Christopherson Acked-by: Kai Huang --- arch/x86/kvm/irq_comm.c | 10 +++------- include/linux/kvm_host.h | 4 ---- virt/kvm/irqchip.c | 2 -- 3 files changed, 3 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/irq_comm.c b/arch/x86/kvm/irq_comm.c index d6d792b5d1bd..e2ae62ff9cc2 100644 --- a/arch/x86/kvm/irq_comm.c +++ b/arch/x86/kvm/irq_comm.c @@ -395,13 +395,6 @@ int kvm_setup_default_irq_routing(struct kvm *kvm) ARRAY_SIZE(default_routing), 0); } =20 -void kvm_arch_post_irq_routing_update(struct kvm *kvm) -{ - if (!irqchip_split(kvm)) - return; - kvm_make_scan_ioapic_request(kvm); -} - void kvm_scan_ioapic_irq(struct kvm_vcpu *vcpu, u32 dest_id, u16 dest_mode, u8 vector, unsigned long *ioapic_handled_vectors) { @@ -466,4 +459,7 @@ void kvm_arch_irq_routing_update(struct kvm *kvm) #ifdef CONFIG_KVM_HYPERV kvm_hv_irq_routing_update(kvm); #endif + + if (irqchip_split(kvm)) + kvm_make_scan_ioapic_request(kvm); } diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index c685fb417e92..963e250664d6 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1020,14 +1020,10 @@ void vcpu_put(struct kvm_vcpu *vcpu); =20 #ifdef __KVM_HAVE_IOAPIC void kvm_arch_post_irq_ack_notifier_list_update(struct kvm *kvm); -void kvm_arch_post_irq_routing_update(struct kvm *kvm); #else static inline void kvm_arch_post_irq_ack_notifier_list_update(struct kvm *= kvm) { } -static inline void kvm_arch_post_irq_routing_update(struct kvm *kvm) -{ -} #endif =20 #ifdef CONFIG_HAVE_KVM_IRQCHIP diff --git a/virt/kvm/irqchip.c b/virt/kvm/irqchip.c index 162d8ed889f2..6ccabfd32287 100644 --- a/virt/kvm/irqchip.c +++ b/virt/kvm/irqchip.c @@ -222,8 +222,6 @@ int kvm_set_irq_routing(struct kvm *kvm, kvm_arch_irq_routing_update(kvm); mutex_unlock(&kvm->irq_lock); =20 - kvm_arch_post_irq_routing_update(kvm); - synchronize_srcu_expedited(&kvm->irq_srcu); =20 new =3D old; --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:23:30 2025 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 B1B87220F2A for ; Mon, 19 May 2025 23:28:18 +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=1747697301; cv=none; b=u1n/tIzQZtxflto16VCW+42fsqFSwRQTdIscUsmSuSRoohAHMWANakfwX3YQjt38pViVhtZsIVS4gidsiy8I1MWtn2K2eikhzTDsr0n3yi3ke3x63bSK/xT8wMhry4gLonZZs6HUTFwmHWjmzx1pOeOb55bdhwe6iHJVLvrkuM0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697301; c=relaxed/simple; bh=UwuKTXN4OqCCMe+jM9zYYbs3thcrz+jQJPX8ot07JVQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Z5fjIWscL5qrRe6A7TNNNHPrDUTWJp7akdgfSyj0Z/2Cyw+Y5EpRb41PiDsWuCJnD21Scd7Xjmzu+qlEhe4XbFB1PPtq/47y4u1h9VwHKOb10J2fFIFdnYEb/6JDOUQ3NIqFkr9DJtUeme04p8z+sSZJQZjlFBX82vNYSg+SmE8= 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=K4RFK20g; 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="K4RFK20g" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30e8aec4689so3730660a91.0 for ; Mon, 19 May 2025 16:28:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697298; x=1748302098; 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=13NzYr+pbxTvviNR9IPlo9XaLmrxzIsUh0zJ2XTyH2o=; b=K4RFK20gIkQkluTweAlC2hqkCOswmV0WOeSL3ISowcfFw3tbgSbqT2M78ACrzQFi3P B4uMfRLfZAn0VrwVMqp4Oi8Z6YlszHcWgx2M4gHVSICjIRoVjVVXaNGcgFK3ZQelxhd4 ZFv3d2ZtLMEE9UhFpzb9MUO+FgvFsNwWe4j9cS1YixdaeMHAcVpIdJ5QE3DLzjonHfzM eRk+2Q9o60iWR/0kbE145g3rMDP3Pt5JVq331fkdK5UpNyBZs+HXsiiQJTAUKJbAErhV KiOvJOWVsZmroz4VyRJtbURTAQvt0+VhrILKySHOE9zumEGCAG09eh4deXNPtUqfVmTo /zpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697298; x=1748302098; 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=13NzYr+pbxTvviNR9IPlo9XaLmrxzIsUh0zJ2XTyH2o=; b=R86E2Au6fgJakta+oYRlfUdP5a0/ODitoN1rodXTMGL+a2p7sRyP8fg9HCW6B47CsQ w5gwCu8joFSqw1w/Fd3X2gPYrFGHy+9a4YV2mrPILPsbFQ7gmaaGjRr0RqGCkU88O2Fe y2bF92dYzXvcEK4jinZKOrhEAT6Yqplug7rXhgvYgLVRtmSDTkT9zFnpf0jd/yCImTTy ZKpqbQpE4Cm8fdMEnoi7Bgx7pUDPEWCB8UQebFg4XRAqenBV7QMlcoS5cEO0g/0iRyjp G+5RAmlhbdy0G71yStHXL5YOiiB4PeeMV2eOVJ6J3dAVJqUst2DkkIgtbX5eoW9M8vNU uMag== X-Forwarded-Encrypted: i=1; AJvYcCUEAhDzG6yB4DwJwiOoOBBdynBJg9knrQB/kNPt8DpKLtREbQ04qTbqUkNXk5XxMPFTrThz2itWdKrNTsM=@vger.kernel.org X-Gm-Message-State: AOJu0YwD9VU2W5mzDahWRkg4MvcGk/qE2LJrneLEbyjRlSzRImtNtEKz cuH02oA2i+tVGM3llONmVTs7EiKUJVNUOYKJc8ai9z8Eewu8i7CMVcW066CVyAUTn8E+jcIAmGT B0yYmGg== X-Google-Smtp-Source: AGHT+IG/CQnReWfNW7O8BXVLAYxKyDMuWKbAMXQaxaCMWzgNBG2DV/wpi6fbS1bYnayYJECsAoA+YL5xfrc= X-Received: from pjbpb8.prod.google.com ([2002:a17:90b:3c08:b0:2ff:852c:ceb8]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1343:b0:30e:9349:2d93 with SMTP id 98e67ed59e1d1-30e93493283mr21837747a91.28.1747697298154; Mon, 19 May 2025 16:28:18 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:27:55 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-3-seanjc@google.com> Subject: [PATCH 02/15] KVM: x86: Drop superfluous kvm_set_pic_irq() => kvm_pic_set_irq() wrapper From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop the superfluous and confusing kvm_set_pic_irq() =3D> kvm_pic_set_irq() wrapper, and instead wire up ->set() directly to its final destination. Opportunistically move the declaration kvm_pic_set_irq() to irq.h to start gathering more of the in-kernel APIC/IO-APIC logic in irq.{c,h}. No functional change intended. Signed-off-by: Sean Christopherson Acked-by: Kai Huang --- arch/x86/include/asm/kvm_host.h | 1 - arch/x86/kvm/i8259.c | 5 ++++- arch/x86/kvm/irq.h | 2 ++ arch/x86/kvm/irq_comm.c | 10 +--------- 4 files changed, 7 insertions(+), 11 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 330cdcbed1a6..f25ec3ec5ce4 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2208,7 +2208,6 @@ static inline int __kvm_irq_line_state(unsigned long = *irq_state, return !!(*irq_state); } =20 -int kvm_pic_set_irq(struct kvm_pic *pic, int irq, int irq_source_id, int l= evel); void kvm_pic_clear_all(struct kvm_pic *pic, int irq_source_id); =20 void kvm_inject_nmi(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/i8259.c b/arch/x86/kvm/i8259.c index a8fb19940975..0150aec4f523 100644 --- a/arch/x86/kvm/i8259.c +++ b/arch/x86/kvm/i8259.c @@ -185,8 +185,11 @@ void kvm_pic_update_irq(struct kvm_pic *s) pic_unlock(s); } =20 -int kvm_pic_set_irq(struct kvm_pic *s, int irq, int irq_source_id, int lev= el) +int kvm_pic_set_irq(struct kvm_kernel_irq_routing_entry *e, struct kvm *kv= m, + int irq_source_id, int level, bool line_status) { + struct kvm_pic *s =3D kvm->arch.vpic; + int irq =3D e->irqchip.pin; int ret, irq_level; =20 BUG_ON(irq < 0 || irq >=3D PIC_NUM_PINS); diff --git a/arch/x86/kvm/irq.h b/arch/x86/kvm/irq.h index 76d46b2f41dd..33dd5666b656 100644 --- a/arch/x86/kvm/irq.h +++ b/arch/x86/kvm/irq.h @@ -63,6 +63,8 @@ int kvm_pic_init(struct kvm *kvm); void kvm_pic_destroy(struct kvm *kvm); int kvm_pic_read_irq(struct kvm *kvm); void kvm_pic_update_irq(struct kvm_pic *s); +int kvm_pic_set_irq(struct kvm_kernel_irq_routing_entry *e, struct kvm *kv= m, + int irq_source_id, int level, bool line_status); =20 static inline int irqchip_split(struct kvm *kvm) { diff --git a/arch/x86/kvm/irq_comm.c b/arch/x86/kvm/irq_comm.c index e2ae62ff9cc2..64f352e7bcb0 100644 --- a/arch/x86/kvm/irq_comm.c +++ b/arch/x86/kvm/irq_comm.c @@ -27,14 +27,6 @@ #include "x86.h" #include "xen.h" =20 -static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e, - struct kvm *kvm, int irq_source_id, int level, - bool line_status) -{ - struct kvm_pic *pic =3D kvm->arch.vpic; - return kvm_pic_set_irq(pic, e->irqchip.pin, irq_source_id, level); -} - static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm, int irq_source_id, int level, bool line_status) @@ -296,7 +288,7 @@ int kvm_set_routing_entry(struct kvm *kvm, case KVM_IRQCHIP_PIC_MASTER: if (ue->u.irqchip.pin >=3D PIC_NUM_PINS / 2) return -EINVAL; - e->set =3D kvm_set_pic_irq; + e->set =3D kvm_pic_set_irq; break; case KVM_IRQCHIP_IOAPIC: if (ue->u.irqchip.pin >=3D KVM_IOAPIC_NUM_PINS) --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:23:30 2025 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 4C01A21FF51 for ; Mon, 19 May 2025 23:28:20 +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=1747697301; cv=none; b=FYF5YxYLBl5LaN3uiP19/lOUxYNFk/xRpRDJCIIFNpmrQORKu6ubLN/cM0q/0bDw2BCPMB2XYqyuAiAPZThAEkQDJhUJ8t4UrYMomXo8eaEzwA/VmQiY8YB6YsI9xJ4FyrqaFwwAujdzH2jjQ61epQvfnG7cGDQnU0mhBY4zv6w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697301; c=relaxed/simple; bh=Rqx4tIOwuvzIkuS+hhvK3QTqXtDusXXoDiyTkrhAXM0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hhpPVF8rRzvLZOAqCr7ROkl1Y5w5AHPdljv85Ao/ICS45zLQU7bKJ31Ow1geJckusTz+9QB7IrjkWVF9O38j9YKKG2f5CQM2f6oHcNu2tdhX9ubaoCUXiXAq5YIlaX7wS1+qTVpPN/kTiLeHzS/4i/GvxyzBsjW89yJ9078MjCg= 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=istqz/YY; 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="istqz/YY" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30ed0017688so1803583a91.2 for ; Mon, 19 May 2025 16:28:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697299; x=1748302099; 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=AbLQPh1fsq8VOWBYp78JIFNrZ7hflbSEfHvBODcXQ5s=; b=istqz/YYvjPBBl18Q7oAl0KF9Ld3jvAevn3xWrXCUzfZK7qRzGkP9eBH/ieNSFjwS2 sC670djpR1q+Q8dICjW7IZaodfQplLJ/cqNMOHoIKtH8BMhVau72PQtdczSk7ydNXSDw boV16c4jKbhn2jkPYRVNKRQzeo+Op+mDRGFEUthH4nsUMlb843CbNJE649ql0kkQcuuL I6v2MbD2ZzlROtbOySeBCJmcBD1FAwlWJfQncTjziBrNPEMw0IGobPRjf8x4e0V7HgU8 TC6mHUa5h9sVkYJ3hQLAwMfzOly/a5jKIsTJinq8NrlTYkBIJDCJv9ozZOG31K+nyJcT OGdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697299; x=1748302099; 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=AbLQPh1fsq8VOWBYp78JIFNrZ7hflbSEfHvBODcXQ5s=; b=PfYLiItz56lyvoUpORl1Wapjjr2VaNVLdyQs1S0WLGv/do0tIukutmyub7BYEx0NrL hXoxaDHwQu2+WhVZetH3UxK3Z9EtXhbiTNDSKRVt02U5kA21gOZ6dZaqXViPYlrpQbBU GIdrqVQcsR9zF9zvWkMlc7AlOqBh1GpxE8WeoMmprCScJXjm1ycaZ+oEb/W839DjB1RU Sykr2M7rrNVGlAnJQ16NbGcln9NFfx82zVzkJIsodwmYC5ObygKIR0aS3wPUFYmzk07v va36lzUz8G46wK0nz7iO2WcaUa0wKCFrmBNnVJ5UbiQRru978k5/vzwI8damNfvaF/Zz hIxg== X-Forwarded-Encrypted: i=1; AJvYcCWGwdLapVCUqueHCdv8kQRvsnEX92eFN9qBmOZVIra2Ny+/QRpIMkCxp7p3jH45ySEG8yKqVHPwcxt2shE=@vger.kernel.org X-Gm-Message-State: AOJu0YwdFtz2TfbEs8HmZ+ATT3qgi63anKRw6XTMJGtkT6mmzQuiRFwF ywUQS9Mge1G3etvN2c3k+bnnmyx3EQe3zSU9oPAPGzZgvj2gIRccBSkVf4cc9NY4u+/xk8TZMhh bC9co5g== X-Google-Smtp-Source: AGHT+IELa/OBaU4oS5x4ZbaidUXv9+BLCZojuJzU3EtQI6qSLxAolguOuiFMHAGXuQ+SyHn2BSZVs6zuCw8= X-Received: from pjbtb12.prod.google.com ([2002:a17:90b:53cc:b0:2fc:13d6:b4cb]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5251:b0:308:5273:4dee with SMTP id 98e67ed59e1d1-30e7d542b40mr21807019a91.15.1747697299584; Mon, 19 May 2025 16:28:19 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:27:56 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-4-seanjc@google.com> Subject: [PATCH 03/15] KVM: x86: Drop superfluous kvm_set_ioapic_irq() => kvm_ioapic_set_irq() wrapper From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop the superfluous and confusing kvm_set_ioapic_irq() and instead wire up ->set() directly to its final destination. No functional change intended. Signed-off-by: Sean Christopherson Acked-by: Kai Huang --- arch/x86/kvm/ioapic.c | 6 ++++-- arch/x86/kvm/ioapic.h | 5 +++-- arch/x86/kvm/irq_comm.c | 11 +---------- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/arch/x86/kvm/ioapic.c b/arch/x86/kvm/ioapic.c index 45dae2d5d2f1..8c8a8062eb19 100644 --- a/arch/x86/kvm/ioapic.c +++ b/arch/x86/kvm/ioapic.c @@ -479,9 +479,11 @@ static int ioapic_service(struct kvm_ioapic *ioapic, i= nt irq, bool line_status) return ret; } =20 -int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_= id, - int level, bool line_status) +int kvm_ioapic_set_irq(struct kvm_kernel_irq_routing_entry *e, struct kvm = *kvm, + int irq_source_id, int level, bool line_status) { + struct kvm_ioapic *ioapic =3D kvm->arch.vioapic; + int irq =3D e->irqchip.pin; int ret, irq_level; =20 BUG_ON(irq < 0 || irq >=3D IOAPIC_NUM_PINS); diff --git a/arch/x86/kvm/ioapic.h b/arch/x86/kvm/ioapic.h index aa8cb4ac0479..a86f59bbea44 100644 --- a/arch/x86/kvm/ioapic.h +++ b/arch/x86/kvm/ioapic.h @@ -111,8 +111,9 @@ void kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, int v= ector, int trigger_mode); int kvm_ioapic_init(struct kvm *kvm); void kvm_ioapic_destroy(struct kvm *kvm); -int kvm_ioapic_set_irq(struct kvm_ioapic *ioapic, int irq, int irq_source_= id, - int level, bool line_status); +int kvm_ioapic_set_irq(struct kvm_kernel_irq_routing_entry *e, struct kvm = *kvm, + int irq_source_id, int level, bool line_status); + void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id); void kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state); void kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state); diff --git a/arch/x86/kvm/irq_comm.c b/arch/x86/kvm/irq_comm.c index 64f352e7bcb0..8dcb6a555902 100644 --- a/arch/x86/kvm/irq_comm.c +++ b/arch/x86/kvm/irq_comm.c @@ -27,15 +27,6 @@ #include "x86.h" #include "xen.h" =20 -static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, - struct kvm *kvm, int irq_source_id, int level, - bool line_status) -{ - struct kvm_ioapic *ioapic =3D kvm->arch.vioapic; - return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, irq_source_id, level, - line_status); -} - int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, struct kvm_lapic_irq *irq, struct dest_map *dest_map) { @@ -293,7 +284,7 @@ int kvm_set_routing_entry(struct kvm *kvm, case KVM_IRQCHIP_IOAPIC: if (ue->u.irqchip.pin >=3D KVM_IOAPIC_NUM_PINS) return -EINVAL; - e->set =3D kvm_set_ioapic_irq; + e->set =3D kvm_ioapic_set_irq; break; default: return -EINVAL; --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:23:30 2025 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 152E1221289 for ; Mon, 19 May 2025 23:28:21 +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=1747697303; cv=none; b=ibut6u4Z7+csYfoCM/TV6X20kTwlKAoV/cOv+mc7ZEXqxR8dbgM4baZHNkFb3WUfA50aaBxCDPihcHge7d4DOF93QzeSjHKRg7u4tKnJBp8C3mS64mNBHW/rMlV+hRcCF1AThQ2/D4T4Fz9YPCsmsMTWBt8rsNseBuN/mH6Jc60= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697303; c=relaxed/simple; bh=TZGr2td1h33ko9T6IYN3lnbl4ki862/9brDQeg1ijGk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=N/64plZvu9RuKUuSL+eORaQdOemB4UMG4mZw262dYSjonURI9yrj/P25GdazHfxUvwt/7KERk26+ce9ADtA0f9L1kTx6Ou0/xVB02ybCr3gY5bt0QwMX95ChOulsJKCH4fQubgOVvsL9S6+xirFtvhkOd/TKUrtAnavWvFibMdY= 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=ys2gxZKo; 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="ys2gxZKo" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b0e5f28841dso3415073a12.2 for ; Mon, 19 May 2025 16:28:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697301; x=1748302101; 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=EcRbALdwTeGNvoC4govCYju6evjp/u15dHDXwG2KWSs=; b=ys2gxZKowIPfHh64xpMd0Y3c9QvzTQCgY0CQCfX5z9QSu5L11V0aY60T5dqnx2ZDmL c3ZYYpnanQ6KmAiDdpgZBiw3OMNCvAQWsWCIHVp3WS+phjxUz6VJkA6LmByLcY1NZq3R 5/uh4KosuEGEnNyieSkoJpX8sHy400v1v7uGGUu+Z7vlBpgBew683oDV086C81pYUwl2 MngmvTy3BclJmkypwU5DDbGy6EvCFuf9y7sDeyqOu+9zqTKsvunm7OvTgqA1UFqXfOK3 H3VP1QWiMI4F/qfy4nPChkymmKYri6eSRWcVQ1QXr5LnY1zjAIkE6/YN1EzaCzVQJ/dC I//g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697301; x=1748302101; 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=EcRbALdwTeGNvoC4govCYju6evjp/u15dHDXwG2KWSs=; b=tngg0dnOKckMWvKRU57D4Fb1GF6+f1C4m9volZ05plCCdQzHhTwrXmByRWaIU1gmru Y6CfWWRhbwbAP7QFPYr171dTJhDJnT4EsEgC2o//E5Rq95LYEja1JdM/Y7w1WSiv9e6N DFhanxN6/icf94knbXISudJF6tP0ACaPZ+DyYu8JooLkCGC2aopX5iFzFXS/Qq2OHPOI yNfkQ6GOu34gp4W5y5RuQ56/keikipTSvNRBx4DN2XU818SjCHn++sSqIH1q1Bydye3F KiWuEGN4qo3ew0Gny13Xu69dgp0VVfBEER9CYHKuTq6zHZkrT1kg/LDUNT2IczWPA8Cy XYyA== X-Forwarded-Encrypted: i=1; AJvYcCURY55a7Oasg4frFzPYujocnkXlSI2g6MEJuFw8/emIpj20dQCJe32SWt7NZtpaINrLW5369nRxD4KdnfY=@vger.kernel.org X-Gm-Message-State: AOJu0YwWUyztneNn5dfDEnStVKa7VFUSwAK2bRlRTYlyFX5bm2XrjNmH qeQ/pVoshF9UoOugd/NX4tyDjZGkR3H/QJRyBm0AWvwLkipi7TgJFVjITMY+IMsu4qjA8AgmScn Q1BCodw== X-Google-Smtp-Source: AGHT+IF7+4b7yGtntkh4zoI53SgqpcYBnPjTzm88X0tIel2RclzHU5WAM3rg5ojEKoeoo+USix/wmVCrCBc= X-Received: from pjboi16.prod.google.com ([2002:a17:90b:3a10:b0:2fe:7f7a:74b2]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:3d25:b0:1f5:8072:d7f3 with SMTP id adf61e73a8af0-216219f04f0mr23522411637.30.1747697301306; Mon, 19 May 2025 16:28:21 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:27:57 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-5-seanjc@google.com> Subject: [PATCH 04/15] KVM: x86: Drop superfluous kvm_hv_set_sint() => kvm_hv_synic_set_irq() wrapper From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop the superfluous kvm_hv_set_sint() and instead wire up ->set() directly to its final destination. No functional change intended. Signed-off-by: Sean Christopherson Acked-by: Kai Huang Reviewed-by: Vitaly Kuznetsov --- arch/x86/kvm/hyperv.c | 10 +++++++--- arch/x86/kvm/hyperv.h | 3 ++- arch/x86/kvm/irq_comm.c | 12 ------------ 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 24f0318c50d7..7f565636edde 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -497,15 +497,19 @@ static int synic_set_irq(struct kvm_vcpu_hv_synic *sy= nic, u32 sint) return ret; } =20 -int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vpidx, u32 sint) +int kvm_hv_set_sint(struct kvm_kernel_irq_routing_entry *e, struct kvm *kv= m, + int irq_source_id, int level, bool line_status) { struct kvm_vcpu_hv_synic *synic; =20 - synic =3D synic_get(kvm, vpidx); + if (!level) + return -1; + + synic =3D synic_get(kvm, e->hv_sint.vcpu); if (!synic) return -EINVAL; =20 - return synic_set_irq(synic, sint); + return synic_set_irq(synic, e->hv_sint.sint); } =20 void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector) diff --git a/arch/x86/kvm/hyperv.h b/arch/x86/kvm/hyperv.h index 913bfc96959c..4ad5a0749739 100644 --- a/arch/x86/kvm/hyperv.h +++ b/arch/x86/kvm/hyperv.h @@ -103,7 +103,8 @@ static inline bool kvm_hv_hypercall_enabled(struct kvm_= vcpu *vcpu) int kvm_hv_hypercall(struct kvm_vcpu *vcpu); =20 void kvm_hv_irq_routing_update(struct kvm *kvm); -int kvm_hv_synic_set_irq(struct kvm *kvm, u32 vcpu_id, u32 sint); +int kvm_hv_set_sint(struct kvm_kernel_irq_routing_entry *e, struct kvm *kv= m, + int irq_source_id, int level, bool line_status); void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector); int kvm_hv_activate_synic(struct kvm_vcpu *vcpu, bool dont_zero_synic_page= s); =20 diff --git a/arch/x86/kvm/irq_comm.c b/arch/x86/kvm/irq_comm.c index 8dcb6a555902..b85e4be2ddff 100644 --- a/arch/x86/kvm/irq_comm.c +++ b/arch/x86/kvm/irq_comm.c @@ -127,18 +127,6 @@ int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, return kvm_irq_delivery_to_apic(kvm, NULL, &irq, NULL); } =20 -#ifdef CONFIG_KVM_HYPERV -static int kvm_hv_set_sint(struct kvm_kernel_irq_routing_entry *e, - struct kvm *kvm, int irq_source_id, int level, - bool line_status) -{ - if (!level) - return -1; - - return kvm_hv_synic_set_irq(kvm, e->hv_sint.vcpu, e->hv_sint.sint); -} -#endif - int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e, struct kvm *kvm, int irq_source_id, int level, bool line_status) --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:23:30 2025 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 A2A05221701 for ; Mon, 19 May 2025 23:28:23 +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=1747697305; cv=none; b=KlH+NiiTA6CC7dLSIP4s3zJW1p3bjaanRG9Q2ijTdBdgaU5j6XDe7ugJuPHqzXnU86A/DurXnsyVAJFjOo7ocVnAfrb0VuVN0wmn5Gt2RHoSfh/KK8Z/28WulLi0/fxYgCmabthAgAAtjCodzTatal34Po7RDz+vXrIS2wkn77k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697305; c=relaxed/simple; bh=vB6B2/snA85bw+tHsEq5O4+hHBMmK6/WsTM7j8JDbK8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Xz5cVMJX8LCpVbMus6LZDlFMYnwnDidLdOi5grqxZRzHcPJTbUS8e2jw2yagyKObG7fmHDjujPfdXHHWr5jI+rXj9JKdy+VkQcaw88pYNH+uh/J3+Q397FWwZ2Yd7fkxYY9MWCGmCCRFrgboI5U0qJazuqwS8FUOJduvMeeUhWc= 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=DuV24uf9; 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="DuV24uf9" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b16b35ea570so4819264a12.0 for ; Mon, 19 May 2025 16:28:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697303; x=1748302103; 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=5677PAq9vSayGLryDnV5on0ZCj/5XK40V+aZo3LfDL4=; b=DuV24uf9RQ6bqZz0cjUgzt4C3nxN3Y365GF+has6q/qRkQdOmgPwWKKTxxnpwKQOqX HH2vXqdGmc4gbAOq8lhpUokK7aaGT5bc7qoAfLl7JQ9OL2w0+UG0PqYYR8xCjiFUEM4w x1gcmGFR1IbXIINAOy4DyI+l6cXlOF85xUFmQlCoGp07xoCzNMq6U4fSZcY3UkIQCQgP I5S6a07C7pqqyBMXjGmEVFIh6/u0tQY26/ZkIcn5Xukq7mGNzcq8eFJJifi6gZ6YCZFx Wj0gUtYv1savr7tCuF9yHnr1l+bj7m06A8bJCG14CbDnb+6tJsmfhIOWcIBep/aLawvc a6zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697303; x=1748302103; 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=5677PAq9vSayGLryDnV5on0ZCj/5XK40V+aZo3LfDL4=; b=kVjiW+QevXv1fjoihGRbZ/z7Txh2C4N9lpzzqDAtvDh59WGr127mENlFylBRQF0kCN G7dLnq67IkcOmqvXyvr7Tq962GZ1GzFlHWe3zD6diLD9vhEAV5wRGm30SZGJw7WyBQ2E 3aUdVm0hsEj2HvPTycLDmaZVnCfXKjAFp+uq/3ebkE/C1wRFTwTxLc+5guoAkPZcla/D lHjSlRb/6+oxh9E9cbcGvPHpdFGesj29+lzRemD9kr7HiE2PeaQNwy34VQ6S5Kai0tJ0 4rPACLv6KZ610MGvl96pDpeXPGmhvKVVX8YGX9CY2ttU1Pm0U1rHt50/DlptnnCs2QCr 3YYA== X-Forwarded-Encrypted: i=1; AJvYcCVs/+nWZZaiuNhxGI22q+Aathq7QDpZR4YoT76VvhUHRUQozyTz3xbfpVdN/MB0m+DNn3CM+JvE+5SVUIE=@vger.kernel.org X-Gm-Message-State: AOJu0YyRLivsZZZ6lEl/vJ0jf2w29yVAaACY+mGFGI9BdUpwC0z9dtC4 jtf+G/TU0zfh5Ii3T6V/dHf3rJdVAcS5MaToZn7paWqkFAy6QXp/yOJl4+T8NnS+b9b4Cpvow3D oGybjWQ== X-Google-Smtp-Source: AGHT+IHwC4AIfQlh3CqCzBFbhI4wTZl3GmqhVvJyK0nru29ZUBIQ3femWzRswMWLjDt/y62obhO+vE/Xt3k= X-Received: from pjyp5.prod.google.com ([2002:a17:90a:e705:b0:2ef:82c0:cb8d]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2c8c:b0:30e:823f:ef31 with SMTP id 98e67ed59e1d1-30e823ff071mr20730925a91.29.1747697302915; Mon, 19 May 2025 16:28:22 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:27:58 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-6-seanjc@google.com> Subject: [PATCH 05/15] KVM: x86: Fold kvm_setup_default_irq_routing() into kvm_ioapic_init() From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the default IRQ routing table used for in-kernel I/O APIC routing to ioapic.c where it belongs, and fold the call to kvm_set_irq_routing() into kvm_ioapic_init() (the call via kvm_setup_default_irq_routing() is done immediately after kvm_ioapic_init()). In addition to making it more obvious that the so called "default" routing only applies to an in-kernel I/O APIC, getting it out of irq_comm.c will allow removing irq_comm.c entirely, and will also allow for guarding KVM's in-kernel I/O APIC emulation with a Kconfig with minimal #ifdefs. No functional change intended. Signed-off-by: Sean Christopherson Acked-by: Kai Huang --- arch/x86/kvm/ioapic.c | 32 ++++++++++++++++++++++++++++++++ arch/x86/kvm/irq.h | 1 - arch/x86/kvm/irq_comm.c | 32 -------------------------------- arch/x86/kvm/x86.c | 6 ------ 4 files changed, 32 insertions(+), 39 deletions(-) diff --git a/arch/x86/kvm/ioapic.c b/arch/x86/kvm/ioapic.c index 8c8a8062eb19..dc45ea9f5b9c 100644 --- a/arch/x86/kvm/ioapic.c +++ b/arch/x86/kvm/ioapic.c @@ -710,6 +710,32 @@ static const struct kvm_io_device_ops ioapic_mmio_ops = =3D { .write =3D ioapic_mmio_write, }; =20 +#define IOAPIC_ROUTING_ENTRY(irq) \ + { .gsi =3D irq, .type =3D KVM_IRQ_ROUTING_IRQCHIP, \ + .u.irqchip =3D { .irqchip =3D KVM_IRQCHIP_IOAPIC, .pin =3D (irq) } } +#define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq) + +#define PIC_ROUTING_ENTRY(irq) \ + { .gsi =3D irq, .type =3D KVM_IRQ_ROUTING_IRQCHIP, \ + .u.irqchip =3D { .irqchip =3D SELECT_PIC(irq), .pin =3D (irq) % 8 } } +#define ROUTING_ENTRY2(irq) \ + IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq) + +static const struct kvm_irq_routing_entry default_routing[] =3D { + ROUTING_ENTRY2(0), ROUTING_ENTRY2(1), + ROUTING_ENTRY2(2), ROUTING_ENTRY2(3), + ROUTING_ENTRY2(4), ROUTING_ENTRY2(5), + ROUTING_ENTRY2(6), ROUTING_ENTRY2(7), + ROUTING_ENTRY2(8), ROUTING_ENTRY2(9), + ROUTING_ENTRY2(10), ROUTING_ENTRY2(11), + ROUTING_ENTRY2(12), ROUTING_ENTRY2(13), + ROUTING_ENTRY2(14), ROUTING_ENTRY2(15), + ROUTING_ENTRY1(16), ROUTING_ENTRY1(17), + ROUTING_ENTRY1(18), ROUTING_ENTRY1(19), + ROUTING_ENTRY1(20), ROUTING_ENTRY1(21), + ROUTING_ENTRY1(22), ROUTING_ENTRY1(23), +}; + int kvm_ioapic_init(struct kvm *kvm) { struct kvm_ioapic *ioapic; @@ -731,8 +757,14 @@ int kvm_ioapic_init(struct kvm *kvm) if (ret < 0) { kvm->arch.vioapic =3D NULL; kfree(ioapic); + return ret; } =20 + ret =3D kvm_set_irq_routing(kvm, default_routing, + ARRAY_SIZE(default_routing), 0); + if (ret) + kvm_ioapic_destroy(kvm); + return ret; } =20 diff --git a/arch/x86/kvm/irq.h b/arch/x86/kvm/irq.h index 33dd5666b656..f6134289523e 100644 --- a/arch/x86/kvm/irq.h +++ b/arch/x86/kvm/irq.h @@ -107,7 +107,6 @@ void __kvm_migrate_timers(struct kvm_vcpu *vcpu); =20 int apic_has_pending_timer(struct kvm_vcpu *vcpu); =20 -int kvm_setup_default_irq_routing(struct kvm *kvm); int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, struct kvm_lapic_irq *irq, struct dest_map *dest_map); diff --git a/arch/x86/kvm/irq_comm.c b/arch/x86/kvm/irq_comm.c index b85e4be2ddff..998c4a34d87c 100644 --- a/arch/x86/kvm/irq_comm.c +++ b/arch/x86/kvm/irq_comm.c @@ -334,38 +334,6 @@ bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct k= vm_lapic_irq *irq, } EXPORT_SYMBOL_GPL(kvm_intr_is_single_vcpu); =20 -#define IOAPIC_ROUTING_ENTRY(irq) \ - { .gsi =3D irq, .type =3D KVM_IRQ_ROUTING_IRQCHIP, \ - .u.irqchip =3D { .irqchip =3D KVM_IRQCHIP_IOAPIC, .pin =3D (irq) } } -#define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq) - -#define PIC_ROUTING_ENTRY(irq) \ - { .gsi =3D irq, .type =3D KVM_IRQ_ROUTING_IRQCHIP, \ - .u.irqchip =3D { .irqchip =3D SELECT_PIC(irq), .pin =3D (irq) % 8 } } -#define ROUTING_ENTRY2(irq) \ - IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq) - -static const struct kvm_irq_routing_entry default_routing[] =3D { - ROUTING_ENTRY2(0), ROUTING_ENTRY2(1), - ROUTING_ENTRY2(2), ROUTING_ENTRY2(3), - ROUTING_ENTRY2(4), ROUTING_ENTRY2(5), - ROUTING_ENTRY2(6), ROUTING_ENTRY2(7), - ROUTING_ENTRY2(8), ROUTING_ENTRY2(9), - ROUTING_ENTRY2(10), ROUTING_ENTRY2(11), - ROUTING_ENTRY2(12), ROUTING_ENTRY2(13), - ROUTING_ENTRY2(14), ROUTING_ENTRY2(15), - ROUTING_ENTRY1(16), ROUTING_ENTRY1(17), - ROUTING_ENTRY1(18), ROUTING_ENTRY1(19), - ROUTING_ENTRY1(20), ROUTING_ENTRY1(21), - ROUTING_ENTRY1(22), ROUTING_ENTRY1(23), -}; - -int kvm_setup_default_irq_routing(struct kvm *kvm) -{ - return kvm_set_irq_routing(kvm, default_routing, - ARRAY_SIZE(default_routing), 0); -} - void kvm_scan_ioapic_irq(struct kvm_vcpu *vcpu, u32 dest_id, u16 dest_mode, u8 vector, unsigned long *ioapic_handled_vectors) { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f9f798f286ce..4a9c252c9dab 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7118,12 +7118,6 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned in= t ioctl, unsigned long arg) goto create_irqchip_unlock; } =20 - r =3D kvm_setup_default_irq_routing(kvm); - if (r) { - kvm_ioapic_destroy(kvm); - kvm_pic_destroy(kvm); - goto create_irqchip_unlock; - } /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ smp_wmb(); kvm->arch.irqchip_mode =3D KVM_IRQCHIP_KERNEL; --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:23:30 2025 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 51E1521FF51 for ; Mon, 19 May 2025 23:28:25 +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=1747697306; cv=none; b=Ac3KZOnT46AgoxQeqN+zkYysoZUGuJntQC9Mhap96bCgYFoG3OiNc+xBJ3ATc+ekdHWoNCj0mnTkH9ntsjp3b0tHCdoJFofkCOty9zCp29DsckAGGp+In3XeymK5qgrmWH2+qPqFCQO97Iw/TNRcGJK0aieNVclRXY6WrrMaN3Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697306; c=relaxed/simple; bh=DxxAtdli8YLcSYXG2cyhc1Qhnv1N2EL2kPaPo+nEHRk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MoMnBRwvFYsrPAWN9XoIWTJUw7QkqNrju0bgVDYGagNH+lbcZHaIZFRMpTX+1PS4w0SXjVFSJjaIMuNyQIh7J2STlz7rQCa5SYbmlNooFvwk6DY3t0VxUscOVcdF0neAvYn5IgtAzVf85QTXO2i4llpF2kjldv25ylmlEVwJwNU= 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=eE1w/xdx; 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="eE1w/xdx" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b00e4358a34so2939200a12.0 for ; Mon, 19 May 2025 16:28:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697304; x=1748302104; 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=6sutRc/njb8rhVGLfAVAjC870Mmm73jxB+i6PD25/Ys=; b=eE1w/xdxnBFEKb7nugsUk+tmoivjyh4eifGZdJJBSQIqcIpsif1f3ESiqBG960QZGP tYchDgV5Tj3kQ05jd2/GXUR1AoESXtp6m4lmK9HF4qdg++oGTr4Pb6pow5WnuG00KsYS pOWjrdAk8+OH2Ndhy/+WDA0uJ4sDrGlOxOatMfJF5UE35Kd3ZcBsqAQL3v/ISwswvnPy uqFh8OxrTZEnYhGj7mktTn6ad9wLSNGM5FhE82RJXn9lUYVhy4FgRYj2f3XoO03OpC4i KHjqRuL5Ehm8PnOot+5KdLh4HmCO2qfQJez7fHesGH3K9eH20cPogctWvkort/GvnUdv LNKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697304; x=1748302104; 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=6sutRc/njb8rhVGLfAVAjC870Mmm73jxB+i6PD25/Ys=; b=HNN90DisOQXpoE5x8wie+8+Fad8e51UqGipmk3WAbzjH8CPSFTV92SEhe7Lqz46qiz MgleukxzhLyego08DZBf6xzUkP0Gi6OfpG8REhRdPvfJ2TnuTVU0hxxivK44fn1N05Tf TgrD3fLL9Ft+j8HeJ4WElvd+HJeI7hw2Ahm/k9yxEsdUk7wioZKdFavq7wbtYRGt024E k2WFZ/rfOu217wpAFDnH9pBIx0XuvhwxGSdtHVZwcPebHLIGbgI6ycMyfHFqK+k5x+wj IuX+koQmEvVeJ9W2VYZuXl4J4crwAMCNi4Z9cY96K7aqD4XI93vQBEeW+wVYSdvZIJaJ 5q1Q== X-Forwarded-Encrypted: i=1; AJvYcCWL2NpjEH73+ZkKFwsEGaY19nYith92PHYD2RAMnP194iB2tkI8YM2iJkjLLZM6WiHRHG8Kjv1rH+wVVHw=@vger.kernel.org X-Gm-Message-State: AOJu0Yx2QN9xHZxy1LhwGe9ns0JqZDixs38F/83MpFIk3Fn0LBv17RWf EuKmbmd972yglFrSEWyRa0puSglwXOZ23cKXHmA8l0Sluf9V4jKSej07IiMeVSU9vk4DDkiS5iC pw3GQVA== X-Google-Smtp-Source: AGHT+IHGPBMajla04MgxrlVwsVQli9dUbzxkAy7PKtDrGVE71mvqfTNjDQWwFxF9Xl/sQL5zU8TiFFsVVXs= X-Received: from pjbqa8.prod.google.com ([2002:a17:90b:4fc8:b0:30e:85f1:6fa5]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:534e:b0:2ef:67c2:4030 with SMTP id 98e67ed59e1d1-30e7d5b7599mr23570889a91.27.1747697304606; Mon, 19 May 2025 16:28:24 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:27:59 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-7-seanjc@google.com> Subject: [PATCH 06/15] KVM: x86: Move kvm_{request,free}_irq_source_id() to i8254.c (PIT) From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move kvm_{request,free}_irq_source_id() to i8254.c, i.e. the dedicated PIT emulation file, in anticipation of removing them entirely in favor of hardcoding the PIT's "requested" source ID (the source ID can only ever be '2', and the request can never fail). No functional change intended. Signed-off-by: Sean Christopherson Acked-by: Kai Huang --- arch/x86/kvm/i8254.c | 44 ++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/irq_comm.c | 44 ---------------------------------------- include/linux/kvm_host.h | 2 -- 3 files changed, 44 insertions(+), 46 deletions(-) diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c index 739aa6c0d0c3..2a0964c859af 100644 --- a/arch/x86/kvm/i8254.c +++ b/arch/x86/kvm/i8254.c @@ -641,6 +641,50 @@ static void kvm_pit_reset(struct kvm_pit *pit) kvm_pit_reset_reinject(pit); } =20 +static int kvm_request_irq_source_id(struct kvm *kvm) +{ + unsigned long *bitmap =3D &kvm->arch.irq_sources_bitmap; + int irq_source_id; + + mutex_lock(&kvm->irq_lock); + irq_source_id =3D find_first_zero_bit(bitmap, BITS_PER_LONG); + + if (irq_source_id >=3D BITS_PER_LONG) { + pr_warn("exhausted allocatable IRQ sources!\n"); + irq_source_id =3D -EFAULT; + goto unlock; + } + + ASSERT(irq_source_id !=3D KVM_USERSPACE_IRQ_SOURCE_ID); + ASSERT(irq_source_id !=3D KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID); + set_bit(irq_source_id, bitmap); +unlock: + mutex_unlock(&kvm->irq_lock); + + return irq_source_id; +} + +static void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id) +{ + ASSERT(irq_source_id !=3D KVM_USERSPACE_IRQ_SOURCE_ID); + ASSERT(irq_source_id !=3D KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID); + + mutex_lock(&kvm->irq_lock); + if (irq_source_id < 0 || + irq_source_id >=3D BITS_PER_LONG) { + pr_err("IRQ source ID out of range!\n"); + goto unlock; + } + clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap); + if (!irqchip_kernel(kvm)) + goto unlock; + + kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id); + kvm_pic_clear_all(kvm->arch.vpic, irq_source_id); +unlock: + mutex_unlock(&kvm->irq_lock); +} + static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask) { struct kvm_pit *pit =3D container_of(kimn, struct kvm_pit, mask_notifier); diff --git a/arch/x86/kvm/irq_comm.c b/arch/x86/kvm/irq_comm.c index 998c4a34d87c..8c827da3e3d6 100644 --- a/arch/x86/kvm/irq_comm.c +++ b/arch/x86/kvm/irq_comm.c @@ -165,50 +165,6 @@ int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_ro= uting_entry *e, return -EWOULDBLOCK; } =20 -int kvm_request_irq_source_id(struct kvm *kvm) -{ - unsigned long *bitmap =3D &kvm->arch.irq_sources_bitmap; - int irq_source_id; - - mutex_lock(&kvm->irq_lock); - irq_source_id =3D find_first_zero_bit(bitmap, BITS_PER_LONG); - - if (irq_source_id >=3D BITS_PER_LONG) { - pr_warn("exhausted allocatable IRQ sources!\n"); - irq_source_id =3D -EFAULT; - goto unlock; - } - - ASSERT(irq_source_id !=3D KVM_USERSPACE_IRQ_SOURCE_ID); - ASSERT(irq_source_id !=3D KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID); - set_bit(irq_source_id, bitmap); -unlock: - mutex_unlock(&kvm->irq_lock); - - return irq_source_id; -} - -void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id) -{ - ASSERT(irq_source_id !=3D KVM_USERSPACE_IRQ_SOURCE_ID); - ASSERT(irq_source_id !=3D KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID); - - mutex_lock(&kvm->irq_lock); - if (irq_source_id < 0 || - irq_source_id >=3D BITS_PER_LONG) { - pr_err("IRQ source ID out of range!\n"); - goto unlock; - } - clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap); - if (!irqchip_kernel(kvm)) - goto unlock; - - kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id); - kvm_pic_clear_all(kvm->arch.vpic, irq_source_id); -unlock: - mutex_unlock(&kvm->irq_lock); -} - void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq, struct kvm_irq_mask_notifier *kimn) { diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 963e250664d6..0d4506598d62 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1780,8 +1780,6 @@ void kvm_register_irq_ack_notifier(struct kvm *kvm, struct kvm_irq_ack_notifier *kian); void kvm_unregister_irq_ack_notifier(struct kvm *kvm, struct kvm_irq_ack_notifier *kian); -int kvm_request_irq_source_id(struct kvm *kvm); -void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id); bool kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args); =20 /* --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:23:30 2025 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 E9C08221F32 for ; Mon, 19 May 2025 23:28:26 +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=1747697308; cv=none; b=BUNX40Yg3D/N6wIkM3qLX1lf/qd88lxX/DT0gq0N+Yswb49qgLqCGJvwMHd5FZ+/pgomuImy2sVNwfZTIJztiwbW65roLakMij695T/9fnkoUKEXwUsU71p36TdMGLJR2Z3ITndM2AXO1y5oEVkisy55O/GCTRUjXBKJo4YP5Nk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697308; c=relaxed/simple; bh=v27lpHZToMipvRUY1lUmbKWwt8EtNtWm7nK3+oLgxfA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Cc7PlrOYxyIjG4xPlWWEnNk/CmWLUhONElKU7YxXCw8mwYsBQLAIclp0NqN3y+DwJ+5dBZZ0U8wXZLlRwekGfAgHo8oMLZM1j+J4HVRWGYn2SQHNa7BRBBZoTwwEjc/wr4zmMlF5QZ4krJTK8RWsEZZNf+wM11PpGKwqxJQz/A8= 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=1zq/cwN0; 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="1zq/cwN0" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30f0ffde3ecso1109882a91.0 for ; Mon, 19 May 2025 16:28:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697306; x=1748302106; 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=CYNfSz3c0MEIbufxqfSo89Xbj3evJh8YTPDSU4jQxx8=; b=1zq/cwN0rVSkRru9SQ2KsxsySymkGirUyyPgXRMVpxsRD+JmlatYLqUhlv3vpuWZGL 5dUWOd5w4itj+igPcFUb0kknmNvbytmKVinrMk+NuVYlPQ8escyWIfHMM+yuBZRgjdpZ 0f/+GT6u4U2Jyk/fj/jSf49CImKOzf22zC4EOCSC9lbU8ghEv5ATlTEuRLt53ocaetYU HpHIPlvC88F4auqcJdCTFgG/DdgwyCtWsfVecul02GK+3Co/7zFWMXn2i8HEa5+OtGFz gxfrE7Jnzea5lP1MRpY/cOvYMY44hBYS25mmOBg9RyrZRw+RK1gXbVDhuRztsLW6S/cf 0PsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697306; x=1748302106; 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=CYNfSz3c0MEIbufxqfSo89Xbj3evJh8YTPDSU4jQxx8=; b=WWK3nvJLImb7QbFHgfp77eqjJsdx9tvpU9hFm8viITSUE33O8fp4wH6fnbdCj0QAlw xmHp2t3bx0cnHCe5kMymDNiQdbux9sy5Zd6Dsu/EcGsCWvZdytkI5exnQE9E8yCKifa+ qio/mLN61kam/17hlo4HGNKm93AwJpHB2lkMHQyU3HZP2MMyHcpldzTKZL5jejESZ+IQ ak2/FX4dMizUeqRs5lmdumeIRdK+1YRQgXngAWgSjyvBWcecnKmZQORKAt//TE7WGgPW XV2hlPO9SU5qG3F40rXaEc3aTfc7xkWayQD5fE7hS+1aRYDi+43GJaQvhG++OhVnovvD 5O3A== X-Forwarded-Encrypted: i=1; AJvYcCUtogJ6JFjODyjPnc6nVftDnYrhST7HHZuCnZ99DZbWV22+RDYi/2EomX9aLL4YGmI2n9BjCUPtLgdKics=@vger.kernel.org X-Gm-Message-State: AOJu0YxtVjH6R2phMPoMh5JYMQ56TlmzRW1Cp7pWoxv5hZqAp/LQ7R8V bEA7U1L6XEBkbGJd9/rnqLawDRrlLu2BrDzYgmkhSz81bDz3XM/z7aRi24UO/luZT43Rha9tTRt uYlai7Q== X-Google-Smtp-Source: AGHT+IHD3d0FUo1F+its1m5Q/ZuLYZpcWvtxoKmFmQSmXNfoYPiCCl0K+3fw4yCk8ORbs3n5QKanZNe6CZM= X-Received: from pjbso6.prod.google.com ([2002:a17:90b:1f86:b0:2ee:3128:390f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3b86:b0:30e:8c5d:8f8 with SMTP id 98e67ed59e1d1-30e8c5d09b3mr23125425a91.14.1747697306228; Mon, 19 May 2025 16:28:26 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:28:00 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-8-seanjc@google.com> Subject: [PATCH 07/15] KVM: x86: Hardcode the PIT IRQ source ID to '2' From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Hardcode the PIT's source IRQ ID to '2' instead of "finding" that bit 2 is always the first available bit in irq_sources_bitmap. Bits 0 and 1 are set/reserved by kvm_arch_init_vm(), i.e. long before kvm_create_pit() can be invoked, and KVM allows at most one in-kernel PIT instance, i.e. it's impossible for the PIT to find a different free bit (there are no other users of kvm_request_irq_source_id(). Delete the now-defunct irq_sources_bitmap and all its associated code. Signed-off-by: Sean Christopherson Acked-by: Kai Huang --- arch/x86/include/asm/kvm_host.h | 1 - arch/x86/kvm/i8254.c | 55 +++++---------------------------- arch/x86/kvm/i8254.h | 1 - arch/x86/kvm/x86.c | 6 ---- include/linux/kvm_host.h | 1 + 5 files changed, 8 insertions(+), 56 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index f25ec3ec5ce4..c8654e461933 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1396,7 +1396,6 @@ struct kvm_arch { bool pause_in_guest; bool cstate_in_guest; =20 - unsigned long irq_sources_bitmap; s64 kvmclock_offset; =20 /* diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c index 2a0964c859af..d4fc20c265b2 100644 --- a/arch/x86/kvm/i8254.c +++ b/arch/x86/kvm/i8254.c @@ -248,8 +248,8 @@ static void pit_do_work(struct kthread_work *work) if (atomic_read(&ps->reinject) && !atomic_xchg(&ps->irq_ack, 0)) return; =20 - kvm_set_irq(kvm, pit->irq_source_id, 0, 1, false); - kvm_set_irq(kvm, pit->irq_source_id, 0, 0, false); + kvm_set_irq(kvm, KVM_PIT_IRQ_SOURCE_ID, 0, 1, false); + kvm_set_irq(kvm, KVM_PIT_IRQ_SOURCE_ID, 0, 0, false); =20 /* * Provides NMI watchdog support via Virtual Wire mode. @@ -641,47 +641,11 @@ static void kvm_pit_reset(struct kvm_pit *pit) kvm_pit_reset_reinject(pit); } =20 -static int kvm_request_irq_source_id(struct kvm *kvm) +static void kvm_pit_clear_all(struct kvm *kvm) { - unsigned long *bitmap =3D &kvm->arch.irq_sources_bitmap; - int irq_source_id; - mutex_lock(&kvm->irq_lock); - irq_source_id =3D find_first_zero_bit(bitmap, BITS_PER_LONG); - - if (irq_source_id >=3D BITS_PER_LONG) { - pr_warn("exhausted allocatable IRQ sources!\n"); - irq_source_id =3D -EFAULT; - goto unlock; - } - - ASSERT(irq_source_id !=3D KVM_USERSPACE_IRQ_SOURCE_ID); - ASSERT(irq_source_id !=3D KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID); - set_bit(irq_source_id, bitmap); -unlock: - mutex_unlock(&kvm->irq_lock); - - return irq_source_id; -} - -static void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id) -{ - ASSERT(irq_source_id !=3D KVM_USERSPACE_IRQ_SOURCE_ID); - ASSERT(irq_source_id !=3D KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID); - - mutex_lock(&kvm->irq_lock); - if (irq_source_id < 0 || - irq_source_id >=3D BITS_PER_LONG) { - pr_err("IRQ source ID out of range!\n"); - goto unlock; - } - clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap); - if (!irqchip_kernel(kvm)) - goto unlock; - - kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id); - kvm_pic_clear_all(kvm->arch.vpic, irq_source_id); -unlock: + kvm_ioapic_clear_all(kvm->arch.vioapic, KVM_PIT_IRQ_SOURCE_ID); + kvm_pic_clear_all(kvm->arch.vpic, KVM_PIT_IRQ_SOURCE_ID); mutex_unlock(&kvm->irq_lock); } =20 @@ -715,10 +679,6 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 fl= ags) if (!pit) return NULL; =20 - pit->irq_source_id =3D kvm_request_irq_source_id(kvm); - if (pit->irq_source_id < 0) - goto fail_request; - mutex_init(&pit->pit_state.lock); =20 pid =3D get_pid(task_tgid(current)); @@ -770,8 +730,7 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 fla= gs) kvm_pit_set_reinject(pit, false); kthread_destroy_worker(pit->worker); fail_kthread: - kvm_free_irq_source_id(kvm, pit->irq_source_id); -fail_request: + kvm_pit_clear_all(kvm); kfree(pit); return NULL; } @@ -788,7 +747,7 @@ void kvm_free_pit(struct kvm *kvm) kvm_pit_set_reinject(pit, false); hrtimer_cancel(&pit->pit_state.timer); kthread_destroy_worker(pit->worker); - kvm_free_irq_source_id(kvm, pit->irq_source_id); + kvm_pit_clear_all(kvm); kfree(pit); } } diff --git a/arch/x86/kvm/i8254.h b/arch/x86/kvm/i8254.h index a768212ba821..14fb310357f2 100644 --- a/arch/x86/kvm/i8254.h +++ b/arch/x86/kvm/i8254.h @@ -42,7 +42,6 @@ struct kvm_pit { struct kvm_io_device speaker_dev; struct kvm *kvm; struct kvm_kpit_state pit_state; - int irq_source_id; struct kvm_irq_mask_notifier mask_notifier; struct kthread_worker *worker; struct kthread_work expired; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4a9c252c9dab..9e2c249d45ca 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12790,12 +12790,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned lon= g type) INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); atomic_set(&kvm->arch.noncoherent_dma_count, 0); =20 - /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ - set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); - /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ - set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, - &kvm->arch.irq_sources_bitmap); - raw_spin_lock_init(&kvm->arch.tsc_write_lock); mutex_init(&kvm->arch.apic_map_lock); seqcount_raw_spinlock_init(&kvm->arch.pvclock_sc, &kvm->arch.tsc_write_lo= ck); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 0d4506598d62..44b439c5fcf4 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -190,6 +190,7 @@ bool kvm_make_all_cpus_request(struct kvm *kvm, unsigne= d int req); =20 #define KVM_USERSPACE_IRQ_SOURCE_ID 0 #define KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID 1 +#define KVM_PIT_IRQ_SOURCE_ID 2 =20 extern struct mutex kvm_lock; extern struct list_head vm_list; --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:23:30 2025 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 5D263221FCF for ; Mon, 19 May 2025 23:28: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=1747697309; cv=none; b=iQqVXHNa9vLq8KUcO5VQgeo9FdEARL0v84+tsOpwrF4S7yRE0HYTSo9U68s/TBYYFQkh7HIWmoGQgzAE6Deb61Ip8q+JWHjSGcbxvU2sFuWAYCtnvz7ETJvFc5ThO3Ce4QTB+LrsACI3vQrXNOUg5/WyOxo18FzLhn5mguwQGEc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697309; c=relaxed/simple; bh=EeoGiFAd9e7iVkaKUKX4cOG8l0ZAUdT5S/GdOseA0v4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BMbjA1vUijlmchY7oriEoEETkAh+vdVShGSehXEi6pSH/MbIz4d/7dCrkaEsJS+NZ4QWVBhYwkstKXat5h34Ap7HUGrLlv4lkc0U0dxsZ+JhbQ7wa1K6iRrGyxJmU6KnsH4HBlM7lBbo9OLZd6r4LLrAEOZbTbubvITxQKWpl8U= 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=r/2tP6Rp; 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="r/2tP6Rp" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30e9659a391so2619985a91.0 for ; Mon, 19 May 2025 16:28:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697308; x=1748302108; 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=k5bXYrAobPzWNfhMoyWUANBVY5VTCDdA5KxrSZD6wWU=; b=r/2tP6RpFz0i88zYKgbxl8h7I+6Qk7b05Iu4Q4IirVDMMM09u+tTg5ERhgZ/GDnRPO DmWgfwc6vBP4aeP82AbQw7B0zUDg94Sy8zqkQwuFAc8HZCM8aF2BS9YZiNsyWor56Hl0 ppccoCmHCd687OVd6ExgJJZw/BLE6FzFfhhW4ZkFimFwZZUdoCpQdJ2hDt6cJB2QpxCh mX4/UrdqhDmGSNQtJJh+e8ohj7YHgSte+Kxjln7AKwM3QDgwpiK6RZssJzJE41cGB8+W PfMGL5wJ99arJ9PJVnfzQ2B6+AwD5NF9TBJch1uTFqeSnF9vWPjKxL1KsygVrr0H8Ecq gb6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697308; x=1748302108; 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=k5bXYrAobPzWNfhMoyWUANBVY5VTCDdA5KxrSZD6wWU=; b=sgSvaVJivSQfqRNh3GGiKC3Z1iI3LR+ZTxPLKUJEgMwpJdwY7Tz0EqAVgIMcV+JGm9 WXVifL2J1GsaUCYPsIzdKI43E0l5aVr0OznAc81UyCurHfm+c51f0hM41P+2KqbhRSq7 osEjHvHwcMlxkyfOkfxRUwTLSs3HxpLv94WXj8Ddwb6hXau6xnDs66tcZgZc6TxR6hkq Dnf+kBDayPiV1Mk0PvfttKBhKC5NFSmtEDldEosiE7jSVa8bvRbSic0OTBfhUytl5stH PhdCqbfBh1t25UY9J4WnxBdolq6XtvtZm64cBCCluBSKG7F6kTfv5aTIfZ7BVRAOgVVp Afbg== X-Forwarded-Encrypted: i=1; AJvYcCURUaYhGcrqzvF7aD5Ox2NazNATHL0p/z2nmXEqqcS21IGAhzXpECyoJ+zaiOVyuwOprzDDsdO3/PePtnk=@vger.kernel.org X-Gm-Message-State: AOJu0YzmseUGGWJ2fROdDcJiKVCpB0mT8N0UkDbyRMSj/8nOwTCuGoR2 R+CFeWaCgTheUx1/yGh9O1D64BLdFwqah/ZpwEL5aQnsBWRfjUhIlOEKzI3hnQdmk6/lPUhDhU2 06Gys4Q== X-Google-Smtp-Source: AGHT+IGOVFyvbbjOcdVaDL1tDlyXnQVlPOhO3+t9xBt7HdnFXlctjkbTyTD3yxn4zuA3RoYGgNhwnpL+OcY= X-Received: from pjbsl15.prod.google.com ([2002:a17:90b:2e0f:b0:2ea:448a:8cd1]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1dc2:b0:2ee:693e:ed7a with SMTP id 98e67ed59e1d1-30e832357d7mr20116257a91.35.1747697307921; Mon, 19 May 2025 16:28:27 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:28:01 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-9-seanjc@google.com> Subject: [PATCH 08/15] KVM: x86: Don't clear PIT's IRQ line status when destroying PIT From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Don't bother clearing the PIT's IRQ line status when destroying the PIT, as userspace can't possibly rely on KVM to lower the IRQ line in any sane use case, and it's not at all obvious that clearing the PIT's IRQ line is correct/desirable in kvm_create_pit()'s error path. When called from kvm_arch_pre_destroy_vm(), the entire VM is being torn down and thus {kvm_pic,kvm_ioapic}.irq_states are unreachable. As for the error path in kvm_create_pit(), the only way the PIT's bit in irq_states can be set is if userspace raises the associated IRQ before KVM_CREATE_PIT{2} completes. Forcefully clearing the bit would clobber's userspace's input, nonsensical though that input may be. Not to mention that no known VMM will continue on if PIT creation fails. Signed-off-by: Sean Christopherson Acked-by: Kai Huang --- arch/x86/include/asm/kvm_host.h | 2 -- arch/x86/kvm/i8254.c | 10 ---------- arch/x86/kvm/i8259.c | 10 ---------- arch/x86/kvm/ioapic.c | 10 ---------- arch/x86/kvm/ioapic.h | 1 - 5 files changed, 33 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index c8654e461933..ebda93979179 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2207,8 +2207,6 @@ static inline int __kvm_irq_line_state(unsigned long = *irq_state, return !!(*irq_state); } =20 -void kvm_pic_clear_all(struct kvm_pic *pic, int irq_source_id); - void kvm_inject_nmi(struct kvm_vcpu *vcpu); int kvm_get_nr_pending_nmis(struct kvm_vcpu *vcpu); =20 diff --git a/arch/x86/kvm/i8254.c b/arch/x86/kvm/i8254.c index d4fc20c265b2..518e2e042605 100644 --- a/arch/x86/kvm/i8254.c +++ b/arch/x86/kvm/i8254.c @@ -641,14 +641,6 @@ static void kvm_pit_reset(struct kvm_pit *pit) kvm_pit_reset_reinject(pit); } =20 -static void kvm_pit_clear_all(struct kvm *kvm) -{ - mutex_lock(&kvm->irq_lock); - kvm_ioapic_clear_all(kvm->arch.vioapic, KVM_PIT_IRQ_SOURCE_ID); - kvm_pic_clear_all(kvm->arch.vpic, KVM_PIT_IRQ_SOURCE_ID); - mutex_unlock(&kvm->irq_lock); -} - static void pit_mask_notifer(struct kvm_irq_mask_notifier *kimn, bool mask) { struct kvm_pit *pit =3D container_of(kimn, struct kvm_pit, mask_notifier); @@ -730,7 +722,6 @@ struct kvm_pit *kvm_create_pit(struct kvm *kvm, u32 fla= gs) kvm_pit_set_reinject(pit, false); kthread_destroy_worker(pit->worker); fail_kthread: - kvm_pit_clear_all(kvm); kfree(pit); return NULL; } @@ -747,7 +738,6 @@ void kvm_free_pit(struct kvm *kvm) kvm_pit_set_reinject(pit, false); hrtimer_cancel(&pit->pit_state.timer); kthread_destroy_worker(pit->worker); - kvm_pit_clear_all(kvm); kfree(pit); } } diff --git a/arch/x86/kvm/i8259.c b/arch/x86/kvm/i8259.c index 0150aec4f523..4de055efc4ee 100644 --- a/arch/x86/kvm/i8259.c +++ b/arch/x86/kvm/i8259.c @@ -206,16 +206,6 @@ int kvm_pic_set_irq(struct kvm_kernel_irq_routing_entr= y *e, struct kvm *kvm, return ret; } =20 -void kvm_pic_clear_all(struct kvm_pic *s, int irq_source_id) -{ - int i; - - pic_lock(s); - for (i =3D 0; i < PIC_NUM_PINS; i++) - __clear_bit(irq_source_id, &s->irq_states[i]); - pic_unlock(s); -} - /* * acknowledge interrupt 'irq' */ diff --git a/arch/x86/kvm/ioapic.c b/arch/x86/kvm/ioapic.c index dc45ea9f5b9c..7d2d47a6c2b6 100644 --- a/arch/x86/kvm/ioapic.c +++ b/arch/x86/kvm/ioapic.c @@ -498,16 +498,6 @@ int kvm_ioapic_set_irq(struct kvm_kernel_irq_routing_e= ntry *e, struct kvm *kvm, return ret; } =20 -void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id) -{ - int i; - - spin_lock(&ioapic->lock); - for (i =3D 0; i < KVM_IOAPIC_NUM_PINS; i++) - __clear_bit(irq_source_id, &ioapic->irq_states[i]); - spin_unlock(&ioapic->lock); -} - static void kvm_ioapic_eoi_inject_work(struct work_struct *work) { int i; diff --git a/arch/x86/kvm/ioapic.h b/arch/x86/kvm/ioapic.h index a86f59bbea44..fee17eb201ef 100644 --- a/arch/x86/kvm/ioapic.h +++ b/arch/x86/kvm/ioapic.h @@ -114,7 +114,6 @@ void kvm_ioapic_destroy(struct kvm *kvm); int kvm_ioapic_set_irq(struct kvm_kernel_irq_routing_entry *e, struct kvm = *kvm, int irq_source_id, int level, bool line_status); =20 -void kvm_ioapic_clear_all(struct kvm_ioapic *ioapic, int irq_source_id); void kvm_get_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state); void kvm_set_ioapic(struct kvm *kvm, struct kvm_ioapic_state *state); void kvm_ioapic_scan_entry(struct kvm_vcpu *vcpu, --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:23:30 2025 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 1B4082222DD for ; Mon, 19 May 2025 23:28:29 +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=1747697311; cv=none; b=TZn0whUi67G1e0JBN3I8IE6DFAnQ7DIeEsEwdVlsPcXC7rgow0xuIEqXcNEwcifTgSjpKzPTDR1hV8X+P3HXlV0//TdW1aWFSXIVRQXwkxrwSb0uOWdVFN4Thaewk7SdlgILXtdfOUeLjWnQKJa+PKDRuXl/yAeScdaTgAqJWk0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697311; c=relaxed/simple; bh=OPUsOxJQRGQw18S5qUhUF69GtSIfHQcNTuuAQQroEw8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Dss1JQsIQ85Bvt7vAzTkmF2hkvYrV1HHy3ZNiHE5wfVQLHDz2Zsr8tzDvsoeNS4NoGEUYhnO9Pkwbr265HRDYSP0bXSCbobvQIoKZ1WicCAoncGRIpc3dM1WC2IU95oxocKhD4l2tKfDEB/EoM+HVuG2x2mf69ODA4Oev+tqsqo= 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=K0U5Xyfq; 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="K0U5Xyfq" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-742a091d290so3301557b3a.3 for ; Mon, 19 May 2025 16:28:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697309; x=1748302109; 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=OzpwPK/JEQxQkuGwL5Uwcgd2u1TTmwDV5SMaEASMIM8=; b=K0U5XyfqgHrS2d9PlpRzAfQDjGsuiEqKG5sEJZDSwrpGBKOTPlolW2ZNCjJAaesWfK WPv/GR4F4SSckoZ4enIKwF6RFley8RsZ7oxr7jCJtDiUMCBCa9m3VCOwxRsZa+Oyrbn8 hr9QsW/4hgJj9B4iMtZTBEbTzIiD0sQA8JuUxOt1QhoXlLfQEoi03h266SoUOpegAFg0 oRJCzs0Zta8fHlrEDqQ77ZPi28z4qj7/x4kQdi1NsOHhyf6TshwKehBUM88WJZn1VS/r OgkjmfqMaNVp3/EamztMXO7eBe4DXdGVngYK0Y9nZ3T0Mf/5vw7EqadqAnsh1WqEs+8E pKRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697309; x=1748302109; 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=OzpwPK/JEQxQkuGwL5Uwcgd2u1TTmwDV5SMaEASMIM8=; b=GT0s08AgxuQ4suh8LfUE0cyOgBFDbKtqRoYLPCN0zaCDfH9csl7Lql+oPuHmxsx1uB QMXeLuUnACKL/fFqlUGGOyTy4cAHD0L7EdtiHH99hPj1BnH6/jdYz+JbIoX5O1OHq8rm lvlTgPW7XuofH9N85zce73f0b+NYkZhx50L62YWwsN8IndDEV0/Qh4wANrgob8bQsgET 5TvKIV/3c9WR4YCqpeol5TyjwoPS71jmv6GlJkco6ifvjExIWfDJXvKN1y9YuTODPA2G 0QSM/BZ6GE8HmLc4GkBzkzc6uyvc7Km9FgHPndSV6foYbDHioXMpVOUfubv+vGUkZsvz 4qyA== X-Forwarded-Encrypted: i=1; AJvYcCUCRhqRCt2QBAmkdQg5R4U3Z0LZUaEo7OhUmZIV9tIy1RdNbA1yC0g34PSkRLrknonQNscg7TTNqoIpMMQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yw45nSi2UX7h5sTxMRzNHXn2klHHjbc+296TM8te/EPpYp34CKh gIRL9pSJMkesd00nq/ZF0mfoEKHf7Nj6AApZb2RQnjF9he9pcbwC4hANTYCn4wiqpURSIWwzrAO XAZVHPA== X-Google-Smtp-Source: AGHT+IES8VHuEi1O9VM21YWHDIFRJ/0jkBwb+8TYaOYI0d7V6KDKgJQT1u+7T8XTP8XEgC0NqDqkFIUei3w= X-Received: from pjyp5.prod.google.com ([2002:a17:90a:e705:b0:30e:7783:edb6]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:69b:b0:1f5:7c6f:6c8a with SMTP id adf61e73a8af0-216219f8d9dmr22093394637.35.1747697309545; Mon, 19 May 2025 16:28:29 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:28:02 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-10-seanjc@google.com> Subject: [PATCH 09/15] KVM: x86: Explicitly check for in-kernel PIC when getting ExtINT From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Explicitly check for an in-kernel PIC when checking for a pending ExtINT in the PIC. Effectively swapping the split vs. full irqchip logic will allow guarding the in-kernel I/O APIC (and PIC) emulation with a Kconfig, and also makes it more obvious that kvm_pic_read_irq() won't result in a NULL pointer dereference. Opportunistically add WARNs in the fallthrough path, mostly to document that the userspace ExtINT logic is only relevant to split IRQ chips. Signed-off-by: Sean Christopherson Acked-by: Kai Huang --- arch/x86/kvm/irq.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index 97d68d837929..b9b9df00ab77 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -41,6 +41,14 @@ static int pending_userspace_extint(struct kvm_vcpu *v) return v->arch.pending_external_vector !=3D -1; } =20 +static int get_userspace_extint(struct kvm_vcpu *vcpu) +{ + int vector =3D vcpu->arch.pending_external_vector; + + vcpu->arch.pending_external_vector =3D -1; + return vector; +} + /* * check if there is pending interrupt from * non-APIC source without intack. @@ -67,10 +75,11 @@ int kvm_cpu_has_extint(struct kvm_vcpu *v) if (!kvm_apic_accept_pic_intr(v)) return 0; =20 - if (irqchip_split(v->kvm)) - return pending_userspace_extint(v); - else + if (pic_in_kernel(v->kvm)) return v->kvm->arch.vpic->output; + + WARN_ON_ONCE(!irqchip_split(v->kvm)); + return pending_userspace_extint(v); } =20 /* @@ -126,13 +135,11 @@ int kvm_cpu_get_extint(struct kvm_vcpu *v) return v->kvm->arch.xen.upcall_vector; #endif =20 - if (irqchip_split(v->kvm)) { - int vector =3D v->arch.pending_external_vector; - - v->arch.pending_external_vector =3D -1; - return vector; - } else + if (pic_in_kernel(v->kvm)) return kvm_pic_read_irq(v->kvm); /* PIC */ + + WARN_ON_ONCE(!irqchip_split(v->kvm)); + return get_userspace_extint(v); } EXPORT_SYMBOL_GPL(kvm_cpu_get_extint); =20 --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:23:30 2025 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 A6D242206B5 for ; Mon, 19 May 2025 23:28:31 +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=1747697313; cv=none; b=DHK+HJd1n0s4W62UWhtLjlZtIGCfNizmoecVK/QXPjauRMSJqcXYhahQP0hJU6ra3fXNScHYIuwDZpQKY3AUkQtcjdw/9OMvaODELqLOj50AMqOVIv0w1xe8IeKb74elPjwtpsDILB+u+ATlbDamfirZU92Dg6ixA6ZpeneDbVo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697313; c=relaxed/simple; bh=KT85hNb1nPKLSeD3d7tvXp+kHVg/bRVevrkhmaFdrRQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=F3usKJh+7Gc6g8KHKYK8dL66+a+hwktZwXLd5lp/hlKSuWdx1NogAGT9bSSBsawI5ADLgAZk0RwIQC2pD+tEA+dcB16QFNax4JsCF00f1WgiezCd4uh6Tl55KAp9GWQtQ3JVEAvKaJJOz3HqVajYs2wLpz3gSoNEp6mtlRD2HB0= 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=b6HgbQQv; 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="b6HgbQQv" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-30e9338430eso2684691a91.3 for ; Mon, 19 May 2025 16:28:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697311; x=1748302111; 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=UnY357EqX+lx3OIGqzNGUHT0chY9mSMI7laKdfKayYM=; b=b6HgbQQvP/HIqwqCV3Qh52BzubgGNZ5l4esc9k2VbJ9v3ar0B2rPytosPzNts4OfEl VyRDB0DNMRkuZ5uIOgbUzY041E7BKAasPct7LC2E+YVdr4v8ymxOUbscaySUGsQNycs5 iEoxr6o4rr6tUPRFYL6/8ki+cbrbzV/cKjemKwglB4ZuOI68RUKPFirBBHLXpIc2AdpY J2+QWiUriTcd2aeRzboz2VGMZgP3v+aQgsf97qYBPIKlWDRNhPTmGT4AK5WVNuqlpqYG UstL4kgyDyxsh8ORAcKEh8gpfYd4D20/7n7s1TTU9MfVotRYWyRfN1M9/m6DDJ7Iq8wu FkqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697311; x=1748302111; 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=UnY357EqX+lx3OIGqzNGUHT0chY9mSMI7laKdfKayYM=; b=d9G39LKJVpwzDgPUz1U1BB33c+6deQfEzNNP4KMeCohtXEkKN1ebat6/6iGXmLQvEl THY4sVxt7UIYvGtnil1DNdjhRpOrUagijILf4bbiL7OeFW9qLGwZh0t954CFSJbdNnLm dGlYunKzHakHacXCZYzoKNv7PJWa/tniLvvQoDgLqqS48ozpJTVdau24LA0DDxFTRu1x 5cOtJzsZHENVFqqQsXjQSHzsT6hKfsYWOYPqx9pwgOwxOnugeAcM2zskDZfuxNt8X3lx ePEKSp0iG2o8ciFuvNRarr1wRJOM8XBeEDOI8XiY7amiLSY4zB2gGEH38l0O6HzxntgO Bmtg== X-Forwarded-Encrypted: i=1; AJvYcCWfp1sivjRb9t2u6IVyJBlui2voWVM8rt/x3HYvJJj8Po40TZqVNSNujirnt980wbPTzQwDQQuCizVFYlw=@vger.kernel.org X-Gm-Message-State: AOJu0YwsoYyQuteL+0FvTz22j45FruHv9O5NuM4z6E5sAsjNEo0BMHeT jl4JCeFBzC0W4TY3JF8/E2InCX2qAaqXYD4s28/plIOJUCOIKGNUAi+KjbwXpJQs2QlyOlbYR92 grxrMIg== X-Google-Smtp-Source: AGHT+IHtFtCumxzhTipUqWyCeTaKG2mOIq8hIVX0ltQyCIbzXTvUEUCPUiAHhLt3YMkpFIqYrZSkQ53mYqY= X-Received: from pjbse12.prod.google.com ([2002:a17:90b:518c:b0:308:6685:55e6]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4b0f:b0:2fa:e9b:33b8 with SMTP id 98e67ed59e1d1-30e7d5565a9mr25304428a91.18.1747697310989; Mon, 19 May 2025 16:28:30 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:28:03 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-11-seanjc@google.com> Subject: [PATCH 10/15] KVM: Move x86-only tracepoints to x86's trace.h From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the I/O APIC tracepoints and trace_kvm_msi_set_irq() to x86, as __KVM_HAVE_IOAPIC is just code for "x86", and trace_kvm_msi_set_irq() isn't unique to I/O APIC emulation. Opportunistically clean up the absurdly messy #includes in ioapic.c. No functional change intended. Signed-off-by: Sean Christopherson Acked-by: Kai Huang --- arch/x86/kvm/ioapic.c | 2 +- arch/x86/kvm/irq_comm.c | 10 ++--- arch/x86/kvm/trace.h | 78 ++++++++++++++++++++++++++++++++++++++ include/trace/events/kvm.h | 77 ------------------------------------- 4 files changed, 82 insertions(+), 85 deletions(-) diff --git a/arch/x86/kvm/ioapic.c b/arch/x86/kvm/ioapic.c index 7d2d47a6c2b6..151ee9a64c3c 100644 --- a/arch/x86/kvm/ioapic.c +++ b/arch/x86/kvm/ioapic.c @@ -41,11 +41,11 @@ #include #include #include -#include =20 #include "ioapic.h" #include "lapic.h" #include "irq.h" +#include "trace.h" =20 static int ioapic_service(struct kvm_ioapic *vioapic, int irq, bool line_status); diff --git a/arch/x86/kvm/irq_comm.c b/arch/x86/kvm/irq_comm.c index 8c827da3e3d6..adef53dc4fef 100644 --- a/arch/x86/kvm/irq_comm.c +++ b/arch/x86/kvm/irq_comm.c @@ -15,15 +15,11 @@ #include #include =20 -#include - -#include "irq.h" - +#include "hyperv.h" #include "ioapic.h" - +#include "irq.h" #include "lapic.h" - -#include "hyperv.h" +#include "trace.h" #include "x86.h" #include "xen.h" =20 diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index ba736cbb0587..4ef17990574d 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -260,6 +260,84 @@ TRACE_EVENT(kvm_cpuid, __entry->used_max_basic ? ", used max basic" : "") ); =20 +#define kvm_deliver_mode \ + {0x0, "Fixed"}, \ + {0x1, "LowPrio"}, \ + {0x2, "SMI"}, \ + {0x3, "Res3"}, \ + {0x4, "NMI"}, \ + {0x5, "INIT"}, \ + {0x6, "SIPI"}, \ + {0x7, "ExtINT"} + +TRACE_EVENT(kvm_ioapic_set_irq, + TP_PROTO(__u64 e, int pin, bool coalesced), + TP_ARGS(e, pin, coalesced), + + TP_STRUCT__entry( + __field( __u64, e ) + __field( int, pin ) + __field( bool, coalesced ) + ), + + TP_fast_assign( + __entry->e =3D e; + __entry->pin =3D pin; + __entry->coalesced =3D coalesced; + ), + + TP_printk("pin %u dst %x vec %u (%s|%s|%s%s)%s", + __entry->pin, (u8)(__entry->e >> 56), (u8)__entry->e, + __print_symbolic((__entry->e >> 8 & 0x7), kvm_deliver_mode), + (__entry->e & (1<<11)) ? "logical" : "physical", + (__entry->e & (1<<15)) ? "level" : "edge", + (__entry->e & (1<<16)) ? "|masked" : "", + __entry->coalesced ? " (coalesced)" : "") +); + +TRACE_EVENT(kvm_ioapic_delayed_eoi_inj, + TP_PROTO(__u64 e), + TP_ARGS(e), + + TP_STRUCT__entry( + __field( __u64, e ) + ), + + TP_fast_assign( + __entry->e =3D e; + ), + + TP_printk("dst %x vec %u (%s|%s|%s%s)", + (u8)(__entry->e >> 56), (u8)__entry->e, + __print_symbolic((__entry->e >> 8 & 0x7), kvm_deliver_mode), + (__entry->e & (1<<11)) ? "logical" : "physical", + (__entry->e & (1<<15)) ? "level" : "edge", + (__entry->e & (1<<16)) ? "|masked" : "") +); + +TRACE_EVENT(kvm_msi_set_irq, + TP_PROTO(__u64 address, __u64 data), + TP_ARGS(address, data), + + TP_STRUCT__entry( + __field( __u64, address ) + __field( __u64, data ) + ), + + TP_fast_assign( + __entry->address =3D address; + __entry->data =3D data; + ), + + TP_printk("dst %llx vec %u (%s|%s|%s%s)", + (u8)(__entry->address >> 12) | ((__entry->address >> 32) & 0xffffff00), + (u8)__entry->data, + __print_symbolic((__entry->data >> 8 & 0x7), kvm_deliver_mode), + (__entry->address & (1<<2)) ? "logical" : "physical", + (__entry->data & (1<<15)) ? "level" : "edge", + (__entry->address & (1<<3)) ? "|rh" : "") +); + #define AREG(x) { APIC_##x, "APIC_" #x } =20 #define kvm_trace_symbol_apic \ diff --git a/include/trace/events/kvm.h b/include/trace/events/kvm.h index fc7d0f8ff078..96e581900c8e 100644 --- a/include/trace/events/kvm.h +++ b/include/trace/events/kvm.h @@ -85,83 +85,6 @@ TRACE_EVENT(kvm_set_irq, #endif /* defined(CONFIG_HAVE_KVM_IRQCHIP) */ =20 #if defined(__KVM_HAVE_IOAPIC) -#define kvm_deliver_mode \ - {0x0, "Fixed"}, \ - {0x1, "LowPrio"}, \ - {0x2, "SMI"}, \ - {0x3, "Res3"}, \ - {0x4, "NMI"}, \ - {0x5, "INIT"}, \ - {0x6, "SIPI"}, \ - {0x7, "ExtINT"} - -TRACE_EVENT(kvm_ioapic_set_irq, - TP_PROTO(__u64 e, int pin, bool coalesced), - TP_ARGS(e, pin, coalesced), - - TP_STRUCT__entry( - __field( __u64, e ) - __field( int, pin ) - __field( bool, coalesced ) - ), - - TP_fast_assign( - __entry->e =3D e; - __entry->pin =3D pin; - __entry->coalesced =3D coalesced; - ), - - TP_printk("pin %u dst %x vec %u (%s|%s|%s%s)%s", - __entry->pin, (u8)(__entry->e >> 56), (u8)__entry->e, - __print_symbolic((__entry->e >> 8 & 0x7), kvm_deliver_mode), - (__entry->e & (1<<11)) ? "logical" : "physical", - (__entry->e & (1<<15)) ? "level" : "edge", - (__entry->e & (1<<16)) ? "|masked" : "", - __entry->coalesced ? " (coalesced)" : "") -); - -TRACE_EVENT(kvm_ioapic_delayed_eoi_inj, - TP_PROTO(__u64 e), - TP_ARGS(e), - - TP_STRUCT__entry( - __field( __u64, e ) - ), - - TP_fast_assign( - __entry->e =3D e; - ), - - TP_printk("dst %x vec %u (%s|%s|%s%s)", - (u8)(__entry->e >> 56), (u8)__entry->e, - __print_symbolic((__entry->e >> 8 & 0x7), kvm_deliver_mode), - (__entry->e & (1<<11)) ? "logical" : "physical", - (__entry->e & (1<<15)) ? "level" : "edge", - (__entry->e & (1<<16)) ? "|masked" : "") -); - -TRACE_EVENT(kvm_msi_set_irq, - TP_PROTO(__u64 address, __u64 data), - TP_ARGS(address, data), - - TP_STRUCT__entry( - __field( __u64, address ) - __field( __u64, data ) - ), - - TP_fast_assign( - __entry->address =3D address; - __entry->data =3D data; - ), - - TP_printk("dst %llx vec %u (%s|%s|%s%s)", - (u8)(__entry->address >> 12) | ((__entry->address >> 32) & 0xffffff00), - (u8)__entry->data, - __print_symbolic((__entry->data >> 8 & 0x7), kvm_deliver_mode), - (__entry->address & (1<<2)) ? "logical" : "physical", - (__entry->data & (1<<15)) ? "level" : "edge", - (__entry->address & (1<<3)) ? "|rh" : "") -); =20 #define kvm_irqchips \ {KVM_IRQCHIP_PIC_MASTER, "PIC master"}, \ --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:23:30 2025 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 77BDC2206BF for ; Mon, 19 May 2025 23:28:33 +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=1747697315; cv=none; b=KQsJJvzx9XI76txE0QjfUy/vof9VzCPakk3OxEIk8zSwcNgYULRs8jIeZFF8T/GZzx5yCi2krWBLuDOYa9k7bS4eRquS5ZO/YM6XhO7jygIbgVYjzZRlirQTTvdzXribP7vDKaqvDai0MzzdyJ9SzhgEj03WpOgcb0cPaSx1Dx4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697315; c=relaxed/simple; bh=A6Mvec40fXAU+3eNtc7h8WPXWVoHL67BNBfmgBmGALs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lPGTy7kcQJWBGYc3v4Ep06JfZVVz/9nET0j6y/l4SrBY6l1POdRw2AJ+Is8j/hnpDkR6mBVdVKSgOkgb0rtxFbz1ndBM2WH5tkx3F2EOUQ6WJFsaiH0tREYx6thfWaY8NwQuLXjf8bux3IlerQcGDjrmSLjcEdZhAAcByNN45s8= 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=EPtMed3y; 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="EPtMed3y" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b26db9af463so4767780a12.2 for ; Mon, 19 May 2025 16:28:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697312; x=1748302112; 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=XckvmjadgADiByTaHXoac+0phhb9MmWn/F+UFko6hq0=; b=EPtMed3yovLBXcGJsaa5njHETmVgREREMm1ciLIeQbAuvq59DbWPMLDpD4nz4B7fOS cnV0Ifv7zNIJvbG6dFIKiJxCAwJyF+kCN6GF4P7uJrYlT9nATPbjftvon1+FgP+fHKQj 9+6q+r+l67A6rY6rtUGbPSI/jvovXTErzdh5dlLSEJYAbHJhxheUtwUHdSdNwoji5tpO /47N0k/MaP0cdZSZMFZW0fbbOSElr08Jp+VcV+rUwfIV3XG7xc9J+wTgmFgYP9x8oxAh X56BqlV/4BWaoAfq015x2oIJt8PTPW+dvSy5+kI8DJ0v1A8tmbF1Nd8QzWAwcdFJ4bK/ Xw/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697312; x=1748302112; 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=XckvmjadgADiByTaHXoac+0phhb9MmWn/F+UFko6hq0=; b=gMMrVD2OQKLcc51PoeY/564FngQ01y24g1dSi5L3nZoozwd3v2LaDCt8WsLIATYDhQ jhEUXjE+qImys4P1Y+9Q2BuuJe6Hx4OcN5Cc4qX7hzckhVsdfrzLlr736r+8v6j6re3z 41ucJF7Xl+wJVgu1SLFNXIFaS45+3n7dovO2yo+xv+jFC5ItnDLNc+HmtW+m4wSrxUnP lNk6VabdfCQevZQQJMWtbEifZpdrnEZwdBx8D2hrurpUk4U7vtEfprM7AY7GHf7ucpo/ Q6C8LXhlpUG0sa8pr9iuVaLEOQR/TrSuQ2cM6iBm6EIHdUw2mNXzTqVEd9zzndoie59R g1tw== X-Forwarded-Encrypted: i=1; AJvYcCVLXLqIa7+0lc39H46Pun8Sy6woW87akODv6cZd5/9ToA20u177FrBnHanrj65RSkAnbDitlD/30FW/FHA=@vger.kernel.org X-Gm-Message-State: AOJu0YzZxLX313HmqDWzO+3Uc9yTN6TnMphzy9UqWKDOFW27e7QX6UJq rDx/ryo5Sl3Zhm4MpSazLKT7kIdqxpzRtUO7PlDBXgr5v1oF7qKXiVGTZgLm6mg889iawTnnE/N TTnM8Dw== X-Google-Smtp-Source: AGHT+IH7nI10wVjZOjLkS5yo4985P4worGDmTWsv4AXMDEnXfYby+Cj2i/UWJk3TknKpQp9WnaJNOICMKAw= X-Received: from pjbsn4.prod.google.com ([2002:a17:90b:2e84:b0:2fc:11a0:c549]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:100f:b0:215:fac3:2ce2 with SMTP id adf61e73a8af0-2162192a9c8mr21422480637.23.1747697312715; Mon, 19 May 2025 16:28:32 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:28:04 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-12-seanjc@google.com> Subject: [PATCH 11/15] KVM: x86: Add CONFIG_KVM_IOAPIC to allow disabling in-kernel I/O APIC From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a Kconfig to allowing building KVM without support for emulating an I/O APIC, PIC, and PIT, which is desirable for deployments that effectively don't support a fully in-kernel IRQ chip, i.e. never expect any VMM to create an in-kernel I/O APIC. E.g. compiling out support eliminates a few thousand lines of guest-facing code and gives security folks warm fuzzies. As a bonus, wrapping relevant paths with CONFIG_KVM_IOAPIC #ifdefs makes it much easier for readers to understand which bits and pieces exist specific for fully in-kernel IRQ chips. Opportunistically convert all two in-kernel uses of __KVM_HAVE_IOAPIC to CONFIG_KVM_IOAPIC, e.g. rather than add a second #ifdef to generate a stub for kvm_arch_post_irq_routing_update(). Signed-off-by: Sean Christopherson Acked-by: Kai Huang --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/Kconfig | 10 ++++++++++ arch/x86/kvm/Makefile | 5 +++-- arch/x86/kvm/irq.c | 6 ++++++ arch/x86/kvm/irq_comm.c | 2 ++ arch/x86/kvm/lapic.c | 7 ++++++- arch/x86/kvm/trace.h | 2 ++ arch/x86/kvm/x86.c | 24 ++++++++++++++++++++---- include/linux/kvm_host.h | 2 +- include/trace/events/kvm.h | 4 ++-- 10 files changed, 54 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index ebda93979179..f5ff5174674c 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1374,9 +1374,11 @@ struct kvm_arch { atomic_t noncoherent_dma_count; #define __KVM_HAVE_ARCH_ASSIGNED_DEVICE atomic_t assigned_device_count; +#ifdef CONFIG_KVM_IOAPIC struct kvm_pic *vpic; struct kvm_ioapic *vioapic; struct kvm_pit *vpit; +#endif atomic_t vapics_in_nmi_mode; struct mutex apic_map_lock; struct kvm_apic_map __rcu *apic_map; diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index 2eeffcec5382..2c86673155c9 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -166,6 +166,16 @@ config KVM_AMD_SEV Encrypted State (SEV-ES), and Secure Encrypted Virtualization with Secure Nested Paging (SEV-SNP) technologies on AMD processors. =20 +config KVM_IOAPIC + bool "I/O APIC, PIC, and PIT emulation" + default y + depends on KVM + help + Provides support for KVM to emulate an I/O APIC, PIC, and PIT, i.e. + for full in-kernel APIC emulation. + + If unsure, say Y. + config KVM_SMM bool "System Management Mode emulation" default y diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile index a5d362c7b504..92c737257789 100644 --- a/arch/x86/kvm/Makefile +++ b/arch/x86/kvm/Makefile @@ -5,12 +5,13 @@ ccflags-$(CONFIG_KVM_WERROR) +=3D -Werror =20 include $(srctree)/virt/kvm/Makefile.kvm =20 -kvm-y +=3D x86.o emulate.o i8259.o irq.o lapic.o \ - i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mtrr.o \ +kvm-y +=3D x86.o emulate.o irq.o lapic.o \ + irq_comm.o cpuid.o pmu.o mtrr.o \ debugfs.o mmu/mmu.o mmu/page_track.o \ mmu/spte.o =20 kvm-$(CONFIG_X86_64) +=3D mmu/tdp_iter.o mmu/tdp_mmu.o +kvm-$(CONFIG_KVM_IOAPIC) +=3D i8259.o i8254.o ioapic.o kvm-$(CONFIG_KVM_HYPERV) +=3D hyperv.o kvm-$(CONFIG_KVM_XEN) +=3D xen.o kvm-$(CONFIG_KVM_SMM) +=3D smm.o diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index b9b9df00ab77..a416ccddde5f 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -75,8 +75,10 @@ int kvm_cpu_has_extint(struct kvm_vcpu *v) if (!kvm_apic_accept_pic_intr(v)) return 0; =20 +#ifdef CONFIG_KVM_IOAPIC if (pic_in_kernel(v->kvm)) return v->kvm->arch.vpic->output; +#endif =20 WARN_ON_ONCE(!irqchip_split(v->kvm)); return pending_userspace_extint(v); @@ -135,8 +137,10 @@ int kvm_cpu_get_extint(struct kvm_vcpu *v) return v->kvm->arch.xen.upcall_vector; #endif =20 +#ifdef CONFIG_KVM_IOAPIC if (pic_in_kernel(v->kvm)) return kvm_pic_read_irq(v->kvm); /* PIC */ +#endif =20 WARN_ON_ONCE(!irqchip_split(v->kvm)); return get_userspace_extint(v); @@ -170,7 +174,9 @@ void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcp= u) void __kvm_migrate_timers(struct kvm_vcpu *vcpu) { __kvm_migrate_apic_timer(vcpu); +#ifdef CONFIG_KVM_IOAPIC __kvm_migrate_pit_timer(vcpu); +#endif kvm_x86_call(migrate_timers)(vcpu); } =20 diff --git a/arch/x86/kvm/irq_comm.c b/arch/x86/kvm/irq_comm.c index adef53dc4fef..a4ef150fdd1c 100644 --- a/arch/x86/kvm/irq_comm.c +++ b/arch/x86/kvm/irq_comm.c @@ -208,6 +208,7 @@ int kvm_set_routing_entry(struct kvm *kvm, * check kvm_arch_can_set_irq_routing() before calling this function. */ switch (ue->type) { +#ifdef CONFIG_KVM_IOAPIC case KVM_IRQ_ROUTING_IRQCHIP: if (irqchip_split(kvm)) return -EINVAL; @@ -231,6 +232,7 @@ int kvm_set_routing_entry(struct kvm *kvm, } e->irqchip.irqchip =3D ue->u.irqchip.irqchip; break; +#endif case KVM_IRQ_ROUTING_MSI: e->set =3D kvm_set_msi; e->msi.address_lo =3D ue->u.msi.address_lo; diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 73418dc0ebb2..4cf8c1f753d3 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1455,7 +1455,7 @@ static bool kvm_ioapic_handles_vector(struct kvm_lapi= c *apic, int vector) =20 static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector) { - int trigger_mode; + int __maybe_unused trigger_mode; =20 /* Eoi the ioapic only if the ioapic doesn't own the vector. */ if (!kvm_ioapic_handles_vector(apic, vector)) @@ -1476,12 +1476,14 @@ static void kvm_ioapic_send_eoi(struct kvm_lapic *a= pic, int vector) return; } =20 +#ifdef CONFIG_KVM_IOAPIC if (apic_test_vector(vector, apic->regs + APIC_TMR)) trigger_mode =3D IOAPIC_LEVEL_TRIG; else trigger_mode =3D IOAPIC_EDGE_TRIG; =20 kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode); +#endif } =20 static int apic_set_eoi(struct kvm_lapic *apic) @@ -3146,8 +3148,11 @@ int kvm_apic_set_state(struct kvm_vcpu *vcpu, struct= kvm_lapic_state *s) kvm_x86_call(hwapic_isr_update)(vcpu, apic_find_highest_isr(apic)); } kvm_make_request(KVM_REQ_EVENT, vcpu); + +#ifdef CONFIG_KVM_IOAPIC if (ioapic_in_kernel(vcpu->kvm)) kvm_rtc_eoi_tracking_restore_one(vcpu); +#endif =20 vcpu->arch.apic_arb_prio =3D 0; =20 diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index 4ef17990574d..ababdba2c186 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -270,6 +270,7 @@ TRACE_EVENT(kvm_cpuid, {0x6, "SIPI"}, \ {0x7, "ExtINT"} =20 +#ifdef CONFIG_KVM_IOAPIC TRACE_EVENT(kvm_ioapic_set_irq, TP_PROTO(__u64 e, int pin, bool coalesced), TP_ARGS(e, pin, coalesced), @@ -314,6 +315,7 @@ TRACE_EVENT(kvm_ioapic_delayed_eoi_inj, (__entry->e & (1<<15)) ? "level" : "edge", (__entry->e & (1<<16)) ? "|masked" : "") ); +#endif =20 TRACE_EVENT(kvm_msi_set_irq, TP_PROTO(__u64 address, __u64 data), diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 9e2c249d45ca..52eff4919d95 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4630,17 +4630,20 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, l= ong ext) case KVM_CAP_EXT_CPUID: case KVM_CAP_EXT_EMUL_CPUID: case KVM_CAP_CLOCKSOURCE: +#ifdef CONFIG_KVM_IOAPIC case KVM_CAP_PIT: + case KVM_CAP_PIT2: + case KVM_CAP_PIT_STATE2: + case KVM_CAP_REINJECT_CONTROL: +#endif case KVM_CAP_NOP_IO_DELAY: case KVM_CAP_MP_STATE: case KVM_CAP_SYNC_MMU: case KVM_CAP_USER_NMI: - case KVM_CAP_REINJECT_CONTROL: case KVM_CAP_IRQ_INJECT_STATUS: case KVM_CAP_IOEVENTFD: case KVM_CAP_IOEVENTFD_NO_LENGTH: - case KVM_CAP_PIT2: - case KVM_CAP_PIT_STATE2: + case KVM_CAP_SET_IDENTITY_MAP_ADDR: case KVM_CAP_VCPU_EVENTS: #ifdef CONFIG_KVM_HYPERV @@ -6393,6 +6396,7 @@ static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *= kvm, return 0; } =20 +#ifdef CONFIG_KVM_IOAPIC static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *c= hip) { struct kvm_pic *pic =3D kvm->arch.vpic; @@ -6521,6 +6525,7 @@ static int kvm_vm_ioctl_reinject(struct kvm *kvm, =20 return 0; } +#endif /* CONFIG_KVM_IOAPIC */ =20 void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *mems= lot) { @@ -7064,9 +7069,11 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned in= t ioctl, unsigned long arg) struct kvm *kvm =3D filp->private_data; void __user *argp =3D (void __user *)arg; int r =3D -ENOTTY; + +#ifdef CONFIG_KVM_IOAPIC /* * This union makes it completely explicit to gcc-3.x - * that these two variables' stack usage should be + * that these three variables' stack usage should be * combined, not added together. */ union { @@ -7074,6 +7081,7 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int= ioctl, unsigned long arg) struct kvm_pit_state2 ps2; struct kvm_pit_config pit_config; } u; +#endif =20 switch (ioctl) { case KVM_SET_TSS_ADDR: @@ -7097,6 +7105,7 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int= ioctl, unsigned long arg) case KVM_SET_NR_MMU_PAGES: r =3D kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); break; +#ifdef CONFIG_KVM_IOAPIC case KVM_CREATE_IRQCHIP: { mutex_lock(&kvm->lock); =20 @@ -7257,6 +7266,7 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int= ioctl, unsigned long arg) r =3D kvm_vm_ioctl_reinject(kvm, &control); break; } +#endif case KVM_SET_BOOT_CPU_ID: r =3D 0; mutex_lock(&kvm->lock); @@ -10716,8 +10726,10 @@ static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu) =20 if (irqchip_split(vcpu->kvm)) kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); +#ifdef CONFIG_KVM_IOAPIC else if (ioapic_in_kernel(vcpu->kvm)) kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); +#endif =20 if (is_guest_mode(vcpu)) vcpu->arch.load_eoi_exitmap_pending =3D true; @@ -12920,7 +12932,9 @@ void kvm_arch_pre_destroy_vm(struct kvm *kvm) cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); =20 +#ifdef CONFIG_KVM_IOAPIC kvm_free_pit(kvm); +#endif =20 kvm_mmu_pre_destroy_vm(kvm); static_call_cond(kvm_x86_vm_pre_destroy)(kvm); @@ -12944,8 +12958,10 @@ void kvm_arch_destroy_vm(struct kvm *kvm) } kvm_destroy_vcpus(kvm); kvm_free_msr_filter(srcu_dereference_check(kvm->arch.msr_filter, &kvm->sr= cu, 1)); +#ifdef CONFIG_KVM_IOAPIC kvm_pic_destroy(kvm); kvm_ioapic_destroy(kvm); +#endif kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); kvm_mmu_uninit_vm(kvm); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 44b439c5fcf4..0e151db44ecd 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1019,7 +1019,7 @@ void kvm_destroy_vcpus(struct kvm *kvm); void vcpu_load(struct kvm_vcpu *vcpu); void vcpu_put(struct kvm_vcpu *vcpu); =20 -#ifdef __KVM_HAVE_IOAPIC +#ifdef CONFIG_KVM_IOAPIC void kvm_arch_post_irq_ack_notifier_list_update(struct kvm *kvm); #else static inline void kvm_arch_post_irq_ack_notifier_list_update(struct kvm *= kvm) diff --git a/include/trace/events/kvm.h b/include/trace/events/kvm.h index 96e581900c8e..1065a81ca57f 100644 --- a/include/trace/events/kvm.h +++ b/include/trace/events/kvm.h @@ -84,14 +84,14 @@ TRACE_EVENT(kvm_set_irq, ); #endif /* defined(CONFIG_HAVE_KVM_IRQCHIP) */ =20 -#if defined(__KVM_HAVE_IOAPIC) +#ifdef CONFIG_KVM_IOAPIC =20 #define kvm_irqchips \ {KVM_IRQCHIP_PIC_MASTER, "PIC master"}, \ {KVM_IRQCHIP_PIC_SLAVE, "PIC slave"}, \ {KVM_IRQCHIP_IOAPIC, "IOAPIC"} =20 -#endif /* defined(__KVM_HAVE_IOAPIC) */ +#endif /* CONFIG_KVM_IOAPIC */ =20 #if defined(CONFIG_HAVE_KVM_IRQCHIP) =20 --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:23:30 2025 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 49A41224244 for ; Mon, 19 May 2025 23:28:35 +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=1747697316; cv=none; b=deX1M5lFCvTomGeserKM/8YKSWlZURYMqOqSa/lcNGvgSpIsj/mmauJNRn5UiEAuF6n2TWYYTc0v5O8iYYYYfTNpgwcF4GqbSyVFL2v61y3zFrPArnKhx2d4HgDm3WcvjUvwsNJ1ogVbIKOjjw5ZGmbI4+F00JhpIVxNwzETIYs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697316; c=relaxed/simple; bh=kXdCtkWuECHGgWR2OT6Gy3/nttBLueu1XR40dPgOpQg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hDu0WBLxak1ydM6bpJg/7AG9+HgyZu2cKSVW04TUz1UFKpdcr3zrLSL7iP14m2Gg5RPilVBqFxlm+vshV7aTtBeh1dHya/uMw558nrjPNm6AsW4lOvqGn/+N+WpNJQAk6FhxQIH5Hi07gIqdVsHiZYDoMfI5RIJmvdyPwhE7V9M= 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=Uak+riy0; 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="Uak+riy0" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b26e6b84a31so2701934a12.2 for ; Mon, 19 May 2025 16:28:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697314; x=1748302114; 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=9gLcS+OzRCY/eRWGmdNSRPdTQEpCa40B7+xlZSCIkqg=; b=Uak+riy0pk/lDPBqauIjSQ6y6gTKbo2844R+OrpbLvLBd9K/P8kWdBTPCZT9pFZDzY u7lCpONqecUQjGvGO85+bQSSa66pPhTVPovx0O/UvE3NSesM0ZI624lYgsuq0DbfGwUF tK+NfT/x56sFkKnGnGNXtWHk3nGVdMofRMtE2WBua1avIFiZ4cp6VYRd3y+pVhEV+Mjc ZiFDYDdcaD3WqZHRgYwrjfupuvYrQK10kK/vBY1x4l2xhW8m5eoZrZOC9pqF0rb6Xw3P zGmWd3sNmll6LanF35Ktm/Z29+ZtKXQuZU/L9ALDQtX4VmeWYqK7xBU2pwuE3+fCVPAZ nr/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697314; x=1748302114; 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=9gLcS+OzRCY/eRWGmdNSRPdTQEpCa40B7+xlZSCIkqg=; b=O4qpWDLs2PwaCsc2qBfbePzzP/ym7t2tmN18RUvXn83/ZYz5xWO4W3Kogj1RPNyRI4 F7oUIFHtCyxRIMaG3bv9LjNOmuhJgFfohhP4MWSC/1R9guKVQO8iBj+2Y7PSj3oAIxAB /EtF4okj5KQoJ5DH0xCYuSHys/pX/HKPwDzJuoUfzO2IuY7StHcPHKMPMYg5nKVzfS0T ArZQeGFT8v8HIt0/aJeX03tmq7Ugh2kxly+Nsbhyp7af8beC7RTXmMD+15zTukCjrhV7 jL15GR3tyGxwuNUruTnE5+GGwvUXOuW+qT5uITbcwKIB2QosQ7YcR/wShm7T1ffjFb1h luuA== X-Forwarded-Encrypted: i=1; AJvYcCVNEA2mDbSsz9Gkqqv6H7g9h1yOgK2GwoZeXpG5ErOgYiYu3Iry4KELnbktnX0aYPMN/puhKWpgRdWwC50=@vger.kernel.org X-Gm-Message-State: AOJu0YwmLwKwSB7pJt1v5zOn6JDwm6X1dCtnX2qAIqK0khmw8YE1RwHM 9g3Hg+k+Pg4KanzNe3cWOV6nXZ/Q/oGwrtncCOsXIvidcUkA1KnhqMAouM2z50LnimFWvJuZvRA 6B7vnDg== X-Google-Smtp-Source: AGHT+IGsS44SKP9udDnxbnYqpNtWDNsH2nu1RdF7aCNajWwjT1ZSWe7ZKUxh/RwFmGOpvKqPfnwHgOPVukI= X-Received: from pjbsl15.prod.google.com ([2002:a17:90b:2e0f:b0:2ea:448a:8cd1]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:1343:b0:30e:9349:2d88 with SMTP id 98e67ed59e1d1-30e93493309mr24785255a91.12.1747697314420; Mon, 19 May 2025 16:28:34 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:28:05 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-13-seanjc@google.com> Subject: [PATCH 12/15] KVM: Squash two CONFIG_HAVE_KVM_IRQCHIP #ifdefs into one From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Squash two #idef CONFIG_HAVE_KVM_IRQCHIP regions in KVM's trace events, as the only code outside of the #idefs depends on CONFIG_KVM_IOAPIC, and that Kconfig only exists for x86, which unconditionally selects HAVE_KVM_IRQCHIP. No functional change intended. Signed-off-by: Sean Christopherson Acked-by: Kai Huang --- include/trace/events/kvm.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/include/trace/events/kvm.h b/include/trace/events/kvm.h index 1065a81ca57f..0b6b79b1a1bc 100644 --- a/include/trace/events/kvm.h +++ b/include/trace/events/kvm.h @@ -82,7 +82,6 @@ TRACE_EVENT(kvm_set_irq, TP_printk("gsi %u level %d source %d", __entry->gsi, __entry->level, __entry->irq_source_id) ); -#endif /* defined(CONFIG_HAVE_KVM_IRQCHIP) */ =20 #ifdef CONFIG_KVM_IOAPIC =20 @@ -93,8 +92,6 @@ TRACE_EVENT(kvm_set_irq, =20 #endif /* CONFIG_KVM_IOAPIC */ =20 -#if defined(CONFIG_HAVE_KVM_IRQCHIP) - #ifdef kvm_irqchips #define kvm_ack_irq_string "irqchip %s pin %u" #define kvm_ack_irq_parm __print_symbolic(__entry->irqchip, kvm_irqchips)= , __entry->pin --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:23:30 2025 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 868952248B8 for ; Mon, 19 May 2025 23:28:36 +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=1747697318; cv=none; b=l/n4i7W5Ah07c43G76RI3KNHS7D5KVxCQV9aseUbGIFVBHZwmuqG1KsNxESyEQBR1iIVhLrFnu9aAbfd8OExZnHTl8CTkinNmV+DsJ6f+vAz8q6ETa4OapCIEXYOo0lUCicuaHMq//3hx2iyKGntYxQkNJuSXaNpsead4ZlG3zU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697318; c=relaxed/simple; bh=qQIM5Tr5nBvDkLCbk46mds4m9dIPyNzVYzD4a6yvVvY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Y1TVzMpNAsDWHuts79+U298agp+p/wJNMb/jYfbYD4auuadl0QDrYMgLZISGWryrB+xm+PSXqs6xVFLzIa2y3tOznohUNX+xfox/DI5Uj7UgeCvA+tqQ7DaX50v6yeTeTGXk5y9bqcWp4D1Ik19YnJECeTdfktmbwTSD5BZBof0= 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=Zcece23w; 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="Zcece23w" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7394792f83cso3733453b3a.3 for ; Mon, 19 May 2025 16:28:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697316; x=1748302116; 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=jzqljEkDXtAFBsLi8kcGKkXBiNdwCtNEp6kp2mMRpEQ=; b=Zcece23wVKa7V75EID6upDRAziP9YbnhojNHdyIbEHBbvy4AnyVRAU1G2Y9FNSrXhX FjHSTK1ZDRz1yFOxNGuXDT49+q9GdVupeUbiDwes56qZ3p2rPUOhJJTtHUk9ygPqZK2C osgHOOMPnxvo5DWljbjXd/pRmJs4Bzgl30VLn37JNbrvjFFK5G9SE2iWcqkz7ecHXG/r XP5y8lhSDV623bd3HEy5m5j8Bh7aBNx8h+uHx3vkxY9z9X4W4oiEk2b41J5W7mQEBop9 g0kMT1bysHu9a8G04eTgmS0YJyvPlAd7OLjbIRHaryK01K48igaFvnJb7kOwQm0yfJGy 7Xwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697316; x=1748302116; 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=jzqljEkDXtAFBsLi8kcGKkXBiNdwCtNEp6kp2mMRpEQ=; b=SHIo2hH/qAnCs0+M9PZxnFZHXSYndJz+XooAH10WzLJ2dK3Caw0ToH5RFwwnSYquos B9m0uYFgmeWacigJc75rVsyYcOefRceK2by48FghUto7lKe8ZXLpAvh3UW7eIvWYjwoH Cdt3/x2Xv4GtiazWXhpX4+B2DbdiXQ7+DFrcyBGGaEC1bK1oy4oEYjAleOhOYIM2PD9Q P2DLRzRvqNCAikKD5S5him+N+mJi/cbdI+SXFO70u0EqvFuaEjrqzFFG/Tn+uZ8KhfPv vQoeSQDoXrYhjXxK9EIqcHWzlJoI9VsF/+wNunJVuaf+OGM1PWkJ8PCtkVqAROfDyQe6 fZmQ== X-Forwarded-Encrypted: i=1; AJvYcCXPULDQuC692LrQHwB0zPIURszm34nXxyREraIBwTb3SGqPt5vaLZIgL9VZbW9nRr8tQXmjhqyADc87i+k=@vger.kernel.org X-Gm-Message-State: AOJu0YzawrBCNeDKwDQ0gV0Mw1BtyLkw5HPPFNz3ttjC5EwRveWtYjb/ TM9uRSqc2qvcwZwazUFtaqmBK37lraPYQwo2kpTcTow5UnpaRjWwflG/TkHkPuKrf67WJ1MvRyo X84O2rA== X-Google-Smtp-Source: AGHT+IGYPQHinoGic07rRcLqLfD0l06dUYUgvVqfZvDuAHBo4yxWC/GP7uWYn1Sj/q1GSZLdB0srqGbjfm4= X-Received: from pfmy21.prod.google.com ([2002:aa7:8055:0:b0:73e:665:360]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:10c6:b0:736:a540:c9ad with SMTP id d2e1a72fcca58-742a98b833bmr19860524b3a.20.1747697315919; Mon, 19 May 2025 16:28:35 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:28:06 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-14-seanjc@google.com> Subject: [PATCH 13/15] KVM: selftests: Fall back to split IRQ chip if full in-kernel chip is unsupported From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that KVM x86 allows compiling out support for in-kernel I/O APIC (and PIC and PIT) emulation, i.e. allows disabling KVM_CREATE_IRQCHIP for all intents and purposes, fall back to a split IRQ chip for x86 if creating the full in-kernel version fails with ENOTTY. Signed-off-by: Sean Christopherson Acked-by: Kai Huang --- tools/testing/selftests/kvm/lib/kvm_util.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/sel= ftests/kvm/lib/kvm_util.c index 50edc59cc0ca..53116f4ffe97 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1713,7 +1713,18 @@ void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t g= pa) /* Create an interrupt controller chip for the specified VM. */ void vm_create_irqchip(struct kvm_vm *vm) { - vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL); + int r; + + /* + * Allocate a fully in-kernel IRQ chip by default, but fall back to a + * split model (x86 only) if that fails (KVM x86 allows compiling out + * support for KVM_CREATE_IRQCHIP). + */ + r =3D __vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL); + if (r && errno =3D=3D ENOTTY && kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP)) + vm_enable_cap(vm, KVM_CAP_SPLIT_IRQCHIP, 24); + else + TEST_ASSERT_VM_VCPU_IOCTL(!r, KVM_CREATE_IRQCHIP, r, vm); =20 vm->has_irqchip =3D true; } --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:23:30 2025 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 64D9C2253FF for ; Mon, 19 May 2025 23:28:38 +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=1747697320; cv=none; b=IJVR69YhXiNuLLWJneI2lNQVzt3N/Ndt9Ib1vp4BNOcUQnCO4+bgw2eXyFJaU8eY/9aRZSCKQT5RvUyx4onWiMlVHmvR+WzolHxRvA9kHhGEUyxG4PSmffZA8XwU1IJy3EbWAZEezL2V4DC/E6q0VD/7g8tpta3tdqWgbj6lDzc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697320; c=relaxed/simple; bh=tc/GDYNKJ/JO970W56dR3hFPJB+jiI1xGAiF+LJQ+ps=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RWuVfF7L7j7PLMeJIlhK8I0gcS6wtI+7Pmmcp1XvqgP14lFS5fLc+LT/CoUz6DWl8eSpFBPYbQ/2Rt4jMQq6OaXrkR+DE750rLTs3blrB+zdMl3wHCZ732Bnf4YZEpbCsHAIPAEPLhv+0pELZ/bsIth/Efz/qjJLsUmNIOx1RFM= 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=wgc9aXZG; 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="wgc9aXZG" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-30a59538b17so4438422a91.3 for ; Mon, 19 May 2025 16:28:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697317; x=1748302117; 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=Yt8dL6gvia2/Aq5+LlGzwWYcB9Uwzc86WQj4RAHOGf4=; b=wgc9aXZGcswbz/OPihLpxSJb3hMF9BvnMYYscI+vI8rwp5+WXvTB9K8Uyfus8Rt9xh DySXqiJKABtTEJrWcVRU+Fa+JvHCmsNSLePgWqO7vPyNk7UyOaHYm7MVI/z+4Ykw2XaE sJ8p2GYIEVGV5WPauCIMzk1psRG1wnQQsGt4pix54aVfjxqeuacvAj9O5utS+jxV3nyX gHG2vT1Y8h7D0dIxLyWBS2jMPQUcJzSBmvJHhRrLHufJaX/OWIPW+rYMeAYTsfuyUWXy WEjBDJYgO+K7uU6n1z3ScEyZLYEqli8Im2qNYLSTa1Kiu/rHCQZ54B88oVGi1gydAYU8 TunA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697317; x=1748302117; 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=Yt8dL6gvia2/Aq5+LlGzwWYcB9Uwzc86WQj4RAHOGf4=; b=cJz4Xkyo5EDpUI3Uy1+ta+Tf6NW57L0x63pUJkupm3coW6tv8TEG+sZUTOGeO91vGV fhrGyKj2XOdzGfuQVb9c38toe9uH/7lTGUSCjT0Z6gPCrTlvH8sh5/BCQTOeTET75qTR C1Ah1/vEUI5UzH8MLuLTxhf1mMEQiYX0RkCfdgd53d30mcW5aPuqN6YUOVnT74Z8Bcgm /at5QS2W80+wct6sNXqVE/Ht1FkaYSZrmFRYMmzvPPWrjn2uDtdTQafPDgOldKs69G/l 2spJ6UWHqz7J/tNnrfWlAHPIZSzfo1UuhMa8KIRzD2SHRpa6I9cKREZKjpYZqZiwvZxQ u8Mw== X-Forwarded-Encrypted: i=1; AJvYcCUzCpNvrpbTL1t68atb8hQ5ox80l0fLL9rBnRlLDQojeaPDOUipIixlDOi59uW0TfkD08RFSW+beWnfqNo=@vger.kernel.org X-Gm-Message-State: AOJu0YxV9yc2+MaXKfwSQT1vt1Rc5JDXTCmXBWfpcT+2BucGEendeyaB BLz3KbnsYXM/B63yK2/zJziFfXhky0SSC7I4zpt/5FK9oxcOXqbrq7GD0KjwEp6+69w0Dz8iGg0 BkcfANQ== X-Google-Smtp-Source: AGHT+IGXrytGt+sKwT00ZGV+dLlBuxNOAw6WYkk4wC/ia6pi8Ets885sNOOc2hhxCbcbWIDEA37QMYR48rc= X-Received: from pjyp3.prod.google.com ([2002:a17:90a:e703:b0:2fa:a101:755]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4c0a:b0:2fa:157e:c78e with SMTP id 98e67ed59e1d1-30e7d4ebdb1mr23846796a91.7.1747697317621; Mon, 19 May 2025 16:28:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:28:07 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-15-seanjc@google.com> Subject: [PATCH 14/15] KVM: x86: Move IRQ mask notifier infrastructure to I/O APIC emulation From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the IRQ mask logic to ioapic.c as KVM's only user is its in-kernel I/O APIC emulation. In addition to encapsulating more I/O APIC specific code, trimming down irq_comm.c helps pave the way for removing it entirely. Signed-off-by: Sean Christopherson Acked-by: Kai Huang --- arch/x86/include/asm/kvm_host.h | 16 -------------- arch/x86/kvm/i8254.h | 2 ++ arch/x86/kvm/i8259.c | 2 ++ arch/x86/kvm/ioapic.c | 37 +++++++++++++++++++++++++++++++++ arch/x86/kvm/ioapic.h | 16 ++++++++++++++ arch/x86/kvm/irq_comm.c | 33 ----------------------------- arch/x86/kvm/x86.c | 1 - 7 files changed, 57 insertions(+), 50 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index f5ff5174674c..21ccb122ab76 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1426,9 +1426,6 @@ struct kvm_arch { struct delayed_work kvmclock_update_work; struct delayed_work kvmclock_sync_work; =20 - /* reads protected by irq_srcu, writes by irq_lock */ - struct hlist_head mask_notifier_list; - #ifdef CONFIG_KVM_HYPERV struct kvm_hv hyperv; #endif @@ -2038,19 +2035,6 @@ int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long= cr3); int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, const void *val, int bytes); =20 -struct kvm_irq_mask_notifier { - void (*func)(struct kvm_irq_mask_notifier *kimn, bool masked); - int irq; - struct hlist_node link; -}; - -void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq, - struct kvm_irq_mask_notifier *kimn); -void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq, - struct kvm_irq_mask_notifier *kimn); -void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned p= in, - bool mask); - extern bool tdp_enabled; =20 u64 vcpu_tsc_khz(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/i8254.h b/arch/x86/kvm/i8254.h index 14fb310357f2..de172567b56a 100644 --- a/arch/x86/kvm/i8254.h +++ b/arch/x86/kvm/i8254.h @@ -6,6 +6,8 @@ =20 #include =20 +#include "ioapic.h" + struct kvm_kpit_channel_state { u32 count; /* can be 65536 */ u16 latched_count; diff --git a/arch/x86/kvm/i8259.c b/arch/x86/kvm/i8259.c index 4de055efc4ee..2ac7f1678c46 100644 --- a/arch/x86/kvm/i8259.c +++ b/arch/x86/kvm/i8259.c @@ -31,6 +31,8 @@ #include #include #include + +#include "ioapic.h" #include "irq.h" =20 #include diff --git a/arch/x86/kvm/ioapic.c b/arch/x86/kvm/ioapic.c index 151ee9a64c3c..daaf16e4681a 100644 --- a/arch/x86/kvm/ioapic.c +++ b/arch/x86/kvm/ioapic.c @@ -310,6 +310,42 @@ void kvm_arch_post_irq_ack_notifier_list_update(struct= kvm *kvm) kvm_make_scan_ioapic_request(kvm); } =20 +void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq, + struct kvm_irq_mask_notifier *kimn) +{ + struct kvm_ioapic *ioapic =3D kvm->arch.vioapic; + + mutex_lock(&kvm->irq_lock); + kimn->irq =3D irq; + hlist_add_head_rcu(&kimn->link, &ioapic->mask_notifier_list); + mutex_unlock(&kvm->irq_lock); +} + +void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq, + struct kvm_irq_mask_notifier *kimn) +{ + mutex_lock(&kvm->irq_lock); + hlist_del_rcu(&kimn->link); + mutex_unlock(&kvm->irq_lock); + synchronize_srcu(&kvm->irq_srcu); +} + +void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned p= in, + bool mask) +{ + struct kvm_ioapic *ioapic =3D kvm->arch.vioapic; + struct kvm_irq_mask_notifier *kimn; + int idx, gsi; + + idx =3D srcu_read_lock(&kvm->irq_srcu); + gsi =3D kvm_irq_map_chip_pin(kvm, irqchip, pin); + if (gsi !=3D -1) + hlist_for_each_entry_rcu(kimn, &ioapic->mask_notifier_list, link) + if (kimn->irq =3D=3D gsi) + kimn->func(kimn, mask); + srcu_read_unlock(&kvm->irq_srcu, idx); +} + static void ioapic_write_indirect(struct kvm_ioapic *ioapic, u32 val) { unsigned index; @@ -736,6 +772,7 @@ int kvm_ioapic_init(struct kvm *kvm) return -ENOMEM; spin_lock_init(&ioapic->lock); INIT_DELAYED_WORK(&ioapic->eoi_inject, kvm_ioapic_eoi_inject_work); + INIT_HLIST_HEAD(&ioapic->mask_notifier_list); kvm->arch.vioapic =3D ioapic; kvm_ioapic_reset(ioapic); kvm_iodevice_init(&ioapic->dev, &ioapic_mmio_ops); diff --git a/arch/x86/kvm/ioapic.h b/arch/x86/kvm/ioapic.h index fee17eb201ef..f5c1ff640635 100644 --- a/arch/x86/kvm/ioapic.h +++ b/arch/x86/kvm/ioapic.h @@ -86,8 +86,24 @@ struct kvm_ioapic { struct delayed_work eoi_inject; u32 irq_eoi[IOAPIC_NUM_PINS]; u32 irr_delivered; + + /* reads protected by irq_srcu, writes by irq_lock */ + struct hlist_head mask_notifier_list; }; =20 +struct kvm_irq_mask_notifier { + void (*func)(struct kvm_irq_mask_notifier *kimn, bool masked); + int irq; + struct hlist_node link; +}; + +void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq, + struct kvm_irq_mask_notifier *kimn); +void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq, + struct kvm_irq_mask_notifier *kimn); +void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned p= in, + bool mask); + #ifdef DEBUG #define ASSERT(x) \ do { \ diff --git a/arch/x86/kvm/irq_comm.c b/arch/x86/kvm/irq_comm.c index a4ef150fdd1c..fc0fa8155882 100644 --- a/arch/x86/kvm/irq_comm.c +++ b/arch/x86/kvm/irq_comm.c @@ -161,39 +161,6 @@ int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_ro= uting_entry *e, return -EWOULDBLOCK; } =20 -void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq, - struct kvm_irq_mask_notifier *kimn) -{ - mutex_lock(&kvm->irq_lock); - kimn->irq =3D irq; - hlist_add_head_rcu(&kimn->link, &kvm->arch.mask_notifier_list); - mutex_unlock(&kvm->irq_lock); -} - -void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq, - struct kvm_irq_mask_notifier *kimn) -{ - mutex_lock(&kvm->irq_lock); - hlist_del_rcu(&kimn->link); - mutex_unlock(&kvm->irq_lock); - synchronize_srcu(&kvm->irq_srcu); -} - -void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned p= in, - bool mask) -{ - struct kvm_irq_mask_notifier *kimn; - int idx, gsi; - - idx =3D srcu_read_lock(&kvm->irq_srcu); - gsi =3D kvm_irq_map_chip_pin(kvm, irqchip, pin); - if (gsi !=3D -1) - hlist_for_each_entry_rcu(kimn, &kvm->arch.mask_notifier_list, link) - if (kimn->irq =3D=3D gsi) - kimn->func(kimn, mask); - srcu_read_unlock(&kvm->irq_srcu, idx); -} - bool kvm_arch_can_set_irq_routing(struct kvm *kvm) { return irqchip_in_kernel(kvm); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 52eff4919d95..3ac6f7c83a06 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12799,7 +12799,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long= type) if (ret) goto out_uninit_mmu; =20 - INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); atomic_set(&kvm->arch.noncoherent_dma_count, 0); =20 raw_spin_lock_init(&kvm->arch.tsc_write_lock); --=20 2.49.0.1101.gccaa498523-goog From nobody Fri Dec 19 14:23:30 2025 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 7EA8D22579E for ; Mon, 19 May 2025 23:28:39 +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=1747697321; cv=none; b=j4d2f++wq8zvy33lMh175csc1ZM6dYLq7l4UyJy6txOm5WNKEe9Vuwx+W/b80d95dCG2DsREhHaAV+RN0CWwFTBrWsPt0Q8pouvoBd4jSUh6oW2QuuYcuxOPoKfQaHE7z0dFtTqu+aRKy1m/mr+MFvah7GLzd903vjpn7m9q8x0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747697321; c=relaxed/simple; bh=2h+R3oCjDJHijaqD59kpzjyUlIgPd4MTEsYrNuKVieg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VWKjouB3qmUvWwllrEM/8OvluZoa7Xll1fC+Uoj9vs3xUAL5wbxWPr/4M2ndIDqIkQGXv+YkTZfKBPaBHjWsTdJhc/qo2onyciFlR4y7jZ6QqGSRndZYfSxeXrYgJYplUgUITMNMK4SQ9xP6i8YHTE98hoxQRSJdTroWjwyP1kc= 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=W0kDoOx7; 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="W0kDoOx7" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-b26e120e300so4641823a12.1 for ; Mon, 19 May 2025 16:28:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747697319; x=1748302119; 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=GYplnj3zQPro5+YI4R9fT1vffU3AD1XGCuuLElZqFbg=; b=W0kDoOx7AhzCdNd+ZSmlzggf5Q/iSt9cUJ9y8EvM3vvJq3fzO/84Tt78lBQMkBWzYn Obu9e6IKKEnEC/O1aoQ1WpSl13vpyhcEqHN5fX42tfi1uY9uqE4nIwbWyliVpU2Mrmpm WmDCwz6cCsY3IPjQ3uYXaAa0py6RSg2hPJU0wrf/yZCRF8Y7j4lOwd800GaHMjf8BOha OMgXnutivfkrD3ba0CaRKvZqsBThO56XgjsRlc1/YpqCo0zyI21xaLc8PEr5d+zl/LCs 25Xj2Im2MsOrnEIoXlDHyjIEsPMu/EeYucDbdg9bY8Wc0ya5IzHhem8STjWIjDpKldEY 3l6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747697319; x=1748302119; 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=GYplnj3zQPro5+YI4R9fT1vffU3AD1XGCuuLElZqFbg=; b=oqvPTg0oruvJSrsGmXtEct7JwmC+nAd0AnPhB+LM8ulsSDDHJDICn4IRVXu02R72cN NWcVTdoAHq1YasTV3ytdmUzFw0hq8H8rc3OeDvw2arWRw+TJrJBNx1ViCL4z8fI7j/be 57JC5qdwu3BvtjOnMX5bBOQ/8HTERqDZKs92x9Mdg8sM/aNLnaEX7TcbfqjKiHVLLAhP KGsgfzGdcSaqIjq1gIwYuOaRHi9GHpZd+LIhsrK7gGyDlCzavxhGtoPYofWJlt5GcWHF O1/RPPIP0lZpYo4BnXSDX/99jRY4CmeZ3sGfcSk61nhCRTT+o2Kw8HvaL6vvc2xehZ3g tiCg== X-Forwarded-Encrypted: i=1; AJvYcCVRHgIFFtHzf4hbXj87tmRjqMqpHujvQcLKeS3WO0uV3v72DqwGydVp3ArEKXowZsWLAH87pNAS3I4TGdc=@vger.kernel.org X-Gm-Message-State: AOJu0YxsyvDHkLc+JvFvPuTY0Ib2Qi81UT2mPxeUK5psy/zu1c1rgU/j IA3Fspb16kG7l3j3pEO5AE5L4w0sr4ukPFkBe/LxJIDr8uH18skNovLj3VmQV2eQVlCxZk9Q+Nq GHHlTgQ== X-Google-Smtp-Source: AGHT+IGWa46bD0SKjXEZmQKHNSJP4I6FRXvqvQ4YBRiR5zoAR4K89jbzjqICZAPpFfqaZdmJiiLtMykGVOw= X-Received: from pjbsw12.prod.google.com ([2002:a17:90b:2c8c:b0:2ea:5084:5297]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3f4c:b0:2ee:5958:828 with SMTP id 98e67ed59e1d1-30e7d522155mr25648574a91.9.1747697319047; Mon, 19 May 2025 16:28:39 -0700 (PDT) Reply-To: Sean Christopherson Date: Mon, 19 May 2025 16:28:08 -0700 In-Reply-To: <20250519232808.2745331-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: <20250519232808.2745331-1-seanjc@google.com> X-Mailer: git-send-email 2.49.0.1101.gccaa498523-goog Message-ID: <20250519232808.2745331-16-seanjc@google.com> Subject: [PATCH 15/15] KVM: x86: Fold irq_comm.c into irq.c From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop irq_comm.c, a.k.a. common IRQ APIs, as there has been no non-x86 user since commit 003f7de62589 ("KVM: ia64: remove") (at the time, irq_comm.c lived in virt/kvm, not arch/x86/kvm). Suggested-by: Paolo Bonzini Signed-off-by: Sean Christopherson Acked-by: Kai Huang --- arch/x86/kvm/Makefile | 6 +- arch/x86/kvm/irq.c | 305 ++++++++++++++++++++++++++++++++++++- arch/x86/kvm/irq_comm.c | 325 ---------------------------------------- 3 files changed, 306 insertions(+), 330 deletions(-) delete mode 100644 arch/x86/kvm/irq_comm.c diff --git a/arch/x86/kvm/Makefile b/arch/x86/kvm/Makefile index 92c737257789..c4b8950c7abe 100644 --- a/arch/x86/kvm/Makefile +++ b/arch/x86/kvm/Makefile @@ -5,10 +5,8 @@ ccflags-$(CONFIG_KVM_WERROR) +=3D -Werror =20 include $(srctree)/virt/kvm/Makefile.kvm =20 -kvm-y +=3D x86.o emulate.o irq.o lapic.o \ - irq_comm.o cpuid.o pmu.o mtrr.o \ - debugfs.o mmu/mmu.o mmu/page_track.o \ - mmu/spte.o +kvm-y +=3D x86.o emulate.o irq.o lapic.o cpuid.o pmu.o mtrr.o \ + debugfs.o mmu/mmu.o mmu/page_track.o mmu/spte.o =20 kvm-$(CONFIG_X86_64) +=3D mmu/tdp_iter.o mmu/tdp_mmu.o kvm-$(CONFIG_KVM_IOAPIC) +=3D i8259.o i8254.o ioapic.o diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c index a416ccddde5f..314a93599942 100644 --- a/arch/x86/kvm/irq.c +++ b/arch/x86/kvm/irq.c @@ -12,8 +12,10 @@ #include #include =20 +#include "hyperv.h" +#include "ioapic.h" #include "irq.h" -#include "i8254.h" +#include "trace.h" #include "x86.h" #include "xen.h" =20 @@ -191,3 +193,304 @@ bool kvm_arch_irqchip_in_kernel(struct kvm *kvm) { return irqchip_in_kernel(kvm); } + +int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, + struct kvm_lapic_irq *irq, struct dest_map *dest_map) +{ + int r =3D -1; + struct kvm_vcpu *vcpu, *lowest =3D NULL; + unsigned long i, dest_vcpu_bitmap[BITS_TO_LONGS(KVM_MAX_VCPUS)]; + unsigned int dest_vcpus =3D 0; + + if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r, dest_map)) + return r; + + if (irq->dest_mode =3D=3D APIC_DEST_PHYSICAL && + irq->dest_id =3D=3D 0xff && kvm_lowest_prio_delivery(irq)) { + pr_info("apic: phys broadcast and lowest prio\n"); + irq->delivery_mode =3D APIC_DM_FIXED; + } + + memset(dest_vcpu_bitmap, 0, sizeof(dest_vcpu_bitmap)); + + kvm_for_each_vcpu(i, vcpu, kvm) { + if (!kvm_apic_present(vcpu)) + continue; + + if (!kvm_apic_match_dest(vcpu, src, irq->shorthand, + irq->dest_id, irq->dest_mode)) + continue; + + if (!kvm_lowest_prio_delivery(irq)) { + if (r < 0) + r =3D 0; + r +=3D kvm_apic_set_irq(vcpu, irq, dest_map); + } else if (kvm_apic_sw_enabled(vcpu->arch.apic)) { + if (!kvm_vector_hashing_enabled()) { + if (!lowest) + lowest =3D vcpu; + else if (kvm_apic_compare_prio(vcpu, lowest) < 0) + lowest =3D vcpu; + } else { + __set_bit(i, dest_vcpu_bitmap); + dest_vcpus++; + } + } + } + + if (dest_vcpus !=3D 0) { + int idx =3D kvm_vector_to_index(irq->vector, dest_vcpus, + dest_vcpu_bitmap, KVM_MAX_VCPUS); + + lowest =3D kvm_get_vcpu(kvm, idx); + } + + if (lowest) + r =3D kvm_apic_set_irq(lowest, irq, dest_map); + + return r; +} + +void kvm_set_msi_irq(struct kvm *kvm, struct kvm_kernel_irq_routing_entry = *e, + struct kvm_lapic_irq *irq) +{ + struct msi_msg msg =3D { .address_lo =3D e->msi.address_lo, + .address_hi =3D e->msi.address_hi, + .data =3D e->msi.data }; + + trace_kvm_msi_set_irq(msg.address_lo | (kvm->arch.x2apic_format ? + (u64)msg.address_hi << 32 : 0), msg.data); + + irq->dest_id =3D x86_msi_msg_get_destid(&msg, kvm->arch.x2apic_format); + irq->vector =3D msg.arch_data.vector; + irq->dest_mode =3D kvm_lapic_irq_dest_mode(msg.arch_addr_lo.dest_mode_log= ical); + irq->trig_mode =3D msg.arch_data.is_level; + irq->delivery_mode =3D msg.arch_data.delivery_mode << 8; + irq->msi_redir_hint =3D msg.arch_addr_lo.redirect_hint; + irq->level =3D 1; + irq->shorthand =3D APIC_DEST_NOSHORT; +} +EXPORT_SYMBOL_GPL(kvm_set_msi_irq); + +static inline bool kvm_msi_route_invalid(struct kvm *kvm, + struct kvm_kernel_irq_routing_entry *e) +{ + return kvm->arch.x2apic_format && (e->msi.address_hi & 0xff); +} + +int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, + struct kvm *kvm, int irq_source_id, int level, bool line_status) +{ + struct kvm_lapic_irq irq; + + if (kvm_msi_route_invalid(kvm, e)) + return -EINVAL; + + if (!level) + return -1; + + kvm_set_msi_irq(kvm, e, &irq); + + return kvm_irq_delivery_to_apic(kvm, NULL, &irq, NULL); +} + +int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e, + struct kvm *kvm, int irq_source_id, int level, + bool line_status) +{ + struct kvm_lapic_irq irq; + int r; + + switch (e->type) { +#ifdef CONFIG_KVM_HYPERV + case KVM_IRQ_ROUTING_HV_SINT: + return kvm_hv_set_sint(e, kvm, irq_source_id, level, + line_status); +#endif + + case KVM_IRQ_ROUTING_MSI: + if (kvm_msi_route_invalid(kvm, e)) + return -EINVAL; + + kvm_set_msi_irq(kvm, e, &irq); + + if (kvm_irq_delivery_to_apic_fast(kvm, NULL, &irq, &r, NULL)) + return r; + break; + +#ifdef CONFIG_KVM_XEN + case KVM_IRQ_ROUTING_XEN_EVTCHN: + if (!level) + return -1; + + return kvm_xen_set_evtchn_fast(&e->xen_evtchn, kvm); +#endif + default: + break; + } + + return -EWOULDBLOCK; +} + +bool kvm_arch_can_set_irq_routing(struct kvm *kvm) +{ + return irqchip_in_kernel(kvm); +} + +int kvm_set_routing_entry(struct kvm *kvm, + struct kvm_kernel_irq_routing_entry *e, + const struct kvm_irq_routing_entry *ue) +{ + /* We can't check irqchip_in_kernel() here as some callers are + * currently initializing the irqchip. Other callers should therefore + * check kvm_arch_can_set_irq_routing() before calling this function. + */ + switch (ue->type) { +#ifdef CONFIG_KVM_IOAPIC + case KVM_IRQ_ROUTING_IRQCHIP: + if (irqchip_split(kvm)) + return -EINVAL; + e->irqchip.pin =3D ue->u.irqchip.pin; + switch (ue->u.irqchip.irqchip) { + case KVM_IRQCHIP_PIC_SLAVE: + e->irqchip.pin +=3D PIC_NUM_PINS / 2; + fallthrough; + case KVM_IRQCHIP_PIC_MASTER: + if (ue->u.irqchip.pin >=3D PIC_NUM_PINS / 2) + return -EINVAL; + e->set =3D kvm_pic_set_irq; + break; + case KVM_IRQCHIP_IOAPIC: + if (ue->u.irqchip.pin >=3D KVM_IOAPIC_NUM_PINS) + return -EINVAL; + e->set =3D kvm_ioapic_set_irq; + break; + default: + return -EINVAL; + } + e->irqchip.irqchip =3D ue->u.irqchip.irqchip; + break; +#endif + case KVM_IRQ_ROUTING_MSI: + e->set =3D kvm_set_msi; + e->msi.address_lo =3D ue->u.msi.address_lo; + e->msi.address_hi =3D ue->u.msi.address_hi; + e->msi.data =3D ue->u.msi.data; + + if (kvm_msi_route_invalid(kvm, e)) + return -EINVAL; + break; +#ifdef CONFIG_KVM_HYPERV + case KVM_IRQ_ROUTING_HV_SINT: + e->set =3D kvm_hv_set_sint; + e->hv_sint.vcpu =3D ue->u.hv_sint.vcpu; + e->hv_sint.sint =3D ue->u.hv_sint.sint; + break; +#endif +#ifdef CONFIG_KVM_XEN + case KVM_IRQ_ROUTING_XEN_EVTCHN: + return kvm_xen_setup_evtchn(kvm, e, ue); +#endif + default: + return -EINVAL; + } + + return 0; +} + +bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq, + struct kvm_vcpu **dest_vcpu) +{ + int r =3D 0; + unsigned long i; + struct kvm_vcpu *vcpu; + + if (kvm_intr_is_single_vcpu_fast(kvm, irq, dest_vcpu)) + return true; + + kvm_for_each_vcpu(i, vcpu, kvm) { + if (!kvm_apic_present(vcpu)) + continue; + + if (!kvm_apic_match_dest(vcpu, NULL, irq->shorthand, + irq->dest_id, irq->dest_mode)) + continue; + + if (++r =3D=3D 2) + return false; + + *dest_vcpu =3D vcpu; + } + + return r =3D=3D 1; +} +EXPORT_SYMBOL_GPL(kvm_intr_is_single_vcpu); + +void kvm_scan_ioapic_irq(struct kvm_vcpu *vcpu, u32 dest_id, u16 dest_mode, + u8 vector, unsigned long *ioapic_handled_vectors) +{ + /* + * Intercept EOI if the vCPU is the target of the new IRQ routing, or + * the vCPU has a pending IRQ from the old routing, i.e. if the vCPU + * may receive a level-triggered IRQ in the future, or already received + * level-triggered IRQ. The EOI needs to be intercepted and forwarded + * to I/O APIC emulation so that the IRQ can be de-asserted. + */ + if (kvm_apic_match_dest(vcpu, NULL, APIC_DEST_NOSHORT, dest_id, dest_mode= )) { + __set_bit(vector, ioapic_handled_vectors); + } else if (kvm_apic_pending_eoi(vcpu, vector)) { + __set_bit(vector, ioapic_handled_vectors); + + /* + * Track the highest pending EOI for which the vCPU is NOT the + * target in the new routing. Only the EOI for the IRQ that is + * in-flight (for the old routing) needs to be intercepted, any + * future IRQs that arrive on this vCPU will be coincidental to + * the level-triggered routing and don't need to be intercepted. + */ + if ((int)vector > vcpu->arch.highest_stale_pending_ioapic_eoi) + vcpu->arch.highest_stale_pending_ioapic_eoi =3D vector; + } +} + +void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu, + ulong *ioapic_handled_vectors) +{ + struct kvm *kvm =3D vcpu->kvm; + struct kvm_kernel_irq_routing_entry *entry; + struct kvm_irq_routing_table *table; + u32 i, nr_ioapic_pins; + int idx; + + idx =3D srcu_read_lock(&kvm->irq_srcu); + table =3D srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); + nr_ioapic_pins =3D min_t(u32, table->nr_rt_entries, + kvm->arch.nr_reserved_ioapic_pins); + for (i =3D 0; i < nr_ioapic_pins; ++i) { + hlist_for_each_entry(entry, &table->map[i], link) { + struct kvm_lapic_irq irq; + + if (entry->type !=3D KVM_IRQ_ROUTING_MSI) + continue; + + kvm_set_msi_irq(vcpu->kvm, entry, &irq); + + if (!irq.trig_mode) + continue; + + kvm_scan_ioapic_irq(vcpu, irq.dest_id, irq.dest_mode, + irq.vector, ioapic_handled_vectors); + } + } + srcu_read_unlock(&kvm->irq_srcu, idx); +} + +void kvm_arch_irq_routing_update(struct kvm *kvm) +{ +#ifdef CONFIG_KVM_HYPERV + kvm_hv_irq_routing_update(kvm); +#endif + + if (irqchip_split(kvm)) + kvm_make_scan_ioapic_request(kvm); +} diff --git a/arch/x86/kvm/irq_comm.c b/arch/x86/kvm/irq_comm.c deleted file mode 100644 index fc0fa8155882..000000000000 --- a/arch/x86/kvm/irq_comm.c +++ /dev/null @@ -1,325 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-only -/* - * irq_comm.c: Common API for in kernel interrupt controller - * Copyright (c) 2007, Intel Corporation. - * - * Authors: - * Yaozu (Eddie) Dong - * - * Copyright 2010 Red Hat, Inc. and/or its affiliates. - */ -#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt - -#include -#include -#include -#include - -#include "hyperv.h" -#include "ioapic.h" -#include "irq.h" -#include "lapic.h" -#include "trace.h" -#include "x86.h" -#include "xen.h" - -int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, - struct kvm_lapic_irq *irq, struct dest_map *dest_map) -{ - int r =3D -1; - struct kvm_vcpu *vcpu, *lowest =3D NULL; - unsigned long i, dest_vcpu_bitmap[BITS_TO_LONGS(KVM_MAX_VCPUS)]; - unsigned int dest_vcpus =3D 0; - - if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r, dest_map)) - return r; - - if (irq->dest_mode =3D=3D APIC_DEST_PHYSICAL && - irq->dest_id =3D=3D 0xff && kvm_lowest_prio_delivery(irq)) { - pr_info("apic: phys broadcast and lowest prio\n"); - irq->delivery_mode =3D APIC_DM_FIXED; - } - - memset(dest_vcpu_bitmap, 0, sizeof(dest_vcpu_bitmap)); - - kvm_for_each_vcpu(i, vcpu, kvm) { - if (!kvm_apic_present(vcpu)) - continue; - - if (!kvm_apic_match_dest(vcpu, src, irq->shorthand, - irq->dest_id, irq->dest_mode)) - continue; - - if (!kvm_lowest_prio_delivery(irq)) { - if (r < 0) - r =3D 0; - r +=3D kvm_apic_set_irq(vcpu, irq, dest_map); - } else if (kvm_apic_sw_enabled(vcpu->arch.apic)) { - if (!kvm_vector_hashing_enabled()) { - if (!lowest) - lowest =3D vcpu; - else if (kvm_apic_compare_prio(vcpu, lowest) < 0) - lowest =3D vcpu; - } else { - __set_bit(i, dest_vcpu_bitmap); - dest_vcpus++; - } - } - } - - if (dest_vcpus !=3D 0) { - int idx =3D kvm_vector_to_index(irq->vector, dest_vcpus, - dest_vcpu_bitmap, KVM_MAX_VCPUS); - - lowest =3D kvm_get_vcpu(kvm, idx); - } - - if (lowest) - r =3D kvm_apic_set_irq(lowest, irq, dest_map); - - return r; -} - -void kvm_set_msi_irq(struct kvm *kvm, struct kvm_kernel_irq_routing_entry = *e, - struct kvm_lapic_irq *irq) -{ - struct msi_msg msg =3D { .address_lo =3D e->msi.address_lo, - .address_hi =3D e->msi.address_hi, - .data =3D e->msi.data }; - - trace_kvm_msi_set_irq(msg.address_lo | (kvm->arch.x2apic_format ? - (u64)msg.address_hi << 32 : 0), msg.data); - - irq->dest_id =3D x86_msi_msg_get_destid(&msg, kvm->arch.x2apic_format); - irq->vector =3D msg.arch_data.vector; - irq->dest_mode =3D kvm_lapic_irq_dest_mode(msg.arch_addr_lo.dest_mode_log= ical); - irq->trig_mode =3D msg.arch_data.is_level; - irq->delivery_mode =3D msg.arch_data.delivery_mode << 8; - irq->msi_redir_hint =3D msg.arch_addr_lo.redirect_hint; - irq->level =3D 1; - irq->shorthand =3D APIC_DEST_NOSHORT; -} -EXPORT_SYMBOL_GPL(kvm_set_msi_irq); - -static inline bool kvm_msi_route_invalid(struct kvm *kvm, - struct kvm_kernel_irq_routing_entry *e) -{ - return kvm->arch.x2apic_format && (e->msi.address_hi & 0xff); -} - -int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, - struct kvm *kvm, int irq_source_id, int level, bool line_status) -{ - struct kvm_lapic_irq irq; - - if (kvm_msi_route_invalid(kvm, e)) - return -EINVAL; - - if (!level) - return -1; - - kvm_set_msi_irq(kvm, e, &irq); - - return kvm_irq_delivery_to_apic(kvm, NULL, &irq, NULL); -} - -int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e, - struct kvm *kvm, int irq_source_id, int level, - bool line_status) -{ - struct kvm_lapic_irq irq; - int r; - - switch (e->type) { -#ifdef CONFIG_KVM_HYPERV - case KVM_IRQ_ROUTING_HV_SINT: - return kvm_hv_set_sint(e, kvm, irq_source_id, level, - line_status); -#endif - - case KVM_IRQ_ROUTING_MSI: - if (kvm_msi_route_invalid(kvm, e)) - return -EINVAL; - - kvm_set_msi_irq(kvm, e, &irq); - - if (kvm_irq_delivery_to_apic_fast(kvm, NULL, &irq, &r, NULL)) - return r; - break; - -#ifdef CONFIG_KVM_XEN - case KVM_IRQ_ROUTING_XEN_EVTCHN: - if (!level) - return -1; - - return kvm_xen_set_evtchn_fast(&e->xen_evtchn, kvm); -#endif - default: - break; - } - - return -EWOULDBLOCK; -} - -bool kvm_arch_can_set_irq_routing(struct kvm *kvm) -{ - return irqchip_in_kernel(kvm); -} - -int kvm_set_routing_entry(struct kvm *kvm, - struct kvm_kernel_irq_routing_entry *e, - const struct kvm_irq_routing_entry *ue) -{ - /* We can't check irqchip_in_kernel() here as some callers are - * currently initializing the irqchip. Other callers should therefore - * check kvm_arch_can_set_irq_routing() before calling this function. - */ - switch (ue->type) { -#ifdef CONFIG_KVM_IOAPIC - case KVM_IRQ_ROUTING_IRQCHIP: - if (irqchip_split(kvm)) - return -EINVAL; - e->irqchip.pin =3D ue->u.irqchip.pin; - switch (ue->u.irqchip.irqchip) { - case KVM_IRQCHIP_PIC_SLAVE: - e->irqchip.pin +=3D PIC_NUM_PINS / 2; - fallthrough; - case KVM_IRQCHIP_PIC_MASTER: - if (ue->u.irqchip.pin >=3D PIC_NUM_PINS / 2) - return -EINVAL; - e->set =3D kvm_pic_set_irq; - break; - case KVM_IRQCHIP_IOAPIC: - if (ue->u.irqchip.pin >=3D KVM_IOAPIC_NUM_PINS) - return -EINVAL; - e->set =3D kvm_ioapic_set_irq; - break; - default: - return -EINVAL; - } - e->irqchip.irqchip =3D ue->u.irqchip.irqchip; - break; -#endif - case KVM_IRQ_ROUTING_MSI: - e->set =3D kvm_set_msi; - e->msi.address_lo =3D ue->u.msi.address_lo; - e->msi.address_hi =3D ue->u.msi.address_hi; - e->msi.data =3D ue->u.msi.data; - - if (kvm_msi_route_invalid(kvm, e)) - return -EINVAL; - break; -#ifdef CONFIG_KVM_HYPERV - case KVM_IRQ_ROUTING_HV_SINT: - e->set =3D kvm_hv_set_sint; - e->hv_sint.vcpu =3D ue->u.hv_sint.vcpu; - e->hv_sint.sint =3D ue->u.hv_sint.sint; - break; -#endif -#ifdef CONFIG_KVM_XEN - case KVM_IRQ_ROUTING_XEN_EVTCHN: - return kvm_xen_setup_evtchn(kvm, e, ue); -#endif - default: - return -EINVAL; - } - - return 0; -} - -bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq, - struct kvm_vcpu **dest_vcpu) -{ - int r =3D 0; - unsigned long i; - struct kvm_vcpu *vcpu; - - if (kvm_intr_is_single_vcpu_fast(kvm, irq, dest_vcpu)) - return true; - - kvm_for_each_vcpu(i, vcpu, kvm) { - if (!kvm_apic_present(vcpu)) - continue; - - if (!kvm_apic_match_dest(vcpu, NULL, irq->shorthand, - irq->dest_id, irq->dest_mode)) - continue; - - if (++r =3D=3D 2) - return false; - - *dest_vcpu =3D vcpu; - } - - return r =3D=3D 1; -} -EXPORT_SYMBOL_GPL(kvm_intr_is_single_vcpu); - -void kvm_scan_ioapic_irq(struct kvm_vcpu *vcpu, u32 dest_id, u16 dest_mode, - u8 vector, unsigned long *ioapic_handled_vectors) -{ - /* - * Intercept EOI if the vCPU is the target of the new IRQ routing, or - * the vCPU has a pending IRQ from the old routing, i.e. if the vCPU - * may receive a level-triggered IRQ in the future, or already received - * level-triggered IRQ. The EOI needs to be intercepted and forwarded - * to I/O APIC emulation so that the IRQ can be de-asserted. - */ - if (kvm_apic_match_dest(vcpu, NULL, APIC_DEST_NOSHORT, dest_id, dest_mode= )) { - __set_bit(vector, ioapic_handled_vectors); - } else if (kvm_apic_pending_eoi(vcpu, vector)) { - __set_bit(vector, ioapic_handled_vectors); - - /* - * Track the highest pending EOI for which the vCPU is NOT the - * target in the new routing. Only the EOI for the IRQ that is - * in-flight (for the old routing) needs to be intercepted, any - * future IRQs that arrive on this vCPU will be coincidental to - * the level-triggered routing and don't need to be intercepted. - */ - if ((int)vector > vcpu->arch.highest_stale_pending_ioapic_eoi) - vcpu->arch.highest_stale_pending_ioapic_eoi =3D vector; - } -} - -void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu, - ulong *ioapic_handled_vectors) -{ - struct kvm *kvm =3D vcpu->kvm; - struct kvm_kernel_irq_routing_entry *entry; - struct kvm_irq_routing_table *table; - u32 i, nr_ioapic_pins; - int idx; - - idx =3D srcu_read_lock(&kvm->irq_srcu); - table =3D srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); - nr_ioapic_pins =3D min_t(u32, table->nr_rt_entries, - kvm->arch.nr_reserved_ioapic_pins); - for (i =3D 0; i < nr_ioapic_pins; ++i) { - hlist_for_each_entry(entry, &table->map[i], link) { - struct kvm_lapic_irq irq; - - if (entry->type !=3D KVM_IRQ_ROUTING_MSI) - continue; - - kvm_set_msi_irq(vcpu->kvm, entry, &irq); - - if (!irq.trig_mode) - continue; - - kvm_scan_ioapic_irq(vcpu, irq.dest_id, irq.dest_mode, - irq.vector, ioapic_handled_vectors); - } - } - srcu_read_unlock(&kvm->irq_srcu, idx); -} - -void kvm_arch_irq_routing_update(struct kvm *kvm) -{ -#ifdef CONFIG_KVM_HYPERV - kvm_hv_irq_routing_update(kvm); -#endif - - if (irqchip_split(kvm)) - kvm_make_scan_ioapic_request(kvm); -} --=20 2.49.0.1101.gccaa498523-goog