diff options
author | David S. Miller <davem@davemloft.net> | 2012-05-23 22:20:20 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2012-05-23 22:20:20 -0400 |
commit | 4efcac3a244de86593a82ca4ed945e839eb4c5af (patch) | |
tree | 7456cbb6d71b3f92385d94cab546e2b93dc19c6a /arch/sparc | |
parent | ff06dffbc8abfc60d6a0332f058f1d1bb01abb31 (diff) |
sparc: Optimize strncpy_from_user() zero byte search.
Compute a mask that will only have 0x80 in the bytes which
had a zero in them. The formula is:
~(((x & 0x7f7f7f7f) + 0x7f7f7f7f) | x | 0x7f7f7f7f)
In the inner word iteration, we have to compute the "x | 0x7f7f7f7f"
part, so we can reuse that in the above calculation.
Once we have this mask, we perform divide and conquer to find the
highest 0x80 location.
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'arch/sparc')
-rw-r--r-- | arch/sparc/lib/usercopy.c | 50 |
1 files changed, 21 insertions, 29 deletions
diff --git a/arch/sparc/lib/usercopy.c b/arch/sparc/lib/usercopy.c index 851cb75ce189..87f96453403a 100644 --- a/arch/sparc/lib/usercopy.c +++ b/arch/sparc/lib/usercopy.c | |||
@@ -11,35 +11,20 @@ EXPORT_SYMBOL(copy_from_user_overflow); | |||
11 | 11 | ||
12 | #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) | 12 | #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) |
13 | 13 | ||
14 | /* Return the high bit set in the first byte that is a zero */ | 14 | static inline long find_zero(unsigned long mask) |
15 | static inline unsigned long has_zero(unsigned long a) | ||
16 | { | ||
17 | return ((a - REPEAT_BYTE(0x01)) & ~a) & REPEAT_BYTE(0x80); | ||
18 | } | ||
19 | |||
20 | static inline long find_zero(unsigned long c) | ||
21 | { | 15 | { |
16 | long byte = 0; | ||
22 | #ifdef CONFIG_64BIT | 17 | #ifdef CONFIG_64BIT |
23 | if (!(c & 0xff00000000000000UL)) | 18 | if (mask >> 32) |
24 | return 0; | 19 | mask >>= 32; |
25 | if (!(c & 0x00ff000000000000UL)) | 20 | else |
26 | return 1; | 21 | byte = 4; |
27 | if (!(c & 0x0000ff0000000000UL)) | ||
28 | return 2; | ||
29 | if (!(c & 0x000000ff00000000UL)) | ||
30 | return 3; | ||
31 | #define __OFF 4 | ||
32 | #else | ||
33 | #define __OFF 0 | ||
34 | #endif | 22 | #endif |
35 | if (!(c & 0xff000000)) | 23 | if (mask >> 16) |
36 | return __OFF + 0; | 24 | mask >>= 16; |
37 | if (!(c & 0x00ff0000)) | 25 | else |
38 | return __OFF + 1; | 26 | byte += 2; |
39 | if (!(c & 0x0000ff00)) | 27 | return (mask >> 8) ? byte : byte + 1; |
40 | return __OFF + 2; | ||
41 | return __OFF + 3; | ||
42 | #undef __OFF | ||
43 | } | 28 | } |
44 | 29 | ||
45 | /* | 30 | /* |
@@ -50,6 +35,8 @@ static inline long find_zero(unsigned long c) | |||
50 | */ | 35 | */ |
51 | static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, unsigned long max) | 36 | static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, unsigned long max) |
52 | { | 37 | { |
38 | const unsigned long high_bits = REPEAT_BYTE(0xfe) + 1; | ||
39 | const unsigned long low_bits = REPEAT_BYTE(0x7f); | ||
53 | long res = 0; | 40 | long res = 0; |
54 | 41 | ||
55 | /* | 42 | /* |
@@ -63,14 +50,19 @@ static inline long do_strncpy_from_user(char *dst, const char __user *src, long | |||
63 | goto byte_at_a_time; | 50 | goto byte_at_a_time; |
64 | 51 | ||
65 | while (max >= sizeof(unsigned long)) { | 52 | while (max >= sizeof(unsigned long)) { |
66 | unsigned long c; | 53 | unsigned long c, v, rhs; |
67 | 54 | ||
68 | /* Fall back to byte-at-a-time if we get a page fault */ | 55 | /* Fall back to byte-at-a-time if we get a page fault */ |
69 | if (unlikely(__get_user(c,(unsigned long __user *)(src+res)))) | 56 | if (unlikely(__get_user(c,(unsigned long __user *)(src+res)))) |
70 | break; | 57 | break; |
58 | rhs = c | low_bits; | ||
59 | v = (c + high_bits) & ~rhs; | ||
71 | *(unsigned long *)(dst+res) = c; | 60 | *(unsigned long *)(dst+res) = c; |
72 | if (has_zero(c)) | 61 | if (v) { |
73 | return res + find_zero(c); | 62 | v = (c & low_bits) + low_bits;; |
63 | v = ~(v | rhs); | ||
64 | return res + find_zero(v); | ||
65 | } | ||
74 | res += sizeof(unsigned long); | 66 | res += sizeof(unsigned long); |
75 | max -= sizeof(unsigned long); | 67 | max -= sizeof(unsigned long); |
76 | } | 68 | } |