All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
	Peter Xu <peterx@redhat.com>,
	QEMU Stable <qemu-stable@nongnu.org>,
	Jason Wang <jasowang@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Richard Henderson <rth@twiddle.net>,
	Eduardo Habkost <ehabkost@redhat.com>,
	Marcel Apfelbaum <marcel.apfelbaum@gmail.com>
Subject: [Qemu-devel] [PULL 20/28] intel-iommu: send PSI always even if across PDEs
Date: Wed, 23 May 2018 17:43:16 +0300	[thread overview]
Message-ID: <1527086545-68024-21-git-send-email-mst@redhat.com> (raw)
In-Reply-To: <1527086545-68024-1-git-send-email-mst@redhat.com>

From: Peter Xu <peterx@redhat.com>

SECURITY IMPLICATION: without this patch, any guest with both assigned
device and a vIOMMU might encounter stale IO page mappings even if guest
has already unmapped the page, which may lead to guest memory
corruption.  The stale mappings will only be limited to the guest's own
memory range, so it should not affect the host memory or other guests on
the host.

During IOVA page table walking, there is a special case when the PSI
covers one whole PDE (Page Directory Entry, which contains 512 Page
Table Entries) or more.  In the past, we skip that entry and we don't
notify the IOMMU notifiers.  This is not correct.  We should send UNMAP
notification to registered UNMAP notifiers in this case.

For UNMAP only notifiers, this might cause IOTLBs cached in the devices
even if they were already invalid.  For MAP/UNMAP notifiers like
vfio-pci, this will cause stale page mappings.

This special case doesn't trigger often, but it is very easy to be
triggered by nested device assignments, since in that case we'll
possibly map the whole L2 guest RAM region into the device's IOVA
address space (several GBs at least), which is far bigger than normal
kernel driver usages of the device (tens of MBs normally).

Without this patch applied to L1 QEMU, nested device assignment to L2
guests will dump some errors like:

qemu-system-x86_64: VFIO_MAP_DMA: -17
qemu-system-x86_64: vfio_dma_map(0x557305420c30, 0xad000, 0x1000,
                    0x7f89a920d000) = -17 (File exists)

CC: QEMU Stable <qemu-stable@nongnu.org>
Acked-by: Jason Wang <jasowang@redhat.com>
[peterx: rewrite the commit message]
Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 hw/i386/intel_iommu.c | 42 ++++++++++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 12 deletions(-)

diff --git a/hw/i386/intel_iommu.c b/hw/i386/intel_iommu.c
index fb31de9..b359efd 100644
--- a/hw/i386/intel_iommu.c
+++ b/hw/i386/intel_iommu.c
@@ -722,6 +722,15 @@ static int vtd_iova_to_slpte(VTDContextEntry *ce, uint64_t iova, bool is_write,
 
 typedef int (*vtd_page_walk_hook)(IOMMUTLBEntry *entry, void *private);
 
+static int vtd_page_walk_one(IOMMUTLBEntry *entry, int level,
+                             vtd_page_walk_hook hook_fn, void *private)
+{
+    assert(hook_fn);
+    trace_vtd_page_walk_one(level, entry->iova, entry->translated_addr,
+                            entry->addr_mask, entry->perm);
+    return hook_fn(entry, private);
+}
+
 /**
  * vtd_page_walk_level - walk over specific level for IOVA range
  *
@@ -781,28 +790,37 @@ static int vtd_page_walk_level(dma_addr_t addr, uint64_t start,
          */
         entry_valid = read_cur | write_cur;
 
+        entry.target_as = &address_space_memory;
+        entry.iova = iova & subpage_mask;
+        entry.perm = IOMMU_ACCESS_FLAG(read_cur, write_cur);
+        entry.addr_mask = ~subpage_mask;
+
         if (vtd_is_last_slpte(slpte, level)) {
-            entry.target_as = &address_space_memory;
-            entry.iova = iova & subpage_mask;
             /* NOTE: this is only meaningful if entry_valid == true */
             entry.translated_addr = vtd_get_slpte_addr(slpte, aw);
-            entry.addr_mask = ~subpage_mask;
-            entry.perm = IOMMU_ACCESS_FLAG(read_cur, write_cur);
             if (!entry_valid && !notify_unmap) {
                 trace_vtd_page_walk_skip_perm(iova, iova_next);
                 goto next;
             }
-            trace_vtd_page_walk_one(level, entry.iova, entry.translated_addr,
-                                    entry.addr_mask, entry.perm);
-            if (hook_fn) {
-                ret = hook_fn(&entry, private);
-                if (ret < 0) {
-                    return ret;
-                }
+            ret = vtd_page_walk_one(&entry, level, hook_fn, private);
+            if (ret < 0) {
+                return ret;
             }
         } else {
             if (!entry_valid) {
-                trace_vtd_page_walk_skip_perm(iova, iova_next);
+                if (notify_unmap) {
+                    /*
+                     * The whole entry is invalid; unmap it all.
+                     * Translated address is meaningless, zero it.
+                     */
+                    entry.translated_addr = 0x0;
+                    ret = vtd_page_walk_one(&entry, level, hook_fn, private);
+                    if (ret < 0) {
+                        return ret;
+                    }
+                } else {
+                    trace_vtd_page_walk_skip_perm(iova, iova_next);
+                }
                 goto next;
             }
             ret = vtd_page_walk_level(vtd_get_slpte_addr(slpte, aw), iova,
-- 
MST

  parent reply	other threads:[~2018-05-23 14:43 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-23 14:42 [Qemu-devel] [PULL 00/28] pc, pci, virtio, vhost: fixes, features Michael S. Tsirkin
2018-05-23 14:42 ` [Qemu-devel] [PULL 01/28] hw/pci-host/q35: Replace hardcoded value with macro Michael S. Tsirkin
2018-05-23 14:42 ` [Qemu-devel] [PULL 02/28] allocate pci id for mdpy Michael S. Tsirkin
2018-05-23 14:42 ` [Qemu-devel] [PULL 04/28] vhost: add trace for IOTLB miss Michael S. Tsirkin
2018-05-23 14:42 ` [Qemu-devel] [PULL 03/28] virtio-balloon: add hugetlb page allocation counts Michael S. Tsirkin
2018-05-23 14:42 ` [Qemu-devel] [PULL 05/28] update-linux-headers.sh: drop kvm_para.h hacks Michael S. Tsirkin
2018-05-23 14:42 ` [Qemu-devel] [PULL 06/28] include/standard-headers: add asm-x86/kvm_para.h Michael S. Tsirkin
2018-05-23 14:43 ` [PULL 07/28] x86/cpu: use standard-headers/asm-x86.kvm_para.h Michael S. Tsirkin
2018-05-23 14:43   ` [Qemu-devel] " Michael S. Tsirkin
2018-05-25 11:06   ` Peter Maydell
2018-05-25 11:06     ` [Qemu-devel] " Peter Maydell
2018-05-25 11:53     ` Peter Maydell
2018-05-25 11:53       ` [Qemu-devel] " Peter Maydell
2018-05-25 12:18       ` Michael S. Tsirkin
2018-05-25 12:18         ` [Qemu-devel] " Michael S. Tsirkin
2018-05-25 12:21         ` Peter Maydell
2018-05-25 12:21           ` [Qemu-devel] " Peter Maydell
2018-05-25 12:27           ` Michael S. Tsirkin
2018-05-25 12:27             ` [Qemu-devel] " Michael S. Tsirkin
2018-05-25 12:30             ` Peter Maydell
2018-05-25 12:30               ` [Qemu-devel] " Peter Maydell
2018-05-25 12:35               ` Michael S. Tsirkin
2018-05-25 12:35                 ` [Qemu-devel] " Michael S. Tsirkin
2018-05-25 12:38                 ` Peter Maydell
2018-05-25 12:38                   ` [Qemu-devel] " Peter Maydell
2018-05-25 12:19     ` Michael S. Tsirkin
2018-05-25 12:19       ` [Qemu-devel] " Michael S. Tsirkin
2018-05-25 14:13     ` Paolo Bonzini
2018-05-25 14:13       ` [Qemu-devel] " Paolo Bonzini
2018-05-23 14:43 ` [Qemu-devel] [PULL 08/28] linux-headers: drop kvm_para.h Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 09/28] update-linux-headers.sh: unistd.h, kvm consistency Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 10/28] linux-headers: add unistd.h on all arches Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 12/28] vhost-user: add Net prefix to internal state structure Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 11/28] linux-headers: add kvm header for mips Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 13/28] vhost-user: support receiving file descriptors in slave_read Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 15/28] vhost-user+postcopy: Use qemu_set_nonblock Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 14/28] virtio: support setting memory region based host notifier Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 16/28] libvhost-user: Send messages with no data Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 17/28] hw/virtio: Fix brace Werror with clang 6.0.0 Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 18/28] contrib/vhost-user-blk: enable protocol feature for vhost-user-blk Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 19/28] nvdimm: fix typo in label-size definition Michael S. Tsirkin
2018-05-23 14:43 ` Michael S. Tsirkin [this message]
2018-05-23 14:43 ` [Qemu-devel] [PULL 21/28] intel-iommu: remove IntelIOMMUNotifierNode Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 22/28] intel-iommu: add iommu lock Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 23/28] intel-iommu: only do page walk for MAP notifiers Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 24/28] intel-iommu: introduce vtd_page_walk_info Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 25/28] intel-iommu: pass in address space when page walk Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 26/28] intel-iommu: trace domain id during " Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 27/28] util: implement simple iova tree Michael S. Tsirkin
2018-05-23 14:43 ` [Qemu-devel] [PULL 28/28] intel-iommu: rework the page walk logic Michael S. Tsirkin
2018-05-23 15:17 ` [Qemu-devel] [PULL 00/28] pc, pci, virtio, vhost: fixes, features no-reply
2018-05-24 14:18 ` Peter Maydell

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=1527086545-68024-21-git-send-email-mst@redhat.com \
    --to=mst@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=jasowang@redhat.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    --cc=rth@twiddle.net \
    /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.