From: Alexander Lobakin <aleksander.lobakin@intel.com>
To: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Cc: netdev@vger.kernel.org, intel-wired-lan@lists.osuosl.org,
Kees Cook <keescook@chromium.org>
Subject: Re: [Intel-wired-lan] [RFC net-next 1/2] overflow: add DECLARE_FLEX() for on-stack allocs
Date: Tue, 1 Aug 2023 15:54:29 +0200 [thread overview]
Message-ID: <d4d5324c-8d4b-b2e1-78c8-5c3015b5c03d@intel.com> (raw)
In-Reply-To: <20230801111923.118268-2-przemyslaw.kitszel@intel.com>
From: Przemek Kitszel <przemyslaw.kitszel@intel.com>
Date: Tue, 1 Aug 2023 13:19:22 +0200
> 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@intel.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 *@type with trailing flexible array.
> + */
> +#define DECLARE_FLEX(type, name, member, count) \
> + u8 name##_buf[struct_size((type)NULL, member, count)] __aligned(8) = {};\
1. You can use struct_size_t() instead of open-coding it.
2. Maybe use alignof(type) instead of 8? Some structures have larger
alignment requirements.
> + type name = (type)&name##_buf
In general, I still think DECLARE_FLEX(struct foo) is better than
DECLARE_FLEX(struct foo *). Looking at container_of(), struct_size_t()
etc., they all take `type`, not `type *`, so even from the consistency
perspective your solution is not optimal to me.
> +
> #endif /* __LINUX_OVERFLOW_H */
Thanks,
Olek
_______________________________________________
Intel-wired-lan mailing list
Intel-wired-lan@osuosl.org
https://lists.osuosl.org/mailman/listinfo/intel-wired-lan
next prev parent reply other threads:[~2023-08-01 13:57 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-01 11:19 [Intel-wired-lan] [RFC net-next 0/2] introduce DECLARE_FLEX() macro Przemek Kitszel
2023-08-01 11:19 ` [Intel-wired-lan] [RFC net-next 1/2] overflow: add DECLARE_FLEX() for on-stack allocs Przemek Kitszel
2023-08-01 13:54 ` Alexander Lobakin [this message]
2023-08-01 14:18 ` Przemek Kitszel
2023-08-01 17:15 ` Alexander Lobakin
2023-08-01 22:31 ` Kees Cook
2023-08-04 10:59 ` Przemek Kitszel
2023-08-04 15:49 ` Alexander Lobakin
2023-08-04 13:47 ` Przemek Kitszel
2023-08-04 15:44 ` Alexander Lobakin
2023-08-07 12:42 ` Przemek Kitszel
2023-08-07 14:47 ` Kees Cook
2023-08-04 22:39 ` Kees Cook
2023-08-01 11:19 ` [Intel-wired-lan] [RFC net-next 2/2] ice: make use of DECLARE_FLEX() in ice_switch.c Przemek Kitszel
2023-08-01 13:48 ` Alexander Lobakin
2023-08-01 14:36 ` Przemek Kitszel
2023-08-01 17:22 ` Alexander Lobakin
2023-08-01 22:35 ` Kees Cook
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=d4d5324c-8d4b-b2e1-78c8-5c3015b5c03d@intel.com \
--to=aleksander.lobakin@intel.com \
--cc=intel-wired-lan@lists.osuosl.org \
--cc=keescook@chromium.org \
--cc=netdev@vger.kernel.org \
--cc=przemyslaw.kitszel@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox