[v5,2/4] iommufd: Add iommufd_access_replace() API
Commit Message
Extract all common procedure, between the iommufd_access_attach() API and
the new iommufd_access_replace() API, to an iommufd_access_change_pt()
helper.
Add iommufd_access_replace() function for VFIO emulated pathway to use.
Suggested-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
drivers/iommu/iommufd/device.c | 60 ++++++++++++++++++++-----
include/linux/iommufd.h | 1 +
tools/testing/selftests/iommu/iommfd*.c | 0
3 files changed, 50 insertions(+), 11 deletions(-)
create mode 100644 tools/testing/selftests/iommu/iommfd*.c
Comments
> From: Nicolin Chen <nicolinc@nvidia.com>
> Sent: Thursday, March 23, 2023 4:33 PM
>
> +int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id)
> +{
> + struct iommufd_ioas *new_ioas;
> +
> + mutex_lock(&access->ioas_lock);
> + if (!access->ioas) {
> mutex_unlock(&access->ioas_lock);
> - iommufd_put_object(obj);
> - return rc;
> + return -ENOENT;
> + }
> + if (access->ioas->obj.id == ioas_id) {
> + mutex_unlock(&access->ioas_lock);
> + return 0;
> }
> - iommufd_ref_to_users(obj);
>
> + new_ioas = iommufd_access_change_pt(access, ioas_id);
> + if (IS_ERR(new_ioas)) {
> + mutex_unlock(&access->ioas_lock);
> + return PTR_ERR(new_ioas);
> + }
> + __iommufd_access_detach(access);
> access->ioas = new_ioas;
> access->ioas_unpin = new_ioas;
Above three lines can be moved into iommufd_access_change_pt():
If (access->ioas)
__iommufd_access_detach(access);
access->ioas = new_ioas;
access->ioas_unpin = new_ioas;
Then both attach/replace can end by calling the common function.
On Fri, Mar 24, 2023 at 03:02:46AM +0000, Tian, Kevin wrote:
> External email: Use caution opening links or attachments
>
>
> > From: Nicolin Chen <nicolinc@nvidia.com>
> > Sent: Thursday, March 23, 2023 4:33 PM
> >
> > +int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id)
> > +{
> > + struct iommufd_ioas *new_ioas;
> > +
> > + mutex_lock(&access->ioas_lock);
> > + if (!access->ioas) {
> > mutex_unlock(&access->ioas_lock);
> > - iommufd_put_object(obj);
> > - return rc;
> > + return -ENOENT;
> > + }
> > + if (access->ioas->obj.id == ioas_id) {
> > + mutex_unlock(&access->ioas_lock);
> > + return 0;
> > }
> > - iommufd_ref_to_users(obj);
> >
> > + new_ioas = iommufd_access_change_pt(access, ioas_id);
> > + if (IS_ERR(new_ioas)) {
> > + mutex_unlock(&access->ioas_lock);
> > + return PTR_ERR(new_ioas);
> > + }
> > + __iommufd_access_detach(access);
> > access->ioas = new_ioas;
> > access->ioas_unpin = new_ioas;
>
> Above three lines can be moved into iommufd_access_change_pt():
>
> If (access->ioas)
> __iommufd_access_detach(access);
> access->ioas = new_ioas;
> access->ioas_unpin = new_ioas;
>
> Then both attach/replace can end by calling the common function.
OK. Will do that in v6.
Thanks
Nicolin
@@ -746,39 +746,77 @@ void iommufd_access_detach(struct iommufd_access *access)
}
EXPORT_SYMBOL_NS_GPL(iommufd_access_detach, IOMMUFD);
-int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id)
+static struct iommufd_ioas *
+iommufd_access_change_pt(struct iommufd_access *access, u32 ioas_id)
{
struct iommufd_ioas *new_ioas;
struct iommufd_object *obj;
int rc = 0;
+ lockdep_assert_held(&access->ioas_lock);
+
+ obj = iommufd_get_object(access->ictx, ioas_id, IOMMUFD_OBJ_IOAS);
+ if (IS_ERR(obj))
+ return (struct iommufd_ioas *)obj;
+ new_ioas = container_of(obj, struct iommufd_ioas, obj);
+
+ rc = iopt_add_access(&new_ioas->iopt, access);
+ if (rc) {
+ iommufd_put_object(obj);
+ return ERR_PTR(rc);
+ }
+ iommufd_ref_to_users(obj);
+ return new_ioas;
+}
+
+int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id)
+{
+ struct iommufd_ioas *new_ioas;
+
mutex_lock(&access->ioas_lock);
if (access->ioas != NULL && access->ioas->obj.id != ioas_id) {
mutex_unlock(&access->ioas_lock);
return -EINVAL;
}
- obj = iommufd_get_object(access->ictx, ioas_id, IOMMUFD_OBJ_IOAS);
- if (IS_ERR(obj)) {
+ new_ioas = iommufd_access_change_pt(access, ioas_id);
+ if (IS_ERR(new_ioas)) {
mutex_unlock(&access->ioas_lock);
- return PTR_ERR(obj);
+ return PTR_ERR(new_ioas);
}
- new_ioas = container_of(obj, struct iommufd_ioas, obj);
+ access->ioas = new_ioas;
+ access->ioas_unpin = new_ioas;
+ mutex_unlock(&access->ioas_lock);
+ return 0;
+}
+EXPORT_SYMBOL_NS_GPL(iommufd_access_attach, IOMMUFD);
- rc = iopt_add_access(&new_ioas->iopt, access);
- if (rc) {
+int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id)
+{
+ struct iommufd_ioas *new_ioas;
+
+ mutex_lock(&access->ioas_lock);
+ if (!access->ioas) {
mutex_unlock(&access->ioas_lock);
- iommufd_put_object(obj);
- return rc;
+ return -ENOENT;
+ }
+ if (access->ioas->obj.id == ioas_id) {
+ mutex_unlock(&access->ioas_lock);
+ return 0;
}
- iommufd_ref_to_users(obj);
+ new_ioas = iommufd_access_change_pt(access, ioas_id);
+ if (IS_ERR(new_ioas)) {
+ mutex_unlock(&access->ioas_lock);
+ return PTR_ERR(new_ioas);
+ }
+ __iommufd_access_detach(access);
access->ioas = new_ioas;
access->ioas_unpin = new_ioas;
mutex_unlock(&access->ioas_lock);
return 0;
}
-EXPORT_SYMBOL_NS_GPL(iommufd_access_attach, IOMMUFD);
+EXPORT_SYMBOL_NS_GPL(iommufd_access_replace, IOMMUFD);
/**
* iommufd_access_notify_unmap - Notify users of an iopt to stop using it
@@ -46,6 +46,7 @@ iommufd_access_create(struct iommufd_ctx *ictx,
const struct iommufd_access_ops *ops, void *data, u32 *id);
void iommufd_access_destroy(struct iommufd_access *access);
int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id);
+int iommufd_access_replace(struct iommufd_access *access, u32 ioas_id);
void iommufd_access_detach(struct iommufd_access *access);
void iommufd_ctx_get(struct iommufd_ctx *ictx);
new file mode 100644