Skip to content

Commit 9bab2bc

Browse files
dhowellsgregkh
authored andcommitted
fscache: Fix cookie key hashing
[ Upstream commit 35b7257 ] The current hash algorithm used for hashing cookie keys is really bad, producing almost no dispersion (after a test kernel build, ~30000 files were split over just 18 out of the 32768 hash buckets). Borrow the full_name_hash() hash function into fscache to do the hashing for cookie keys and, in the future, volume keys. I don't want to use full_name_hash() as-is because I want the hash value to be consistent across arches and over time as the hash value produced may get used on disk. I can also optimise parts of it away as the key will always be a padded array of aligned 32-bit words. Fixes: ec0328e ("fscache: Maintain a catalogue of allocated cookies") Signed-off-by: David Howells <dhowells@redhat.com> Reviewed-by: Jeff Layton <jlayton@redhat.com> cc: linux-cachefs@redhat.com Link: https://lore.kernel.org/r/162431201844.2908479.8293647220901514696.stgit@warthog.procyon.org.uk/ Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent b4849e2 commit 9bab2bc

3 files changed

Lines changed: 42 additions & 13 deletions

File tree

‎fs/fscache/cookie.c‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,8 @@ void fscache_free_cookie(struct fscache_cookie *cookie)
7474
static int fscache_set_key(struct fscache_cookie *cookie,
7575
const void *index_key, size_t index_key_len)
7676
{
77-
unsigned long long h;
7877
u32 *buf;
7978
int bufs;
80-
int i;
8179

8280
bufs = DIV_ROUND_UP(index_key_len, sizeof(*buf));
8381

@@ -91,17 +89,7 @@ static int fscache_set_key(struct fscache_cookie *cookie,
9189
}
9290

9391
memcpy(buf, index_key, index_key_len);
94-
95-
/* Calculate a hash and combine this with the length in the first word
96-
* or first half word
97-
*/
98-
h = (unsigned long)cookie->parent;
99-
h += index_key_len + cookie->type;
100-
101-
for (i = 0; i < bufs; i++)
102-
h += buf[i];
103-
104-
cookie->key_hash = h ^ (h >> 32);
92+
cookie->key_hash = fscache_hash(0, buf, bufs);
10593
return 0;
10694
}
10795

‎fs/fscache/internal.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ extern struct workqueue_struct *fscache_object_wq;
9797
extern struct workqueue_struct *fscache_op_wq;
9898
DECLARE_PER_CPU(wait_queue_head_t, fscache_object_cong_wait);
9999

100+
extern unsigned int fscache_hash(unsigned int salt, unsigned int *data, unsigned int n);
101+
100102
static inline bool fscache_object_congested(void)
101103
{
102104
return workqueue_congested(WORK_CPU_UNBOUND, fscache_object_wq);

‎fs/fscache/main.c‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,45 @@ static struct ctl_table fscache_sysctls_root[] = {
9393
};
9494
#endif
9595

96+
/*
97+
* Mixing scores (in bits) for (7,20):
98+
* Input delta: 1-bit 2-bit
99+
* 1 round: 330.3 9201.6
100+
* 2 rounds: 1246.4 25475.4
101+
* 3 rounds: 1907.1 31295.1
102+
* 4 rounds: 2042.3 31718.6
103+
* Perfect: 2048 31744
104+
* (32*64) (32*31/2 * 64)
105+
*/
106+
#define HASH_MIX(x, y, a) \
107+
( x ^= (a), \
108+
y ^= x, x = rol32(x, 7),\
109+
x += y, y = rol32(y,20),\
110+
y *= 9 )
111+
112+
static inline unsigned int fold_hash(unsigned long x, unsigned long y)
113+
{
114+
/* Use arch-optimized multiply if one exists */
115+
return __hash_32(y ^ __hash_32(x));
116+
}
117+
118+
/*
119+
* Generate a hash. This is derived from full_name_hash(), but we want to be
120+
* sure it is arch independent and that it doesn't change as bits of the
121+
* computed hash value might appear on disk. The caller also guarantees that
122+
* the hashed data will be a series of aligned 32-bit words.
123+
*/
124+
unsigned int fscache_hash(unsigned int salt, unsigned int *data, unsigned int n)
125+
{
126+
unsigned int a, x = 0, y = salt;
127+
128+
for (; n; n--) {
129+
a = *data++;
130+
HASH_MIX(x, y, a);
131+
}
132+
return fold_hash(x, y);
133+
}
134+
96135
/*
97136
* initialise the fs caching module
98137
*/

0 commit comments

Comments
 (0)