parisc kernel cache flush issue with mprotect()
Subsystems (4)
Helge Deller
maintainerWhile analyzing testcase failures in the libunwind package, I found a parisc Linux kernel issue.
The attached testcase initilizes a memory region, then calls mprotect(PROT_NONE) on that region and thus effectively should prevent userspace and kernel to access that memory.
But on parisc it seems the CPU cache isn't flushed when changing the protection of a memory region with mprotect(). So, the kernel will wrongly not fault when accessing this memory.
I think we are missing to purge the data cache when changing the protection.
Ideas?
Helge
/* Testcase based on Ltest-mem-validate.c from libunwind */
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/types.h>
int main(int argc, char **argv)
{
unsigned long page_size = sysconf(_SC_PAGESIZE); char *p = malloc(3 * page_size);
char *p_aligned;
/* initialize memory region. If not initialized, write syscall below will correctly return EFAULT. */ if (1)
memset(p, 'X', 3 * page_size);
p_aligned = (char *) ((((uintptr_t) p) + (2*page_size - 1)) & ~(page_size - 1)); /* Drop PROT_READ protection. Kernel and userspace should fault when accessing that memory region */ mprotect(p_aligned, page_size, PROT_NONE);
/* the following write() should return EFAULT, since PROT_READ was dropped by previous mprotect() */ int ret = write(2, p_aligned, 1);
if (!ret || errno != EFAULT)
printf("\n FAILURE: write() did not returned expected EFAULT value\n"); return 0;
}
Quoted 7 lines · Helge Deller
While analyzing testcase failures in the libunwind package, I found a parisc Linux kernel issue.
The attached testcase initilizes a memory region, then calls mprotect(PROT_NONE) on that region and thus effectively should prevent userspace and kernel to access that memory.
Quoted 7 lines
But on parisc it seems the CPU cache isn't flushed when changing the protection of a memory region with mprotect(). So, the kernel will wrongly not fault when accessing this memory.
I think we are missing to purge the data cache when changing the protection.
Ideas?
The attached patch fixes the test. It also
After a lot of study regarding our TLB handling, I realized that user pages can always be read by the kernel even when _PAGE_READ is dropped. We need to use the probe instruction to determine whether a page is accessible in user mode or not.
Probably, get_user needs a similar fix.
I've wondered if _PAGE_WRITE pages should always be writable by kernel? We could put _PAGE_WRITE in the most significant bit of PL2. Access mode 0 is nominally for public pages as protection checking is disabled for it. Maybe shouldn't use it? Does _PAGE_WRITE imply _PAGE_READ on Linux?
Dave
Signature · 1 line
John David Anglin [email protected]
arch/parisc/include/asm/special_insns.h +15 -0
@@ -32,6 +32,21 @@ pa; \ }) +#define prober_user (va) ({ \ + unsigned long pa; \ + __asm__ __volatile__( \ + "copy %%r0,%0\n" \ + "8:\tprobei,r (%%sr3,%1),3,%0\n" \ + "9:\n" \ + ASM_EXCEPTIONTABLE_ENTRY(8b, 9b, \ + "or %%r0,%%r0,%%r0") \ + : "=&r" (pa) \ + : "r" (va) \ + : "memory" \ + ); \ + pa; \ +}) +#define CR_EIEM 15/* External Interrupt Enable Mask */ #define CR_CR16 16/* CR16 Interval Timer */ #define CR_EIRR 23/* External Interrupt Request Register */
arch/parisc/lib/memcpy.c +9 -0
@@ -32,6 +32,15 @@ EXPORT_SYMBOL(raw_copy_to_user);unsigned long raw_copy_from_user (void *dst,const void __user *src,unsigned long len) { +unsigned long start = (unsigned long ) src; +unsigned long end = start + len; + +/* Check region is user accessible */ +while (start < end) { +if (!prober_user (src)) +return len; + start +=PAGE_SIZE ; + }mtsp (get_user_space (),SR_TEMP1 );mtsp (get_kernel_space (),SR_TEMP2 );return pa_memcpy (dst, (void __force *)src, len);
Quoted 21 lines · Dave Anglin
Quoted 7 lines · Helge Deller
While analyzing testcase failures in the libunwind package, I found a parisc Linux kernel issue.
The attached testcase initilizes a memory region, then calls mprotect(PROT_NONE) on that region and thus effectively should prevent userspace and kernel to access that memory.
Quoted 7 lines
But on parisc it seems the CPU cache isn't flushed when changing the protection of a memory region with mprotect(). So, the kernel will wrongly not fault when accessing this memory.
I think we are missing to purge the data cache when changing the protection.
Ideas?The attached patch fixes the test. It also
After a lot of study regarding our TLB handling, I realized that user pages can always be read by the kernel even when
_PAGE_READis dropped. We need to use the probe instruction to determine whether a page is accessible in user mode or not.
Quoted 6 lines
Probably,
get_userneeds a similar fix.I've wondered if
_PAGE_WRITEpages should always be writable by kernel? We could put_PAGE_WRITEin the most significant bit of PL2. Access mode 0 is nominally for public pages as protection checking is disabled for it. Maybe shouldn't use it? Does_PAGE_WRITEimply_PAGE_READon Linux?
Attached is the current patch that I'm testing on mx3210. I believe it fixes everything that I noticed debugging the mprotect-testcase. It also has your command line patch and some old radeon changes.
Dave
Signature · 1 line
John David Anglin [email protected]
arch/parisc/include/asm/pgtable.h +4 -3
@@ -276,7 +276,7 @@ extern unsigned long *empty_zero_page;#define pte_none (x) (pte_val(x) == 0)#define pte_present (x) (pte_val(x) & _PAGE_PRESENT)#define pte_user (x) (pte_val(x) & _PAGE_USER) -#define pte_clear (mm, addr, xp) set_pte(xp, __pte(0)) +#define pte_clear (mm, addr, xp) set_pte_at((mm), (addr), (xp), __pte(0))#define pmd_flag (x) (pmd_val(x) & PxD_FLAG_MASK)#define pmd_address (x) ((unsigned long)(pmd_val(x) &~ PxD_FLAG_MASK) << PxD_VALUE_SHIFT)
@@ -392,6 +392,7 @@ static inline void set_ptes(struct mm_struct *mm, unsigned long addr, } }#define set_ptes set_ptes +#define set_pte_at (mm, addr, ptep, pte) set_ptes(mm, addr, ptep, pte, 1)/* Used for deferring calls to flush_dcache_page() */
@@ -456,7 +457,7 @@ static inline int ptep_test_and_clear_young(struct vm_area_struct *vma, unsignedif (!pte_young (pte)) {return 0 ; } -set_pte (ptep,pte_mkold (pte)); +set_pte_at (vma->vm_mm, addr, ptep,pte_mkold (pte));return 1 ; }
@@ -466,7 +467,7 @@ pte_t ptep_clear_flush(struct vm_area_struct *vma, unsigned long addr, pte_t *ptstruct mm_struct ;static inline void ptep_set_wrprotect (struct mm_struct *mm,unsigned long addr,pte_t *ptep) { -set_pte (ptep,pte_wrprotect (*ptep)); +set_pte_at (mm, addr, ptep,pte_wrprotect (*ptep)); }#define pte_same (A ,B ) (pte_val(A) == pte_val(B))
arch/parisc/include/asm/special_insns.h +15 -0
@@ -32,6 +32,21 @@ pa; \ }) +#define prober_user (va) ({ \ + unsigned long pa; \ + __asm__ __volatile__( \ + "copy %%r0,%0\n" \ + "8:\tprobei,r (%%sr3,%1),3,%0\n" \ + "9:\n" \ + ASM_EXCEPTIONTABLE_ENTRY(8b, 9b, \ + "or %%r0,%%r0,%%r0") \ + : "=&r" (pa) \ + : "r" (va) \ + : "memory" \ + ); \ + pa; \ +}) +#define CR_EIEM 15/* External Interrupt Enable Mask */ #define CR_CR16 16/* CR16 Interval Timer */ #define CR_EIRR 23/* External Interrupt Request Register */
arch/parisc/kernel/cache.c +3 -3
@@ -429,7 +429,7 @@ static inline pte_t *get_ptep(struct mm_struct *mm, unsigned long addr)return ptep; } -static inline bool pte_needs_flush (pte_t pte) +static inline bool pte_needs_cache_flush (pte_t pte) {return (pte_val (pte) & (_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_NO_CACHE)) == (_PAGE_PRESENT | _PAGE_ACCESSED);
@@ -630,7 +630,7 @@ static void flush_cache_page_if_present(struct vm_area_struct *vma, ptep =get_ptep (vma->vm_mm, vmaddr);if (ptep) { pte =ptep_get (ptep); - needs_flush =pte_needs_flush (pte); + needs_flush =pte_needs_cache_flush (pte);pte_unmap (ptep); }if (needs_flush)
@@ -841,7 +841,7 @@ void flush_cache_vmap(unsigned long start, unsigned long end) } vm =find_vm_area ((void *)start); -if (WARN_ON_ONCE (!vm)) { +if (!vm) {flush_cache_all ();return ; }
arch/parisc/kernel/entry.S +12 -5
@@ -499,6 +499,12 @@ * this happens is quite subtle, read below */ .macro make_insert_tlb spc,pte,prot,tmp space_to_prot \spc \prot /* create prot id from space */ + +#if _PAGE_SPECIAL_BIT == _PAGE_DMB_BIT + /* need to drop DMB bit, as it's used as SPECIAL flag */ + depi 0,_PAGE_SPECIAL_BIT,1,\pte +#endif + /* The following is the real subtlety. This is depositing * T <-> _PAGE_REFTRAP * D <-> _PAGE_DIRTY
@@ -511,17 +517,18 @@ * Finally, _PAGE_READ goes in the top bit of PL1 (so we * trigger an access rights trap in user space if the user * tries to read an unreadable page */ -#if _PAGE_SPECIAL_BIT == _PAGE_DMB_BIT - /* need to drop DMB bit, as it's used as SPECIAL flag */ - depi 0,_PAGE_SPECIAL_BIT,1,\pte -#endif depd \pte,8,7,\prot /* PAGE_USER indicates the page can be read with user privileges, * so deposit X1|11 to PL1|PL2 (remember the upper bit of PL1 - * contains _PAGE_READ) */ + * contains _PAGE_READ). While the kernel can't directly write + * user pages which have _PAGE_WRITE zero, it can read pages + * pages which have _PAGE_READ zero (PL <= PL1). Thus, the kernel + * exception fault handler doesn't trigger when reading pages + * that aren't user read accessible */ extrd,u,*= \pte,_PAGE_USER_BIT+32,1,%r0 depdi 7,11,3,\prot + /* If we're a gateway page, drop PL2 back to zero for promotion * to kernel privilege (so we can execute the page as kernel). * Any privilege promotion page always denys read and write */
arch/parisc/lib/memcpy.c +9 -0
@@ -32,6 +32,15 @@ EXPORT_SYMBOL(raw_copy_to_user);unsigned long raw_copy_from_user (void *dst,const void __user *src,unsigned long len) { +unsigned long start = (unsigned long ) src; +unsigned long end = start + len; + +/* Check region is user accessible */ +while (start < end) { +if (!prober_user (src)) +return len; + start +=PAGE_SIZE ; + }mtsp (get_user_space (),SR_TEMP1 );mtsp (get_kernel_space (),SR_TEMP2 );return pa_memcpy (dst, (void __force *)src, len);
arch/parisc/mm/fault.c +147 -0
171 lines not on this page · raw message
drivers/gpu/drm/drm_cache.c +2 -0
@@ -174,6 +174,8 @@ drm_clflush_virt_range(void *addr, unsigned long length)if (wbinvd_on_all_cpus ())pr_err ("Timed out waiting for cache flush\n" ); +#elif defined(CONFIG_PARISC ) +flush_kernel_dcache_range ((unsigned long ) addr, length);#else WARN_ONCE (1 ,"Architecture has no drm_cache.c support\n" );#endif
drivers/gpu/drm/radeon/r100.c +4 -0
@@ -643,6 +643,10 @@ void r100_hpd_fini(struct radeon_device *rdev)*/ void r100_pci_gart_tlb_flush (struct radeon_device *rdev) { +/* flush gtt[] gart table entries from r100_pci_gart_set_page() */ +dma_sync_single_for_device (&rdev->pdev->dev, rdev->gart.table_addr, + rdev->gart.table_size,DMA_TO_DEVICE ); +/* TODO: can we do somethings here ? */ /* It seems hw only cache one entry so we should discard this * entry otherwise if first GPU GART read hit this entry it
drivers/gpu/drm/radeon/radeon_ring.c +4 -0
@@ -31,6 +31,7 @@#include <drm/drm_device.h> #include <drm/drm_file.h> +#include <drm/drm_cache.h> #include "radeon.h"
@@ -179,6 +180,9 @@ void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring,radeon_ring_write (ring, ring->nop); }mb (); +if (IS_ENABLED (CONFIG_PARISC )) +drm_clflush_virt_range ((void *)&ring->ring[0 ], ring->wptr *sizeof (uint32_t )); +/* If we are emitting the HDP flush via MMIO, we need to do it after * all CPU writes to VRAM finished. */
Quoted 6 lines · On Fri, 4 Jul 2025 15:49:07 -0400 Dave Anglin <[email protected]> wrote:
Attached is the current patch that I'm testing on mx3210. I believe it fixes everything that I noticed debugging the mprotect-testcase. It also has your command line patch and some old radeon changes.
Dave
While the kernel can't directly write user pages which have _PAGE_WRITE zero, it can read pages pages which have [...]
^^^^^ ^^^^^
Kind regards,
jer