[PATCH v2] hw/riscv/riscv-iommu: fix entry leak with ioatc-limit=0

Tan Chi posted 1 patch 1 month, 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261005111523.277550-1-tanchi25@mails.ucas.ac.cn
Maintainers: Palmer Dabbelt <palmer@dabbelt.com>, Alistair Francis <alistair.francis@wdc.com>, Weiwei Li <liwei1518@gmail.com>, Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>, Liu Zhiwei <zhiwei_liu@linux.alibaba.com>, Chao Liu <chao.liu@processmission.com>
hw/riscv/riscv-iommu.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH v2] hw/riscv/riscv-iommu: fix entry leak with ioatc-limit=0
Posted by Tan Chi 1 month, 2 weeks ago
riscv_iommu_translate() allocates an IOATC entry before passing it to
riscv_iommu_iot_update(). The update helper normally transfers ownership
to the hash table.

When ioatc-limit is zero, however, the helper returns without inserting or
freeing the entry. Every cacheable non-identity translation therefore leaks
one RISCVIOMMUEntry.

Avoid this by disabling caching at the caller when ioatc-limit is zero,
so riscv_iommu_translate() does not allocate an IOATC entry in the first
place. This avoids special-casing entry freeing in riscv_iommu_iot_update()
and keeps the ownership semantics simpler.

Suggested-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
Fixes: 9d085a1c3cb2 ("hw/riscv/riscv-iommu: add Address Translation Cache (IOATC)")
Cc: qemu-stable@nongnu.org
Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
---
v2:
- Disable caching when ioatc-limit is zero instead of freeing the
  allocated entry in riscv_iommu_iot_update().

 hw/riscv/riscv-iommu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c
index 323a041b4a..866f49cc39 100644
--- a/hw/riscv/riscv-iommu.c
+++ b/hw/riscv/riscv-iommu.c
@@ -2919,7 +2919,8 @@ static IOMMUTLBEntry riscv_iommu_memory_region_translate(
         /* Translation disabled or invalid. */
         iotlb.addr_mask = 0;
         iotlb.perm = IOMMU_NONE;
-    } else if (riscv_iommu_translate(as->iommu, ctx, &iotlb, true)) {
+    } else if (riscv_iommu_translate(as->iommu, ctx, &iotlb,
+                                     as->iommu->iot_limit > 0)) {
         /* Translation disabled or fault reported. */
         iotlb.addr_mask = 0;
         iotlb.perm = IOMMU_NONE;
-- 
2.53.0
Re: [PATCH v2] hw/riscv/riscv-iommu: fix entry leak with ioatc-limit=0
Posted by Alistair Francis 1 month ago
On Mon, 2026-10-05 at 19:15 +0800, Tan Chi wrote:
> riscv_iommu_translate() allocates an IOATC entry before passing it to
> riscv_iommu_iot_update(). The update helper normally transfers
> ownership
> to the hash table.
> 
> When ioatc-limit is zero, however, the helper returns without
> inserting or
> freeing the entry. Every cacheable non-identity translation therefore
> leaks
> one RISCVIOMMUEntry.
> 
> Avoid this by disabling caching at the caller when ioatc-limit is
> zero,
> so riscv_iommu_translate() does not allocate an IOATC entry in the
> first
> place. This avoids special-casing entry freeing in
> riscv_iommu_iot_update()
> and keeps the ownership semantics simpler.
> 
> Suggested-by: Daniel Henrique Barboza
> <daniel.barboza@oss.qualcomm.com>
> Fixes: 9d085a1c3cb2 ("hw/riscv/riscv-iommu: add Address Translation
> Cache (IOATC)")
> Cc: qemu-stable@nongnu.org
> Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>

Thanks!

Applied to riscv-to-apply.next

Alistair

> ---
> v2:
> - Disable caching when ioatc-limit is zero instead of freeing the
>   allocated entry in riscv_iommu_iot_update().
> 
>  hw/riscv/riscv-iommu.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c
> index 323a041b4a..866f49cc39 100644
> --- a/hw/riscv/riscv-iommu.c
> +++ b/hw/riscv/riscv-iommu.c
> @@ -2919,7 +2919,8 @@ static IOMMUTLBEntry
> riscv_iommu_memory_region_translate(
>          /* Translation disabled or invalid. */
>          iotlb.addr_mask = 0;
>          iotlb.perm = IOMMU_NONE;
> -    } else if (riscv_iommu_translate(as->iommu, ctx, &iotlb, true))
> {
> +    } else if (riscv_iommu_translate(as->iommu, ctx, &iotlb,
> +                                     as->iommu->iot_limit > 0)) {
>          /* Translation disabled or fault reported. */
>          iotlb.addr_mask = 0;
>          iotlb.perm = IOMMU_NONE;
Re: [PATCH v2] hw/riscv/riscv-iommu: fix entry leak with ioatc-limit=0
Posted by Nutty.Liu 1 month, 2 weeks ago
On 10/5/2026 7:15 PM, Tan Chi wrote:
> riscv_iommu_translate() allocates an IOATC entry before passing it to
> riscv_iommu_iot_update(). The update helper normally transfers ownership
> to the hash table.
>
> When ioatc-limit is zero, however, the helper returns without inserting or
> freeing the entry. Every cacheable non-identity translation therefore leaks
> one RISCVIOMMUEntry.
>
> Avoid this by disabling caching at the caller when ioatc-limit is zero,
> so riscv_iommu_translate() does not allocate an IOATC entry in the first
> place. This avoids special-casing entry freeing in riscv_iommu_iot_update()
> and keeps the ownership semantics simpler.
>
> Suggested-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
> Fixes: 9d085a1c3cb2 ("hw/riscv/riscv-iommu: add Address Translation Cache (IOATC)")
> Cc: qemu-stable@nongnu.org
> Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
Reviewed-by: Nutty Liu <nutty.liu@hotmail.com>

Thanks,
Nutty
> ---
> v2:
> - Disable caching when ioatc-limit is zero instead of freeing the
>    allocated entry in riscv_iommu_iot_update().
>
>   hw/riscv/riscv-iommu.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c
> index 323a041b4a..866f49cc39 100644
> --- a/hw/riscv/riscv-iommu.c
> +++ b/hw/riscv/riscv-iommu.c
> @@ -2919,7 +2919,8 @@ static IOMMUTLBEntry riscv_iommu_memory_region_translate(
>           /* Translation disabled or invalid. */
>           iotlb.addr_mask = 0;
>           iotlb.perm = IOMMU_NONE;
> -    } else if (riscv_iommu_translate(as->iommu, ctx, &iotlb, true)) {
> +    } else if (riscv_iommu_translate(as->iommu, ctx, &iotlb,
> +                                     as->iommu->iot_limit > 0)) {
>           /* Translation disabled or fault reported. */
>           iotlb.addr_mask = 0;
>           iotlb.perm = IOMMU_NONE;
Re: [PATCH v2] hw/riscv/riscv-iommu: fix entry leak with ioatc-limit=0
Posted by Daniel Henrique Barboza 1 month, 2 weeks ago

On 10/5/2026 8:15 AM, Tan Chi wrote:
> riscv_iommu_translate() allocates an IOATC entry before passing it to
> riscv_iommu_iot_update(). The update helper normally transfers ownership
> to the hash table.
> 
> When ioatc-limit is zero, however, the helper returns without inserting or
> freeing the entry. Every cacheable non-identity translation therefore leaks
> one RISCVIOMMUEntry.
> 
> Avoid this by disabling caching at the caller when ioatc-limit is zero,
> so riscv_iommu_translate() does not allocate an IOATC entry in the first
> place. This avoids special-casing entry freeing in riscv_iommu_iot_update()
> and keeps the ownership semantics simpler.
> 
> Suggested-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
> Fixes: 9d085a1c3cb2 ("hw/riscv/riscv-iommu: add Address Translation Cache (IOATC)")
> Cc: qemu-stable@nongnu.org
> Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
> ---

Reviewed-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>

> v2:
> - Disable caching when ioatc-limit is zero instead of freeing the
>    allocated entry in riscv_iommu_iot_update().
> 
>   hw/riscv/riscv-iommu.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c
> index 323a041b4a..866f49cc39 100644
> --- a/hw/riscv/riscv-iommu.c
> +++ b/hw/riscv/riscv-iommu.c
> @@ -2919,7 +2919,8 @@ static IOMMUTLBEntry riscv_iommu_memory_region_translate(
>           /* Translation disabled or invalid. */
>           iotlb.addr_mask = 0;
>           iotlb.perm = IOMMU_NONE;
> -    } else if (riscv_iommu_translate(as->iommu, ctx, &iotlb, true)) {
> +    } else if (riscv_iommu_translate(as->iommu, ctx, &iotlb,
> +                                     as->iommu->iot_limit > 0)) {
>           /* Translation disabled or fault reported. */
>           iotlb.addr_mask = 0;
>           iotlb.perm = IOMMU_NONE;