Skip to contentSkip to conversation

VT-d: some enhancements on iotlb

PATCH Superseded Peter Xu 3 participants 17 messages Jul 12, 2017 – Jul 27, 2017

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

v3 cover VT-d: some enhancements on iotlb

v3:
- fix issue reported by patchew on style

v2:
- rebase to master
- removed all "info iommu" patches for now
(is it bad to introduce new HMP commands without QMP? I thought not,
but in case I missed anything, please kindly let me know...)

Patch 1: fixes a very rare PT path issue on iova value. It didn't break anything since it's merely not touched (only if when IOMMU enabled, then set one device to PT), but still better fix it.

Patch 2: introduce "x-iotlb-size" to tune iotlb size, or to turn it off (e.g., when we want to measure how iotlb affects one payload).

Patch 3: some refine on iotlb entry.

Patch 4: implemented MRU list algorithm for iotlb.

Please review. Thanks,

Peter Xu (4):
intel_iommu: fix VTD_PAGE_MASK
intel_iommu: let iotlb size tunable
intel_iommu: use access_flags for iotlb
intel_iommu: implement mru list for iotlb

 hw/i386/intel_iommu.c          | 103 +++++++++++++++++++++++++----------------
 hw/i386/intel_iommu_internal.h |  11 +----
 hw/i386/trace-events           |   1 -
 include/hw/i386/intel_iommu.h  |  10 ++--
 4 files changed, 71 insertions(+), 54 deletions(-)
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH v3 1/4 intel_iommu: fix VTD_PAGE_MASK

IOMMUTLBEntry.iova is returned incorrectly in one PT path (though mostly we cannot really trigger this path, even if we do, we are mostly disgarding this value, so it didn't break anything). Fix it by converting the VTD_PAGE_MASK into normal definition (normally it should be pfn mask, not offset mask), then switch the other user of it.

Fixes b93130 ("intel_iommu: cleanup vtd_{do_}iommu_translate()")
---
 hw/i386/intel_iommu.c          | 2 +-
 hw/i386/intel_iommu_internal.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/i386/intel_iommu.c +1 -1
index 88dc042..392da45 100644
@@ -1141,7 +1141,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     if (vtd_ce_get_type(&ce) == VTD_CONTEXT_TT_PASS_THROUGH) {
         entry->iova = addr & VTD_PAGE_MASK;
         entry->translated_addr = entry->iova;
-        entry->addr_mask = VTD_PAGE_MASK;
+        entry->addr_mask = ~VTD_PAGE_MASK;
         entry->perm = IOMMU_RW;
         trace_vtd_translate_pt(source_id, entry->iova);
 
hw/i386/intel_iommu_internal.h +1 -1
index f50ecd8..754cf8a 100644
@@ -384,7 +384,7 @@ typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo;
 /* Pagesize of VTD paging structures, including root and context tables */
 #define VTD_PAGE_SHIFT              12
 #define VTD_PAGE_SIZE               (1ULL << VTD_PAGE_SHIFT)
-#define VTD_PAGE_MASK               (VTD_PAGE_SIZE - 1)
+#define VTD_PAGE_MASK               (~(VTD_PAGE_SIZE - 1))
 
 #define VTD_PAGE_SHIFT_4K           12
 #define VTD_PAGE_MASK_4K            (~((1ULL << VTD_PAGE_SHIFT_4K) - 1))
Signature · 1 line
2.7.4
author
#4 lore raw

PATCH v3 2/4 intel_iommu: let iotlb size tunable

In reply to Peter Xu

We were having static IOTLB size as 1024. Let it be a tunable. We can also turns IOTLB off if we want, by specify the size as zero.

The tunable is named as "x-iotlb-size" since that should not really be something used by user yet, but mostly for debugging purpose now.

---
 hw/i386/intel_iommu.c          | 11 ++++++++++-
 hw/i386/intel_iommu_internal.h |  1 -
 include/hw/i386/intel_iommu.h  |  1 +
 3 files changed, 11 insertions(+), 2 deletions(-)
hw/i386/intel_iommu.c +10 -1
index 392da45..9c36866 100644
@@ -222,6 +222,10 @@ static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
     uint64_t key;
     int level;
 
+    if (s->iotlb_size == 0) {
+        return NULL;
+    }
+
     for (level = VTD_SL_PT_LEVEL; level < VTD_SL_PML4_LEVEL; level++) {
         key = vtd_get_iotlb_key(vtd_get_iotlb_gfn(addr, level),
                                 source_id, level);
@@ -244,8 +248,12 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
     uint64_t *key = g_malloc(sizeof(*key));
     uint64_t gfn = vtd_get_iotlb_gfn(addr, level);
 
+    if (s->iotlb_size == 0) {
+        return;
+    }
+
     trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id);
-    if (g_hash_table_size(s->iotlb) >= VTD_IOTLB_MAX_SIZE) {
+    if (g_hash_table_size(s->iotlb) >= s->iotlb_size) {
         trace_vtd_iotlb_reset("iotlb exceeds size limit");
         vtd_reset_iotlb(s);
     }
@@ -2397,6 +2405,7 @@ static Property vtd_properties[] = {
                             ON_OFF_AUTO_AUTO),
     DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false),
     DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE),
+    DEFINE_PROP_UINT16("x-iotlb-size", IntelIOMMUState, iotlb_size, 1024),
     DEFINE_PROP_END_OF_LIST(),
 };
 
hw/i386/intel_iommu_internal.h +0 -1
index 754cf8a..2d77249 100644
@@ -116,7 +116,6 @@
 /* The shift of source_id in the key of IOTLB hash table */
 #define VTD_IOTLB_SID_SHIFT         36
 #define VTD_IOTLB_LVL_SHIFT         52
-#define VTD_IOTLB_MAX_SIZE          1024    /* Max size of the hash table */
 
 /* IOTLB_REG */
 #define VTD_TLB_GLOBAL_FLUSH        (1ULL << 60) /* Global invalidation */
include/hw/i386/intel_iommu.h +1 -0
index 3e51876..a57f419 100644
@@ -288,6 +288,7 @@ struct IntelIOMMUState {
 
     uint32_t context_cache_gen;     /* Should be in [1,MAX] */
     GHashTable *iotlb;              /* IOTLB */
+    uint16_t iotlb_size;            /* IOTLB max cache entries */
 
     MemoryRegionIOMMUOps iommu_ops;
     GHashTable *vtd_as_by_busptr;   /* VTDBus objects indexed by PCIBus* reference */
Signature · 1 line
2.7.4
author
#7 lore raw

PATCH v3 3/4 intel_iommu: use access_flags for iotlb

In reply to Peter Xu

It was cached by read/write separately. Let's merge them.

---
 hw/i386/intel_iommu.c         | 15 +++++++--------
 include/hw/i386/intel_iommu.h |  3 +--
 2 files changed, 8 insertions(+), 10 deletions(-)
hw/i386/intel_iommu.c +7 -8
index 9c36866..e17340c 100644
@@ -241,8 +241,7 @@ out:
 
 static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
                              uint16_t domain_id, hwaddr addr, uint64_t slpte,
-                             bool read_flags, bool write_flags,
-                             uint32_t level)
+                             uint8_t access_flags, uint32_t level)
 {
     VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
     uint64_t *key = g_malloc(sizeof(*key));
@@ -261,8 +260,7 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
     entry->gfn = gfn;
     entry->domain_id = domain_id;
     entry->slpte = slpte;
-    entry->read_flags = read_flags;
-    entry->write_flags = write_flags;
+    entry->access_flags = access_flags;
     entry->mask = vtd_slpt_level_page_mask(level);
     *key = vtd_get_iotlb_key(gfn, source_id, level);
     g_hash_table_replace(s->iotlb, key, entry);
@@ -1095,6 +1093,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     bool is_fpd_set = false;
     bool reads = true;
     bool writes = true;
+    uint8_t access_flags;
     VTDIOTLBEntry *iotlb_entry;
 
     /*
@@ -1109,8 +1108,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
         trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte,
                                  iotlb_entry->domain_id);
         slpte = iotlb_entry->slpte;
-        reads = iotlb_entry->read_flags;
-        writes = iotlb_entry->write_flags;
+        access_flags = iotlb_entry->access_flags;
         page_mask = iotlb_entry->mask;
         goto out;
     }
@@ -1180,13 +1178,14 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     }
 
     page_mask = vtd_slpt_level_page_mask(level);
+    access_flags = IOMMU_ACCESS_FLAG(reads, writes);
     vtd_update_iotlb(s, source_id, VTD_CONTEXT_ENTRY_DID(ce.hi), addr, slpte,
-                     reads, writes, level);
+                     access_flags, level);
 out:
     entry->iova = addr & page_mask;
     entry->translated_addr = vtd_get_slpte_addr(slpte) & page_mask;
     entry->addr_mask = ~page_mask;
-    entry->perm = IOMMU_ACCESS_FLAG(reads, writes);
+    entry->perm = access_flags;
     return true;
 
 error:
include/hw/i386/intel_iommu.h +1 -2
index a57f419..1b51c9f 100644
@@ -101,8 +101,7 @@ struct VTDIOTLBEntry {
     uint16_t domain_id;
     uint64_t slpte;
     uint64_t mask;
-    bool read_flags;
-    bool write_flags;
+    uint8_t access_flags;
 };
 
 /* VT-d Source-ID Qualifier types */
Signature · 1 line
2.7.4
author
#8 lore raw

PATCH v3 4/4 intel_iommu: implement mru list for iotlb

In reply to Peter Xu

It is not wise to disgard all the IOTLB cache when cache size reaches max, but that's what we do now. A slightly better (but still simple) way to do this is, we just throw away the least recent used cache entry.

This patch implemented MRU list algorithm for VT-d IOTLB. The main logic is to maintain a Most Recently Used list for the IOTLB entries. The hash table is still used for lookup, though a new list field is added to each IOTLB entry for a iotlb MRU list. For each active IOTLB entry, it's both in the hash table in s->iotlb, and also linked into the MRU list of s->iotlb_head. The hash helps in fast lookup, and the MRU list helps in knowing whether the cache is still hot.

After we have such a MRU list, replacing all the iterators of IOTLB entries by using list iterations rather than hash table iterations.

---
 hw/i386/intel_iommu.c          | 75 +++++++++++++++++++++++++-----------------
 hw/i386/intel_iommu_internal.h |  8 -----
 hw/i386/trace-events           |  1 -
 include/hw/i386/intel_iommu.h  |  6 +++-
 4 files changed, 50 insertions(+), 40 deletions(-)
hw/i386/intel_iommu.c +45 -30
index e17340c..6320dea 100644
@@ -37,6 +37,9 @@
 #include "kvm_i386.h"
 #include "trace.h"
 
+#define FOREACH_IOTLB_SAFE(entry, s, entry_n) \
+    QTAILQ_FOREACH_SAFE(entry, &(s)->iotlb_head, link, entry_n)
+
 static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val,
                             uint64_t wmask, uint64_t w1cmask)
 {
@@ -139,14 +142,6 @@ static guint vtd_uint64_hash(gconstpointer v)
     return (guint)*(const uint64_t *)v;
 }
 
-static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
-                                          gpointer user_data)
-{
-    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
-    uint16_t domain_id = *(uint16_t *)user_data;
-    return entry->domain_id == domain_id;
-}
-
 /* The shift of an addr for a certain level of paging structure */
 static inline uint32_t vtd_slpt_level_shift(uint32_t level)
 {
@@ -159,18 +154,6 @@ static inline uint64_t vtd_slpt_level_page_mask(uint32_t level)
     return ~((1ULL << vtd_slpt_level_shift(level)) - 1);
 }
 
-static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
-                                        gpointer user_data)
-{
-    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
-    VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
-    uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask;
-    uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K;
-    return (entry->domain_id == info->domain_id) &&
-            (((entry->gfn & info->mask) == gfn) ||
-             (entry->gfn == gfn_tlb));
-}
-
 /* Reset all the gen of VTDAddressSpace to zero and set the gen of
  * IntelIOMMUState to 1.
  */
@@ -201,6 +184,7 @@ static void vtd_reset_iotlb(IntelIOMMUState *s)
 {
     assert(s->iotlb);
     g_hash_table_remove_all(s->iotlb);
+    QTAILQ_INIT(&s->iotlb_head);
 }
 
 static uint64_t vtd_get_iotlb_key(uint64_t gfn, uint16_t source_id,
@@ -231,6 +215,9 @@ static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
                                 source_id, level);
         entry = g_hash_table_lookup(s->iotlb, &key);
         if (entry) {
+            /* Move the entry to the head of MRU list */
+            QTAILQ_REMOVE(&s->iotlb_head, entry, link);
+            QTAILQ_INSERT_HEAD(&s->iotlb_head, entry, link);
             goto out;
         }
     }
@@ -239,11 +226,23 @@ out:
     return entry;
 }
 
+static void vtd_iotlb_remove_entry(IntelIOMMUState *s, VTDIOTLBEntry *entry)
+{
+    uint64_t key = entry->key;
+
+    /*
+     * To remove an entry, we need to both remove it from the MRU
+     * list, and also from the hash table.
+     */
+    QTAILQ_REMOVE(&s->iotlb_head, entry, link);
+    g_hash_table_remove(s->iotlb, &key);
+}
+
 static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
                              uint16_t domain_id, hwaddr addr, uint64_t slpte,
                              uint8_t access_flags, uint32_t level)
 {
-    VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
+    VTDIOTLBEntry *entry = g_new0(VTDIOTLBEntry, 1), *last;
     uint64_t *key = g_malloc(sizeof(*key));
     uint64_t gfn = vtd_get_iotlb_gfn(addr, level);
 
@@ -253,8 +252,9 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
 
     trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id);
     if (g_hash_table_size(s->iotlb) >= s->iotlb_size) {
-        trace_vtd_iotlb_reset("iotlb exceeds size limit");
-        vtd_reset_iotlb(s);
+        /* Remove the Least Recently Used cache */
+        last = QTAILQ_LAST(&s->iotlb_head, VTDIOTLBEntryHead);
+        vtd_iotlb_remove_entry(s, last);
     }
 
     entry->gfn = gfn;
@@ -263,7 +263,11 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
     entry->access_flags = access_flags;
     entry->mask = vtd_slpt_level_page_mask(level);
     *key = vtd_get_iotlb_key(gfn, source_id, level);
+    entry->key = *key;
     g_hash_table_replace(s->iotlb, key, entry);
+
+    /* Update MRU list */
+    QTAILQ_INSERT_HEAD(&s->iotlb_head, entry, link);
 }
 
 /* Given the reg addr of both the message data and address, generate an
@@ -1354,11 +1358,15 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
     IntelIOMMUNotifierNode *node;
     VTDContextEntry ce;
     VTDAddressSpace *vtd_as;
+    VTDIOTLBEntry *entry, *entry_n;
 
     trace_vtd_inv_desc_iotlb_domain(domain_id);
 
-    g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
-                                &domain_id);
+    FOREACH_IOTLB_SAFE(entry, s, entry_n) {
+        if (entry->domain_id == domain_id) {
+            vtd_iotlb_remove_entry(s, entry);
+        }
+    }
 
     QLIST_FOREACH(node, &s->notifiers_list, next) {
         vtd_as = node->vtd_as;
@@ -1400,15 +1408,22 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
 static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
                                       hwaddr addr, uint8_t am)
 {
-    VTDIOTLBPageInvInfo info;
+    VTDIOTLBEntry *entry, *entry_n;
+    uint64_t gfn, mask;
 
     trace_vtd_inv_desc_iotlb_pages(domain_id, addr, am);
 
     assert(am <= VTD_MAMV);
-    info.domain_id = domain_id;
-    info.addr = addr;
-    info.mask = ~((1 << am) - 1);
-    g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
+
+    mask = ~((1 << am) - 1);
+    gfn = (addr >> VTD_PAGE_SHIFT) & mask;
+
+    FOREACH_IOTLB_SAFE(entry, s, entry_n) {
+        if (entry->domain_id == domain_id && (entry->gfn & mask) == gfn) {
+            vtd_iotlb_remove_entry(s, entry);
+        }
+    }
+
     vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am);
 }
 
hw/i386/intel_iommu_internal.h +0 -8
index 2d77249..76efc66 100644
@@ -372,14 +372,6 @@ typedef union VTDInvDesc VTDInvDesc;
 #define VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI 0xffeULL
 #define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0fff8
 
-/* Information about page-selective IOTLB invalidate */
-struct VTDIOTLBPageInvInfo {
-    uint16_t domain_id;
-    uint64_t addr;
-    uint8_t mask;
-};
-typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo;
-
 /* Pagesize of VTD paging structures, including root and context tables */
 #define VTD_PAGE_SHIFT              12
 #define VTD_PAGE_SIZE               (1ULL << VTD_PAGE_SHIFT)
hw/i386/trace-events +0 -1
index 42d8a7e..b552815 100644
@@ -34,7 +34,6 @@ vtd_iotlb_page_hit(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t domain)
 vtd_iotlb_page_update(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t domain) "IOTLB page update sid 0x%"PRIx16" iova 0x%"PRIx64" slpte 0x%"PRIx64" domain 0x%"PRIx16
 vtd_iotlb_cc_hit(uint8_t bus, uint8_t devfn, uint64_t high, uint64_t low, uint32_t gen) "IOTLB context hit bus 0x%"PRIx8" devfn 0x%"PRIx8" high 0x%"PRIx64" low 0x%"PRIx64" gen %"PRIu32
 vtd_iotlb_cc_update(uint8_t bus, uint8_t devfn, uint64_t high, uint64_t low, uint32_t gen1, uint32_t gen2) "IOTLB context update bus 0x%"PRIx8" devfn 0x%"PRIx8" high 0x%"PRIx64" low 0x%"PRIx64" gen %"PRIu32" -> gen %"PRIu32
-vtd_iotlb_reset(const char *reason) "IOTLB reset (reason: %s)"
 vtd_fault_disabled(void) "Fault processing disabled for context entry"
 vtd_replay_ce_valid(uint8_t bus, uint8_t dev, uint8_t fn, uint16_t domain, uint64_t hi, uint64_t lo) "replay valid context device %02"PRIx8":%02"PRIx8".%02"PRIx8" domain 0x%"PRIx16" hi 0x%"PRIx64" lo 0x%"PRIx64
 vtd_replay_ce_invalid(uint8_t bus, uint8_t dev, uint8_t fn) "replay invalid context device %02"PRIx8":%02"PRIx8".%02"PRIx8
include/hw/i386/intel_iommu.h +5 -1
index 1b51c9f..d2caab3 100644
@@ -98,9 +98,11 @@ struct VTDBus {
 
 struct VTDIOTLBEntry {
     uint64_t gfn;
-    uint16_t domain_id;
     uint64_t slpte;
     uint64_t mask;
+    uint64_t key;
+    QTAILQ_ENTRY(VTDIOTLBEntry) link;
+    uint16_t domain_id;
     uint8_t access_flags;
 };
 
@@ -288,6 +290,8 @@ struct IntelIOMMUState {
     uint32_t context_cache_gen;     /* Should be in [1,MAX] */
     GHashTable *iotlb;              /* IOTLB */
     uint16_t iotlb_size;            /* IOTLB max cache entries */
+    /* Head of IOTLB MRU list */
+    QTAILQ_HEAD(VTDIOTLBEntryHead, VTDIOTLBEntry) iotlb_head;
 
     MemoryRegionIOMMUOps iommu_ops;
     GHashTable *vtd_as_by_busptr;   /* VTDBus objects indexed by PCIBus* reference */
Signature · 1 line
2.7.4
#2 lore raw

Re: [Qemu-devel] [PATCH v3 1/4] intel_iommu: fix VTD_PAGE_MASK

In reply to Peter Xu
Quoted 31 lines · Peter Xu

IOMMUTLBEntry.iova is returned incorrectly in one PT path (though mostly we cannot really trigger this path, even if we do, we are mostly disgarding this value, so it didn't break anything). Fix it by converting the VTD_PAGE_MASK into normal definition (normally it should be pfn mask, not offset mask), then switch the other user of it.

Fixes b93130 ("intel_iommu: cleanup vtd_{do_}iommu_translate()")
---
  hw/i386/intel_iommu.c          | 2 +-
  hw/i386/intel_iommu_internal.h | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
hw/i386/intel_iommu.c +1 -1
index 88dc042..392da45 100644
@@ -1141,7 +1141,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
      if (vtd_ce_get_type(&ce) == VTD_CONTEXT_TT_PASS_THROUGH) {
          entry->iova = addr & VTD_PAGE_MASK;
          entry->translated_addr = entry->iova;
-        entry->addr_mask = VTD_PAGE_MASK;
+        entry->addr_mask = ~VTD_PAGE_MASK;
          entry->perm = IOMMU_RW;
          trace_vtd_translate_pt(source_id, entry->iova);
 
hw/i386/intel_iommu_internal.h +0 -0
index f50ecd8..754cf8a 100644

hw/i386/intel_iommu_internal.h typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo; · line 390

Quoted 8 lines
hw/i386/intel_iommu_internal.h +1 -1
@@ -384,7 +384,7 @@ typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo;
  /* Pagesize of VTD paging structures, including root and context tables */
  #define VTD_PAGE_SHIFT              12
  #define VTD_PAGE_SIZE               (1ULL << VTD_PAGE_SHIFT)
-#define VTD_PAGE_MASK               (VTD_PAGE_SIZE - 1)
+#define VTD_PAGE_MASK               (~(VTD_PAGE_SIZE - 1))
 
  #define VTD_PAGE_SHIFT_4K           12
  #define VTD_PAGE_MASK_4K            (~((1ULL << VTD_PAGE_SHIFT_4K) - 1))

I wonder whether or not using VTD_PAGE_MASK_4K is more than enough.

Thanks

#9 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] intel_iommu: implement mru list for iotlb

In reply to Peter Xu
Quoted 12 lines · Peter Xu

It is not wise to disgard all the IOTLB cache when cache size reaches max, but that's what we do now. A slightly better (but still simple) way to do this is, we just throw away the least recent used cache entry.

This patch implemented MRU list algorithm for VT-d IOTLB. The main logic is to maintain a Most Recently Used list for the IOTLB entries. The hash table is still used for lookup, though a new list field is added to each IOTLB entry for a iotlb MRU list. For each active IOTLB entry, it's both in the hash table in s->iotlb, and also linked into the MRU list of s->iotlb_head. The hash helps in fast lookup, and the MRU list helps in knowing whether the cache is still hot.

Quoted 2 lines

After we have such a MRU list, replacing all the iterators of IOTLB entries by using list iterations rather than hash table iterations.

Any reason of doing this, I thought hashtable was even a little bit faster?

Thanks

Quoted 227 lines
---
  hw/i386/intel_iommu.c          | 75 +++++++++++++++++++++++++-----------------
  hw/i386/intel_iommu_internal.h |  8 -----
  hw/i386/trace-events           |  1 -
  include/hw/i386/intel_iommu.h  |  6 +++-
  4 files changed, 50 insertions(+), 40 deletions(-)
hw/i386/intel_iommu.c +45 -30
index e17340c..6320dea 100644
@@ -37,6 +37,9 @@
  #include "kvm_i386.h"
  #include "trace.h"
 
+#define FOREACH_IOTLB_SAFE(entry, s, entry_n) \
+    QTAILQ_FOREACH_SAFE(entry, &(s)->iotlb_head, link, entry_n)
+
  static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val,
                              uint64_t wmask, uint64_t w1cmask)
  {
@@ -139,14 +142,6 @@ static guint vtd_uint64_hash(gconstpointer v)
      return (guint)*(const uint64_t *)v;
  }
 
-static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
-                                          gpointer user_data)
-{
-    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
-    uint16_t domain_id = *(uint16_t *)user_data;
-    return entry->domain_id == domain_id;
-}
-
  /* The shift of an addr for a certain level of paging structure */
  static inline uint32_t vtd_slpt_level_shift(uint32_t level)
  {
@@ -159,18 +154,6 @@ static inline uint64_t vtd_slpt_level_page_mask(uint32_t level)
      return ~((1ULL << vtd_slpt_level_shift(level)) - 1);
  }
 
-static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
-                                        gpointer user_data)
-{
-    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
-    VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
-    uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask;
-    uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K;
-    return (entry->domain_id == info->domain_id) &&
-            (((entry->gfn & info->mask) == gfn) ||
-             (entry->gfn == gfn_tlb));
-}
-
  /* Reset all the gen of VTDAddressSpace to zero and set the gen of
   * IntelIOMMUState to 1.
   */
@@ -201,6 +184,7 @@ static void vtd_reset_iotlb(IntelIOMMUState *s)
  {
      assert(s->iotlb);
      g_hash_table_remove_all(s->iotlb);
+    QTAILQ_INIT(&s->iotlb_head);
  }
 
  static uint64_t vtd_get_iotlb_key(uint64_t gfn, uint16_t source_id,
@@ -231,6 +215,9 @@ static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
                                  source_id, level);
          entry = g_hash_table_lookup(s->iotlb, &key);
          if (entry) {
+            /* Move the entry to the head of MRU list */
+            QTAILQ_REMOVE(&s->iotlb_head, entry, link);
+            QTAILQ_INSERT_HEAD(&s->iotlb_head, entry, link);
              goto out;
          }
      }
@@ -239,11 +226,23 @@ out:
      return entry;
  }
 
+static void vtd_iotlb_remove_entry(IntelIOMMUState *s, VTDIOTLBEntry *entry)
+{
+    uint64_t key = entry->key;
+
+    /*
+     * To remove an entry, we need to both remove it from the MRU
+     * list, and also from the hash table.
+     */
+    QTAILQ_REMOVE(&s->iotlb_head, entry, link);
+    g_hash_table_remove(s->iotlb, &key);
+}
+
  static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
                               uint16_t domain_id, hwaddr addr, uint64_t slpte,
                               uint8_t access_flags, uint32_t level)
  {
-    VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
+    VTDIOTLBEntry *entry = g_new0(VTDIOTLBEntry, 1), *last;
      uint64_t *key = g_malloc(sizeof(*key));
      uint64_t gfn = vtd_get_iotlb_gfn(addr, level);
 
@@ -253,8 +252,9 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
 
      trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id);
      if (g_hash_table_size(s->iotlb) >= s->iotlb_size) {
-        trace_vtd_iotlb_reset("iotlb exceeds size limit");
-        vtd_reset_iotlb(s);
+        /* Remove the Least Recently Used cache */
+        last = QTAILQ_LAST(&s->iotlb_head, VTDIOTLBEntryHead);
+        vtd_iotlb_remove_entry(s, last);
      }
 
      entry->gfn = gfn;
@@ -263,7 +263,11 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
      entry->access_flags = access_flags;
      entry->mask = vtd_slpt_level_page_mask(level);
      *key = vtd_get_iotlb_key(gfn, source_id, level);
+    entry->key = *key;
      g_hash_table_replace(s->iotlb, key, entry);
+
+    /* Update MRU list */
+    QTAILQ_INSERT_HEAD(&s->iotlb_head, entry, link);
  }
 
  /* Given the reg addr of both the message data and address, generate an
@@ -1354,11 +1358,15 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
      IntelIOMMUNotifierNode *node;
      VTDContextEntry ce;
      VTDAddressSpace *vtd_as;
+    VTDIOTLBEntry *entry, *entry_n;
 
      trace_vtd_inv_desc_iotlb_domain(domain_id);
 
-    g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
-                                &domain_id);
+    FOREACH_IOTLB_SAFE(entry, s, entry_n) {
+        if (entry->domain_id == domain_id) {
+            vtd_iotlb_remove_entry(s, entry);
+        }
+    }
 
      QLIST_FOREACH(node, &s->notifiers_list, next) {
          vtd_as = node->vtd_as;
@@ -1400,15 +1408,22 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
  static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
                                        hwaddr addr, uint8_t am)
  {
-    VTDIOTLBPageInvInfo info;
+    VTDIOTLBEntry *entry, *entry_n;
+    uint64_t gfn, mask;
 
      trace_vtd_inv_desc_iotlb_pages(domain_id, addr, am);
 
      assert(am <= VTD_MAMV);
-    info.domain_id = domain_id;
-    info.addr = addr;
-    info.mask = ~((1 << am) - 1);
-    g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
+
+    mask = ~((1 << am) - 1);
+    gfn = (addr >> VTD_PAGE_SHIFT) & mask;
+
+    FOREACH_IOTLB_SAFE(entry, s, entry_n) {
+        if (entry->domain_id == domain_id && (entry->gfn & mask) == gfn) {
+            vtd_iotlb_remove_entry(s, entry);
+        }
+    }
+
      vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am);
  }
 
hw/i386/intel_iommu_internal.h +0 -8
index 2d77249..76efc66 100644
@@ -372,14 +372,6 @@ typedef union VTDInvDesc VTDInvDesc;
  #define VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI 0xffeULL
  #define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0fff8
 
-/* Information about page-selective IOTLB invalidate */
-struct VTDIOTLBPageInvInfo {
-    uint16_t domain_id;
-    uint64_t addr;
-    uint8_t mask;
-};
-typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo;
-
  /* Pagesize of VTD paging structures, including root and context tables */
  #define VTD_PAGE_SHIFT              12
  #define VTD_PAGE_SIZE               (1ULL << VTD_PAGE_SHIFT)
hw/i386/trace-events +0 -1
index 42d8a7e..b552815 100644
@@ -34,7 +34,6 @@ vtd_iotlb_page_hit(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t domain)
  vtd_iotlb_page_update(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t domain) "IOTLB page update sid 0x%"PRIx16" iova 0x%"PRIx64" slpte 0x%"PRIx64" domain 0x%"PRIx16
  vtd_iotlb_cc_hit(uint8_t bus, uint8_t devfn, uint64_t high, uint64_t low, uint32_t gen) "IOTLB context hit bus 0x%"PRIx8" devfn 0x%"PRIx8" high 0x%"PRIx64" low 0x%"PRIx64" gen %"PRIu32
  vtd_iotlb_cc_update(uint8_t bus, uint8_t devfn, uint64_t high, uint64_t low, uint32_t gen1, uint32_t gen2) "IOTLB context update bus 0x%"PRIx8" devfn 0x%"PRIx8" high 0x%"PRIx64" low 0x%"PRIx64" gen %"PRIu32" -> gen %"PRIu32
-vtd_iotlb_reset(const char *reason) "IOTLB reset (reason: %s)"
  vtd_fault_disabled(void) "Fault processing disabled for context entry"
  vtd_replay_ce_valid(uint8_t bus, uint8_t dev, uint8_t fn, uint16_t domain, uint64_t hi, uint64_t lo) "replay valid context device %02"PRIx8":%02"PRIx8".%02"PRIx8" domain 0x%"PRIx16" hi 0x%"PRIx64" lo 0x%"PRIx64
  vtd_replay_ce_invalid(uint8_t bus, uint8_t dev, uint8_t fn) "replay invalid context device %02"PRIx8":%02"PRIx8".%02"PRIx8
include/hw/i386/intel_iommu.h +5 -1
index 1b51c9f..d2caab3 100644
@@ -98,9 +98,11 @@ struct VTDBus {
 
  struct VTDIOTLBEntry {
      uint64_t gfn;
-    uint16_t domain_id;
      uint64_t slpte;
      uint64_t mask;
+    uint64_t key;
+    QTAILQ_ENTRY(VTDIOTLBEntry) link;
+    uint16_t domain_id;
      uint8_t access_flags;
  };
 
@@ -288,6 +290,8 @@ struct IntelIOMMUState {
      uint32_t context_cache_gen;     /* Should be in [1,MAX] */
      GHashTable *iotlb;              /* IOTLB */
      uint16_t iotlb_size;            /* IOTLB max cache entries */
+    /* Head of IOTLB MRU list */
+    QTAILQ_HEAD(VTDIOTLBEntryHead, VTDIOTLBEntry) iotlb_head;
 
      MemoryRegionIOMMUOps iommu_ops;
      GHashTable *vtd_as_by_busptr;   /* VTDBus objects indexed by PCIBus* reference */
#5 lore raw

Re: [Qemu-devel] [PATCH v3 2/4] intel_iommu: let iotlb size tunable

In reply to Peter Xu
Quoted 7 lines · Peter Xu

We were having static IOTLB size as 1024. Let it be a tunable. We can also turns IOTLB off if we want, by specify the size as zero.

The tunable is named as "x-iotlb-size" since that should not really be something used by user yet, but mostly for debugging purpose now.

I'm somewhat vary of adding tunables just for developers to play with. People will find and abuse them :)

Quoted 69 lines
---
 hw/i386/intel_iommu.c          | 11 ++++++++++-
 hw/i386/intel_iommu_internal.h |  1 -
 include/hw/i386/intel_iommu.h  |  1 +
 3 files changed, 11 insertions(+), 2 deletions(-)
hw/i386/intel_iommu.c +10 -1
index 392da45..9c36866 100644
@@ -222,6 +222,10 @@ static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
     uint64_t key;
     int level;
 
+    if (s->iotlb_size == 0) {
+        return NULL;
+    }
+
     for (level = VTD_SL_PT_LEVEL; level < VTD_SL_PML4_LEVEL; level++) {
         key = vtd_get_iotlb_key(vtd_get_iotlb_gfn(addr, level),
                                 source_id, level);
@@ -244,8 +248,12 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
     uint64_t *key = g_malloc(sizeof(*key));
     uint64_t gfn = vtd_get_iotlb_gfn(addr, level);
 
+    if (s->iotlb_size == 0) {
+        return;
+    }
+
     trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id);
-    if (g_hash_table_size(s->iotlb) >= VTD_IOTLB_MAX_SIZE) {
+    if (g_hash_table_size(s->iotlb) >= s->iotlb_size) {
         trace_vtd_iotlb_reset("iotlb exceeds size limit");
         vtd_reset_iotlb(s);
     }
@@ -2397,6 +2405,7 @@ static Property vtd_properties[] = {
                             ON_OFF_AUTO_AUTO),
     DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false),
     DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE),
+    DEFINE_PROP_UINT16("x-iotlb-size", IntelIOMMUState, iotlb_size, 1024),
     DEFINE_PROP_END_OF_LIST(),
 };
 
hw/i386/intel_iommu_internal.h +0 -1
index 754cf8a..2d77249 100644
@@ -116,7 +116,6 @@
 /* The shift of source_id in the key of IOTLB hash table */
 #define VTD_IOTLB_SID_SHIFT         36
 #define VTD_IOTLB_LVL_SHIFT         52
-#define VTD_IOTLB_MAX_SIZE          1024    /* Max size of the hash table */
 
 /* IOTLB_REG */
 #define VTD_TLB_GLOBAL_FLUSH        (1ULL << 60) /* Global invalidation */
include/hw/i386/intel_iommu.h +1 -0
index 3e51876..a57f419 100644
@@ -288,6 +288,7 @@ struct IntelIOMMUState {
 
     uint32_t context_cache_gen;     /* Should be in [1,MAX] */
     GHashTable *iotlb;              /* IOTLB */
+    uint16_t iotlb_size;            /* IOTLB max cache entries */
 
     MemoryRegionIOMMUOps iommu_ops;
     GHashTable *vtd_as_by_busptr;   /* VTDBus objects indexed by PCIBus* reference */
Signature · 1 line
2.7.4
#15 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] intel_iommu: implement mru list for iotlb

In reply to Peter Xu
Quoted 3 lines · Peter Xu

It is not wise to disgard all the IOTLB cache when cache size reaches max, but that's what we do now. A slightly better (but still simple) way to do this is, we just throw away the least recent used cache entry.

Not wise how? Slower? It seems simpler.

Quoted 7 lines

This patch implemented MRU list algorithm for VT-d IOTLB. The main logic is to maintain a Most Recently Used list for the IOTLB entries. The hash table is still used for lookup, though a new list field is added to each IOTLB entry for a iotlb MRU list. For each active IOTLB entry, it's both in the hash table in s->iotlb, and also linked into the MRU list of s->iotlb_head. The hash helps in fast lookup, and the MRU list helps in knowing whether the cache is still hot.

Helps how? Faster? What's missing here is a report of the performance impact.

Quoted 231 lines

After we have such a MRU list, replacing all the iterators of IOTLB entries by using list iterations rather than hash table iterations.

---
 hw/i386/intel_iommu.c          | 75 +++++++++++++++++++++++++-----------------
 hw/i386/intel_iommu_internal.h |  8 -----
 hw/i386/trace-events           |  1 -
 include/hw/i386/intel_iommu.h  |  6 +++-
 4 files changed, 50 insertions(+), 40 deletions(-)
hw/i386/intel_iommu.c +45 -30
index e17340c..6320dea 100644
@@ -37,6 +37,9 @@
 #include "kvm_i386.h"
 #include "trace.h"
 
+#define FOREACH_IOTLB_SAFE(entry, s, entry_n) \
+    QTAILQ_FOREACH_SAFE(entry, &(s)->iotlb_head, link, entry_n)
+
 static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val,
                             uint64_t wmask, uint64_t w1cmask)
 {
@@ -139,14 +142,6 @@ static guint vtd_uint64_hash(gconstpointer v)
     return (guint)*(const uint64_t *)v;
 }
 
-static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
-                                          gpointer user_data)
-{
-    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
-    uint16_t domain_id = *(uint16_t *)user_data;
-    return entry->domain_id == domain_id;
-}
-
 /* The shift of an addr for a certain level of paging structure */
 static inline uint32_t vtd_slpt_level_shift(uint32_t level)
 {
@@ -159,18 +154,6 @@ static inline uint64_t vtd_slpt_level_page_mask(uint32_t level)
     return ~((1ULL << vtd_slpt_level_shift(level)) - 1);
 }
 
-static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
-                                        gpointer user_data)
-{
-    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
-    VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
-    uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask;
-    uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K;
-    return (entry->domain_id == info->domain_id) &&
-            (((entry->gfn & info->mask) == gfn) ||
-             (entry->gfn == gfn_tlb));
-}
-
 /* Reset all the gen of VTDAddressSpace to zero and set the gen of
  * IntelIOMMUState to 1.
  */
@@ -201,6 +184,7 @@ static void vtd_reset_iotlb(IntelIOMMUState *s)
 {
     assert(s->iotlb);
     g_hash_table_remove_all(s->iotlb);
+    QTAILQ_INIT(&s->iotlb_head);
 }
 
 static uint64_t vtd_get_iotlb_key(uint64_t gfn, uint16_t source_id,
@@ -231,6 +215,9 @@ static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
                                 source_id, level);
         entry = g_hash_table_lookup(s->iotlb, &key);
         if (entry) {
+            /* Move the entry to the head of MRU list */
+            QTAILQ_REMOVE(&s->iotlb_head, entry, link);
+            QTAILQ_INSERT_HEAD(&s->iotlb_head, entry, link);
             goto out;
         }
     }
@@ -239,11 +226,23 @@ out:
     return entry;
 }
 
+static void vtd_iotlb_remove_entry(IntelIOMMUState *s, VTDIOTLBEntry *entry)
+{
+    uint64_t key = entry->key;
+
+    /*
+     * To remove an entry, we need to both remove it from the MRU
+     * list, and also from the hash table.
+     */
+    QTAILQ_REMOVE(&s->iotlb_head, entry, link);
+    g_hash_table_remove(s->iotlb, &key);
+}
+
 static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
                              uint16_t domain_id, hwaddr addr, uint64_t slpte,
                              uint8_t access_flags, uint32_t level)
 {
-    VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
+    VTDIOTLBEntry *entry = g_new0(VTDIOTLBEntry, 1), *last;
     uint64_t *key = g_malloc(sizeof(*key));
     uint64_t gfn = vtd_get_iotlb_gfn(addr, level);
 
@@ -253,8 +252,9 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
 
     trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id);
     if (g_hash_table_size(s->iotlb) >= s->iotlb_size) {
-        trace_vtd_iotlb_reset("iotlb exceeds size limit");
-        vtd_reset_iotlb(s);
+        /* Remove the Least Recently Used cache */
+        last = QTAILQ_LAST(&s->iotlb_head, VTDIOTLBEntryHead);
+        vtd_iotlb_remove_entry(s, last);
     }
 
     entry->gfn = gfn;
@@ -263,7 +263,11 @@ static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
     entry->access_flags = access_flags;
     entry->mask = vtd_slpt_level_page_mask(level);
     *key = vtd_get_iotlb_key(gfn, source_id, level);
+    entry->key = *key;
     g_hash_table_replace(s->iotlb, key, entry);
+
+    /* Update MRU list */
+    QTAILQ_INSERT_HEAD(&s->iotlb_head, entry, link);
 }
 
 /* Given the reg addr of both the message data and address, generate an
@@ -1354,11 +1358,15 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
     IntelIOMMUNotifierNode *node;
     VTDContextEntry ce;
     VTDAddressSpace *vtd_as;
+    VTDIOTLBEntry *entry, *entry_n;
 
     trace_vtd_inv_desc_iotlb_domain(domain_id);
 
-    g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
-                                &domain_id);
+    FOREACH_IOTLB_SAFE(entry, s, entry_n) {
+        if (entry->domain_id == domain_id) {
+            vtd_iotlb_remove_entry(s, entry);
+        }
+    }
 
     QLIST_FOREACH(node, &s->notifiers_list, next) {
         vtd_as = node->vtd_as;
@@ -1400,15 +1408,22 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
 static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
                                       hwaddr addr, uint8_t am)
 {
-    VTDIOTLBPageInvInfo info;
+    VTDIOTLBEntry *entry, *entry_n;
+    uint64_t gfn, mask;
 
     trace_vtd_inv_desc_iotlb_pages(domain_id, addr, am);
 
     assert(am <= VTD_MAMV);
-    info.domain_id = domain_id;
-    info.addr = addr;
-    info.mask = ~((1 << am) - 1);
-    g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
+
+    mask = ~((1 << am) - 1);
+    gfn = (addr >> VTD_PAGE_SHIFT) & mask;
+
+    FOREACH_IOTLB_SAFE(entry, s, entry_n) {
+        if (entry->domain_id == domain_id && (entry->gfn & mask) == gfn) {
+            vtd_iotlb_remove_entry(s, entry);
+        }
+    }
+
     vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am);
 }
 
hw/i386/intel_iommu_internal.h +0 -8
index 2d77249..76efc66 100644
@@ -372,14 +372,6 @@ typedef union VTDInvDesc VTDInvDesc;
 #define VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI 0xffeULL
 #define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0fff8
 
-/* Information about page-selective IOTLB invalidate */
-struct VTDIOTLBPageInvInfo {
-    uint16_t domain_id;
-    uint64_t addr;
-    uint8_t mask;
-};
-typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo;
-
 /* Pagesize of VTD paging structures, including root and context tables */
 #define VTD_PAGE_SHIFT              12
 #define VTD_PAGE_SIZE               (1ULL << VTD_PAGE_SHIFT)
hw/i386/trace-events +0 -1
index 42d8a7e..b552815 100644
@@ -34,7 +34,6 @@ vtd_iotlb_page_hit(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t domain)
 vtd_iotlb_page_update(uint16_t sid, uint64_t addr, uint64_t slpte, uint16_t domain) "IOTLB page update sid 0x%"PRIx16" iova 0x%"PRIx64" slpte 0x%"PRIx64" domain 0x%"PRIx16
 vtd_iotlb_cc_hit(uint8_t bus, uint8_t devfn, uint64_t high, uint64_t low, uint32_t gen) "IOTLB context hit bus 0x%"PRIx8" devfn 0x%"PRIx8" high 0x%"PRIx64" low 0x%"PRIx64" gen %"PRIu32
 vtd_iotlb_cc_update(uint8_t bus, uint8_t devfn, uint64_t high, uint64_t low, uint32_t gen1, uint32_t gen2) "IOTLB context update bus 0x%"PRIx8" devfn 0x%"PRIx8" high 0x%"PRIx64" low 0x%"PRIx64" gen %"PRIu32" -> gen %"PRIu32
-vtd_iotlb_reset(const char *reason) "IOTLB reset (reason: %s)"
 vtd_fault_disabled(void) "Fault processing disabled for context entry"
 vtd_replay_ce_valid(uint8_t bus, uint8_t dev, uint8_t fn, uint16_t domain, uint64_t hi, uint64_t lo) "replay valid context device %02"PRIx8":%02"PRIx8".%02"PRIx8" domain 0x%"PRIx16" hi 0x%"PRIx64" lo 0x%"PRIx64
 vtd_replay_ce_invalid(uint8_t bus, uint8_t dev, uint8_t fn) "replay invalid context device %02"PRIx8":%02"PRIx8".%02"PRIx8
include/hw/i386/intel_iommu.h +5 -1
index 1b51c9f..d2caab3 100644
@@ -98,9 +98,11 @@ struct VTDBus {
 
 struct VTDIOTLBEntry {
     uint64_t gfn;
-    uint16_t domain_id;
     uint64_t slpte;
     uint64_t mask;
+    uint64_t key;
+    QTAILQ_ENTRY(VTDIOTLBEntry) link;
+    uint16_t domain_id;
     uint8_t access_flags;
 };
 
@@ -288,6 +290,8 @@ struct IntelIOMMUState {
     uint32_t context_cache_gen;     /* Should be in [1,MAX] */
     GHashTable *iotlb;              /* IOTLB */
     uint16_t iotlb_size;            /* IOTLB max cache entries */
+    /* Head of IOTLB MRU list */
+    QTAILQ_HEAD(VTDIOTLBEntryHead, VTDIOTLBEntry) iotlb_head;
 
     MemoryRegionIOMMUOps iommu_ops;
     GHashTable *vtd_as_by_busptr;   /* VTDBus objects indexed by PCIBus* reference */
Signature · 1 line
2.7.4
author
#3 lore raw

Re: [Qemu-devel] [PATCH v3 1/4] intel_iommu: fix VTD_PAGE_MASK

In reply to Jason Wang
Quoted 34 lines · Jason Wang
Quoted 31 lines · Peter Xu

IOMMUTLBEntry.iova is returned incorrectly in one PT path (though mostly we cannot really trigger this path, even if we do, we are mostly disgarding this value, so it didn't break anything). Fix it by converting the VTD_PAGE_MASK into normal definition (normally it should be pfn mask, not offset mask), then switch the other user of it.

Fixes b93130 ("intel_iommu: cleanup vtd_{do_}iommu_translate()")
---
 hw/i386/intel_iommu.c          | 2 +-
 hw/i386/intel_iommu_internal.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/i386/intel_iommu.c +1 -1
index 88dc042..392da45 100644
@@ -1141,7 +1141,7 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     if (vtd_ce_get_type(&ce) == VTD_CONTEXT_TT_PASS_THROUGH) {
         entry->iova = addr & VTD_PAGE_MASK;
         entry->translated_addr = entry->iova;
-        entry->addr_mask = VTD_PAGE_MASK;
+        entry->addr_mask = ~VTD_PAGE_MASK;
         entry->perm = IOMMU_RW;
         trace_vtd_translate_pt(source_id, entry->iova);
hw/i386/intel_iommu_internal.h +0 -0
index f50ecd8..754cf8a 100644
@@ -384,7 +384,7 @@ typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo;
 /* Pagesize of VTD paging structures, including root and context tables */
Quoted 8 lines

hw/i386/intel_iommu_internal.h typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo; · line 390

Quoted 6 lines
hw/i386/intel_iommu_internal.h +1 -1
 #define VTD_PAGE_SHIFT              12
 #define VTD_PAGE_SIZE               (1ULL << VTD_PAGE_SHIFT)
-#define VTD_PAGE_MASK               (VTD_PAGE_SIZE - 1)
+#define VTD_PAGE_MASK               (~(VTD_PAGE_SIZE - 1))
 #define VTD_PAGE_SHIFT_4K           12
 #define VTD_PAGE_MASK_4K            (~((1ULL << VTD_PAGE_SHIFT_4K) - 1))

I wonder whether or not using VTD_PAGE_MASK_4K is more than enough.

Sure. Let me remove VTD_PAGE_SIZE/MASK. Thanks.

Signature · 1 line
Peter Xu
author
#6 lore raw

Re: [Qemu-devel] [PATCH v3 2/4] intel_iommu: let iotlb size tunable

In reply to Michael S. Tsirkin
Quoted 11 lines · Michael S. Tsirkin
Quoted 7 lines · Peter Xu

We were having static IOTLB size as 1024. Let it be a tunable. We can also turns IOTLB off if we want, by specify the size as zero.

The tunable is named as "x-iotlb-size" since that should not really be something used by user yet, but mostly for debugging purpose now.

I'm somewhat vary of adding tunables just for developers to play with. People will find and abuse them :)

I would think it convenient (especially with "x-") and I would never worry about people using it (I really see nothing dangerous...). But if you don't like it, I can drop it in next version for sure.

Thanks,

Signature · 1 line
Peter Xu
author
#10 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] intel_iommu: implement mru list for iotlb

In reply to Jason Wang
Quoted 15 lines · Jason Wang
Quoted 12 lines · Peter Xu

It is not wise to disgard all the IOTLB cache when cache size reaches max, but that's what we do now. A slightly better (but still simple) way to do this is, we just throw away the least recent used cache entry.

This patch implemented MRU list algorithm for VT-d IOTLB. The main logic is to maintain a Most Recently Used list for the IOTLB entries. The hash table is still used for lookup, though a new list field is added to each IOTLB entry for a iotlb MRU list. For each active IOTLB entry, it's both in the hash table in s->iotlb, and also linked into the MRU list of s->iotlb_head. The hash helps in fast lookup, and the MRU list helps in knowing whether the cache is still hot.

Quoted 4 lines
Quoted 2 lines

After we have such a MRU list, replacing all the iterators of IOTLB entries by using list iterations rather than hash table iterations.

Any reason of doing this, I thought hashtable was even a little bit faster?

Could I ask why?

I thought they are merely the same (when iterating all the items)?

Signature · 1 line
Peter Xu
author
#16 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] intel_iommu: implement mru list for iotlb

In reply to Michael S. Tsirkin
Quoted 6 lines · Michael S. Tsirkin
Quoted 3 lines · Peter Xu

It is not wise to disgard all the IOTLB cache when cache size reaches max, but that's what we do now. A slightly better (but still simple) way to do this is, we just throw away the least recent used cache entry.

Not wise how? Slower? It seems simpler.

When the cache is full, the cached items are still valid, but we just throw them away. I agree it is definitely simpler, but is that really wise?

Quoted 11 lines
Quoted 8 lines

This patch implemented MRU list algorithm for VT-d IOTLB. The main logic is to maintain a Most Recently Used list for the IOTLB entries. The hash table is still used for lookup, though a new list field is added to each IOTLB entry for a iotlb MRU list. For each active IOTLB entry, it's both in the hash table in s->iotlb, and also linked into the MRU list of s->iotlb_head. The hash helps in fast lookup, and the MRU list helps in knowing whether the cache is still hot.

Helps how? Faster? What's missing here is a report of the performance impact.

Not faster, the hash is already there. This patch only tries to fix the thing I mentioned above (not throwing valid entries when cache full). As far as I tested (all simple ones), it does not have performance boost since it's not easy to trigger cache full (our cache size is 1024, not to mention that current intel iommu driver will do very rapid global flush). I just think it makes sense, and maybe it can help in the future when there are no that frequent iotlb flushes.

If we still require performance numbers to merge this patch, I think we can put this patch aside, until one day we might think it useful.

Thanks,

Signature · 1 line
Peter Xu
#11 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] intel_iommu: implement mru list for iotlb

In reply to Peter Xu
Quoted 15 lines · Peter Xu
Quoted 14 lines · Jason Wang
Quoted 12 lines · Peter Xu

It is not wise to disgard all the IOTLB cache when cache size reaches max, but that's what we do now. A slightly better (but still simple) way to do this is, we just throw away the least recent used cache entry.

This patch implemented MRU list algorithm for VT-d IOTLB. The main logic is to maintain a Most Recently Used list for the IOTLB entries. The hash table is still used for lookup, though a new list field is added to each IOTLB entry for a iotlb MRU list. For each active IOTLB entry, it's both in the hash table in s->iotlb, and also linked into the MRU list of s->iotlb_head. The hash helps in fast lookup, and the MRU list helps in knowing whether the cache is still hot.

Quoted 7 lines
Quoted 3 lines
Quoted 2 lines

After we have such a MRU list, replacing all the iterators of IOTLB entries by using list iterations rather than hash table iterations.

Any reason of doing this, I thought hashtable was even a little bit faster?

Could I ask why?

I thought they are merely the same (when iterating all the items)?

Ok, looks like I was wrong, but it they are merely the same, why bother?

Thanks

author
#12 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] intel_iommu: implement mru list for iotlb

Quoted 20 lines · Jason Wang
Quoted 17 lines · Peter Xu
Quoted 16 lines · Jason Wang
Quoted 14 lines · Peter Xu

It is not wise to disgard all the IOTLB cache when cache size reaches max, but that's what we do now. A slightly better (but still simple) way to do this is, we just throw away the least recent used cache entry.

This patch implemented MRU list algorithm for VT-d IOTLB. The main logic is to maintain a Most Recently Used list for the IOTLB entries. The hash table is still used for lookup, though a new list field is added to each IOTLB entry for a iotlb MRU list. For each active IOTLB entry, it's both in the hash table in s->iotlb, and also linked into the MRU list of s->iotlb_head. The hash helps in fast lookup, and the MRU list helps in knowing whether the cache is still hot.

After we have such a MRU list, replacing all the iterators of IOTLB entries by using list iterations rather than hash table iterations.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Any reason of doing this, I thought hashtable was even a little bit faster?

Could I ask why?

I thought they are merely the same (when iterating all the items)?

Ok, looks like I was wrong, but it they are merely the same, why bother?

Because imho looping over list needs fewer LOCs and is also more direct. E.g., for domain flush, hash needs this:

static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
gpointer user_data)

{
    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
    uint16_t domain_id = *(uint16_t *)user_data;
    return entry->domain_id == domain_id;
}

Then:

    g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
                                &domain_id);

For list it is only:

    FOREACH_IOTLB_SAFE(entry, s, entry_n) {
        if (entry->domain_id == domain_id) {
            vtd_iotlb_remove_entry(s, entry);
        }
    }

Thanks,

Signature · 1 line
Peter Xu
#13 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] intel_iommu: implement mru list for iotlb

Quoted 47 lines · Peter Xu
Quoted 20 lines · Jason Wang
Quoted 17 lines · Peter Xu
Quoted 16 lines · Jason Wang
Quoted 14 lines · Peter Xu

It is not wise to disgard all the IOTLB cache when cache size reaches max, but that's what we do now. A slightly better (but still simple) way to do this is, we just throw away the least recent used cache entry.

This patch implemented MRU list algorithm for VT-d IOTLB. The main logic is to maintain a Most Recently Used list for the IOTLB entries. The hash table is still used for lookup, though a new list field is added to each IOTLB entry for a iotlb MRU list. For each active IOTLB entry, it's both in the hash table in s->iotlb, and also linked into the MRU list of s->iotlb_head. The hash helps in fast lookup, and the MRU list helps in knowing whether the cache is still hot.

After we have such a MRU list, replacing all the iterators of IOTLB entries by using list iterations rather than hash table iterations.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Any reason of doing this, I thought hashtable was even a little bit faster?

Could I ask why?

I thought they are merely the same (when iterating all the items)?

Ok, looks like I was wrong, but it they are merely the same, why bother?

Because imho looping over list needs fewer LOCs and is also more direct. E.g., for domain flush, hash needs this:

static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
gpointer user_data)

{
    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
    uint16_t domain_id = *(uint16_t *)user_data;
    return entry->domain_id == domain_id;
}

Then:

    g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
                                &domain_id);

For list it is only:

Quoted 7 lines
    FOREACH_IOTLB_SAFE(entry, s, entry_n) {
        if (entry->domain_id == domain_id) {
            vtd_iotlb_remove_entry(s, entry);
        }
    }

Thanks,

Well the LOC seems to have gone up with this patch. If we are trying to simplify code, please state this in commit log.

Quoted 2 lines
Signature · 1 line
Peter Xu
author
#14 lore raw

Re: [Qemu-devel] [PATCH v3 4/4] intel_iommu: implement mru list for iotlb

Quoted 54 lines · Michael S. Tsirkin
Quoted 53 lines · Peter Xu
Quoted 20 lines · Jason Wang
Quoted 17 lines · Peter Xu
Quoted 16 lines · Jason Wang
Quoted 14 lines · Peter Xu

It is not wise to disgard all the IOTLB cache when cache size reaches max, but that's what we do now. A slightly better (but still simple) way to do this is, we just throw away the least recent used cache entry.

This patch implemented MRU list algorithm for VT-d IOTLB. The main logic is to maintain a Most Recently Used list for the IOTLB entries. The hash table is still used for lookup, though a new list field is added to each IOTLB entry for a iotlb MRU list. For each active IOTLB entry, it's both in the hash table in s->iotlb, and also linked into the MRU list of s->iotlb_head. The hash helps in fast lookup, and the MRU list helps in knowing whether the cache is still hot.

After we have such a MRU list, replacing all the iterators of IOTLB entries by using list iterations rather than hash table iterations.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Any reason of doing this, I thought hashtable was even a little bit faster?

Could I ask why?

I thought they are merely the same (when iterating all the items)?

Ok, looks like I was wrong, but it they are merely the same, why bother?

Because imho looping over list needs fewer LOCs and is also more direct. E.g., for domain flush, hash needs this:

static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
gpointer user_data)

{
    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
    uint16_t domain_id = *(uint16_t *)user_data;
    return entry->domain_id == domain_id;
}

Then:

    g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
                                &domain_id);

For list it is only:

    FOREACH_IOTLB_SAFE(entry, s, entry_n) {
        if (entry->domain_id == domain_id) {
            vtd_iotlb_remove_entry(s, entry);
        }
    }
Quoted 5 lines
Quoted 1 line

Thanks,

Well the LOC seems to have gone up with this patch. If we are trying to simplify code, please state this in commit log.

Ok. I can split the patch into two (one to introduce MRU list, one to replace the iterations of hash tables) if it will be needed in the future. Currently since we don't have plan to merge it, I'll put it aside for now. Thanks.

Signature · 1 line
Peter Xu