* [PATCH][next] container_of: add container_first() macro
@ 2024-09-20 12:50 Gustavo A. R. Silva
2024-09-21 5:45 ` Dan Carpenter
0 siblings, 1 reply; 4+ messages in thread
From: Gustavo A. R. Silva @ 2024-09-20 12:50 UTC (permalink / raw)
To: Greg KH, Dan Carpenter; +Cc: linux-kernel, Gustavo A. R. Silva, linux-hardening
This is like container_of() but it has an assert to ensure that it's
using the first struct member.
Co-developed-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.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))); })
+
+
/**
* container_of_const - cast a member of a structure out to the containing
* structure and preserve the const-ness of the pointer
--
2.34.1
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH][next] container_of: add container_first() macro
2024-09-20 12:50 [PATCH][next] container_of: add container_first() macro Gustavo A. R. Silva
@ 2024-09-21 5:45 ` Dan Carpenter
2024-09-21 5:52 ` Greg KH
0 siblings, 1 reply; 4+ messages in thread
From: Dan Carpenter @ 2024-09-21 5:45 UTC (permalink / raw)
To: Gustavo A. R. Silva; +Cc: Greg KH, linux-kernel, linux-hardening
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@linaro.org>
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH][next] container_of: add container_first() macro
2024-09-21 5:45 ` Dan Carpenter
@ 2024-09-21 5:52 ` Greg KH
2024-09-21 7:10 ` Gustavo A. R. Silva
0 siblings, 1 reply; 4+ messages in thread
From: Greg KH @ 2024-09-21 5:52 UTC (permalink / raw)
To: Dan Carpenter; +Cc: Gustavo A. R. Silva, linux-kernel, linux-hardening
On Sat, Sep 21, 2024 at 08:45:28AM +0300, Dan Carpenter wrote:
> 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().
Yes, that would be best.
> 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.
I agree, but I wonder if it's safe to just do that already and how many
build warnings would happen. Last I checked it was a lot.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH][next] container_of: add container_first() macro
2024-09-21 5:52 ` Greg KH
@ 2024-09-21 7:10 ` Gustavo A. R. Silva
0 siblings, 0 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2024-09-21 7:10 UTC (permalink / raw)
To: Greg KH, Dan Carpenter; +Cc: Gustavo A. R. Silva, linux-kernel, linux-hardening
On 21/09/24 07:52, Greg KH wrote:
> On Sat, Sep 21, 2024 at 08:45:28AM +0300, Dan Carpenter wrote:
>> 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().
>
> Yes, that would be best.
Okay, I'll change that.
--
Gustavo
>
>> 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.
>
> I agree, but I wonder if it's safe to just do that already and how many
> build warnings would happen. Last I checked it was a lot.
>
> thanks,
>
> greg k-h
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-09-21 7:11 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-20 12:50 [PATCH][next] container_of: add container_first() macro Gustavo A. R. Silva
2024-09-21 5:45 ` Dan Carpenter
2024-09-21 5:52 ` Greg KH
2024-09-21 7:10 ` Gustavo A. R. Silva
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®