[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <694a8a87-cc98-4c04-8228-d399133fdd39@suswa.mountain>
Date: Sat, 21 Sep 2024 08:45:28 +0300
From: Dan Carpenter <dan.carpenter@...aro.org>
To: "Gustavo A. R. Silva" <gustavoars@...nel.org>
Cc: Greg KH <gregkh@...uxfoundation.org>, linux-kernel@...r.kernel.org,
linux-hardening@...r.kernel.org
Subject: Re: [PATCH][next] container_of: add container_first() macro
On Fri, Sep 20, 2024 at 02:50:02PM +0200, Gustavo A. R. Silva wrote:
> This is like container_of() but it has an assert to ensure that it's
> using the first struct member.
>
I just remembered that Greg wanted this based on container_of_const().
Really container_of_const() should be renamed to container_of() and
container_of() should be renamed to container_of_helper() and we
would add a #define container_of_const container_of for the transition.
> Co-developed-by: Dan Carpenter <dan.carpenter@...aro.org>
> Signed-off-by: Dan Carpenter <dan.carpenter@...aro.org>
> Signed-off-by: Gustavo A. R. Silva <gustavoars@...nel.org>
> ---
> include/linux/container_of.h | 18 ++++++++++++++++++
> 1 file changed, 18 insertions(+)
>
> diff --git a/include/linux/container_of.h b/include/linux/container_of.h
> index 713890c867be..928c5865c1b4 100644
> --- a/include/linux/container_of.h
> +++ b/include/linux/container_of.h
> @@ -22,6 +22,24 @@
> "pointer type mismatch in container_of()"); \
> ((type *)(__mptr - offsetof(type, member))); })
>
> +/**
> + * container_first - cast first member of a structure out to the containing
> + * structure
> + * @ptr: the pointer to the member.
> + * @type: the type of the container struct this is embedded in.
> + * @member: the name of the member within the struct.
> + *
> + * WARNING: any const qualifier of @ptr is lost.
> + */
> +#define container_first(ptr, type, member) ({ \
> + void *__mptr = (void *)(ptr); \
> + static_assert(__same_type(*(ptr), ((type *)0)->member) || \
> + __same_type(*(ptr), void), \
> + "pointer type mismatch in container_of()"); \
> + static_assert(offsetof(type, member) == 0, "not first member"); \
> + ((type *)(__mptr - offsetof(type, member))); })
> +
> +
I added too many blank lines.
regards,
dan carpenter
Powered by blists - more mailing lists