ARM: 8043/1: uprobes need icache flush after xol write
authorVictor Kamensky <victor.kamensky@linaro.org>
Tue, 29 Apr 2014 03:20:52 +0000 (04:20 +0100)
committerRussell King <rmk+kernel@arm.linux.org.uk>
Sun, 25 May 2014 22:48:45 +0000 (23:48 +0100)
After instruction write into xol area, on ARM V7
architecture code need to flush dcache and icache to sync
them up for given set of addresses. Having just
'flush_dcache_page(page)' call is not enough - it is
possible to have stale instruction sitting in icache
for given xol area slot address.

Introduce arch_uprobe_ixol_copy weak function
that by default calls uprobes copy_to_page function and
than flush_dcache_page function and on ARM define new one
that handles xol slot copy in ARM specific way

flush_uprobe_xol_access function shares/reuses implementation
with/of flush_ptrace_access function and takes care of writing
instruction to user land address space on given variety of
different cache types on ARM CPUs. Because
flush_uprobe_xol_access does not have vma around
flush_ptrace_access was split into two parts. First that
retrieves set of condition from vma and common that receives
those conditions as flags.

Note ARM cache flush function need kernel address
through which instruction write happened, so instead
of using uprobes copy_to_page function changed
code to explicitly map page and do memcpy.

Note arch_uprobe_copy_ixol function, in similar way as
copy_to_user_page function, has preempt_disable/preempt_enable.

Signed-off-by: Victor Kamensky <victor.kamensky@linaro.org>
Acked-by: Oleg Nesterov <oleg@redhat.com>
Reviewed-by: David A. Long <dave.long@linaro.org>
Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
arch/arm/include/asm/cacheflush.h
arch/arm/kernel/uprobes.c
arch/arm/mm/flush.c
include/linux/uprobes.h
kernel/events/uprobes.c

index 00af9fe435e641e3c3a3ecf6cdcecfd082372c30..fd43f7f55b701f95393af71be7ef2259139f2586 100644 (file)
@@ -487,4 +487,6 @@ int set_memory_rw(unsigned long addr, int numpages);
 int set_memory_x(unsigned long addr, int numpages);
 int set_memory_nx(unsigned long addr, int numpages);
 
+void flush_uprobe_xol_access(struct page *page, unsigned long uaddr,
+                            void *kaddr, unsigned long len);
 #endif
index f9bacee973bf686cf61405ca2dda3916e3f92d80..56adf9c1fde0670d6a3826536391dcf1d2bb5646 100644 (file)
@@ -113,6 +113,26 @@ int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm,
        return 0;
 }
 
+void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
+                          void *src, unsigned long len)
+{
+       void *xol_page_kaddr = kmap_atomic(page);
+       void *dst = xol_page_kaddr + (vaddr & ~PAGE_MASK);
+
+       preempt_disable();
+
+       /* Initialize the slot */
+       memcpy(dst, src, len);
+
+       /* flush caches (dcache/icache) */
+       flush_uprobe_xol_access(page, vaddr, dst, len);
+
+       preempt_enable();
+
+       kunmap_atomic(xol_page_kaddr);
+}
+
+
 int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
 {
        struct uprobe_task *utask = current->utask;
index 3387e60e4ea381c579725774ebab8d587eeded0c..43d54f5b26b9b08edf95dd1b48b13ff6440451af 100644 (file)
@@ -104,17 +104,20 @@ void flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsig
 #define flush_icache_alias(pfn,vaddr,len)      do { } while (0)
 #endif
 
+#define FLAG_PA_IS_EXEC 1
+#define FLAG_PA_CORE_IN_MM 2
+
 static void flush_ptrace_access_other(void *args)
 {
        __flush_icache_all();
 }
 
-static
-void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
-                        unsigned long uaddr, void *kaddr, unsigned long len)
+static inline
+void __flush_ptrace_access(struct page *page, unsigned long uaddr, void *kaddr,
+                          unsigned long len, unsigned int flags)
 {
        if (cache_is_vivt()) {
-               if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(vma->vm_mm))) {
+               if (flags & FLAG_PA_CORE_IN_MM) {
                        unsigned long addr = (unsigned long)kaddr;
                        __cpuc_coherent_kern_range(addr, addr + len);
                }
@@ -128,7 +131,7 @@ void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
        }
 
        /* VIPT non-aliasing D-cache */
-       if (vma->vm_flags & VM_EXEC) {
+       if (flags & FLAG_PA_IS_EXEC) {
                unsigned long addr = (unsigned long)kaddr;
                if (icache_is_vipt_aliasing())
                        flush_icache_alias(page_to_pfn(page), uaddr, len);
@@ -140,6 +143,26 @@ void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
        }
 }
 
+static
+void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
+                        unsigned long uaddr, void *kaddr, unsigned long len)
+{
+       unsigned int flags = 0;
+       if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(vma->vm_mm)))
+               flags |= FLAG_PA_CORE_IN_MM;
+       if (vma->vm_flags & VM_EXEC)
+               flags |= FLAG_PA_IS_EXEC;
+       __flush_ptrace_access(page, uaddr, kaddr, len, flags);
+}
+
+void flush_uprobe_xol_access(struct page *page, unsigned long uaddr,
+                            void *kaddr, unsigned long len)
+{
+       unsigned int flags = FLAG_PA_CORE_IN_MM|FLAG_PA_IS_EXEC;
+
+       __flush_ptrace_access(page, uaddr, kaddr, len, flags);
+}
+
 /*
  * Copy user data from/to a page which is mapped into a different
  * processes address space.  Really, we want to allow our "user
index edff2b97b86436a615895177b54b3de4a3f06aaa..c52f827ba6ce6449c3e6c62736d97d5c11192c39 100644 (file)
@@ -32,6 +32,7 @@ struct vm_area_struct;
 struct mm_struct;
 struct inode;
 struct notifier_block;
+struct page;
 
 #define UPROBE_HANDLER_REMOVE          1
 #define UPROBE_HANDLER_MASK            1
@@ -127,6 +128,8 @@ extern int  arch_uprobe_exception_notify(struct notifier_block *self, unsigned l
 extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
 extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
 extern bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs);
+extern void __weak arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
+                                        void *src, unsigned long len);
 #else /* !CONFIG_UPROBES */
 struct uprobes_state {
 };
index 04709b66369d83d1dc883ab5eea92505d5eed410..4968213c63fad484d921a36762ecc7fe4d6c3e28 100644 (file)
@@ -1296,14 +1296,8 @@ static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
        if (unlikely(!xol_vaddr))
                return 0;
 
-       /* Initialize the slot */
-       copy_to_page(area->page, xol_vaddr,
-                       &uprobe->arch.ixol, sizeof(uprobe->arch.ixol));
-       /*
-        * We probably need flush_icache_user_range() but it needs vma.
-        * This should work on supported architectures too.
-        */
-       flush_dcache_page(area->page);
+       arch_uprobe_copy_ixol(area->page, xol_vaddr,
+                             &uprobe->arch.ixol, sizeof(uprobe->arch.ixol));
 
        return xol_vaddr;
 }
@@ -1346,6 +1340,21 @@ static void xol_free_insn_slot(struct task_struct *tsk)
        }
 }
 
+void __weak arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
+                                 void *src, unsigned long len)
+{
+       /* Initialize the slot */
+       copy_to_page(page, vaddr, src, len);
+
+       /*
+        * We probably need flush_icache_user_range() but it needs vma.
+        * This should work on most of architectures by default. If
+        * architecture needs to do something different it can define
+        * its own version of the function.
+        */
+       flush_dcache_page(page);
+}
+
 /**
  * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  * @regs: Reflects the saved state of the task after it has hit a breakpoint
This page took 0.056255 seconds and 5 git commands to generate.