All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alex Williamson <alex.williamson@redhat.com>
To: alex.williamson@redhat.com
Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
	jgg@nvidia.com, peterx@redhat.com
Subject: [PATCH 6/7] vfio: Add vfio_device_io_remap_mapping_range()
Date: Thu, 05 Aug 2021 11:08:12 -0600	[thread overview]
Message-ID: <162818329235.1511194.15804833796430403640.stgit@omen> (raw)
In-Reply-To: <162818167535.1511194.6614962507750594786.stgit@omen>

This provides a mirror of vfio_device_unmap_mapping_range() for
vmas mapping device memory where the pfn is provided by
vfio_device_vma_to_pfn().

Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---
 drivers/vfio/vfio.c  |   44 ++++++++++++++++++++++++++++++++++++++++++++
 include/linux/vfio.h |    2 ++
 2 files changed, 46 insertions(+)

diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index 42ca93be152a..c5b3a3446dd9 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -33,6 +33,7 @@
 #include <linux/uaccess.h>
 #include <linux/vfio.h>
 #include <linux/wait.h>
+#include <linux/sched/mm.h>
 #include <linux/sched/signal.h>
 
 #define DRIVER_VERSION	"0.3"
@@ -567,6 +568,49 @@ void vfio_device_unmap_mapping_range(struct vfio_device *device,
 }
 EXPORT_SYMBOL_GPL(vfio_device_unmap_mapping_range);
 
+int vfio_device_io_remap_mapping_range(struct vfio_device *device,
+				       loff_t start, loff_t len)
+{
+	struct address_space *mapping = device->inode->i_mapping;
+	int ret = 0;
+
+	i_mmap_lock_write(mapping);
+	if (mapping_mapped(mapping)) {
+		struct rb_root_cached *root = &mapping->i_mmap;
+		pgoff_t pgstart = start >> PAGE_SHIFT;
+		pgoff_t pgend = (start + len - 1) >> PAGE_SHIFT;
+		struct vm_area_struct *vma;
+
+		vma_interval_tree_foreach(vma, root, pgstart, pgend) {
+			unsigned long pfn;
+			unsigned int flags;
+
+			ret = vfio_device_vma_to_pfn(device, vma, &pfn);
+			if (ret)
+				break;
+
+			/*
+			 * Force NOFS memory allocation context to avoid
+			 * deadlock while we hold i_mmap_rwsem.
+			 */
+			flags = memalloc_nofs_save();
+			ret = io_remap_pfn_range(vma, vma->vm_start, pfn,
+						 vma->vm_end - vma->vm_start,
+						 vma->vm_page_prot);
+			memalloc_nofs_restore(flags);
+			if (ret)
+				break;
+		}
+	}
+	i_mmap_unlock_write(mapping);
+
+	if (ret)
+		vfio_device_unmap_mapping_range(device, start, len);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(vfio_device_io_remap_mapping_range);
+
 /**
  * Device objects - create, release, get, put, search
  */
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 5f07ebe0f85d..c2c51c7a6f05 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -71,6 +71,8 @@ extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
 extern void vfio_device_put(struct vfio_device *device);
 extern void vfio_device_unmap_mapping_range(struct vfio_device *device,
 					    loff_t start, loff_t len);
+extern int vfio_device_io_remap_mapping_range(struct vfio_device *device,
+					      loff_t start, loff_t len);
 extern int vfio_device_vma_to_pfn(struct vfio_device *device,
 				  struct vm_area_struct *vma,
 				  unsigned long *pfn);



  parent reply	other threads:[~2021-08-05 17:08 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-05 17:06 [PATCH 0/7] vfio: device fd address space and vfio-pci mmap invalidation cleanup Alex Williamson
2021-08-05 17:07 ` [PATCH 1/7] vfio: Create vfio_fs_type with inode per device Alex Williamson
2021-08-10  8:43   ` Christoph Hellwig
2021-08-10 14:52     ` Alex Williamson
2021-08-10 14:57       ` Christoph Hellwig
2021-08-10 18:49         ` Peter Xu
2021-08-10 21:16           ` Alex Williamson
2021-08-10 22:18             ` Peter Xu
2021-08-05 17:07 ` [PATCH 2/7] vfio: Export unmap_mapping_range() wrapper Alex Williamson
2021-08-10  8:45   ` Christoph Hellwig
2021-08-10 18:56   ` Peter Xu
2021-08-05 17:07 ` [PATCH 3/7] vfio/pci: Use vfio_device_unmap_mapping_range() Alex Williamson
2021-08-06  1:04   ` Jason Gunthorpe
2021-08-06 20:17     ` Alex Williamson
2021-08-10  8:51       ` Christoph Hellwig
2021-08-10 11:57         ` Jason Gunthorpe
2021-08-10 12:55           ` Christoph Hellwig
2021-08-10 21:50             ` Alex Williamson
2021-08-11 11:57               ` Jason Gunthorpe
2021-08-10  8:53   ` Christoph Hellwig
2021-08-10 19:02     ` Peter Xu
2021-08-10 20:51       ` Alex Williamson
2021-08-10 18:48   ` Peter Xu
2021-08-10 19:59     ` Alex Williamson
2021-08-10 20:20       ` Peter Xu
2021-08-05 17:07 ` [PATCH 4/7] vfio,vfio-pci: Add vma to pfn callback Alex Williamson
2021-08-06  1:01   ` Jason Gunthorpe
2021-08-10  9:00     ` Christoph Hellwig
2021-08-10  9:00   ` Christoph Hellwig
2021-08-05 17:08 ` [PATCH 5/7] mm/interval_tree.c: Export vma interval tree iterators Alex Williamson
2021-08-05 17:08 ` Alex Williamson [this message]
2021-08-10  9:04   ` [PATCH 6/7] vfio: Add vfio_device_io_remap_mapping_range() Christoph Hellwig
2021-08-05 17:08 ` [PATCH 7/7] vfio/pci: Remove map-on-fault behavior Alex Williamson
2021-08-10  9:11   ` Christoph Hellwig
2021-08-10 15:04     ` Alex Williamson
2021-08-10 20:54   ` Peter Xu
2021-08-10 21:45     ` Alex Williamson
2021-08-10 22:27       ` Peter Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=162818329235.1511194.15804833796430403640.stgit@omen \
    --to=alex.williamson@redhat.com \
    --cc=jgg@nvidia.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterx@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.