Skip to contentSkip to conversation

aspeed/hace: Fix mapped address may not be unmapped issue

PATCH Superseded Kane Chen 3 participants 9 messages May 8 – May 12 Arm/Aspeed machine support

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Kane Chen

author
#0 lore raw

cover aspeed/hace: Fix mapped address may not be unmapped issue

Dear reviewer,

The Aspeed HACE hash flow may leave DMA mappings active when an error occurs after guest buffers have already been mapped.

Several failure paths return early without unmapping previously mapped iov entries. This can lead to stale mappings and resource leaks.

Fix this by introducing a common cleanup helper that unmaps all previously mapped iov entries before returning from error paths.

Additionally, track the actual mapped length of each iov entry explicitly. This is necessary because some hashing flows may apply padding during processing, and the resulting buffer length used later in the flow can differ from the original DMA mapped length. Using the modified length during dma_memory_unmap() may result in an incorrect unmap size.

Note: this patch series is based on
https://patchew.org/QEMU/[email protected]/.

Best Regards,
Kane

Kane-Chen-AS (1):
aspeed/hace: Fix mapped address may not be unmapped issue

 hw/misc/aspeed_hace.c | 63 ++++++++++++++++++++++++++++++-------------
 1 file changed, 45 insertions(+), 18 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/1 aspeed/hace: Fix mapped address may not be unmapped issue

In the do_hash_operation, the code may be returned earlier because hash_prepare_sg_iov or hash_prepare_direct_iov may return a failure. When this condition is happened, current code flow doesn't go through later code segments. Finally, it causes the mapped address isn't unmapped properly.

This change is intended to unmap the mapped address if there is any error is occurred. Thus, it could prevent a reasoure leak situation.

---
 hw/misc/aspeed_hace.c | 63 ++++++++++++++++++++++++++++++-------------
 1 file changed, 45 insertions(+), 18 deletions(-)
hw/misc/aspeed_hace.c +45 -18
index 0504d61cbf..a322905cb3 100644
@@ -218,8 +218,19 @@ static bool hash_accumulate_len(AspeedHACEState *s, hwaddr plen)
     return true;
 }
 
+static void hash_iov_unmap(AspeedHACEState *s, struct iovec *iov,
+                           hwaddr *mapped_lens, int iov_count)
+{
+    for (; iov_count > 0; iov_count--) {
+        address_space_unmap(&s->dram_as, iov[iov_count - 1].iov_base,
+                            mapped_lens[iov_count - 1], false,
+                            mapped_lens[iov_count - 1]);
+    }
+}
+
 static int hash_prepare_direct_iov(AspeedHACEState *s, struct iovec *iov,
-                                   bool acc_mode, bool *acc_final_request)
+                                   bool acc_mode, bool *acc_final_request,
+                                   hwaddr *mapped_lens)
 {
     uint32_t total_msg_len;
     uint32_t pad_offset;
@@ -243,9 +254,11 @@ static int hash_prepare_direct_iov(AspeedHACEState *s, struct iovec *iov,
 
     iov[0].iov_base = haddr;
     iov_idx = 1;
+    mapped_lens[0] = plen;
 
     if (acc_mode) {
         if (!hash_accumulate_len(s, plen)) {
+            hash_iov_unmap(s, iov, mapped_lens, 1);
             return -1;
         }
 
@@ -265,7 +278,8 @@ static int hash_prepare_direct_iov(AspeedHACEState *s, struct iovec *iov,
 }
 
 static int hash_prepare_sg_iov(AspeedHACEState *s, struct iovec *iov,
-                               bool acc_mode, bool *acc_final_request)
+                               bool acc_mode, bool *acc_final_request,
+                               hwaddr *mapped_lens)
 {
     uint32_t total_msg_len;
     uint32_t pad_offset;
@@ -275,6 +289,7 @@ static int hash_prepare_sg_iov(AspeedHACEState *s, struct iovec *iov,
     int iov_idx;
     hwaddr plen;
     void *haddr;
+    int iov_mapped = 0;
 
     src = hash_get_source_addr(s);
     for (iov_idx = 0; !(len & SG_LIST_LEN_LAST); iov_idx++) {
@@ -282,7 +297,7 @@ static int hash_prepare_sg_iov(AspeedHACEState *s, struct iovec *iov,
             qemu_log_mask(LOG_GUEST_ERROR,
                           "%s: Failed to set end of sg list marker\n",
                           __func__);
-            return -1;
+            goto fail;
         }
 
         len = address_space_ldl_le(&s->dram_as, src,
@@ -307,15 +322,17 @@ static int hash_prepare_sg_iov(AspeedHACEState *s, struct iovec *iov,
                           "%s: Unable to map address, sg_addr=0x%x, "
                           "plen=0x%" HWADDR_PRIx "\n",
                           __func__, sg_addr, plen);
-            return -1;
+            goto fail;
         }
 
         src += SG_LIST_ENTRY_SIZE;
 
         iov[iov_idx].iov_base = haddr;
+        iov_mapped = iov_idx + 1;
+        mapped_lens[iov_idx] = plen;
         if (acc_mode) {
             if (!hash_accumulate_len(s, plen)) {
-                return -1;
+                goto fail;
             }
 
             if (has_padding(s, &iov[iov_idx], plen, &total_msg_len,
@@ -332,6 +349,10 @@ static int hash_prepare_sg_iov(AspeedHACEState *s, struct iovec *iov,
     }
 
     return iov_idx;
+
+fail:
+    hash_iov_unmap(s, iov, mapped_lens, iov_mapped);
+    return -1;
 }
 
 static uint64_t hash_get_digest_addr(AspeedHACEState *s)
@@ -350,6 +371,7 @@ static uint64_t hash_get_digest_addr(AspeedHACEState *s)
 static void hash_write_digest_and_unmap_iov(AspeedHACEState *s,
                                             struct iovec *iov,
                                             int iov_idx,
+                                            hwaddr *mapped_lens,
                                             uint8_t *digest_buf,
                                             size_t digest_len)
 {
@@ -369,15 +391,12 @@ static void hash_write_digest_and_unmap_iov(AspeedHACEState *s,
         hace_hexdump("digest", (char *)digest_buf, digest_len);
     }
 
-    for (; iov_idx > 0; iov_idx--) {
-        address_space_unmap(&s->dram_as, iov[iov_idx - 1].iov_base,
-                            iov[iov_idx - 1].iov_len, false,
-                            iov[iov_idx - 1].iov_len);
-    }
+    hash_iov_unmap(s, iov, mapped_lens, iov_idx);
 }
 
 static void hash_execute_non_acc_mode(AspeedHACEState *s, int algo,
-                                      struct iovec *iov, int iov_idx)
+                                      struct iovec *iov, int iov_idx,
+                                      hwaddr *mapped_lens)
 {
     g_autofree uint8_t *digest_buf = NULL;
     Error *local_err = NULL;
@@ -389,15 +408,17 @@ static void hash_execute_non_acc_mode(AspeedHACEState *s, int algo,
                       "%s: qcrypto hash bytesv failed : %s",
                       __func__, error_get_pretty(local_err));
         error_free(local_err);
+        hash_iov_unmap(s, iov, mapped_lens, iov_idx);
         return;
     }
 
-    hash_write_digest_and_unmap_iov(s, iov, iov_idx, digest_buf, digest_len);
+    hash_write_digest_and_unmap_iov(s, iov, iov_idx, mapped_lens,
+                                    digest_buf, digest_len);
 }
 
 static void hash_execute_acc_mode(AspeedHACEState *s, int algo,
                                   struct iovec *iov, int iov_idx,
-                                  bool final_request)
+                                  bool final_request, hwaddr *mapped_lens)
 {
     g_autofree uint8_t *digest_buf = NULL;
     Error *local_err = NULL;
@@ -411,6 +432,7 @@ static void hash_execute_acc_mode(AspeedHACEState *s, int algo,
             qemu_log_mask(LOG_GUEST_ERROR, "%s: qcrypto hash new failed : %s",
                           __func__, error_get_pretty(local_err));
             error_free(local_err);
+            hash_iov_unmap(s, iov, mapped_lens, iov_idx);
             return;
         }
     }
@@ -419,6 +441,7 @@ static void hash_execute_acc_mode(AspeedHACEState *s, int algo,
         qemu_log_mask(LOG_GUEST_ERROR, "%s: qcrypto hash updatev failed : %s",
                       __func__, error_get_pretty(local_err));
         error_free(local_err);
+        hash_iov_unmap(s, iov, mapped_lens, iov_idx);
         return;
     }
 
@@ -438,22 +461,25 @@ static void hash_execute_acc_mode(AspeedHACEState *s, int algo,
         s->total_req_len = 0;
     }
 
-    hash_write_digest_and_unmap_iov(s, iov, iov_idx, digest_buf, digest_len);
+    hash_write_digest_and_unmap_iov(s, iov, iov_idx, mapped_lens,
+                                    digest_buf, digest_len);
 }
 
 static void do_hash_operation(AspeedHACEState *s, int algo, bool sg_mode,
                               bool acc_mode)
 {
     QEMU_UNINITIALIZED struct iovec iov[ASPEED_HACE_MAX_SG];
+    hwaddr mapped_lens[ASPEED_HACE_MAX_SG] = { 0 };
     bool acc_final_request = false;
     int iov_idx = -1;
 
     /* Prepares the iov for hashing operations based on the selected mode */
     if (sg_mode) {
-        iov_idx = hash_prepare_sg_iov(s, iov, acc_mode, &acc_final_request);
+        iov_idx = hash_prepare_sg_iov(s, iov, acc_mode, &acc_final_request,
+                                      mapped_lens);
     } else {
         iov_idx = hash_prepare_direct_iov(s, iov, acc_mode,
-                                          &acc_final_request);
+                                          &acc_final_request, mapped_lens);
     }
 
     if (iov_idx <= 0) {
@@ -468,9 +494,10 @@ static void do_hash_operation(AspeedHACEState *s, int algo, bool sg_mode,
 
     /* Executes the hash operation */
     if (acc_mode) {
-        hash_execute_acc_mode(s, algo, iov, iov_idx, acc_final_request);
+        hash_execute_acc_mode(s, algo, iov, iov_idx, acc_final_request,
+                              mapped_lens);
     } else {
-        hash_execute_non_acc_mode(s, algo, iov, iov_idx);
+        hash_execute_non_acc_mode(s, algo, iov, iov_idx, mapped_lens);
     }
 }
 
Signature · 1 line
2.43.0
#2 lore raw
Quoted 8 lines · Kane Chen

In the do_hash_operation, the code may be returned earlier because hash_prepare_sg_iov or hash_prepare_direct_iov may return a failure. When this condition is happened, current code flow doesn't go through later code segments. Finally, it causes the mapped address isn't unmapped properly.

This change is intended to unmap the mapped address if there is any error is occurred. Thus, it could prevent a reasoure leak situation.

May be change that last paragraph to :

This change unmaps any mapped addresses when an error occurs, preventing a resource leak.

The change looks correct. An hack from an HACE-aware engineer would be appreciated.

Thanks,

C.

Quoted 206 lines
---
  hw/misc/aspeed_hace.c | 63 ++++++++++++++++++++++++++++++-------------
  1 file changed, 45 insertions(+), 18 deletions(-)
hw/misc/aspeed_hace.c +45 -18
index 0504d61cbf..a322905cb3 100644
@@ -218,8 +218,19 @@ static bool hash_accumulate_len(AspeedHACEState *s, hwaddr plen)
      return true;
  }
 
+static void hash_iov_unmap(AspeedHACEState *s, struct iovec *iov,
+                           hwaddr *mapped_lens, int iov_count)
+{
+    for (; iov_count > 0; iov_count--) {
+        address_space_unmap(&s->dram_as, iov[iov_count - 1].iov_base,
+                            mapped_lens[iov_count - 1], false,
+                            mapped_lens[iov_count - 1]);
+    }
+}
+
  static int hash_prepare_direct_iov(AspeedHACEState *s, struct iovec *iov,
-                                   bool acc_mode, bool *acc_final_request)
+                                   bool acc_mode, bool *acc_final_request,
+                                   hwaddr *mapped_lens)
  {
      uint32_t total_msg_len;
      uint32_t pad_offset;
@@ -243,9 +254,11 @@ static int hash_prepare_direct_iov(AspeedHACEState *s, struct iovec *iov,
 
      iov[0].iov_base = haddr;
      iov_idx = 1;
+    mapped_lens[0] = plen;
 
      if (acc_mode) {
          if (!hash_accumulate_len(s, plen)) {
+            hash_iov_unmap(s, iov, mapped_lens, 1);
              return -1;
          }
 
@@ -265,7 +278,8 @@ static int hash_prepare_direct_iov(AspeedHACEState *s, struct iovec *iov,
  }
 
  static int hash_prepare_sg_iov(AspeedHACEState *s, struct iovec *iov,
-                               bool acc_mode, bool *acc_final_request)
+                               bool acc_mode, bool *acc_final_request,
+                               hwaddr *mapped_lens)
  {
      uint32_t total_msg_len;
      uint32_t pad_offset;
@@ -275,6 +289,7 @@ static int hash_prepare_sg_iov(AspeedHACEState *s, struct iovec *iov,
      int iov_idx;
      hwaddr plen;
      void *haddr;
+    int iov_mapped = 0;
 
      src = hash_get_source_addr(s);
      for (iov_idx = 0; !(len & SG_LIST_LEN_LAST); iov_idx++) {
@@ -282,7 +297,7 @@ static int hash_prepare_sg_iov(AspeedHACEState *s, struct iovec *iov,
              qemu_log_mask(LOG_GUEST_ERROR,
                            "%s: Failed to set end of sg list marker\n",
                            __func__);
-            return -1;
+            goto fail;
          }
 
          len = address_space_ldl_le(&s->dram_as, src,
@@ -307,15 +322,17 @@ static int hash_prepare_sg_iov(AspeedHACEState *s, struct iovec *iov,
                            "%s: Unable to map address, sg_addr=0x%x, "
                            "plen=0x%" HWADDR_PRIx "\n",
                            __func__, sg_addr, plen);
-            return -1;
+            goto fail;
          }
 
          src += SG_LIST_ENTRY_SIZE;
 
          iov[iov_idx].iov_base = haddr;
+        iov_mapped = iov_idx + 1;
+        mapped_lens[iov_idx] = plen;
          if (acc_mode) {
              if (!hash_accumulate_len(s, plen)) {
-                return -1;
+                goto fail;
              }
 
              if (has_padding(s, &iov[iov_idx], plen, &total_msg_len,
@@ -332,6 +349,10 @@ static int hash_prepare_sg_iov(AspeedHACEState *s, struct iovec *iov,
      }
 
      return iov_idx;
+
+fail:
+    hash_iov_unmap(s, iov, mapped_lens, iov_mapped);
+    return -1;
  }
 
  static uint64_t hash_get_digest_addr(AspeedHACEState *s)
@@ -350,6 +371,7 @@ static uint64_t hash_get_digest_addr(AspeedHACEState *s)
  static void hash_write_digest_and_unmap_iov(AspeedHACEState *s,
                                              struct iovec *iov,
                                              int iov_idx,
+                                            hwaddr *mapped_lens,
                                              uint8_t *digest_buf,
                                              size_t digest_len)
  {
@@ -369,15 +391,12 @@ static void hash_write_digest_and_unmap_iov(AspeedHACEState *s,
          hace_hexdump("digest", (char *)digest_buf, digest_len);
      }
 
-    for (; iov_idx > 0; iov_idx--) {
-        address_space_unmap(&s->dram_as, iov[iov_idx - 1].iov_base,
-                            iov[iov_idx - 1].iov_len, false,
-                            iov[iov_idx - 1].iov_len);
-    }
+    hash_iov_unmap(s, iov, mapped_lens, iov_idx);
  }
 
  static void hash_execute_non_acc_mode(AspeedHACEState *s, int algo,
-                                      struct iovec *iov, int iov_idx)
+                                      struct iovec *iov, int iov_idx,
+                                      hwaddr *mapped_lens)
  {
      g_autofree uint8_t *digest_buf = NULL;
      Error *local_err = NULL;
@@ -389,15 +408,17 @@ static void hash_execute_non_acc_mode(AspeedHACEState *s, int algo,
                        "%s: qcrypto hash bytesv failed : %s",
                        __func__, error_get_pretty(local_err));
          error_free(local_err);
+        hash_iov_unmap(s, iov, mapped_lens, iov_idx);
          return;
      }
 
-    hash_write_digest_and_unmap_iov(s, iov, iov_idx, digest_buf, digest_len);
+    hash_write_digest_and_unmap_iov(s, iov, iov_idx, mapped_lens,
+                                    digest_buf, digest_len);
  }
 
  static void hash_execute_acc_mode(AspeedHACEState *s, int algo,
                                    struct iovec *iov, int iov_idx,
-                                  bool final_request)
+                                  bool final_request, hwaddr *mapped_lens)
  {
      g_autofree uint8_t *digest_buf = NULL;
      Error *local_err = NULL;
@@ -411,6 +432,7 @@ static void hash_execute_acc_mode(AspeedHACEState *s, int algo,
              qemu_log_mask(LOG_GUEST_ERROR, "%s: qcrypto hash new failed : %s",
                            __func__, error_get_pretty(local_err));
              error_free(local_err);
+            hash_iov_unmap(s, iov, mapped_lens, iov_idx);
              return;
          }
      }
@@ -419,6 +441,7 @@ static void hash_execute_acc_mode(AspeedHACEState *s, int algo,
          qemu_log_mask(LOG_GUEST_ERROR, "%s: qcrypto hash updatev failed : %s",
                        __func__, error_get_pretty(local_err));
          error_free(local_err);
+        hash_iov_unmap(s, iov, mapped_lens, iov_idx);
          return;
      }
 
@@ -438,22 +461,25 @@ static void hash_execute_acc_mode(AspeedHACEState *s, int algo,
          s->total_req_len = 0;
      }
 
-    hash_write_digest_and_unmap_iov(s, iov, iov_idx, digest_buf, digest_len);
+    hash_write_digest_and_unmap_iov(s, iov, iov_idx, mapped_lens,
+                                    digest_buf, digest_len);
  }
 
  static void do_hash_operation(AspeedHACEState *s, int algo, bool sg_mode,
                                bool acc_mode)
  {
      QEMU_UNINITIALIZED struct iovec iov[ASPEED_HACE_MAX_SG];
+    hwaddr mapped_lens[ASPEED_HACE_MAX_SG] = { 0 };
      bool acc_final_request = false;
      int iov_idx = -1;
 
      /* Prepares the iov for hashing operations based on the selected mode */
      if (sg_mode) {
-        iov_idx = hash_prepare_sg_iov(s, iov, acc_mode, &acc_final_request);
+        iov_idx = hash_prepare_sg_iov(s, iov, acc_mode, &acc_final_request,
+                                      mapped_lens);
      } else {
          iov_idx = hash_prepare_direct_iov(s, iov, acc_mode,
-                                          &acc_final_request);
+                                          &acc_final_request, mapped_lens);
      }
 
      if (iov_idx <= 0) {
@@ -468,9 +494,10 @@ static void do_hash_operation(AspeedHACEState *s, int algo, bool sg_mode,
 
      /* Executes the hash operation */
      if (acc_mode) {
-        hash_execute_acc_mode(s, algo, iov, iov_idx, acc_final_request);
+        hash_execute_acc_mode(s, algo, iov, iov_idx, acc_final_request,
+                              mapped_lens);
      } else {
-        hash_execute_non_acc_mode(s, algo, iov, iov_idx);
+        hash_execute_non_acc_mode(s, algo, iov, iov_idx, mapped_lens);
      }
  }
 
author
#3 lore raw
Quoted 28 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Monday, May 11, 2026 4:17 PM
To: Kane Chen <[email protected]>; Peter Maydell <[email protected]>; Steven Lee <[email protected]>; Troy Lee <[email protected]>; Jamin Lin <[email protected]>; Andrew Jeffery <[email protected]>; Joel Stanley <[email protected]>; open list:ASPEED BMCs <[email protected]>; open list:All patches CC here <[email protected]>
Cc: Troy Lee <[email protected]>
Subject: Re: [PATCH v1 1/1] aspeed/hace: Fix mapped address may not be unmapped issue

Quoted 8 lines · Kane Chen

In the do_hash_operation, the code may be returned earlier because hash_prepare_sg_iov or hash_prepare_direct_iov may return a failure. When this condition is happened, current code flow doesn't go through later code segments. Finally, it causes the mapped address isn't unmapped properly.

This change is intended to unmap the mapped address if there is any error is occurred. Thus, it could prevent a reasoure leak situation.

May be change that last paragraph to :

This change unmaps any mapped addresses when an error occurs, preventing a resource leak.

Quoted 7 lines

The change looks correct. An hack from an HACE-aware engineer would be appreciated.

Thanks,

C.

Hi Cédric,

Thanks for your review and comments.
Should I submit a v2 patch to incorporate the requested changes to the commit log?

Best Regards,
Kane

#4 lore raw

Hi Kane,

Quoted 4 lines

Hi Cédric,

Thanks for your review and comments.
Should I submit a v2 patch to incorporate the requested changes to the commit log?

Yes please and add Jamin's Rb too.

Thanks,

C.

author
#5 lore raw
Quoted 19 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Tuesday, May 12, 2026 2:37 PM
To: Kane Chen <[email protected]>; Peter Maydell <[email protected]>; Steven Lee <[email protected]>; Troy Lee <[email protected]>; Jamin Lin <[email protected]>; Andrew Jeffery <[email protected]>; Joel Stanley <[email protected]>; open list:ASPEED BMCs <[email protected]>; open list:All patches CC here <[email protected]>
Cc: Troy Lee <[email protected]>
Subject: Re: [PATCH v1 1/1] aspeed/hace: Fix mapped address may not be unmapped issue

Hi Kane,

Quoted 4 lines

Hi Cédric,

Thanks for your review and comments.
Should I submit a v2 patch to incorporate the requested changes to the

Quoted 7 lines

commit log?

Yes please and add Jamin's Rb too.

Thanks,

C.

Hi Cédric,

The v2 patch has been sent, and Jamin's Reviewed-by tag has also been added.

Please help review it. Thank you.

Best Regards,
Kane

Reviewed-by on v1 1/1
#6 lore raw
In reply to Kane Chen

Hi Kane,

Quoted 238 lines

Subject: [PATCH v1 1/1] aspeed/hace: Fix mapped address may not be unmapped issue

In the do_hash_operation, the code may be returned earlier because hash_prepare_sg_iov or hash_prepare_direct_iov may return a failure. When this condition is happened, current code flow doesn't go through later code segments. Finally, it causes the mapped address isn't unmapped properly.

This change is intended to unmap the mapped address if there is any error is occurred. Thus, it could prevent a reasoure leak situation.

---
 hw/misc/aspeed_hace.c | 63 ++++++++++++++++++++++++++++++-------------
 1 file changed, 45 insertions(+), 18 deletions(-)
hw/misc/aspeed_hace.c index renamed from hw/misc/aspeed_hace.c +0 -0

0504d61cbf..a322905cb3 100644

hw/misc/aspeed_hace.c +15 -2
@@ -218,8 +218,19 @@ static bool hash_accumulate_len(AspeedHACEState
 *s, hwaddr plen)
     return true;
 }
 
+static void hash_iov_unmap(AspeedHACEState *s, struct iovec *iov,
+                           hwaddr *mapped_lens, int iov_count) {
+    for (; iov_count > 0; iov_count--) {
+        address_space_unmap(&s->dram_as, iov[iov_count - 1].iov_base,
+                            mapped_lens[iov_count - 1], false,
+                            mapped_lens[iov_count - 1]);
+    }
+}
+
 static int hash_prepare_direct_iov(AspeedHACEState *s, struct iovec *iov,
-                                   bool acc_mode, bool
 *acc_final_request)
+                                   bool acc_mode, bool
 *acc_final_request,
+                                   hwaddr *mapped_lens)
 {
     uint32_t total_msg_len;
     uint32_t pad_offset;
@@ -243,9 +254,11 @@ static int hash_prepare_direct_iov(AspeedHACEState
 *s, struct iovec *iov,
 
     iov[0].iov_base = haddr;
     iov_idx = 1;
+    mapped_lens[0] = plen;
 
     if (acc_mode) {
         if (!hash_accumulate_len(s, plen)) {
+            hash_iov_unmap(s, iov, mapped_lens, 1);
             return -1;
         }
@@ -265,7 +278,8 @@ static int hash_prepare_direct_iov(AspeedHACEState
 *s, struct iovec *iov,  }
 
 static int hash_prepare_sg_iov(AspeedHACEState *s, struct iovec *iov,
-                               bool acc_mode, bool
 *acc_final_request)
+                               bool acc_mode, bool
 *acc_final_request,
+                               hwaddr *mapped_lens)
 {
     uint32_t total_msg_len;
     uint32_t pad_offset;
hw/misc/aspeed_hace.c +1 -0
@@ -275,6 +289,7 @@ static int hash_prepare_sg_iov(AspeedHACEState *s,
 struct iovec *iov,
     int iov_idx;
     hwaddr plen;
     void *haddr;
+    int iov_mapped = 0;
 
     src = hash_get_source_addr(s);
     for (iov_idx = 0; !(len & SG_LIST_LEN_LAST); iov_idx++) { @@ -282,7

+297,7 @@ static int hash_prepare_sg_iov(AspeedHACEState *s, struct iovec *iov,
qemu_log_mask(LOG_GUEST_ERROR,
"%s: Failed to set end of sg list marker\n", __func__);

hw/misc/aspeed_hace.c +1 -1
-            return -1;
+            goto fail;
         }
 
         len = address_space_ldl_le(&s->dram_as, src, @@ -307,15 +322,17

@@ static int hash_prepare_sg_iov(AspeedHACEState *s, struct iovec *iov,

                           "%s: Unable to map address, sg_addr=0x%x, "
                           "plen=0x%" HWADDR_PRIx "\n",
                           __func__, sg_addr, plen);
hw/misc/aspeed_hace.c +4 -3
-            return -1;
+            goto fail;
         }
 
         src += SG_LIST_ENTRY_SIZE;
 
         iov[iov_idx].iov_base = haddr;
+        iov_mapped = iov_idx + 1;
+        mapped_lens[iov_idx] = plen;
         if (acc_mode) {
             if (!hash_accumulate_len(s, plen)) {
-                return -1;
+                goto fail;
             }
 
             if (has_padding(s, &iov[iov_idx], plen, &total_msg_len, @@
-332,6 +349,10 @@ static int hash_prepare_sg_iov(AspeedHACEState *s,

struct iovec *iov,

     }

     return iov_idx;
hw/misc/aspeed_hace.c +5 -0
+
+fail:
+    hash_iov_unmap(s, iov, mapped_lens, iov_mapped);
+    return -1;
 }
 
 static uint64_t hash_get_digest_addr(AspeedHACEState *s) @@ -350,6
+371,7 @@ static uint64_t hash_get_digest_addr(AspeedHACEState *s)

static void hash_write_digest_and_unmap_iov(AspeedHACEState *s,
struct iovec *iov, int iov_idx,
+ hwaddr *mapped_lens,
uint8_t *digest_buf, size_t digest_len)
{ @@ -369,15 +391,12 @@ static void hash_write_digest_and_unmap_iov(AspeedHACEState *s,

         hace_hexdump("digest", (char *)digest_buf, digest_len);
     }
hw/misc/aspeed_hace.c +9 -8
-    for (; iov_idx > 0; iov_idx--) {
-        address_space_unmap(&s->dram_as, iov[iov_idx - 1].iov_base,
-                            iov[iov_idx - 1].iov_len, false,
-                            iov[iov_idx - 1].iov_len);
-    }
+    hash_iov_unmap(s, iov, mapped_lens, iov_idx);
 }
 
 static void hash_execute_non_acc_mode(AspeedHACEState *s, int algo,
-                                      struct iovec *iov, int iov_idx)
+                                      struct iovec *iov, int iov_idx,
+                                      hwaddr *mapped_lens)
 {
     g_autofree uint8_t *digest_buf = NULL;
     Error *local_err = NULL;
@@ -389,15 +408,17 @@ static void
 hash_execute_non_acc_mode(AspeedHACEState *s, int algo,
                       "%s: qcrypto hash bytesv failed : %s",
                       __func__, error_get_pretty(local_err));
         error_free(local_err);
+        hash_iov_unmap(s, iov, mapped_lens, iov_idx);
         return;
     }
 
-    hash_write_digest_and_unmap_iov(s, iov, iov_idx, digest_buf,
 digest_len);
+    hash_write_digest_and_unmap_iov(s, iov, iov_idx, mapped_lens,
+                                    digest_buf, digest_len);
 }
 
 static void hash_execute_acc_mode(AspeedHACEState *s, int algo,
                                   struct iovec *iov, int iov_idx,
-                                  bool final_request)
+                                  bool final_request, hwaddr
+ *mapped_lens)
 {
     g_autofree uint8_t *digest_buf = NULL;
     Error *local_err = NULL;
@@ -411,6 +432,7 @@ static void hash_execute_acc_mode(AspeedHACEState
 *s, int algo,
             qemu_log_mask(LOG_GUEST_ERROR, "%s: qcrypto hash new
 failed : %s",
                           __func__, error_get_pretty(local_err));
             error_free(local_err);
+            hash_iov_unmap(s, iov, mapped_lens, iov_idx);
             return;
         }
     }
hw/misc/aspeed_hace.c +1 -0
@@ -419,6 +441,7 @@ static void hash_execute_acc_mode(AspeedHACEState
 *s, int algo,
         qemu_log_mask(LOG_GUEST_ERROR, "%s: qcrypto hash updatev
 failed : %s",
                       __func__, error_get_pretty(local_err));
         error_free(local_err);
+        hash_iov_unmap(s, iov, mapped_lens, iov_idx);
         return;
     }
hw/misc/aspeed_hace.c +10 -6
@@ -438,22 +461,25 @@ static void
 hash_execute_acc_mode(AspeedHACEState *s, int algo,
         s->total_req_len = 0;
     }
 
-    hash_write_digest_and_unmap_iov(s, iov, iov_idx, digest_buf,
 digest_len);
+    hash_write_digest_and_unmap_iov(s, iov, iov_idx, mapped_lens,
+                                    digest_buf, digest_len);
 }
 
 static void do_hash_operation(AspeedHACEState *s, int algo, bool sg_mode,
                               bool acc_mode)  {
     QEMU_UNINITIALIZED struct iovec iov[ASPEED_HACE_MAX_SG];
+    hwaddr mapped_lens[ASPEED_HACE_MAX_SG] = { 0 };
     bool acc_final_request = false;
     int iov_idx = -1;
 
     /* Prepares the iov for hashing operations based on the selected mode
 */
     if (sg_mode) {
-        iov_idx = hash_prepare_sg_iov(s, iov, acc_mode,
 &acc_final_request);
+        iov_idx = hash_prepare_sg_iov(s, iov, acc_mode,
 &acc_final_request,
+                                      mapped_lens);
     } else {
         iov_idx = hash_prepare_direct_iov(s, iov, acc_mode,
-                                          &acc_final_request);
+                                          &acc_final_request,
+ mapped_lens);
     }
 
     if (iov_idx <= 0) {
@@ -468,9 +494,10 @@ static void do_hash_operation(AspeedHACEState *s,
 int algo, bool sg_mode,
 
     /* Executes the hash operation */
     if (acc_mode) {
-        hash_execute_acc_mode(s, algo, iov, iov_idx, acc_final_request);
+        hash_execute_acc_mode(s, algo, iov, iov_idx, acc_final_request,
+                              mapped_lens);
     } else {
-        hash_execute_non_acc_mode(s, algo, iov, iov_idx);
+        hash_execute_non_acc_mode(s, algo, iov, iov_idx, mapped_lens);
     }
 }
 
--
Quoted 1 line

2.43.0

Thanks,
Jamin

#7 lore raw
In reply to Kane Chen
Quoted 18 lines · Kane Chen

Dear reviewer,

The Aspeed HACE hash flow may leave DMA mappings active when an error occurs after guest buffers have already been mapped.

Several failure paths return early without unmapping previously mapped iov entries. This can lead to stale mappings and resource leaks.

Fix this by introducing a common cleanup helper that unmaps all previously mapped iov entries before returning from error paths.

Additionally, track the actual mapped length of each iov entry explicitly. This is necessary because some hashing flows may apply padding during processing, and the resulting buffer length used later in the flow can differ from the original DMA mapped length. Using the modified length during dma_memory_unmap() may result in an incorrect unmap size.

Quoted 5 lines

Note: this patch series is based on
https://patchew.org/QEMU/[email protected]/.

Best Regards,
Kane

Jamin, I need your help here too.

Thanks,

C.

#8 lore raw

Hi Cédric,

Quoted 22 lines

Subject: Re: [PATCH v1 0/1] aspeed/hace: Fix mapped address may not be unmapped issue

Quoted 18 lines · Kane Chen

Dear reviewer,

The Aspeed HACE hash flow may leave DMA mappings active when an error occurs after guest buffers have already been mapped.

Several failure paths return early without unmapping previously mapped iov entries. This can lead to stale mappings and resource leaks.

Fix this by introducing a common cleanup helper that unmaps all previously mapped iov entries before returning from error paths.

Additionally, track the actual mapped length of each iov entry explicitly. This is necessary because some hashing flows may apply padding during processing, and the resulting buffer length used later in the flow can differ from the original DMA mapped length. Using the modified length during dma_memory_unmap() may result in an incorrect unmap size.

Quoted 7 lines
Quoted 5 lines

Note: this patch series is based on
https://patchew.org/QEMU/[email protected]/.

Best Regards,
Kane

Jamin, I need your help here too.

Looks good to me.
Added review-tag.

Thanks,
Jamin

Quoted 4 lines

Thanks,

C.