Skip to content

Commit b5021cb

Browse files
nicolincjgunthorpe
authored andcommitted
iommufd: Share iommufd_hwpt_alloc with IOMMUFD_OBJ_HWPT_NESTED
Allow iommufd_hwpt_alloc() to have a common routine but jump to different allocators corresponding to different user input pt_obj types, either an IOMMUFD_OBJ_IOAS for a PAGING hwpt or an IOMMUFD_OBJ_HWPT_PAGING as the parent for a NESTED hwpt. Also, move the "flags" validation to the hwpt allocator (paging), so that later the hwpt_nested allocator can do its own separate flags validation. Link: https://lore.kernel.org/r/20231026043938.63898-6-yi.l.liu@intel.com Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> Signed-off-by: Yi Liu <yi.l.liu@intel.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Reviewed-by: Jason Gunthorpe <jgg@nvidia.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent 89db316 commit b5021cb

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

drivers/iommu/iommufd/hw_pagetable.c

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
8282
struct iommufd_device *idev, u32 flags,
8383
bool immediate_attach)
8484
{
85+
const u32 valid_flags = IOMMU_HWPT_ALLOC_NEST_PARENT |
86+
IOMMU_HWPT_ALLOC_DIRTY_TRACKING;
8587
const struct iommu_ops *ops = dev_iommu_ops(idev->dev);
8688
struct iommufd_hwpt_paging *hwpt_paging;
8789
struct iommufd_hw_pagetable *hwpt;
@@ -91,6 +93,8 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
9193

9294
if (flags && !ops->domain_alloc_user)
9395
return ERR_PTR(-EOPNOTSUPP);
96+
if (flags & ~valid_flags)
97+
return ERR_PTR(-EOPNOTSUPP);
9498

9599
hwpt_paging = __iommufd_object_alloc(
96100
ictx, hwpt_paging, IOMMUFD_OBJ_HWPT_PAGING, common.obj);
@@ -167,35 +171,41 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, struct iommufd_ioas *ioas,
167171
int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd)
168172
{
169173
struct iommu_hwpt_alloc *cmd = ucmd->cmd;
170-
struct iommufd_hwpt_paging *hwpt_paging;
171174
struct iommufd_hw_pagetable *hwpt;
175+
struct iommufd_ioas *ioas = NULL;
176+
struct iommufd_object *pt_obj;
172177
struct iommufd_device *idev;
173-
struct iommufd_ioas *ioas;
174178
int rc;
175179

176-
if ((cmd->flags & ~(IOMMU_HWPT_ALLOC_NEST_PARENT |
177-
IOMMU_HWPT_ALLOC_DIRTY_TRACKING)) ||
178-
cmd->__reserved)
180+
if (cmd->__reserved)
179181
return -EOPNOTSUPP;
180182

181183
idev = iommufd_get_device(ucmd, cmd->dev_id);
182184
if (IS_ERR(idev))
183185
return PTR_ERR(idev);
184186

185-
ioas = iommufd_get_ioas(ucmd->ictx, cmd->pt_id);
186-
if (IS_ERR(ioas)) {
187-
rc = PTR_ERR(ioas);
187+
pt_obj = iommufd_get_object(ucmd->ictx, cmd->pt_id, IOMMUFD_OBJ_ANY);
188+
if (IS_ERR(pt_obj)) {
189+
rc = -EINVAL;
188190
goto out_put_idev;
189191
}
190192

191-
mutex_lock(&ioas->mutex);
192-
hwpt_paging = iommufd_hwpt_paging_alloc(ucmd->ictx, ioas, idev,
193-
cmd->flags, false);
194-
if (IS_ERR(hwpt_paging)) {
195-
rc = PTR_ERR(hwpt_paging);
196-
goto out_unlock;
193+
if (pt_obj->type == IOMMUFD_OBJ_IOAS) {
194+
struct iommufd_hwpt_paging *hwpt_paging;
195+
196+
ioas = container_of(pt_obj, struct iommufd_ioas, obj);
197+
mutex_lock(&ioas->mutex);
198+
hwpt_paging = iommufd_hwpt_paging_alloc(ucmd->ictx, ioas, idev,
199+
cmd->flags, false);
200+
if (IS_ERR(hwpt_paging)) {
201+
rc = PTR_ERR(hwpt_paging);
202+
goto out_unlock;
203+
}
204+
hwpt = &hwpt_paging->common;
205+
} else {
206+
rc = -EINVAL;
207+
goto out_put_pt;
197208
}
198-
hwpt = &hwpt_paging->common;
199209

200210
cmd->out_hwpt_id = hwpt->obj.id;
201211
rc = iommufd_ucmd_respond(ucmd, sizeof(*cmd));
@@ -207,8 +217,10 @@ int iommufd_hwpt_alloc(struct iommufd_ucmd *ucmd)
207217
out_hwpt:
208218
iommufd_object_abort_and_destroy(ucmd->ictx, &hwpt->obj);
209219
out_unlock:
210-
mutex_unlock(&ioas->mutex);
211-
iommufd_put_object(&ioas->obj);
220+
if (ioas)
221+
mutex_unlock(&ioas->mutex);
222+
out_put_pt:
223+
iommufd_put_object(pt_obj);
212224
out_put_idev:
213225
iommufd_put_object(&idev->obj);
214226
return rc;

0 commit comments

Comments
 (0)