Skip to content

Commit 72e6ae2

Browse files
Victor KamenskyRussell King
authored andcommitted
ARM: 8043/1: uprobes need icache flush after xol write
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>
1 parent 166aaf3 commit 72e6ae2

File tree

5 files changed

+70
-13
lines changed

5 files changed

+70
-13
lines changed

arch/arm/include/asm/cacheflush.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,4 +487,6 @@ int set_memory_rw(unsigned long addr, int numpages);
487487
int set_memory_x(unsigned long addr, int numpages);
488488
int set_memory_nx(unsigned long addr, int numpages);
489489

490+
void flush_uprobe_xol_access(struct page *page, unsigned long uaddr,
491+
void *kaddr, unsigned long len);
490492
#endif

arch/arm/kernel/uprobes.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,26 @@ int arch_uprobe_analyze_insn(struct arch_uprobe *auprobe, struct mm_struct *mm,
113113
return 0;
114114
}
115115

116+
void arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
117+
void *src, unsigned long len)
118+
{
119+
void *xol_page_kaddr = kmap_atomic(page);
120+
void *dst = xol_page_kaddr + (vaddr & ~PAGE_MASK);
121+
122+
preempt_disable();
123+
124+
/* Initialize the slot */
125+
memcpy(dst, src, len);
126+
127+
/* flush caches (dcache/icache) */
128+
flush_uprobe_xol_access(page, vaddr, dst, len);
129+
130+
preempt_enable();
131+
132+
kunmap_atomic(xol_page_kaddr);
133+
}
134+
135+
116136
int arch_uprobe_pre_xol(struct arch_uprobe *auprobe, struct pt_regs *regs)
117137
{
118138
struct uprobe_task *utask = current->utask;

arch/arm/mm/flush.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,20 @@ void flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsig
104104
#define flush_icache_alias(pfn,vaddr,len) do { } while (0)
105105
#endif
106106

107+
#define FLAG_PA_IS_EXEC 1
108+
#define FLAG_PA_CORE_IN_MM 2
109+
107110
static void flush_ptrace_access_other(void *args)
108111
{
109112
__flush_icache_all();
110113
}
111114

112-
static
113-
void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
114-
unsigned long uaddr, void *kaddr, unsigned long len)
115+
static inline
116+
void __flush_ptrace_access(struct page *page, unsigned long uaddr, void *kaddr,
117+
unsigned long len, unsigned int flags)
115118
{
116119
if (cache_is_vivt()) {
117-
if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(vma->vm_mm))) {
120+
if (flags & FLAG_PA_CORE_IN_MM) {
118121
unsigned long addr = (unsigned long)kaddr;
119122
__cpuc_coherent_kern_range(addr, addr + len);
120123
}
@@ -128,7 +131,7 @@ void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
128131
}
129132

130133
/* VIPT non-aliasing D-cache */
131-
if (vma->vm_flags & VM_EXEC) {
134+
if (flags & FLAG_PA_IS_EXEC) {
132135
unsigned long addr = (unsigned long)kaddr;
133136
if (icache_is_vipt_aliasing())
134137
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,
140143
}
141144
}
142145

146+
static
147+
void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
148+
unsigned long uaddr, void *kaddr, unsigned long len)
149+
{
150+
unsigned int flags = 0;
151+
if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(vma->vm_mm)))
152+
flags |= FLAG_PA_CORE_IN_MM;
153+
if (vma->vm_flags & VM_EXEC)
154+
flags |= FLAG_PA_IS_EXEC;
155+
__flush_ptrace_access(page, uaddr, kaddr, len, flags);
156+
}
157+
158+
void flush_uprobe_xol_access(struct page *page, unsigned long uaddr,
159+
void *kaddr, unsigned long len)
160+
{
161+
unsigned int flags = FLAG_PA_CORE_IN_MM|FLAG_PA_IS_EXEC;
162+
163+
__flush_ptrace_access(page, uaddr, kaddr, len, flags);
164+
}
165+
143166
/*
144167
* Copy user data from/to a page which is mapped into a different
145168
* processes address space. Really, we want to allow our "user

include/linux/uprobes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct vm_area_struct;
3232
struct mm_struct;
3333
struct inode;
3434
struct notifier_block;
35+
struct page;
3536

3637
#define UPROBE_HANDLER_REMOVE 1
3738
#define UPROBE_HANDLER_MASK 1
@@ -127,6 +128,8 @@ extern int arch_uprobe_exception_notify(struct notifier_block *self, unsigned l
127128
extern void arch_uprobe_abort_xol(struct arch_uprobe *aup, struct pt_regs *regs);
128129
extern unsigned long arch_uretprobe_hijack_return_addr(unsigned long trampoline_vaddr, struct pt_regs *regs);
129130
extern bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs);
131+
extern void __weak arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
132+
void *src, unsigned long len);
130133
#else /* !CONFIG_UPROBES */
131134
struct uprobes_state {
132135
};

kernel/events/uprobes.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,14 +1296,8 @@ static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
12961296
if (unlikely(!xol_vaddr))
12971297
return 0;
12981298

1299-
/* Initialize the slot */
1300-
copy_to_page(area->page, xol_vaddr,
1301-
&uprobe->arch.ixol, sizeof(uprobe->arch.ixol));
1302-
/*
1303-
* We probably need flush_icache_user_range() but it needs vma.
1304-
* This should work on supported architectures too.
1305-
*/
1306-
flush_dcache_page(area->page);
1299+
arch_uprobe_copy_ixol(area->page, xol_vaddr,
1300+
&uprobe->arch.ixol, sizeof(uprobe->arch.ixol));
13071301

13081302
return xol_vaddr;
13091303
}
@@ -1346,6 +1340,21 @@ static void xol_free_insn_slot(struct task_struct *tsk)
13461340
}
13471341
}
13481342

1343+
void __weak arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
1344+
void *src, unsigned long len)
1345+
{
1346+
/* Initialize the slot */
1347+
copy_to_page(page, vaddr, src, len);
1348+
1349+
/*
1350+
* We probably need flush_icache_user_range() but it needs vma.
1351+
* This should work on most of architectures by default. If
1352+
* architecture needs to do something different it can define
1353+
* its own version of the function.
1354+
*/
1355+
flush_dcache_page(page);
1356+
}
1357+
13491358
/**
13501359
* uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
13511360
* @regs: Reflects the saved state of the task after it has hit a breakpoint

0 commit comments

Comments
 (0)