All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jakub Bogusz <qboosh@pld-linux.org>
To: bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>
Subject: [PATCH] fix libbpf hashmap with size_t shorter than long long
Date: Sun, 21 Jun 2020 16:25:59 +0200	[thread overview]
Message-ID: <20200621142559.GA25517@stranger.qboosh.pl> (raw)

[-- Attachment #1: Type: text/plain, Size: 296 bytes --]

Hello,

I noticed that _bpftool crashes when building kernel tools (5.7.x) for
32-bit targets because in libbpf hashmap implementation hash_bits()
function returning numbers exceeding hashmap buckets capacity.

Attached patch fixes this problem.


Regards,

-- 
Jakub Bogusz    http://qboosh.pl/

[-- Attachment #2: kernel-tools-bpf-hashmap.patch --]
[-- Type: text/plain, Size: 914 bytes --]

Ensure that hash_bits returns value fits in given bits (for bits > 0):
multiplier is long long (which is the same or wider than size_t), so shift bits
must be based on long long size, not __WORDSIZE.

Signed-off-by: Jakub Bogusz <qboosh@pld-linux.org>

--- linux-5.7/tools/lib/bpf/hashmap.h.orig	2020-06-01 01:49:15.000000000 +0200
+++ linux-5.7/tools/lib/bpf/hashmap.h	2020-06-21 15:22:07.298466419 +0200
@@ -10,17 +10,12 @@
 
 #include <stdbool.h>
 #include <stddef.h>
-#ifdef __GLIBC__
-#include <bits/wordsize.h>
-#else
-#include <bits/reg.h>
-#endif
 #include "libbpf_internal.h"
 
 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);
+	return (h * 11400714819323198485llu) >> (__SIZEOF_LONG_LONG__ * 8 - bits);
 }
 
 typedef size_t (*hashmap_hash_fn)(const void *key, void *ctx);

             reply	other threads:[~2020-06-21 14:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-21 14:25 Jakub Bogusz [this message]
2020-06-23  5:44 ` [PATCH] fix libbpf hashmap with size_t shorter than long long Andrii Nakryiko
2020-06-23 19:29   ` Jakub Bogusz
2020-06-23 19:40     ` Andrii Nakryiko
2020-06-27  9:07       ` Jakub Bogusz
2020-06-27 20:25         ` Andrii Nakryiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200621142559.GA25517@stranger.qboosh.pl \
    --to=qboosh@pld-linux.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.