bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf] libbpf: Fix libbpf hashmap on (I)LP32 architectures
@ 2020-07-09 22:57 Andrii Nakryiko
  2020-07-09 22:59 ` Andrii Nakryiko
  0 siblings, 1 reply; 2+ messages in thread
From: Andrii Nakryiko @ 2020-07-09 22:57 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel, qboosh
  Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko

From: Jakub Bogusz <qboosh@pld-linux.org>

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: e3b924224028 ("libbpf: add resizable non-thread safe internal hashmap")
Signed-off-by: Jakub Bogusz <qboosh@pld-linux.org>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/hashmap.h | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/tools/lib/bpf/hashmap.h b/tools/lib/bpf/hashmap.h
index df59fd4fc95b..e0af36b0e5d8 100644
--- a/tools/lib/bpf/hashmap.h
+++ b/tools/lib/bpf/hashmap.h
@@ -11,14 +11,18 @@
 #include <stdbool.h>
 #include <stddef.h>
 #include <limits.h>
-#ifndef __WORDSIZE
-#define __WORDSIZE (__SIZEOF_LONG__ * 8)
-#endif
 
 static inline size_t hash_bits(size_t h, int bits)
 {
 	/* shuffle bits and return requested number of upper bits */
-	return (h * 11400714819323198485llu) >> (__WORDSIZE - bits);
+#if (__SIZEOF_SIZE_T__ == __SIZEOF_LONG_LONG__)
+	/* LP64 case */
+	return (h * 11400714819323198485llu) >> (__SIZEOF_LONG_LONG__ * 8 - bits);
+#elif (__SIZEOF_SIZE_T__ <= __SIZEOF_LONG__)
+	return (h * 2654435769lu) >> (__SIZEOF_LONG__ * 8 - bits);
+#else
+#	error "Unsupported size_t size"
+#endif
 }
 
 typedef size_t (*hashmap_hash_fn)(const void *key, void *ctx);
-- 
2.24.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH bpf] libbpf: Fix libbpf hashmap on (I)LP32 architectures
  2020-07-09 22:57 [PATCH bpf] libbpf: Fix libbpf hashmap on (I)LP32 architectures Andrii Nakryiko
@ 2020-07-09 22:59 ` Andrii Nakryiko
  0 siblings, 0 replies; 2+ messages in thread
From: Andrii Nakryiko @ 2020-07-09 22:59 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, Networking, Alexei Starovoitov, Daniel Borkmann,
	Jakub Bogusz, Kernel Team

On Thu, Jul 9, 2020 at 3:57 PM Andrii Nakryiko <andriin@fb.com> wrote:
>
> From: Jakub Bogusz <qboosh@pld-linux.org>
>
> 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: e3b924224028 ("libbpf: add resizable non-thread safe internal hashmap")
> Signed-off-by: Jakub Bogusz <qboosh@pld-linux.org>
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>
> ---

This was supposed to have my Acked-by as well, I forgot to add it, sorry.

>  tools/lib/bpf/hashmap.h | 12 ++++++++----
>  1 file changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/tools/lib/bpf/hashmap.h b/tools/lib/bpf/hashmap.h
> index df59fd4fc95b..e0af36b0e5d8 100644
> --- a/tools/lib/bpf/hashmap.h
> +++ b/tools/lib/bpf/hashmap.h
> @@ -11,14 +11,18 @@
>  #include <stdbool.h>
>  #include <stddef.h>
>  #include <limits.h>
> -#ifndef __WORDSIZE
> -#define __WORDSIZE (__SIZEOF_LONG__ * 8)
> -#endif
>
>  static inline size_t hash_bits(size_t h, int bits)
>  {
>         /* shuffle bits and return requested number of upper bits */
> -       return (h * 11400714819323198485llu) >> (__WORDSIZE - bits);
> +#if (__SIZEOF_SIZE_T__ == __SIZEOF_LONG_LONG__)
> +       /* LP64 case */
> +       return (h * 11400714819323198485llu) >> (__SIZEOF_LONG_LONG__ * 8 - bits);
> +#elif (__SIZEOF_SIZE_T__ <= __SIZEOF_LONG__)
> +       return (h * 2654435769lu) >> (__SIZEOF_LONG__ * 8 - bits);
> +#else
> +#      error "Unsupported size_t size"
> +#endif
>  }
>
>  typedef size_t (*hashmap_hash_fn)(const void *key, void *ctx);
> --
> 2.24.1
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-07-09 22:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-09 22:57 [PATCH bpf] libbpf: Fix libbpf hashmap on (I)LP32 architectures Andrii Nakryiko
2020-07-09 22:59 ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).