]>
Commit | Line | Data |
---|---|---|
83250735 TL |
1 | From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
2 | From: Maxim Levitsky <mlevitsk@redhat.com> | |
3 | Date: Wed, 3 Aug 2022 18:49:59 +0300 | |
4 | Subject: [PATCH] bug: introduce ASSERT_STRUCT_OFFSET | |
5 | ||
6 | ASSERT_STRUCT_OFFSET allows to assert during the build of | |
7 | the kernel that a field in a struct have an expected offset. | |
8 | ||
9 | KVM used to have such macro, but there is almost nothing KVM specific | |
10 | in it so move it to build_bug.h, so that it can be used in other | |
11 | places in KVM. | |
12 | ||
13 | Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com> | |
14 | Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com> | |
15 | --- | |
16 | arch/x86/kvm/vmx/vmcs12.h | 5 ++--- | |
17 | include/linux/build_bug.h | 9 +++++++++ | |
18 | 2 files changed, 11 insertions(+), 3 deletions(-) | |
19 | ||
20 | diff --git a/arch/x86/kvm/vmx/vmcs12.h b/arch/x86/kvm/vmx/vmcs12.h | |
3ae8384f | 21 | index 746129ddd5ae..01936013428b 100644 |
83250735 TL |
22 | --- a/arch/x86/kvm/vmx/vmcs12.h |
23 | +++ b/arch/x86/kvm/vmx/vmcs12.h | |
24 | @@ -208,9 +208,8 @@ struct __packed vmcs12 { | |
25 | /* | |
26 | * For save/restore compatibility, the vmcs12 field offsets must not change. | |
27 | */ | |
28 | -#define CHECK_OFFSET(field, loc) \ | |
29 | - BUILD_BUG_ON_MSG(offsetof(struct vmcs12, field) != (loc), \ | |
30 | - "Offset of " #field " in struct vmcs12 has changed.") | |
31 | +#define CHECK_OFFSET(field, loc) \ | |
32 | + ASSERT_STRUCT_OFFSET(struct vmcs12, field, loc) | |
33 | ||
34 | static inline void vmx_check_vmcs12_offsets(void) | |
35 | { | |
36 | diff --git a/include/linux/build_bug.h b/include/linux/build_bug.h | |
37 | index e3a0be2c90ad..3aa3640f8c18 100644 | |
38 | --- a/include/linux/build_bug.h | |
39 | +++ b/include/linux/build_bug.h | |
40 | @@ -77,4 +77,13 @@ | |
41 | #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr) | |
42 | #define __static_assert(expr, msg, ...) _Static_assert(expr, msg) | |
43 | ||
44 | + | |
45 | +/* | |
46 | + * Compile time check that field has an expected offset | |
47 | + */ | |
48 | +#define ASSERT_STRUCT_OFFSET(type, field, expected_offset) \ | |
49 | + BUILD_BUG_ON_MSG(offsetof(type, field) != (expected_offset), \ | |
50 | + "Offset of " #field " in " #type " has changed.") | |
51 | + | |
52 | + | |
53 | #endif /* _LINUX_BUILD_BUG_H */ |