Skip to content

Commit

Permalink
iommufd/device: Add iommufd_access_detach() API
Browse files Browse the repository at this point in the history
Previously, the detach routine is only done by the destroy(). And it was
called by vfio_iommufd_emulated_unbind() when the device runs close(), so
all the mappings in iopt were cleaned in that setup, when the call trace
reaches this detach() routine.

Now, there's a need of a detach uAPI, meaning that it does not only need
a new iommufd_access_detach() API, but also requires access->ops->unmap()
call as a cleanup. So add one.

However, leaving that unprotected can introduce some potential of a race
condition during the pin_/unpin_pages() call, where access->ioas->iopt is
getting referenced. So, add an ioas_lock to protect the context of iopt
referencings.

Also, to allow the iommufd_access_unpin_pages() callback to happen via
this unmap() call, add an ioas_unpin pointer, so the unpin routine won't
be affected by the "access->ioas = NULL" trick.

Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
  • Loading branch information
nicolinc committed Mar 15, 2023
1 parent abca7e1 commit 4110522
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
67 changes: 64 additions & 3 deletions drivers/iommu/iommufd/device.c
Expand Up @@ -702,6 +702,38 @@ void iommufd_access_destroy(struct iommufd_access *access)
}
EXPORT_SYMBOL_NS_GPL(iommufd_access_destroy, IOMMUFD);

static void __iommufd_access_detach(struct iommufd_access *access)
{
struct iommufd_ioas *cur_ioas = access->ioas;

lockdep_assert_held(&access->ioas_lock);
/*
* Set ioas to NULL to block any further iommufd_access_pin_pages().
* iommufd_access_unpin_pages() can continue using access->ioas_unpin.
*/
access->ioas = NULL;

if (access->ops->unmap) {
mutex_unlock(&access->ioas_lock);
access->ops->unmap(access->data, 0, ULONG_MAX);
mutex_lock(&access->ioas_lock);
}
iopt_remove_access(&cur_ioas->iopt, access);
refcount_dec(&cur_ioas->obj.users);
}

void iommufd_access_detach(struct iommufd_access *access)
{
mutex_lock(&access->ioas_lock);
if (WARN_ON(!access->ioas))
goto out;
__iommufd_access_detach(access);
out:
access->ioas_unpin = NULL;
mutex_unlock(&access->ioas_lock);
}
EXPORT_SYMBOL_NS_GPL(iommufd_access_detach, IOMMUFD);

int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id)
{
struct iommufd_ioas *new_ioas;
Expand All @@ -713,14 +745,18 @@ int iommufd_access_attach(struct iommufd_access *access, u32 ioas_id)
return PTR_ERR(obj);
new_ioas = container_of(obj, struct iommufd_ioas, obj);

mutex_lock(&access->ioas_lock);
rc = iopt_add_access(&new_ioas->iopt, access);
if (rc) {
mutex_unlock(&access->ioas_lock);
iommufd_put_object(obj);
return rc;
}
iommufd_ref_to_users(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);
Expand Down Expand Up @@ -775,15 +811,22 @@ void iommufd_access_notify_unmap(struct io_pagetable *iopt, unsigned long iova,
void iommufd_access_unpin_pages(struct iommufd_access *access,
unsigned long iova, unsigned long length)
{
struct io_pagetable *iopt = &access->ioas->iopt;
struct iopt_area_contig_iter iter;
struct io_pagetable *iopt;
unsigned long last_iova;
struct iopt_area *area;

if (WARN_ON(!length) ||
WARN_ON(check_add_overflow(iova, length - 1, &last_iova)))
return;

mutex_lock(&access->ioas_lock);
if (!access->ioas_unpin) {
mutex_unlock(&access->ioas_lock);
return;
}
iopt = &access->ioas_unpin->iopt;

down_read(&iopt->iova_rwsem);
iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova)
iopt_area_remove_access(
Expand All @@ -793,6 +836,7 @@ void iommufd_access_unpin_pages(struct iommufd_access *access,
min(last_iova, iopt_area_last_iova(area))));
up_read(&iopt->iova_rwsem);
WARN_ON(!iopt_area_contig_done(&iter));
mutex_unlock(&access->ioas_lock);
}
EXPORT_SYMBOL_NS_GPL(iommufd_access_unpin_pages, IOMMUFD);

Expand Down Expand Up @@ -838,8 +882,8 @@ int iommufd_access_pin_pages(struct iommufd_access *access, unsigned long iova,
unsigned long length, struct page **out_pages,
unsigned int flags)
{
struct io_pagetable *iopt = &access->ioas->iopt;
struct iopt_area_contig_iter iter;
struct io_pagetable *iopt;
unsigned long last_iova;
struct iopt_area *area;
int rc;
Expand All @@ -854,6 +898,13 @@ int iommufd_access_pin_pages(struct iommufd_access *access, unsigned long iova,
if (check_add_overflow(iova, length - 1, &last_iova))
return -EOVERFLOW;

mutex_lock(&access->ioas_lock);
if (!access->ioas) {
mutex_unlock(&access->ioas_lock);
return -ENOENT;
}
iopt = &access->ioas->iopt;

down_read(&iopt->iova_rwsem);
iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova) {
unsigned long last = min(last_iova, iopt_area_last_iova(area));
Expand Down Expand Up @@ -884,6 +935,7 @@ int iommufd_access_pin_pages(struct iommufd_access *access, unsigned long iova,
}

up_read(&iopt->iova_rwsem);
mutex_unlock(&access->ioas_lock);
return 0;

err_remove:
Expand All @@ -898,6 +950,7 @@ int iommufd_access_pin_pages(struct iommufd_access *access, unsigned long iova,
iopt_area_last_iova(area))));
}
up_read(&iopt->iova_rwsem);
mutex_unlock(&access->ioas_lock);
return rc;
}
EXPORT_SYMBOL_NS_GPL(iommufd_access_pin_pages, IOMMUFD);
Expand All @@ -917,8 +970,8 @@ EXPORT_SYMBOL_NS_GPL(iommufd_access_pin_pages, IOMMUFD);
int iommufd_access_rw(struct iommufd_access *access, unsigned long iova,
void *data, size_t length, unsigned int flags)
{
struct io_pagetable *iopt = &access->ioas->iopt;
struct iopt_area_contig_iter iter;
struct io_pagetable *iopt;
struct iopt_area *area;
unsigned long last_iova;
int rc;
Expand All @@ -928,6 +981,13 @@ int iommufd_access_rw(struct iommufd_access *access, unsigned long iova,
if (check_add_overflow(iova, length - 1, &last_iova))
return -EOVERFLOW;

mutex_lock(&access->ioas_lock);
if (!access->ioas) {
mutex_unlock(&access->ioas_lock);
return -ENOENT;
}
iopt = &access->ioas->iopt;

down_read(&iopt->iova_rwsem);
iopt_for_each_contig_area(&iter, area, iopt, iova, last_iova) {
unsigned long last = min(last_iova, iopt_area_last_iova(area));
Expand All @@ -954,6 +1014,7 @@ int iommufd_access_rw(struct iommufd_access *access, unsigned long iova,
rc = -ENOENT;
err_out:
up_read(&iopt->iova_rwsem);
mutex_unlock(&access->ioas_lock);
return rc;
}
EXPORT_SYMBOL_NS_GPL(iommufd_access_rw, IOMMUFD);
2 changes: 2 additions & 0 deletions drivers/iommu/iommufd/iommufd_private.h
Expand Up @@ -312,6 +312,8 @@ struct iommufd_access {
struct iommufd_object obj;
struct iommufd_ctx *ictx;
struct iommufd_ioas *ioas;
struct iommufd_ioas *ioas_unpin;
struct mutex ioas_lock;
const struct iommufd_access_ops *ops;
void *data;
unsigned long iova_alignment;
Expand Down
1 change: 1 addition & 0 deletions include/linux/iommufd.h
Expand Up @@ -48,6 +48,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);
void iommufd_access_detach(struct iommufd_access *access);

void iommufd_ctx_get(struct iommufd_ctx *ictx);

Expand Down

0 comments on commit 4110522

Please sign in to comment.