1f73373
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
Jeremy Cline d1b6f8c
From: Jiri Benc <jbenc@redhat.com>
Jeremy Cline d1b6f8c
Date: Wed, 19 Feb 2020 11:52:19 +0100
Jeremy Cline d1b6f8c
Subject: [PATCH] redhat: rh_kabi: introduce RH_KABI_EXTEND_WITH_SIZE
Jeremy Cline d1b6f8c
MIME-Version: 1.0
Jeremy Cline d1b6f8c
Content-Type: text/plain; charset=UTF-8
Jeremy Cline d1b6f8c
Content-Transfer-Encoding: 8bit
Jeremy Cline d1b6f8c
Jeremy Cline d1b6f8c
RH-Acked-by: Toke Høiland-Jørgensen <toke@redhat.com>
Jeremy Cline d1b6f8c
RH-Acked-by: Hangbin Liu <haliu@redhat.com>
Jeremy Cline d1b6f8c
Jeremy Cline d1b6f8c
Upstream status: RHEL only
Jeremy Cline d1b6f8c
Jeremy Cline d1b6f8c
This allows addition of a struct into struct while reserving extra space.
Jeremy Cline d1b6f8c
See the documentation in rh_kabi.h for details.
Jeremy Cline d1b6f8c
Jeremy Cline d1b6f8c
The check for size is automatically disabled in -debug kernels that have
Jeremy Cline d1b6f8c
many fields larger than the production kernels (mutexes etc.) and that are
Jeremy Cline d1b6f8c
not under kABI guarantee.
Jeremy Cline d1b6f8c
Jeremy Cline d1b6f8c
I'm also moving a misplaced comment for RH_KABI_FILL_HOLE.
Jeremy Cline d1b6f8c
Jeremy Cline d1b6f8c
(Pointed out by Sabrina:) We need to force align the added field to 8 byte
Jeremy Cline d1b6f8c
offset. Otherwise, if the required alignment of the added field changed
Jeremy Cline d1b6f8c
later (e.g. it was a structure with only a single field that was changed
Jeremy Cline d1b6f8c
from int to char) and the previous field was smaller than long, it could
Jeremy Cline d1b6f8c
lead to the whole union shifting position and offsets of the following
Jeremy Cline d1b6f8c
fields could change. It's also safer to align the size, too.
Jeremy Cline d1b6f8c
Jeremy Cline d1b6f8c
Let the 'size' parameter specify number of longs to be added and not number
Jeremy Cline d1b6f8c
of bytes. Note we assume a 64 bit architecture, which is the case for RHEL.
Jeremy Cline d1b6f8c
Jeremy Cline d1b6f8c
Upstream Status: RHEL only
Jeremy Cline d1b6f8c
Signed-off-by: Jiri Benc <jbenc@redhat.com>
Jeremy Cline d1b6f8c
---
Jeremy Cline d1b6f8c
 include/linux/rh_kabi.h | 29 ++++++++++++++++++++++++++++-
Jeremy Cline d1b6f8c
 1 file changed, 28 insertions(+), 1 deletion(-)
Jeremy Cline d1b6f8c
Jeremy Cline d1b6f8c
diff --git a/include/linux/rh_kabi.h b/include/linux/rh_kabi.h
Jeremy Cline d1b6f8c
index cdc636d3013d..4debb7aaad48 100644
Jeremy Cline d1b6f8c
--- a/include/linux/rh_kabi.h
Jeremy Cline d1b6f8c
+++ b/include/linux/rh_kabi.h
Jeremy Cline d1b6f8c
@@ -45,11 +45,24 @@
Jeremy Cline d1b6f8c
  * RH_KABI_EXTEND
Jeremy Cline d1b6f8c
  *   Simple macro for adding a new element to a struct.
Jeremy Cline d1b6f8c
  *
Jeremy Cline d1b6f8c
- *   Warning: only use if a hole exists for _all_ arches.  Use pahole to verify.
Jeremy Cline d1b6f8c
+ * RH_KABI_EXTEND_WITH_SIZE
Jeremy Cline d1b6f8c
+ *   Adds a new element (usually a struct) to a struct and reserves extra
Jeremy Cline d1b6f8c
+ *   space for the new element.  The provided 'size' is the total space to
Jeremy Cline d1b6f8c
+ *   be added in longs (i.e. it's 8 * 'size' bytes), including the size of
Jeremy Cline d1b6f8c
+ *   the added element.  It is automatically checked that the new element
Jeremy Cline d1b6f8c
+ *   does not overflow the reserved space, now nor in the future. However,
Jeremy Cline d1b6f8c
+ *   no attempt is done to check the content of the added element (struct)
Jeremy Cline d1b6f8c
+ *   for kABI conformance - kABI checking inside the added element is
Jeremy Cline d1b6f8c
+ *   effectively switched off.
Jeremy Cline d1b6f8c
+ *   For any struct being added by RH_KABI_EXTEND_WITH_SIZE, it is
Jeremy Cline d1b6f8c
+ *   recommended its content to be documented as not covered by kABI
Jeremy Cline d1b6f8c
+ *   guarantee.
Jeremy Cline d1b6f8c
  *
Jeremy Cline d1b6f8c
  * RH_KABI_FILL_HOLE
Jeremy Cline d1b6f8c
  *   Simple macro for filling a hole in a struct.
Jeremy Cline d1b6f8c
  *
Jeremy Cline d1b6f8c
+ *   Warning: only use if a hole exists for _all_ arches.  Use pahole to verify.
Jeremy Cline d1b6f8c
+ *
Jeremy Cline d1b6f8c
  * RH_KABI_RENAME
Jeremy Cline d1b6f8c
  *   Simple macro for renaming an element without changing its type.  This
Jeremy Cline d1b6f8c
  *   macro can be used in bitfields, for example.
Jeremy Cline d1b6f8c
@@ -133,8 +146,12 @@
Jeremy Cline d1b6f8c
 		_Static_assert(__alignof__(struct{_new;}) <= __alignof__(struct{_orig;}), \
Jeremy Cline d1b6f8c
 			       __FILE__ ":" __stringify(__LINE__) ": "  __stringify(_orig) " is not aligned the same as " __stringify(_new) RH_KABI_ALIGN_WARNING); \
Jeremy Cline d1b6f8c
 	}
Jeremy Cline d1b6f8c
+# define __RH_KABI_CHECK_SIZE(_item, _size)				\
Jeremy Cline d1b6f8c
+	_Static_assert(sizeof(struct{_item;}) <= _size,			\
Jeremy Cline d1b6f8c
+		       __FILE__ ":" __stringify(__LINE__) ": " __stringify(_item) " is larger than the reserved size (" __stringify(_size) " bytes)" RH_KABI_ALIGN_WARNING)
Jeremy Cline d1b6f8c
 #else
Jeremy Cline d1b6f8c
 # define __RH_KABI_CHECK_SIZE_ALIGN(_orig, _new)
Jeremy Cline d1b6f8c
+# define __RH_KABI_CHECK_SIZE(_item, _size)
Jeremy Cline d1b6f8c
 #endif
6c2cc50
Jeremy Cline d1b6f8c
 # define _RH_KABI_DEPRECATE(_type, _orig)	_type rh_reserved_##_orig
Jeremy Cline d1b6f8c
@@ -186,6 +203,16 @@
6c2cc50
Jeremy Cline d1b6f8c
 #define RH_KABI_EXCLUDE(_elem)		_RH_KABI_EXCLUDE(_elem);
6c2cc50
Jeremy Cline d1b6f8c
+/*
Jeremy Cline d1b6f8c
+ * Extending a struct while reserving extra space.
Jeremy Cline d1b6f8c
+ */
Jeremy Cline d1b6f8c
+#define RH_KABI_EXTEND_WITH_SIZE(_new, _size)				\
Jeremy Cline d1b6f8c
+	RH_KABI_EXTEND(union {						\
Jeremy Cline d1b6f8c
+		_new;							\
Jeremy Cline d1b6f8c
+		unsigned long __UNIQUE_ID(rh_kabi_reserved)[_size];	\
Jeremy Cline d1b6f8c
+		__RH_KABI_CHECK_SIZE(_new, 8 * (_size));		\
Jeremy Cline d1b6f8c
+	})
Jeremy Cline d1b6f8c
+
Jeremy Cline d1b6f8c
 /*
Jeremy Cline d1b6f8c
  * RHEL macros to extend structs.
Jeremy Cline d1b6f8c
  *
Jeremy Cline d1b6f8c
-- 
6c2cc50
2.26.2
Jeremy Cline d1b6f8c