Linux IOMMU Development
 help / color / mirror / Atom feed
From: Yi Liu <yi.l.liu@intel.com>
To: Jason Gunthorpe <jgg@nvidia.com>
Cc: <joro@8bytes.org>, <kevin.tian@intel.com>,
	<baolu.lu@linux.intel.com>, <iommu@lists.linux.dev>,
	<robin.murphy@arm.com>, <nicolinc@nvidia.com>, <will@kernel.org>
Subject: Re: [PATCH 4/5] iommu: Swap the order of setting group->pasid_array and calling attach op of iommu drivers
Date: Wed, 19 Feb 2025 12:29:37 +0800	[thread overview]
Message-ID: <975f9f29-bf06-48d2-96bf-49a68ae1cd49@intel.com> (raw)
In-Reply-To: <20250218192718.GE4183890@nvidia.com>

On 2025/2/19 03:27, Jason Gunthorpe wrote:
> On Tue, Feb 11, 2025 at 10:05:39PM -0800, Yi Liu wrote:
>> The current implementation stores group->pasid_array before the underlying
>> iommu driver has successfully set the new domain. This can lead to issues
>> where PRIs are received on the new domain before the attach operation is
>> completed. This patch swaps the order of operations to ensure that the
>> domain is set in the iommu driver before updating group->pasid_array.
> 
> I think I would drop this note in a comment since it is subtle and
> important..
> 
>> -	ret = xa_insert(&group->pasid_array, pasid, pasid_entry, GFP_KERNEL);
>> -	if (ret)
>> +	if (xa_load(&group->pasid_array, pasid)) {
>> +		ret = -EBUSY;
>>   		goto out_unlock;
>> +	}
> 
> Let's write it like this:
> 
> 	ret = xa_insert(&group->pasid_array, pasid, XA_ZERO_ENTRY, GFP_KERNEL);
> 	if (ret)
> 		goto out_unlock;
> 
> 	ret = __iommu_set_group_pasid(domain, group, pasid);
> 	if (ret) {
> 		xa_release(&group->pasid_array, pasid);
> 		goto out_unlock;
> 	}
> 
> 	/*
> 	 * The xa_insert() above reserved the memory, and the group->mutex is
> 	 * held, this cannot fail. The new domain cannot be visible until the
> 	 * operation succeeds as we cannot tolerate PRIs becoming concurrently
> 	 * queued and then failing attach.
> 	 */
> 	ret = xa_err(
> 		xa_store(&group->pasid_array, pasid, pasid_entry, GFP_KERNEL));
> 	WARN_ON(ret);
> 	ret = 0;

perhaps we can save the above line by WARN_ON(xa_is_err(xa_store())); :)

> 
>> @@ -3510,19 +3518,26 @@ int iommu_attach_group_handle(struct iommu_domain *domain,
>>   	pasid_entry = iommu_make_pasid_entry(domain, handle);
>>   
>>   	mutex_lock(&group->mutex);
>> -	ret = xa_insert(&group->pasid_array,
>> -			IOMMU_NO_PASID, pasid_entry, GFP_KERNEL);
>> -	if (ret)
>> +	if (xa_load(&group->pasid_array, IOMMU_NO_PASID)) {
>> +		ret = -EBUSY;
>>   		goto err_unlock;
>> +	}
> 
> Same here

yes.

-- 
Regards,
Yi Liu

  reply	other threads:[~2025-02-19  4:24 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-12  6:05 [PATCH 0/5] Misc iommu_attach_handle enhancements in iommu core Yi Liu
2025-02-12  6:05 ` [PATCH 1/5] iommu: Make @handle mandatory in iommu_{attach|replace}_group_handle() Yi Liu
2025-02-18 19:09   ` Jason Gunthorpe
2025-02-19 16:48   ` Nicolin Chen
2025-02-20  3:50     ` Yi Liu
2025-02-20  8:21   ` Tian, Kevin
2025-02-12  6:05 ` [PATCH 2/5] iommu: Drop iommu_group_replace_domain() Yi Liu
2025-02-18 19:10   ` Jason Gunthorpe
2025-02-19 16:53   ` Nicolin Chen
2025-02-20  3:51     ` Yi Liu
2025-02-20  8:22   ` Tian, Kevin
2025-02-12  6:05 ` [PATCH 3/5] iommu: Store either domain or handle in group->pasid_array Yi Liu
2025-02-18 19:14   ` Jason Gunthorpe
2025-02-19 17:15   ` Nicolin Chen
2025-02-20  3:51     ` Yi Liu
2025-02-20  8:28     ` Tian, Kevin
2025-02-20  8:27   ` Tian, Kevin
2025-02-12  6:05 ` [PATCH 4/5] iommu: Swap the order of setting group->pasid_array and calling attach op of iommu drivers Yi Liu
2025-02-18 19:27   ` Jason Gunthorpe
2025-02-19  4:29     ` Yi Liu [this message]
2025-02-20  8:31     ` Tian, Kevin
2025-02-12  6:05 ` [PATCH 5/5] iommu: Retire group->domain Yi Liu
2025-02-18 19:39   ` Jason Gunthorpe
2025-02-19  6:52     ` Yi Liu
2025-02-19 12:31       ` Yi Liu
2025-02-19 13:10         ` Jason Gunthorpe
2025-02-20  4:01           ` Yi Liu
2025-02-20  8:33             ` Tian, Kevin
2025-02-20 16:18             ` Jason Gunthorpe
2025-02-18 19:57   ` Jason Gunthorpe
2025-02-19 12:20     ` Yi Liu
2025-02-12 15:25 ` [PATCH 0/5] Misc iommu_attach_handle enhancements in iommu core Jason Gunthorpe
2025-02-13  3:16   ` Yi Liu
2025-02-13 15:08     ` Jason Gunthorpe

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=975f9f29-bf06-48d2-96bf-49a68ae1cd49@intel.com \
    --to=yi.l.liu@intel.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@nvidia.com \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=nicolinc@nvidia.com \
    --cc=robin.murphy@arm.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