From: Robin Murphy <robin.murphy@arm.com>
To: Niklas Schnelle <schnelle@linux.ibm.com>,
Alexander Gordeev <agordeev@linux.ibm.com>,
Matthew Rosato <mjrosato@linux.ibm.com>
Cc: Pierre Morel <pmorel@linux.ibm.com>,
iommu@lists.linux.dev, linux-s390@vger.kernel.org,
borntraeger@linux.ibm.com, hca@linux.ibm.com, gor@linux.ibm.com,
gerald.schaefer@linux.ibm.com, svens@linux.ibm.com,
joro@8bytes.org, will@kernel.org, jgg@nvidia.com,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] iommu/s390: Fix race with release_device ops
Date: Thu, 25 Aug 2022 12:26:33 +0100 [thread overview]
Message-ID: <04bf5f9a-a170-55bd-10f0-fa3695b85347@arm.com> (raw)
In-Reply-To: <69b7b496c3658b385f2404d6e3209970b3677c08.camel@linux.ibm.com>
On 2022-08-25 12:11, Niklas Schnelle wrote:
> On Thu, 2022-08-25 at 09:22 +0200, Alexander Gordeev wrote:
>> On Wed, Aug 24, 2022 at 04:25:19PM -0400, Matthew Rosato wrote:
>>>>> @@ -90,15 +90,39 @@ static int s390_iommu_attach_device(struct iommu_domain *domain,
>>>>> struct zpci_dev *zdev = to_zpci_dev(dev);
>>>>> struct s390_domain_device *domain_device;
>>>>> unsigned long flags;
>>>>> - int cc, rc;
>>>>> + int cc, rc = 0;
>>>>> if (!zdev)
>>>>> return -ENODEV;
>>>>> + /* First check compatibility */
>>>>> + spin_lock_irqsave(&s390_domain->list_lock, flags);
>>>>> + /* First device defines the DMA range limits */
>>>>> + if (list_empty(&s390_domain->devices)) {
>>>>> + domain->geometry.aperture_start = zdev->start_dma;
>>>>> + domain->geometry.aperture_end = zdev->end_dma;
>>>>> + domain->geometry.force_aperture = true;
>>>>> + /* Allow only devices with identical DMA range limits */
>>>>> + } else if (domain->geometry.aperture_start != zdev->start_dma ||
>>>>> + domain->geometry.aperture_end != zdev->end_dma) {
>>>>> + rc = -EINVAL;
>>>>> + }
>>>>> + spin_unlock_irqrestore(&s390_domain->list_lock, flags);
>>>>> + if (rc)
>>>>> + return rc;
>>>>> +
>>>>> domain_device = kzalloc(sizeof(*domain_device), GFP_KERNEL);
>>>>> if (!domain_device)
>>>>> return -ENOMEM;
>>>>> + /* Leave now if the device has already been released */
>>>>> + spin_lock_irqsave(&zdev->dma_domain_lock, flags);
>>>>> + if (!dev_iommu_priv_get(dev)) {
>>>>> + spin_unlock_irqrestore(&zdev->dma_domain_lock, flags);
>>>>> + kfree(domain_device);
>>>>> + return 0;
>>>>> + }
>>>>> +
>>>>> if (zdev->dma_table && !zdev->s390_domain) {
>>>>> cc = zpci_dma_exit_device(zdev);
>>>>> if (cc) {
>>>>
>>>> Am I wrong? It seems to me that zpci_dma_exit_device here is called with the spin_lock locked but this function zpci_dma_exit_device calls vfree which may sleep.
>>>>
>>>
>>> Oh, good point, I just enabled lockdep to verify that.
>>>
>>> I think we could just replace this with a mutex instead, it's not a performance path. I've been running tests successfully today with this patch modified to instead use a mutex for dma_domain_lock.
>>
>> But your original version uses irq-savvy spinlocks.
>> Are there data that need to be protected against interrupts?
>>
>> Thanks!
>
> I think that was a carry over from my original attempt that used the
> zdev->dma_domain_lock in some more places including in interrupt
> context. I think these are gone now so I think Matt is right in his
> version this can be a mutex.
Yes, probe/release/attach/detach should absolutely not be happening from
atomic/IRQ context. At the very least, the IOMMU core itself needs to
take the group mutex in those paths.
Cheers,
Robin.
prev parent reply other threads:[~2022-08-25 11:27 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-08-23 20:30 [PATCH] iommu/s390: Fix race with release_device ops Matthew Rosato
2022-08-24 8:37 ` Pierre Morel
2022-08-24 20:25 ` Matthew Rosato
2022-08-25 7:22 ` Alexander Gordeev
2022-08-25 11:11 ` Niklas Schnelle
2022-08-25 11:26 ` Robin Murphy [this message]
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=04bf5f9a-a170-55bd-10f0-fa3695b85347@arm.com \
--to=robin.murphy@arm.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=pmorel@linux.ibm.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