From nobody Sun Feb 8 00:12:21 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1EB84145A11 for ; Tue, 4 Mar 2025 01:33:40 +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=1741052022; cv=none; b=B7whhxFJhwFNNwz0iaSp2zf6sBng3kryR7OgIbmQQrmjy9SjtYeTI2Cd2wFPbLzmcQAS3WB+Ffecdo2nfxz3UDAwSsqu7E+RRDEI5Aa6MwBOfaPUEZWz880f6luRGN5KkpmMHKXMRIjDTfBIuMCLzoMubbAG3IhZbR0k1LdDfdo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741052022; c=relaxed/simple; bh=58+GQHt2qduafXX+TgqOXjvPQXwK8jv6jk++1FQYVOU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Mg0ca8oBs5HtXSU7oxglnbt0hJ5ijgGJ5LLeAB/MDL6Q6I36l4B8CNnN4L9qQfTMI/dBS56wOo8a3QC8uNZQa1tgb7v3lYk7117H8L7UAgClVKtaApJMjG8p3Z+2/EKvCsiFtJTvGSSIUmzP5hYzUSuO7V8X8p/b+eE46Jbm3m0= 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=TdqeXcPA; 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="TdqeXcPA" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2feb4648d4dso14041105a91.1 for ; Mon, 03 Mar 2025 17:33:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741052020; x=1741656820; 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=V2gfYfzd7JIdYMe13GtAqBNZ71NvSFhhiOtC85P36nM=; b=TdqeXcPA1URcMscMukMgr4/hWRUQfyQg+Bn7CZQSWwwGo14bjpgexm17I074+TkznE HFiS6yBDvEPs2IZ7Zqdr2kJNt57uVztPl6LQ4fWNX2Pvi4+i0OdK0IBT+NsD6MtkqmfS 5XI2+WjDNQkrTYjzEsALrMQjCqyRFkrF0W+wHexTCNThIks7ZJIeCFNGRfYysm9bq/RC LcqNbPkX0NbH2ese/aphrpXm3pIFRvH4luZOA843nG3Ye6D9scIH6jhDupvTwqCo15uV D6hM9F3QGUiGnzHmea0nXU2ed/XKaMQuOO6d8+4w9w+SllQ4Z3bFSaWl7+v3EkZU4aE/ 8AlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741052020; x=1741656820; 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=V2gfYfzd7JIdYMe13GtAqBNZ71NvSFhhiOtC85P36nM=; b=gIC7iLCMbOx8dtn9TRWW5CDzg1DcW30QxOpeB7cCentyizKvG+meK+khfu36oNwKbN UA1HJ/hL+jLcSwsNZEwk1AKawqPeeFLPMzF3C6KpuCpSixkS9XtCX6S/xrINeul5MjOT du24dQhEl3Y7Ll5fbVBH3+xwxEVTwsI7VNUyK2N4yLE850+zkBek6Ymvy3/uZHnMwfaL DSt9MlacopdaptQOls7QeDAr+cQRSLW2Azvzn7PytJfSJdflybRFHG96eQhf2apEe42h EwHUGB3SS8gFd6TTs9ykh1l+0HKwOms64s5tCDm62a/yI6/Vgnb0J5dUnqKmu8zOguwh Gjdw== X-Forwarded-Encrypted: i=1; AJvYcCViKe1+EyidnH09fh40qiYr6yLk2rMnExKPJbFg6QBqQ02+Wz6HJZI7RSDdjegz1Vgzd77zjNiE90PvODM=@vger.kernel.org X-Gm-Message-State: AOJu0YwTvTM/AwwHAAtx5sIworf1B/gjcSYVQiZ7pn8HyrxABXIGQxNR 2QCkIgyDHCTrJX+sYNnuvNlXcoSX1aHqUrbBMFREKQlQQMKDus7wR51YVVGXIQi444UdxPm56tP GTA== X-Google-Smtp-Source: AGHT+IEC6xZmbOA1zeqtVe50RifjBGDvKSe7UtTePMqhcFtWNi+tt3qWgCKnd/Zv8ZReH+DmFDKZv1kNGFA= X-Received: from pjbee14.prod.google.com ([2002:a17:90a:fc4e:b0:2ea:5469:76c2]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:384c:b0:2fe:8694:3956 with SMTP id 98e67ed59e1d1-2febab787d6mr27249929a91.16.1741052020443; Mon, 03 Mar 2025 17:33:40 -0800 (PST) Reply-To: Sean Christopherson Date: Mon, 3 Mar 2025 17:33:33 -0800 In-Reply-To: <20250304013335.4155703-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: <20250304013335.4155703-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.711.g2feabab25a-goog Message-ID: <20250304013335.4155703-2-seanjc@google.com> Subject: [PATCH v5 1/3] KVM: x86: Isolate edge vs. level check in userspace I/O APIC route scanning From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kai Huang , xuyun , weizijie Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extract and isolate the trigger mode check in kvm_scan_ioapic_routes() in anticipation of moving destination matching logic to a common helper (for userspace vs. in-kernel I/O APIC emulation). No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang --- arch/x86/kvm/irq_comm.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/irq_comm.c b/arch/x86/kvm/irq_comm.c index 8136695f7b96..866f84392797 100644 --- a/arch/x86/kvm/irq_comm.c +++ b/arch/x86/kvm/irq_comm.c @@ -424,10 +424,12 @@ void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu, =20 kvm_set_msi_irq(vcpu->kvm, entry, &irq); =20 - if (irq.trig_mode && - (kvm_apic_match_dest(vcpu, NULL, APIC_DEST_NOSHORT, - irq.dest_id, irq.dest_mode) || - kvm_apic_pending_eoi(vcpu, irq.vector))) + if (!irq.trig_mode) + continue; + + if (kvm_apic_match_dest(vcpu, NULL, APIC_DEST_NOSHORT, + irq.dest_id, irq.dest_mode) || + kvm_apic_pending_eoi(vcpu, irq.vector)) __set_bit(irq.vector, ioapic_handled_vectors); } } --=20 2.48.1.711.g2feabab25a-goog From nobody Sun Feb 8 00:12:21 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5CA01ACED1 for ; Tue, 4 Mar 2025 01:33:42 +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=1741052024; cv=none; b=EdTyOfvVh5cqRx3RvZ9sVl274xgDBtvk+gq8BwDbH7nQQiqngt48BVrQ85DOgsCdnDb28CHWDmjHDoItZ3Z2Xuo5RuZdlmnGxiIBaU/zzLoCvUQLdqitG8/ST1JCSrm39trjlkOvC92umzBV2f4UrdhcoaNG20JisI0ngrqyyNU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741052024; c=relaxed/simple; bh=bWa7MI7Je53+hcO260m6tDnoOA7tYXPBsMlcHDj5OIo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tq4rIPA+DcbptUCid5guMMmoIfOH2VKnYjV3RW1VExoX1yd6RMEOp1NiYgBW7/54PUYmMAgFspJi0qKsdIVU6Sdf3R7Cresx7y8T2vox09HESOMRr4ZPKRe0dNjQL0BTKceldTd7LAA8VqWdKvbzg2hoWo3PF822e1HsA3CGROQ= 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=epF1dJPS; 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="epF1dJPS" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2fe870bc003so10197311a91.1 for ; Mon, 03 Mar 2025 17:33:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741052022; x=1741656822; 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=X9wy79sbNpkEBREyMkIDmJ5bL01i0GTTP9jdKigT/f8=; b=epF1dJPSJoIjx/5l9rks5Ltj6C0W3uT4vbDwj3VYDbaVcWrMTOKLshVh6dX4Zgku+0 +ely5Tt3LYMysBckLf1irSrm8/tGv38riOLZTbDp+1McTRBna2xoo61Tc52NQjLFkjuw OvQVmuc+S4Y9jwpeUW51GG/Zkc/8/LM/NssyL0+nIZBrBMV5U/BKzSTCN9P0mAAy+YC4 gMdVgNn0u0RAoyU3nsgjvngVWQz+0qRS0OUnvxMpr1/x+9NmpXI+iqAX2h5m4AWjfrzb QPKgYAy0nVddeTg8vy/8Wla0mgW4/GA4X0hIJBZ9BWQhBW5jcmAXrlK/6x5Vk5hk17Kf KkTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741052022; x=1741656822; 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=X9wy79sbNpkEBREyMkIDmJ5bL01i0GTTP9jdKigT/f8=; b=kHYu9FT6dbW1SkKf9yNtBBFYdznKT+ZCxxa7z/Kq1dq9UTGQpsBpjmcon0Bw0PBc+h I1tULPVEVP/VmXEALmwMv3PizJ8Lh0PgbyJDvJaswW5D2v7Co5LdxAETSzbkwE/UEe1n b/y5lynTBdV5efcCeDXXvPKyIE7FJ45cUy2+gj944uZfdJuhD7ShWlKlLWt/9CLOpoJx RGjmWFHUteEfJSKoy6SMEzN4WNl4uJLqIGy/6wIHo08DiXnfDmT843jJOFk4E4NJD1yv KGFL9Z3JtsYm9VcFYBdHTstNVLB1Zc/AUGawV83WIGoXHPqRf3WFcB3l67jBekcawQ83 oWOg== X-Forwarded-Encrypted: i=1; AJvYcCWMILCG/KLIhdGyp/vDPtHDgY9peWUb3lld9Fh53ZaXBPTnnfFq9kqjE+iXfGeeYuk+HCx3k/tiyzweyQU=@vger.kernel.org X-Gm-Message-State: AOJu0YySlaQMzyqHrz0INIpf8vA39dmUZ4Z4fjXqIVBwBjHHCv5AhS92 0HGnJG+df8gQUvcMVYtx4wPUECe+fDjMNBBzOprXtRC9xyosBpBNdB7bpca0gmxFaeOMJI5+JoO nrQ== X-Google-Smtp-Source: AGHT+IGVk1UjX3zn4sKZXBI9Q4g1z0uT75lXD5hnr1yXiiySOX13OZkHBINhW/GTF9uaoBbYjX8IGddC1WU= X-Received: from pjbsj5.prod.google.com ([2002:a17:90b:2d85:b0:2f9:c349:2f84]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:d2c3:b0:2f9:d0cd:3403 with SMTP id 98e67ed59e1d1-2ff33bdb4a2mr2138807a91.16.1741052022093; Mon, 03 Mar 2025 17:33:42 -0800 (PST) Reply-To: Sean Christopherson Date: Mon, 3 Mar 2025 17:33:34 -0800 In-Reply-To: <20250304013335.4155703-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: <20250304013335.4155703-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.711.g2feabab25a-goog Message-ID: <20250304013335.4155703-3-seanjc@google.com> Subject: [PATCH v5 2/3] KVM: x86: Add a helper to deduplicate I/O APIC EOI interception logic From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kai Huang , xuyun , weizijie Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extract the vCPU specific EOI interception logic for I/O APIC emulation into a common helper for userspace and in-kernel emulation in anticipation of optimizing the "pending EOI" case. No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang --- arch/x86/kvm/ioapic.c | 7 ++----- arch/x86/kvm/ioapic.h | 2 ++ arch/x86/kvm/irq_comm.c | 21 +++++++++++++++++---- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/ioapic.c b/arch/x86/kvm/ioapic.c index 995eb5054360..45dae2d5d2f1 100644 --- a/arch/x86/kvm/ioapic.c +++ b/arch/x86/kvm/ioapic.c @@ -296,11 +296,8 @@ void kvm_ioapic_scan_entry(struct kvm_vcpu *vcpu, ulon= g *ioapic_handled_vectors) index =3D=3D RTC_GSI) { u16 dm =3D kvm_lapic_irq_dest_mode(!!e->fields.dest_mode); =20 - if (kvm_apic_match_dest(vcpu, NULL, APIC_DEST_NOSHORT, - e->fields.dest_id, dm) || - kvm_apic_pending_eoi(vcpu, e->fields.vector)) - __set_bit(e->fields.vector, - ioapic_handled_vectors); + kvm_scan_ioapic_irq(vcpu, e->fields.dest_id, dm, + e->fields.vector, ioapic_handled_vectors); } } spin_unlock(&ioapic->lock); diff --git a/arch/x86/kvm/ioapic.h b/arch/x86/kvm/ioapic.h index 539333ac4b38..aa8cb4ac0479 100644 --- a/arch/x86/kvm/ioapic.h +++ b/arch/x86/kvm/ioapic.h @@ -120,4 +120,6 @@ void kvm_ioapic_scan_entry(struct kvm_vcpu *vcpu, ulong *ioapic_handled_vectors); void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu, ulong *ioapic_handled_vectors); +void kvm_scan_ioapic_irq(struct kvm_vcpu *vcpu, u32 dest_id, u16 dest_mode, + u8 vector, unsigned long *ioapic_handled_vectors); #endif diff --git a/arch/x86/kvm/irq_comm.c b/arch/x86/kvm/irq_comm.c index 866f84392797..14590d9c4a37 100644 --- a/arch/x86/kvm/irq_comm.c +++ b/arch/x86/kvm/irq_comm.c @@ -402,6 +402,21 @@ void kvm_arch_post_irq_routing_update(struct kvm *kvm) kvm_make_scan_ioapic_request(kvm); } =20 +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= ) || + kvm_apic_pending_eoi(vcpu, vector)) + __set_bit(vector, ioapic_handled_vectors); +} + void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu, ulong *ioapic_handled_vectors) { @@ -427,10 +442,8 @@ void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu, if (!irq.trig_mode) continue; =20 - if (kvm_apic_match_dest(vcpu, NULL, APIC_DEST_NOSHORT, - irq.dest_id, irq.dest_mode) || - kvm_apic_pending_eoi(vcpu, irq.vector)) - __set_bit(irq.vector, ioapic_handled_vectors); + kvm_scan_ioapic_irq(vcpu, irq.dest_id, irq.dest_mode, + irq.vector, ioapic_handled_vectors); } } srcu_read_unlock(&kvm->irq_srcu, idx); --=20 2.48.1.711.g2feabab25a-goog From nobody Sun Feb 8 00:12:21 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A7381DC046 for ; Tue, 4 Mar 2025 01:33:44 +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=1741052026; cv=none; b=XlRtJeh9OPLhLJQInVROMxJe/jrGhzpe7Gp8PA59LoIlKIpPbMrgmqzQseW+UVl1MoOu+SUjBiqoCwTN7v4pEfBJk7zI5RQaW7IZp3SOm+R+CfSl25D+tWuG23gUtfhOTVpxSditT/6kfqIDjYLA6tfrJzapm1HJc52VAQZV2To= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741052026; c=relaxed/simple; bh=3bM8LluU4Z3E3P/OumEsl2hMh8xJPdKkueEVx9wyqcI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZFCsWh1MfUaX3WbAqCezOI631OoZ1xiX15YKnFOhhgZz7h7oARdM03RWmBcM2DZSzNisHdNYw2qVxD/BEOv38ZCzzKS2XW76dLapf9ev2l2WRcmasOP8vH4toHS7j+UWFxxkjpMdTPjoI6dqMGG7ynmLhG61LSGv4Bfg3iW1Vjg= 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=lMQ36Qrt; 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="lMQ36Qrt" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2feb8d29740so7693421a91.1 for ; Mon, 03 Mar 2025 17:33:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741052024; x=1741656824; 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=pVWF/+OYw6uzU0HOKtc8UI2eMuXCIdB02LubQ/Jgc4A=; b=lMQ36QrtN4pKrlUn10OuZQj7JChGZKd9gh+VFl4B/f4eTO+t2CnuLoHn5xiA3mhUBG 63gSQrzHdzD+whGPP1Ln+0DqGYmR+PVKbLCXo+fO/aICdWsY3itULKm6q7EXNauoD2fS aSKPLqtqvKCyOngYtpvVQ6QeUxWd0mrh1DT46Q0xVnAnQeLIf3Y+auv59b9MIQCSc+KP AyLiA9zMRKXpyqpHdaTM18QsP7shDchu41OdHSMApGUbE+TPZ4WHGuy0g/8xS8j0GPUo 9IGrKTQ597gvf+huO5YyrHEJ9FHZfzqx8T38+HVHPpFFMt7LylVUrwPoZjQWZQXx40Zz w0KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741052024; x=1741656824; 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=pVWF/+OYw6uzU0HOKtc8UI2eMuXCIdB02LubQ/Jgc4A=; b=GCmZn+uQK0j1IeQ2esbYPUvNRTFyA2usgDIRAwBojDs+/h7wSXPuRJqXQaSLWWNrID prNyZoAb0yg7/4tuN0+CDDF6yxGtm91iPHOrrU+8zTjpySRAdGxBksqGbp9sQ+wOvt8p j+itcHeVEb+CtQObbTjqWMk5aIiLyg+KWeOhOyOc8izohz74sVgut7UC1CgugVA/MoMs qX42c1ew5miQEONB9LsWe3ocaNvsfR9JHlZm51T/B6JNCh7XUfn8Ko547KoeB8EovyyT aILg92tHOeXwZOqsbn2qDo0QMFHWOJxWnzICNamglT25b/9+58S5CvLPTEKkE54yPfLb hCyg== X-Forwarded-Encrypted: i=1; AJvYcCXfVnNj6SR476sLOXaPBmOudUAiPQCjTTR4InjOm7tPn7D09FAHEcx6sHciZXuT7h36KinWid+LdfcfA9Q=@vger.kernel.org X-Gm-Message-State: AOJu0Ywb0Szy3KKjNjTYzwetqKAgAPWZ7FYX2kJRRM9YkBI4QFHEqoC0 D8QPmVfWBltJeOK7A05h49J2F4FMA648H3xrTWl79b1qwwy3N8HbRsdbVqOItt/DIKW5LVtRRva REw== X-Google-Smtp-Source: AGHT+IEiqQkt5+UNuOvzeaEsfiMYJNiN4m8YxDgw4H7w/e5EeOuHeJe3r5VOcS0UXwoNZ+TKzKVyw7qbpOc= X-Received: from pgbeq11.prod.google.com ([2002:a05:6a02:268b:b0:ad5:512a:7755]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:2d12:b0:1e1:a75a:c452 with SMTP id adf61e73a8af0-1f2f4d1ffc9mr23538694637.19.1741052023760; Mon, 03 Mar 2025 17:33:43 -0800 (PST) Reply-To: Sean Christopherson Date: Mon, 3 Mar 2025 17:33:35 -0800 In-Reply-To: <20250304013335.4155703-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: <20250304013335.4155703-1-seanjc@google.com> X-Mailer: git-send-email 2.48.1.711.g2feabab25a-goog Message-ID: <20250304013335.4155703-4-seanjc@google.com> Subject: [PATCH v5 3/3] KVM: x86: Rescan I/O APIC routes after EOI interception for old routing From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kai Huang , xuyun , weizijie Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: weizijie Rescan I/O APIC routes for a vCPU after handling an intercepted I/O APIC EOI for an IRQ that is not targeting said vCPU, i.e. after handling what's effectively a stale EOI VM-Exit. If a level-triggered IRQ is in-flight when IRQ routing changes, e.g. because the guest change routing from its IRQ handler, then KVM intercepts EOIs on both the new and old target vCPUs, so that the in-flight IRQ can be de-asserted when it's EOI'd. However, only the EOI for the in-flight IRQ needs to intercepted, as IRQs on the same vector with the new routing are coincidental, i.e. occur only if the guest is reusing the vector for multiple interrupt sources. If the I/O APIC routes aren't rescanned, KVM will unnecessarily intercept EOIs for the vector and negative impact the vCPU's interrupt performance. Note, both commit db2bdcbbbd32 ("KVM: x86: fix edge EOI and IOAPIC reconfig race") and commit 0fc5a36dd6b3 ("KVM: x86: ioapic: Fix level-triggered EOI and IOAPIC reconfigure race") mentioned this issue, but it was considered a "rare" occurrence thus was not addressed. However in real environments, this issue can happen even in a well-behaved guest. Cc: Kai Huang Co-developed-by: xuyun Signed-off-by: xuyun Signed-off-by: weizijie [sean: massage changelog and comments, use int/-1, reset at scan] Signed-off-by: Sean Christopherson Reviewed-by: Kai Huang --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/irq_comm.c | 16 ++++++++++++++-- arch/x86/kvm/lapic.c | 8 ++++++++ arch/x86/kvm/x86.c | 1 + 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 44007a351e88..b378414c3104 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1025,6 +1025,7 @@ struct kvm_vcpu_arch { =20 int pending_ioapic_eoi; int pending_external_vector; + int highest_stale_pending_ioapic_eoi; =20 /* be preempted when it's in kernel-mode(cpl=3D0) */ bool preempted_in_kernel; diff --git a/arch/x86/kvm/irq_comm.c b/arch/x86/kvm/irq_comm.c index 14590d9c4a37..d6d792b5d1bd 100644 --- a/arch/x86/kvm/irq_comm.c +++ b/arch/x86/kvm/irq_comm.c @@ -412,9 +412,21 @@ void kvm_scan_ioapic_irq(struct kvm_vcpu *vcpu, u32 de= st_id, u16 dest_mode, * 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= ) || - kvm_apic_pending_eoi(vcpu, vector)) + 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; + } } =20 void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu, diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 9dbc0f5d9865..6af84a0f84f3 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1459,6 +1459,14 @@ static void kvm_ioapic_send_eoi(struct kvm_lapic *ap= ic, int vector) if (!kvm_ioapic_handles_vector(apic, vector)) return; =20 + /* + * If the intercepted EOI is for an IRQ that was pending from previous + * routing, then re-scan the I/O APIC routes as EOIs for the IRQ likely + * no longer need to be intercepted. + */ + if (apic->vcpu->arch.highest_stale_pending_ioapic_eoi =3D=3D vector) + kvm_make_request(KVM_REQ_SCAN_IOAPIC, apic->vcpu); + /* Request a KVM exit to inform the userspace IOAPIC. */ if (irqchip_split(apic->vcpu->kvm)) { apic->vcpu->arch.pending_ioapic_eoi =3D vector; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 7d4b9e2f1a38..a40b09dfb36a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10650,6 +10650,7 @@ static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu) return; =20 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); + vcpu->arch.highest_stale_pending_ioapic_eoi =3D -1; =20 kvm_x86_call(sync_pir_to_irr)(vcpu); =20 --=20 2.48.1.711.g2feabab25a-goog