Blame SOURCES/0034-util-add-the-struct_size-helper-from-the-kernel.patch

2eb93d
From 7aa7c7be6e803de267a165237e23577ab496e792 Mon Sep 17 00:00:00 2001
2eb93d
From: Vishal Verma <vishal.l.verma@intel.com>
2eb93d
Date: Thu, 7 Oct 2021 02:21:26 -0600
2eb93d
Subject: [PATCH 034/217] util: add the struct_size() helper from the kernel
2eb93d
2eb93d
Add struct_size() from include/linux/overflow.h which calculates the
2eb93d
size of a struct with a trailing variable length array.
2eb93d
2eb93d
Suggested-by: Dan Williams <dan.j.williams@intel.com>
2eb93d
Reviewed-by: Dan Williams <dan.j.williams@intel.com>
2eb93d
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
2eb93d
---
2eb93d
 util/size.h | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++
2eb93d
 util/util.h |  6 ++++++
2eb93d
 2 files changed, 68 insertions(+)
2eb93d
2eb93d
diff --git a/util/size.h b/util/size.h
2eb93d
index 646edae..a0f3593 100644
2eb93d
--- a/util/size.h
2eb93d
+++ b/util/size.h
2eb93d
@@ -4,6 +4,8 @@
2eb93d
 #ifndef _NDCTL_SIZE_H_
2eb93d
 #define _NDCTL_SIZE_H_
2eb93d
 #include <stdbool.h>
2eb93d
+#include <stdint.h>
2eb93d
+#include <util/util.h>
2eb93d
 
2eb93d
 #define SZ_1K     0x00000400
2eb93d
 #define SZ_4K     0x00001000
2eb93d
@@ -30,4 +32,64 @@ static inline bool is_power_of_2(unsigned long long v)
2eb93d
 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
2eb93d
 #define HPAGE_SIZE (2 << 20)
2eb93d
 
2eb93d
+/*
2eb93d
+ * Helpers for struct_size() copied from include/linux/overflow.h (GPL-2.0)
2eb93d
+ *
2eb93d
+ * For simplicity and code hygiene, the fallback code below insists on
2eb93d
+ * a, b and *d having the same type (similar to the min() and max()
2eb93d
+ * macros), whereas gcc's type-generic overflow checkers accept
2eb93d
+ * different types. Hence we don't just make check_add_overflow an
2eb93d
+ * alias for __builtin_add_overflow, but add type checks similar to
2eb93d
+ * below.
2eb93d
+ */
2eb93d
+#define check_add_overflow(a, b, d) (({	\
2eb93d
+	typeof(a) __a = (a);			\
2eb93d
+	typeof(b) __b = (b);			\
2eb93d
+	typeof(d) __d = (d);			\
2eb93d
+	(void) (&__a == &__b);			\
2eb93d
+	(void) (&__a == __d);			\
2eb93d
+	__builtin_add_overflow(__a, __b, __d);	\
2eb93d
+}))
2eb93d
+
2eb93d
+#define check_mul_overflow(a, b, d) (({	\
2eb93d
+	typeof(a) __a = (a);			\
2eb93d
+	typeof(b) __b = (b);			\
2eb93d
+	typeof(d) __d = (d);			\
2eb93d
+	(void) (&__a == &__b);			\
2eb93d
+	(void) (&__a == __d);			\
2eb93d
+	__builtin_mul_overflow(__a, __b, __d);	\
2eb93d
+}))
2eb93d
+
2eb93d
+/*
2eb93d
+ * Compute a*b+c, returning SIZE_MAX on overflow. Internal helper for
2eb93d
+ * struct_size() below.
2eb93d
+ */
2eb93d
+static inline size_t __ab_c_size(size_t a, size_t b, size_t c)
2eb93d
+{
2eb93d
+	size_t bytes;
2eb93d
+
2eb93d
+	if (check_mul_overflow(a, b, &bytes))
2eb93d
+		return SIZE_MAX;
2eb93d
+	if (check_add_overflow(bytes, c, &bytes))
2eb93d
+		return SIZE_MAX;
2eb93d
+
2eb93d
+	return bytes;
2eb93d
+}
2eb93d
+
2eb93d
+/**
2eb93d
+ * struct_size() - Calculate size of structure with trailing array.
2eb93d
+ * @p: Pointer to the structure.
2eb93d
+ * @member: Name of the array member.
2eb93d
+ * @count: Number of elements in the array.
2eb93d
+ *
2eb93d
+ * Calculates size of memory needed for structure @p followed by an
2eb93d
+ * array of @count number of @member elements.
2eb93d
+ *
2eb93d
+ * Return: number of bytes needed or SIZE_MAX on overflow.
2eb93d
+ */
2eb93d
+#define struct_size(p, member, count)					\
2eb93d
+	__ab_c_size(count,						\
2eb93d
+		    sizeof(*(p)->member) + __must_be_array((p)->member),\
2eb93d
+		    sizeof(*(p)))
2eb93d
+
2eb93d
 #endif /* _NDCTL_SIZE_H_ */
2eb93d
diff --git a/util/util.h b/util/util.h
2eb93d
index ae0e4e1..b2b4ae6 100644
2eb93d
--- a/util/util.h
2eb93d
+++ b/util/util.h
2eb93d
@@ -63,6 +63,12 @@
2eb93d
 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
2eb93d
 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
2eb93d
 
2eb93d
+/* Are two types/vars the same type (ignoring qualifiers)? */
2eb93d
+#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
2eb93d
+
2eb93d
+/* &a[0] degrades to a pointer: a different type from an array */
2eb93d
+#define __must_be_array(a)	BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
2eb93d
+
2eb93d
 enum {
2eb93d
 	READ, WRITE,
2eb93d
 };
2eb93d
-- 
2eb93d
2.27.0
2eb93d