Re: [PATCH net-next v2] netdevice: define and allocate &net_device _properly_
From: Alexander Lobakin
Date: Thu Jul 11 2024 - 08:54:32 EST
From: Eric Dumazet <edumazet@xxxxxxxxxx>
Date: Wed, 10 Jul 2024 10:04:39 -0700
> On Wed, Jul 10, 2024 at 4:19 AM Breno Leitao <leitao@xxxxxxxxxx> wrote:
>>
>> Hello Eric,
>>
>> On Tue, Jul 09, 2024 at 08:27:45AM -0700, Eric Dumazet wrote:
>>> On Tue, Jul 9, 2024 at 5:54 AM Breno Leitao <leitao@xxxxxxxxxx> wrote:
>>
>>>> @@ -2596,7 +2599,7 @@ void dev_net_set(struct net_device *dev, struct net *net)
>>>> */
>>>> static inline void *netdev_priv(const struct net_device *dev)
>>>> {
>>>> - return (char *)dev + ALIGN(sizeof(struct net_device), NETDEV_ALIGN);
>>>> + return (void *)dev->priv;
>>>
>>> Minor remark : the cast is not needed, but this is fine.
>>
>> In fact, the compiler is not very happy if I remove the cast:
>>
>> ./include/linux/netdevice.h:2603:9: error: returning 'const u8[]' (aka 'const unsigned char[]') from a function with result type 'void *' discards qualifiers [-Werror,-Wincompatible-pointer-types-discards-qualifiers]
>> 2603 | return dev->priv;
>> | ^~~~~~~~~
>
> This is because of the ‘const’ qualifier of the parameter.
>
> This could be solved with _Generic() later, if we want to keep the
> const qualifier.
I tried _Generic() when I was working on this patch and it seems like
lots of drivers need to be fixed first. They pass a const &net_device,
but assign the result to a non-const variable and modify fields there.
That's why I bet up on this and just casted to (void *) for now.
Thanks,
Olek