Skip to contentSkip to conversation

memory: warning on out of scope notification

PATCH Superseded Yan Zhao 2 participants 4 messages Jun 24, 2019 – Jun 25, 2019

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Yan Zhao

author
#0 lore raw

PATCH v2 1/1 memory: warning on out of scope notification

if an entry has parts out of scope of notifier's range, print warning message.

Out of scope mapping/unmapping would cause problem, as in below case:

1. initially there are two notifiers with ranges 0-0xfedfffff, 0xfef00000-0xffffffffffffffff,
IOVAs from 0x3c000000 - 0x3c1fffff is in shadow page table.

2. in vfio, memory_region_register_iommu_notifier() is followed by memory_region_iommu_replay(), which will first call address space unmap,
and walk and add back all entries in vtd shadow page table. e.g.
(1) for notifier 0-0xfedfffff,

    IOVAs from 0 - 0xffffffff get unmapped,
    and IOVAs from 0x3c000000 - 0x3c1fffff get mapped
(2) for notifier 0xfef00000-0xffffffffffffffff
    IOVAs from 0 - 0x7fffffffff get unmapped,
    but IOVAs from 0x3c000000 - 0x3c1fffff cannot get mapped back.

---
v2:
1. added a local variable entry_end (Auger Eric)
2. using PRIx64 as format for address range in warning message (Auger Eric)

---
 memory.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
memory.c +12 -1
index 0a089a7..3aacc09 100644
@@ -1937,12 +1937,13 @@ void memory_region_notify_one(IOMMUNotifier *notifier,
                               IOMMUTLBEntry *entry)
 {
     IOMMUNotifierFlag request_flags;
+    hwaddr entry_end = entry->iova + entry->addr_mask;
 
     /*
      * Skip the notification if the notification does not overlap
      * with registered range.
      */
-    if (notifier->start > entry->iova + entry->addr_mask ||
+    if (notifier->start > entry_end ||
         notifier->end < entry->iova) {
         return;
     }
@@ -1953,6 +1954,16 @@ void memory_region_notify_one(IOMMUNotifier *notifier,
         request_flags = IOMMU_NOTIFIER_UNMAP;
     }
 
+    if (entry->iova < notifier->start ||
+            entry_end > notifier->end) {
+        warn_report("%s IOMMUTLBEntry 0x%" PRIx64 "-0x%" PRIx64
+                "outside of notifier scope 0x%" PRIx64 "-0x%" PRIx64,
+                (request_flags == IOMMU_NOTIFIER_MAP) ?
+                "Mapping" : "Unmapping",
+                entry->iova, entry->iova + entry->addr_mask,
+                notifier->start, notifier->end);
+    }
+
     if (notifier->notifier_flags & request_flags) {
         notifier->notify(notifier, entry);
     }
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v3 1/1 memory: warning on out of scope notification

if an entry has parts out of scope of notifier's range, print warning message.

Out of scope mapping/unmapping would cause problem, as in below case:

1. initially there are two notifiers with ranges 0-0xfedfffff, 0xfef00000-0xffffffffffffffff,
IOVAs from 0x3c000000 - 0x3c1fffff is in shadow page table.

2. in vfio, memory_region_register_iommu_notifier() is followed by memory_region_iommu_replay(), which will first call address space unmap,
and walk and add back all entries in vtd shadow page table. e.g.
(1) for notifier 0-0xfedfffff,

    IOVAs from 0 - 0xffffffff get unmapped,
    and IOVAs from 0x3c000000 - 0x3c1fffff get mapped
(2) for notifier 0xfef00000-0xffffffffffffffff
    IOVAs from 0 - 0x7fffffffff get unmapped,
    but IOVAs from 0x3c000000 - 0x3c1fffff cannot get mapped back.

---
v3:
refined code style and message format

v2:
1. added a local variable entry_end (Auger Eric)
2. using PRIx64 as format for address range in warning message (Auger Eric)

---
 memory.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
memory.c +11 -2
index 0a089a7..c7154ab 100644
@@ -1937,13 +1937,13 @@ void memory_region_notify_one(IOMMUNotifier *notifier,
                               IOMMUTLBEntry *entry)
 {
     IOMMUNotifierFlag request_flags;
+    hwaddr entry_end = entry->iova + entry->addr_mask;
 
     /*
      * Skip the notification if the notification does not overlap
      * with registered range.
      */
-    if (notifier->start > entry->iova + entry->addr_mask ||
-        notifier->end < entry->iova) {
+    if (notifier->start > entry_end || notifier->end < entry->iova) {
         return;
     }
 
@@ -1953,6 +1953,15 @@ void memory_region_notify_one(IOMMUNotifier *notifier,
         request_flags = IOMMU_NOTIFIER_UNMAP;
     }
 
+    if (entry->iova < notifier->start || entry_end > notifier->end) {
+        warn_report("%s IOMMUTLBEntry 0x%" PRIx64 " - 0x%" PRIx64
+                " outside of notifier scope 0x%" PRIx64 " - 0x%" PRIx64,
+                (request_flags == IOMMU_NOTIFIER_MAP) ?
+                "Mapping" : "Unmapping",
+                entry->iova, entry->iova + entry->addr_mask,
+                notifier->start, notifier->end);
+    }
+
     if (notifier->notifier_flags & request_flags) {
         notifier->notify(notifier, entry);
     }
Signature · 1 line
2.7.4
#2 lore raw

Hi Yan,

Quoted 56 lines · Yan Zhao

if an entry has parts out of scope of notifier's range, print warning message.

Out of scope mapping/unmapping would cause problem, as in below case:

1. initially there are two notifiers with ranges 0-0xfedfffff, 0xfef00000-0xffffffffffffffff,
IOVAs from 0x3c000000 - 0x3c1fffff is in shadow page table.

2. in vfio, memory_region_register_iommu_notifier() is followed by memory_region_iommu_replay(), which will first call address space unmap,
and walk and add back all entries in vtd shadow page table. e.g.
(1) for notifier 0-0xfedfffff,

    IOVAs from 0 - 0xffffffff get unmapped,
    and IOVAs from 0x3c000000 - 0x3c1fffff get mapped
(2) for notifier 0xfef00000-0xffffffffffffffff
    IOVAs from 0 - 0x7fffffffff get unmapped,
    but IOVAs from 0x3c000000 - 0x3c1fffff cannot get mapped back.

---
v3:
refined code style and message format

v2:
1. added a local variable entry_end (Auger Eric)
2. using PRIx64 as format for address range in warning message (Auger Eric)

---
 memory.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
memory.c +2 -2
index 0a089a7..c7154ab 100644
@@ -1937,13 +1937,13 @@ void memory_region_notify_one(IOMMUNotifier *notifier,
                               IOMMUTLBEntry *entry)
 {
     IOMMUNotifierFlag request_flags;
+    hwaddr entry_end = entry->iova + entry->addr_mask;
 
     /*
      * Skip the notification if the notification does not overlap
      * with registered range.
      */
-    if (notifier->start > entry->iova + entry->addr_mask ||
-        notifier->end < entry->iova) {
+    if (notifier->start > entry_end || notifier->end < entry->iova) {
         return;
     }
 

memory.c void memory_region_notify_one(IOMMUNotifier *notifier, · line 1961

Quoted 9 lines
memory.c +6 -0
@@ -1953,6 +1953,15 @@ void memory_region_notify_one(IOMMUNotifier *notifier,
         request_flags = IOMMU_NOTIFIER_UNMAP;
     }
 
+    if (entry->iova < notifier->start || entry_end > notifier->end) {
+        warn_report("%s IOMMUTLBEntry 0x%" PRIx64 " - 0x%" PRIx64
+                " outside of notifier scope 0x%" PRIx64 " - 0x%" PRIx64,
+                (request_flags == IOMMU_NOTIFIER_MAP) ?
+                "Mapping" : "Unmapping",
+                entry->iova, entry->iova + entry->addr_mask,

entry_end as well.

Then shouldn't we assert in that case?

Thanks

Eric

Quoted 7 lines
memory.c +3 -0
+                notifier->start, notifier->end);
+    }
+
     if (notifier->notifier_flags & request_flags) {
         notifier->notify(notifier, entry);
     }
author
#3 lore raw
Quoted 70 lines · Auger Eric

Hi Yan,

Quoted 56 lines · Yan Zhao

if an entry has parts out of scope of notifier's range, print warning message.

Out of scope mapping/unmapping would cause problem, as in below case:

1. initially there are two notifiers with ranges 0-0xfedfffff, 0xfef00000-0xffffffffffffffff,
IOVAs from 0x3c000000 - 0x3c1fffff is in shadow page table.

2. in vfio, memory_region_register_iommu_notifier() is followed by memory_region_iommu_replay(), which will first call address space unmap,
and walk and add back all entries in vtd shadow page table. e.g.
(1) for notifier 0-0xfedfffff,

    IOVAs from 0 - 0xffffffff get unmapped,
    and IOVAs from 0x3c000000 - 0x3c1fffff get mapped
(2) for notifier 0xfef00000-0xffffffffffffffff
    IOVAs from 0 - 0x7fffffffff get unmapped,
    but IOVAs from 0x3c000000 - 0x3c1fffff cannot get mapped back.

---
v3:
refined code style and message format

v2:
1. added a local variable entry_end (Auger Eric)
2. using PRIx64 as format for address range in warning message (Auger Eric)

---
 memory.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
memory.c +2 -2
index 0a089a7..c7154ab 100644
@@ -1937,13 +1937,13 @@ void memory_region_notify_one(IOMMUNotifier *notifier,
                               IOMMUTLBEntry *entry)
 {
     IOMMUNotifierFlag request_flags;
+    hwaddr entry_end = entry->iova + entry->addr_mask;
 
     /*
      * Skip the notification if the notification does not overlap
      * with registered range.
      */
-    if (notifier->start > entry->iova + entry->addr_mask ||
-        notifier->end < entry->iova) {
+    if (notifier->start > entry_end || notifier->end < entry->iova) {
         return;
     }
 

memory.c void memory_region_notify_one(IOMMUNotifier *notifier, · line 1961

Quoted 9 lines
memory.c +6 -0
@@ -1953,6 +1953,15 @@ void memory_region_notify_one(IOMMUNotifier *notifier,
         request_flags = IOMMU_NOTIFIER_UNMAP;
     }
 
+    if (entry->iova < notifier->start || entry_end > notifier->end) {
+        warn_report("%s IOMMUTLBEntry 0x%" PRIx64 " - 0x%" PRIx64
+                " outside of notifier scope 0x%" PRIx64 " - 0x%" PRIx64,
+                (request_flags == IOMMU_NOTIFIER_MAP) ?
+                "Mapping" : "Unmapping",
+                entry->iova, entry->iova + entry->addr_mask,

entry_end as well.

Quoted 2 lines

Then shouldn't we assert in that case?

Yes, assert is good. I'll change warning to assert then. Thanks
Yan

Quoted 10 lines

Thanks

Eric

Quoted 7 lines
memory.c +3 -0
+                notifier->start, notifier->end);
+    }
+
     if (notifier->notifier_flags & request_flags) {
         notifier->notify(notifier, entry);
     }