diff mbox series

[net-next,v2,1/7] overflow: add DEFINE_FLEX() for on-stack allocs

Message ID 20230811120814.169952-2-przemyslaw.kitszel@intel.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series introduce DEFINE_FLEX() macro | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 15915 this patch: 14386
netdev/cc_maintainers warning 3 maintainers not CCed: peterz@infradead.org samitolvanen@google.com mark.rutland@arm.com
netdev/build_clang success Errors and warnings before: 4781 this patch: 4781
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn fail Errors and warnings before: 16955 this patch: 15229
netdev/checkpatch warning CHECK: Macro argument 'name' may be better as '(name)' to avoid precedence issues WARNING: line length of 81 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Przemek Kitszel Aug. 11, 2023, 12:08 p.m. UTC
Add DEFINE_FLEX() macro for on-stack allocations of structs with
flexible array member.

Expose __struct_size() macro outside of fortify-string.h, as it could be
used to read size of structs allocated by DEFINE_FLEX().
Move __member_size() alongside it.
-Kees

Using underlying array for on-stack storage lets us to declare
known-at-compile-time structures without kzalloc().

Actual usage for ice driver is in following patches of the series.

Co-developed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Przemek Kitszel <przemyslaw.kitszel@intel.com>
---
v2: Kees: reuse __struct_size() instead of adding new macro
    (adding Kees as Co-dev here)
v1: change macro name; add macro for size read;
    accept struct type instead of ptr to it; change alignment;
---
 include/linux/compiler_types.h | 12 ++++++++++++
 include/linux/fortify-string.h |  4 ----
 include/linux/overflow.h       | 27 +++++++++++++++++++++++++++
 3 files changed, 39 insertions(+), 4 deletions(-)

Comments

kernel test robot Aug. 11, 2023, 2:10 p.m. UTC | #1
Hi Przemek,

kernel test robot noticed the following build errors:

[auto build test ERROR on 6a1ed1430daa2ccf8ac457e0db93fb0925b801ca]

url:    https://github.com/intel-lab-lkp/linux/commits/Przemek-Kitszel/overflow-add-DEFINE_FLEX-for-on-stack-allocs/20230811-201509
base:   6a1ed1430daa2ccf8ac457e0db93fb0925b801ca
patch link:    https://lore.kernel.org/r/20230811120814.169952-2-przemyslaw.kitszel%40intel.com
patch subject: [PATCH net-next v2 1/7] overflow: add DEFINE_FLEX() for on-stack allocs
config: m68k-randconfig-r024-20230811 (https://download.01.org/0day-ci/archive/20230811/202308112122.OuF0YZqL-lkp@intel.com/config)
compiler: m68k-linux-gcc (GCC) 12.3.0
reproduce: (https://download.01.org/0day-ci/archive/20230811/202308112122.OuF0YZqL-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202308112122.OuF0YZqL-lkp@intel.com/

All error/warnings (new ones prefixed by >>):

   In file included from include/linux/compiler.h:5,
                    from include/linux/export.h:5,
                    from include/asm-generic/export.h:9,
                    from ./arch/m68k/include/generated/asm/export.h:1,
                    from arch/m68k/lib/mulsi3.S:35:
>> include/linux/compiler_types.h:331:5: warning: "__has_builtin" is not defined, evaluates to 0 [-Wundef]
     331 | #if __has_builtin(__builtin_dynamic_object_size)
         |     ^~~~~~~~~~~~~
>> include/linux/compiler_types.h:331:18: error: missing binary operator before token "("
     331 | #if __has_builtin(__builtin_dynamic_object_size)
         |                  ^


vim +331 include/linux/compiler_types.h

   326	
   327	/*
   328	 * When the size of an allocated object is needed, use the best available
   329	 * mechanism to find it. (For cases where sizeof() cannot be used.)
   330	 */
 > 331	#if __has_builtin(__builtin_dynamic_object_size)
   332	#define __struct_size(p)	__builtin_dynamic_object_size(p, 0)
   333	#define __member_size(p)	__builtin_dynamic_object_size(p, 1)
   334	#else
   335	#define __struct_size(p)	__builtin_object_size(p, 0)
   336	#define __member_size(p)	__builtin_object_size(p, 1)
   337	#endif
   338
diff mbox series

Patch

diff --git a/include/linux/compiler_types.h b/include/linux/compiler_types.h
index 547ea1ff806e..d880c1180de2 100644
--- a/include/linux/compiler_types.h
+++ b/include/linux/compiler_types.h
@@ -324,6 +324,18 @@  struct ftrace_likely_data {
 # define __realloc_size(x, ...)
 #endif
 
+/*
+ * When the size of an allocated object is needed, use the best available
+ * mechanism to find it. (For cases where sizeof() cannot be used.)
+ */
+#if __has_builtin(__builtin_dynamic_object_size)
+#define __struct_size(p)	__builtin_dynamic_object_size(p, 0)
+#define __member_size(p)	__builtin_dynamic_object_size(p, 1)
+#else
+#define __struct_size(p)	__builtin_object_size(p, 0)
+#define __member_size(p)	__builtin_object_size(p, 1)
+#endif
+
 #ifndef asm_volatile_goto
 #define asm_volatile_goto(x...) asm goto(x)
 #endif
diff --git a/include/linux/fortify-string.h b/include/linux/fortify-string.h
index da51a83b2829..1e7711185ec6 100644
--- a/include/linux/fortify-string.h
+++ b/include/linux/fortify-string.h
@@ -93,13 +93,9 @@  extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size)
 #if __has_builtin(__builtin_dynamic_object_size)
 #define POS			__pass_dynamic_object_size(1)
 #define POS0			__pass_dynamic_object_size(0)
-#define __struct_size(p)	__builtin_dynamic_object_size(p, 0)
-#define __member_size(p)	__builtin_dynamic_object_size(p, 1)
 #else
 #define POS			__pass_object_size(1)
 #define POS0			__pass_object_size(0)
-#define __struct_size(p)	__builtin_object_size(p, 0)
-#define __member_size(p)	__builtin_object_size(p, 1)
 #endif
 
 #define __compiletime_lessthan(bounds, length)	(	\
diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index f9b60313eaea..21a4410799eb 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -309,4 +309,31 @@  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)
 
+/**
+ * DEFINE_FLEX() - Define a zeroed, on-stack, instance of @type structure with
+ * a trailing flexible array member.
+ *
+ * @type: structure type name, including "struct" keyword.
+ * @name: Name for a variable to define.
+ * @member: Name of the array member.
+ * @count: Number of elements in the array; must be compile-time const.
+ */
+#define DEFINE_FLEX(type, name, member, count)					\
+	union {									\
+		u8 bytes[struct_size_t(type, member, count)];			\
+		type obj;							\
+	} name##_u __aligned(_Alignof(type)) = {};				\
+	type *name = (type *)&name##_u
+
+/**
+ * const_flex_size() - Get size of on-stack instance of structure with
+ * a trailing flexible array member.
+ *
+ * @name: Name of the variable, the one defined by DEFINE_FLEX() macro above.
+ *
+ * Get size of @name, which is equivalent to struct_size(name, array, count),
+ * but does not require (repeating) last two arguments.
+ */
+#define const_flex_size(name)	__builtin_object_size(name, 1)
+
 #endif /* __LINUX_OVERFLOW_H */