[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230801111923.118268-2-przemyslaw.kitszel@intel.com>
Date: Tue, 1 Aug 2023 13:19:22 +0200
From: Przemek Kitszel <przemyslaw.kitszel@...el.com>
To: Kees Cook <keescook@...omium.org>
Cc: Jacob Keller <jacob.e.keller@...el.com>,
intel-wired-lan@...ts.osuosl.org,
netdev@...r.kernel.org,
Alexander Lobakin <aleksander.lobakin@...el.com>,
Przemek Kitszel <przemyslaw.kitszel@...el.com>
Subject: [RFC net-next 1/2] overflow: add DECLARE_FLEX() for on-stack allocs
Add DECLARE_FLEX() macro for on-stack allocations of structs with
flexible array member.
Using underlying array for on-stack storage lets us to declare known
on compile-time structures without kzalloc().
Actual usage for ice driver is in next patch of the series.
Note that "struct" kw and "*" char is moved to the caller, to both:
have shorter macro name, and have more natural type specification
in the driver code (IOW not hiding an actual type of var).
Signed-off-by: Przemek Kitszel <przemyslaw.kitszel@...el.com>
---
include/linux/overflow.h | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index f9b60313eaea..403b7ec120a2 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -309,4 +309,18 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
#define struct_size_t(type, member, count) \
struct_size((type *)NULL, member, count)
+/**
+ * DECLARE_FLEX() - Declare an on-stack instance of structure with trailing
+ * flexible array.
+ * @type: Pointer to structure type, including "struct" keyword and "*" char.
+ * @name: Name for a (pointer) variable to create.
+ * @member: Name of the array member.
+ * @count: Number of elements in the array; must be compile-time const.
+ *
+ * Declare an instance of structure *@...e with trailing flexible array.
+ */
+#define DECLARE_FLEX(type, name, member, count) \
+ u8 name##_buf[struct_size((type)NULL, member, count)] __aligned(8) = {};\
+ type name = (type)&name##_buf
+
#endif /* __LINUX_OVERFLOW_H */
--
2.38.1
Powered by blists - more mailing lists