[PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates

Tan Chi posted 3 patches 1 week, 2 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261113151251.30618-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>, Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
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(-)
[PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates
Posted by Tan Chi 1 week, 2 days ago
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
Re: [PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates
Posted by Gong Shuai 5 days, 13 hours ago
> 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
>
Re: [PATCH v2 0/3] hw/riscv: iommu: Support automatic PTE A/D updates
Posted by Tan Chi 4 days, 13 hours ago
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