0016-bug-introduce-ASSERT_STRUCT_OFFSET.patch 1.9 KB

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