From: Leo Yan <leo.yan@linaro.org>
To: Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
Cc: "kvm@vger.kernel.org" <kvm@vger.kernel.org>,
Marc Zyngier <Marc.Zyngier@arm.com>,
Will Deacon <Will.Deacon@arm.com>,
Robin Murphy <Robin.Murphy@arm.com>,
"kvmarm@lists.cs.columbia.edu" <kvmarm@lists.cs.columbia.edu>
Subject: Re: [PATCH kvmtool v3 3/3] vfio-pci: Re-enable INTx mode when disable MSI/MSIX
Date: Fri, 5 Apr 2019 11:46:09 +0800 [thread overview]
Message-ID: <20190405034609.GA6963@leoy-ThinkPad-X240s> (raw)
In-Reply-To: <fea39efb-1bc1-deff-e1a0-f695db429abb@arm.com>
Hi Jean-Philippe,
On Thu, Apr 04, 2019 at 12:06:51PM +0100, Jean-Philippe Brucker wrote:
> On 26/03/2019 07:41, Leo Yan wrote:
> > 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. In this case, the INTx
> > mode has been disabled and has no chance to re-enable it, thus both INTx
> > mode and MSI/MSIX mode cannot work in vfio.
> >
> > 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
> > re-enable it so that the device can fallback to use it.
> >
> > Since vfio_pci_disable_intx() / vfio_pci_enable_intx() pair functions
> > may be called for multiple times, this patch uses 'intx_fd == -1' to
> > denote the INTx is disabled, the pair functions can directly bail out
> > when detect INTx has been disabled and enabled respectively.
> >
> > Suggested-by: Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
> > Signed-off-by: Leo Yan <leo.yan@linaro.org>
> > ---
> > vfio/pci.c | 41 ++++++++++++++++++++++++++++++-----------
> > 1 file changed, 30 insertions(+), 11 deletions(-)
> >
> > diff --git a/vfio/pci.c b/vfio/pci.c
> > index 3c39844..3b2b1e7 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.
> > + */
>
> I don't think the comment change is useful, we don't need that much
> detail. The text that you replaced was trying to explain why we enable
> INTx from the start, and would still apply (although it should have been
> s/user/guest/)
>
> > + 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,16 @@ 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)
> > + 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 +1009,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 */
>
> Here as well, the comments on intx_fd seem unnecessary. But these are
> only nits, the code is fine and I tested for regressions on my hardware, so:
>
> Reviewed-by: Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
Thanks for reviewing. I will spin a new patch set to drop unnecessary
comment to let changes as simple as possible.
Thanks,
Leo Yan
WARNING: multiple messages have this Message-ID (diff)
From: Leo Yan <leo.yan@linaro.org>
To: Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
Cc: "kvm@vger.kernel.org" <kvm@vger.kernel.org>,
"kvmarm@lists.cs.columbia.edu" <kvmarm@lists.cs.columbia.edu>,
Will Deacon <Will.Deacon@arm.com>,
Marc Zyngier <Marc.Zyngier@arm.com>,
Eric Auger <eric.auger@redhat.com>,
Robin Murphy <Robin.Murphy@arm.com>
Subject: Re: [PATCH kvmtool v3 3/3] vfio-pci: Re-enable INTx mode when disable MSI/MSIX
Date: Fri, 5 Apr 2019 11:46:09 +0800 [thread overview]
Message-ID: <20190405034609.GA6963@leoy-ThinkPad-X240s> (raw)
Message-ID: <20190405034609.8K2CAfNY3mzVqDx0xfm7XH85t1QCGPeYcYcOqEKf4Rg@z> (raw)
In-Reply-To: <fea39efb-1bc1-deff-e1a0-f695db429abb@arm.com>
Hi Jean-Philippe,
On Thu, Apr 04, 2019 at 12:06:51PM +0100, Jean-Philippe Brucker wrote:
> On 26/03/2019 07:41, Leo Yan wrote:
> > 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. In this case, the INTx
> > mode has been disabled and has no chance to re-enable it, thus both INTx
> > mode and MSI/MSIX mode cannot work in vfio.
> >
> > 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
> > re-enable it so that the device can fallback to use it.
> >
> > Since vfio_pci_disable_intx() / vfio_pci_enable_intx() pair functions
> > may be called for multiple times, this patch uses 'intx_fd == -1' to
> > denote the INTx is disabled, the pair functions can directly bail out
> > when detect INTx has been disabled and enabled respectively.
> >
> > Suggested-by: Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
> > Signed-off-by: Leo Yan <leo.yan@linaro.org>
> > ---
> > vfio/pci.c | 41 ++++++++++++++++++++++++++++++-----------
> > 1 file changed, 30 insertions(+), 11 deletions(-)
> >
> > diff --git a/vfio/pci.c b/vfio/pci.c
> > index 3c39844..3b2b1e7 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.
> > + */
>
> I don't think the comment change is useful, we don't need that much
> detail. The text that you replaced was trying to explain why we enable
> INTx from the start, and would still apply (although it should have been
> s/user/guest/)
>
> > + 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,16 @@ 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)
> > + 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 +1009,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 */
>
> Here as well, the comments on intx_fd seem unnecessary. But these are
> only nits, the code is fine and I tested for regressions on my hardware, so:
>
> Reviewed-by: Jean-Philippe Brucker <jean-philippe.brucker@arm.com>
Thanks for reviewing. I will spin a new patch set to drop unnecessary
comment to let changes as simple as possible.
Thanks,
Leo Yan
next prev parent reply other threads:[~2019-04-05 3:46 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-03-26 7:41 [PATCH kvmtool v3 0/3] vfio-pci: Support INTx mode re-enabling Leo Yan
2019-03-26 7:41 ` [PATCH kvmtool v3 1/3] vfio-pci: Release INTx's unmask eventfd properly Leo Yan
2019-03-26 7:41 ` [PATCH kvmtool v3 2/3] vfio-pci: Add new function for INTx one-time initialisation Leo Yan
2019-04-04 11:06 ` Jean-Philippe Brucker
2019-03-26 7:41 ` [PATCH kvmtool v3 3/3] vfio-pci: Re-enable INTx mode when disable MSI/MSIX Leo Yan
2019-04-04 11:06 ` Jean-Philippe Brucker
2019-04-05 3:46 ` Leo Yan [this message]
2019-04-05 3:46 ` Leo Yan
2019-04-02 16:50 ` [PATCH kvmtool v3 0/3] vfio-pci: Support INTx mode re-enabling Will Deacon
2019-04-03 3:58 ` Leo Yan
2019-04-03 12:43 ` Will Deacon
2019-04-04 2:57 ` Leo Yan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190405034609.GA6963@leoy-ThinkPad-X240s \
--to=leo.yan@linaro.org \
--cc=Marc.Zyngier@arm.com \
--cc=Robin.Murphy@arm.com \
--cc=Will.Deacon@arm.com \
--cc=jean-philippe.brucker@arm.com \
--cc=kvm@vger.kernel.org \
--cc=kvmarm@lists.cs.columbia.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox