From patchwork Wed Mar 20 06:20:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leo Yan X-Patchwork-Id: 10860883 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 72A911575 for ; Wed, 20 Mar 2019 06:21:22 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 57312299D3 for ; Wed, 20 Mar 2019 06:21:22 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4AD0E299D5; Wed, 20 Mar 2019 06:21:22 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham 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 B2790299D3 for ; Wed, 20 Mar 2019 06:21:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727512AbfCTGVU (ORCPT ); Wed, 20 Mar 2019 02:21:20 -0400 Received: from mail-ot1-f67.google.com ([209.85.210.67]:34297 "EHLO mail-ot1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727518AbfCTGVU (ORCPT ); Wed, 20 Mar 2019 02:21:20 -0400 Received: by mail-ot1-f67.google.com with SMTP id r19so1138354otn.1 for ; Tue, 19 Mar 2019 23:21:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=GITb0xWl1Z9utyhYYoG1NuN7yxh2JF4G4hy7ux2h46o=; b=cE5nEg3pKkLqW+4qRtMe6VcxtSMjoNi7R8dHw7mdQKD7goioBqYxUWYlbBMkNrX8Up hS4FEXMt977Twy6hxa7L5qA/fCA50+5YUG8ileVNqaqiCF7ojGYfOFSAsRdyAPo144Ns 537+m6vnkBjK/5XHTsKN3RFvYDUGWiQsJYtb/HT1W3qsrZX7Ems7qUCXxwmS2haN/fCK c6mgh0gLIqos4KsEdcX97roQ0cZjYpW8OGx6Fy/nkbSdfK/C99GPpuK5UMB9ORLElPDf vc1zuQwdOp/oDZCcS/phV20MRZxMOXHf1qt/Sr19krROPt7TKmiOpoiIEkAAnqqH5cMo F0BQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=GITb0xWl1Z9utyhYYoG1NuN7yxh2JF4G4hy7ux2h46o=; b=jcv7Gkixs8SMFPtHVc7BnCjF8ClC5zAd8IpB23pZyWWYFRsujLCmkXafYgPYvf16rl aUbd5LCtDfZOT7hYqhXlkbmoQL/aLzyoRokd0LkcXnJhkMQ9z064n/IOpSnS3wBR2GH0 /uATWKfa/pImxQG/7MKt7L2dk5IH/BoPLKNUO2iuw4eh64YI0jqOgElkQS+skHJjynvX HhZGwBkLcG8VwivqTqtc7TDZaq3pBpGflU+ht4m+nk0lQKAvu7rzl3n4ReQNkafNCU1v blJzVNxK6F6uN5omIkZbTIQZ5cAh+E2m9Ku3huLO3tyN3za/NdoEeNhFIwV98szFINxk dFMw== X-Gm-Message-State: APjAAAXLBEWz+KmFX2gGcesfg05EljxZnHQW6ML0MYc1OdiJ05v28NPO qjmIKyfXHvR/R/IXdSqOKhDc8kna53k= X-Google-Smtp-Source: APXvYqzTlVclefO9sMW0xxsq9h56sXdO2K/uPNA07d7w3VuzYEJ/bAiVBNoBi/EQdNvxZrd5nRDNZw== X-Received: by 2002:a9d:4c9a:: with SMTP id m26mr4313837otf.369.1553062878672; Tue, 19 Mar 2019 23:21:18 -0700 (PDT) Received: from localhost.localdomain (li808-42.members.linode.com. [104.237.132.42]) by smtp.gmail.com with ESMTPSA id e186sm401517oia.44.2019.03.19.23.21.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Mar 2019 23:21:17 -0700 (PDT) From: Leo Yan To: kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, Will Deacon , Marc Zyngier , Jean-Philippe Brucker , Eric Auger , Robin Murphy Cc: Leo Yan Subject: [PATCH kvmtool v2 3/3] vfio-pci: Re-enable INTx mode when disable MSI/MSIX Date: Wed, 20 Mar 2019 14:20:46 +0800 Message-Id: <20190320062046.3895-4-leo.yan@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190320062046.3895-1-leo.yan@linaro.org> References: <20190320062046.3895-1-leo.yan@linaro.org> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Since PCI forbids enabling INTx, MSI or MSIX at the same time, it's by default to disable INTx mode when enable MSI/MSIX mode; but this logic is easily broken if the guest PCI driver detects the MSI/MSIX cannot work as expected and tries to rollback to use INTx mode. The INTx mode has been disabled and it has no chance to be enabled again, thus both INTx mode and MSI/MSIX mode will not be enabled in vfio for this case. Below shows the detailed flow for introducing this issue: vfio_pci_configure_dev_irqs() `-> vfio_pci_enable_intx() vfio_pci_enable_msis() `-> vfio_pci_disable_intx() vfio_pci_disable_msis() => Guest PCI driver disables MSI To fix this issue, when disable MSI/MSIX we need to check if INTx mode is available for this device or not; if the device can support INTx then we need to re-enable it so the device can fallback to use it. In this patch, should note two minor changes: - vfio_pci_disable_intx() may be called multiple times (each time the guest enables one MSI vector). This patch changes to use 'intx_fd == -1' to denote the INTx disabled, vfio_pci_disable_intx() and vfio_pci_enable_intx will directly bail out when detect INTx has been disabled and enabled respectively. - Since pci_device_header will be corrupted after PCI configuration and all irq related info will be lost. Before re-enabling INTx mode, this patch restores 'irq_pin' and 'irq_line' fields in struct pci_device_header. Signed-off-by: Leo Yan --- vfio/pci.c | 59 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/vfio/pci.c b/vfio/pci.c index d025581..ba971eb 100644 --- a/vfio/pci.c +++ b/vfio/pci.c @@ -28,6 +28,7 @@ struct vfio_irq_eventfd { msi_update_state(state, val, VFIO_PCI_MSI_STATE_EMPTY) static void vfio_pci_disable_intx(struct kvm *kvm, struct vfio_device *vdev); +static int vfio_pci_enable_intx(struct kvm *kvm, struct vfio_device *vdev); static int vfio_pci_enable_msis(struct kvm *kvm, struct vfio_device *vdev, bool msix) @@ -50,17 +51,14 @@ static int vfio_pci_enable_msis(struct kvm *kvm, struct vfio_device *vdev, if (!msi_is_enabled(msis->virt_state)) return 0; - if (pdev->irq_modes & VFIO_PCI_IRQ_MODE_INTX) { - /* - * PCI (and VFIO) forbids enabling INTx, MSI or MSIX at the same - * time. Since INTx has to be enabled from the start (we don't - * have a reliable way to know when the user starts using it), - * disable it now. - */ + /* + * PCI (and VFIO) forbids enabling INTx, MSI or MSIX at the same + * time. Since INTx has to be enabled from the start (after enabling + * 'pdev->intx_fd' will be assigned to an eventfd and doesn't equal + * to the init value -1), disable it now. + */ + if (pdev->irq_modes & VFIO_PCI_IRQ_MODE_INTX) vfio_pci_disable_intx(kvm, vdev); - /* Permanently disable INTx */ - pdev->irq_modes &= ~VFIO_PCI_IRQ_MODE_INTX; - } eventfds = (void *)msis->irq_set + sizeof(struct vfio_irq_set); @@ -162,7 +160,34 @@ static int vfio_pci_disable_msis(struct kvm *kvm, struct vfio_device *vdev, msi_set_enabled(msis->phys_state, false); msi_set_empty(msis->phys_state, true); - return 0; + /* + * When MSI or MSIX is disabled, this might be called when + * PCI driver detects the MSI interrupt failure and wants to + * rollback to INTx mode. Thus enable INTx if the device + * supports INTx mode in this case. + */ + if (pdev->irq_modes & VFIO_PCI_IRQ_MODE_INTX) { + /* + * Struct pci_device_header is not only used for header, + * it also is used for PCI configuration; and in the function + * vfio_pci_cfg_write() it firstly writes configuration space + * and then read back the configuration space data into the + * header structure; thus 'irq_pin' and 'irq_line' in the + * header will be overwritten. + * + * If want to enable INTx mode properly, firstly needs to + * restore 'irq_pin' and 'irq_line' values; we can simply set 1 + * to 'irq_pin', and 'pdev->intx_gsi' keeps gsi value when + * enable INTx mode previously so we can simply use it to + * recover irq line number by adding offset KVM_IRQ_OFFSET. + */ + pdev->hdr.irq_pin = 1; + pdev->hdr.irq_line = pdev->intx_gsi + KVM_IRQ_OFFSET; + + ret = vfio_pci_enable_intx(kvm, vdev); + } + + return ret >= 0 ? 0 : ret; } static int vfio_pci_update_msi_entry(struct kvm *kvm, struct vfio_device *vdev, @@ -1002,6 +1027,10 @@ static void vfio_pci_disable_intx(struct kvm *kvm, struct vfio_device *vdev) .index = VFIO_PCI_INTX_IRQ_INDEX, }; + /* INTx mode has been disabled */ + if (pdev->intx_fd == -1) + return; + pr_debug("user requested MSI, disabling INTx %d", gsi); ioctl(vdev->fd, VFIO_DEVICE_SET_IRQS, &irq_set); @@ -1009,6 +1038,7 @@ static void vfio_pci_disable_intx(struct kvm *kvm, struct vfio_device *vdev) close(pdev->intx_fd); close(pdev->unmask_fd); + pdev->intx_fd = -1; } static int vfio_pci_enable_intx(struct kvm *kvm, struct vfio_device *vdev) @@ -1025,6 +1055,10 @@ static int vfio_pci_enable_intx(struct kvm *kvm, struct vfio_device *vdev) .index = VFIO_PCI_INTX_IRQ_INDEX, }; + /* INTx mode has been enabled */ + if (pdev->intx_fd != -1) + return 0; + ret = ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info); if (ret || irq_info.count == 0) { vfio_dev_err(vdev, "no INTx reported by VFIO"); @@ -1140,6 +1174,9 @@ static int vfio_pci_configure_dev_irqs(struct kvm *kvm, struct vfio_device *vdev return ret; } + /* Use intx_fd=-1 to denote INTx is disabled */ + pdev->intx_fd = -1; + if (pdev->irq_modes & VFIO_PCI_IRQ_MODE_INTX) ret = vfio_pci_enable_intx(kvm, vdev);