Linux IOMMU Development
 help / color / mirror / Atom feed
From: Baolu Lu <baolu.lu@linux.intel.com>
To: Joao Martins <joao.m.martins@oracle.com>, iommu@lists.linux.dev
Cc: baolu.lu@linux.intel.com, Jason Gunthorpe <jgg@nvidia.com>,
	Kevin Tian <kevin.tian@intel.com>,
	Shameerali Kolothum Thodi <shameerali.kolothum.thodi@huawei.com>,
	Yi Liu <yi.l.liu@intel.com>, Yi Y Sun <yi.y.sun@intel.com>,
	Nicolin Chen <nicolinc@nvidia.com>,
	Joerg Roedel <joro@8bytes.org>,
	Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>,
	Will Deacon <will@kernel.org>,
	Robin Murphy <robin.murphy@arm.com>,
	Alex Williamson <alex.williamson@redhat.com>,
	kvm@vger.kernel.org
Subject: Re: [PATCH v3 19/19] iommu/intel: Access/Dirty bit support for SL domains
Date: Tue, 17 Oct 2023 20:49:59 +0800	[thread overview]
Message-ID: <4b5cd75a-c0cd-c9bd-0d08-8c889861d48f@linux.intel.com> (raw)
In-Reply-To: <e797b35b-6a17-4114-a886-95e6402ad03c@oracle.com>

On 2023/10/17 19:22, Joao Martins wrote:
> On 17/10/2023 03:08, Baolu Lu wrote:
>> On 10/17/23 12:00 AM, Joao Martins wrote:
>>>>> The iommu_dirty_bitmap is defined in iommu core. The iommu driver has no
>>>>> need to understand it and check its member anyway.
>>>>>
>>>> (...) The iommu driver has no need to understand it. iommu_dirty_bitmap_record()
>>>> already makes those checks in case there's no iova_bitmap to set bits to.
>>>>
>>> This is all true but the reason I am checking iommu_dirty_bitmap::bitmap is to
>>> essentially not record anything in the iova bitmap and just clear the dirty bits
>>> from the IOPTEs, all when dirty tracking is technically disabled. This is done
>>> internally only when starting dirty tracking, and thus to ensure that we cleanup
>>> all dirty bits before we enable dirty tracking to have a consistent snapshot as
>>> opposed to inheriting dirties from the past.
>>
>> It's okay since it serves a functional purpose. Can you please add some
>> comments around the code to explain the rationale.
>>
> 
> I added this comment below:
> 
> +       /*
> +        * IOMMUFD core calls into a dirty tracking disabled domain without an
> +        * IOVA bitmap set in order to clean dirty bits in all PTEs that might
> +        * have occured when we stopped dirty tracking. This ensures that we
> +        * never inherit dirtied bits from a previous cycle.
> +        */
> 

Yes. It's clear. Thank you!

> Also fixed an issue where I could theoretically clear the bit with
> IOMMU_NO_CLEAR. Essentially passed the read_and_clear_dirty flags and let
> dma_sl_pte_test_and_clear_dirty() to test and test-and-clear, similar to AMD:
> 
> @@ -781,6 +788,16 @@ static inline bool dma_pte_present(struct dma_pte *pte)
>          return (pte->val & 3) != 0;
>   }
> 
> +static inline bool dma_sl_pte_test_and_clear_dirty(struct dma_pte *pte,
> +                                                  unsigned long flags)
> +{
> +       if (flags & IOMMU_DIRTY_NO_CLEAR)
> +               return (pte->val & DMA_SL_PTE_DIRTY) != 0;
> +
> +       return test_and_clear_bit(DMA_SL_PTE_DIRTY_BIT,
> +                                 (unsigned long *)&pte->val);
> +}
> +

Yes. Sure.

> Anyhow, see below the full diff compared to this patch. Some things are in tree
> that is different to submitted from this patch.

[...]

> @@ -4113,7 +4123,7 @@ static void intel_iommu_domain_free(struct iommu_domain
> *domain)
>   }
> 
>   static int prepare_domain_attach_device(struct iommu_domain *domain,
> -					struct device *dev)
> +					struct device *dev, ioasid_t pasid)

How about blocking pasid attaching in intel_iommu_set_dev_pasid().

>   {
>   	struct dmar_domain *dmar_domain = to_dmar_domain(domain);
>   	struct intel_iommu *iommu;
> @@ -4126,7 +4136,8 @@ static int prepare_domain_attach_device(struct
> iommu_domain *domain,
>   	if (dmar_domain->force_snooping && !ecap_sc_support(iommu->ecap))
>   		return -EINVAL;
> 
> -	if (domain->dirty_ops && !intel_iommu_slads_supported(iommu))
> +	if (domain->dirty_ops &&
> +	    (!slads_supported(iommu) || pasid != IOMMU_NO_PASID))
>   		return -EINVAL;
> 
>   	/* check if this iommu agaw is sufficient for max mapped address */

[...]

> 
> @@ -4886,14 +4897,16 @@ static int intel_iommu_read_and_clear_dirty(struct
> iommu_domain *domain,
>   	unsigned long pgsize;
>   	bool ad_enabled;
> 
> -	spin_lock(&dmar_domain->lock);
> +	/*
> +	 * IOMMUFD core calls into a dirty tracking disabled domain without an
> +	 * IOVA bitmap set in order to clean dirty bits in all PTEs that might
> +	 * have occured when we stopped dirty tracking. This ensures that we
> +	 * never inherit dirtied bits from a previous cycle.
> +	 */
>   	ad_enabled = dmar_domain->dirty_tracking;
> -	spin_unlock(&dmar_domain->lock);
> -
>   	if (!ad_enabled && dirty->bitmap)

How about
	if (!dmar_domain->dirty_tracking && dirty->bitmap)
		return -EINVAL;
?

>   		return -EINVAL;
> 
> -	rcu_read_lock();
>   	do {
>   		struct dma_pte *pte;
>   		int lvl = 0;
> @@ -4906,14 +4919,10 @@ static int intel_iommu_read_and_clear_dirty(struct
> iommu_domain *domain,
>   			continue;
>   		}
> 
> -		/* It is writable, set the bitmap */
> -		if (((flags & IOMMU_DIRTY_NO_CLEAR) &&
> -				dma_sl_pte_dirty(pte)) ||
> -		    dma_sl_pte_test_and_clear_dirty(pte))
> +		if (dma_sl_pte_test_and_clear_dirty(pte, flags))
>   			iommu_dirty_bitmap_record(dirty, iova, pgsize);
>   		iova += pgsize;
>   	} while (iova < end);
> -	rcu_read_unlock();
> 
>   	return 0;
>   }
> diff --git a/drivers/iommu/intel/iommu.h b/drivers/iommu/intel/iommu.h
> index bccd44db3316..0b390d9e669b 100644
> --- a/drivers/iommu/intel/iommu.h
> +++ b/drivers/iommu/intel/iommu.h
> @@ -542,6 +542,9 @@ enum {
>   #define sm_supported(iommu)	(intel_iommu_sm && ecap_smts((iommu)->ecap))
>   #define pasid_supported(iommu)	(sm_supported(iommu) &&			\
>   				 ecap_pasid((iommu)->ecap))
> +#define slads_supported(iommu) (sm_supported(iommu) &&                 \
> +                                ecap_slads((iommu)->ecap))
> +
> 
>   struct pasid_entry;
>   struct pasid_state_entry;
> @@ -785,13 +788,12 @@ static inline bool dma_pte_present(struct dma_pte *pte)
>   	return (pte->val & 3) != 0;
>   }
> 
> -static inline bool dma_sl_pte_dirty(struct dma_pte *pte)
> +static inline bool dma_sl_pte_test_and_clear_dirty(struct dma_pte *pte,
> +						   unsigned long flags)
>   {
> -	return (pte->val & DMA_SL_PTE_DIRTY) != 0;
> -}
> +	if (flags & IOMMU_DIRTY_NO_CLEAR)
> +		return (pte->val & DMA_SL_PTE_DIRTY) != 0;
> 
> -static inline bool dma_sl_pte_test_and_clear_dirty(struct dma_pte *pte)
> -{
>   	return test_and_clear_bit(DMA_SL_PTE_DIRTY_BIT,
>   				  (unsigned long *)&pte->val);
>   }
> diff --git a/drivers/iommu/intel/pasid.c b/drivers/iommu/intel/pasid.c
> index 03814942d59c..785384a59d55 100644
> --- a/drivers/iommu/intel/pasid.c
> +++ b/drivers/iommu/intel/pasid.c
> @@ -686,15 +686,29 @@ int intel_pasid_setup_dirty_tracking(struct intel_iommu
> *iommu,
> 
>   	spin_lock(&iommu->lock);
> 
> -	did = domain_id_iommu(domain, iommu);
>   	pte = intel_pasid_get_entry(dev, pasid);
>   	if (!pte) {
>   		spin_unlock(&iommu->lock);
> -		dev_err(dev, "Failed to get pasid entry of PASID %d\n", pasid);
> +		dev_err_ratelimited(dev,
> +				    "Failed to get pasid entry of PASID %d\n",
> +				    pasid);
>   		return -ENODEV;
>   	}
> 
> +	did = domain_id_iommu(domain, iommu);
>   	pgtt = pasid_pte_get_pgtt(pte);
> +	if (pgtt != PASID_ENTRY_PGTT_SL_ONLY && pgtt != PASID_ENTRY_PGTT_NESTED) {
> +		spin_unlock(&iommu->lock);
> +		dev_err_ratelimited(dev,
> +				    "Dirty tracking not supported on translation type %d\n",
> +				    pgtt);
> +		return -EOPNOTSUPP;
> +	}
> +
> +	if (pasid_get_ssade(pte) == enabled) {
> +		spin_unlock(&iommu->lock);
> +		return 0;
> +	}
> 
>   	if (enabled)
>   		pasid_set_ssade(pte);
> @@ -702,6 +716,9 @@ int intel_pasid_setup_dirty_tracking(struct intel_iommu *iommu,
>   		pasid_clear_ssade(pte);
>   	spin_unlock(&iommu->lock);
> 
> +	if (!ecap_coherent(iommu->ecap))
> +		clflush_cache_range(pte, sizeof(*pte));
> +
>   	/*
>   	 * From VT-d spec table 25 "Guidance to Software for Invalidations":
>   	 *
> @@ -720,8 +737,6 @@ int intel_pasid_setup_dirty_tracking(struct intel_iommu *iommu,
> 
>   	if (pgtt == PASID_ENTRY_PGTT_SL_ONLY || pgtt == PASID_ENTRY_PGTT_NESTED)
>   		iommu->flush.flush_iotlb(iommu, did, 0, 0, DMA_TLB_DSI_FLUSH);
> -	else
> -		qi_flush_piotlb(iommu, did, pasid, 0, -1, 0);
> 
>   	/* Device IOTLB doesn't need to be flushed in caching mode. */
>   	if (!cap_caching_mode(iommu->cap))

Others look good to me. Thanks a lot.

Best regards,
baolu

  reply	other threads:[~2023-10-17 12:50 UTC|newest]

Thread overview: 140+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-23  1:24 [PATCH v3 00/19] IOMMUFD Dirty Tracking Joao Martins
2023-09-23  1:24 ` [PATCH v3 01/19] vfio/iova_bitmap: Export more API symbols Joao Martins
2023-10-13 15:43   ` Jason Gunthorpe
2023-10-13 15:57     ` Joao Martins
2023-10-13 16:03       ` Jason Gunthorpe
2023-10-13 16:22         ` Joao Martins
2023-09-23  1:24 ` [PATCH v3 02/19] vfio: Move iova_bitmap into iommu core Joao Martins
2023-10-13 15:48   ` Jason Gunthorpe
2023-10-13 16:00     ` Joao Martins
2023-10-13 16:04       ` Jason Gunthorpe
2023-10-13 16:23         ` Joao Martins
2023-10-13 17:10       ` Joao Martins
2023-10-13 17:16         ` Jason Gunthorpe
2023-10-13 17:23           ` Joao Martins
2023-10-13 17:28             ` Jason Gunthorpe
2023-10-13 17:32               ` Joao Martins
2023-10-13 20:41             ` Alex Williamson
2023-10-13 21:20               ` Joao Martins
2023-10-13 21:51                 ` Alex Williamson
2023-10-14  0:02                   ` Jason Gunthorpe
2023-10-16 16:25                     ` Joao Martins
2023-10-16 16:34                       ` Jason Gunthorpe
2023-10-16 17:52                         ` Joao Martins
2023-10-16 18:05                           ` Jason Gunthorpe
2023-10-16 18:15                             ` Joao Martins
2023-10-16 18:20                               ` Jason Gunthorpe
2023-10-16 18:37                                 ` Joao Martins
2023-10-16 18:50                                   ` Joao Martins
2023-10-17 12:58                                     ` Jason Gunthorpe
2023-10-17 15:20                                       ` Joao Martins
2023-10-17 15:23                                         ` Jason Gunthorpe
2023-10-17 15:44                                           ` Joao Martins
2023-10-18 10:19                             ` Joao Martins
2023-10-18 12:03                               ` Jason Gunthorpe
2023-10-18 12:48                                 ` Joao Martins
2023-10-18 14:23                                   ` Jason Gunthorpe
2023-10-18 15:34                                     ` Joao Martins
2023-10-18 15:43                                       ` Jason Gunthorpe
2023-09-23  1:24 ` [PATCH v3 03/19] iommu: Add iommu_domain ops for dirty tracking Joao Martins
2023-10-13 16:05   ` Jason Gunthorpe
2023-10-13 16:27     ` Joao Martins
2023-09-23  1:24 ` [PATCH v3 04/19] iommufd: Add a flag to enforce dirty tracking on attach Joao Martins
2023-10-13 15:52   ` Jason Gunthorpe
2023-10-13 16:14     ` Joao Martins
2023-10-13 16:16       ` Jason Gunthorpe
2023-10-13 16:29         ` Joao Martins
2023-09-23  1:24 ` [PATCH v3 05/19] iommufd/selftest: Expand mock_domain with dev_flags Joao Martins
2023-10-13 16:02   ` Jason Gunthorpe
2023-10-13 16:21     ` Joao Martins
2023-09-23  1:24 ` [PATCH v3 06/19] iommufd/selftest: Test IOMMU_HWPT_ALLOC_ENFORCE_DIRTY Joao Martins
2023-09-23  1:24 ` [PATCH v3 07/19] iommufd: Dirty tracking data support Joao Martins
2023-09-23  1:40   ` Joao Martins
2023-10-17 12:06     ` Joao Martins
2023-10-17 15:29       ` Jason Gunthorpe
2023-10-17 15:51         ` Joao Martins
2023-10-17 16:01           ` Jason Gunthorpe
2023-10-17 16:51             ` Joao Martins
2023-10-17 17:13               ` Jason Gunthorpe
2023-10-17 17:30                 ` Joao Martins
2023-10-17 18:14                   ` Joao Martins
2023-09-23  1:25 ` [PATCH v3 08/19] iommufd: Add IOMMU_HWPT_SET_DIRTY Joao Martins
2023-10-13 16:13   ` Jason Gunthorpe
2023-09-23  1:25 ` [PATCH v3 09/19] iommufd/selftest: Test IOMMU_HWPT_SET_DIRTY Joao Martins
2023-09-23  1:25 ` [PATCH v3 10/19] iommufd: Add IOMMU_HWPT_GET_DIRTY_IOVA Joao Martins
2023-10-13 16:22   ` Jason Gunthorpe
2023-10-13 16:58     ` Joao Martins
2023-10-13 17:03       ` Jason Gunthorpe
2023-09-23  1:25 ` [PATCH v3 11/19] iommufd/selftest: Test IOMMU_HWPT_GET_DIRTY_IOVA Joao Martins
2023-09-23  1:25 ` [PATCH v3 12/19] iommufd: Add capabilities to IOMMU_GET_HW_INFO Joao Martins
2023-09-23  1:25 ` [PATCH v3 13/19] iommufd/selftest: Test out_capabilities in IOMMU_GET_HW_INFO Joao Martins
2023-09-23  1:25 ` [PATCH v3 14/19] iommufd: Add a flag to skip clearing of IOPTE dirty Joao Martins
2023-09-23  1:25 ` [PATCH v3 15/19] iommufd/selftest: Test IOMMU_GET_DIRTY_IOVA_NO_CLEAR flag Joao Martins
2023-09-23  1:25 ` [PATCH v3 16/19] iommu/amd: Add domain_alloc_user based domain allocation Joao Martins
2023-10-17  2:00   ` Suthikulpanit, Suravee
2023-10-17  9:07     ` Joao Martins
2023-10-17 13:10       ` Jason Gunthorpe
2023-10-17 14:14         ` Joao Martins
2023-10-17 14:37           ` Joao Martins
2023-10-17 15:32             ` Jason Gunthorpe
2023-10-18  8:29             ` Vasant Hegde
2023-09-23  1:25 ` [PATCH v3 17/19] iommu/amd: Access/Dirty bit support in IOPTEs Joao Martins
2023-10-04 17:01   ` Joao Martins
2023-10-17  8:18   ` Suthikulpanit, Suravee
2023-10-17  9:54     ` Joao Martins
2023-10-17 18:32       ` Joao Martins
2023-10-17 18:49         ` Jason Gunthorpe
2023-10-17 19:03           ` Joao Martins
2023-10-17 22:04             ` Joao Martins
2023-10-18 11:47               ` Suthikulpanit, Suravee
2023-10-18 20:40             ` Joao Martins
2023-10-18 11:46       ` Suthikulpanit, Suravee
2023-10-18 13:04       ` Suthikulpanit, Suravee
2023-10-18 13:17         ` Joao Martins
2023-10-18 13:31           ` Joao Martins
2023-10-18 15:50         ` Jason Gunthorpe
2023-09-23  1:25 ` [PATCH v3 18/19] iommu/amd: Print access/dirty bits if supported Joao Martins
2023-10-17  3:48   ` Suthikulpanit, Suravee
2023-10-17  9:07     ` Joao Martins
2023-10-18  8:32   ` Vasant Hegde
2023-10-18  8:53     ` Joao Martins
2023-10-18  9:03       ` Vasant Hegde
2023-10-18  9:05         ` Joao Martins
2023-10-18 15:52         ` Jason Gunthorpe
2023-10-18 15:55           ` Joao Martins
2023-09-23  1:25 ` [PATCH v3 19/19] iommu/intel: Access/Dirty bit support for SL domains Joao Martins
2023-09-25  7:01   ` Baolu Lu
2023-09-25  9:08     ` Joao Martins
2023-10-16  2:26       ` Baolu Lu
2023-10-16  0:51   ` Baolu Lu
2023-10-16 10:42     ` Joao Martins
2023-10-16 12:41       ` Baolu Lu
2023-10-16  1:37   ` Baolu Lu
2023-10-16 10:57     ` Joao Martins
2023-10-16 11:42       ` Jason Gunthorpe
2023-10-16 12:58         ` Baolu Lu
2023-10-16 12:59           ` Jason Gunthorpe
2023-10-16 13:01             ` Baolu Lu
2023-10-17 10:51               ` Joao Martins
2023-10-17 12:41                 ` Baolu Lu
2023-10-17 14:16                   ` Joao Martins
2023-10-17 14:25                     ` Joao Martins
2023-10-18  2:06                       ` Baolu Lu
2023-10-16  2:07   ` Baolu Lu
2023-10-16 11:26     ` Joao Martins
2023-10-16 16:00       ` Joao Martins
2023-10-17  2:08         ` Baolu Lu
2023-10-17 11:22           ` Joao Martins
2023-10-17 12:49             ` Baolu Lu [this message]
2023-10-17 14:19               ` Joao Martins
2023-10-17 13:10             ` Jason Gunthorpe
2023-10-17 14:11               ` Joao Martins
2023-10-17 15:31                 ` Jason Gunthorpe
2023-10-17 15:54                   ` Joao Martins
2023-10-16  2:21   ` Baolu Lu
2023-10-16 11:39     ` Joao Martins
2023-10-16 13:06       ` Baolu Lu
2023-09-26  8:58 ` [PATCH v3 00/19] IOMMUFD Dirty Tracking Shameerali Kolothum Thodi
2023-10-13 16:29 ` Jason Gunthorpe
2023-10-13 18:11   ` Joao Martins
2023-10-14  7:53     ` Baolu Lu

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=4b5cd75a-c0cd-c9bd-0d08-8c889861d48f@linux.intel.com \
    --to=baolu.lu@linux.intel.com \
    --cc=alex.williamson@redhat.com \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@nvidia.com \
    --cc=joao.m.martins@oracle.com \
    --cc=joro@8bytes.org \
    --cc=kevin.tian@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=nicolinc@nvidia.com \
    --cc=robin.murphy@arm.com \
    --cc=shameerali.kolothum.thodi@huawei.com \
    --cc=suravee.suthikulpanit@amd.com \
    --cc=will@kernel.org \
    --cc=yi.l.liu@intel.com \
    --cc=yi.y.sun@intel.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