From: Michael Ellerman <mpe@ellerman.id.au> Date: 2021-03-31 00:40:54
In the past we had a fallback definition for _PAGE_KERNEL_ROX, but we
removed that in commit d82fd29c5a8c ("powerpc/mm: Distribute platform
specific PAGE and PMD flags and definitions") and added definitions
for each MMU family.
However we missed adding a definition for 64s, which was not really a
bug because it's currently not used.
But we'd like to use PAGE_KERNEL_ROX in a future patch so add a
definition now.
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
---
arch/powerpc/include/asm/book3s/64/pgtable.h | 1 +
1 file changed, 1 insertion(+)
v2: Unchanged.
From: Michael Ellerman <mpe@ellerman.id.au> Date: 2021-03-31 00:39:17
In hash__mark_rodata_ro() we pass the raw PP_RXXX value to
hash__change_memory_range(). That has the effect of setting the key to
zero, because PP_RXXX contains no key value.
Fix it by using htab_convert_pte_flags(), which knows how to convert a
pgprot into a pp value, including the key.
Fixes: d94b827e89dc ("powerpc/book3s64/kuap: Use Key 3 for kernel mapping with hash translation")
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Reviewed-by: Daniel Axtens <redacted>
---
arch/powerpc/mm/book3s64/hash_pgtable.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
v2: Unchanged.
From: Michael Ellerman <mpe@ellerman.id.au> Date: 2021-03-31 00:39:38
When we enabled STRICT_KERNEL_RWX we received some reports of boot
failures when using the Hash MMU and running under phyp. The crashes
are intermittent, and often exhibit as a completely unresponsive
system, or possibly an oops.
One example, which was caught in xmon:
[ 14.068327][ T1] devtmpfs: mounted
[ 14.069302][ T1] Freeing unused kernel memory: 5568K
[ 14.142060][ T347] BUG: Unable to handle kernel instruction fetch
[ 14.142063][ T1] Run /sbin/init as init process
[ 14.142074][ T347] Faulting instruction address: 0xc000000000004400
cpu 0x2: Vector: 400 (Instruction Access) at [c00000000c7475e0]
pc: c000000000004400: exc_virt_0x4400_instruction_access+0x0/0x80
lr: c0000000001862d4: update_rq_clock+0x44/0x110
sp: c00000000c747880
msr: 8000000040001031
current = 0xc00000000c60d380
paca = 0xc00000001ec9de80 irqmask: 0x03 irq_happened: 0x01
pid = 347, comm = kworker/2:1
...
enter ? for help
[c00000000c747880] c0000000001862d4 update_rq_clock+0x44/0x110 (unreliable)
[c00000000c7478f0] c000000000198794 update_blocked_averages+0xb4/0x6d0
[c00000000c7479f0] c000000000198e40 update_nohz_stats+0x90/0xd0
[c00000000c747a20] c0000000001a13b4 _nohz_idle_balance+0x164/0x390
[c00000000c747b10] c0000000001a1af8 newidle_balance+0x478/0x610
[c00000000c747be0] c0000000001a1d48 pick_next_task_fair+0x58/0x480
[c00000000c747c40] c000000000eaab5c __schedule+0x12c/0x950
[c00000000c747cd0] c000000000eab3e8 schedule+0x68/0x120
[c00000000c747d00] c00000000016b730 worker_thread+0x130/0x640
[c00000000c747da0] c000000000174d50 kthread+0x1a0/0x1b0
[c00000000c747e10] c00000000000e0f0 ret_from_kernel_thread+0x5c/0x6c
This shows that CPU 2, which was idle, woke up and then appears to
randomly take an instruction fault on a completely valid area of
kernel text.
The cause turns out to be the call to hash__mark_rodata_ro(), late in
boot. Due to the way we layout text and rodata, that function actually
changes the permissions for all of text and rodata to read-only plus
execute.
To do the permission change we use a hypervisor call, H_PROTECT. On
phyp that appears to be implemented by briefly removing the mapping of
the kernel text, before putting it back with the updated permissions.
If any other CPU is executing during that window, it will see spurious
faults on the kernel text and/or data, leading to crashes.
To fix it we use stop machine to collect all other CPUs, and then have
them drop into real mode (MMU off), while we change the mapping. That
way they are unaffected by the mapping temporarily disappearing.
We don't see this bug on KVM because KVM always use VPM=1, where
faults are directed to the hypervisor, and the fault will be
serialised vs the h_protect() by HPTE_V_HVLOCK.
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
---
arch/powerpc/mm/book3s64/hash_pgtable.c | 105 +++++++++++++++++++++++-
1 file changed, 104 insertions(+), 1 deletion(-)
v2: Add mention of why we don't see the issue on KVM.
Use hard_irq_disable() not __hard_EE_RI_disable() as noticed by Nick.
@@ -400,6 +401,19 @@ EXPORT_SYMBOL_GPL(hash__has_transparent_hugepage);#endif /* CONFIG_TRANSPARENT_HUGEPAGE */#ifdef CONFIG_STRICT_KERNEL_RWX++structchange_memory_parms{+unsignedlongstart,end,newpp;+unsignedintstep,nr_cpus,master_cpu;+atomic_tcpu_counter;+};++// We'd rather this was on the stack but it has to be in the RMO+staticstructchange_memory_parmschmem_parms;++// And therefore we need a lock to protect it from concurrent use+staticDEFINE_MUTEX(chmem_lock);+staticvoidchange_memory_range(unsignedlongstart,unsignedlongend,unsignedintstep,unsignedlongnewpp){
@@ -414,6 +428,73 @@ static void change_memory_range(unsigned long start, unsigned long end,mmu_kernel_ssize);}+staticintnotracechmem_secondary_loop(structchange_memory_parms*parms)+{+unsignedlongmsr,tmp,flags;+int*p;++p=&parms->cpu_counter.counter;++local_irq_save(flags);+hard_irq_disable();++asmvolatile(+// Switch to real mode and leave interrupts off+"mfmsr %[msr] ;"+"li %[tmp], %[MSR_IR_DR] ;"+"andc %[tmp], %[msr], %[tmp] ;"+"mtmsrd %[tmp] ;"++// Tell the master we are in real mode+"1: "+"lwarx %[tmp], 0, %[p] ;"+"addic %[tmp], %[tmp], -1 ;"+"stwcx. %[tmp], 0, %[p] ;"+"bne- 1b ;"++// Spin until the counter goes to zero+"2: ;"+"lwz %[tmp], 0(%[p]) ;"+"cmpwi %[tmp], 0 ;"+"bne- 2b ;"++// Switch back to virtual mode+"mtmsrd %[msr] ;"++:// outputs+[msr]"=&r"(msr),[tmp]"=&b"(tmp),"+m"(*p)+:// inputs+[p]"b"(p),[MSR_IR_DR]"i"(MSR_IR|MSR_DR)+:// clobbers+"cc","xer"+);++local_irq_restore(flags);++return0;+}++staticintchange_memory_range_fn(void*data)+{+structchange_memory_parms*parms=data;++if(parms->master_cpu!=smp_processor_id())+returnchmem_secondary_loop(parms);++// Wait for all but one CPU (this one) to call-in+while(atomic_read(&parms->cpu_counter)>1)+barrier();++change_memory_range(parms->start,parms->end,parms->step,parms->newpp);++mb();++// Signal the other CPUs that we're done+atomic_dec(&parms->cpu_counter);++return0;+}+staticboolhash__change_memory_range(unsignedlongstart,unsignedlongend,unsignedlongnewpp){
@@ -428,7 +509,29 @@ static bool hash__change_memory_range(unsigned long start, unsigned long end,if(start>=end)returnfalse;-change_memory_range(start,end,step,newpp);+if(firmware_has_feature(FW_FEATURE_LPAR)){+mutex_lock(&chmem_lock);++chmem_parms.start=start;+chmem_parms.end=end;+chmem_parms.step=step;+chmem_parms.newpp=newpp;+chmem_parms.master_cpu=smp_processor_id();++cpus_read_lock();++atomic_set(&chmem_parms.cpu_counter,num_online_cpus());++// Ensure state is consistent before we call the other CPUs+mb();++stop_machine_cpuslocked(change_memory_range_fn,&chmem_parms,+cpu_online_mask);++cpus_read_unlock();+mutex_unlock(&chmem_lock);+}else+change_memory_range(start,end,step,newpp);returntrue;}
From: Michael Ellerman <mpe@ellerman.id.au> Date: 2021-03-31 00:39:59
We have now fixed the known bugs in STRICT_KERNEL_RWX for Book3S
64-bit Hash and Radix MMUs, see preceding commits, so allow the
option to be selected again.
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
---
arch/powerpc/Kconfig | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
v2: Unchanged.
From: Michael Ellerman <mpe@ellerman.id.au> Date: 2021-03-31 00:40:32
Pull the loop calling hpte_updateboltedpp() out of
hash__change_memory_range() into a helper function. We need it to be a
separate function for the next patch.
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
---
arch/powerpc/mm/book3s64/hash_pgtable.c | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)
v2: Unchanged.
@@ -400,10 +400,23 @@ EXPORT_SYMBOL_GPL(hash__has_transparent_hugepage);#endif /* CONFIG_TRANSPARENT_HUGEPAGE */#ifdef CONFIG_STRICT_KERNEL_RWX+staticvoidchange_memory_range(unsignedlongstart,unsignedlongend,+unsignedintstep,unsignedlongnewpp)+{+unsignedlongidx;++pr_debug("Changing page protection on range 0x%lx-0x%lx, to 0x%lx, step 0x%x\n",+start,end,newpp,step);++for(idx=start;idx<end;idx+=step)+/* Not sure if we can do much with the return value */+mmu_hash_ops.hpte_updateboltedpp(newpp,idx,mmu_linear_psize,+mmu_kernel_ssize);+}+staticboolhash__change_memory_range(unsignedlongstart,unsignedlongend,unsignedlongnewpp){-unsignedlongidx;unsignedintstep,shift;shift=mmu_psize_defs[mmu_linear_psize].shift;
@@ -415,13 +428,7 @@ static bool hash__change_memory_range(unsigned long start, unsigned long end,if(start>=end)returnfalse;-pr_debug("Changing page protection on range 0x%lx-0x%lx, to 0x%lx, step 0x%x\n",-start,end,newpp,step);--for(idx=start;idx<end;idx+=step)-/* Not sure if we can do much with the return value */-mmu_hash_ops.hpte_updateboltedpp(newpp,idx,mmu_linear_psize,-mmu_kernel_ssize);+change_memory_range(start,end,step,newpp);returntrue;}
From: Michael Ellerman <mpe@ellerman.id.au> Date: 2021-03-31 00:41:17
The flags argument to plpar_pte_protect() (aka. H_PROTECT), includes
the key in bits 9-13, but currently we always set those bits to zero.
In the past that hasn't been a problem because we always used key 0
for the kernel, and updateboltedpp() is only used for kernel mappings.
However since commit d94b827e89dc ("powerpc/book3s64/kuap: Use Key 3
for kernel mapping with hash translation") we are now inadvertently
changing the key (to zero) when we call plpar_pte_protect().
That hasn't broken anything because updateboltedpp() is only used for
STRICT_KERNEL_RWX, which is currently disabled on 64s due to other
bugs.
But we want to fix that, so first we need to pass the key correctly to
plpar_pte_protect(). We can't pass our newpp value directly in, we
have to convert it into the form expected by the hcall.
The hcall we're using here is H_PROTECT, which is specified in section
14.5.4.1.6 of LoPAPR v1.1.
It takes a `flags` parameter, and the description for flags says:
* flags: AVPN, pp0, pp1, pp2, key0-key4, n, and for the CMO
option: CMO Option flags as defined in Table 189‚
If you then go to the start of the parent section, 14.5.4.1, on page
405, it says:
Register Linkage (For hcall() tokens 0x04 - 0x18)
* On Call
* R3 function call token
* R4 flags (see Table 178‚ “Page Frame Table Access flags field
definition‚” on page 401)
Then you have to go to section 14.5.3, and on page 394 there is a list
of hcalls and their tokens (table 176), and there you can see that
H_PROTECT == 0x18.
Finally you can look at table 178, on page 401, where it specifies the
layout of the bits for the key:
Bit Function
-----------------
50-54 | key0-key4
Those are big-endian bit numbers, converting to normal bit numbers you
get bits 9-13, or 0x3e00.
In the kernel we have:
#define HPTE_R_KEY_HI ASM_CONST(0x3000000000000000)
#define HPTE_R_KEY_LO ASM_CONST(0x0000000000000e00)
So the LO bits of newpp are already in the right place, and the HI
bits need to be shifted down by 48.
Fixes: d94b827e89dc ("powerpc/book3s64/kuap: Use Key 3 for kernel mapping with hash translation")
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
---
arch/powerpc/platforms/pseries/lpar.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
v2: Expand change log with explanation of where the format of the
flags parameter comes from, prompted by dja.
From: Michael Ellerman <hidden> Date: 2021-04-10 14:34:26
On Wed, 31 Mar 2021 11:38:40 +1100, Michael Ellerman wrote:
In the past we had a fallback definition for _PAGE_KERNEL_ROX, but we
removed that in commit d82fd29c5a8c ("powerpc/mm: Distribute platform
specific PAGE and PMD flags and definitions") and added definitions
for each MMU family.
However we missed adding a definition for 64s, which was not really a
bug because it's currently not used.
[...]