Skip to content

Commit 1ac2e6c

Browse files
Robert RichterIngo Molnar
authored andcommitted
x86, perf: Make copy_from_user_nmi() a library function
copy_from_user_nmi() is used in oprofile and perf. Moving it to other library functions like copy_from_user(). As this is x86 code for 32 and 64 bits, create a new file usercopy.c for unified code. Signed-off-by: Robert Richter <robert.richter@amd.com> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: http://lkml.kernel.org/r/20110607172413.GJ20052@erda.amd.com Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 9985c20 commit 1ac2e6c

5 files changed

Lines changed: 48 additions & 74 deletions

File tree

‎arch/x86/include/asm/uaccess.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,9 @@ struct __large_struct { unsigned long buf[100]; };
555555

556556
#endif /* CONFIG_X86_WP_WORKS_OK */
557557

558+
extern unsigned long
559+
copy_from_user_nmi(void *to, const void __user *from, unsigned long n);
560+
558561
/*
559562
* movsl can be slow when source and dest are not both 8-byte aligned
560563
*/

‎arch/x86/kernel/cpu/perf_event.c‎

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <linux/sched.h>
2323
#include <linux/uaccess.h>
2424
#include <linux/slab.h>
25-
#include <linux/highmem.h>
2625
#include <linux/cpu.h>
2726
#include <linux/bitops.h>
2827

@@ -67,40 +66,6 @@ enum extra_reg_type {
6766
EXTRA_REG_MAX /* number of entries needed */
6867
};
6968

70-
/*
71-
* best effort, GUP based copy_from_user() that assumes IRQ or NMI context
72-
*/
73-
static unsigned long
74-
copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
75-
{
76-
unsigned long offset, addr = (unsigned long)from;
77-
unsigned long size, len = 0;
78-
struct page *page;
79-
void *map;
80-
int ret;
81-
82-
do {
83-
ret = __get_user_pages_fast(addr, 1, 0, &page);
84-
if (!ret)
85-
break;
86-
87-
offset = addr & (PAGE_SIZE - 1);
88-
size = min(PAGE_SIZE - offset, n - len);
89-
90-
map = kmap_atomic(page);
91-
memcpy(to, map+offset, size);
92-
kunmap_atomic(map);
93-
put_page(page);
94-
95-
len += size;
96-
to += size;
97-
addr += size;
98-
99-
} while (len < n);
100-
101-
return len;
102-
}
103-
10469
struct event_constraint {
10570
union {
10671
unsigned long idxmsk[BITS_TO_LONGS(X86_PMC_IDX_MAX)];

‎arch/x86/lib/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ obj-$(CONFIG_SMP) += msr-smp.o cache-smp.o
1818

1919
lib-y := delay.o
2020
lib-y += thunk_$(BITS).o
21-
lib-y += usercopy_$(BITS).o getuser.o putuser.o
21+
lib-y += usercopy_$(BITS).o usercopy.o getuser.o putuser.o
2222
lib-y += memcpy_$(BITS).o
2323
lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o
2424

‎arch/x86/lib/usercopy.c‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* User address space access functions.
3+
*
4+
* For licencing details see kernel-base/COPYING
5+
*/
6+
7+
#include <linux/highmem.h>
8+
#include <linux/module.h>
9+
10+
/*
11+
* best effort, GUP based copy_from_user() that is NMI-safe
12+
*/
13+
unsigned long
14+
copy_from_user_nmi(void *to, const void __user *from, unsigned long n)
15+
{
16+
unsigned long offset, addr = (unsigned long)from;
17+
unsigned long size, len = 0;
18+
struct page *page;
19+
void *map;
20+
int ret;
21+
22+
do {
23+
ret = __get_user_pages_fast(addr, 1, 0, &page);
24+
if (!ret)
25+
break;
26+
27+
offset = addr & (PAGE_SIZE - 1);
28+
size = min(PAGE_SIZE - offset, n - len);
29+
30+
map = kmap_atomic(page);
31+
memcpy(to, map+offset, size);
32+
kunmap_atomic(map);
33+
put_page(page);
34+
35+
len += size;
36+
to += size;
37+
addr += size;
38+
39+
} while (len < n);
40+
41+
return len;
42+
}
43+
EXPORT_SYMBOL_GPL(copy_from_user_nmi);

‎arch/x86/oprofile/backtrace.c‎

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
#include <linux/sched.h>
1313
#include <linux/mm.h>
1414
#include <linux/compat.h>
15-
#include <linux/highmem.h>
15+
#include <linux/uaccess.h>
1616

1717
#include <asm/ptrace.h>
18-
#include <asm/uaccess.h>
1918
#include <asm/stacktrace.h>
2019

2120
static int backtrace_stack(void *data, char *name)
@@ -38,42 +37,6 @@ static struct stacktrace_ops backtrace_ops = {
3837
.walk_stack = print_context_stack,
3938
};
4039

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-
7740
#ifdef CONFIG_COMPAT
7841
static struct stack_frame_ia32 *
7942
dump_user_backtrace_32(struct stack_frame_ia32 *head)

0 commit comments

Comments
 (0)