Skip to contentSkip to conversation

target/riscv: pmp: Fixup TLB size calculation

PATCH Applied Alistair Francis 4 participants 6 messages Oct 12, 2022 – Oct 14, 2022 RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv: pmp: Fixup TLB size calculation

From: Alistair Francis <[email protected]>

Since commit 4047368938f6 "accel/tcg: Introduce tlb_set_page_full" we have been seeing this assert

    ../accel/tcg/cputlb.c:1294: tlb_set_page_with_attrs: Assertion `is_power_of_2(size)' failed.

When running Tock on the OpenTitan machine.

The issue is that pmp_get_tlb_size() would return a TLB size that wasn't a power of 2. The size was also smaller then TARGET_PAGE_SIZE.

This patch ensures that any TLB size less then TARGET_PAGE_SIZE is rounded down to 1 to ensure it's a valid size.

---
This is based on advice from Richard:
https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/#25043166

 target/riscv/pmp.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
target/riscv/pmp.c +12 -0
index ea2b67d947..2b43e399b8 100644
@@ -628,6 +628,18 @@ bool pmp_is_range_in_tlb(CPURISCVState *env, hwaddr tlb_sa,
     }
 
     if (*tlb_size != 0) {
+        /*
+         * At this point we have a tlb_size that is the smallest possible size
+         * That fits within a TARGET_PAGE_SIZE and the PMP region.
+         *
+         * If the size is less then TARGET_PAGE_SIZE we drop the size to 1.
+         * This means the result isn't cached in the TLB and is only used for
+         * a single translation.
+         */
+        if (*tlb_size < TARGET_PAGE_SIZE) {
+            *tlb_size = 1;
+        }
+
         return true;
     }
 
Signature · 1 line
2.37.3
Reviewed-by on v1 1/1
#1 lore raw

By the way, we missed one related patch that once had been picked to riscv-next patch.

The patch v3:
https://lore.kernel.org/all/[email protected]/T/#m183e4430bda408bc3a2b2751aa94eff7fc02e23c

The patch v4:
https://lists.gnu.org/archive/html/qemu-devel/2021-12/msg02854.html

I think the patch v4 should be taken at the same time with this patch.

Thanks,
Zhiwei

Quoted 46 lines · Alistair Francis

From: Alistair Francis <[email protected]>

Since commit 4047368938f6 "accel/tcg: Introduce tlb_set_page_full" we have been seeing this assert

     ../accel/tcg/cputlb.c:1294: tlb_set_page_with_attrs: Assertion `is_power_of_2(size)' failed.

When running Tock on the OpenTitan machine.

The issue is that pmp_get_tlb_size() would return a TLB size that wasn't a power of 2. The size was also smaller then TARGET_PAGE_SIZE.

This patch ensures that any TLB size less then TARGET_PAGE_SIZE is rounded down to 1 to ensure it's a valid size.

---
This is based on advice from Richard:
https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/#25043166

  target/riscv/pmp.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
target/riscv/pmp.c +12 -0
index ea2b67d947..2b43e399b8 100644
@@ -628,6 +628,18 @@ bool pmp_is_range_in_tlb(CPURISCVState *env, hwaddr tlb_sa,
      }
 
      if (*tlb_size != 0) {
+        /*
+         * At this point we have a tlb_size that is the smallest possible size
+         * That fits within a TARGET_PAGE_SIZE and the PMP region.
+         *
+         * If the size is less then TARGET_PAGE_SIZE we drop the size to 1.
+         * This means the result isn't cached in the TLB and is only used for
+         * a single translation.
+         */
+        if (*tlb_size < TARGET_PAGE_SIZE) {
+            *tlb_size = 1;
+        }
+
          return true;
      }
 
#2 lore raw
Quoted 2 lines · LIU Zhiwei

Thanks!

Quoted 5 lines

By the way, we missed one related patch that once had been picked to riscv-next patch.

The patch v3:
https://lore.kernel.org/all/[email protected]/T/#m183e4430bda408bc3a2b2751aa94eff7fc02e23c

So this was applied but caused boot failures so it was dropped from my RISC-V tree

Quoted 3 lines

The patch v4:
https://lists.gnu.org/archive/html/qemu-devel/2021-12/msg02854.html

I think I misunderstood this comment [1] as applying to v4 and it never got applied.

Do you mind resending the patch?

1: https://lore.kernel.org/all/[email protected]/T/#m5e958d702d9905169a941f2ae59fdf7ac4a02383

Alistair

Quoted 53 lines

I think the patch v4 should be taken at the same time with this patch.

Thanks,
Zhiwei

Quoted 46 lines · Alistair Francis

From: Alistair Francis <[email protected]>

Since commit 4047368938f6 "accel/tcg: Introduce tlb_set_page_full" we have been seeing this assert

     ../accel/tcg/cputlb.c:1294: tlb_set_page_with_attrs: Assertion `is_power_of_2(size)' failed.

When running Tock on the OpenTitan machine.

The issue is that pmp_get_tlb_size() would return a TLB size that wasn't a power of 2. The size was also smaller then TARGET_PAGE_SIZE.

This patch ensures that any TLB size less then TARGET_PAGE_SIZE is rounded down to 1 to ensure it's a valid size.

---
This is based on advice from Richard:
https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/#25043166

  target/riscv/pmp.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
target/riscv/pmp.c +12 -0
index ea2b67d947..2b43e399b8 100644
@@ -628,6 +628,18 @@ bool pmp_is_range_in_tlb(CPURISCVState *env, hwaddr tlb_sa,
      }
 
      if (*tlb_size != 0) {
+        /*
+         * At this point we have a tlb_size that is the smallest possible size
+         * That fits within a TARGET_PAGE_SIZE and the PMP region.
+         *
+         * If the size is less then TARGET_PAGE_SIZE we drop the size to 1.
+         * This means the result isn't cached in the TLB and is only used for
+         * a single translation.
+         */
+        if (*tlb_size < TARGET_PAGE_SIZE) {
+            *tlb_size = 1;
+        }
+
          return true;
      }
 
#3 lore raw
Quoted 12 lines · Alistair Francis
Quoted 1 line · LIU Zhiwei

Thanks!

Quoted 4 lines

By the way, we missed one related patch that once had been picked to riscv-next patch.

The patch v3:
https://lore.kernel.org/all/[email protected]/T/#m183e4430bda408bc3a2b2751aa94eff7fc02e23c

So this was applied but caused boot failures so it was dropped from my RISC-V tree

Quoted 6 lines
Quoted 2 lines

The patch v4:
https://lists.gnu.org/archive/html/qemu-devel/2021-12/msg02854.html

I think I misunderstood this comment [1] as applying to v4 and it never got applied.

Do you mind resending the patch?

Sure. I have  rebased it to your patch and sent it to the mail list.

Thanks,
Zhiwei

Quoted 57 lines

1: https://lore.kernel.org/all/[email protected]/T/#m5e958d702d9905169a941f2ae59fdf7ac4a02383

Alistair

Quoted 52 lines

I think the patch v4 should be taken at the same time with this patch.

Thanks,
Zhiwei

Quoted 46 lines · Alistair Francis

From: Alistair Francis <[email protected]>

Since commit 4047368938f6 "accel/tcg: Introduce tlb_set_page_full" we have been seeing this assert

      ../accel/tcg/cputlb.c:1294: tlb_set_page_with_attrs: Assertion `is_power_of_2(size)' failed.

When running Tock on the OpenTitan machine.

The issue is that pmp_get_tlb_size() would return a TLB size that wasn't a power of 2. The size was also smaller then TARGET_PAGE_SIZE.

This patch ensures that any TLB size less then TARGET_PAGE_SIZE is rounded down to 1 to ensure it's a valid size.

---
This is based on advice from Richard:
https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/#25043166

   target/riscv/pmp.c | 12 ++++++++++++
   1 file changed, 12 insertions(+)
target/riscv/pmp.c +12 -0
index ea2b67d947..2b43e399b8 100644
@@ -628,6 +628,18 @@ bool pmp_is_range_in_tlb(CPURISCVState *env, hwaddr tlb_sa,
       }
 
       if (*tlb_size != 0) {
+        /*
+         * At this point we have a tlb_size that is the smallest possible size
+         * That fits within a TARGET_PAGE_SIZE and the PMP region.
+         *
+         * If the size is less then TARGET_PAGE_SIZE we drop the size to 1.
+         * This means the result isn't cached in the TLB and is only used for
+         * a single translation.
+         */
+        if (*tlb_size < TARGET_PAGE_SIZE) {
+            *tlb_size = 1;
+        }
+
           return true;
       }
 
Reviewed-by on v1 1/1
#4 lore raw
In reply to Alistair Francis
Quoted 15 lines · Alistair Francis

From: Alistair Francis<[email protected]>

Since commit 4047368938f6 "accel/tcg: Introduce tlb_set_page_full" we have been seeing this assert

     ../accel/tcg/cputlb.c:1294: tlb_set_page_with_attrs: Assertion `is_power_of_2(size)' failed.

When running Tock on the OpenTitan machine.

The issue is that pmp_get_tlb_size() would return a TLB size that wasn't a power of 2. The size was also smaller then TARGET_PAGE_SIZE.

This patch ensures that any TLB size less then TARGET_PAGE_SIZE is rounded down to 1 to ensure it's a valid size.

Quoted 7 lines

---
This is based on advice from Richard:
https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/#25043166

  target/riscv/pmp.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)

r~

#5 lore raw
In reply to Alistair Francis
Quoted 10 lines · Alistair Francis

From: Alistair Francis <[email protected]>

Since commit 4047368938f6 "accel/tcg: Introduce tlb_set_page_full" we have been seeing this assert

    ../accel/tcg/cputlb.c:1294: tlb_set_page_with_attrs: Assertion `is_power_of_2(size)' failed.

When running Tock on the OpenTitan machine.

Quoted 7 lines

The issue is that pmp_get_tlb_size() would return a TLB size that wasn't a power of 2. The size was also smaller then TARGET_PAGE_SIZE.

This patch ensures that any TLB size less then TARGET_PAGE_SIZE is rounded down to 1 to ensure it's a valid size.

Thanks!

Applied to riscv-to-apply.next

Alistair

Quoted 33 lines

---
This is based on advice from Richard:
https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/#25043166

 target/riscv/pmp.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
target/riscv/pmp.c +12 -1
index ea2b67d947..2b43e399b8 100644
@@ -628,6 +628,18 @@ bool pmp_is_range_in_tlb(CPURISCVState *env, hwaddr tlb_sa,
     }
 
     if (*tlb_size != 0) {
+        /*
+         * At this point we have a tlb_size that is the smallest possible size
+         * That fits within a TARGET_PAGE_SIZE and the PMP region.
+         *
+         * If the size is less then TARGET_PAGE_SIZE we drop the size to 1.
+         * This means the result isn't cached in the TLB and is only used for
+         * a single translation.
+         */
+        if (*tlb_size < TARGET_PAGE_SIZE) {
+            *tlb_size = 1;
+        }
+
         return true;
     }
 
--

2.37.3