Skip to contentSkip to conversation

target/riscv: reduce overhead of MSTATUS_SUM change

PATCH Superseded [email protected] 4 participants 20 messages Mar 21, 2023 – Mar 22, 2023 RISC-V

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/riscv: reduce overhead of MSTATUS_SUM change

From: Fei Wu <[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many other syscalls benefit a lot from this one too.

---
 target/riscv/cpu.h        |  4 ++++
 target/riscv/cpu_helper.c |  7 +++++++
 target/riscv/csr.c        | 14 +++++++++++++-
 3 files changed, 24 insertions(+), 1 deletion(-)
target/riscv/cpu.h +4 -0
index 638e47c75a..926dbce59f 100644
@@ -383,6 +383,10 @@ struct CPUArchState {
     uint64_t kvm_timer_compare;
     uint64_t kvm_timer_state;
     uint64_t kvm_timer_frequency;
+
+#define MAX_CACHED_SUM_U_ADDR_NUM 4
+    uint64_t sum_u_count;
+    uint64_t sum_u_addr[MAX_CACHED_SUM_U_ADDR_NUM];
 };
 
 OBJECT_DECLARE_CPU_TYPE(RISCVCPU, RISCVCPUClass, RISCV_CPU)
target/riscv/cpu_helper.c +7 -0
index f88c503cf4..5ad0418eb6 100644
@@ -1068,6 +1068,13 @@ restart:
                     (access_type == MMU_DATA_STORE || (pte & PTE_D))) {
                 *prot |= PAGE_WRITE;
             }
+            if ((pte & PTE_U) && (mode & PRV_S) &&
+                    get_field(env->mstatus, MSTATUS_SUM)) {
+                if (env->sum_u_count < MAX_CACHED_SUM_U_ADDR_NUM) {
+                    env->sum_u_addr[env->sum_u_count] = addr;
+                }
+                ++env->sum_u_count;
+            }
             return TRANSLATE_SUCCESS;
         }
     }
target/riscv/csr.c +13 -1
index ab566639e5..74b7638c8a 100644
@@ -1246,9 +1246,21 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
 
     /* flush tlb on mstatus fields that affect VM */
     if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV |
-            MSTATUS_MPRV | MSTATUS_SUM)) {
+            MSTATUS_MPRV)) {
         tlb_flush(env_cpu(env));
+        env->sum_u_count = 0;
+    } else if ((mstatus & MSTATUS_SUM) && !(val & MSTATUS_SUM)) {
+        if (env->sum_u_count > MAX_CACHED_SUM_U_ADDR_NUM) {
+            tlb_flush(env_cpu(env));
+        } else {
+            for (int i = 0; i < env->sum_u_count; ++i) {
+                tlb_flush_page_by_mmuidx(env_cpu(env), env->sum_u_addr[i],
+                                         1 << PRV_S | 1 << PRV_M);
+            }
+        }
+        env->sum_u_count = 0;
     }
+
     mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
         MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM |
         MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
Signature · 1 line
2.25.1
#1 lore raw
Quoted 74 lines · On 2023/3/21 14:37, [email protected] wrote:

From: Fei Wu <[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many other syscalls benefit a lot from this one too.

---
  target/riscv/cpu.h        |  4 ++++
  target/riscv/cpu_helper.c |  7 +++++++
  target/riscv/csr.c        | 14 +++++++++++++-
  3 files changed, 24 insertions(+), 1 deletion(-)
target/riscv/cpu.h +4 -0
index 638e47c75a..926dbce59f 100644
@@ -383,6 +383,10 @@ struct CPUArchState {
      uint64_t kvm_timer_compare;
      uint64_t kvm_timer_state;
      uint64_t kvm_timer_frequency;
+
+#define MAX_CACHED_SUM_U_ADDR_NUM 4
+    uint64_t sum_u_count;
+    uint64_t sum_u_addr[MAX_CACHED_SUM_U_ADDR_NUM];
  };
 
  OBJECT_DECLARE_CPU_TYPE(RISCVCPU, RISCVCPUClass, RISCV_CPU)
target/riscv/cpu_helper.c +7 -0
index f88c503cf4..5ad0418eb6 100644
@@ -1068,6 +1068,13 @@ restart:
                      (access_type == MMU_DATA_STORE || (pte & PTE_D))) {
                  *prot |= PAGE_WRITE;
              }
+            if ((pte & PTE_U) && (mode & PRV_S) &&
+                    get_field(env->mstatus, MSTATUS_SUM)) {
+                if (env->sum_u_count < MAX_CACHED_SUM_U_ADDR_NUM) {
+                    env->sum_u_addr[env->sum_u_count] = addr;
+                }
+                ++env->sum_u_count;
+            }
              return TRANSLATE_SUCCESS;
          }
      }
target/riscv/csr.c +3 -1
index ab566639e5..74b7638c8a 100644
@@ -1246,9 +1246,21 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
 
      /* flush tlb on mstatus fields that affect VM */
      if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV |
-            MSTATUS_MPRV | MSTATUS_SUM)) {
+            MSTATUS_MPRV)) {
          tlb_flush(env_cpu(env));
+        env->sum_u_count = 0;
+    } else if ((mstatus & MSTATUS_SUM) && !(val & MSTATUS_SUM)) {

target/riscv/csr.c static RISCVException write_mstatus(CPURISCVState *env, int csrno, · line 1262

Quoted 10 lines
target/riscv/csr.c +9 -0
+        if (env->sum_u_count > MAX_CACHED_SUM_U_ADDR_NUM) {
+            tlb_flush(env_cpu(env));
+        } else {
+            for (int i = 0; i < env->sum_u_count; ++i) {
+                tlb_flush_page_by_mmuidx(env_cpu(env), env->sum_u_addr[i],
+                                         1 << PRV_S | 1 << PRV_M);
+            }
+        }
+        env->sum_u_count = 0;
      }

Whether tlb should  be flushed when SUM is changed from 0 to 1?

Regards,

Weiwei Li

Quoted 4 lines
target/riscv/csr.c +1 -0
+
      mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
          MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM |
          MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
author
#2 lore raw
Quoted 80 lines · liweiwei
Quoted 78 lines · On 2023/3/21 14:37, [email protected] wrote:

From: Fei Wu <[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many other syscalls benefit a lot from this one too.

---
  target/riscv/cpu.h        |  4 ++++
  target/riscv/cpu_helper.c |  7 +++++++
  target/riscv/csr.c        | 14 +++++++++++++-

  3 files changed, 24 insertions(+), 1 deletion(-)
target/riscv/cpu.h +4 -0
index 638e47c75a..926dbce59f 100644
@@ -383,6 +383,10 @@ struct CPUArchState {
       uint64_t kvm_timer_compare;
       uint64_t kvm_timer_state;
       uint64_t kvm_timer_frequency;
+
+#define MAX_CACHED_SUM_U_ADDR_NUM 4
+    uint64_t sum_u_count;
+    uint64_t sum_u_addr[MAX_CACHED_SUM_U_ADDR_NUM];
   };
     OBJECT_DECLARE_CPU_TYPE(RISCVCPU, RISCVCPUClass, RISCV_CPU)
target/riscv/cpu_helper.c +7 -0
index f88c503cf4..5ad0418eb6 100644
@@ -1068,6 +1068,13 @@ restart:
                       (access_type == MMU_DATA_STORE || (pte & PTE_D))) {
                   *prot |= PAGE_WRITE;
               }
+            if ((pte & PTE_U) && (mode & PRV_S) &&
+                    get_field(env->mstatus, MSTATUS_SUM)) {
+                if (env->sum_u_count < MAX_CACHED_SUM_U_ADDR_NUM) {
+                    env->sum_u_addr[env->sum_u_count] = addr;
+                }
+                ++env->sum_u_count;
+            }
               return TRANSLATE_SUCCESS;
           }
       }
target/riscv/csr.c +0 -0
index ab566639e5..74b7638c8a 100644
@@ -1246,9 +1246,21 @@ static RISCVException
 write_mstatus(CPURISCVState *env, int csrno,
         /* flush tlb on mstatus fields that affect VM */

      if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV | -            MSTATUS_MPRV | MSTATUS_SUM)) {
+            MSTATUS_MPRV)) {
          tlb_flush(env_cpu(env));
+        env->sum_u_count = 0;
+    } else if ((mstatus & MSTATUS_SUM) && !(val & MSTATUS_SUM)) { +        if (env->sum_u_count > MAX_CACHED_SUM_U_ADDR_NUM) { +            tlb_flush(env_cpu(env));
+        } else {
+            for (int i = 0; i < env->sum_u_count; ++i) {
+                tlb_flush_page_by_mmuidx(env_cpu(env),

Quoted 9 lines
Quoted 6 lines

env->sum_u_addr[i],
+                                         1 << PRV_S | 1 << PRV_M); +            }
+        }
+        env->sum_u_count = 0;
      }

Whether tlb should  be flushed when SUM is changed from 0 to 1?

When SUM is changed from 0 to 1, all the existing tlb entries remain valid as the permission is elevated instead of reduced, so I don't think it's necessary to flush tlb.

Thanks,
Fei.

Quoted 9 lines

Regards,

Weiwei Li

Quoted 4 lines

+
      mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE | MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM | MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |

#3 lore raw
Quoted 88 lines · Fei
Quoted 78 lines · liweiwei
Quoted 77 lines · On 2023/3/21 14:37, [email protected] wrote:

From: Fei Wu <[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many other syscalls benefit a lot from this one too.

---
   target/riscv/cpu.h        |  4 ++++
   target/riscv/cpu_helper.c |  7 +++++++
   target/riscv/csr.c        | 14 +++++++++++++-
   3 files changed, 24 insertions(+), 1 deletion(-)
target/riscv/cpu.h +4 -0
index 638e47c75a..926dbce59f 100644
@@ -383,6 +383,10 @@ struct CPUArchState {
       uint64_t kvm_timer_compare;
       uint64_t kvm_timer_state;
       uint64_t kvm_timer_frequency;
+
+#define MAX_CACHED_SUM_U_ADDR_NUM 4
+    uint64_t sum_u_count;
+    uint64_t sum_u_addr[MAX_CACHED_SUM_U_ADDR_NUM];
   };
     OBJECT_DECLARE_CPU_TYPE(RISCVCPU, RISCVCPUClass, RISCV_CPU)
target/riscv/cpu_helper.c +7 -0
index f88c503cf4..5ad0418eb6 100644
@@ -1068,6 +1068,13 @@ restart:
                       (access_type == MMU_DATA_STORE || (pte & PTE_D))) {
                   *prot |= PAGE_WRITE;
               }
+            if ((pte & PTE_U) && (mode & PRV_S) &&
+                    get_field(env->mstatus, MSTATUS_SUM)) {
+                if (env->sum_u_count < MAX_CACHED_SUM_U_ADDR_NUM) {
+                    env->sum_u_addr[env->sum_u_count] = addr;
+                }
+                ++env->sum_u_count;
+            }
               return TRANSLATE_SUCCESS;
           }
       }
target/riscv/csr.c +7 -1
index ab566639e5..74b7638c8a 100644
@@ -1246,9 +1246,21 @@ static RISCVException
 write_mstatus(CPURISCVState *env, int csrno,
         /* flush tlb on mstatus fields that affect VM */
       if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV |
-            MSTATUS_MPRV | MSTATUS_SUM)) {
+            MSTATUS_MPRV)) {
           tlb_flush(env_cpu(env));
+        env->sum_u_count = 0;
+    } else if ((mstatus & MSTATUS_SUM) && !(val & MSTATUS_SUM)) {
+        if (env->sum_u_count > MAX_CACHED_SUM_U_ADDR_NUM) {
+            tlb_flush(env_cpu(env));
+        } else {
+            for (int i = 0; i < env->sum_u_count; ++i) {
Quoted 9 lines

target/riscv/csr.c static RISCVException write_mstatus(CPURISCVState *env, int csrno, · line 1262

Quoted 7 lines
target/riscv/csr.c +5 -0
+                tlb_flush_page_by_mmuidx(env_cpu(env),
 env->sum_u_addr[i],
+                                         1 << PRV_S | 1 << PRV_M);
+            }
+        }
+        env->sum_u_count = 0;
       }

Whether tlb should  be flushed when SUM is changed from 0 to 1?

Quoted 3 lines

When SUM is changed from 0 to 1, all the existing tlb entries remain valid as the permission is elevated instead of reduced, so I don't think it's necessary to flush tlb.

If elevated not unchanged, I think the tlb also needs update, since new permitted access rights may be added to the tlb.

Regards,

Weiwei Li

Quoted 12 lines

Thanks,
Fei.

Quoted 8 lines

Regards,

Weiwei Li

Quoted 4 lines
target/riscv/csr.c +1 -0
+
       mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
           MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM |

          MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |

author
#4 lore raw
Quoted 92 lines · liweiwei
Quoted 90 lines · Fei
Quoted 89 lines · liweiwei
Quoted 78 lines · On 2023/3/21 14:37, [email protected] wrote:

From: Fei Wu <[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window
is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many other syscalls benefit a lot from this one too.

---
   target/riscv/cpu.h        |  4 ++++
   target/riscv/cpu_helper.c |  7 +++++++
   target/riscv/csr.c        | 14 +++++++++++++-

   3 files changed, 24 insertions(+), 1 deletion(-)
target/riscv/cpu.h +4 -0
index 638e47c75a..926dbce59f 100644
@@ -383,6 +383,10 @@ struct CPUArchState {
        uint64_t kvm_timer_compare;
        uint64_t kvm_timer_state;
        uint64_t kvm_timer_frequency;
+
+#define MAX_CACHED_SUM_U_ADDR_NUM 4
+    uint64_t sum_u_count;
+    uint64_t sum_u_addr[MAX_CACHED_SUM_U_ADDR_NUM];
    };
      OBJECT_DECLARE_CPU_TYPE(RISCVCPU, RISCVCPUClass, RISCV_CPU)
target/riscv/cpu_helper.c +0 -0
index f88c503cf4..5ad0418eb6 100644
@@ -1068,6 +1068,13 @@ restart:

                       (access_type == MMU_DATA_STORE || (pte & PTE_D))) {
                   *prot |= PAGE_WRITE;
               }
+            if ((pte & PTE_U) && (mode & PRV_S) &&
+                    get_field(env->mstatus, MSTATUS_SUM)) { +                if (env->sum_u_count < MAX_CACHED_SUM_U_ADDR_NUM) { +                    env->sum_u_addr[env->sum_u_count] = addr; +                }
+                ++env->sum_u_count;
+            }
               return TRANSLATE_SUCCESS;
           }
       }

target/riscv/csr.c +0 -0
index ab566639e5..74b7638c8a 100644
@@ -1246,9 +1246,21 @@ static RISCVException
 write_mstatus(CPURISCVState *env, int csrno,
          /* flush tlb on mstatus fields that affect VM */

       if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV | -            MSTATUS_MPRV | MSTATUS_SUM)) {
+            MSTATUS_MPRV)) {
           tlb_flush(env_cpu(env));
+        env->sum_u_count = 0;
+    } else if ((mstatus & MSTATUS_SUM) && !(val & MSTATUS_SUM)) { +        if (env->sum_u_count > MAX_CACHED_SUM_U_ADDR_NUM) { +            tlb_flush(env_cpu(env));
+        } else {

Quoted 8 lines

+            for (int i = 0; i < env->sum_u_count; ++i) { +                tlb_flush_page_by_mmuidx(env_cpu(env), env->sum_u_addr[i],
+                                         1 << PRV_S | 1 << PRV_M); +            }
+        }
+        env->sum_u_count = 0;
       }

Whether tlb should  be flushed when SUM is changed from 0 to 1?

Quoted 7 lines
Quoted 3 lines

When SUM is changed from 0 to 1, all the existing tlb entries remain valid as the permission is elevated instead of reduced, so I don't think it's necessary to flush tlb.

If elevated not unchanged, I think the tlb also needs update, since new permitted access rights may be added to the tlb.

Assume the following flow, if the new rights have been added to tlb during SUM=0, they're visible and still valid after setting SUM=1 again. Could you please add a specific counter example in this flow?

enable uaccess (set SUM = 1)
... (access user mem from S mode)
disable uaccess (set SUM = 0)

... (update TLB_SUM_0)

<-- flush tlb or not right before enabling uaccess?
enable uaccess (set SUM = 1)
<-- okay to access TLB_SUM_0?
disable uaccess (set SUM = 0)

Thanks,
Fei.

Quoted 17 lines

Regards,

Weiwei Li

Quoted 12 lines

Thanks,
Fei.

Quoted 8 lines

Regards,

Weiwei Li

Quoted 4 lines

+
       mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE | MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM | MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |

#5 lore raw
Quoted 95 lines · Fei
Quoted 94 lines · liweiwei
Quoted 93 lines · Fei
Quoted 80 lines · liweiwei
Quoted 79 lines · On 2023/3/21 14:37, [email protected] wrote:

From: Fei Wu <[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window
is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many other syscalls benefit a lot from this one too.

---
    target/riscv/cpu.h        |  4 ++++
    target/riscv/cpu_helper.c |  7 +++++++
    target/riscv/csr.c        | 14 +++++++++++++-
    3 files changed, 24 insertions(+), 1 deletion(-)
target/riscv/cpu.h +4 -0
index 638e47c75a..926dbce59f 100644
@@ -383,6 +383,10 @@ struct CPUArchState {
        uint64_t kvm_timer_compare;
        uint64_t kvm_timer_state;
        uint64_t kvm_timer_frequency;
+
+#define MAX_CACHED_SUM_U_ADDR_NUM 4
+    uint64_t sum_u_count;
+    uint64_t sum_u_addr[MAX_CACHED_SUM_U_ADDR_NUM];
    };
      OBJECT_DECLARE_CPU_TYPE(RISCVCPU, RISCVCPUClass, RISCV_CPU)
target/riscv/cpu_helper.c +7 -0
index f88c503cf4..5ad0418eb6 100644
@@ -1068,6 +1068,13 @@ restart:
                        (access_type == MMU_DATA_STORE || (pte &
 PTE_D))) {
                    *prot |= PAGE_WRITE;
                }
+            if ((pte & PTE_U) && (mode & PRV_S) &&
+                    get_field(env->mstatus, MSTATUS_SUM)) {
+                if (env->sum_u_count < MAX_CACHED_SUM_U_ADDR_NUM) {
+                    env->sum_u_addr[env->sum_u_count] = addr;
+                }
+                ++env->sum_u_count;
+            }
                return TRANSLATE_SUCCESS;
            }

       }

target/riscv/csr.c +7 -1
index ab566639e5..74b7638c8a 100644
@@ -1246,9 +1246,21 @@ static RISCVException
 write_mstatus(CPURISCVState *env, int csrno,
          /* flush tlb on mstatus fields that affect VM */
        if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV |
-            MSTATUS_MPRV | MSTATUS_SUM)) {
+            MSTATUS_MPRV)) {
            tlb_flush(env_cpu(env));
+        env->sum_u_count = 0;
+    } else if ((mstatus & MSTATUS_SUM) && !(val & MSTATUS_SUM)) {
+        if (env->sum_u_count > MAX_CACHED_SUM_U_ADDR_NUM) {
+            tlb_flush(env_cpu(env));
+        } else {
+            for (int i = 0; i < env->sum_u_count; ++i) {
Quoted 9 lines

target/riscv/csr.c static RISCVException write_mstatus(CPURISCVState *env, int csrno, · line 1262

Quoted 7 lines
target/riscv/csr.c +5 -0
+                tlb_flush_page_by_mmuidx(env_cpu(env),
 env->sum_u_addr[i],
+                                         1 << PRV_S | 1 << PRV_M);
+            }
+        }
+        env->sum_u_count = 0;
        }

Whether tlb should  be flushed when SUM is changed from 0 to 1?

When SUM is changed from 0 to 1, all the existing tlb entries remain valid as the permission is elevated instead of reduced, so I don't think it's necessary to flush tlb.

Quoted 7 lines
Quoted 3 lines

If elevated not unchanged, I think the tlb also needs update, since new permitted access rights may be added to the tlb.

Assume the following flow, if the new rights have been added to tlb during SUM=0, they're visible and still valid after setting SUM=1 again. Could you please add a specific counter example in this flow?

Assuming addr0 cannot be access from S mode when SUM = 0, but can be accessed from S mode if SUM=1,

and there is a tlb entry for it when SUM = 0

Quoted 1 line

enable uaccess (set SUM = 1)

if we don't flush it when we change SUM to 1 in this step

Quoted 1 line

... (access user mem from S mode)

when we access addr0 here, tlb will be hit( not updated) and the access will trigger fault instead of allowing the access

Quoted 8 lines

disable uaccess (set SUM = 0)

... (update TLB_SUM_0)

<-- flush tlb or not right before enabling uaccess?
enable uaccess (set SUM = 1)
<-- okay to access TLB_SUM_0?
disable uaccess (set SUM = 0)

So, I think the question is whether the rights in TLB entry can be elevated. Or whether there is legal tlb entry for this addr0 when SUM = 0?

If not,  all my above assumption will not be right.

Regards,

Weiwei Li

Quoted 22 lines

Thanks,
Fei.

Quoted 15 lines

Regards,

Weiwei Li

Quoted 11 lines

Thanks,
Fei.

Quoted 8 lines

Regards,

Weiwei Li

Quoted 4 lines
target/riscv/csr.c +1 -0
+
        mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
            MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM |

           MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |

author
#6 lore raw
Quoted 122 lines · liweiwei
Quoted 99 lines · Fei
Quoted 98 lines · liweiwei
Quoted 97 lines · Fei
Quoted 83 lines · liweiwei
Quoted 82 lines · On 2023/3/21 14:37, [email protected] wrote:

From: Fei Wu <[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window
is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save
all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many
other syscalls benefit a lot from this one too.

---
    target/riscv/cpu.h        |  4 ++++
    target/riscv/cpu_helper.c |  7 +++++++
    target/riscv/csr.c        | 14 +++++++++++++-

    3 files changed, 24 insertions(+), 1 deletion(-)
target/riscv/cpu.h +4 -0
index 638e47c75a..926dbce59f 100644
@@ -383,6 +383,10 @@ struct CPUArchState {
         uint64_t kvm_timer_compare;
         uint64_t kvm_timer_state;
         uint64_t kvm_timer_frequency;
+
+#define MAX_CACHED_SUM_U_ADDR_NUM 4
+    uint64_t sum_u_count;
+    uint64_t sum_u_addr[MAX_CACHED_SUM_U_ADDR_NUM];
     };
       OBJECT_DECLARE_CPU_TYPE(RISCVCPU, RISCVCPUClass, RISCV_CPU)
target/riscv/cpu_helper.c +0 -0
index f88c503cf4..5ad0418eb6 100644
@@ -1068,6 +1068,13 @@ restart:

                        (access_type == MMU_DATA_STORE || (pte & PTE_D))) {
                    *prot |= PAGE_WRITE;
                }
+            if ((pte & PTE_U) && (mode & PRV_S) &&
+                    get_field(env->mstatus, MSTATUS_SUM)) { +                if (env->sum_u_count < MAX_CACHED_SUM_U_ADDR_NUM) { +                    env->sum_u_addr[env->sum_u_count] = addr; +                }
+                ++env->sum_u_count;
+            }
                return TRANSLATE_SUCCESS;
            }
        }

target/riscv/csr.c +0 -0
index ab566639e5..74b7638c8a 100644
@@ -1246,9 +1246,21 @@ static RISCVException
 write_mstatus(CPURISCVState *env, int csrno,
           /* flush tlb on mstatus fields that affect VM */

        if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV |
-            MSTATUS_MPRV | MSTATUS_SUM)) {
+            MSTATUS_MPRV)) {
            tlb_flush(env_cpu(env));
+        env->sum_u_count = 0;
+    } else if ((mstatus & MSTATUS_SUM) && !(val & MSTATUS_SUM)) { +        if (env->sum_u_count > MAX_CACHED_SUM_U_ADDR_NUM) { +            tlb_flush(env_cpu(env));
+        } else {
+            for (int i = 0; i < env->sum_u_count; ++i) {

Quoted 9 lines
Quoted 7 lines

+                tlb_flush_page_by_mmuidx(env_cpu(env), env->sum_u_addr[i],
+                                         1 << PRV_S | 1 << PRV_M); +            }
+        }
+        env->sum_u_count = 0;
        }

Whether tlb should  be flushed when SUM is changed from 0 to 1?

When SUM is changed from 0 to 1, all the existing tlb entries remain valid as the permission is elevated instead of reduced, so I don't think
it's necessary to flush tlb.

Quoted 7 lines
Quoted 3 lines

If elevated not unchanged, I think the tlb also needs update, since new permitted access rights may be added to the tlb.

Assume the following flow, if the new rights have been added to tlb during SUM=0, they're visible and still valid after setting SUM=1 again. Could you please add a specific counter example in this flow?

Assuming addr0 cannot be access from S mode when SUM = 0, but can be accessed from S mode if SUM=1,

and there is a tlb entry for it when SUM = 0

Quoted 1 line

enable uaccess (set SUM = 1)

if we don't flush it when we change SUM to 1 in this step

Quoted 1 line

... (access user mem from S mode)

when we access addr0 here, tlb will be hit( not updated) and the access will trigger fault instead of allowing the access

Quoted 4 lines

disable uaccess (set SUM = 0)

... (update TLB_SUM_0)

Quoted 8 lines
Quoted 4 lines

     <-- flush tlb or not right before enabling uaccess? enable uaccess (set SUM = 1)
     <-- okay to access TLB_SUM_0?
disable uaccess (set SUM = 0)

So, I think the question is whether the rights in TLB entry can be elevated. Or whether there is legal tlb entry for this addr0 when SUM = 0?

I think there is no such tlb entry:
* If it's loaded into tlb when SUM = 0. This is impossible as it's not accessible when SUM = 0, it's a fault instead of being loaded into tlb.
* If it's loaded into tlb when SUM = 1. It will be flushed when SUM sets to 0.

Thanks,
Fei.

Quoted 30 lines

If not,  all my above assumption will not be right.

Regards,

Weiwei Li

Quoted 23 lines

Thanks,
Fei.

Quoted 16 lines

Regards,

Weiwei Li

Quoted 12 lines

Thanks,
Fei.

Quoted 9 lines

Regards,

Weiwei Li

Quoted 5 lines

+
        mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
            MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM | MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |

Reviewed-by
#7 lore raw
Quoted 53 lines · Fei
Quoted 52 lines · liweiwei
Quoted 51 lines · Fei
Quoted 50 lines · liweiwei
Quoted 49 lines · Fei
Quoted 48 lines · liweiwei
Quoted 47 lines · On 2023/3/21 14:37,[email protected] wrote:

From: Fei Wu<[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window
is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save
all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many
other syscalls benefit a lot from this one too.

---
     target/riscv/cpu.h        |  4 ++++
     target/riscv/cpu_helper.c |  7 +++++++
     target/riscv/csr.c        | 14 +++++++++++++-
     3 files changed, 24 insertions(+), 1 deletion(-)
target/riscv/cpu.h +4 -0
index 638e47c75a..926dbce59f 100644
@@ -383,6 +383,10 @@ struct CPUArchState {
         uint64_t kvm_timer_compare;
         uint64_t kvm_timer_state;
         uint64_t kvm_timer_frequency;
+
+#define MAX_CACHED_SUM_U_ADDR_NUM 4
+    uint64_t sum_u_count;
+    uint64_t sum_u_addr[MAX_CACHED_SUM_U_ADDR_NUM];
     };
       OBJECT_DECLARE_CPU_TYPE(RISCVCPU, RISCVCPUClass, RISCV_CPU)
target/riscv/cpu_helper.c +0 -0
index f88c503cf4..5ad0418eb6 100644
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

target/riscv/cpu_helper.c restart: · line 1072

Quoted 8 lines
target/riscv/cpu_helper.c +1 -0
@@ -1068,6 +1068,13 @@ restart:
                         (access_type == MMU_DATA_STORE || (pte &
 PTE_D))) {
                     *prot |= PAGE_WRITE;
                 }
+            if ((pte & PTE_U) && (mode & PRV_S) &&

It's more readable to use "mode == PRV_S" instead of  "mode & PRV_S" here.

Quoted 68 lines
Quoted 59 lines
Quoted 40 lines
Quoted 40 lines
Quoted 40 lines
Quoted 27 lines
Quoted 27 lines
target/riscv/cpu_helper.c +6 -0
+                    get_field(env->mstatus, MSTATUS_SUM)) {
+                if (env->sum_u_count < MAX_CACHED_SUM_U_ADDR_NUM) {
+                    env->sum_u_addr[env->sum_u_count] = addr;
+                }
+                ++env->sum_u_count;
+            }
                 return TRANSLATE_SUCCESS;
             }

        }

target/riscv/csr.c +0 -0
index ab566639e5..74b7638c8a 100644
@@ -1246,9 +1246,21 @@ static RISCVException
 write_mstatus(CPURISCVState *env, int csrno,
           /* flush tlb on mstatus fields that affect VM */
         if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP |

MSTATUS_MPV |
-            MSTATUS_MPRV | MSTATUS_SUM)) {
+            MSTATUS_MPRV)) {
            tlb_flush(env_cpu(env));
+        env->sum_u_count = 0;
+    } else if ((mstatus & MSTATUS_SUM) && !(val & MSTATUS_SUM)) { +        if (env->sum_u_count > MAX_CACHED_SUM_U_ADDR_NUM) { +            tlb_flush(env_cpu(env));
+        } else {
+            for (int i = 0; i < env->sum_u_count; ++i) {

Quoted 9 lines
Quoted 7 lines

+                tlb_flush_page_by_mmuidx(env_cpu(env), env->sum_u_addr[i],
+                                         1 << PRV_S | 1 << PRV_M); +            }
+        }
+        env->sum_u_count = 0;
        }

Whether tlb should  be flushed when SUM is changed from 0 to 1?

When SUM is changed from 0 to 1, all the existing tlb entries remain valid as the permission is elevated instead of reduced, so I don't think
it's necessary to flush tlb.

Quoted 7 lines
Quoted 3 lines

If elevated not unchanged, I think the tlb also needs update, since new permitted access rights may be added to the tlb.

Assume the following flow, if the new rights have been added to tlb during SUM=0, they're visible and still valid after setting SUM=1 again. Could you please add a specific counter example in this flow?

Assuming addr0 cannot be access from S mode when SUM = 0, but can be accessed from S mode if SUM=1,

and there is a tlb entry for it when SUM = 0

Quoted 1 line

enable uaccess (set SUM = 1)

if we don't flush it when we change SUM to 1 in this step

Quoted 1 line

... (access user mem from S mode)

when we access addr0 here, tlb will be hit( not updated) and the access will trigger fault instead of allowing the access

Quoted 2 lines

disable uaccess (set SUM = 0)

Quoted 9 lines
Quoted 6 lines

... (update TLB_SUM_0)

     <-- flush tlb or not right before enabling uaccess?
enable uaccess (set SUM = 1)
     <-- okay to access TLB_SUM_0?
disable uaccess (set SUM = 0)

So, I think the question is whether the rights in TLB entry can be elevated. Or whether there is legal tlb entry for this addr0 when SUM = 0?

Quoted 9 lines

I think there is no such tlb entry:
* If it's loaded into tlb when SUM = 0. This is impossible as it's not accessible when SUM = 0, it's a fault instead of being loaded into tlb.
* If it's loaded into tlb when SUM = 1. It will be flushed when SUM sets to 0.

Thanks,
Fei.

OK. It's acceptable to me.

Weiwei Li

Quoted 27 lines
Quoted 27 lines

If not,  all my above assumption will not be right.

Regards,

Weiwei Li

Quoted 21 lines

Thanks,
Fei.

Quoted 16 lines

Regards,

Weiwei Li

Quoted 12 lines

Thanks,
Fei.

Quoted 9 lines

Regards,

Weiwei Li

Quoted 5 lines

+
        mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE |
MSTATUS_MPIE |
            MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM | MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |

#8 lore raw
In reply to [email protected]
Quoted 66 lines · On 2023/3/21 14:37, [email protected] wrote:

From: Fei Wu <[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many other syscalls benefit a lot from this one too.

---
  target/riscv/cpu.h        |  4 ++++
  target/riscv/cpu_helper.c |  7 +++++++
  target/riscv/csr.c        | 14 +++++++++++++-
  3 files changed, 24 insertions(+), 1 deletion(-)
target/riscv/cpu.h +4 -0
index 638e47c75a..926dbce59f 100644
@@ -383,6 +383,10 @@ struct CPUArchState {
      uint64_t kvm_timer_compare;
      uint64_t kvm_timer_state;
      uint64_t kvm_timer_frequency;
+
+#define MAX_CACHED_SUM_U_ADDR_NUM 4
+    uint64_t sum_u_count;
+    uint64_t sum_u_addr[MAX_CACHED_SUM_U_ADDR_NUM];
  };
 
  OBJECT_DECLARE_CPU_TYPE(RISCVCPU, RISCVCPUClass, RISCV_CPU)
target/riscv/cpu_helper.c +7 -0
index f88c503cf4..5ad0418eb6 100644
@@ -1068,6 +1068,13 @@ restart:
                      (access_type == MMU_DATA_STORE || (pte & PTE_D))) {
                  *prot |= PAGE_WRITE;
              }
+            if ((pte & PTE_U) && (mode & PRV_S) &&
+                    get_field(env->mstatus, MSTATUS_SUM)) {
+                if (env->sum_u_count < MAX_CACHED_SUM_U_ADDR_NUM) {
+                    env->sum_u_addr[env->sum_u_count] = addr;
+                }
+                ++env->sum_u_count;
+            }
              return TRANSLATE_SUCCESS;
          }
      }
target/riscv/csr.c +0 -0
index ab566639e5..74b7638c8a 100644

target/riscv/csr.c static RISCVException write_mstatus(CPURISCVState *env, int csrno, · line 1254

Quoted 10 lines
target/riscv/csr.c +5 -1
@@ -1246,9 +1246,21 @@ static RISCVException write_mstatus(CPURISCVState *env, int csrno,
 
      /* flush tlb on mstatus fields that affect VM */
      if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV |
-            MSTATUS_MPRV | MSTATUS_SUM)) {
+            MSTATUS_MPRV)) {
          tlb_flush(env_cpu(env));
+        env->sum_u_count = 0;
+    } else if ((mstatus & MSTATUS_SUM) && !(val & MSTATUS_SUM)) {
+        if (env->sum_u_count > MAX_CACHED_SUM_U_ADDR_NUM) {
+            tlb_flush(env_cpu(env));

SUM seems only affect S mode TLB. Maybe we can only flush S mode TLB here.

target/riscv/csr.c static RISCVException write_mstatus(CPURISCVState *env, int csrno, · line 1258

Quoted 4 lines
target/riscv/csr.c +4 -0
+        } else {
+            for (int i = 0; i < env->sum_u_count; ++i) {
+                tlb_flush_page_by_mmuidx(env_cpu(env), env->sum_u_addr[i],
+                                         1 << PRV_S | 1 << PRV_M);

Similar case here.

Regards,

Weiwei Li

Quoted 8 lines
target/riscv/csr.c +4 -0
+            }
+        }
+        env->sum_u_count = 0;
      }
+
      mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
          MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM |
          MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |
author
#9 lore raw
Quoted 71 lines · liweiwei
Quoted 69 lines · On 2023/3/21 14:37, [email protected] wrote:

From: Fei Wu <[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many other syscalls benefit a lot from this one too.

---
  target/riscv/cpu.h        |  4 ++++
  target/riscv/cpu_helper.c |  7 +++++++
  target/riscv/csr.c        | 14 +++++++++++++-

  3 files changed, 24 insertions(+), 1 deletion(-)
target/riscv/cpu.h +4 -0
index 638e47c75a..926dbce59f 100644
@@ -383,6 +383,10 @@ struct CPUArchState {
       uint64_t kvm_timer_compare;
       uint64_t kvm_timer_state;
       uint64_t kvm_timer_frequency;
+
+#define MAX_CACHED_SUM_U_ADDR_NUM 4
+    uint64_t sum_u_count;
+    uint64_t sum_u_addr[MAX_CACHED_SUM_U_ADDR_NUM];
   };
     OBJECT_DECLARE_CPU_TYPE(RISCVCPU, RISCVCPUClass, RISCV_CPU)
target/riscv/cpu_helper.c +7 -0
index f88c503cf4..5ad0418eb6 100644
@@ -1068,6 +1068,13 @@ restart:
                       (access_type == MMU_DATA_STORE || (pte & PTE_D))) {
                   *prot |= PAGE_WRITE;
               }
+            if ((pte & PTE_U) && (mode & PRV_S) &&
+                    get_field(env->mstatus, MSTATUS_SUM)) {
+                if (env->sum_u_count < MAX_CACHED_SUM_U_ADDR_NUM) {
+                    env->sum_u_addr[env->sum_u_count] = addr;
+                }
+                ++env->sum_u_count;
+            }
               return TRANSLATE_SUCCESS;
           }
       }
target/riscv/csr.c +0 -0
index ab566639e5..74b7638c8a 100644
@@ -1246,9 +1246,21 @@ static RISCVException
 write_mstatus(CPURISCVState *env, int csrno,
         /* flush tlb on mstatus fields that affect VM */

      if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV | -            MSTATUS_MPRV | MSTATUS_SUM)) {

Quoted 9 lines
Quoted 6 lines

+            MSTATUS_MPRV)) {
          tlb_flush(env_cpu(env));
+        env->sum_u_count = 0;
+    } else if ((mstatus & MSTATUS_SUM) && !(val & MSTATUS_SUM)) { +        if (env->sum_u_count > MAX_CACHED_SUM_U_ADDR_NUM) { +            tlb_flush(env_cpu(env));

SUM seems only affect S mode TLB. Maybe we can only flush S mode TLB here.

It's also in effect when MPRV=1 and MPP=S in M mode, we can only flush the tlb of PRV_S and PRV_M.

Thanks,
Fei.

Quoted 21 lines
Quoted 5 lines

+        } else {
+            for (int i = 0; i < env->sum_u_count; ++i) { +                tlb_flush_page_by_mmuidx(env_cpu(env), env->sum_u_addr[i],
+                                         1 << PRV_S | 1 << PRV_M);

Similar case here.

Regards,

Weiwei Li

Quoted 8 lines

+            }
+        }
+        env->sum_u_count = 0;
      }
+
      mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE | MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM | MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |

#10 lore raw
Quoted 79 lines · Fei
Quoted 69 lines · liweiwei
Quoted 68 lines · On 2023/3/21 14:37, [email protected] wrote:

From: Fei Wu <[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many other syscalls benefit a lot from this one too.

---
   target/riscv/cpu.h        |  4 ++++
   target/riscv/cpu_helper.c |  7 +++++++
   target/riscv/csr.c        | 14 +++++++++++++-
   3 files changed, 24 insertions(+), 1 deletion(-)
target/riscv/cpu.h +4 -0
index 638e47c75a..926dbce59f 100644
@@ -383,6 +383,10 @@ struct CPUArchState {
       uint64_t kvm_timer_compare;
       uint64_t kvm_timer_state;
       uint64_t kvm_timer_frequency;
+
+#define MAX_CACHED_SUM_U_ADDR_NUM 4
+    uint64_t sum_u_count;
+    uint64_t sum_u_addr[MAX_CACHED_SUM_U_ADDR_NUM];
   };
     OBJECT_DECLARE_CPU_TYPE(RISCVCPU, RISCVCPUClass, RISCV_CPU)
target/riscv/cpu_helper.c +7 -0
index f88c503cf4..5ad0418eb6 100644
@@ -1068,6 +1068,13 @@ restart:
                       (access_type == MMU_DATA_STORE || (pte & PTE_D))) {
                   *prot |= PAGE_WRITE;
               }
+            if ((pte & PTE_U) && (mode & PRV_S) &&
+                    get_field(env->mstatus, MSTATUS_SUM)) {
+                if (env->sum_u_count < MAX_CACHED_SUM_U_ADDR_NUM) {
+                    env->sum_u_addr[env->sum_u_count] = addr;
+                }
+                ++env->sum_u_count;
+            }
               return TRANSLATE_SUCCESS;
           }
       }
target/riscv/csr.c +0 -0
index ab566639e5..74b7638c8a 100644
@@ -1246,9 +1246,21 @@ static RISCVException
 write_mstatus(CPURISCVState *env, int csrno,
         /* flush tlb on mstatus fields that affect VM */
       if ((val ^ mstatus) & (MSTATUS_MXR | MSTATUS_MPP | MSTATUS_MPV |
Quoted 9 lines

target/riscv/csr.c static RISCVException write_mstatus(CPURISCVState *env, int csrno, · line 1254

Quoted 7 lines
target/riscv/csr.c +5 -1
-            MSTATUS_MPRV | MSTATUS_SUM)) {
+            MSTATUS_MPRV)) {
           tlb_flush(env_cpu(env));
+        env->sum_u_count = 0;
+    } else if ((mstatus & MSTATUS_SUM) && !(val & MSTATUS_SUM)) {
+        if (env->sum_u_count > MAX_CACHED_SUM_U_ADDR_NUM) {
+            tlb_flush(env_cpu(env));

SUM seems only affect S mode TLB. Maybe we can only flush S mode TLB here.

Quoted 2 lines

It's also in effect when MPRV=1 and MPP=S in M mode, we can only flush the tlb of PRV_S and PRV_M.

OK. Good point.

Regards,

Weiwei Li

Quoted 23 lines

Thanks,
Fei.

Quoted 19 lines

target/riscv/csr.c static RISCVException write_mstatus(CPURISCVState *env, int csrno, · line 1258

Quoted 5 lines
target/riscv/csr.c +4 -0
+        } else {
+            for (int i = 0; i < env->sum_u_count; ++i) {
+                tlb_flush_page_by_mmuidx(env_cpu(env),
 env->sum_u_addr[i],
+                                         1 << PRV_S | 1 << PRV_M);

Similar case here.

Regards,

Weiwei Li

Quoted 8 lines
target/riscv/csr.c +4 -0
+            }
+        }
+        env->sum_u_count = 0;
       }
+
       mask = MSTATUS_SIE | MSTATUS_SPIE | MSTATUS_MIE | MSTATUS_MPIE |
           MSTATUS_SPP | MSTATUS_MPRV | MSTATUS_SUM |

          MSTATUS_MPP | MSTATUS_MXR | MSTATUS_TVM | MSTATUS_TSR |

#11 lore raw
In reply to [email protected]
Quoted 14 lines · On 3/20/23 23:37, [email protected] wrote:

From: Fei Wu <[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

Quoted 5 lines

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many other syscalls benefit a lot from this one too.

This is not the correct approach.

You should be making use of different softmmu indexes, similar to how ARM uses a separate index for PAN (privileged access never) mode. If I read the manual properly, PAN == !SUM.

When you do this, you need no additional flushing.

r~

#12 lore raw
Quoted 18 lines · Richard Henderson
Quoted 17 lines · On 3/20/23 23:37, [email protected] wrote:

From: Fei Wu <[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Quoted 8 lines
Quoted 2 lines

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many other syscalls benefit a lot from this one too.

This is not the correct approach.

You should be making use of different softmmu indexes, similar to how ARM uses a separate index for PAN (privileged access never) mode.  If I read the manual properly, PAN == !SUM.

When you do this, you need no additional flushing.

Hi Fei,

Let's follow Richard's advice.

Zhiwei

Quoted 3 lines

r~

author
#13 lore raw
Quoted 29 lines · LIU Zhiwei
Quoted 27 lines · Richard Henderson
Quoted 14 lines · On 3/20/23 23:37, [email protected] wrote:

From: Fei Wu <[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

Quoted 5 lines

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many other syscalls benefit a lot from this one too.

This is not the correct approach.

You should be making use of different softmmu indexes, similar to how ARM uses a separate index for PAN (privileged access never) mode.  If I read the manual properly, PAN == !SUM.

Quoted 6 lines
Quoted 1 line

When you do this, you need no additional flushing.

Hi Fei,

Let's follow Richard's advice.
Yes, I'm thinking about how to do it, and thank Richard for the advice.

My question is:
* If we ensure this separate index (S+SUM) has no overlapping tlb entries with S-mode (ignore M-mode so far), during SUM=1, we have to look into both (S+SUM) and S index for kernel address translation, that should be not desired.

* If all the tlb operations are against (S+SUM) during SUM=1, then (S+SUM) could contain some duplicated tlb entries of kernel address in S index, the duplication means extra tlb lookup and fill. Also if we want to flush tlb entry of specific addr0, we have to flush both index.

I will take a look at how arm handles this.

Thanks,
Fei.

Quoted 5 lines

Zhiwei

Quoted 3 lines

r~

#14 lore raw
Quoted 28 lines · Fei
Quoted 27 lines · LIU Zhiwei
Quoted 26 lines · Richard Henderson
Quoted 14 lines · On 3/20/23 23:37, [email protected] wrote:

From: Fei Wu <[email protected]>

Kernel needs to access user mode memory e.g. during syscalls, the window is usually opened up for a very limited time through MSTATUS.SUM, the overhead is too much if tlb_flush() gets called for every SUM change. This patch saves addresses accessed when SUM=1, and flushs only these pages when SUM changes to 0. If the buffer is not large enough to save all the pages during SUM=1, it will fall back to tlb_flush when necessary.

The buffer size is set to 4 since in this MSTATUS.SUM open-up window, most of the time kernel accesses 1 or 2 pages, it's very rare to see more than 4 pages accessed.

Quoted 5 lines

It's not necessary to save/restore these new added status, as tlb_flush() is always called after restore.

Result of 'pipe 10' from unixbench boosts from 223656 to 1327407. Many other syscalls benefit a lot from this one too.

This is not the correct approach.

You should be making use of different softmmu indexes, similar to how ARM uses a separate index for PAN (privileged access never) mode.  If I read the manual properly, PAN == !SUM.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

When you do this, you need no additional flushing.

Hi Fei,

Let's follow Richard's advice.
Yes, I'm thinking about how to do it, and thank Richard for the advice.

My question is:
* If we ensure this separate index (S+SUM) has no overlapping tlb entries with S-mode (ignore M-mode so far), during SUM=1,

Yes, every mmu index will have their own cache.

Quoted 3 lines

we have to
look into both (S+SUM) and S index for kernel address translation, that should be not desired.

No, we  have to choose one, because each access will be constrained with a mmu idex.

Quoted 5 lines

* If all the tlb operations are against (S+SUM) during SUM=1, then (S+SUM) could contain some duplicated tlb entries of kernel address in S index, the duplication means extra tlb lookup and fill. Also if we want to flush tlb entry of specific addr0, we have to flush both index.

This is not the case.

Zhiwei

Quoted 10 lines

I will take a look at how arm handles this.

Thanks,
Fei.

Quoted 4 lines

Zhiwei

Quoted 2 lines

r~

#15 lore raw
In reply to Wu, Fei
Quoted 8 lines · Fei
Quoted 8 lines
Quoted 5 lines

You should be making use of different softmmu indexes, similar to how ARM uses a separate index for PAN (privileged access never) mode.  If I read the manual properly, PAN == !SUM.

When you do this, you need no additional flushing.

Hi Fei,

Quoted 8 lines
Quoted 2 lines

Let's follow Richard's advice.
Yes, I'm thinking about how to do it, and thank Richard for the advice.

My question is:
* If we ensure this separate index (S+SUM) has no overlapping tlb entries with S-mode (ignore M-mode so far), during SUM=1, we have to look into both (S+SUM) and S index for kernel address translation, that should be not desired.

This is an incorrect assumption. S+SUM may very well have overlapping tlb entries with S. With SUM=1, you *only* look in S+SUM index; with SUM=0, you *only* look in S index.

The only difference is a check in get_physical_address is no longer against MSTATUS_SUM directly, but against the mmu_index.

Quoted 3 lines

* If all the tlb operations are against (S+SUM) during SUM=1, then (S+SUM) could contain some duplicated tlb entries of kernel address in S index, the duplication means extra tlb lookup and fill.

Yes, if the same address is probed via S and S+SUM, there is a duplicated lookup. But this is harmless.

Quoted 2 lines

Also if we want
to flush tlb entry of specific addr0, we have to flush both index.

Yes, this is also true. But so far target/riscv is making no use of per-mmuidx flushing. At the moment you're *only* using tlb_flush(cpu), which flushes every mmuidx. Nor are you making use of per-page flushing.

So, really, no change required at all there.

r~

author
#16 lore raw
Quoted 34 lines · Richard Henderson
Quoted 8 lines · Fei
Quoted 8 lines
Quoted 5 lines

You should be making use of different softmmu indexes, similar to how ARM uses a separate index for PAN (privileged access never) mode.  If I read the manual properly, PAN == !SUM.

When you do this, you need no additional flushing.

Hi Fei,

Quoted 8 lines
Quoted 2 lines

Let's follow Richard's advice.
Yes, I'm thinking about how to do it, and thank Richard for the advice.

My question is:
* If we ensure this separate index (S+SUM) has no overlapping tlb entries with S-mode (ignore M-mode so far), during SUM=1, we have to look into both (S+SUM) and S index for kernel address translation, that should be not desired.

This is an incorrect assumption.  S+SUM may very well have overlapping tlb entries with S.
With SUM=1, you *only* look in S+SUM index; with SUM=0, you *only* look in S index.

The only difference is a check in get_physical_address is no longer against MSTATUS_SUM directly, but against the mmu_index.

Quoted 3 lines

* If all the tlb operations are against (S+SUM) during SUM=1, then (S+SUM) could contain some duplicated tlb entries of kernel address in S index, the duplication means extra tlb lookup and fill.

Yes, if the same address is probed via S and S+SUM, there is a duplicated lookup.  But this is harmless.

Quoted 9 lines
Quoted 2 lines

Also if we want
to flush tlb entry of specific addr0, we have to flush both index.

Yes, this is also true.  But so far target/riscv is making no use of per-mmuidx flushing. At the moment you're *only* using tlb_flush(cpu), which flushes every mmuidx.  Nor are you making use of per-page flushing.

So, really, no change required at all there.

Got it, let me try this method.

Thanks,
Fei.

Quoted 2 lines

r~

author
#17 lore raw
Quoted 38 lines · Fei
Quoted 37 lines · Richard Henderson
Quoted 8 lines · Fei
Quoted 8 lines
Quoted 5 lines

You should be making use of different softmmu indexes, similar to how ARM uses a separate index for PAN (privileged access never) mode.  If I read the manual properly, PAN == !SUM.

When you do this, you need no additional flushing.

Hi Fei,

Quoted 8 lines
Quoted 2 lines

Let's follow Richard's advice.
Yes, I'm thinking about how to do it, and thank Richard for the advice.

My question is:
* If we ensure this separate index (S+SUM) has no overlapping tlb entries with S-mode (ignore M-mode so far), during SUM=1, we have to look into both (S+SUM) and S index for kernel address translation, that should be not desired.

This is an incorrect assumption.  S+SUM may very well have overlapping tlb entries with S.
With SUM=1, you *only* look in S+SUM index; with SUM=0, you *only* look in S index.

The only difference is a check in get_physical_address is no longer against MSTATUS_SUM directly, but against the mmu_index.

Quoted 3 lines

* If all the tlb operations are against (S+SUM) during SUM=1, then (S+SUM) could contain some duplicated tlb entries of kernel address in S index, the duplication means extra tlb lookup and fill.

Yes, if the same address is probed via S and S+SUM, there is a duplicated lookup.  But this is harmless.

Quoted 2 lines

Also if we want
to flush tlb entry of specific addr0, we have to flush both index.

Quoted 8 lines
Quoted 6 lines

Yes, this is also true.  But so far target/riscv is making no use of per-mmuidx flushing. At the moment you're *only* using tlb_flush(cpu), which flushes every mmuidx.  Nor are you making use of per-page flushing.

So, really, no change required at all there.

Got it, let me try this method.

There seems no room in flags for this extra index, all 3 bits for mem_idx have been used in target/riscv/cpu.h. We need some trick.

#define TB_FLAGS_PRIV_MMU_MASK 3
#define TB_FLAGS_PRIV_HYP_ACCESS_MASK (1 << 2)

FIELD(TB_FLAGS, MEM_IDX, 0, 3)
FIELD(TB_FLAGS, LMUL, 3, 3)

Thanks,
Fei.

Quoted 6 lines

Thanks,
Fei.

Quoted 2 lines

r~

#18 lore raw
Quoted 40 lines · Fei
Quoted 39 lines · Fei
Quoted 38 lines · Richard Henderson
Quoted 7 lines · Fei
Quoted 7 lines
Quoted 5 lines

You should be making use of different softmmu indexes, similar to how ARM uses a separate index for PAN (privileged access never) mode.  If I read the manual properly, PAN == !SUM.

When you do this, you need no additional flushing.

Hi Fei,

Quoted 7 lines
Quoted 2 lines

Let's follow Richard's advice.
Yes, I'm thinking about how to do it, and thank Richard for the advice.

My question is:
* If we ensure this separate index (S+SUM) has no overlapping tlb entries with S-mode (ignore M-mode so far), during SUM=1, we have to look into both (S+SUM) and S index for kernel address translation, that should be not desired.

This is an incorrect assumption.  S+SUM may very well have overlapping tlb entries with S.
With SUM=1, you *only* look in S+SUM index; with SUM=0, you *only* look in S index.

The only difference is a check in get_physical_address is no longer against MSTATUS_SUM directly, but against the mmu_index.

Quoted 3 lines

* If all the tlb operations are against (S+SUM) during SUM=1, then (S+SUM) could contain some duplicated tlb entries of kernel address in S index, the duplication means extra tlb lookup and fill.

Yes, if the same address is probed via S and S+SUM, there is a duplicated lookup.  But this is harmless.

Quoted 2 lines

Also if we want
to flush tlb entry of specific addr0, we have to flush both index.

Yes, this is also true.  But so far target/riscv is making no use of per-mmuidx flushing. At the moment you're *only* using tlb_flush(cpu), which flushes every mmuidx.  Nor are you making use of per-page flushing.

So, really, no change required at all there.

Quoted 7 lines
Quoted 2 lines

Got it, let me try this method.

There seems no room in flags for this extra index, all 3 bits for mem_idx have been used in target/riscv/cpu.h. We need some trick.

#define TB_FLAGS_PRIV_MMU_MASK 3
#define TB_FLAGS_PRIV_HYP_ACCESS_MASK (1 << 2)

#define TB_FLAGS_PRIV_HYP_ACCESS_MASK (1 << 3)

Renumber the new mmu index to 5 (Probably by extending the function riscv_cpu_mmu_index)

Zhiwei

Quoted 10 lines

FIELD(TB_FLAGS, MEM_IDX, 0, 3)
FIELD(TB_FLAGS, LMUL, 3, 3)

Thanks,
Fei.

Quoted 4 lines

Thanks,
Fei.

Quoted 1 line

r~

author
#19 lore raw
Quoted 53 lines · LIU Zhiwei
Quoted 51 lines · Fei
Quoted 38 lines · Fei
Quoted 37 lines · Richard Henderson
Quoted 12 lines · Fei
Quoted 6 lines
Quoted 6 lines

You should be making use of different softmmu indexes, similar to how
ARM uses a separate index for PAN (privileged access never) mode.  If
I read the manual properly, PAN == !SUM.

Quoted 6 lines
Quoted 1 line

When you do this, you need no additional flushing.

Hi Fei,

Let's follow Richard's advice.
Yes, I'm thinking about how to do it, and thank Richard for the advice.

Quoted 6 lines

My question is:
* If we ensure this separate index (S+SUM) has no overlapping tlb entries with S-mode (ignore M-mode so far), during SUM=1, we have to look into both (S+SUM) and S index for kernel address translation, that
should be not desired.

This is an incorrect assumption.  S+SUM may very well have overlapping tlb entries with S.
With SUM=1, you *only* look in S+SUM index; with SUM=0, you *only* look in S index.

The only difference is a check in get_physical_address is no longer against MSTATUS_SUM directly, but against the mmu_index.

Quoted 4 lines

* If all the tlb operations are against (S+SUM) during SUM=1, then (S+SUM) could contain some duplicated tlb entries of kernel address in S
index, the duplication means extra tlb lookup and fill.

Yes, if the same address is probed via S and S+SUM, there is a duplicated lookup.  But this is harmless.

Quoted 2 lines

Also if we want
to flush tlb entry of specific addr0, we have to flush both index.

Quoted 9 lines
Quoted 7 lines

Yes, this is also true.  But so far target/riscv is making no use of per-mmuidx flushing. At the moment you're *only* using tlb_flush(cpu), which flushes every mmuidx.  Nor are you making use of per-page flushing.

So, really, no change required at all there.

Got it, let me try this method.

There seems no room in flags for this extra index, all 3 bits for mem_idx have been used in target/riscv/cpu.h. We need some trick.

Quoted 8 lines
Quoted 2 lines

#define TB_FLAGS_PRIV_MMU_MASK                3
#define TB_FLAGS_PRIV_HYP_ACCESS_MASK   (1 << 2)

#define TB_FLAGS_PRIV_HYP_ACCESS_MASK   (1 << 3)

Renumber the new mmu index to 5 (Probably by extending the function riscv_cpu_mmu_index)

Currently mem_idx is also saved in flags (tb_flags) below, which only has 3 bits for mem_idx and can't expand.

Thanks,
Fei.

Quoted 13 lines

Zhiwei

Quoted 10 lines

FIELD(TB_FLAGS, MEM_IDX, 0, 3)
FIELD(TB_FLAGS, LMUL, 3, 3)

Thanks,
Fei.

Quoted 4 lines

Thanks,
Fei.

Quoted 1 line

r~