Skip to contentSkip to conversation

Fix probe_pages and vext_ldff issues

PATCH Applied Max Chou 4 participants 11 messages Mar 18 – Mar 23 RISC-V

lore
Share & tools
lore

Max Chou

author
#0 lore raw

cover Fix probe_pages and vext_ldff issues

This patchset fixes following issues in the RISC-V vector fault-only-first load implementation and the probe_pages helper function.

1. probe_pages flag handling:
When probing memory spanning two pages, probe_pages calls probe_access_flags twice. The flags from the second page were overwriting the first page's flags instead of being merged, causing watchpoint and other TLB attributes to be lost.

2. vext_ldff cross-page logic:
The manual two-phase probing in vext_ldff had three issues:
a) Wrong condition: checked "env->vl > elems" instead of

      "env->vl > elems + env->vstart", missing the vstart offset

b) Wrong address: used "addr + (elems << log2_esz)" instead of "addr + page_split", probing incorrect addresses for segment loads (nf > 1)
c) Wrong size: used "elems * msize" (first page size) instead of calculating remaining size, potentially missing faults

Patch 1 fixes the probe_pages helper to properly merge flags from both pages.

Patch 2 fixes vext_ldff by replacing the buggy manual cross-page logic by a single probe_pages call with the correct total size. This leverages the infrastructure that probe_pages already provides for handling cross-page accesses automatically.

rnax

Max Chou (2):
target/riscv: rvv: Fix missing flags merge in probe_pages for
cross-page accesses
target/riscv: rvv: Fix page probe issues in vext_ldff

 target/riscv/vector_helper.c | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)
Signature · 1 line
2.43.7
author
#1 lore raw

PATCH 1/2 target/riscv: rvv: Fix missing flags merge in probe_pages for cross-page accesses

When probe_pages probes a memory region that spans two pages, it calls probe_access_flags twice - once for each page. However, the flags from the second page probe were overwriting the flags from the first page instead of being merged together.

---
 target/riscv/vector_helper.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
target/riscv/vector_helper.c +3 -4
index 3334662dcd..a74ce70943 100644
@@ -265,9 +265,9 @@ static void probe_pages(CPURISCVState *env, target_ulong addr, target_ulong len,
         addr += curlen;
         curlen = len - curlen;
         if (flags != NULL) {
-            *flags = probe_access_flags(env, adjust_addr(env, addr), curlen,
-                                        access_type, mmu_index, nonfault,
-                                        host, ra);
+            *flags |= probe_access_flags(env, adjust_addr(env, addr), curlen,
+                                         access_type, mmu_index, nonfault,
+                                         host, ra);
         } else {
             probe_access(env, adjust_addr(env, addr), curlen, access_type,
                          mmu_index, ra);
@@ -275,7 +275,6 @@ static void probe_pages(CPURISCVState *env, target_ulong addr, target_ulong len,
     }
 }
 
-
 static inline void vext_set_elem_mask(void *v0, int index,
                                       uint8_t value)
 {
Signature · 1 line
2.43.7
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] target/riscv: rvv: Fix missing flags merge in probe_pages for cross-page accesses

Quoted 7 lines · Max Chou

When probe_pages probes a memory region that spans two pages, it calls probe_access_flags twice - once for each page. However, the flags from the second page probe were overwriting the flags from the first page instead of being merged together.

Alistair

Quoted 33 lines
---
 target/riscv/vector_helper.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
target/riscv/vector_helper.c +3 -5
index 3334662dcd..a74ce70943 100644
@@ -265,9 +265,9 @@ static void probe_pages(CPURISCVState *env, target_ulong addr, target_ulong len,
         addr += curlen;
         curlen = len - curlen;
         if (flags != NULL) {
-            *flags = probe_access_flags(env, adjust_addr(env, addr), curlen,
-                                        access_type, mmu_index, nonfault,
-                                        host, ra);
+            *flags |= probe_access_flags(env, adjust_addr(env, addr), curlen,
+                                         access_type, mmu_index, nonfault,
+                                         host, ra);
         } else {
             probe_access(env, adjust_addr(env, addr), curlen, access_type,
                          mmu_index, ra);
@@ -275,7 +275,6 @@ static void probe_pages(CPURISCVState *env, target_ulong addr, target_ulong len,
     }
 }
 
-
 static inline void vext_set_elem_mask(void *v0, int index,
                                       uint8_t value)
 {
--

2.43.7

author
#3 lore raw

PATCH 2/2 target/riscv: rvv: Fix page probe issues in vext_ldff

In reply to Max Chou

Commit 17288e38bebf ("optimize the memory probing for vector fault-only-first loads") introduced an optimization that moved from per-element probing to a fast-path broad probe. Unfortunately it introduced following bugs in cross-page handling:

- Wrong condition for second page probing: checked "env->vl > elems" instead of "env->vl > elems + env->vstart", failing to account for the vstart offset.

- Incorrect second page address calculation: used
"addr + (elems << log2_esz)" instead of "addr + page_split". For segment loads (nf > 1), this would probe the wrong address,not at the page boundary.

- Wrong second page probe size: used "elems * msize" (the first page size) instead of calculating the remaining size as "(env->vl - env->vstart) * msize - page_split". This would probe too little memory and could miss faults.

This commit fixes these bugs by leveraging the probe_pages helper which automatically handles cross-page memory accesses correctly.

Fixes 17288e38bebf ("optimize the memory probing for vector fault-only-first loads.")
---
 target/riscv/vector_helper.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
target/riscv/vector_helper.c +4 -12
index a74ce70943..f038997095 100644
@@ -772,9 +772,9 @@ vext_ldff(void *vd, void *v0, target_ulong base, CPURISCVState *env,
     uint32_t esz = 1 << log2_esz;
     uint32_t msize = nf * esz;
     uint32_t vma = vext_vma(desc);
-    target_ulong addr, addr_probe, addr_i, offset, remain, page_split, elems;
+    target_ulong addr, addr_i, offset, remain, page_split, elems;
     int mmu_index = riscv_env_mmu_index(env, false);
-    int flags, probe_flags;
+    int flags;
     void *host;
 
     VSTART_CHECK_EARLY_EXIT(env, env->vl);
@@ -788,16 +788,8 @@ vext_ldff(void *vd, void *v0, target_ulong base, CPURISCVState *env,
     }
 
     /* Check page permission/pmp/watchpoint/etc. */
-    probe_pages(env, addr, elems * msize, ra, MMU_DATA_LOAD, mmu_index, &host,
-                &flags, true);
-
-    /* If we are crossing a page check also the second page. */
-    if (env->vl > elems) {
-        addr_probe = addr + (elems << log2_esz);
-        probe_pages(env, addr_probe, elems * msize, ra, MMU_DATA_LOAD,
-                    mmu_index, &host, &probe_flags, true);
-        flags |= probe_flags;
-    }
+    probe_pages(env, addr, (env->vl - env->vstart) * msize, ra, MMU_DATA_LOAD,
+                mmu_index, &host, &flags, true);
 
     if (flags & ~TLB_WATCHPOINT) {
         /* probe every access */
Signature · 1 line
2.43.7
Acked-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] target/riscv: rvv: Fix page probe issues in vext_ldff

Quoted 20 lines · Max Chou

Commit 17288e38bebf ("optimize the memory probing for vector fault-only-first loads") introduced an optimization that moved from per-element probing to a fast-path broad probe. Unfortunately it introduced following bugs in cross-page handling:

- Wrong condition for second page probing: checked "env->vl > elems" instead of "env->vl > elems + env->vstart", failing to account for the vstart offset.

- Incorrect second page address calculation: used
"addr + (elems << log2_esz)" instead of "addr + page_split". For segment loads (nf > 1), this would probe the wrong address,not at the page boundary.

- Wrong second page probe size: used "elems * msize" (the first page size) instead of calculating the remaining size as "(env->vl - env->vstart) * msize - page_split". This would probe too little memory and could miss faults.

Quoted 6 lines

This commit fixes these bugs by leveraging the probe_pages helper which automatically handles cross-page memory accesses correctly.

Fixes 17288e38bebf ("optimize the memory probing for vector fault-only-first loads.")

Alistair

Quoted 43 lines
---
 target/riscv/vector_helper.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)
target/riscv/vector_helper.c +4 -13
index a74ce70943..f038997095 100644
@@ -772,9 +772,9 @@ vext_ldff(void *vd, void *v0, target_ulong base, CPURISCVState *env,
     uint32_t esz = 1 << log2_esz;
     uint32_t msize = nf * esz;
     uint32_t vma = vext_vma(desc);
-    target_ulong addr, addr_probe, addr_i, offset, remain, page_split, elems;
+    target_ulong addr, addr_i, offset, remain, page_split, elems;
     int mmu_index = riscv_env_mmu_index(env, false);
-    int flags, probe_flags;
+    int flags;
     void *host;
 
     VSTART_CHECK_EARLY_EXIT(env, env->vl);
@@ -788,16 +788,8 @@ vext_ldff(void *vd, void *v0, target_ulong base, CPURISCVState *env,
     }
 
     /* Check page permission/pmp/watchpoint/etc. */
-    probe_pages(env, addr, elems * msize, ra, MMU_DATA_LOAD, mmu_index, &host,
-                &flags, true);
-
-    /* If we are crossing a page check also the second page. */
-    if (env->vl > elems) {
-        addr_probe = addr + (elems << log2_esz);
-        probe_pages(env, addr_probe, elems * msize, ra, MMU_DATA_LOAD,
-                    mmu_index, &host, &probe_flags, true);
-        flags |= probe_flags;
-    }
+    probe_pages(env, addr, (env->vl - env->vstart) * msize, ra, MMU_DATA_LOAD,
+                mmu_index, &host, &flags, true);
 
     if (flags & ~TLB_WATCHPOINT) {
         /* probe every access */
--

2.43.7

#5 lore raw
In reply to Max Chou
Quoted 30 lines · Max Chou

This patchset fixes following issues in the RISC-V vector fault-only-first load implementation and the probe_pages helper function.

1. probe_pages flag handling:
When probing memory spanning two pages, probe_pages calls probe_access_flags twice. The flags from the second page were overwriting the first page's flags instead of being merged, causing watchpoint and other TLB attributes to be lost.

2. vext_ldff cross-page logic:
The manual two-phase probing in vext_ldff had three issues:
a) Wrong condition: checked "env->vl > elems" instead of

      "env->vl > elems + env->vstart", missing the vstart offset

b) Wrong address: used "addr + (elems << log2_esz)" instead of "addr + page_split", probing incorrect addresses for segment loads (nf > 1)
c) Wrong size: used "elems * msize" (first page size) instead of calculating remaining size, potentially missing faults

Patch 1 fixes the probe_pages helper to properly merge flags from both pages.

Patch 2 fixes vext_ldff by replacing the buggy manual cross-page logic by a single probe_pages call with the correct total size. This leverages the infrastructure that probe_pages already provides for handling cross-page accesses automatically.

Quoted 7 lines

rnax

Max Chou (2):
target/riscv: rvv: Fix missing flags merge in probe_pages for
cross-page accesses
target/riscv: rvv: Fix page probe issues in vext_ldff

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 8 lines
 target/riscv/vector_helper.c | 23 +++++++----------------
 1 file changed, 7 insertions(+), 16 deletions(-)

--
2.43.7

#6 lore raw
In reply to Max Chou
Quoted 24 lines · Max Chou

This patchset fixes following issues in the RISC-V vector fault-only-first load implementation and the probe_pages helper function.

1. probe_pages flag handling:
When probing memory spanning two pages, probe_pages calls probe_access_flags twice. The flags from the second page were overwriting the first page's flags instead of being merged, causing watchpoint and other TLB attributes to be lost.

2. vext_ldff cross-page logic:
The manual two-phase probing in vext_ldff had three issues:
a) Wrong condition: checked "env->vl > elems" instead of "env->vl > elems + env->vstart", missing the vstart offset
b) Wrong address: used "addr + (elems << log2_esz)" instead of "addr + page_split", probing incorrect addresses for segment loads (nf > 1)
c) Wrong size: used "elems * msize" (first page size) instead of calculating remaining size, potentially missing faults

Patch 1 fixes the probe_pages helper to properly merge flags from both pages.

Quoted 6 lines

Patch 2 fixes vext_ldff by replacing the buggy manual cross-page logic by a single probe_pages call with the correct total size. This leverages the infrastructure that probe_pages already provides for handling cross-page accesses automatically.

rnax

This feels like a qemu-stable material but I'm not sure about that.

Please let me know if these fixes should be picked up for the current stable releases of qemu, especially for 10.0.x series which is currently an LTS series.

Thanks,

/mjt

author
#7 lore raw
Quoted 9 lines · Michael Tokarev

This feels like a qemu-stable material but I'm not sure about that.

Please let me know if these fixes should be picked up for the current stable releases of qemu, especially for 10.0.x series which is currently an LTS series.

Thanks,

/mjt

Hi Michael,

Yes, I believe this patchset should be picked up for the current stable release. These patches address the issues that were introduced after v10.0.0-rc0.

Thanks,

rnax

#8 lore raw
Quoted 3 lines · Max Chou

Yes, I believe this patchset should be picked up for the current stable release. These patches address the issues that were introduced after v10.0.0-rc0.

This makes sense.

However, 10.0 lacks d887736225 "Expand the probe_pages helper function to handle probe flags", which makes the first change in this series (5568177738 "target/riscv: rvv: Fix missing flags merge in probe_pages for cross-page accesses") non-applicable.

Is it okay to pick d887736225 for 10.0.x too? (Adding Paolo Savini to the Cc list).

The result works and passes tests but since it's not my area it'd be nice if someone can confirm/deny. Or maybe some other fixes should be picked up for 10.0.x too. The current result is at
https://gitlab.com/mjt0k/qemu/-/commits/staging-10.0

Thanks!

/mjt

author
#9 lore raw
Quoted 10 lines · Michael Tokarev

This makes sense.

However, 10.0 lacks d887736225 "Expand the probe_pages helper function to handle probe flags", which makes the first change in this series (5568177738 "target/riscv: rvv: Fix missing flags merge in probe_pages for cross-page accesses") non-applicable.

Is it okay to pick d887736225 for 10.0.x too? (Adding Paolo Savini to the Cc list).

Quoted 8 lines

The result works and passes tests but since it's not my area it'd be nice if someone can confirm/deny. Or maybe some other fixes should be picked up for 10.0.x too. The current result is at
https://gitlab.com/mjt0k/qemu/-/commits/staging-10.0

Thanks!

/mjt

Hi Michael,

Regarding commit d887736225, I agree with you that it should be picked up for the 10.0.x series. It includes the necessary modifications for probe_pages that enable the first patch of this patchset to apply correctly, so I think that is okay to include.

I've checked the cherry-picks (3b3e6682/2f4335d7/ba624f3f) in the stable-10.0, and they LGTM. It appears to include the related commits of this patchset.

Thanks,

rnax

#10 lore raw

Hi Michael,

Thanks for this. I agree commitd887736225 is needed and adding it looks

legitimate to me.

Best wishes

Paolo Savini

Quoted 33 lines · Max Chou
Quoted 10 lines · Michael Tokarev

This makes sense.

However, 10.0 lacks d887736225 "Expand the probe_pages helper function to handle probe flags", which makes the first change in this series (5568177738 "target/riscv: rvv: Fix missing flags merge in probe_pages for cross-page accesses") non-applicable.

Is it okay to pick d887736225 for 10.0.x too? (Adding Paolo Savini to the Cc list).

Quoted 8 lines

The result works and passes tests but since it's not my area it'd be nice if someone can confirm/deny. Or maybe some other fixes should be picked up for 10.0.x too. The current result is at
https://gitlab.com/mjt0k/qemu/-/commits/staging-10.0

Thanks!

/mjt

Hi Michael,

Regarding commit d887736225, I agree with you that it should be picked up for the 10.0.x series. It includes the necessary modifications for probe_pages that enable the first patch of this patchset to apply correctly, so I think that is okay to include.

I've checked the cherry-picks (3b3e6682/2f4335d7/ba624f3f) in the stable-10.0, and they LGTM. It appears to include the related commits of this patchset.

Thanks,

rnax