Skip to content

Commit d56d1e8

Browse files
nicolincjgunthorpe
authored andcommitted
iommufd: Verify object in iommufd_object_finalize/abort()
To support driver-allocated vIOMMU objects, it's required for IOMMU driver to call the provided iommufd_viommu_alloc helper to embed the core struct. However, there is no guarantee that every driver will call it and allocate objects properly. Make the iommufd_object_finalize/abort functions more robust to verify if the xarray slot indexed by the input obj->id is having an XA_ZERO_ENTRY, which is the reserved value stored by xa_alloc via iommufd_object_alloc. Link: https://patch.msgid.link/r/334bd4dde8e0a88eb30fa67eeef61827cdb546f9.1730836219.git.nicolinc@nvidia.com Suggested-by: Jason Gunthorpe <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Signed-off-by: Nicolin Chen <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 6b22d56 commit d56d1e8

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

drivers/iommu/iommufd/main.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,26 @@ static struct miscdevice vfio_misc_dev;
4141
void iommufd_object_finalize(struct iommufd_ctx *ictx,
4242
struct iommufd_object *obj)
4343
{
44+
XA_STATE(xas, &ictx->objects, obj->id);
4445
void *old;
4546

46-
old = xa_store(&ictx->objects, obj->id, obj, GFP_KERNEL);
47-
/* obj->id was returned from xa_alloc() so the xa_store() cannot fail */
48-
WARN_ON(old);
47+
xa_lock(&ictx->objects);
48+
old = xas_store(&xas, obj);
49+
xa_unlock(&ictx->objects);
50+
/* obj->id was returned from xa_alloc() so the xas_store() cannot fail */
51+
WARN_ON(old != XA_ZERO_ENTRY);
4952
}
5053

5154
/* Undo _iommufd_object_alloc() if iommufd_object_finalize() was not called */
5255
void iommufd_object_abort(struct iommufd_ctx *ictx, struct iommufd_object *obj)
5356
{
57+
XA_STATE(xas, &ictx->objects, obj->id);
5458
void *old;
5559

56-
old = xa_erase(&ictx->objects, obj->id);
57-
WARN_ON(old);
60+
xa_lock(&ictx->objects);
61+
old = xas_store(&xas, NULL);
62+
xa_unlock(&ictx->objects);
63+
WARN_ON(old != XA_ZERO_ENTRY);
5864
kfree(obj);
5965
}
6066

0 commit comments

Comments
 (0)