From: Yan Zhao <yan.y.zhao@intel.com>
To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: alex.williamson@redhat.com, kevin.tian@intel.com, jgg@nvidia.com,
yishaih@nvidia.com, shameerali.kolothum.thodi@huawei.com,
Yan Zhao <yan.y.zhao@intel.com>
Subject: [PATCH] vfio/pci: take mmap write lock for io_remap_pfn_range
Date: Mon, 8 May 2023 20:58:42 +0800 [thread overview]
Message-ID: <20230508125842.28193-1-yan.y.zhao@intel.com> (raw)
In VFIO type1, vaddr_get_pfns() will try fault in MMIO PFNs after
pin_user_pages_remote() returns -EFAULT.
follow_fault_pfn
fixup_user_fault
handle_mm_fault
handle_mm_fault
do_fault
do_shared_fault
do_fault
__do_fault
vfio_pci_mmap_fault
io_remap_pfn_range
remap_pfn_range
track_pfn_remap
vm_flags_set ==> mmap_assert_write_locked(vma->vm_mm)
remap_pfn_range_notrack
vm_flags_set ==> mmap_assert_write_locked(vma->vm_mm)
As io_remap_pfn_range() will call vm_flags_set() to update vm_flags [1],
holding of mmap write lock is required.
So, update vfio_pci_mmap_fault() to drop mmap read lock and take mmap
write lock.
[1] https://lkml.kernel.org/r/20230126193752.297968-3-surenb@google.com
commit bc292ab00f6c ("mm: introduce vma->vm_flags wrapper functions")
commit 1c71222e5f23
("mm: replace vma->vm_flags direct modifications with modifier calls")
Signed-off-by: Yan Zhao <yan.y.zhao@intel.com>
---
drivers/vfio/pci/vfio_pci_core.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index a5ab416cf476..5082f89152b3 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -1687,6 +1687,12 @@ static vm_fault_t vfio_pci_mmap_fault(struct vm_fault *vmf)
struct vfio_pci_mmap_vma *mmap_vma;
vm_fault_t ret = VM_FAULT_NOPAGE;
+ mmap_assert_locked(vma->vm_mm);
+ mmap_read_unlock(vma->vm_mm);
+
+ if (mmap_write_lock_killable(vma->vm_mm))
+ return VM_FAULT_RETRY;
+
mutex_lock(&vdev->vma_lock);
down_read(&vdev->memory_lock);
@@ -1726,6 +1732,17 @@ static vm_fault_t vfio_pci_mmap_fault(struct vm_fault *vmf)
up_out:
up_read(&vdev->memory_lock);
mutex_unlock(&vdev->vma_lock);
+ mmap_write_unlock(vma->vm_mm);
+
+ /* If PTE is installed successfully, add the completed flag to
+ * indicate mmap lock is released,
+ * otherwise retake the read lock
+ */
+ if (ret == VM_FAULT_NOPAGE)
+ ret |= VM_FAULT_COMPLETED;
+ else
+ mmap_read_lock(vma->vm_mm);
+
return ret;
}
base-commit: 705b004ee377b789e39ae237519bab714297ac83
--
2.17.1
next reply other threads:[~2023-05-08 13:23 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-05-08 12:58 Yan Zhao [this message]
2023-05-08 16:48 ` Jason Gunthorpe
2023-05-08 20:57 ` Alex Williamson
2023-05-10 20:41 ` Jason Gunthorpe
2023-05-11 6:56 ` Yan Zhao
2023-05-11 7:38 ` Cédric Le Goater
2023-05-11 7:32 ` Cédric Le Goater
2023-05-11 16:07 ` Alex Williamson
2023-05-11 17:47 ` Jason Gunthorpe
2023-05-12 8:02 ` Yan Zhao
2024-05-22 16:56 ` Andrew Jones
2024-05-22 17:50 ` Alex Williamson
2024-05-22 18:30 ` Jason Gunthorpe
2024-05-22 19:43 ` Alex Williamson
2024-05-22 21:21 ` 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=20230508125842.28193-1-yan.y.zhao@intel.com \
--to=yan.y.zhao@intel.com \
--cc=alex.williamson@redhat.com \
--cc=jgg@nvidia.com \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=shameerali.kolothum.thodi@huawei.com \
--cc=yishaih@nvidia.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®