From patchwork Tue Aug 8 11:37:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Longpeng(Mike)" X-Patchwork-Id: 9887641 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C7DCF60384 for ; Tue, 8 Aug 2017 11:40:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BA9A728401 for ; Tue, 8 Aug 2017 11:40:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AF96A287DA; Tue, 8 Aug 2017 11:40:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 371DE28401 for ; Tue, 8 Aug 2017 11:40:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752068AbdHHLhh (ORCPT ); Tue, 8 Aug 2017 07:37:37 -0400 Received: from szxga05-in.huawei.com ([45.249.212.191]:2587 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751925AbdHHLhf (ORCPT ); Tue, 8 Aug 2017 07:37:35 -0400 Received: from 172.30.72.59 (EHLO DGGEMS411-HUB.china.huawei.com) ([172.30.72.59]) by dggrg05-dlp.huawei.com (MOS 4.4.6-GA FastPath queued) with ESMTP id DEU75780; Tue, 08 Aug 2017 19:37:32 +0800 (CST) Received: from localhost (10.177.246.209) by DGGEMS411-HUB.china.huawei.com (10.3.19.211) with Microsoft SMTP Server id 14.3.301.0; Tue, 8 Aug 2017 19:37:23 +0800 From: "Longpeng(Mike)" To: , CC: , , , , , , "Longpeng(Mike)" Subject: [PATCH] KVM: X86: expand ->arch.apic_arb_prio to u64 Date: Tue, 8 Aug 2017 19:37:14 +0800 Message-ID: <1502192234-14068-1-git-send-email-longpeng2@huawei.com> X-Mailer: git-send-email 1.8.4.msysgit.0 MIME-Version: 1.0 X-Originating-IP: [10.177.246.209] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020205.5989A27D.00E2, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 71831013cfdaf30468cbbd7628dcd694 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently 'apic_arb_prio' is int32_t, it's too short for long time running. In our environment, it overflowed and then the UBSAN was angry: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int' CPU: 22 PID: 31237 Comm: qemu-kvm Tainted: ... ... Call Trace: [] dump_stack+0x1e/0x20 [] ubsan_epilogue+0x12/0x55 [] handle_overflow+0x1ba/0x215 [] __ubsan_handle_add_overflow+0x2a/0x31 [] __apic_accept_irq+0x57a/0x5d0 [kvm] [] kvm_apic_set_irq+0x9f/0xf0 [kvm] [] kvm_irq_delivery_to_apic_fast+0x450/0x910 [kvm] [] kvm_irq_delivery_to_apic+0xfa/0x7a0 [kvm] [] kvm_set_msi+0xa9/0x100 [kvm] [] kvm_send_userspace_msi+0x14d/0x1f0 [kvm] [] kvm_vm_ioctl+0x4ee/0xdd0 [kvm] ... We expand it to u64, this is large enough. Suppose the vcpu receives 1000 irqs per second, then it won't overflow in 584942417 years. ( 18446744073709551615/1000/3600/24/365 = 584942417 ) Signed-off-by: Longpeng(Mike) --- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/ioapic.h | 3 ++- arch/x86/kvm/irq_comm.c | 2 +- arch/x86/kvm/lapic.c | 6 +++--- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 87ac4fb..ce9a5f5 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -499,7 +499,7 @@ struct kvm_vcpu_arch { bool apicv_active; DECLARE_BITMAP(ioapic_handled_vectors, 256); unsigned long apic_attention; - int32_t apic_arb_prio; + u64 apic_arb_prio; int mp_state; u64 ia32_misc_enable_msr; u64 smbase; diff --git a/arch/x86/kvm/ioapic.h b/arch/x86/kvm/ioapic.h index 29ce197..a26deed 100644 --- a/arch/x86/kvm/ioapic.h +++ b/arch/x86/kvm/ioapic.h @@ -117,7 +117,8 @@ static inline int ioapic_in_kernel(struct kvm *kvm) void kvm_rtc_eoi_tracking_restore_one(struct kvm_vcpu *vcpu); bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source, int short_hand, unsigned int dest, int dest_mode); -int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2); +/* Return true if vcpu1's priority is lower */ +bool kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2); void kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, int vector, int trigger_mode); int kvm_ioapic_init(struct kvm *kvm); diff --git a/arch/x86/kvm/irq_comm.c b/arch/x86/kvm/irq_comm.c index 3cc3b2d..03b1487 100644 --- a/arch/x86/kvm/irq_comm.c +++ b/arch/x86/kvm/irq_comm.c @@ -90,7 +90,7 @@ int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, if (!kvm_vector_hashing_enabled()) { if (!lowest) lowest = vcpu; - else if (kvm_apic_compare_prio(vcpu, lowest) < 0) + else if (kvm_apic_compare_prio(vcpu, lowest)) lowest = vcpu; } else { __set_bit(i, dest_vcpu_bitmap); diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 589dcc1..1e2b1f2 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -840,7 +840,7 @@ static inline bool kvm_apic_map_get_dest_lapic(struct kvm *kvm, if (lowest < 0) lowest = i; else if (kvm_apic_compare_prio((*dst)[i]->vcpu, - (*dst)[lowest]->vcpu) < 0) + (*dst)[lowest]->vcpu)) lowest = i; } } else { @@ -1048,9 +1048,9 @@ static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, return result; } -int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2) +bool kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2) { - return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio; + return vcpu1->arch.apic_arb_prio < vcpu2->arch.apic_arb_prio; } static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)