Skip to contentSkip to conversation

target/arm: Fix FEAT_HADFS when page tables are in MMIO (cxl)

RFC PATCH Superseded Jonathan Cameron 2 participants 3 messages Feb 15, 2024

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover target/arm: Fix FEAT_HADFS when page tables are in MMIO (cxl)

https://lore.kernel.org/qemu-devel/[email protected]/
gives more details of the reason the CXL emulation uses MMIO space (short answer is it has very fine grained address routing).

Obviously this one is lower priority for me to upstream as the ARM support in general is only in my CXL staging tree (I'll get back to that soonish).

The equivalent tests with CXL memory and forcing applications to run from it on ARM64 TCG ran into an additional issue. We need to be able to do atomic compare and swap to update the access and dirty flags as part of a page walk if we have the ARM 8.1 FEAT_HADFS (note you can disable this in the kernel then this problem isn't hit but that's not a viable long term solution though it did help me diagnose the problem)

As the patch says I'm far from confident on this fix. Whilst it 'works' for little endian at least I'm hoping someone more familiar with the locking requirements etc can let me know if the BQL is the right option here and people can give a general opinion on whether such a hack is the right solution.

Jonathan Cameron (1):
arm/ptw: Handle atomic updates of page tables entries in MMIO during
PTW.

 target/arm/ptw.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

RFC PATCH 1/1 arm/ptw: Handle atomic updates of page tables entries in MMIO during PTW.

I'm far from confident this handling here is correct. Hence RFC. In particular not sure on what locks I should hold for this to be even moderately safe.

The function already appears to be inconsistent in what it returns as the CONFIG_ATOMIC64 block returns the endian converted 'eventual' value of the cmpxchg whereas the TCG_OVERSIZED_GUEST case returns the previous value.

---
 target/arm/ptw.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
target/arm/ptw.c +32 -2
index 5eb3577bcd..37ddb4a4db 100644
@@ -711,8 +711,38 @@ static uint64_t arm_casq_ptw(CPUARMState *env, uint64_t old_val,
     void *host = ptw->out_host;
 
     if (unlikely(!host)) {
-        fi->type = ARMFault_UnsuppAtomicUpdate;
-        return 0;
+        /* Can I do a load and store via the physical address */
+
+        bool locked = bql_locked();
+        if (!locked) {
+            bql_lock();
+        }
+        /* Page table in MMIO Memory Region */
+        if (ptw->out_be) {
+            old_val = cpu_to_be64(old_val);
+            new_val = cpu_to_be64(new_val);
+            cpu_physical_memory_read(ptw->out_phys, &cur_val, 8);
+            if (cur_val == old_val) {
+                cpu_physical_memory_write(ptw->out_phys, &new_val, 8);
+                cur_val = be64_to_cpu(new_val);
+            } else {
+                cur_val = be64_to_cpu(cur_val);
+            }
+        } else {
+            old_val = cpu_to_le64(old_val);
+            new_val = cpu_to_le64(new_val);
+            cpu_physical_memory_read(ptw->out_phys, &cur_val, 8);
+            if (cur_val == old_val) {
+                cpu_physical_memory_write(ptw->out_phys, &new_val, 8);
+                cur_val = le64_to_cpu(new_val);
+            } else {
+                cur_val = le64_to_cpu(cur_val);
+            }
+        }
+        if (!locked) {
+            bql_unlock();
+        }
+        return cur_val;
     }
 
     /*
Signature · 1 line
2.39.2
#2 lore raw

Re: [RFC PATCH 1/1] arm/ptw: Handle atomic updates of page tables entries in MMIO during PTW.

Quoted 26 lines · Jonathan Cameron

I'm far from confident this handling here is correct. Hence RFC. In particular not sure on what locks I should hold for this to be even moderately safe.

The function already appears to be inconsistent in what it returns as the CONFIG_ATOMIC64 block returns the endian converted 'eventual' value of the cmpxchg whereas the TCG_OVERSIZED_GUEST case returns the previous value.

---
 target/arm/ptw.c | 34 ++++++++++++++++++++++++++++++++--
 1 file changed, 32 insertions(+), 2 deletions(-)
target/arm/ptw.c +1 -2
index 5eb3577bcd..37ddb4a4db 100644
@@ -711,8 +711,38 @@ static uint64_t arm_casq_ptw(CPUARMState *env, uint64_t old_val,
     void *host = ptw->out_host;
 
     if (unlikely(!host)) {
-        fi->type = ARMFault_UnsuppAtomicUpdate;
-        return 0;
+        /* Can I do a load and store via the physical address */

target/arm/ptw.c static uint64_t arm_casq_ptw(CPUARMState *env, uint64_t old_val, · line 724

Quoted 10 lines
target/arm/ptw.c +10 -0
+
+        bool locked = bql_locked();
+        if (!locked) {
+            bql_lock();
+        }
+        /* Page table in MMIO Memory Region */
+        if (ptw->out_be) {
+            old_val = cpu_to_be64(old_val);
+            new_val = cpu_to_be64(new_val);
+            cpu_physical_memory_read(ptw->out_phys, &cur_val, 8);

This is definitely wrong because it takes no account of address spaces. You need to also account for ptw->out_space. Compare what arm_ldq_ptw() does.

Quoted 12 lines
target/arm/ptw.c +12 -0
+            if (cur_val == old_val) {
+                cpu_physical_memory_write(ptw->out_phys, &new_val, 8);
+                cur_val = be64_to_cpu(new_val);
+            } else {
+                cur_val = be64_to_cpu(cur_val);
+            }
+        } else {
+            old_val = cpu_to_le64(old_val);
+            new_val = cpu_to_le64(new_val);
+            cpu_physical_memory_read(ptw->out_phys, &cur_val, 8);
+            if (cur_val == old_val) {
+                cpu_physical_memory_write(ptw->out_phys, &new_val, 8);

target/arm/ptw.c static uint64_t arm_casq_ptw(CPUARMState *env, uint64_t old_val, · line 746

Quoted 10 lines
target/arm/ptw.c +9 -0
+                cur_val = le64_to_cpu(new_val);
+            } else {
+                cur_val = le64_to_cpu(cur_val);
+            }
+        }
+        if (!locked) {
+            bql_unlock();
+        }
+        return cur_val;
     }

thanks
-- PMM