1111#include <linux/oprofile.h>
1212#include <linux/sched.h>
1313#include <linux/mm.h>
14+ #include <linux/compat.h>
15+ #include <linux/highmem.h>
16+
1417#include <asm/ptrace.h>
1518#include <asm/uaccess.h>
1619#include <asm/stacktrace.h>
17- #include <linux/compat.h>
1820
1921static int backtrace_stack (void * data , char * name )
2022{
@@ -36,17 +38,53 @@ static struct stacktrace_ops backtrace_ops = {
3638 .walk_stack = print_context_stack ,
3739};
3840
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+ */
46+ static unsigned long
47+ copy_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+
3977#ifdef CONFIG_COMPAT
4078static struct stack_frame_ia32 *
4179dump_user_backtrace_32 (struct stack_frame_ia32 * head )
4280{
81+ /* Also check accessibility of one struct frame_head beyond: */
4382 struct stack_frame_ia32 bufhead [2 ];
4483 struct stack_frame_ia32 * fp ;
84+ unsigned long bytes ;
4585
46- /* Also check accessibility of one struct frame_head beyond */
47- if (!access_ok (VERIFY_READ , head , sizeof (bufhead )))
48- return NULL ;
49- if (__copy_from_user_inatomic (bufhead , head , sizeof (bufhead )))
86+ bytes = copy_from_user_nmi (bufhead , head , sizeof (bufhead ));
87+ if (bytes != sizeof (bufhead ))
5088 return NULL ;
5189
5290 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)
87125
88126static struct stack_frame * dump_user_backtrace (struct stack_frame * head )
89127{
128+ /* Also check accessibility of one struct frame_head beyond: */
90129 struct stack_frame bufhead [2 ];
130+ unsigned long bytes ;
91131
92- /* Also check accessibility of one struct stack_frame beyond */
93- if (!access_ok (VERIFY_READ , head , sizeof (bufhead )))
94- return NULL ;
95- if (__copy_from_user_inatomic (bufhead , head , sizeof (bufhead )))
132+ bytes = copy_from_user_nmi (bufhead , head , sizeof (bufhead ));
133+ if (bytes != sizeof (bufhead ))
96134 return NULL ;
97135
98136 oprofile_add_trace (bufhead [0 ].return_address );
0 commit comments