anitazha / rpms / ndctl

Forked from rpms/ndctl a year ago
Clone

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

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