All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yi Liu <yi.l.liu@intel.com>
To: Nicolin Chen <nicolinc@nvidia.com>
Cc: <joro@8bytes.org>, <jgg@nvidia.com>, <kevin.tian@intel.com>,
	<baolu.lu@linux.intel.com>, <iommu@lists.linux.dev>,
	<robin.murphy@arm.com>, <will@kernel.org>
Subject: Re: [PATCH v2 4/4] iommu: Swap the order of setting group->pasid_array and calling attach op of iommu drivers
Date: Sat, 22 Feb 2025 09:59:05 +0800	[thread overview]
Message-ID: <ceb475fa-dfdf-4bcb-a4ba-8128ba86a39a@intel.com> (raw)
In-Reply-To: <Z7i3dvyA/opurciP@Asurada-Nvidia>

On 2025/2/22 01:27, Nicolin Chen wrote:
> On Fri, Feb 21, 2025 at 06:33:35AM -0800, Yi Liu wrote:
>> The current implementation stores entry to the 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 underlying iommu driver before updating the group->pasid_array.
>>
>> Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
>> Reviewed-by: Kevin Tian <kevin.tian@intel.com>
>> Signed-off-by: Yi Liu <yi.l.liu@intel.com>
> 
> Reviewed-by: Nicolin Chen <nicolinc@nvidia.com>
> 
> A nit:
> 
>> ---
>>   drivers/iommu/iommu.c | 44 +++++++++++++++++++++++++++++++------------
>>   1 file changed, 32 insertions(+), 12 deletions(-)
>>
>> diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
>> index eff5f678883b..73555e1cf016 100644
>> --- a/drivers/iommu/iommu.c
>> +++ b/drivers/iommu/iommu.c
>> @@ -3388,13 +3388,25 @@ int iommu_attach_device_pasid(struct iommu_domain *domain,
>>   
>>   	entry = iommu_make_pasid_array_entry(domain, handle);
>>   
>> -	ret = xa_insert(&group->pasid_array, pasid, entry, GFP_KERNEL);
>> +	ret = xa_insert(&group->pasid_array, pasid, XA_ZERO_ENTRY, GFP_KERNEL);
> 
> Maybe xa_reserve() pairing xa_release()? Same thing though..

there is slight difference. xa_reserve() will return 0 if there is already
a valid entry. xa_insert() shall fail with EBUSY. We want to catch such
failures here. You may notice that the replace function uses xa_reserve()
as it intend to replace existing entry.

>>   	if (ret)
>>   		goto out_unlock;
>>   
>>   	ret = __iommu_set_group_pasid(domain, group, pasid);
>> -	if (ret)
>> -		xa_erase(&group->pasid_array, pasid);
>> +	if (ret) {
>> +		xa_release(&group->pasid_array, pasid);
>> +		goto out_unlock;
>> +	}
>> +
>> +	/*
>> +	 * The xa_insert() above reserved the memory, and the group->mutex is
> 
> Just in case we would like update the patch, reword "xa_insert".
> 
>> +	 * 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.
>> +	 */
>> +	WARN_ON(xa_is_err(xa_store(&group->pasid_array,
>> +				   pasid, entry, GFP_KERNEL)));
>> +
>>   out_unlock:
>>   	mutex_unlock(&group->mutex);
>>   	return ret;
>> @@ -3509,19 +3521,27 @@ int iommu_attach_group_handle(struct iommu_domain *domain,
>>   
>>   	mutex_lock(&group->mutex);
>>   	entry = iommu_make_pasid_array_entry(domain, handle);
>> -	ret = xa_insert(&group->pasid_array, IOMMU_NO_PASID, entry, GFP_KERNEL);
>> +	ret = xa_insert(&group->pasid_array,
>> +			IOMMU_NO_PASID, XA_ZERO_ENTRY, GFP_KERNEL);
> 
> Ditto.
> 
>>   	if (ret)
>> -		goto err_unlock;
>> +		goto out_unlock;
>>   
>>   	ret = __iommu_attach_group(domain, group);
>> -	if (ret)
>> -		goto err_erase;
>> -	mutex_unlock(&group->mutex);
>> +	if (ret) {
>> +		xa_release(&group->pasid_array, IOMMU_NO_PASID);
>> +		goto out_unlock;
>> +	}
>>   
>> -	return 0;
>> -err_erase:
>> -	xa_erase(&group->pasid_array, IOMMU_NO_PASID);
>> -err_unlock:
>> +	/*
>> +	 * The xa_insert() above reserved the memory, and the group->mutex is
> 
> "xa_insert" here too.
> 
> Thanks
> Nicolin
> 

-- 
Regards,
Yi Liu

  reply	other threads:[~2025-02-22  1:53 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-21 14:33 [PATCH v2 0/4] Misc iommu_attach_handle enhancements in iommu core Yi Liu
2025-02-21 14:33 ` [PATCH v2 1/4] iommu: Make @handle mandatory in iommu_{attach|replace}_group_handle() Yi Liu
2025-02-23 13:08   ` Baolu Lu
2025-02-21 14:33 ` [PATCH v2 2/4] iommu: Drop iommu_group_replace_domain() Yi Liu
2025-02-23 13:18   ` Baolu Lu
2025-02-24  4:40     ` Yi Liu
2025-02-24 14:43       ` Jason Gunthorpe
2025-02-21 14:33 ` [PATCH v2 3/4] iommu: Store either domain or handle in group->pasid_array Yi Liu
2025-02-23 13:22   ` Baolu Lu
2025-02-24  2:38     ` Tian, Kevin
2025-02-24  2:57       ` Baolu Lu
2025-02-24  3:04         ` Tian, Kevin
2025-02-24  4:22           ` Yi Liu
2025-02-21 14:33 ` [PATCH v2 4/4] iommu: Swap the order of setting group->pasid_array and calling attach op of iommu drivers Yi Liu
2025-02-21 17:27   ` Nicolin Chen
2025-02-22  1:59     ` Yi Liu [this message]
2025-02-22 15:35       ` Nicolin Chen
2025-02-23 13:32       ` Baolu Lu
2025-02-24  4:38         ` Yi Liu
2025-02-24  5:07           ` Baolu Lu
2025-02-24  5:28             ` Yi Liu
2025-02-23 13:28   ` Baolu Lu
2025-02-25  0:12   ` Jason Gunthorpe
2025-02-25  0:13 ` [PATCH v2 0/4] Misc iommu_attach_handle enhancements in iommu core Jason Gunthorpe
2025-02-25  3:35   ` Yi Liu
2025-02-28  9:23   ` Joerg Roedel

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=ceb475fa-dfdf-4bcb-a4ba-8128ba86a39a@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.