android_kabi.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * android_kabi.h - Android kernel abi abstraction header
  4. *
  5. * Copyright (C) 2020 Google, Inc.
  6. *
  7. * Heavily influenced by rh_kabi.h which came from the RHEL/CENTOS kernel and
  8. * was:
  9. * Copyright (c) 2014 Don Zickus
  10. * Copyright (c) 2015-2018 Jiri Benc
  11. * Copyright (c) 2015 Sabrina Dubroca, Hannes Frederic Sowa
  12. * Copyright (c) 2016-2018 Prarit Bhargava
  13. * Copyright (c) 2017 Paolo Abeni, Larry Woodman
  14. *
  15. * These macros are to be used to try to help alleviate future kernel abi
  16. * changes that will occur as LTS and other kernel patches are merged into the
  17. * tree during a period in which the kernel abi is wishing to not be disturbed.
  18. *
  19. * There are two times these macros should be used:
  20. * - Before the kernel abi is "frozen"
  21. * Padding can be added to various kernel structures that have in the past
  22. * been known to change over time. That will give "room" in the structure
  23. * that can then be used when fields are added so that the structure size
  24. * will not change.
  25. *
  26. * - After the kernel abi is "frozen"
  27. * If a structure's field is changed to a type that is identical in size to
  28. * the previous type, it can be changed with a union macro
  29. * If a field is added to a structure, the padding fields can be used to add
  30. * the new field in a "safe" way.
  31. */
  32. #ifndef _ANDROID_KABI_H
  33. #define _ANDROID_KABI_H
  34. #include <linux/compiler.h>
  35. /*
  36. * Worker macros, don't use these, use the ones without a leading '_'
  37. */
  38. #define __ANDROID_KABI_CHECK_SIZE_ALIGN(_orig, _new) \
  39. union { \
  40. _Static_assert(sizeof(struct{_new;}) <= sizeof(struct{_orig;}), \
  41. __FILE__ ":" __stringify(__LINE__) ": " \
  42. __stringify(_new) \
  43. " is larger than " \
  44. __stringify(_orig) ); \
  45. _Static_assert(__alignof__(struct{_new;}) <= __alignof__(struct{_orig;}), \
  46. __FILE__ ":" __stringify(__LINE__) ": " \
  47. __stringify(_orig) \
  48. " is not aligned the same as " \
  49. __stringify(_new) ); \
  50. }
  51. #ifdef __GENKSYMS__
  52. #define _ANDROID_KABI_REPLACE(_orig, _new) _orig
  53. #else
  54. #define _ANDROID_KABI_REPLACE(_orig, _new) \
  55. union { \
  56. _new; \
  57. struct { \
  58. _orig; \
  59. }; \
  60. __ANDROID_KABI_CHECK_SIZE_ALIGN(_orig, _new); \
  61. }
  62. #endif /* __GENKSYMS__ */
  63. #define _ANDROID_KABI_RESERVE(n) u64 android_kabi_reserved##n
  64. /*
  65. * Macros to use _before_ the ABI is frozen
  66. */
  67. /*
  68. * ANDROID_KABI_RESERVE
  69. * Reserve some "padding" in a structure for potential future use.
  70. * This normally placed at the end of a structure.
  71. * number: the "number" of the padding variable in the structure. Start with
  72. * 1 and go up.
  73. */
  74. #ifdef CONFIG_ANDROID_KABI_RESERVE
  75. #define ANDROID_KABI_RESERVE(number) _ANDROID_KABI_RESERVE(number)
  76. #else
  77. #define ANDROID_KABI_RESERVE(number)
  78. #endif
  79. /*
  80. * Macros to use _after_ the ABI is frozen
  81. */
  82. /*
  83. * ANDROID_KABI_USE(number, _new)
  84. * Use a previous padding entry that was defined with ANDROID_KABI_RESERVE
  85. * number: the previous "number" of the padding variable
  86. * _new: the variable to use now instead of the padding variable
  87. */
  88. #define ANDROID_KABI_USE(number, _new) \
  89. _ANDROID_KABI_REPLACE(_ANDROID_KABI_RESERVE(number), _new)
  90. /*
  91. * ANDROID_KABI_USE2(number, _new1, _new2)
  92. * Use a previous padding entry that was defined with ANDROID_KABI_RESERVE for
  93. * two new variables that fit into 64 bits. This is good for when you do not
  94. * want to "burn" a 64bit padding variable for a smaller variable size if not
  95. * needed.
  96. */
  97. #define ANDROID_KABI_USE2(number, _new1, _new2) \
  98. _ANDROID_KABI_REPLACE(_ANDROID_KABI_RESERVE(number), struct{ _new1; _new2; })
  99. #endif /* _ANDROID_KABI_H */