Commit 5ce995f3 authored by Jason Wang's avatar Jason Wang Committed by Michael S. Tsirkin

vhost: use mmgrab() instead of mmget() for non worker device

For the device that doesn't use vhost worker and use_mm(), mmget() is
too heavy weight and it may brings troubles for implementing mmap()
support for vDPA device.

This is because, an reference to the address space was held via
mm_get() in vhost_dev_set_owner() and an reference to the file was
held in mmap(). This means when process exits, the mm can not be
released thus we can not release the file.

This patch tries to use mmgrab() instead of mmget(), which allows the
address space to be destroy in process exit without releasing the mm
structure itself. This is sufficient for vDPA device which pin user
pages and does not depend on the address space to work.
Signed-off-by: default avatarJason Wang <jasowang@redhat.com>
Link: https://lore.kernel.org/r/20200529080303.15449-3-jasowang@redhat.comSigned-off-by: default avatarMichael S. Tsirkin <mst@redhat.com>
parent 01fcb1cb
...@@ -541,6 +541,36 @@ bool vhost_dev_has_owner(struct vhost_dev *dev) ...@@ -541,6 +541,36 @@ bool vhost_dev_has_owner(struct vhost_dev *dev)
} }
EXPORT_SYMBOL_GPL(vhost_dev_has_owner); EXPORT_SYMBOL_GPL(vhost_dev_has_owner);
static void vhost_attach_mm(struct vhost_dev *dev)
{
/* No owner, become one */
if (dev->use_worker) {
dev->mm = get_task_mm(current);
} else {
/* vDPA device does not use worker thead, so there's
* no need to hold the address space for mm. This help
* to avoid deadlock in the case of mmap() which may
* held the refcnt of the file and depends on release
* method to remove vma.
*/
dev->mm = current->mm;
mmgrab(dev->mm);
}
}
static void vhost_detach_mm(struct vhost_dev *dev)
{
if (!dev->mm)
return;
if (dev->use_worker)
mmput(dev->mm);
else
mmdrop(dev->mm);
dev->mm = NULL;
}
/* Caller should have device mutex */ /* Caller should have device mutex */
long vhost_dev_set_owner(struct vhost_dev *dev) long vhost_dev_set_owner(struct vhost_dev *dev)
{ {
...@@ -553,8 +583,8 @@ long vhost_dev_set_owner(struct vhost_dev *dev) ...@@ -553,8 +583,8 @@ long vhost_dev_set_owner(struct vhost_dev *dev)
goto err_mm; goto err_mm;
} }
/* No owner, become one */ vhost_attach_mm(dev);
dev->mm = get_task_mm(current);
dev->kcov_handle = kcov_common_handle(); dev->kcov_handle = kcov_common_handle();
if (dev->use_worker) { if (dev->use_worker) {
worker = kthread_create(vhost_worker, dev, worker = kthread_create(vhost_worker, dev,
...@@ -583,9 +613,7 @@ long vhost_dev_set_owner(struct vhost_dev *dev) ...@@ -583,9 +613,7 @@ long vhost_dev_set_owner(struct vhost_dev *dev)
dev->worker = NULL; dev->worker = NULL;
} }
err_worker: err_worker:
if (dev->mm) vhost_detach_mm(dev);
mmput(dev->mm);
dev->mm = NULL;
dev->kcov_handle = 0; dev->kcov_handle = 0;
err_mm: err_mm:
return err; return err;
...@@ -682,9 +710,7 @@ void vhost_dev_cleanup(struct vhost_dev *dev) ...@@ -682,9 +710,7 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
dev->worker = NULL; dev->worker = NULL;
dev->kcov_handle = 0; dev->kcov_handle = 0;
} }
if (dev->mm) vhost_detach_mm(dev);
mmput(dev->mm);
dev->mm = NULL;
} }
EXPORT_SYMBOL_GPL(vhost_dev_cleanup); EXPORT_SYMBOL_GPL(vhost_dev_cleanup);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment