Re: [PATCH 2/3] container_of: remove useless pair of parentheses

From: Vincent Mailhol

Date: Wed Jul 15 2026 - 12:30:10 EST


On 15/07/2026 at 16:57, Greg Kroah-Hartman wrote:
> On Wed, Jul 15, 2026 at 04:51:28PM +0200, Vincent Mailhol wrote:
>> On 15/07/2026 at 16:30, Alexey Dobriyan wrote:
>>> On Tue, Jul 14, 2026 at 08:18:02PM +0200, Vincent Mailhol wrote:
>>>> The last expression in container_of() doesn't need an extra pair of
>>>> parenthesis. Remove it.
>>>>
>>>> Signed-off-by: Vincent Mailhol <mailhol@xxxxxxxxxx>
>>>> ---
>>>> include/linux/container_of.h | 2 +-
>>>> 1 file changed, 1 insertion(+), 1 deletion(-)
>>>>
>>>> diff --git a/include/linux/container_of.h b/include/linux/container_of.h
>>>> index 28500a62ab7e..68153170db32 100644
>>>> --- a/include/linux/container_of.h
>>>> +++ b/include/linux/container_of.h
>>>> @@ -21,7 +21,7 @@
>>>> static_assert(__same_type(*(ptr), typeof_member(type, member)) || \
>>>> __same_type(*(ptr), void), \
>>>> "pointer type mismatch in container_of()"); \
>>>> - ((type *)(__mptr - offsetof(type, member))); })
>>>> + (type *)(__mptr - offsetof(type, member)); })
>>>
>>> container_of_const() has the same problem.
>>
>> Yes. But scoped I my series to the container_of() macro only.
>>
>> I don't plan to send a follow-up patch to remove the other useless
>> parenthesis. The things which really bothered me was the __mptr
>> variable which I removed in patch #3. And with that, I am done with
>> what I wanted to contribute in this file.
>>
>> But you are welcome to do this clean-up if you feel the need for it.
>>
>>> And it should parenthesize first argument to _Generic.
>>
>> Do you mean like this:
>>
>> #define container_of_const(ptr, type, member) \
>> _Generic((ptr), \
>> const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\
>> default: ((type *)container_of(ptr, type, member)) \
>> )
>>
>> ?
>>
>> Why would parenthesis be needed here? I don't see what kind of
>> expression would make the current implementation unsafe.
>
> It took me a while working with container_of_const() to get it right
> with the (), so be careful removing any.

I just tested this patch:

---8<---
diff --git a/include/linux/container_of.h b/include/linux/container_of.h
index 1f6ebf27d962..03d5b91534a7 100644
--- a/include/linux/container_of.h
+++ b/include/linux/container_of.h
@@ -34,8 +34,8 @@
*/
#define container_of_const(ptr, type, member) \
_Generic(ptr, \
- const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\
- default: ((type *)container_of(ptr, type, member)) \
+ const typeof(*(ptr)) *: (const type *)container_of(ptr, type, member),\
+ default: (type *)container_of(ptr, type, member) \
)
#endif /* _LINUX_CONTAINER_OF_H */
---8<---

I could compile and boot it, no problem (I am actually writing this
message from a machine on which this patch is applied).

So, yes, I believe that these two pairs of parenthesis can be removed.
Just not sure if it is worth it. For container_of(), the parenthesis
removal was part of a series with more meaningful changes. I don't
feel exited of sending another series to just remove the
container_of_const() parenthesis.


Yours sincerely,
Vincent Mailhol