diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2012-05-26 13:14:39 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-05-26 13:14:39 -0400 |
commit | 4ae73f2d53255c388d50bf83c1681112a6f9cba1 (patch) | |
tree | 0b0d9adce97c2270a427e1f1c19c977b8f066d5b /arch/x86/lib/usercopy.c | |
parent | da89fb165e5e51a2ec1ff8a0ff6bc052d1068184 (diff) |
x86: use generic strncpy_from_user routine
The generic strncpy_from_user() is not really optimal, since it is
designed to work on both little-endian and big-endian. And on
little-endian you can simplify much of the logic to find the first zero
byte, since little-endian arithmetic doesn't have to worry about the
carry bit propagating into earlier bytes (only later bytes, which we
don't care about).
But I have patches to make the generic routines use the architecture-
specific <asm/word-at-a-time.h> infrastructure, so that we can regain
the little-endian optimizations. But before we do that, switch over to
the generic routines to make the patches each do just one well-defined
thing.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'arch/x86/lib/usercopy.c')
-rw-r--r-- | arch/x86/lib/usercopy.c | 97 |
1 files changed, 0 insertions, 97 deletions
diff --git a/arch/x86/lib/usercopy.c b/arch/x86/lib/usercopy.c index 2e4e4b02c37a..f61ee67ec00f 100644 --- a/arch/x86/lib/usercopy.c +++ b/arch/x86/lib/usercopy.c | |||
@@ -43,100 +43,3 @@ copy_from_user_nmi(void *to, const void __user *from, unsigned long n) | |||
43 | return len; | 43 | return len; |
44 | } | 44 | } |
45 | EXPORT_SYMBOL_GPL(copy_from_user_nmi); | 45 | EXPORT_SYMBOL_GPL(copy_from_user_nmi); |
46 | |||
47 | /* | ||
48 | * Do a strncpy, return length of string without final '\0'. | ||
49 | * 'count' is the user-supplied count (return 'count' if we | ||
50 | * hit it), 'max' is the address space maximum (and we return | ||
51 | * -EFAULT if we hit it). | ||
52 | */ | ||
53 | static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, unsigned long max) | ||
54 | { | ||
55 | long res = 0; | ||
56 | |||
57 | /* | ||
58 | * Truncate 'max' to the user-specified limit, so that | ||
59 | * we only have one limit we need to check in the loop | ||
60 | */ | ||
61 | if (max > count) | ||
62 | max = count; | ||
63 | |||
64 | while (max >= sizeof(unsigned long)) { | ||
65 | unsigned long c, mask; | ||
66 | |||
67 | /* Fall back to byte-at-a-time if we get a page fault */ | ||
68 | if (unlikely(__get_user(c,(unsigned long __user *)(src+res)))) | ||
69 | break; | ||
70 | mask = has_zero(c); | ||
71 | if (mask) { | ||
72 | mask = (mask - 1) & ~mask; | ||
73 | mask >>= 7; | ||
74 | *(unsigned long *)(dst+res) = c & mask; | ||
75 | return res + count_masked_bytes(mask); | ||
76 | } | ||
77 | *(unsigned long *)(dst+res) = c; | ||
78 | res += sizeof(unsigned long); | ||
79 | max -= sizeof(unsigned long); | ||
80 | } | ||
81 | |||
82 | while (max) { | ||
83 | char c; | ||
84 | |||
85 | if (unlikely(__get_user(c,src+res))) | ||
86 | return -EFAULT; | ||
87 | dst[res] = c; | ||
88 | if (!c) | ||
89 | return res; | ||
90 | res++; | ||
91 | max--; | ||
92 | } | ||
93 | |||
94 | /* | ||
95 | * Uhhuh. We hit 'max'. But was that the user-specified maximum | ||
96 | * too? If so, that's ok - we got as much as the user asked for. | ||
97 | */ | ||
98 | if (res >= count) | ||
99 | return res; | ||
100 | |||
101 | /* | ||
102 | * Nope: we hit the address space limit, and we still had more | ||
103 | * characters the caller would have wanted. That's an EFAULT. | ||
104 | */ | ||
105 | return -EFAULT; | ||
106 | } | ||
107 | |||
108 | /** | ||
109 | * strncpy_from_user: - Copy a NUL terminated string from userspace. | ||
110 | * @dst: Destination address, in kernel space. This buffer must be at | ||
111 | * least @count bytes long. | ||
112 | * @src: Source address, in user space. | ||
113 | * @count: Maximum number of bytes to copy, including the trailing NUL. | ||
114 | * | ||
115 | * Copies a NUL-terminated string from userspace to kernel space. | ||
116 | * | ||
117 | * On success, returns the length of the string (not including the trailing | ||
118 | * NUL). | ||
119 | * | ||
120 | * If access to userspace fails, returns -EFAULT (some data may have been | ||
121 | * copied). | ||
122 | * | ||
123 | * If @count is smaller than the length of the string, copies @count bytes | ||
124 | * and returns @count. | ||
125 | */ | ||
126 | long | ||
127 | strncpy_from_user(char *dst, const char __user *src, long count) | ||
128 | { | ||
129 | unsigned long max_addr, src_addr; | ||
130 | |||
131 | if (unlikely(count <= 0)) | ||
132 | return 0; | ||
133 | |||
134 | max_addr = current_thread_info()->addr_limit.seg; | ||
135 | src_addr = (unsigned long)src; | ||
136 | if (likely(src_addr < max_addr)) { | ||
137 | unsigned long max = max_addr - src_addr; | ||
138 | return do_strncpy_from_user(dst, src, count, max); | ||
139 | } | ||
140 | return -EFAULT; | ||
141 | } | ||
142 | EXPORT_SYMBOL(strncpy_from_user); | ||