aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/oprofile
diff options
context:
space:
mode:
authorRobert Richter <robert.richter@amd.com>2011-06-03 10:37:47 -0400
committerRobert Richter <robert.richter@amd.com>2011-06-15 08:31:33 -0400
commita0e3e70243f5b270bc3eca718f0a9fa5e6b8262e (patch)
treeebe0a5da53d123f4a0b3c0e4bfa83d322d406187 /arch/x86/oprofile
parent8fe7e94eb71430cf63a742f3c19739d82a662758 (diff)
oprofile, x86: Fix nmi-unsafe callgraph support
Current oprofile's x86 callgraph support may trigger page faults throwing the BUG_ON(in_nmi()) message below. This patch fixes this by using the same nmi-safe copy-from-user code as in perf. ------------[ cut here ]------------ kernel BUG at .../arch/x86/kernel/traps.c:436! invalid opcode: 0000 [#1] SMP last sysfs file: /sys/devices/pci0000:00/0000:00:0a.0/0000:07:00.0/0000:08:04.0/net/eth0/broadcast CPU 5 Modules linked in: Pid: 8611, comm: opcontrol Not tainted 2.6.39-00007-gfe47ae7 #1 Advanced Micro Device Anaheim/Anaheim RIP: 0010:[<ffffffff813e8e35>] [<ffffffff813e8e35>] do_nmi+0x22/0x1ee RSP: 0000:ffff88042fd47f28 EFLAGS: 00010002 RAX: ffff88042c0a7fd8 RBX: 0000000000000001 RCX: 00000000c0000101 RDX: 00000000ffff8804 RSI: ffffffffffffffff RDI: ffff88042fd47f58 RBP: ffff88042fd47f48 R08: 0000000000000004 R09: 0000000000001484 R10: 0000000000000001 R11: 0000000000000000 R12: ffff88042fd47f58 R13: 0000000000000000 R14: ffff88042fd47d98 R15: 0000000000000020 FS: 00007fca25e56700(0000) GS:ffff88042fd40000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000000074 CR3: 000000042d28b000 CR4: 00000000000006e0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000ffff0ff0 DR7: 0000000000000400 Process opcontrol (pid: 8611, threadinfo ffff88042c0a6000, task ffff88042c532310) Stack: 0000000000000000 0000000000000001 ffff88042c0a7fd8 0000000000000000 ffff88042fd47de8 ffffffff813e897a 0000000000000020 ffff88042fd47d98 0000000000000000 ffff88042c0a7fd8 ffff88042fd47de8 0000000000000074 Call Trace: <NMI> [<ffffffff813e897a>] nmi+0x1a/0x20 [<ffffffff813f08ab>] ? bad_to_user+0x25/0x771 <<EOE>> Code: ff 59 5b 41 5c 41 5d c9 c3 55 65 48 8b 04 25 88 b5 00 00 48 89 e5 41 55 41 54 49 89 fc 53 48 83 ec 08 f6 80 47 e0 ff ff 04 74 04 <0f> 0b eb fe 81 80 44 e0 ff ff 00 00 01 04 65 ff 04 25 c4 0f 01 RIP [<ffffffff813e8e35>] do_nmi+0x22/0x1ee RSP <ffff88042fd47f28> ---[ end trace ed6752185092104b ]--- Kernel panic - not syncing: Fatal exception in interrupt Pid: 8611, comm: opcontrol Tainted: G D 2.6.39-00007-gfe47ae7 #1 Call Trace: <NMI> [<ffffffff813e5e0a>] panic+0x8c/0x188 [<ffffffff813e915c>] oops_end+0x81/0x8e [<ffffffff8100403d>] die+0x55/0x5e [<ffffffff813e8c45>] do_trap+0x11c/0x12b [<ffffffff810023c8>] do_invalid_op+0x91/0x9a [<ffffffff813e8e35>] ? do_nmi+0x22/0x1ee [<ffffffff8131e6fa>] ? oprofile_add_sample+0x83/0x95 [<ffffffff81321670>] ? op_amd_check_ctrs+0x4f/0x2cf [<ffffffff813ee4d5>] invalid_op+0x15/0x20 [<ffffffff813e8e35>] ? do_nmi+0x22/0x1ee [<ffffffff813e8e7a>] ? do_nmi+0x67/0x1ee [<ffffffff813e897a>] nmi+0x1a/0x20 [<ffffffff813f08ab>] ? bad_to_user+0x25/0x771 <<EOE>> Cc: John Lumby <johnlumby@hotmail.com> Cc: Maynard Johnson <maynardj@us.ibm.com> Cc: <stable@kernel.org> # .37+ Signed-off-by: Robert Richter <robert.richter@amd.com>
Diffstat (limited to 'arch/x86/oprofile')
-rw-r--r--arch/x86/oprofile/backtrace.c56
1 files changed, 47 insertions, 9 deletions
diff --git a/arch/x86/oprofile/backtrace.c b/arch/x86/oprofile/backtrace.c
index a5b64ab4cd6e..32f78eb46744 100644
--- a/arch/x86/oprofile/backtrace.c
+++ b/arch/x86/oprofile/backtrace.c
@@ -11,10 +11,12 @@
11#include <linux/oprofile.h> 11#include <linux/oprofile.h>
12#include <linux/sched.h> 12#include <linux/sched.h>
13#include <linux/mm.h> 13#include <linux/mm.h>
14#include <linux/compat.h>
15#include <linux/highmem.h>
16
14#include <asm/ptrace.h> 17#include <asm/ptrace.h>
15#include <asm/uaccess.h> 18#include <asm/uaccess.h>
16#include <asm/stacktrace.h> 19#include <asm/stacktrace.h>
17#include <linux/compat.h>
18 20
19static int backtrace_stack(void *data, char *name) 21static int backtrace_stack(void *data, char *name)
20{ 22{
@@ -36,17 +38,53 @@ static struct stacktrace_ops backtrace_ops = {
36 .walk_stack = print_context_stack, 38 .walk_stack = print_context_stack,
37}; 39};
38 40
41/* from arch/x86/kernel/cpu/perf_event.c: */
42
43/*
44 * best effort, GUP based copy_from_user() that assumes IRQ or NMI context
45 */
46static unsigned long
47copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
48{
49 unsigned long offset, addr = (unsigned long)from;
50 unsigned long size, len = 0;
51 struct page *page;
52 void *map;
53 int ret;
54
55 do {
56 ret = __get_user_pages_fast(addr, 1, 0, &page);
57 if (!ret)
58 break;
59
60 offset = addr & (PAGE_SIZE - 1);
61 size = min(PAGE_SIZE - offset, n - len);
62
63 map = kmap_atomic(page);
64 memcpy(to, map+offset, size);
65 kunmap_atomic(map);
66 put_page(page);
67
68 len += size;
69 to += size;
70 addr += size;
71
72 } while (len < n);
73
74 return len;
75}
76
39#ifdef CONFIG_COMPAT 77#ifdef CONFIG_COMPAT
40static struct stack_frame_ia32 * 78static struct stack_frame_ia32 *
41dump_user_backtrace_32(struct stack_frame_ia32 *head) 79dump_user_backtrace_32(struct stack_frame_ia32 *head)
42{ 80{
81 /* Also check accessibility of one struct frame_head beyond: */
43 struct stack_frame_ia32 bufhead[2]; 82 struct stack_frame_ia32 bufhead[2];
44 struct stack_frame_ia32 *fp; 83 struct stack_frame_ia32 *fp;
84 unsigned long bytes;
45 85
46 /* Also check accessibility of one struct frame_head beyond */ 86 bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
47 if (!access_ok(VERIFY_READ, head, sizeof(bufhead))) 87 if (bytes != sizeof(bufhead))
48 return NULL;
49 if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead)))
50 return NULL; 88 return NULL;
51 89
52 fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame); 90 fp = (struct stack_frame_ia32 *) compat_ptr(bufhead[0].next_frame);
@@ -87,12 +125,12 @@ x86_backtrace_32(struct pt_regs * const regs, unsigned int depth)
87 125
88static struct stack_frame *dump_user_backtrace(struct stack_frame *head) 126static struct stack_frame *dump_user_backtrace(struct stack_frame *head)
89{ 127{
128 /* Also check accessibility of one struct frame_head beyond: */
90 struct stack_frame bufhead[2]; 129 struct stack_frame bufhead[2];
130 unsigned long bytes;
91 131
92 /* Also check accessibility of one struct stack_frame beyond */ 132 bytes = copy_from_user_nmi(bufhead, head, sizeof(bufhead));
93 if (!access_ok(VERIFY_READ, head, sizeof(bufhead))) 133 if (bytes != sizeof(bufhead))
94 return NULL;
95 if (__copy_from_user_inatomic(bufhead, head, sizeof(bufhead)))
96 return NULL; 134 return NULL;
97 135
98 oprofile_add_trace(bufhead[0].return_address); 136 oprofile_add_trace(bufhead[0].return_address);