]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
libbpf: remove linux/unaligned.h dependency for libbpf_sha256()
authorAndrii Nakryiko <andrii@kernel.org>
Wed, 1 Oct 2025 17:13:26 +0000 (10:13 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 1 Oct 2025 22:27:25 +0000 (15:27 -0700)
linux/unaligned.h include dependency is causing issues for libbpf's
Github mirror due to {get,put}_unaligned_be32() usage.

So get rid of it by implementing custom variants of those macros that
will work both in kernel and Github mirror repos.

Also switch round_up() to roundup(), as the former is not available in
Github mirror (and is just a subtly more specific variant of roundup()
anyways).

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20251001171326.3883055-6-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
tools/lib/bpf/libbpf_utils.c

index f8290a0b3aaf2d74c98aaed3df4010fce8de3e7e..2bae8cafc0774a794c21316766014b5d10894fe2 100644 (file)
@@ -13,7 +13,6 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <linux/kernel.h>
-#include <linux/unaligned.h>
 
 #include "libbpf.h"
 #include "libbpf_internal.h"
@@ -149,6 +148,16 @@ const char *libbpf_errstr(int err)
        }
 }
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wpacked"
+struct __packed_u32 { __u32 __val; } __attribute__((packed));
+#pragma GCC diagnostic pop
+
+#define get_unaligned_be32(p) be32_to_cpu((((struct __packed_u32 *)(p))->__val))
+#define put_unaligned_be32(v, p) do {                                                  \
+       ((struct __packed_u32 *)(p))->__val = cpu_to_be32(v);                           \
+} while (0)
+
 #define SHA256_BLOCK_LENGTH 64
 #define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
 #define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
@@ -232,7 +241,7 @@ void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH])
 
        memcpy(final_data, data + len - final_len, final_len);
        final_data[final_len] = 0x80;
-       final_len = round_up(final_len + 9, SHA256_BLOCK_LENGTH);
+       final_len = roundup(final_len + 9, SHA256_BLOCK_LENGTH);
        memcpy(&final_data[final_len - 8], &bitcount, 8);
 
        sha256_blocks(state, final_data, final_len / SHA256_BLOCK_LENGTH);