All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates
@ 2026-11-13 15:12 Tan Chi
  2026-11-13 15:12 ` [PATCH v2 1/3] hw/riscv: iommu: Fix implicit G-stage page-table accesses Tan Chi
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Tan Chi @ 2026-11-13 15:12 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-riscv, palmer, alistair.francis, liwei1518, daniel.barboza,
	zhiwei_liu, chao.liu, pierrick.bouvier, nutty.liu, Tan Chi

This series adds support for automatic updates of page-table accessed
(A) and dirty (D) bits in the RISC-V IOMMU, as specified by the RISC-V
IOMMU architecture. The feature is advertised through the AMO_HWAD
capability and controlled by the SADE and GADE bits in device contexts.

The first two patches fix implicit G-stage page-table access handling
and IOATC permission checks. The third patch implements atomic A/D bit
updates using CAS, with page-table walk retries on concurrent
modifications and G-stage permission checks for implicit writes during
nested translation.

The implementation has been validated with QTests, Linux DMA tests under
KVM and TCG, and concurrency tests covering CAS retries, PTE
modifications, and G-stage mapping changes.

---
Changes in v2:
- Set iotval2 to zero for S-stage page faults, as suggested by
  Daniel Henrique Barboza.
- Avoid duplicate implicit_read calculations, as suggested by Nutty Liu.
- Document the new amo-hwad device property.

v1: https://lore.kernel.org/qemu-devel/20261108143934.129923-1-tanchi25@mails.ucas.ac.cn/

Tan Chi (3):
  hw/riscv: iommu: Fix implicit G-stage page-table accesses
  hw/riscv: iommu: Check permissions on IOATC hits
  hw/riscv: iommu: Support automatic PTE A/D updates

 docs/specs/riscv-iommu.rst |   2 +
 hw/riscv/riscv-iommu.c     | 254 +++++++++++++++++++++++++++++++++----
 hw/riscv/riscv-iommu.h     |   1 +
 3 files changed, 232 insertions(+), 25 deletions(-)

-- 
2.53.0



^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 1/3] hw/riscv: iommu: Fix implicit G-stage page-table accesses
  2026-11-13 15:12 [PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates Tan Chi
@ 2026-11-13 15:12 ` Tan Chi
  2026-11-13 15:12 ` [PATCH v2 2/3] hw/riscv: iommu: Check permissions on IOATC hits Tan Chi
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Tan Chi @ 2026-11-13 15:12 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-riscv, palmer, alistair.francis, liwei1518, daniel.barboza,
	zhiwei_liu, chao.liu, pierrick.bouvier, nutty.liu, Tan Chi

When translating a DMA write, G-stage accesses to first-stage page
tables are implicit reads. The current implementation checks permissions
using the original DMA access type, which may incorrectly require write
permission for these reads.

Use read permission for implicit page-table accesses while preserving
the original DMA access type for the final translation and fault
reporting.

Also track the fault address separately from the translation result and
set the implicit-access flag in iotval2 for guest-page faults. Route
address-range failures through the common page-fault path to report the
correct fault address.

Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
Reviewed-by: Nutty Liu <nutty.liu@hotmail.com>
---
 hw/riscv/riscv-iommu.c | 58 ++++++++++++++++++++++++++----------------
 1 file changed, 36 insertions(+), 22 deletions(-)

diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c
index 0e6970773a..5b8cfff3bd 100644
--- a/hw/riscv/riscv-iommu.c
+++ b/hw/riscv/riscv-iommu.c
@@ -273,18 +273,21 @@ static hwaddr riscv_iommu_napot_page_mask(hwaddr ppn, hwaddr addr, hwaddr *out)
  * Keeping them separate for now, as error reporting and flow specifics are
  * sufficiently different for separate implementation.
  *
- * @s        : IOMMU Device State
- * @ctx      : Translation context for device id and process address space id.
- * @iotlb    : translation data: physical address and access mode.
- * @return   : success or fault cause code.
+ * @s               : IOMMU Device State
+ * @ctx             : Translation context for device id and process
+ *                    address space id.
+ * @iotlb           : translation data: physical address and access mode.
+ * @fault_iotval2   : Fault address and implicit-access flags for iotval2.
+ * @return          : success or fault cause code.
  */
 static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
-    IOMMUTLBEntry *iotlb)
+    IOMMUTLBEntry *iotlb, uint64_t *fault_iotval2)
 {
     IOMMUAccessFlags trans_perm = IOMMU_NONE;
     dma_addr_t addr, base;
     uint64_t satp, gatp, pte;
     bool en_s, en_g;
+    bool implicit_read;
     struct {
         unsigned char step;
         unsigned char levels;
@@ -299,10 +302,10 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
     MemTxResult ret;
     bool pv = !!ctx->process_id;
     /*
-     * Keep the request permission separate from iotlb->perm.  G-stage
+     * Keep the request permission separate from iotlb->perm. G-stage
      * walks translate S-stage PTE addresses before the real leaf is
      * reached, but permission checks and fault types must still use the
-     * original request.  A successful walk leaves iotlb->perm with the
+     * original request. A successful walk leaves iotlb->perm with the
      * effective leaf permission for the translation cache.
      */
     const IOMMUAccessFlags req_perm = iotlb->perm;
@@ -424,6 +427,14 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
         const dma_addr_t pte_addr = base + idx * sc[pass].ptesize;
         const bool ade =
             ctx->tc & (pass ? RISCV_IOMMU_DC_TC_GADE : RISCV_IOMMU_DC_TC_SADE);
+        /*
+         * G-stage accesses to S-stage page tables are implicit reads.
+         * The original DMA permission is used for the final data access.
+         */
+        implicit_read =
+            pass == G_STAGE && en_s && sc[S_STAGE].step != sc[S_STAGE].levels;
+        const IOMMUAccessFlags acc_perm =
+            implicit_read ? IOMMU_RO : req_perm;
 
         if (ade && !(s->cap & RISCV_IOMMU_CAP_AMO_HWAD)) {
             /* GADE/SADE are reserved bits if AMO_HWAD is cleared.  */
@@ -442,15 +453,11 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
                 masked_msbs = (addr >> (va_len - 1)) & mask;
 
                 if (masked_msbs != 0 && masked_msbs != mask) {
-                    return (req_perm & IOMMU_WO) ?
-                                RISCV_IOMMU_FQ_CAUSE_WR_FAULT_S :
-                                RISCV_IOMMU_FQ_CAUSE_RD_FAULT_S;
+                    goto page_fault;
                 }
             } else {
                 if ((addr & va_mask) != addr) {
-                    return (req_perm & IOMMU_WO) ?
-                                RISCV_IOMMU_FQ_CAUSE_WR_FAULT_VS :
-                                RISCV_IOMMU_FQ_CAUSE_RD_FAULT_VS;
+                    goto page_fault;
                 }
             }
         }
@@ -499,13 +506,13 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
              * non-user mode leaf PTE and !pv we need to fault.
              */
             break;
-        } else if ((req_perm & IOMMU_RO) && !(pte & PTE_R)) {
+        } else if ((acc_perm & IOMMU_RO) && !(pte & PTE_R)) {
             break;                /* Read access check failed */
-        } else if ((req_perm & IOMMU_WO) && !(pte & PTE_W)) {
+        } else if ((acc_perm & IOMMU_WO) && !(pte & PTE_W)) {
             break;                /* Write access check failed */
         } else if (!ade && !(pte & PTE_A)) {
             break;                /* Access bit not set */
-        } else if ((req_perm & IOMMU_WO) && !ade && !(pte & PTE_D)) {
+        } else if ((acc_perm & IOMMU_WO) && !ade && !(pte & PTE_D)) {
             break;                /* Dirty bit not set */
         } else if (pass == G_STAGE && !(pte & PTE_U)) {
             /*
@@ -587,13 +594,19 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
         }
     } while (1);
 
+page_fault:
     /*
      * riscv_iommu_translate() will receive a fault and then call
-     * riscv_iommu_report_fault() using iotlb->translated_addr
-     * as iotval2.  Update translated_addr it with the latest
-     * translated addr we have.
+     * riscv_iommu_report_fault() using fault_iotval2 as iotval2.
+     * Set fault_iotval2 to zero for S-stage page faults.
+     * For guest-page faults, report the faulting GPA and set the implicit
+     * first-stage page-table access flag in the low bit.
      */
-    iotlb->translated_addr = addr;
+    *fault_iotval2 = 0;
+
+    if (pass == G_STAGE) {
+        *fault_iotval2 = (addr & ~3ULL) | (implicit_read ? 1 : 0);
+    }
 
     return (req_perm & IOMMU_WO) ?
                 (pass ? RISCV_IOMMU_FQ_CAUSE_WR_FAULT_VS :
@@ -1740,6 +1753,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
     bool enable_pri;
     hwaddr phys = 0;
     int fault;
+    uint64_t fault_iotval2 = 0;
 
     riscv_iommu_hpm_incr_ctr(s, ctx, RISCV_IOMMU_HPMEVENT_URQ);
 
@@ -1776,7 +1790,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
     riscv_iommu_hpm_incr_ctr(s, ctx, RISCV_IOMMU_HPMEVENT_TLB_MISS);
 
     /* Translate using device directory / page table information. */
-    fault = riscv_iommu_spa_fetch(s, ctx, iotlb);
+    fault = riscv_iommu_spa_fetch(s, ctx, iotlb, &fault_iotval2);
 
     if (!fault && iotlb->target_as == &s->trap_as) {
         /* Do not cache trapped MSI translations */
@@ -1827,7 +1841,7 @@ done:
         }
 
         riscv_iommu_report_fault(s, ctx, ttype, fault, enable_pid,
-                                 iotlb->iova, iotlb->translated_addr);
+                                 iotlb->iova, fault_iotval2);
         return fault;
     }
 
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 2/3] hw/riscv: iommu: Check permissions on IOATC hits
  2026-11-13 15:12 [PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates Tan Chi
  2026-11-13 15:12 ` [PATCH v2 1/3] hw/riscv: iommu: Fix implicit G-stage page-table accesses Tan Chi
@ 2026-11-13 15:12 ` Tan Chi
  2026-11-13 15:12 ` [PATCH v2 3/3] hw/riscv: iommu: Support automatic PTE A/D updates Tan Chi
  2026-11-17 14:30 ` [PATCH v2 0/3] " Gong Shuai
  3 siblings, 0 replies; 9+ messages in thread
From: Tan Chi @ 2026-11-13 15:12 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-riscv, palmer, alistair.francis, liwei1518, daniel.barboza,
	zhiwei_liu, chao.liu, pierrick.bouvier, nutty.liu, Tan Chi

The current implementation reuses cached translations without checking
whether their permissions satisfy the requested access.

Only reuse an IOATC entry when its permissions cover the current
request. Otherwise, perform a page-table walk to check the requested
access. Preserve the existing behavior for requests with IOMMU_NONE.

This is also needed for hardware A/D updates, where mappings with a
clear D bit must not cache write permission until the bit has been set.

Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
Reviewed-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
Reviewed-by: Nutty Liu <nutty.liu@hotmail.com>
---
 hw/riscv/riscv-iommu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c
index 5b8cfff3bd..8c26b7f2b2 100644
--- a/hw/riscv/riscv-iommu.c
+++ b/hw/riscv/riscv-iommu.c
@@ -1779,7 +1779,9 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
         phys = iot->phys;
     }
     qemu_mutex_unlock(&s->iot_lock);
-    if (perm != IOMMU_NONE) {
+    if (perm != IOMMU_NONE &&
+        (iotlb->perm == IOMMU_NONE ||
+         (perm & iotlb->perm) == iotlb->perm)) {
         iotlb->translated_addr = PPN_PHYS(phys);
         iotlb->addr_mask = ~TARGET_PAGE_MASK;
         iotlb->perm = perm;
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 3/3] hw/riscv: iommu: Support automatic PTE A/D updates
  2026-11-13 15:12 [PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates Tan Chi
  2026-11-13 15:12 ` [PATCH v2 1/3] hw/riscv: iommu: Fix implicit G-stage page-table accesses Tan Chi
  2026-11-13 15:12 ` [PATCH v2 2/3] hw/riscv: iommu: Check permissions on IOATC hits Tan Chi
@ 2026-11-13 15:12 ` Tan Chi
  2026-11-17 14:30 ` [PATCH v2 0/3] " Gong Shuai
  3 siblings, 0 replies; 9+ messages in thread
From: Tan Chi @ 2026-11-13 15:12 UTC (permalink / raw)
  To: qemu-devel
  Cc: qemu-riscv, palmer, alistair.francis, liwei1518, daniel.barboza,
	zhiwei_liu, chao.liu, pierrick.bouvier, nutty.liu, Tan Chi

Add support for the AMO_HWAD capability to automatically update the
accessed and dirty bits of leaf PTEs when SADE or GADE is enabled.

Use atomic compare-and-exchange to update PTEs, restarting the
page-table walk if a concurrent modification is detected.

For nested translation, check G-stage write permissions before updating
an S-stage PTE. Restart the walk if the write translation resolves to a
different physical address.

Avoid caching write permission when the PTE's dirty bit is clear, so
subsequent writes can trigger the required update.

Expose the capability through the new "amo-hwad" device property,
disabled by default.

Signed-off-by: Tan Chi <tanchi25@mails.ucas.ac.cn>
Reviewed-by: Daniel Henrique Barboza <daniel.barboza@oss.qualcomm.com>
Reviewed-by: Nutty Liu <nutty.liu@hotmail.com>
---
 docs/specs/riscv-iommu.rst |   2 +
 hw/riscv/riscv-iommu.c     | 196 ++++++++++++++++++++++++++++++++++++-
 hw/riscv/riscv-iommu.h     |   1 +
 3 files changed, 195 insertions(+), 4 deletions(-)

diff --git a/docs/specs/riscv-iommu.rst b/docs/specs/riscv-iommu.rst
index 571a6a6cc9..7912361196 100644
--- a/docs/specs/riscv-iommu.rst
+++ b/docs/specs/riscv-iommu.rst
@@ -69,6 +69,7 @@ Several options are available to control the capabilities of the device, namely:
 - "ioatc-limit": size of the Address Translation Cache (default to 2Mb)
 - "intremap": enable/disable MSI support
 - "ats": enable ATS support
+- "amo-hwad": enable automatic PTE A/D bit updates when SADE or GADE is set
 - "off" (Out-of-reset translation mode: 'on' for DMA disabled, 'off' for 'BARE' (passthrough))
 - "s-stage": enable s-stage support
 - "g-stage": enable g-stage support
@@ -96,6 +97,7 @@ riscv-iommu options:
 - "ioatc-limit": default value (2Mb)
 - "intremap": enabled
 - "ats": enabled
+- "amo-hwad": disabled
 - "off": on (DMA disabled)
 - "s-stage": enabled
 - "g-stage": enabled
diff --git a/hw/riscv/riscv-iommu.c b/hw/riscv/riscv-iommu.c
index 8c26b7f2b2..5b1a388364 100644
--- a/hw/riscv/riscv-iommu.c
+++ b/hw/riscv/riscv-iommu.c
@@ -265,6 +265,128 @@ static hwaddr riscv_iommu_napot_page_mask(hwaddr ppn, hwaddr addr, hwaddr *out)
     return page_mask;
 }
 
+typedef enum RISCVIOMMUADResult {
+    RISCV_IOMMU_AD_SUCCESS,
+    RISCV_IOMMU_AD_RETRY,
+    RISCV_IOMMU_AD_FAULT,
+} RISCVIOMMUADResult;
+
+/*
+ * Atomically update the A/D bits of a leaf PTE.
+ * Retry the page-table walk if the PTE has changed.
+ */
+static RISCVIOMMUADResult
+riscv_iommu_ad_update(RISCVIOMMUState *s, hwaddr pte_addr,
+                      unsigned int pte_size, uint64_t pte, uint64_t updated_pte)
+{
+    MemoryRegion *mr;
+    hwaddr offset, len = pte_size;
+    uint64_t old_pte;
+    void *pte_ptr;
+
+    if (pte_addr & (pte_size - 1)) {
+        return RISCV_IOMMU_AD_FAULT;
+    }
+
+    RCU_READ_LOCK_GUARD();
+
+    mr = address_space_translate(s->target_as, pte_addr, &offset,
+                                 &len, true, MEMTXATTRS_UNSPECIFIED);
+
+    if (len < pte_size || !memory_region_is_ram(mr)) {
+        /*
+         * The entire PTE must be backed by a single RAM region
+         * for an atomic update.
+         */
+        return RISCV_IOMMU_AD_FAULT;
+    }
+
+    pte_ptr = qemu_map_ram_ptr(mr->ram_block, offset);
+
+    if (pte_size == 4) {
+        uint32_t expected = cpu_to_le32((uint32_t)pte);
+        uint32_t desired = cpu_to_le32((uint32_t)updated_pte);
+
+        old_pte = qatomic_cmpxchg((uint32_t *)pte_ptr, expected, desired);
+        old_pte = le32_to_cpu(old_pte);
+    } else {
+        uint64_t expected = cpu_to_le64(pte);
+        uint64_t desired = cpu_to_le64(updated_pte);
+
+        old_pte = qatomic_cmpxchg((uint64_t *)pte_ptr, expected, desired);
+        old_pte = le64_to_cpu(old_pte);
+    }
+
+    if (old_pte != pte) {
+        return RISCV_IOMMU_AD_RETRY;
+    }
+
+    return RISCV_IOMMU_AD_SUCCESS;
+}
+
+static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
+    IOMMUTLBEntry *iotlb, bool implicit_write, uint64_t *fault_iotval2);
+
+/*
+ * Updating a first-stage leaf PTE requires an implicit write.
+ * Translate its GPA through the G-stage, checking write permission
+ * and updating the G-stage's A/D bits as needed.
+ */
+static int riscv_iommu_check_pte_write(RISCVIOMMUState *s,
+                                       RISCVIOMMUContext *ctx,
+                                       dma_addr_t pte_gpa,
+                                       IOMMUAccessFlags req_perm,
+                                       hwaddr *pte_spa,
+                                       uint64_t *fault_iotval2)
+{
+    RISCVIOMMUContext gctx = *ctx;
+    uint64_t iotval2 = 0;
+    int fault;
+
+    IOMMUTLBEntry iotlb = {
+        .iova = pte_gpa,
+        .target_as = s->target_as,
+        .addr_mask = ~0ULL,
+        .perm = IOMMU_WO,
+    };
+
+    gctx.satp = RISCV_IOMMU_DC_FSC_MODE_BARE;
+
+    fault = riscv_iommu_spa_fetch(s, &gctx, &iotlb, true, &iotval2);
+
+    if (fault) {
+        *fault_iotval2 = iotval2;
+
+        if (fault == RISCV_IOMMU_FQ_CAUSE_RD_FAULT_VS ||
+            fault == RISCV_IOMMU_FQ_CAUSE_WR_FAULT_VS) {
+            /*
+             * Per the RISC-V IOMMU Fault/Event Queue format, iotval2[0]
+             * indicates an implicit first-stage access and iotval2[1]
+             * indicates an implicit write. Report the fault for the
+             * original DMA access rather than the implicit write.
+             */
+            *fault_iotval2 = (pte_gpa & ~3ULL) | 3;
+
+            fault = (req_perm & IOMMU_WO) ?
+                        RISCV_IOMMU_FQ_CAUSE_WR_FAULT_VS :
+                        RISCV_IOMMU_FQ_CAUSE_RD_FAULT_VS;
+        } else if (fault == RISCV_IOMMU_FQ_CAUSE_RD_FAULT ||
+                   fault == RISCV_IOMMU_FQ_CAUSE_WR_FAULT) {
+            /*
+             * A physical access fault also uses the original
+             * DMA access type.
+             */
+            fault = (req_perm & IOMMU_WO) ?
+                        RISCV_IOMMU_FQ_CAUSE_WR_FAULT :
+                        RISCV_IOMMU_FQ_CAUSE_RD_FAULT;
+        }
+        return fault;
+    }
+
+    *pte_spa = iotlb.translated_addr;
+    return 0;
+}
+
 /*
  * RISCV IOMMU Address Translation Lookup - Page Table Walk
  *
@@ -277,14 +399,15 @@ static hwaddr riscv_iommu_napot_page_mask(hwaddr ppn, hwaddr addr, hwaddr *out)
  * @ctx             : Translation context for device id and process
  *                    address space id.
  * @iotlb           : translation data: physical address and access mode.
+ * @implicit_write  : Whether this is an implicit write to a first-stage PTE.
  * @fault_iotval2   : Fault address and implicit-access flags for iotval2.
  * @return          : success or fault cause code.
  */
 static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
-    IOMMUTLBEntry *iotlb, uint64_t *fault_iotval2)
+    IOMMUTLBEntry *iotlb, bool implicit_write, uint64_t *fault_iotval2)
 {
     IOMMUAccessFlags trans_perm = IOMMU_NONE;
-    dma_addr_t addr, base;
+    dma_addr_t addr, base, s_base_gpa = 0;
     uint64_t satp, gatp, pte;
     bool en_s, en_g;
     bool implicit_read;
@@ -309,6 +432,8 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
      * effective leaf permission for the translation cache.
      */
     const IOMMUAccessFlags req_perm = iotlb->perm;
+    const hwaddr addr_mask = iotlb->addr_mask;
+    const hwaddr translated_addr = iotlb->translated_addr;
 
     satp = get_field(ctx->satp, RISCV_IOMMU_ATP_MODE_FIELD);
     gatp = get_field(ctx->gatp, RISCV_IOMMU_ATP_MODE_FIELD);
@@ -327,7 +452,7 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
      * means we can't do an early MSI check unless we have
      * strictly !en_s.
      */
-    if (!en_s && (req_perm & IOMMU_WO) &&
+    if (!implicit_write && !en_s && (req_perm & IOMMU_WO) &&
         riscv_iommu_msi_check(s, ctx, iotlb->iova)) {
         iotlb->target_as = &s->trap_as;
         iotlb->translated_addr = iotlb->iova;
@@ -414,6 +539,17 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
     /* S/G stages translation tables root pointers */
     gatp = PPN_PHYS(get_field(ctx->gatp, RISCV_IOMMU_ATP_PPN_FIELD));
     satp = PPN_PHYS(get_field(ctx->satp, RISCV_IOMMU_ATP_PPN_FIELD));
+
+restart:
+    sc[S_STAGE].step = 0;
+    sc[G_STAGE].step = 0;
+    trans_perm = IOMMU_NONE;
+    iotlb->addr_mask = addr_mask;
+    iotlb->translated_addr = translated_addr;
+    iotlb->perm = req_perm;
+    s_base_gpa = 0;
+    *fault_iotval2 = 0;
+
     addr = (en_s && en_g) ? satp : iotlb->iova;
     base = en_g ? gatp : satp;
     pass = en_g ? G_STAGE : S_STAGE;
@@ -541,8 +677,51 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
                 iotlb->addr_mask &= (1ULL << va_skip) - 1;
             }
 
+            /* Automatically update the leaf PTE's A/D bits when enabled. */
+            uint64_t updated_pte = pte;
+            if (ade) {
+                updated_pte |= PTE_A;
+
+                if (acc_perm & IOMMU_WO) {
+                    updated_pte |= PTE_D;
+                }
+            }
+            if (updated_pte != pte) {
+                if (pass == S_STAGE && en_g) {
+                    const dma_addr_t s_pte_gpa =
+                        s_base_gpa + idx * sc[pass].ptesize;
+                    hwaddr write_spa;
+
+                    int fault = riscv_iommu_check_pte_write(
+                        s, ctx, s_pte_gpa, req_perm, &write_spa,
+                        fault_iotval2);
+                    if (fault) {
+                        return fault;
+                    }
+
+                    if (write_spa != pte_addr) {
+                        goto restart;
+                    }
+                }
+
+                RISCVIOMMUADResult result = riscv_iommu_ad_update(
+                    s, pte_addr, sc[pass].ptesize, pte, updated_pte);
+
+                if (result == RISCV_IOMMU_AD_RETRY) {
+                    goto restart;
+                }
+                if (result == RISCV_IOMMU_AD_FAULT) {
+                    return (req_perm & IOMMU_WO) ?
+                        RISCV_IOMMU_FQ_CAUSE_WR_FAULT :
+                        RISCV_IOMMU_FQ_CAUSE_RD_FAULT;
+                }
+                pte = updated_pte;
+            }
+
             /* Continue with S-Stage translation? */
             if (pass && sc[0].step != sc[0].levels) {
+                /* Save the S-stage page-table GPA for A/D updates. */
+                s_base_gpa = addr;
                 pass = S_STAGE;
                 addr = iotlb->iova;
                 continue;
@@ -553,6 +732,11 @@ static int riscv_iommu_spa_fetch(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
                                          ((pte & PTE_R) ? IOMMU_RW : IOMMU_WO) :
                                          IOMMU_RO;
 
+            /* Do not cache write permission until the D bit is set. */
+            if (!(pte & PTE_D)) {
+                leaf_perm &= ~IOMMU_WO;
+            }
+
             trans_perm = trans_perm == IOMMU_NONE ?
                          leaf_perm : trans_perm & leaf_perm;
 
@@ -1792,7 +1976,7 @@ static int riscv_iommu_translate(RISCVIOMMUState *s, RISCVIOMMUContext *ctx,
     riscv_iommu_hpm_incr_ctr(s, ctx, RISCV_IOMMU_HPMEVENT_TLB_MISS);
 
     /* Translate using device directory / page table information. */
-    fault = riscv_iommu_spa_fetch(s, ctx, iotlb, &fault_iotval2);
+    fault = riscv_iommu_spa_fetch(s, ctx, iotlb, false, &fault_iotval2);
 
     if (!fault && iotlb->target_as == &s->trap_as) {
         /* Do not cache trapped MSI translations */
@@ -2730,6 +2914,9 @@ static void riscv_iommu_realize(DeviceState *dev, Error **errp)
     if (s->enable_ats) {
         s->cap |= RISCV_IOMMU_CAP_ATS;
     }
+    if (s->enable_amo_hwad) {
+        s->cap |= RISCV_IOMMU_CAP_AMO_HWAD;
+    }
     if (s->enable_s_stage) {
         s->cap |= RISCV_IOMMU_CAP_SV32 | RISCV_IOMMU_CAP_SV39 |
                   RISCV_IOMMU_CAP_SV48 | RISCV_IOMMU_CAP_SV57;
@@ -2889,6 +3076,7 @@ static const Property riscv_iommu_properties[] = {
         LIMIT_CACHE_IOT),
     DEFINE_PROP_BOOL("intremap", RISCVIOMMUState, enable_msi, TRUE),
     DEFINE_PROP_BOOL("ats", RISCVIOMMUState, enable_ats, TRUE),
+    DEFINE_PROP_BOOL("amo-hwad", RISCVIOMMUState, enable_amo_hwad, false),
     DEFINE_PROP_BOOL("off", RISCVIOMMUState, enable_off, TRUE),
     DEFINE_PROP_BOOL("s-stage", RISCVIOMMUState, enable_s_stage, TRUE),
     DEFINE_PROP_BOOL("g-stage", RISCVIOMMUState, enable_g_stage, TRUE),
diff --git a/hw/riscv/riscv-iommu.h b/hw/riscv/riscv-iommu.h
index 403d10b321..8cf228f698 100644
--- a/hw/riscv/riscv-iommu.h
+++ b/hw/riscv/riscv-iommu.h
@@ -44,6 +44,7 @@ struct RISCVIOMMUState {
     bool enable_off;      /* Enable out-of-reset OFF mode (DMA disabled) */
     bool enable_msi;      /* Enable MSI remapping */
     bool enable_ats;      /* Enable ATS support */
+    bool enable_amo_hwad; /* Enable hardware A/D updates */
     bool enable_s_stage;  /* Enable S/VS-Stage translation */
     bool enable_g_stage;  /* Enable G-Stage translation */
 
-- 
2.53.0



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates
  2026-11-13 15:12 [PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates Tan Chi
                   ` (2 preceding siblings ...)
  2026-11-13 15:12 ` [PATCH v2 3/3] hw/riscv: iommu: Support automatic PTE A/D updates Tan Chi
@ 2026-11-17 14:30 ` Gong Shuai
  2026-11-18 14:40   ` Tan Chi
  2026-11-23 19:58   ` Daniel Henrique Barboza
  3 siblings, 2 replies; 9+ messages in thread
From: Gong Shuai @ 2026-11-17 14:30 UTC (permalink / raw)
  To: tanchi25
  Cc: alistair.francis, chao.liu, daniel.barboza, liwei1518, nutty.liu,
	palmer, pierrick.bouvier, qemu-devel, qemu-riscv, zhiwei_liu,
	gong.shuai

> This series adds support for automatic updates of page-table accessed
> (A) and dirty (D) bits in the RISC-V IOMMU, as specified by the RISC-V
> IOMMU architecture. The feature is advertised through the AMO_HWAD
> capability and controlled by the SADE and GADE bits in device contexts.
> 
> The first two patches fix implicit G-stage page-table access handling
> and IOATC permission checks. The third patch implements atomic A/D bit
> updates using CAS, with page-table walk retries on concurrent
> modifications and G-stage permission checks for implicit writes during
> nested translation.
> 
> The implementation has been validated with QTests, Linux DMA tests under
> KVM and TCG, and concurrency tests covering CAS retries, PTE
> modifications, and G-stage mapping changes.
> 
> ---
> Changes in v2:
> - Set iotval2 to zero for S-stage page faults, as suggested by
>   Daniel Henrique Barboza.
> - Avoid duplicate implicit_read calculations, as suggested by Nutty Liu.
> - Document the new amo-hwad device property.
> 
> v1: https://lore.kernel.org/qemu-devel/20261108143934.129923-1-tanchi25@mails.ucas.ac.cn/
> 
> Tan Chi (3):
>   hw/riscv: iommu: Fix implicit G-stage page-table accesses
>   hw/riscv: iommu: Check permissions on IOATC hits
>   hw/riscv: iommu: Support automatic PTE A/D updates

Hi Tan Chi,

Thanks for your work.

I have tested this series on my nested IOMMU setup.

- Outer QEMU: this series on top of master, -M virt,...,iommu-sys=on,
              -global riscv-iommu.amo-hwad=on, emulated NVMe
- Inner QEMU: needs accel=on support for riscv-iommu-pci, and
              vfio-pci with iommufd
- Outer Linux host: needs hardware dirty tracking and nested IOMMU
              support

Test flow:

1. Boot host and guest, do NVMe I/O
2. Clear host S2 D bits and trigger GVMA inval in the inner QEMU monitor,
   use command: calc_dirty_rate -b 1
3. Back to guest shell and do another NVMe I/O

It completed without a guest hang or I/O error. With temporary local
debug prints I could see en_s=1/en_g=1 and implicit G-stage page-table
walks.

One host-side workaround is that on the host I need
echo never > /sys/kernel/mm/transparent_hugepage/enabled
to avoid an hang with THP; otherwise calc_dirty_rate -b can leave the
guest stuck.

This is only a limited positive test. A Linux guest is not suitable for
constructing negative cases so I would not claim full Tested-by coverage.

Thanks,
Shuai

> 
>  docs/specs/riscv-iommu.rst |   2 +
>  hw/riscv/riscv-iommu.c     | 254 +++++++++++++++++++++++++++++++++----
>  hw/riscv/riscv-iommu.h     |   1 +
>  3 files changed, 232 insertions(+), 25 deletions(-)
> 
> -- 
> 2.53.0
> 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates
  2026-11-17 14:30 ` [PATCH v2 0/3] " Gong Shuai
@ 2026-11-18 14:40   ` Tan Chi
  2026-11-23 19:58   ` Daniel Henrique Barboza
  1 sibling, 0 replies; 9+ messages in thread
From: Tan Chi @ 2026-11-18 14:40 UTC (permalink / raw)
  To: gsh517025
  Cc: alistair.francis, chao.liu, daniel.barboza, liwei1518, nutty.liu,
	palmer, pierrick.bouvier, qemu-devel, qemu-riscv, zhiwei_liu,
	gong.shuai

Hi Shuai,

Thanks for testing the series and for the detailed report.

Using your test setup, I was able to reproduce the hang with THP enabled.
Further investigation showed that the L2 vCPU repeatedly encountered a
store guest-page fault at the same GPA, while KVM kept attempting to
install a 2 MiB G-stage mapping.

This appears to be related to the issue addressed by the following
KVM patch:

https://lore.kernel.org/linux-riscv/20260815103245.864925-1-jinyu.tang@linux.dev/

I applied a similar local fix to skip THP adjustment when a 4 KiB
G-stage leaf already exists, and the test completed successfully with
THP enabled.

Thanks,
Tan Chi



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates
  2026-11-17 14:30 ` [PATCH v2 0/3] " Gong Shuai
  2026-11-18 14:40   ` Tan Chi
@ 2026-11-23 19:58   ` Daniel Henrique Barboza
  2026-11-24  5:18     ` Gong Shuai
  1 sibling, 1 reply; 9+ messages in thread
From: Daniel Henrique Barboza @ 2026-11-23 19:58 UTC (permalink / raw)
  To: Gong Shuai, tanchi25
  Cc: alistair.francis, chao.liu, liwei1518, nutty.liu, palmer,
	pierrick.bouvier, qemu-devel, qemu-riscv, zhiwei_liu, gong.shuai

Hello,

On 11/17/2026 11:30 AM, Gong Shuai wrote:
>> This series adds support for automatic updates of page-table accessed
>> (A) and dirty (D) bits in the RISC-V IOMMU, as specified by the RISC-V
>> IOMMU architecture. The feature is advertised through the AMO_HWAD
>> capability and controlled by the SADE and GADE bits in device contexts.
>>
>> The first two patches fix implicit G-stage page-table access handling
>> and IOATC permission checks. The third patch implements atomic A/D bit
>> updates using CAS, with page-table walk retries on concurrent
>> modifications and G-stage permission checks for implicit writes during
>> nested translation.
>>
>> The implementation has been validated with QTests, Linux DMA tests under
>> KVM and TCG, and concurrency tests covering CAS retries, PTE
>> modifications, and G-stage mapping changes.
>>
>> ---
>> Changes in v2:
>> - Set iotval2 to zero for S-stage page faults, as suggested by
>>    Daniel Henrique Barboza.
>> - Avoid duplicate implicit_read calculations, as suggested by Nutty Liu.
>> - Document the new amo-hwad device property.
>>
>> v1: https://lore.kernel.org/qemu-devel/20261108143934.129923-1-tanchi25@mails.ucas.ac.cn/
>>
>> Tan Chi (3):
>>    hw/riscv: iommu: Fix implicit G-stage page-table accesses
>>    hw/riscv: iommu: Check permissions on IOATC hits
>>    hw/riscv: iommu: Support automatic PTE A/D updates
> 
> Hi Tan Chi,
> 
> Thanks for your work.
> 
> I have tested this series on my nested IOMMU setup.
> 
> - Outer QEMU: this series on top of master, -M virt,...,iommu-sys=on,
>                -global riscv-iommu.amo-hwad=on, emulated NVMe
> - Inner QEMU: needs accel=on support for riscv-iommu-pci, and
>                vfio-pci with iommufd
> - Outer Linux host: needs hardware dirty tracking and nested IOMMU
>                support
> 
> Test flow:
> 
> 1. Boot host and guest, do NVMe I/O
> 2. Clear host S2 D bits and trigger GVMA inval in the inner QEMU monitor,
>     use command: calc_dirty_rate -b 1
> 3. Back to guest shell and do another NVMe I/O
> 
> It completed without a guest hang or I/O error. With temporary local
> debug prints I could see en_s=1/en_g=1 and implicit G-stage page-table
> walks.
> 
> One host-side workaround is that on the host I need
> echo never > /sys/kernel/mm/transparent_hugepage/enabled
> to avoid an hang with THP; otherwise calc_dirty_rate -b can leave the
> guest stuck.


Can you please clarify if this THP hang also happens on master?  It's not clear
to me if this scenario is already broken or if this automatic PTE A/D work is
exposing it.

Even if it's a kernel/KVM side problem (as Tan Chi mentioned in another reply) we
should be wary of adding emulation changes that can regress regular software
usage.  At the very least we should document that we're doing an emulation change
that can break software that doesn't have a certain workaround/patch.

Thanks,
Daniel

> 
> This is only a limited positive test. A Linux guest is not suitable for
> constructing negative cases so I would not claim full Tested-by coverage.
> 
> Thanks,
> Shuai
> 
>>
>>   docs/specs/riscv-iommu.rst |   2 +
>>   hw/riscv/riscv-iommu.c     | 254 +++++++++++++++++++++++++++++++++----
>>   hw/riscv/riscv-iommu.h     |   1 +
>>   3 files changed, 232 insertions(+), 25 deletions(-)
>>
>> -- 
>> 2.53.0
>>



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates
  2026-11-23 19:58   ` Daniel Henrique Barboza
@ 2026-11-24  5:18     ` Gong Shuai
  2026-11-24 19:58       ` Daniel Henrique Barboza
  0 siblings, 1 reply; 9+ messages in thread
From: Gong Shuai @ 2026-11-24  5:18 UTC (permalink / raw)
  To: Daniel Henrique Barboza, tanchi25
  Cc: alistair.francis, chao.liu, liwei1518, nutty.liu, palmer,
	pierrick.bouvier, qemu-devel, qemu-riscv, zhiwei_liu, gong.shuai

Hi Daniel,

On 11/24/2026 3:58 AM, Daniel Henrique Barboza wrote:
> Hello,
> 
> On 11/17/2026 11:30 AM, Gong Shuai wrote:
>>> This series adds support for automatic updates of page-table accessed
>>> (A) and dirty (D) bits in the RISC-V IOMMU, as specified by the RISC-V
>>> IOMMU architecture. The feature is advertised through the AMO_HWAD
>>> capability and controlled by the SADE and GADE bits in device contexts.
>>>
>>> The first two patches fix implicit G-stage page-table access handling
>>> and IOATC permission checks. The third patch implements atomic A/D bit
>>> updates using CAS, with page-table walk retries on concurrent
>>> modifications and G-stage permission checks for implicit writes during
>>> nested translation.
>>>
>>> The implementation has been validated with QTests, Linux DMA tests under
>>> KVM and TCG, and concurrency tests covering CAS retries, PTE
>>> modifications, and G-stage mapping changes.
>>>
>>> ---
>>> Changes in v2:
>>> - Set iotval2 to zero for S-stage page faults, as suggested by
>>>    Daniel Henrique Barboza.
>>> - Avoid duplicate implicit_read calculations, as suggested by Nutty Liu.
>>> - Document the new amo-hwad device property.
>>>
>>> v1: https://lore.kernel.org/qemu-devel/20261108143934.129923-1-tanchi25@mails.ucas.ac.cn/
>>>
>>> Tan Chi (3):
>>>    hw/riscv: iommu: Fix implicit G-stage page-table accesses
>>>    hw/riscv: iommu: Check permissions on IOATC hits
>>>    hw/riscv: iommu: Support automatic PTE A/D updates
>> 
>> Hi Tan Chi,
>> 
>> Thanks for your work.
>> 
>> I have tested this series on my nested IOMMU setup.
>> 
>> - Outer QEMU: this series on top of master, -M virt,...,iommu-sys=on,
>>                -global riscv-iommu.amo-hwad=on, emulated NVMe
>> - Inner QEMU: needs accel=on support for riscv-iommu-pci, and
>>                vfio-pci with iommufd
>> - Outer Linux host: needs hardware dirty tracking and nested IOMMU
>>                support
>> 
>> Test flow:
>> 
>> 1. Boot host and guest, do NVMe I/O
>> 2. Clear host S2 D bits and trigger GVMA inval in the inner QEMU monitor,
>>     use command: calc_dirty_rate -b 1
>> 3. Back to guest shell and do another NVMe I/O
>> 
>> It completed without a guest hang or I/O error. With temporary local
>> debug prints I could see en_s=1/en_g=1 and implicit G-stage page-table
>> walks.
>> 
>> One host-side workaround is that on the host I need
>> echo never > /sys/kernel/mm/transparent_hugepage/enabled
>> to avoid an hang with THP; otherwise calc_dirty_rate -b can leave the
>> guest stuck.
> 
> 
> Can you please clarify if this THP hang also happens on master?  It's not clear
> to me if this scenario is already broken or if this automatic PTE A/D work is
> exposing it.

Yes, the THP hang also occurs on master (2bfbe39d0fff).
It is not introduced by the amo_hwad series.

> 
> Even if it's a kernel/KVM side problem (as Tan Chi mentioned in another reply) we
> should be wary of adding emulation changes that can regress regular software
> usage.  At the very least we should document that we're doing an emulation change
> that can break software that doesn't have a certain workaround/patch.

This is indeed a KVM-side issue. It is not related to nested IOMMU
or the amo_hwad series; the hang was simply observed while running
tests that involved them.

I can provide a simple setup to reproduce it.
(Hope the following information is useful.)

With master QEMU as the RISC-V virt host and kernel v7.4-rc4 built
with defconfig + KVM + TRANSPARENT_HUGEPAGE, running:

     timeout 180 ./dirty_log_perf_test \
     -e -s anonymous_thp -v 1 -b 512M -i 2

hangs and exits due to timeout, whereas:

     timeout 180 ./dirty_log_perf_test \
     -e -s anonymous -v 1 -b 512M -i 2

exits normally.

After applying the fix patch [1] to the kernel, both tests exit
normally.

Detailed logs for the four command runs are provided below for
reference.

Case 1: without fix patch, anonymous_thp ---- HANG and TIMEOUT(124)

   $ cat /sys/kernel/mm/transparent_hugepage/enabled
   [always] madvise never
   $ timeout 180 ./dirty_log_perf_test -e -s anonymous_thp -v 1 -b 512M -i 2
   Random seed: 0x77be2754
   Test iterations: 2
   Testing guest mode: PA-bits:50,  VA-bits:48,  4K pages
   Random seed: 0x1
   guest physical test memory: [0x3ffffdfe00000, 0x3ffffffe00000)
   Populate memory time: 0.277703400s
   Enabling dirty logging time: 0.001101300s

   Iteration 1 dirty memory time: 0.086482400s
   Iteration 1 get dirty log time: 0.000188500s
   Iteration 1 clear dirty log time: 0.017205400s
   Iteration 2 dirty memory time: 3.617855200s
   Iteration 2 get dirty log time: 0.000093700s
   Iteration 2 clear dirty log time: 0.014652300s
   Disabling dirty logging time: 0.001215600s

   $ echo $?
   124

Case 2: without fix patch, anonymous

   $ timeout 180 ./dirty_log_perf_test -e -s anonymous -v 1 -b 512M -i 2
   Random seed: 0x68f16937
   Test iterations: 2
   Testing guest mode: PA-bits:50,  VA-bits:48,  4K pages
   Random seed: 0x1
   guest physical test memory: [0x3ffffdffff000, 0x3fffffffff000)
   Populate memory time: 8.404907600s
   Enabling dirty logging time: 0.001048700s

   Iteration 1 dirty memory time: 0.080460200s
   Iteration 1 get dirty log time: 0.000119300s
   Iteration 1 clear dirty log time: 0.014741000s
   Iteration 2 dirty memory time: 3.689446400s
   Iteration 2 get dirty log time: 0.000034200s
   Iteration 2 clear dirty log time: 0.013190500s
   Disabling dirty logging time: 0.000651400s
   Get dirty log over 2 iterations took 0.000153500s. (Avg 0.000076750s/iteration)
   Clear dirty log over 2 iterations took 0.027931500s. (Avg 0.013965750s/iteration)
   Testing guest mode: PA-bits:50,  VA-bits:39,  4K pages
   guest physical test memory: [0x3ffffdffff000, 0x3fffffffff000)
   Populate memory time: 7.828295400s
   Enabling dirty logging time: 0.000798200s

   Iteration 1 dirty memory time: 0.066219100s
   Iteration 1 get dirty log time: 0.000039900s
   Iteration 1 clear dirty log time: 0.023695500s
   Iteration 2 dirty memory time: 3.429129500s
   Iteration 2 get dirty log time: 0.000032200s
   Iteration 2 clear dirty log time: 0.020796400s
   Disabling dirty logging time: 0.000772600s
   Get dirty log over 2 iterations took 0.000072100s. (Avg 0.000036050s/iteration)
   Clear dirty log over 2 iterations took 0.044491900s. (Avg 0.022245950s/iteration)

Case 3: withfix patch, anonymous_thp

   $ cat /sys/kernel/mm/transparent_hugepage/enabled
   [always] madvise never
   $ timeout 180 ./dirty_log_perf_test -e -s anonymous_thp -v 1 -b 512M -i 2
   Random seed: 0x3b6f1968
   Test iterations: 2
   Testing guest mode: PA-bits:50,  VA-bits:48,  4K pages
   Random seed: 0x1
   guest physical test memory: [0x3ffffdfe00000, 0x3ffffffe00000)
   Populate memory time: 0.221626400s
   Enabling dirty logging time: 0.001002400s

   Iteration 1 dirty memory time: 0.079966800s
   Iteration 1 get dirty log time: 0.000119400s
   Iteration 1 clear dirty log time: 0.012897100s
   Iteration 2 dirty memory time: 3.490279400s
   Iteration 2 get dirty log time: 0.000044900s
   Iteration 2 clear dirty log time: 0.013184200s
   Disabling dirty logging time: 0.000883200s
   Get dirty log over 2 iterations took 0.000164300s. (Avg 0.000082150s/iteration)
   Clear dirty log over 2 iterations took 0.026081300s. (Avg 0.013040650s/iteration)
   Testing guest mode: PA-bits:50,  VA-bits:39,  4K pages
   guest physical test memory: [0x3ffffdfe00000, 0x3ffffffe00000)
   Populate memory time: 0.209773300s
   Enabling dirty logging time: 0.000790800s

   Iteration 1 dirty memory time: 0.063432900s
   Iteration 1 get dirty log time: 0.000040100s
   Iteration 1 clear dirty log time: 0.012308600s
   Iteration 2 dirty memory time: 3.367311900s
   Iteration 2 get dirty log time: 0.000032500s
   Iteration 2 clear dirty log time: 0.013141600s
   Disabling dirty logging time: 0.000953800s
   Get dirty log over 2 iterations took 0.000072600s. (Avg 0.000036300s/iteration)
   Clear dirty log over 2 iterations took 0.025450200s. (Avg 0.012725100s/iteration)

Case 4: withfix patch, anonymous

   $ timeout 180 ./dirty_log_perf_test -e -s anonymous -v 1 -b 512M -i 2
   Random seed: 0x679b7adb
   Test iterations: 2
   Testing guest mode: PA-bits:50,  VA-bits:48,  4K pages
   Random seed: 0x1
   guest physical test memory: [0x3ffffdffff000, 0x3fffffffff000)
   Populate memory time: 8.066872600s
   Enabling dirty logging time: 0.001247200s

   Iteration 1 dirty memory time: 0.081895900s
   Iteration 1 get dirty log time: 0.000186200s
   Iteration 1 clear dirty log time: 0.014706800s
   Iteration 2 dirty memory time: 3.441165900s
   Iteration 2 get dirty log time: 0.000031500s
   Iteration 2 clear dirty log time: 0.013069200s
   Disabling dirty logging time: 0.000469100s
   Get dirty log over 2 iterations took 0.000217700s. (Avg 0.000108850s/iteration)
   Clear dirty log over 2 iterations took 0.027776000s. (Avg 0.013888000s/iteration)
   Testing guest mode: PA-bits:50,  VA-bits:39,  4K pages
   guest physical test memory: [0x3ffffdffff000, 0x3fffffffff000)
   Populate memory time: 7.804275600s
   Enabling dirty logging time: 0.003435000s

   Iteration 1 dirty memory time: 0.067627700s
   Iteration 1 get dirty log time: 0.000037100s
   Iteration 1 clear dirty log time: 0.013302700s
   Iteration 2 dirty memory time: 3.433272000s
   Iteration 2 get dirty log time: 0.000031800s
   Iteration 2 clear dirty log time: 0.013085400s
   Disabling dirty logging time: 0.000614100s
   Get dirty log over 2 iterations took 0.000068900s. (Avg 0.000034450s/iteration)
   Clear dirty log over 2 iterations took 0.026388100s. (Avg 0.013194050s/iteration)

[1] https://lore.kernel.org/linux-riscv/20260815103245.864925-1-jinyu.tang@linux.dev/

Thanks,
Shuai

> 
> Thanks,
> Daniel
> 
>> 
>> This is only a limited positive test. A Linux guest is not suitable for
>> constructing negative cases so I would not claim full Tested-by coverage.
>> 
>> Thanks,
>> Shuai
>> 
>>>
>>>   docs/specs/riscv-iommu.rst |   2 +
>>>   hw/riscv/riscv-iommu.c     | 254 +++++++++++++++++++++++++++++++++----
>>>   hw/riscv/riscv-iommu.h     |   1 +
>>>   3 files changed, 232 insertions(+), 25 deletions(-)
>>>
>>> -- 
>>> 2.53.0
>>>
> 



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates
  2026-11-24  5:18     ` Gong Shuai
@ 2026-11-24 19:58       ` Daniel Henrique Barboza
  0 siblings, 0 replies; 9+ messages in thread
From: Daniel Henrique Barboza @ 2026-11-24 19:58 UTC (permalink / raw)
  To: Gong Shuai, tanchi25
  Cc: alistair.francis, chao.liu, liwei1518, nutty.liu, palmer,
	pierrick.bouvier, qemu-devel, qemu-riscv, zhiwei_liu, gong.shuai

Hi Shuai,

On 11/24/2026 2:18 AM, Gong Shuai wrote:
> Hi Daniel,
> 
> On 11/24/2026 3:58 AM, Daniel Henrique Barboza wrote:
>> Hello,
>>
>> On 11/17/2026 11:30 AM, Gong Shuai wrote:
>>>> This series adds support for automatic updates of page-table accessed
>>>> (A) and dirty (D) bits in the RISC-V IOMMU, as specified by the RISC-V
>>>> IOMMU architecture. The feature is advertised through the AMO_HWAD
>>>> capability and controlled by the SADE and GADE bits in device contexts.
>>>>
>>>> The first two patches fix implicit G-stage page-table access handling
>>>> and IOATC permission checks. The third patch implements atomic A/D bit
>>>> updates using CAS, with page-table walk retries on concurrent
>>>> modifications and G-stage permission checks for implicit writes during
>>>> nested translation.
>>>>
>>>> The implementation has been validated with QTests, Linux DMA tests under
>>>> KVM and TCG, and concurrency tests covering CAS retries, PTE
>>>> modifications, and G-stage mapping changes.
>>>>
>>>> ---
>>>> Changes in v2:
>>>> - Set iotval2 to zero for S-stage page faults, as suggested by
>>>>    Daniel Henrique Barboza.
>>>> - Avoid duplicate implicit_read calculations, as suggested by Nutty Liu.
>>>> - Document the new amo-hwad device property.
>>>>
>>>> v1: https://lore.kernel.org/qemu-devel/20261108143934.129923-1-tanchi25@mails.ucas.ac.cn/
>>>>
>>>> Tan Chi (3):
>>>>    hw/riscv: iommu: Fix implicit G-stage page-table accesses
>>>>    hw/riscv: iommu: Check permissions on IOATC hits
>>>>    hw/riscv: iommu: Support automatic PTE A/D updates
>>>
>>> Hi Tan Chi,
>>>
>>> Thanks for your work.
>>>
>>> I have tested this series on my nested IOMMU setup.
>>>
>>> - Outer QEMU: this series on top of master, -M virt,...,iommu-sys=on,
>>>                -global riscv-iommu.amo-hwad=on, emulated NVMe
>>> - Inner QEMU: needs accel=on support for riscv-iommu-pci, and
>>>                vfio-pci with iommufd
>>> - Outer Linux host: needs hardware dirty tracking and nested IOMMU
>>>                support
>>>
>>> Test flow:
>>>
>>> 1. Boot host and guest, do NVMe I/O
>>> 2. Clear host S2 D bits and trigger GVMA inval in the inner QEMU monitor,
>>>     use command: calc_dirty_rate -b 1
>>> 3. Back to guest shell and do another NVMe I/O
>>>
>>> It completed without a guest hang or I/O error. With temporary local
>>> debug prints I could see en_s=1/en_g=1 and implicit G-stage page-table
>>> walks.
>>>
>>> One host-side workaround is that on the host I need
>>> echo never > /sys/kernel/mm/transparent_hugepage/enabled
>>> to avoid an hang with THP; otherwise calc_dirty_rate -b can leave the
>>> guest stuck.
>>
>>
>> Can you please clarify if this THP hang also happens on master?  It's not clear
>> to me if this scenario is already broken or if this automatic PTE A/D work is
>> exposing it.
> 
> Yes, the THP hang also occurs on master (2bfbe39d0fff).
> It is not introduced by the amo_hwad series.

Thanks for clarifying.  This means that we can proceed with the upstreaming of this
work without worrying about having to document regressions and what have you.

> 
>>
>> Even if it's a kernel/KVM side problem (as Tan Chi mentioned in another reply) we
>> should be wary of adding emulation changes that can regress regular software
>> usage.  At the very least we should document that we're doing an emulation change
>> that can break software that doesn't have a certain workaround/patch.
> 
> This is indeed a KVM-side issue. It is not related to nested IOMMU
> or the amo_hwad series; the hang was simply observed while running
> tests that involved them.
> 
> I can provide a simple setup to reproduce it.
> (Hope the following information is useful.)
> 
> With master QEMU as the RISC-V virt host and kernel v7.4-rc4 built
> with defconfig + KVM + TRANSPARENT_HUGEPAGE, running:
> 
>      timeout 180 ./dirty_log_perf_test \
>      -e -s anonymous_thp -v 1 -b 512M -i 2
> 
> hangs and exits due to timeout, whereas:
> 
>      timeout 180 ./dirty_log_perf_test \
>      -e -s anonymous -v 1 -b 512M -i 2
> 
> exits normally.
> 
> After applying the fix patch [1] to the kernel, both tests exit
> normally.
> 
> Detailed logs for the four command runs are provided below for
> reference.
> 
> Case 1: without fix patch, anonymous_thp ---- HANG and TIMEOUT(124)
> 
>    $ cat /sys/kernel/mm/transparent_hugepage/enabled
>    [always] madvise never
>    $ timeout 180 ./dirty_log_perf_test -e -s anonymous_thp -v 1 -b 512M -i 2
>    Random seed: 0x77be2754
>    Test iterations: 2
>    Testing guest mode: PA-bits:50,  VA-bits:48,  4K pages
>    Random seed: 0x1
>    guest physical test memory: [0x3ffffdfe00000, 0x3ffffffe00000)
>    Populate memory time: 0.277703400s
>    Enabling dirty logging time: 0.001101300s
> 
>    Iteration 1 dirty memory time: 0.086482400s
>    Iteration 1 get dirty log time: 0.000188500s
>    Iteration 1 clear dirty log time: 0.017205400s
>    Iteration 2 dirty memory time: 3.617855200s
>    Iteration 2 get dirty log time: 0.000093700s
>    Iteration 2 clear dirty log time: 0.014652300s
>    Disabling dirty logging time: 0.001215600s
> 
>    $ echo $?
>    124
> 
> Case 2: without fix patch, anonymous
> 
>    $ timeout 180 ./dirty_log_perf_test -e -s anonymous -v 1 -b 512M -i 2
>    Random seed: 0x68f16937
>    Test iterations: 2
>    Testing guest mode: PA-bits:50,  VA-bits:48,  4K pages
>    Random seed: 0x1
>    guest physical test memory: [0x3ffffdffff000, 0x3fffffffff000)
>    Populate memory time: 8.404907600s
>    Enabling dirty logging time: 0.001048700s
> 
>    Iteration 1 dirty memory time: 0.080460200s
>    Iteration 1 get dirty log time: 0.000119300s
>    Iteration 1 clear dirty log time: 0.014741000s
>    Iteration 2 dirty memory time: 3.689446400s
>    Iteration 2 get dirty log time: 0.000034200s
>    Iteration 2 clear dirty log time: 0.013190500s
>    Disabling dirty logging time: 0.000651400s
>    Get dirty log over 2 iterations took 0.000153500s. (Avg 0.000076750s/iteration)
>    Clear dirty log over 2 iterations took 0.027931500s. (Avg 0.013965750s/iteration)
>    Testing guest mode: PA-bits:50,  VA-bits:39,  4K pages
>    guest physical test memory: [0x3ffffdffff000, 0x3fffffffff000)
>    Populate memory time: 7.828295400s
>    Enabling dirty logging time: 0.000798200s
> 
>    Iteration 1 dirty memory time: 0.066219100s
>    Iteration 1 get dirty log time: 0.000039900s
>    Iteration 1 clear dirty log time: 0.023695500s
>    Iteration 2 dirty memory time: 3.429129500s
>    Iteration 2 get dirty log time: 0.000032200s
>    Iteration 2 clear dirty log time: 0.020796400s
>    Disabling dirty logging time: 0.000772600s
>    Get dirty log over 2 iterations took 0.000072100s. (Avg 0.000036050s/iteration)
>    Clear dirty log over 2 iterations took 0.044491900s. (Avg 0.022245950s/iteration)
> 
> Case 3: withfix patch, anonymous_thp
> 
>    $ cat /sys/kernel/mm/transparent_hugepage/enabled
>    [always] madvise never
>    $ timeout 180 ./dirty_log_perf_test -e -s anonymous_thp -v 1 -b 512M -i 2
>    Random seed: 0x3b6f1968
>    Test iterations: 2
>    Testing guest mode: PA-bits:50,  VA-bits:48,  4K pages
>    Random seed: 0x1
>    guest physical test memory: [0x3ffffdfe00000, 0x3ffffffe00000)
>    Populate memory time: 0.221626400s
>    Enabling dirty logging time: 0.001002400s
> 
>    Iteration 1 dirty memory time: 0.079966800s
>    Iteration 1 get dirty log time: 0.000119400s
>    Iteration 1 clear dirty log time: 0.012897100s
>    Iteration 2 dirty memory time: 3.490279400s
>    Iteration 2 get dirty log time: 0.000044900s
>    Iteration 2 clear dirty log time: 0.013184200s
>    Disabling dirty logging time: 0.000883200s
>    Get dirty log over 2 iterations took 0.000164300s. (Avg 0.000082150s/iteration)
>    Clear dirty log over 2 iterations took 0.026081300s. (Avg 0.013040650s/iteration)
>    Testing guest mode: PA-bits:50,  VA-bits:39,  4K pages
>    guest physical test memory: [0x3ffffdfe00000, 0x3ffffffe00000)
>    Populate memory time: 0.209773300s
>    Enabling dirty logging time: 0.000790800s
> 
>    Iteration 1 dirty memory time: 0.063432900s
>    Iteration 1 get dirty log time: 0.000040100s
>    Iteration 1 clear dirty log time: 0.012308600s
>    Iteration 2 dirty memory time: 3.367311900s
>    Iteration 2 get dirty log time: 0.000032500s
>    Iteration 2 clear dirty log time: 0.013141600s
>    Disabling dirty logging time: 0.000953800s
>    Get dirty log over 2 iterations took 0.000072600s. (Avg 0.000036300s/iteration)
>    Clear dirty log over 2 iterations took 0.025450200s. (Avg 0.012725100s/iteration)
> 
> Case 4: withfix patch, anonymous
> 
>    $ timeout 180 ./dirty_log_perf_test -e -s anonymous -v 1 -b 512M -i 2
>    Random seed: 0x679b7adb
>    Test iterations: 2
>    Testing guest mode: PA-bits:50,  VA-bits:48,  4K pages
>    Random seed: 0x1
>    guest physical test memory: [0x3ffffdffff000, 0x3fffffffff000)
>    Populate memory time: 8.066872600s
>    Enabling dirty logging time: 0.001247200s
> 
>    Iteration 1 dirty memory time: 0.081895900s
>    Iteration 1 get dirty log time: 0.000186200s
>    Iteration 1 clear dirty log time: 0.014706800s
>    Iteration 2 dirty memory time: 3.441165900s
>    Iteration 2 get dirty log time: 0.000031500s
>    Iteration 2 clear dirty log time: 0.013069200s
>    Disabling dirty logging time: 0.000469100s
>    Get dirty log over 2 iterations took 0.000217700s. (Avg 0.000108850s/iteration)
>    Clear dirty log over 2 iterations took 0.027776000s. (Avg 0.013888000s/iteration)
>    Testing guest mode: PA-bits:50,  VA-bits:39,  4K pages
>    guest physical test memory: [0x3ffffdffff000, 0x3fffffffff000)
>    Populate memory time: 7.804275600s
>    Enabling dirty logging time: 0.003435000s
> 
>    Iteration 1 dirty memory time: 0.067627700s
>    Iteration 1 get dirty log time: 0.000037100s
>    Iteration 1 clear dirty log time: 0.013302700s
>    Iteration 2 dirty memory time: 3.433272000s
>    Iteration 2 get dirty log time: 0.000031800s
>    Iteration 2 clear dirty log time: 0.013085400s
>    Disabling dirty logging time: 0.000614100s
>    Get dirty log over 2 iterations took 0.000068900s. (Avg 0.000034450s/iteration)
>    Clear dirty log over 2 iterations took 0.026388100s. (Avg 0.013194050s/iteration)
> 
> [1] https://lore.kernel.org/linux-riscv/20260815103245.864925-1-jinyu.tang@linux.dev/


Thanks for the detailed setup!  I bookmarked it for future reference when, hopefully,
the KVM fix is in mainline and then we have a way of testing on our end too.


Cheers,
Daniel


> 
> Thanks,
> Shuai
> 
>>
>> Thanks,
>> Daniel
>>
>>>
>>> This is only a limited positive test. A Linux guest is not suitable for
>>> constructing negative cases so I would not claim full Tested-by coverage.
>>>
>>> Thanks,
>>> Shuai
>>>
>>>>
>>>>   docs/specs/riscv-iommu.rst |   2 +
>>>>   hw/riscv/riscv-iommu.c     | 254 +++++++++++++++++++++++++++++++++----
>>>>   hw/riscv/riscv-iommu.h     |   1 +
>>>>   3 files changed, 232 insertions(+), 25 deletions(-)
>>>>
>>>> -- 
>>>> 2.53.0
>>>>
>>
> 



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-11-24 19:58 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-13 15:12 [PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates Tan Chi
2026-11-13 15:12 ` [PATCH v2 1/3] hw/riscv: iommu: Fix implicit G-stage page-table accesses Tan Chi
2026-11-13 15:12 ` [PATCH v2 2/3] hw/riscv: iommu: Check permissions on IOATC hits Tan Chi
2026-11-13 15:12 ` [PATCH v2 3/3] hw/riscv: iommu: Support automatic PTE A/D updates Tan Chi
2026-11-17 14:30 ` [PATCH v2 0/3] " Gong Shuai
2026-11-18 14:40   ` Tan Chi
2026-11-23 19:58   ` Daniel Henrique Barboza
2026-11-24  5:18     ` Gong Shuai
2026-11-24 19:58       ` Daniel Henrique Barboza

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.