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 35 #include <linux/compiler.h> 36 #include <linux/stringify.h> 37 38 /* 39 * Worker macros, don't use these, use the ones without a leading '_' 40 */ 41 42 #define __ANDROID_KABI_CHECK_SIZE_ALIGN(_orig, _new) \ 43 union { \ 44 _Static_assert(sizeof(struct{_new;}) <= sizeof(struct{_orig;}), \ 45 __FILE__ ":" __stringify(__LINE__) ": " \ 46 __stringify(_new) \ 47 " is larger than " \ 48 __stringify(_orig) ); \ 49 _Static_assert(__alignof__(struct{_new;}) <= __alignof__(struct{_orig;}), \ 50 __FILE__ ":" __stringify(__LINE__) ": " \ 51 __stringify(_orig) \ 52 " is not aligned the same as " \ 53 __stringify(_new) ); \ 54 } 55 56 #ifdef __GENKSYMS__ 57 58 #define _ANDROID_KABI_REPLACE(_orig, _new) _orig 59 60 #else 61 62 #define _ANDROID_KABI_REPLACE(_orig, _new) \ 63 union { \ 64 _new; \ 65 struct { \ 66 _orig; \ 67 }; \ 68 __ANDROID_KABI_CHECK_SIZE_ALIGN(_orig, _new); \ 69 } 70 71 #endif /* __GENKSYMS__ */ 72 73 #define _ANDROID_KABI_RESERVE(n) u64 android_kabi_reserved##n 74 75 76 /* 77 * Macros to use _before_ the ABI is frozen 78 */ 79 80 /* 81 * ANDROID_KABI_RESERVE 82 * Reserve some "padding" in a structure for potential future use. 83 * This normally placed at the end of a structure. 84 * number: the "number" of the padding variable in the structure. Start with 85 * 1 and go up. 86 */ 87 #ifdef CONFIG_ANDROID_KABI_RESERVE 88 #define ANDROID_KABI_RESERVE(number) _ANDROID_KABI_RESERVE(number) 89 #else 90 #define ANDROID_KABI_RESERVE(number) 91 #endif 92 93 94 /* 95 * Macros to use _after_ the ABI is frozen 96 */ 97 98 /* 99 * ANDROID_KABI_USE(number, _new) 100 * Use a previous padding entry that was defined with ANDROID_KABI_RESERVE 101 * number: the previous "number" of the padding variable 102 * _new: the variable to use now instead of the padding variable 103 */ 104 #define ANDROID_KABI_USE(number, _new) \ 105 _ANDROID_KABI_REPLACE(_ANDROID_KABI_RESERVE(number), _new) 106 107 /* 108 * ANDROID_KABI_USE2(number, _new1, _new2) 109 * Use a previous padding entry that was defined with ANDROID_KABI_RESERVE for 110 * two new variables that fit into 64 bits. This is good for when you do not 111 * want to "burn" a 64bit padding variable for a smaller variable size if not 112 * needed. 113 */ 114 #define ANDROID_KABI_USE2(number, _new1, _new2) \ 115 _ANDROID_KABI_REPLACE(_ANDROID_KABI_RESERVE(number), struct{ _new1; _new2; }) 116 117 118 #endif /* _ANDROID_KABI_H */ 119