diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2012-05-26 13:43:17 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-05-26 14:33:40 -0400 |
commit | 36126f8f2ed8168eb13aa0662b9b9585cba100a9 (patch) | |
tree | 543f6b6ab60dd3e47af931142aa84f0ba7749d43 /lib/strncpy_from_user.c | |
parent | 4ae73f2d53255c388d50bf83c1681112a6f9cba1 (diff) |
word-at-a-time: make the interfaces truly generic
This changes the interfaces in <asm/word-at-a-time.h> to be a bit more
complicated, but a lot more generic.
In particular, it allows us to really do the operations efficiently on
both little-endian and big-endian machines, pretty much regardless of
machine details. For example, if you can rely on a fast population
count instruction on your architecture, this will allow you to make your
optimized <asm/word-at-a-time.h> file with that.
NOTE! The "generic" version in include/asm-generic/word-at-a-time.h is
not truly generic, it actually only works on big-endian. Why? Because
on little-endian the generic algorithms are wasteful, since you can
inevitably do better. The x86 implementation is an example of that.
(The only truly non-generic part of the asm-generic implementation is
the "find_zero()" function, and you could make a little-endian version
of it. And if the Kbuild infrastructure allowed us to pick a particular
header file, that would be lovely)
The <asm/word-at-a-time.h> functions are as follows:
- WORD_AT_A_TIME_CONSTANTS: specific constants that the algorithm
uses.
- has_zero(): take a word, and determine if it has a zero byte in it.
It gets the word, the pointer to the constant pool, and a pointer to
an intermediate "data" field it can set.
This is the "quick-and-dirty" zero tester: it's what is run inside
the hot loops.
- "prep_zero_mask()": take the word, the data that has_zero() produced,
and the constant pool, and generate an *exact* mask of which byte had
the first zero. This is run directly *outside* the loop, and allows
the "has_zero()" function to answer the "is there a zero byte"
question without necessarily getting exactly *which* byte is the
first one to contain a zero.
If you do multiple byte lookups concurrently (eg "hash_name()", which
looks for both NUL and '/' bytes), after you've done the prep_zero_mask()
phase, the result of those can be or'ed together to get the "either
or" case.
- The result from "prep_zero_mask()" can then be fed into "find_zero()"
(to find the byte offset of the first byte that was zero) or into
"zero_bytemask()" (to find the bytemask of the bytes preceding the
zero byte).
The existence of zero_bytemask() is optional, and is not necessary
for the normal string routines. But dentry name hashing needs it, so
if you enable DENTRY_WORD_AT_A_TIME you need to expose it.
This changes the generic strncpy_from_user() function and the dentry
hashing functions to use these modified word-at-a-time interfaces. This
gets us back to the optimized state of the x86 strncpy that we lost in
the previous commit when moving over to the generic version.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'lib/strncpy_from_user.c')
-rw-r--r-- | lib/strncpy_from_user.c | 47 |
1 files changed, 7 insertions, 40 deletions
diff --git a/lib/strncpy_from_user.c b/lib/strncpy_from_user.c index c4c09b0e96ba..bb2b201d6ad0 100644 --- a/lib/strncpy_from_user.c +++ b/lib/strncpy_from_user.c | |||
@@ -4,37 +4,7 @@ | |||
4 | #include <linux/errno.h> | 4 | #include <linux/errno.h> |
5 | 5 | ||
6 | #include <asm/byteorder.h> | 6 | #include <asm/byteorder.h> |
7 | 7 | #include <asm/word-at-a-time.h> | |
8 | static inline long find_zero(unsigned long mask) | ||
9 | { | ||
10 | long byte = 0; | ||
11 | |||
12 | #ifdef __BIG_ENDIAN | ||
13 | #ifdef CONFIG_64BIT | ||
14 | if (mask >> 32) | ||
15 | mask >>= 32; | ||
16 | else | ||
17 | byte = 4; | ||
18 | #endif | ||
19 | if (mask >> 16) | ||
20 | mask >>= 16; | ||
21 | else | ||
22 | byte += 2; | ||
23 | return (mask >> 8) ? byte : byte + 1; | ||
24 | #else | ||
25 | #ifdef CONFIG_64BIT | ||
26 | if (!((unsigned int) mask)) { | ||
27 | mask >>= 32; | ||
28 | byte = 4; | ||
29 | } | ||
30 | #endif | ||
31 | if (!(mask & 0xffff)) { | ||
32 | mask >>= 16; | ||
33 | byte += 2; | ||
34 | } | ||
35 | return (mask & 0xff) ? byte : byte + 1; | ||
36 | #endif | ||
37 | } | ||
38 | 8 | ||
39 | #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS | 9 | #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
40 | #define IS_UNALIGNED(src, dst) 0 | 10 | #define IS_UNALIGNED(src, dst) 0 |
@@ -51,8 +21,7 @@ static inline long find_zero(unsigned long mask) | |||
51 | */ | 21 | */ |
52 | static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, unsigned long max) | 22 | static inline long do_strncpy_from_user(char *dst, const char __user *src, long count, unsigned long max) |
53 | { | 23 | { |
54 | const unsigned long high_bits = REPEAT_BYTE(0xfe) + 1; | 24 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
55 | const unsigned long low_bits = REPEAT_BYTE(0x7f); | ||
56 | long res = 0; | 25 | long res = 0; |
57 | 26 | ||
58 | /* | 27 | /* |
@@ -66,18 +35,16 @@ static inline long do_strncpy_from_user(char *dst, const char __user *src, long | |||
66 | goto byte_at_a_time; | 35 | goto byte_at_a_time; |
67 | 36 | ||
68 | while (max >= sizeof(unsigned long)) { | 37 | while (max >= sizeof(unsigned long)) { |
69 | unsigned long c, v, rhs; | 38 | unsigned long c, data; |
70 | 39 | ||
71 | /* Fall back to byte-at-a-time if we get a page fault */ | 40 | /* Fall back to byte-at-a-time if we get a page fault */ |
72 | if (unlikely(__get_user(c,(unsigned long __user *)(src+res)))) | 41 | if (unlikely(__get_user(c,(unsigned long __user *)(src+res)))) |
73 | break; | 42 | break; |
74 | rhs = c | low_bits; | ||
75 | v = (c + high_bits) & ~rhs; | ||
76 | *(unsigned long *)(dst+res) = c; | 43 | *(unsigned long *)(dst+res) = c; |
77 | if (v) { | 44 | if (has_zero(c, &data, &constants)) { |
78 | v = (c & low_bits) + low_bits; | 45 | data = prep_zero_mask(c, data, &constants); |
79 | v = ~(v | rhs); | 46 | data = create_zero_mask(data); |
80 | return res + find_zero(v); | 47 | return res + find_zero(data); |
81 | } | 48 | } |
82 | res += sizeof(unsigned long); | 49 | res += sizeof(unsigned long); |
83 | max -= sizeof(unsigned long); | 50 | max -= sizeof(unsigned long); |