Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

dcache: allow word-at-a-time name hashing with big-endian CPUs

When explicitly hashing the end of a string with the word-at-a-time
interface, we have to be careful which end of the word we pick up.

On big-endian CPUs, the upper-bits will contain the data we're after, so
ensure we generate our masks accordingly (and avoid hashing whatever
random junk may have been sitting after the string).

This patch adds a new dcache helper, bytemask_from_count, which creates
a mask appropriate for the CPU endianness.

Cc: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Will Deacon and committed by
Linus Torvalds
a5c21dce 319720f5

+4 -7
+1 -1
fs/dcache.c
··· 192 192 if (!tcount) 193 193 return 0; 194 194 } 195 - mask = ~(~0ul << tcount*8); 195 + mask = bytemask_from_count(tcount); 196 196 return unlikely(!!((a ^ b) & mask)); 197 197 } 198 198
+1 -6
fs/namei.c
··· 1598 1598 * do a "get_unaligned()" if this helps and is sufficiently 1599 1599 * fast. 1600 1600 * 1601 - * - Little-endian machines (so that we can generate the mask 1602 - * of low bytes efficiently). Again, we *could* do a byte 1603 - * swapping load on big-endian architectures if that is not 1604 - * expensive enough to make the optimization worthless. 1605 - * 1606 1601 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1607 1602 * do not trap on the (extremely unlikely) case of a page 1608 1603 * crossing operation. ··· 1641 1646 if (!len) 1642 1647 goto done; 1643 1648 } 1644 - mask = ~(~0ul << len*8); 1649 + mask = bytemask_from_count(len); 1645 1650 hash += mask & a; 1646 1651 done: 1647 1652 return fold_hash(hash);
+2
include/linux/dcache.h
··· 29 29 /* The hash is always the low bits of hash_len */ 30 30 #ifdef __LITTLE_ENDIAN 31 31 #define HASH_LEN_DECLARE u32 hash; u32 len; 32 + #define bytemask_from_count(cnt) (~(~0ul << (cnt)*8)) 32 33 #else 33 34 #define HASH_LEN_DECLARE u32 len; u32 hash; 35 + #define bytemask_from_count(cnt) (~(~0ul >> (cnt)*8)) 34 36 #endif 35 37 36 38 /*