Linux IOMMU Development
 help / color / mirror / Atom feed
From: Vasant Hegde <vasant.hegde@amd.com>
To: Robin Murphy <robin.murphy@arm.com>,
	iommu@lists.linux.dev, joro@8bytes.org
Cc: suravee.suthikulpanit@amd.com, Lianbo Jiang <lijiang@redhat.com>
Subject: Re: [PATCH 1/2] iommu: Take group lock before attaching device in iommu_deferred_attach()
Date: Wed, 29 May 2024 12:54:14 +0530	[thread overview]
Message-ID: <029a1733-4e7e-4deb-92d2-874040679bd2@amd.com> (raw)
In-Reply-To: <aaf7b619-1826-4db2-aaa6-235a25f46299@arm.com>

Hi Robin,


On 5/28/2024 11:36 PM, Robin Murphy wrote:
> On 2024-05-28 5:39 pm, Vasant Hegde wrote:
>> Commit 3ab657291638 ("iommu: use the __iommu_attach_device() directly for
>> deferred attach")
>> replaced iommu_attach_device() call with __iommu_attach_device(). But
>> missed to take group lock. Take lock before attaching device to domain.
> 
> Why? Nothing here is even touching the group. If we've reached a deferred attach
> then we know the domain is the default domain already initialised and set as the
> current of the device's group, and the device is already otherwise added and
> holding a reference to that group, and a driver is bound to the device in order
> to make the DMA API call we're inside, so nothing should be at risk of
> disappearing under our feet. Please clarify what purpose this locking actually
> serves.

I am sorry. I should have written better description. I was trying to see if its
safe to use  iommu_group_mutex_assert() in our driver. I did audit the iommu.c
code and saw this one place it was missed. I just looked into git history and
tried to fix it. As you explained below I should have looked into the caller of
this function in detail!

> 
> But then there's also the fact that the initial DMA mapping operation which
> triggers deferred attach could legitimately be called under a spinlock or in an
> IRQ handler, so if anything it was a bug in 795bbbb9b6f8 ("iommu/dma-iommu:
> Handle deferred devices") to ever use iommu_attach_device() (and thus involve
> the mutex) in the first place :/

Thanks for the detailed explanation. I will drop this patch in next version.

-Vasant


> 
> Thanks,
> Robin.
> 
>> Fixes: 3ab657291638 ("iommu: use the __iommu_attach_device() directly for
>> deferred attach")
>> Cc: Lianbo Jiang <lijiang@redhat.com>
>> Cc: Robin Murphy <robin.murphy@arm.com>
>> Signed-off-by: Vasant Hegde <vasant.hegde@amd.com>
>> ---
>>   drivers/iommu/iommu.c | 15 ++++++++++++---
>>   1 file changed, 12 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
>> index 9df7cc75c1bc..1743dba023b6 100644
>> --- a/drivers/iommu/iommu.c
>> +++ b/drivers/iommu/iommu.c
>> @@ -2114,10 +2114,19 @@ EXPORT_SYMBOL_GPL(iommu_attach_device);
>>     int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain)
>>   {
>> -    if (dev->iommu && dev->iommu->attach_deferred)
>> -        return __iommu_attach_device(domain, dev);
>> +    int ret = 0;
>> +    struct iommu_group *group = dev->iommu_group;
>>   -    return 0;
>> +    if (!group)
>> +        return -EINVAL;
>> +
>> +    if (dev->iommu && dev->iommu->attach_deferred) {
>> +        mutex_lock(&group->mutex);
>> +        ret = __iommu_attach_device(domain, dev);
>> +        mutex_unlock(&group->mutex);
>> +    }
>> +
>> +    return ret;
>>   }
>>     void iommu_detach_device(struct iommu_domain *domain, struct device *dev)

  reply	other threads:[~2024-05-29  7:24 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-28 16:39 [PATCH 1/2] iommu: Take group lock before attaching device in iommu_deferred_attach() Vasant Hegde
2024-05-28 16:39 ` [PATCH 2/2] iommu/amd: Fix Invalid wait context issue Vasant Hegde
2024-05-28 20:43   ` Chris Bainbridge
2024-05-29  6:35   ` Mikhail Gavrilov
2024-05-28 18:06 ` [PATCH 1/2] iommu: Take group lock before attaching device in iommu_deferred_attach() Robin Murphy
2024-05-29  7:24   ` Vasant Hegde [this message]
2024-06-10 17:44     ` Jason Gunthorpe
2024-06-24 14:20       ` Vasant Hegde
2024-06-24 15:25         ` Jason Gunthorpe
2024-06-24 16:02           ` Vasant Hegde

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=029a1733-4e7e-4deb-92d2-874040679bd2@amd.com \
    --to=vasant.hegde@amd.com \
    --cc=iommu@lists.linux.dev \
    --cc=joro@8bytes.org \
    --cc=lijiang@redhat.com \
    --cc=robin.murphy@arm.com \
    --cc=suravee.suthikulpanit@amd.com \
    /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