Skip to contentSkip to conversation

Don't insert DDT cache in Bare mode

PATCH Superseded Jay Chang 4 participants 9 messages Dec 11, 2025 – May 18 RISC-V

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jay Chang

author
#0 lore raw

cover Don't insert DDT cache in Bare mode

When IOMMU is in Bare mode, DDT (Device Directory Table) lookup is not performed. This series fixes two issues:

1. Avoid inserting unnecessary DDT cache entries in Bare mode
2. Properly free Bare mode contexts to prevent memory leaks

Jay Chang (2):
hw/riscv: Don't insert DDT cache in Bare mode hw/riscv: Refactor riscv_iommu_ctx_put() for Bare mode handling

 hw/riscv/riscv-iommu.c | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)
Signature · 1 line
2.48.1
author
#1 lore raw

PATCH 1/2 hw/riscv: Don't insert DDT cache in Bare mode

In Bare mode the IOMMU does not perform DDT look-ups, therefore caching DDT entries is unnecessary.

---
 hw/riscv/riscv-iommu.c | 23 +++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)
hw/riscv/riscv-iommu.c +15 -8
index 69852f4f13..79eee2e85e 100644
@@ -1312,16 +1312,23 @@ static RISCVIOMMUContext *riscv_iommu_ctx(RISCVIOMMUState *s,
 
     int fault = riscv_iommu_ctx_fetch(s, ctx);
     if (!fault) {
-        if (g_hash_table_size(ctx_cache) >= LIMIT_CACHE_CTX) {
+        if (mode != RISCV_IOMMU_DDTP_MODE_BARE) {
+            if (g_hash_table_size(ctx_cache) >= LIMIT_CACHE_CTX) {
+                g_hash_table_unref(ctx_cache);
+                ctx_cache = g_hash_table_new_full(riscv_iommu_ctx_hash,
+                                                  riscv_iommu_ctx_equal,
+                                                  g_free, NULL);
+                g_hash_table_ref(ctx_cache);
+                g_hash_table_unref(qatomic_xchg(&s->ctx_cache, ctx_cache));
+            }
+
+            g_hash_table_add(ctx_cache, ctx);
+            *ref = ctx_cache;
+        } else {
             g_hash_table_unref(ctx_cache);
-            ctx_cache = g_hash_table_new_full(riscv_iommu_ctx_hash,
-                                              riscv_iommu_ctx_equal,
-                                              g_free, NULL);
-            g_hash_table_ref(ctx_cache);
-            g_hash_table_unref(qatomic_xchg(&s->ctx_cache, ctx_cache));
+            /* Remember ctx so it can be freed */
+            *ref = ctx;
         }
-        g_hash_table_add(ctx_cache, ctx);
-        *ref = ctx_cache;
         return ctx;
     }
 
Signature · 1 line
2.48.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] hw/riscv: Don't insert DDT cache in Bare mode

Quoted 6 lines · Jay Chang

In Bare mode the IOMMU does not perform DDT look-ups, therefore caching DDT entries is unnecessary.

---

Quoted 39 lines
  hw/riscv/riscv-iommu.c | 23 +++++++++++++++--------
  1 file changed, 15 insertions(+), 8 deletions(-)
hw/riscv/riscv-iommu.c +15 -8
index 69852f4f13..79eee2e85e 100644
@@ -1312,16 +1312,23 @@ static RISCVIOMMUContext *riscv_iommu_ctx(RISCVIOMMUState *s,
 
      int fault = riscv_iommu_ctx_fetch(s, ctx);
      if (!fault) {
-        if (g_hash_table_size(ctx_cache) >= LIMIT_CACHE_CTX) {
+        if (mode != RISCV_IOMMU_DDTP_MODE_BARE) {
+            if (g_hash_table_size(ctx_cache) >= LIMIT_CACHE_CTX) {
+                g_hash_table_unref(ctx_cache);
+                ctx_cache = g_hash_table_new_full(riscv_iommu_ctx_hash,
+                                                  riscv_iommu_ctx_equal,
+                                                  g_free, NULL);
+                g_hash_table_ref(ctx_cache);
+                g_hash_table_unref(qatomic_xchg(&s->ctx_cache, ctx_cache));
+            }
+
+            g_hash_table_add(ctx_cache, ctx);
+            *ref = ctx_cache;
+        } else {
              g_hash_table_unref(ctx_cache);
-            ctx_cache = g_hash_table_new_full(riscv_iommu_ctx_hash,
-                                              riscv_iommu_ctx_equal,
-                                              g_free, NULL);
-            g_hash_table_ref(ctx_cache);
-            g_hash_table_unref(qatomic_xchg(&s->ctx_cache, ctx_cache));
+            /* Remember ctx so it can be freed */
+            *ref = ctx;
          }
-        g_hash_table_add(ctx_cache, ctx);
-        *ref = ctx_cache;
          return ctx;
      }
 
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] hw/riscv: Don't insert DDT cache in Bare mode

In reply to Jay Chang
Quoted 6 lines · Jay Chang

In Bare mode the IOMMU does not perform DDT look-ups, therefore caching DDT entries is unnecessary.

---

Thanks,
Nutty

Quoted 39 lines
  hw/riscv/riscv-iommu.c | 23 +++++++++++++++--------
  1 file changed, 15 insertions(+), 8 deletions(-)
hw/riscv/riscv-iommu.c +15 -8
index 69852f4f13..79eee2e85e 100644
@@ -1312,16 +1312,23 @@ static RISCVIOMMUContext *riscv_iommu_ctx(RISCVIOMMUState *s,
 
      int fault = riscv_iommu_ctx_fetch(s, ctx);
      if (!fault) {
-        if (g_hash_table_size(ctx_cache) >= LIMIT_CACHE_CTX) {
+        if (mode != RISCV_IOMMU_DDTP_MODE_BARE) {
+            if (g_hash_table_size(ctx_cache) >= LIMIT_CACHE_CTX) {
+                g_hash_table_unref(ctx_cache);
+                ctx_cache = g_hash_table_new_full(riscv_iommu_ctx_hash,
+                                                  riscv_iommu_ctx_equal,
+                                                  g_free, NULL);
+                g_hash_table_ref(ctx_cache);
+                g_hash_table_unref(qatomic_xchg(&s->ctx_cache, ctx_cache));
+            }
+
+            g_hash_table_add(ctx_cache, ctx);
+            *ref = ctx_cache;
+        } else {
              g_hash_table_unref(ctx_cache);
-            ctx_cache = g_hash_table_new_full(riscv_iommu_ctx_hash,
-                                              riscv_iommu_ctx_equal,
-                                              g_free, NULL);
-            g_hash_table_ref(ctx_cache);
-            g_hash_table_unref(qatomic_xchg(&s->ctx_cache, ctx_cache));
+            /* Remember ctx so it can be freed */
+            *ref = ctx;
          }
-        g_hash_table_add(ctx_cache, ctx);
-        *ref = ctx_cache;
          return ctx;
      }
 
author
#4 lore raw

PATCH 2/2 hw/riscv: Refactor riscv_iommu_ctx_put() for Bare mode handling

In reply to Jay Chang

Align SPEC: Bare mode contexts are not cached, so they require direct memory deallocation via g_free instead of hash table cleanup.

---
 hw/riscv/riscv-iommu.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
hw/riscv/riscv-iommu.c +10 -1
index 79eee2e85e..fca5763858 100644
@@ -1344,7 +1344,16 @@ static RISCVIOMMUContext *riscv_iommu_ctx(RISCVIOMMUState *s,
 
 static void riscv_iommu_ctx_put(RISCVIOMMUState *s, void *ref)
 {
-    if (ref) {
+    unsigned mode = get_field(s->ddtp, RISCV_IOMMU_DDTP_MODE);
+
+    if (!ref) {
+        return;
+    }
+
+    /* ref is pointing to ctx in Bare mode. Bare mode ctx is not cached */
+    if (mode == RISCV_IOMMU_DDTP_MODE_BARE) {
+        g_free(ref);
+    } else {
         g_hash_table_unref((GHashTable *)ref);
     }
 }
Signature · 1 line
2.48.1
Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] hw/riscv: Refactor riscv_iommu_ctx_put() for Bare mode handling

Quoted 6 lines · Jay Chang

Align SPEC: Bare mode contexts are not cached, so they require direct memory deallocation via g_free instead of hash table cleanup.

---

Quoted 25 lines
  hw/riscv/riscv-iommu.c | 11 ++++++++++-
  1 file changed, 10 insertions(+), 1 deletion(-)
hw/riscv/riscv-iommu.c +10 -1
index 79eee2e85e..fca5763858 100644
@@ -1344,7 +1344,16 @@ static RISCVIOMMUContext *riscv_iommu_ctx(RISCVIOMMUState *s,
 
  static void riscv_iommu_ctx_put(RISCVIOMMUState *s, void *ref)
  {
-    if (ref) {
+    unsigned mode = get_field(s->ddtp, RISCV_IOMMU_DDTP_MODE);
+
+    if (!ref) {
+        return;
+    }
+
+    /* ref is pointing to ctx in Bare mode. Bare mode ctx is not cached */
+    if (mode == RISCV_IOMMU_DDTP_MODE_BARE) {
+        g_free(ref);
+    } else {
          g_hash_table_unref((GHashTable *)ref);
      }
  }
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] hw/riscv: Refactor riscv_iommu_ctx_put() for Bare mode handling

In reply to Jay Chang
Quoted 6 lines · Jay Chang

Align SPEC: Bare mode contexts are not cached, so they require direct memory deallocation via g_free instead of hash table cleanup.

---

Thanks,
Nutty

Quoted 25 lines
  hw/riscv/riscv-iommu.c | 11 ++++++++++-
  1 file changed, 10 insertions(+), 1 deletion(-)
hw/riscv/riscv-iommu.c +10 -1
index 79eee2e85e..fca5763858 100644
@@ -1344,7 +1344,16 @@ static RISCVIOMMUContext *riscv_iommu_ctx(RISCVIOMMUState *s,
 
  static void riscv_iommu_ctx_put(RISCVIOMMUState *s, void *ref)
  {
-    if (ref) {
+    unsigned mode = get_field(s->ddtp, RISCV_IOMMU_DDTP_MODE);
+
+    if (!ref) {
+        return;
+    }
+
+    /* ref is pointing to ctx in Bare mode. Bare mode ctx is not cached */
+    if (mode == RISCV_IOMMU_DDTP_MODE_BARE) {
+        g_free(ref);
+    } else {
          g_hash_table_unref((GHashTable *)ref);
      }
  }
author
#7 lore raw
In reply to Jay Chang

Hi,

Gentle ping on this patch.

Thanks,
Jay Chang

Quoted 17 lines · Jay Chang

When IOMMU is in Bare mode, DDT (Device Directory Table) lookup is not performed. This series fixes two issues:

1. Avoid inserting unnecessary DDT cache entries in Bare mode
2. Properly free Bare mode contexts to prevent memory leaks

Jay Chang (2):
hw/riscv: Don't insert DDT cache in Bare mode hw/riscv: Refactor riscv_iommu_ctx_put() for Bare mode handling

 hw/riscv/riscv-iommu.c | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)

--
2.48.1

#8 lore raw
Quoted 4 lines · Jay Chang

Hi,

Gentle ping on this patch.

Can you please resend the series, I lost this the first time around

Alistair

Quoted 22 lines

Thanks,
Jay Chang

Quoted 17 lines · Jay Chang

When IOMMU is in Bare mode, DDT (Device Directory Table) lookup is not performed. This series fixes two issues:

1. Avoid inserting unnecessary DDT cache entries in Bare mode
2. Properly free Bare mode contexts to prevent memory leaks

Jay Chang (2):
hw/riscv: Don't insert DDT cache in Bare mode hw/riscv: Refactor riscv_iommu_ctx_put() for Bare mode handling

 hw/riscv/riscv-iommu.c | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)

--
2.48.1