Linux IOMMU Development
 help / color / mirror / Atom feed
From: Vasant Hegde <vasant.hegde@amd.com>
To: Jason Gunthorpe <jgg@ziepe.ca>
Cc: iommu@lists.linux.dev, joro@8bytes.org,
	suravee.suthikulpanit@amd.com, wei.huang2@amd.com,
	jsnitsel@redhat.com, Baolu Lu <baolu.lu@linux.intel.com>
Subject: Re: [PATCH v4 07/16] iommu/amd: Introduce per-device domain ID to workaround potential TLB aliasing issue
Date: Thu, 11 Jan 2024 16:48:01 +0530	[thread overview]
Message-ID: <d71a2bc0-ed07-53eb-faf1-bd36b6ff6ffb@amd.com> (raw)
In-Reply-To: <20240105185549.GM50608@ziepe.ca>

Joson,

[+ Baolu - adding as I have some question around PASID support for UNMANAGED domain]

On 1/6/2024 12:25 AM, Jason Gunthorpe wrote:
> On Tue, Dec 12, 2023 at 08:52:15AM +0000, Vasant Hegde wrote:
>> With v1 page table, the AMD IOMMU spec states that the hardware must use
>> the domain ID to tag its internal translation caches. I/O devices with
>> different v1 page tables must be given different domain IDs. I/O devices
>> that share the same v1 page table __may__ be given the same domain ID.
>> This domain ID management policy is currently implemented by the AMD
>> IOMMU driver. In this case, only the domain ID is needed when issuing the
>> INVALIDATE_IOMMU_PAGES command to invalidate the IOMMU translation cache
>> (TLB).
>>
>> With v2 page table, the hardware uses domain ID and PASID as parameters
>> to tag and issue the INVALIDATE_IOMMU_PAGES command. Since the GCR3 table
>> is setup per-device, and there is no guarantee for PASID to be unique
>> across multiple devices. The same PASID for different devices could
>> have different v2 page tables. In such case, if multiple devices share the
>> same domain ID, IOMMU translation cache for these devices would be polluted
>> due to TLB aliasing.
>>
>> Hence, avoid the TLB aliasing issue with v2 page table by allocating unique
>> domain ID for each device even when multiple devices are sharing the same v1
>> page table. Please note that this workaround would result in multiple
>> INVALIDATE_IOMMU_PAGES commands (one per domain id) when unmapping a
>> translation on the shared v1 page table.
> 
> Huh? This is a typo? "the same v2 page table"  "unmapping a
> translation on the shared v2 page table" ??

Its typo. Will fix it.

> 
> The code seems to be fine, domain_flush_pages_v1() looks optimal?
> 

I'd say its optimal for given state. I have a patch to move dev_iommu[] to
xarray. I am planning to fine tune and post those patches after SVA. With that
changes it will be better.


>> diff --git a/drivers/iommu/amd/amd_iommu_types.h b/drivers/iommu/amd/amd_iommu_types.h
>> index fead9033796f..51daf5dd4729 100644
>> --- a/drivers/iommu/amd/amd_iommu_types.h
>> +++ b/drivers/iommu/amd/amd_iommu_types.h
>> @@ -842,6 +842,8 @@ struct iommu_dev_data {
>>  	u8 ppr          :1;		  /* Enable device PPR support */
>>  	bool use_vapic;			  /* Enable device to use vapic mode */
>>  	bool defer_attach;
>> +	/* Per device domain ID. Used with V2 page table */
>> +	u16 domid;
> 
> This should really be put into the 'struct gcr3_tbl_info' - logically
> that is the struct the HW cache tag is linked to. ie if the gcr3 table
> is the same pointer then the cache tag can be re-used by the HW.
> 

The reason we put it in dev_data is because its per device ID, not specific to
GCR3 table.

> Then when you want to optimize for the no-pasid case then the right
> way to do it is putting a 'struct gcr3_tbl_info' inside the v2
> protection_domain.
> 
> The DTE will point at the v2 protection_domain's version of the gcr3
> if the PASID table is empty, otherwise the DTE will point at the
> struct iommu_dev_data version of the gcr3 table.

This makese sense if we are sure we will do per-device-domain-ID only with V2
page table. I still need to see how SVA support with vIOMMU works. For now I
will keep this in my list. Once I have better picture I will fine tune.

> 
> Naturally this will optimize the lifecycle of the domain_id.
> 
> Then put the domain_id alloc/dealloc inside the functions that
> alloc/free the memory under the struct gcr3_tbl_info - ie it is part
> of the gcr3 layer.

Domain ID is part of domain/device not specific to gcr3 layer. In V1 we don't
have GCR3 stuff.

> 
>> +/*
>> + * Allocate per device domain ID when using V2 page table
>> + */
>> +static inline bool domain_id_is_per_dev(struct protection_domain *pdom)
>> +{
>> +	return (pdom && pdom->pd_mode != PD_MODE_V1);
>> +}
> 
> Under that view this function probably would make more sense as:
> 
> domain_requires_gcr3(pdom)
> 
> But frankly I'd just stick with pdom_is_v2_pgtbl_mode().
> 
> Also pdom should never be null when this is called, right?

Right. Will fix it.

> 
>> -/*
>> - * TLB invalidation function which is called from the mapping functions.
>> - * It invalidates a single PTE if the range to flush is within a single
>> - * page. Otherwise it flushes the whole TLB of the IOMMU.
>> - */
>> -static void __domain_flush_pages(struct protection_domain *domain,
>> +static int domain_flush_pages_v2(struct protection_domain *pdom,
>>  				 u64 address, size_t size)
>>  {
>>  	struct iommu_dev_data *dev_data;
>>  	struct iommu_cmd cmd;
>> -	int ret = 0, i;
>> -	ioasid_t pasid = IOMMU_NO_PASID;
>> -	bool gn = false;
>> +	int ret = 0;
>>  
>> -	if (pdom_is_v2_pgtbl_mode(domain))
>> -		gn = true;
>> +	list_for_each_entry(dev_data, &pdom->dev_list, list) {
>> +		struct amd_iommu *iommu = get_amd_iommu_from_dev(dev_data->dev);
>> +
>> +		build_inv_iommu_pages(&cmd, address, size,
>> +				      dev_data->domid, IOMMU_NO_PASID, true);
>> +
>> +		ret |= iommu_queue_command(iommu, &cmd);
>> +	}
> 
> This is fine for where things are here, but what you want to get to
> long term is what I've been talking about of having the attachment
> list on the protection_domain.
> 
> Each attachment entry would hold:
>   struct device *dev;
>   unsigned int pasid;
> 
> Keep the list sorted by (iommu, gcr.domain_id).
> 
> Then optimized invalidation is simply this:
> 
> for_each:
>   if (get_amd_iommu_from_dev(entry->dev) == last_iommu &&
>       get_gcr(entry, pdom)->domain_id == last_domain_id)
>      continue;
>   build_inv_iommu_pages(..)
>   last_iommu = get_amd_iommu_from_dev(entry->dev)
>   domain_id = get_gcr(entry, pdom)->domain_id;


I have done a prototype something in this line. Basically we will eventually
have single API for invalidation :
  domain_flush_pages(pdom, addr, size)

.. and internally it will decide to flush host/guest page table.


[Slightly unrelated topic]

UNAMANGED Domain and PASID support:
  - I was considering this scenario as well. I don't think I understood the use
case of and how invalidation is suppose to work here.

   Can you explain (again?) the use case and how invalidation is suppose to work?

  - For PASID capable device we will have per-device-domain-ID
  - We will have default page table setup (PASID zero in our case) during domain
initialization.
  - We attach PASIDs to same domain. We can add this to list (protection domain
device List info - which will have dev_data/PASID). So set/remove PASIDs is fine.

  - iommu_ops->iotlb_sync_map/flush_iotlb_all will flush PASID zero.
    Looking into intel driver they seems to be invalidating all PASIDs in this
path. I didn't get why it has to flush all PASIDs here.

  - For other PASIDs do we have mmu notifier to invalidate as its attached to
some process?


> 
> And this algorithm matches what Intel and SMMU need and I'm strongly
> thinking about making a driver utility library to handle it, so
> we can revisit this later on.

If things are common across drivers then it makes sense. We can consider common
set of functions.


> 
>> +static int domain_flush_pages_v1(struct protection_domain *pdom,
>> +				 u64 address, size_t size)
>> +{
>> +	struct iommu_cmd cmd;
>> +	int ret = 0, i;
>>  
>> -	build_inv_iommu_pages(&cmd, address, size, domain->id, pasid, gn);
>> +	build_inv_iommu_pages(&cmd, address, size,
>> +			      pdom->id, IOMMU_NO_PASID, false);
>>  
>>  	for (i = 0; i < amd_iommu_get_num_iommus(); ++i) {
>> -		if (!domain->dev_iommu[i])
>> +		if (!pdom->dev_iommu[i])
>>  			continue;
> 
> Right, iterate over every iommu

As explained above I believe this will be better once I move this to xarray.

-Vasant

  reply	other threads:[~2024-01-11 11:18 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-12  8:52 [PATCH v4 00/16] iommu/amd: SVA Support (part 3) - refactor support for GCR3 table Vasant Hegde
2023-12-12  8:52 ` [PATCH v4 01/16] iommu/amd: Pass struct iommu_dev_data to set_dte_entry() Vasant Hegde
2023-12-12  8:52 ` [PATCH v4 02/16] iommu/amd: Enable Guest Translation before registering devices Vasant Hegde
2024-01-05 18:17   ` Jason Gunthorpe
2023-12-12  8:52 ` [PATCH v4 03/16] iommu/amd: Introduce get_amd_iommu_from_dev() Vasant Hegde
2024-01-05 18:27   ` Jason Gunthorpe
2024-01-10 12:07     ` Vasant Hegde
2023-12-12  8:52 ` [PATCH v4 04/16] iommu/amd: Introduce struct protection_domain.pd_mode Vasant Hegde
2023-12-12  8:52 ` [PATCH v4 05/16] iommu/amd: Introduce per-device GCR3 table Vasant Hegde
2023-12-12  8:52 ` [PATCH v4 06/16] iommu/amd: Use protection_domain.flags to check page table mode Vasant Hegde
2024-01-05 18:28   ` Jason Gunthorpe
2023-12-12  8:52 ` [PATCH v4 07/16] iommu/amd: Introduce per-device domain ID to workaround potential TLB aliasing issue Vasant Hegde
2024-01-05 18:55   ` Jason Gunthorpe
2024-01-11 11:18     ` Vasant Hegde [this message]
2024-01-11 13:59       ` Jason Gunthorpe
2024-01-12 12:45         ` Vasant Hegde
2024-01-12 14:59           ` Jason Gunthorpe
2024-01-16 10:52             ` Vasant Hegde
2024-01-16 14:00               ` Jason Gunthorpe
2024-01-16 17:08                 ` Vasant Hegde
2024-01-16 17:22                   ` Jason Gunthorpe
2023-12-12  8:52 ` [PATCH v4 08/16] iommu/amd: Add support for device based TLB invalidation Vasant Hegde
2024-01-05 19:03   ` Jason Gunthorpe
2023-12-12  8:52 ` [PATCH v4 09/16] iommu/amd: Rearrange GCR3 table setup code Vasant Hegde
2024-01-05 19:04   ` Jason Gunthorpe
2023-12-12  8:52 ` [PATCH v4 10/16] iommu/amd: Refactor helper function for setting / clearing GCR3 Vasant Hegde
2024-01-05 19:12   ` Jason Gunthorpe
2024-01-11 11:52     ` Vasant Hegde
2024-01-11 13:26       ` Jason Gunthorpe
2024-01-12  9:00         ` Vasant Hegde
2023-12-12  8:52 ` [PATCH v4 11/16] iommu/amd: Refactor attaching / detaching device functions Vasant Hegde
2024-01-05 19:14   ` Jason Gunthorpe
2024-01-11 10:06     ` Vasant Hegde
2023-12-12  8:52 ` [PATCH v4 12/16] iommu/amd: Refactor protection_domain helper functions Vasant Hegde
2023-12-12  8:52 ` [PATCH v4 13/16] iommu/amd: Refactor GCR3 table " Vasant Hegde
2024-01-05 19:21   ` Jason Gunthorpe
2024-01-11  5:39     ` Vasant Hegde
2023-12-12  8:52 ` [PATCH v4 14/16] iommu/amd: Remove unused flush pasid functions Vasant Hegde
2024-01-05 19:22   ` Jason Gunthorpe
2023-12-12  8:52 ` [PATCH v4 15/16] iommu/amd: Rearrange device flush code Vasant Hegde
2024-01-05 19:22   ` Jason Gunthorpe
2023-12-12  8:52 ` [PATCH v4 16/16] iommu/amd: Remove unused GCR3 table parameters from struct protection_domain 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=d71a2bc0-ed07-53eb-faf1-bd36b6ff6ffb@amd.com \
    --to=vasant.hegde@amd.com \
    --cc=baolu.lu@linux.intel.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=joro@8bytes.org \
    --cc=jsnitsel@redhat.com \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=wei.huang2@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