Skip to content

Commit 2ccabf8

Browse files
nicolincjgunthorpe
authored andcommitted
iommufd: Only enforce cache coherency in iommufd_hw_pagetable_alloc
According to the conversation in the following link: https://lore.kernel.org/linux-iommu/20231020135501.GG3952@nvidia.com/ The enforce_cache_coherency should be set/enforced in the hwpt allocation routine. The iommu driver in its attach_dev() op should decide whether to reject or not a device that doesn't match with the configuration of cache coherency. Drop the enforce_cache_coherency piece in the attach/replace() and move the remaining "num_devices" piece closer to the refcount that is using it. Accordingly drop its function prototype in the header and mark it static. Also add some extra comments to clarify the expected behaviors. Link: https://lore.kernel.org/r/20231024012958.30842-1-nicolinc@nvidia.com Suggested-by: Kevin Tian <kevin.tian@intel.com> Reviewed-by: Lu Baolu <baolu.lu@linux.intel.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 0795b30 commit 2ccabf8

File tree

3 files changed

+10
-20
lines changed

3 files changed

+10
-20
lines changed

drivers/iommu/iommufd/device.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -337,13 +337,6 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
337337
goto err_unlock;
338338
}
339339

340-
/* Try to upgrade the domain we have */
341-
if (idev->enforce_cache_coherency) {
342-
rc = iommufd_hw_pagetable_enforce_cc(hwpt);
343-
if (rc)
344-
goto err_unlock;
345-
}
346-
347340
rc = iopt_table_enforce_dev_resv_regions(&hwpt->ioas->iopt, idev->dev,
348341
&idev->igroup->sw_msi_start);
349342
if (rc)
@@ -413,8 +406,8 @@ iommufd_device_do_replace(struct iommufd_device *idev,
413406
{
414407
struct iommufd_group *igroup = idev->igroup;
415408
struct iommufd_hw_pagetable *old_hwpt;
416-
unsigned int num_devices = 0;
417409
struct iommufd_device *cur;
410+
unsigned int num_devices;
418411
int rc;
419412

420413
mutex_lock(&idev->igroup->lock);
@@ -429,16 +422,6 @@ iommufd_device_do_replace(struct iommufd_device *idev,
429422
return NULL;
430423
}
431424

432-
/* Try to upgrade the domain we have */
433-
list_for_each_entry(cur, &igroup->device_list, group_item) {
434-
num_devices++;
435-
if (cur->enforce_cache_coherency) {
436-
rc = iommufd_hw_pagetable_enforce_cc(hwpt);
437-
if (rc)
438-
goto err_unlock;
439-
}
440-
}
441-
442425
old_hwpt = igroup->hwpt;
443426
if (hwpt->ioas != old_hwpt->ioas) {
444427
list_for_each_entry(cur, &igroup->device_list, group_item) {
@@ -465,6 +448,7 @@ iommufd_device_do_replace(struct iommufd_device *idev,
465448

466449
igroup->hwpt = hwpt;
467450

451+
num_devices = list_count_nodes(&igroup->device_list);
468452
/*
469453
* Move the refcounts held by the device_list to the new hwpt. Retain a
470454
* refcount for this thread as the caller will free it.

drivers/iommu/iommufd/hw_pagetable.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void iommufd_hw_pagetable_abort(struct iommufd_object *obj)
4242
iommufd_hw_pagetable_destroy(obj);
4343
}
4444

45-
int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt)
45+
static int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt)
4646
{
4747
if (hwpt->enforce_cache_coherency)
4848
return 0;
@@ -116,6 +116,13 @@ iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
116116
* doing any maps. It is an iommu driver bug to report
117117
* IOMMU_CAP_ENFORCE_CACHE_COHERENCY but fail enforce_cache_coherency on
118118
* a new domain.
119+
*
120+
* The cache coherency mode must be configured here and unchanged later.
121+
* Note that a HWPT (non-CC) created for a device (non-CC) can be later
122+
* reused by another device (either non-CC or CC). However, A HWPT (CC)
123+
* created for a device (CC) cannot be reused by another device (non-CC)
124+
* but only devices (CC). Instead user space in this case would need to
125+
* allocate a separate HWPT (non-CC).
119126
*/
120127
if (idev->enforce_cache_coherency) {
121128
rc = iommufd_hw_pagetable_enforce_cc(hwpt);

drivers/iommu/iommufd/iommufd_private.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,6 @@ struct iommufd_hw_pagetable *
266266
iommufd_hw_pagetable_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
267267
struct iommufd_device *idev, u32 flags,
268268
bool immediate_attach);
269-
int iommufd_hw_pagetable_enforce_cc(struct iommufd_hw_pagetable *hwpt);
270269
int iommufd_hw_pagetable_attach(struct iommufd_hw_pagetable *hwpt,
271270
struct iommufd_device *idev);
272271
struct iommufd_hw_pagetable *

0 commit comments

Comments
 (0)