Skip to content

Commit b2f9f15

Browse files
qbooshAlexei Starovoitov
authored andcommitted
libbpf: Fix libbpf hashmap on (I)LP32 architectures
On ILP32, 64-bit result was shifted by value calculated for 32-bit long type and returned value was much outside hashmap capacity. As advised by Andrii Nakryiko, this patch uses different hashing variant for architectures with size_t shorter than long long. Fixes: e3b9242 ("libbpf: add resizable non-thread safe internal hashmap") Signed-off-by: Jakub Bogusz <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent f43cb0d commit b2f9f15

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

tools/lib/bpf/hashmap.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@
1111
#include <stdbool.h>
1212
#include <stddef.h>
1313
#include <limits.h>
14-
#ifndef __WORDSIZE
15-
#define __WORDSIZE (__SIZEOF_LONG__ * 8)
16-
#endif
1714

1815
static inline size_t hash_bits(size_t h, int bits)
1916
{
2017
/* shuffle bits and return requested number of upper bits */
21-
return (h * 11400714819323198485llu) >> (__WORDSIZE - bits);
18+
#if (__SIZEOF_SIZE_T__ == __SIZEOF_LONG_LONG__)
19+
/* LP64 case */
20+
return (h * 11400714819323198485llu) >> (__SIZEOF_LONG_LONG__ * 8 - bits);
21+
#elif (__SIZEOF_SIZE_T__ <= __SIZEOF_LONG__)
22+
return (h * 2654435769lu) >> (__SIZEOF_LONG__ * 8 - bits);
23+
#else
24+
# error "Unsupported size_t size"
25+
#endif
2226
}
2327

2428
typedef size_t (*hashmap_hash_fn)(const void *key, void *ctx);

0 commit comments

Comments
 (0)