Skip to content

Commit

Permalink
Murmurhash byteswapping fix v2 [VDO-5720]
Browse files Browse the repository at this point in the history
Per suggestion from Eric Biggers on dm-devel, don't use cpu_to_le64
and friends, use get_unaligned_le64 and friends. Call them directly
rather than via a wrapper function, since we don't need the
conditional mess. Use the same code for user mode, as well.

Signed-off-by: Matthew Sakai <[email protected]>
  • Loading branch information
raeburn authored and lorelei-sakai committed Mar 22, 2024
1 parent 1abf6fa commit 0da5304
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions drivers/md/dm-vdo/murmurhash3.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,14 @@

#include "murmurhash3.h"

#include <asm/byteorder.h>
#include <asm/unaligned.h>

static inline u64 rotl64(u64 x, s8 r)
{
return (x << r) | (x >> (64 - r));
}

#define ROTL64(x, y) rotl64(x, y)
static __always_inline u64 getblock64(const u64 *p, int i)
{
return le64_to_cpup(&p[i]);
}

static __always_inline void putblock64(u64 *p, int i, u64 value)
{
p[i] = cpu_to_le64(value);
}

/* Finalization mix - force all bits of a hash block to avalanche */

Expand All @@ -50,15 +41,17 @@ void murmurhash3_128(const void *key, const int len, const u32 seed, void *out)
const u64 c1 = 0x87c37b91114253d5LLU;
const u64 c2 = 0x4cf5ad432745937fLLU;

u64 *hash_out = out;

/* body */

const u64 *blocks = (const u64 *)(data);

int i;

for (i = 0; i < nblocks; i++) {
u64 k1 = getblock64(blocks, i * 2 + 0);
u64 k2 = getblock64(blocks, i * 2 + 1);
u64 k1 = get_unaligned_le64(&blocks[i * 2]);
u64 k2 = get_unaligned_le64(&blocks[i * 2 + 1]);

k1 *= c1;
k1 = ROTL64(k1, 31);
Expand Down Expand Up @@ -160,6 +153,6 @@ void murmurhash3_128(const void *key, const int len, const u32 seed, void *out)
h1 += h2;
h2 += h1;

putblock64((u64 *)out, 0, h1);
putblock64((u64 *)out, 1, h2);
put_unaligned_le64(h1, &hash_out[0]);
put_unaligned_le64(h2, &hash_out[1]);
}

0 comments on commit 0da5304

Please sign in to comment.