Skip to content

Commit a5c21dc

Browse files
wildea01torvalds
authored andcommitted
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>
1 parent 319720f commit a5c21dc

3 files changed

Lines changed: 4 additions & 7 deletions

File tree

‎fs/dcache.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char
192192
if (!tcount)
193193
return 0;
194194
}
195-
mask = ~(~0ul << tcount*8);
195+
mask = bytemask_from_count(tcount);
196196
return unlikely(!!((a ^ b) & mask));
197197
}
198198

‎fs/namei.c‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,11 +1598,6 @@ static inline int nested_symlink(struct path *path, struct nameidata *nd)
15981598
* do a "get_unaligned()" if this helps and is sufficiently
15991599
* fast.
16001600
*
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-
*
16061601
* - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
16071602
* do not trap on the (extremely unlikely) case of a page
16081603
* crossing operation.
@@ -1646,7 +1641,7 @@ unsigned int full_name_hash(const unsigned char *name, unsigned int len)
16461641
if (!len)
16471642
goto done;
16481643
}
1649-
mask = ~(~0ul << len*8);
1644+
mask = bytemask_from_count(len);
16501645
hash += mask & a;
16511646
done:
16521647
return fold_hash(hash);

‎include/linux/dcache.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ struct vfsmount;
2929
/* The hash is always the low bits of hash_len */
3030
#ifdef __LITTLE_ENDIAN
3131
#define HASH_LEN_DECLARE u32 hash; u32 len;
32+
#define bytemask_from_count(cnt) (~(~0ul << (cnt)*8))
3233
#else
3334
#define HASH_LEN_DECLARE u32 len; u32 hash;
35+
#define bytemask_from_count(cnt) (~(~0ul >> (cnt)*8))
3436
#endif
3537

3638
/*

0 commit comments

Comments
 (0)