Skip to content

Commit d3a133a

Browse files
hramrachmpe
authored andcommitted
powerpc/perf: Consolidate perf_callchain_user_[64|32]()
perf_callchain_user_64() and perf_callchain_user_32() are nearly identical. Consolidate into one function with thin wrappers. Suggested-by: Nicholas Piggin <[email protected]> Signed-off-by: Michal Suchanek <[email protected]> [mpe: Adapt to copy_from_user_nofault(), minor formatting] Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent a0ff72f commit d3a133a

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

arch/powerpc/perf/callchain.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef _POWERPC_PERF_CALLCHAIN_H
33
#define _POWERPC_PERF_CALLCHAIN_H
44

5-
int read_user_stack_slow(void __user *ptr, void *buf, int nb);
5+
int read_user_stack_slow(const void __user *ptr, void *buf, int nb);
66
void perf_callchain_user_64(struct perf_callchain_entry_ctx *entry,
77
struct pt_regs *regs);
88
void perf_callchain_user_32(struct perf_callchain_entry_ctx *entry,
@@ -16,4 +16,27 @@ static inline bool invalid_user_sp(unsigned long sp)
1616
return (!sp || (sp & mask) || (sp > top));
1717
}
1818

19+
/*
20+
* On 32-bit we just access the address and let hash_page create a
21+
* HPTE if necessary, so there is no need to fall back to reading
22+
* the page tables. Since this is called at interrupt level,
23+
* do_page_fault() won't treat a DSI as a page fault.
24+
*/
25+
static inline int __read_user_stack(const void __user *ptr, void *ret,
26+
size_t size)
27+
{
28+
unsigned long addr = (unsigned long)ptr;
29+
int rc;
30+
31+
if (addr > TASK_SIZE - size || (addr & (size - 1)))
32+
return -EFAULT;
33+
34+
rc = copy_from_user_nofault(ret, ptr, size);
35+
36+
if (IS_ENABLED(CONFIG_PPC64) && rc)
37+
return read_user_stack_slow(ptr, ret, size);
38+
39+
return rc;
40+
}
41+
1942
#endif /* _POWERPC_PERF_CALLCHAIN_H */

arch/powerpc/perf/callchain_32.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,9 @@
3030

3131
#endif /* CONFIG_PPC64 */
3232

33-
/*
34-
* On 32-bit we just access the address and let hash_page create a
35-
* HPTE if necessary, so there is no need to fall back to reading
36-
* the page tables. Since this is called at interrupt level,
37-
* do_page_fault() won't treat a DSI as a page fault.
38-
*/
39-
static int read_user_stack_32(unsigned int __user *ptr, unsigned int *ret)
33+
static int read_user_stack_32(const unsigned int __user *ptr, unsigned int *ret)
4034
{
41-
int rc;
42-
43-
if ((unsigned long)ptr > TASK_SIZE - sizeof(unsigned int) ||
44-
((unsigned long)ptr & 3))
45-
return -EFAULT;
46-
47-
rc = copy_from_user_nofault(ret, ptr, sizeof(*ret));
48-
49-
if (IS_ENABLED(CONFIG_PPC64) && rc)
50-
return read_user_stack_slow(ptr, ret, 4);
51-
52-
return rc;
35+
return __read_user_stack(ptr, ret, sizeof(*ret));
5336
}
5437

5538
/*

arch/powerpc/perf/callchain_64.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* interrupt context, so if the access faults, we read the page tables
2424
* to find which page (if any) is mapped and access it directly.
2525
*/
26-
int read_user_stack_slow(void __user *ptr, void *buf, int nb)
26+
int read_user_stack_slow(const void __user *ptr, void *buf, int nb)
2727
{
2828

2929
unsigned long addr = (unsigned long) ptr;
@@ -44,16 +44,9 @@ int read_user_stack_slow(void __user *ptr, void *buf, int nb)
4444
return -EFAULT;
4545
}
4646

47-
static int read_user_stack_64(unsigned long __user *ptr, unsigned long *ret)
47+
static int read_user_stack_64(const unsigned long __user *ptr, unsigned long *ret)
4848
{
49-
if ((unsigned long)ptr > TASK_SIZE - sizeof(unsigned long) ||
50-
((unsigned long)ptr & 7))
51-
return -EFAULT;
52-
53-
if (!copy_from_user_nofault(ret, ptr, sizeof(*ret)))
54-
return 0;
55-
56-
return read_user_stack_slow(ptr, ret, 8);
49+
return __read_user_stack(ptr, ret, sizeof(*ret));
5750
}
5851

5952
/*

0 commit comments

Comments
 (0)