Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

bug: introduce ASSERT_STRUCT_OFFSET

ASSERT_STRUCT_OFFSET allows to assert during the build of
the kernel that a field in a struct have an expected offset.

KVM used to have such macro, but there is almost nothing KVM specific
in it so move it to build_bug.h, so that it can be used in other
places in KVM.

Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
Message-Id: <20221025124741.228045-10-mlevitsk@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Maxim Levitsky and committed by
Paolo Bonzini
07a368b3 00009406

+11 -3
+2 -3
arch/x86/kvm/vmx/vmcs12.h
··· 208 208 /* 209 209 * For save/restore compatibility, the vmcs12 field offsets must not change. 210 210 */ 211 - #define CHECK_OFFSET(field, loc) \ 212 - BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \ 213 - "Offset of " #field " in struct vmcs12 has changed.") 211 + #define CHECK_OFFSET(field, loc) \ 212 + ASSERT_STRUCT_OFFSET(struct vmcs12, field, loc) 214 213 215 214 static inline void vmx_check_vmcs12_offsets(void) 216 215 {
+9
include/linux/build_bug.h
··· 77 77 #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr) 78 78 #define __static_assert(expr, msg, ...) _Static_assert(expr, msg) 79 79 80 + 81 + /* 82 + * Compile time check that field has an expected offset 83 + */ 84 + #define ASSERT_STRUCT_OFFSET(type, field, expected_offset) \ 85 + BUILD_BUG_ON_MSG(offsetof(type, field) != (expected_offset), \ 86 + "Offset of " #field " in " #type " has changed.") 87 + 88 + 80 89 #endif /* _LINUX_BUILD_BUG_H */