Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
7816e21
Documentation
arch
alpha
arm
boot
common
configs
include
kernel
lib
Makefile
ashldi3.S
ashrdi3.S
backtrace.S
bitops.h
changebit.S
clear_user.S
clearbit.S
copy_from_user.S
copy_page.S
copy_template.S
copy_to_user.S
csumipv6.S
csumpartial.S
csumpartialcopy.S
csumpartialcopygeneric.S
csumpartialcopyuser.S
delay.S
div64.S
ecard.S
findbit.S
floppydma.S
getuser.S
io-acorn.S
io-readsb.S
io-readsl.S
io-readsw-armv3.S
io-readsw-armv4.S
io-shark.c
io-writesb.S
io-writesl.S
io-writesw-armv3.S
io-writesw-armv4.S
lib1funcs.S
lshrdi3.S
memchr.S
memcpy.S
memmove.S
memset.S
memzero.S
muldi3.S
putuser.S
setbit.S
strchr.S
strncpy_from_user.S
strnlen_user.S
strrchr.S
testchangebit.S
testclearbit.S
testsetbit.S
uaccess.S
uaccess_with_memcpy.c
ucmpdi2.S
mach-at91
mach-bcmring
mach-clps711x
mach-cns3xxx
mach-davinci
mach-dove
mach-ebsa110
mach-ep93xx
mach-exynos4
mach-footbridge
mach-gemini
mach-h720x
mach-imx
mach-integrator
mach-iop13xx
mach-iop32x
mach-iop33x
mach-ixp2000
mach-ixp23xx
mach-ixp4xx
mach-kirkwood
mach-ks8695
mach-l7200
mach-lpc32xx
mach-mmp
mach-msm
mach-mv78xx0
mach-mx5
mach-mxs
mach-netx
mach-nomadik
mach-nuc93x
mach-omap1
mach-omap2
mach-orion5x
mach-pnx4008
mach-prima2
mach-pxa
mach-realview
mach-rpc
mach-s3c2400
mach-s3c2410
mach-s3c2412
mach-s3c2416
mach-s3c2440
mach-s3c2443
mach-s3c64xx
mach-s5p64x0
mach-s5pc100
mach-s5pv210
mach-sa1100
mach-shark
mach-shmobile
mach-spear3xx
mach-spear6xx
mach-tcc8k
mach-tegra
mach-u300
mach-ux500
mach-versatile
mach-vexpress
mach-vt8500
mach-w90x900
mach-zynq
mm
nwfpe
oprofile
plat-iop
plat-mxc
plat-nomadik
plat-omap
plat-orion
plat-pxa
plat-s3c24xx
plat-s5p
plat-samsung
plat-spear
plat-tcc
plat-versatile
tools
vfp
Kconfig
Kconfig-nommu
Kconfig.debug
Makefile
avr32
blackfin
cris
frv
h8300
ia64
m32r
m68k
microblaze
mips
mn10300
openrisc
parisc
powerpc
s390
score
sh
sparc
tile
um
unicore32
x86
xtensa
.gitignore
Kconfig
block
crypto
drivers
firmware
fs
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
tools
usr
virt
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
REPORTING-BUGS
Breadcrumbs
linux
/
arch
/
arm
/
lib
/
uaccess_with_memcpy.c
Blame
Blame
Latest commit
History
History
235 lines (197 loc) · 5.3 KB
Breadcrumbs
linux
/
arch
/
arm
/
lib
/
uaccess_with_memcpy.c
Top
File metadata and controls
Code
Blame
235 lines (197 loc) · 5.3 KB
Raw
/* * linux/arch/arm/lib/uaccess_with_memcpy.c * * Written by: Lennert Buytenhek and Nicolas Pitre * Copyright (C) 2009 Marvell Semiconductor * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ #include <linux/kernel.h> #include <linux/ctype.h> #include <linux/uaccess.h> #include <linux/rwsem.h> #include <linux/mm.h> #include <linux/sched.h> #include <linux/hardirq.h> /* for in_atomic() */ #include <linux/gfp.h> #include <linux/highmem.h> #include <asm/current.h> #include <asm/page.h> static int pin_page_for_write(const void __user *_addr, pte_t **ptep, spinlock_t **ptlp) { unsigned long addr = (unsigned long)_addr; pgd_t *pgd; pmd_t *pmd; pte_t *pte; pud_t *pud; spinlock_t *ptl; pgd = pgd_offset(current->mm, addr); if (unlikely(pgd_none(*pgd) || pgd_bad(*pgd))) return 0; pud = pud_offset(pgd, addr); if (unlikely(pud_none(*pud) || pud_bad(*pud))) return 0; pmd = pmd_offset(pud, addr); if (unlikely(pmd_none(*pmd) || pmd_bad(*pmd))) return 0; pte = pte_offset_map_lock(current->mm, pmd, addr, &ptl); if (unlikely(!pte_present(*pte) || !pte_young(*pte) || !pte_write(*pte) || !pte_dirty(*pte))) { pte_unmap_unlock(pte, ptl); return 0; } *ptep = pte; *ptlp = ptl; return 1; } static unsigned long noinline __copy_to_user_memcpy(void __user *to, const void *from, unsigned long n) { int atomic; if (unlikely(segment_eq(get_fs(), KERNEL_DS))) { memcpy((void *)to, from, n); return 0; } /* the mmap semaphore is taken only if not in an atomic context */ atomic = in_atomic(); if (!atomic) down_read(¤t->mm->mmap_sem); while (n) { pte_t *pte; spinlock_t *ptl; int tocopy; while (!pin_page_for_write(to, &pte, &ptl)) { if (!atomic) up_read(¤t->mm->mmap_sem); if (__put_user(0, (char __user *)to)) goto out; if (!atomic) down_read(¤t->mm->mmap_sem); } tocopy = (~(unsigned long)to & ~PAGE_MASK) + 1; if (tocopy > n) tocopy = n; memcpy((void *)to, from, tocopy); to += tocopy; from += tocopy; n -= tocopy; pte_unmap_unlock(pte, ptl); } if (!atomic) up_read(¤t->mm->mmap_sem); out: return n; } unsigned long __copy_to_user(void __user *to, const void *from, unsigned long n) { /* * This test is stubbed out of the main function above to keep * the overhead for small copies low by avoiding a large * register dump on the stack just to reload them right away. * With frame pointer disabled, tail call optimization kicks in * as well making this test almost invisible. */ if (n < 64) return __copy_to_user_std(to, from, n); return __copy_to_user_memcpy(to, from, n); } static unsigned long noinline __clear_user_memset(void __user *addr, unsigned long n) { if (unlikely(segment_eq(get_fs(), KERNEL_DS))) { memset((void *)addr, 0, n); return 0; } down_read(¤t->mm->mmap_sem); while (n) { pte_t *pte; spinlock_t *ptl; int tocopy; while (!pin_page_for_write(addr, &pte, &ptl)) { up_read(¤t->mm->mmap_sem); if (__put_user(0, (char __user *)addr)) goto out; down_read(¤t->mm->mmap_sem); } tocopy = (~(unsigned long)addr & ~PAGE_MASK) + 1; if (tocopy > n) tocopy = n; memset((void *)addr, 0, tocopy); addr += tocopy; n -= tocopy; pte_unmap_unlock(pte, ptl); } up_read(¤t->mm->mmap_sem); out: return n; } unsigned long __clear_user(void __user *addr, unsigned long n) { /* See rational for this in __copy_to_user() above. */ if (n < 64) return __clear_user_std(addr, n); return __clear_user_memset(addr, n); } #if 0 /* * This code is disabled by default, but kept around in case the chosen * thresholds need to be revalidated. Some overhead (small but still) * would be implied by a runtime determined variable threshold, and * so far the measurement on concerned targets didn't show a worthwhile * variation. * * Note that a fairly precise sched_clock() implementation is needed * for results to make some sense. */ #include <linux/vmalloc.h> static int __init test_size_treshold(void) { struct page *src_page, *dst_page; void *user_ptr, *kernel_ptr; unsigned long long t0, t1, t2; int size, ret; ret = -ENOMEM; src_page = alloc_page(GFP_KERNEL); if (!src_page) goto no_src; dst_page = alloc_page(GFP_KERNEL); if (!dst_page) goto no_dst; kernel_ptr = page_address(src_page); user_ptr = vmap(&dst_page, 1, VM_IOREMAP, __pgprot(__P010)); if (!user_ptr) goto no_vmap; /* warm up the src page dcache */ ret = __copy_to_user_memcpy(user_ptr, kernel_ptr, PAGE_SIZE); for (size = PAGE_SIZE; size >= 4; size /= 2) { t0 = sched_clock(); ret |= __copy_to_user_memcpy(user_ptr, kernel_ptr, size); t1 = sched_clock(); ret |= __copy_to_user_std(user_ptr, kernel_ptr, size); t2 = sched_clock(); printk("copy_to_user: %d %llu %llu\n", size, t1 - t0, t2 - t1); } for (size = PAGE_SIZE; size >= 4; size /= 2) { t0 = sched_clock(); ret |= __clear_user_memset(user_ptr, size); t1 = sched_clock(); ret |= __clear_user_std(user_ptr, size); t2 = sched_clock(); printk("clear_user: %d %llu %llu\n", size, t1 - t0, t2 - t1); } if (ret) ret = -EFAULT; vunmap(user_ptr); no_vmap: put_page(dst_page); no_dst: put_page(src_page); no_src: return ret; } subsys_initcall(test_size_treshold); #endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
You can’t perform that action at this time.