From: Pierre Morel <pmorel@linux.ibm.com>
To: Matthew Rosato <mjrosato@linux.ibm.com>, iommu@lists.linux.dev
Cc: linux-s390@vger.kernel.org, schnelle@linux.ibm.com,
borntraeger@linux.ibm.com, hca@linux.ibm.com, gor@linux.ibm.com,
gerald.schaefer@linux.ibm.com, agordeev@linux.ibm.com,
svens@linux.ibm.com, joro@8bytes.org, will@kernel.org,
robin.murphy@arm.com, jgg@nvidia.com,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4 1/2] iommu/s390: Fix race with release_device ops
Date: Thu, 1 Sep 2022 09:56:09 +0200 [thread overview]
Message-ID: <9887e2f4-3f3d-137d-dad7-59dab5f98aab@linux.ibm.com> (raw)
In-Reply-To: <20220831201236.77595-2-mjrosato@linux.ibm.com>
On 8/31/22 22:12, Matthew Rosato wrote:
> With commit fa7e9ecc5e1c ("iommu/s390: Tolerate repeat attach_dev
> calls") s390-iommu is supposed to handle dynamic switching between IOMMU
> domains and the DMA API handling. However, this commit does not
> sufficiently handle the case where the device is released via a call
> to the release_device op as it may occur at the same time as an opposing
> attach_dev or detach_dev since the group mutex is not held over
> release_device. This was observed if the device is deconfigured during a
> small window during vfio-pci initialization and can result in WARNs and
> potential kernel panics.
>
> Handle this by tracking when the device is probed/released via
> dev_iommu_priv_set/get(). Ensure that once the device is released only
> release_device handles the re-init of the device DMA.
>
> Fixes: fa7e9ecc5e1c ("iommu/s390: Tolerate repeat attach_dev calls")
> Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
> ---
> arch/s390/include/asm/pci.h | 1 +
> arch/s390/pci/pci.c | 1 +
> drivers/iommu/s390-iommu.c | 39 ++++++++++++++++++++++++++++++++++---
> 3 files changed, 38 insertions(+), 3 deletions(-)
>
> diff --git a/arch/s390/include/asm/pci.h b/arch/s390/include/asm/pci.h
> index 7b4cdadbc023..080251e7b275 100644
> --- a/arch/s390/include/asm/pci.h
> +++ b/arch/s390/include/asm/pci.h
> @@ -157,6 +157,7 @@ struct zpci_dev {
> /* DMA stuff */
> unsigned long *dma_table;
> spinlock_t dma_table_lock;
> + struct mutex dma_domain_lock; /* protects s390_domain value */
> int tlb_refresh;
>
> spinlock_t iommu_bitmap_lock;
> diff --git a/arch/s390/pci/pci.c b/arch/s390/pci/pci.c
> index 73cdc5539384..973edd32ecc9 100644
> --- a/arch/s390/pci/pci.c
> +++ b/arch/s390/pci/pci.c
> @@ -832,6 +832,7 @@ struct zpci_dev *zpci_create_device(u32 fid, u32 fh, enum zpci_state state)
> kref_init(&zdev->kref);
> mutex_init(&zdev->lock);
> mutex_init(&zdev->kzdev_lock);
> + mutex_init(&zdev->dma_domain_lock);
>
> rc = zpci_init_iommu(zdev);
> if (rc)
> diff --git a/drivers/iommu/s390-iommu.c b/drivers/iommu/s390-iommu.c
> index c898bcbbce11..1137d669e849 100644
> --- a/drivers/iommu/s390-iommu.c
> +++ b/drivers/iommu/s390-iommu.c
> @@ -99,6 +99,14 @@ static int s390_iommu_attach_device(struct iommu_domain *domain,
> if (!domain_device)
> return -ENOMEM;
>
> + /* Leave now if the device has already been released */
> + mutex_lock(&zdev->dma_domain_lock);
> + if (!dev_iommu_priv_get(dev)) {
> + mutex_unlock(&zdev->dma_domain_lock);
> + kfree(domain_device);
> + return 0;
> + }
> +
> if (zdev->dma_table && !zdev->s390_domain) {
> cc = zpci_dma_exit_device(zdev);
> if (cc) {
> @@ -132,9 +140,10 @@ static int s390_iommu_attach_device(struct iommu_domain *domain,
> goto out_restore;
> }
> domain_device->zdev = zdev;
> - zdev->s390_domain = s390_domain;
> list_add(&domain_device->list, &s390_domain->devices);
> spin_unlock_irqrestore(&s390_domain->list_lock, flags);
> + zdev->s390_domain = s390_domain;
> + mutex_unlock(&zdev->dma_domain_lock);
>
> return 0;
>
> @@ -147,6 +156,7 @@ static int s390_iommu_attach_device(struct iommu_domain *domain,
> virt_to_phys(zdev->dma_table));
> }
> out_free:
> + mutex_unlock(&zdev->dma_domain_lock);
> kfree(domain_device);
>
> return rc;
> @@ -176,17 +186,22 @@ static void s390_iommu_detach_device(struct iommu_domain *domain,
> }
> spin_unlock_irqrestore(&s390_domain->list_lock, flags);
>
> - if (found && (zdev->s390_domain == s390_domain)) {
> + mutex_lock(&zdev->dma_domain_lock);
> + if (found && (zdev->s390_domain == s390_domain) &&
> + dev_iommu_priv_get(dev)) {
> zdev->s390_domain = NULL;
> zpci_unregister_ioat(zdev, 0);
> zpci_dma_init_device(zdev);
> }
> + mutex_unlock(&zdev->dma_domain_lock);
> }
>
> static struct iommu_device *s390_iommu_probe_device(struct device *dev)
> {
> struct zpci_dev *zdev = to_zpci_dev(dev);
>
> + dev_iommu_priv_set(dev, zdev);
> +
> return &zdev->iommu_dev;
> }
>
> @@ -206,10 +221,28 @@ static void s390_iommu_release_device(struct device *dev)
> *
> * So let's call detach_dev from here if it hasn't been called before.
> */
> - if (zdev && zdev->s390_domain) {
> + if (zdev) {
> + /*
> + * Clear priv to block further attaches for this device,
> + * ensure detaches don't init DMA. Hold the domain lock
> + * to ensure that attach/detach get a consistent view of
> + * whether or not the device is released.
> + */
> + mutex_lock(&zdev->dma_domain_lock);
> + dev_iommu_priv_set(dev, NULL);
> + mutex_unlock(&zdev->dma_domain_lock);
We release the lock here to later call s390_iommu_detach_device safely
right?
Couldn't we keep the lock and put the common code from
s390_iommu_release_device and s390_iommu_detach_device inside a common
function?
> + /* Make sure this device is removed from the domain list */
> domain = iommu_get_domain_for_dev(dev);
> if (domain)
> s390_iommu_detach_device(domain, dev);
> + /* Now ensure DMA is initialized from here */
> + mutex_lock(&zdev->dma_domain_lock);
> + if (zdev->s390_domain) {
> + zdev->s390_domain = NULL;
> + zpci_unregister_ioat(zdev, 0);
> + zpci_dma_init_device(zdev);
Sorry if it is a stupid question, but two things looks strange to me:
- having DMA initialized just after having unregistered the IOAT
Is that really all we need to unregister before calling dma_init_device?
- having DMA initialized inside the release_device callback:
Why isn't it done in the device_probe ?
> + }
> + mutex_unlock(&zdev->dma_domain_lock);
> }
> }
>
>
--
Pierre Morel
IBM Lab Boeblingen
next prev parent reply other threads:[~2022-09-01 7:56 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-31 20:12 [PATCH v4 0/2] iommu/s390: fixes related to repeat attach_dev calls Matthew Rosato
2022-08-31 20:12 ` [PATCH v4 1/2] iommu/s390: Fix race with release_device ops Matthew Rosato
2022-09-01 7:56 ` Pierre Morel [this message]
2022-09-01 9:37 ` Niklas Schnelle
2022-09-01 11:01 ` Robin Murphy
2022-09-01 13:42 ` Niklas Schnelle
2022-09-01 14:17 ` Niklas Schnelle
2022-09-01 14:29 ` Robin Murphy
2022-09-01 14:34 ` Jason Gunthorpe
2022-09-01 15:03 ` Robin Murphy
2022-09-01 15:49 ` Jason Gunthorpe
2022-09-01 17:00 ` Robin Murphy
2022-09-01 20:28 ` Matthew Rosato
2022-09-02 7:49 ` Niklas Schnelle
2022-09-01 10:25 ` Robin Murphy
2022-09-01 16:14 ` Matthew Rosato
2022-09-01 20:37 ` Jason Gunthorpe
2022-09-02 17:11 ` Matthew Rosato
2022-09-02 17:21 ` Jason Gunthorpe
2022-09-02 18:20 ` Matthew Rosato
2022-09-05 9:46 ` Robin Murphy
2022-09-06 13:36 ` Jason Gunthorpe
2022-09-02 10:48 ` Robin Murphy
2022-08-31 20:12 ` [PATCH v4 2/2] iommu/s390: fix leak of s390_domain_device Matthew Rosato
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=9887e2f4-3f3d-137d-dad7-59dab5f98aab@linux.ibm.com \
--to=pmorel@linux.ibm.com \
--cc=agordeev@linux.ibm.com \
--cc=borntraeger@linux.ibm.com \
--cc=gerald.schaefer@linux.ibm.com \
--cc=gor@linux.ibm.com \
--cc=hca@linux.ibm.com \
--cc=iommu@lists.linux.dev \
--cc=jgg@nvidia.com \
--cc=joro@8bytes.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=mjrosato@linux.ibm.com \
--cc=robin.murphy@arm.com \
--cc=schnelle@linux.ibm.com \
--cc=svens@linux.ibm.com \
--cc=will@kernel.org \
/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;
as well as URLs for NNTP newsgroup(s).