parisc: Fix patch code locking and flushing
authorJohn David Anglin <dave.anglin@bell.net>
Mon, 16 May 2022 15:32:00 +0000 (15:32 +0000)
committerHelge Deller <deller@gmx.de>
Tue, 17 May 2022 19:52:59 +0000 (21:52 +0200)
This change fixes the following:

1) The flags variable is not initialized. Always use raw_spin_lock_irqsave
and raw_spin_unlock_irqrestore to serialize patching.

2) flush_kernel_vmap_range is primarily intended for DMA flushes.
The whole cache flush in flush_kernel_vmap_range is only possible
when interrupts are enabled on SMP machines. Since __patch_text_multiple
calls flush_kernel_vmap_range with interrupts disabled, it is better
to directly call flush_kernel_dcache_range_asm and
flush_kernel_icache_range_asm.

3) The final call to flush_icache_range is unnecessary.

Tested with `[PATCH, V3] parisc: Rewrite cache flush code for
PA8800/PA8900' change on rp3440, c8000 and c3750 (32 and 64-bit).

Note by Helge:
This patch had been temporarily reverted shortly before v5.18-rc6 in order
to fix boot issues. Now it can be re-applied.

Signed-off-by: John David Anglin <dave.anglin@bell.net>
Signed-off-by: Helge Deller <deller@gmx.de>
arch/parisc/kernel/patch.c

index 80a0ab3..e59574f 100644 (file)
@@ -40,10 +40,7 @@ static void __kprobes *patch_map(void *addr, int fixmap, unsigned long *flags,
 
        *need_unmap = 1;
        set_fixmap(fixmap, page_to_phys(page));
-       if (flags)
-               raw_spin_lock_irqsave(&patch_lock, *flags);
-       else
-               __acquire(&patch_lock);
+       raw_spin_lock_irqsave(&patch_lock, *flags);
 
        return (void *) (__fix_to_virt(fixmap) + (uintaddr & ~PAGE_MASK));
 }
@@ -52,10 +49,7 @@ static void __kprobes patch_unmap(int fixmap, unsigned long *flags)
 {
        clear_fixmap(fixmap);
 
-       if (flags)
-               raw_spin_unlock_irqrestore(&patch_lock, *flags);
-       else
-               __release(&patch_lock);
+       raw_spin_unlock_irqrestore(&patch_lock, *flags);
 }
 
 void __kprobes __patch_text_multiple(void *addr, u32 *insn, unsigned int len)
@@ -67,8 +61,9 @@ void __kprobes __patch_text_multiple(void *addr, u32 *insn, unsigned int len)
        int mapped;
 
        /* Make sure we don't have any aliases in cache */
-       flush_kernel_vmap_range(addr, len);
-       flush_icache_range(start, end);
+       flush_kernel_dcache_range_asm(start, end);
+       flush_kernel_icache_range_asm(start, end);
+       flush_tlb_kernel_range(start, end);
 
        p = fixmap = patch_map(addr, FIX_TEXT_POKE0, &flags, &mapped);
 
@@ -81,8 +76,10 @@ void __kprobes __patch_text_multiple(void *addr, u32 *insn, unsigned int len)
                         * We're crossing a page boundary, so
                         * need to remap
                         */
-                       flush_kernel_vmap_range((void *)fixmap,
-                                               (p-fixmap) * sizeof(*p));
+                       flush_kernel_dcache_range_asm((unsigned long)fixmap,
+                                                     (unsigned long)p);
+                       flush_tlb_kernel_range((unsigned long)fixmap,
+                                              (unsigned long)p);
                        if (mapped)
                                patch_unmap(FIX_TEXT_POKE0, &flags);
                        p = fixmap = patch_map(addr, FIX_TEXT_POKE0, &flags,
@@ -90,10 +87,10 @@ void __kprobes __patch_text_multiple(void *addr, u32 *insn, unsigned int len)
                }
        }
 
-       flush_kernel_vmap_range((void *)fixmap, (p-fixmap) * sizeof(*p));
+       flush_kernel_dcache_range_asm((unsigned long)fixmap, (unsigned long)p);
+       flush_tlb_kernel_range((unsigned long)fixmap, (unsigned long)p);
        if (mapped)
                patch_unmap(FIX_TEXT_POKE0, &flags);
-       flush_icache_range(start, end);
 }
 
 void __kprobes __patch_text(void *addr, u32 insn)