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

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