2024-02-28 23:54:23

by Andy Shevchenko

[permalink] [raw]
Subject: [PATCH v4 7/8] net-device: Use new helpers from overflow.h in netdevice APIs

We have two new helpers struct_size_with_data() and struct_data_pointer()
that we can utilize in alloc_netdev_mqs() and netdev_priv(). Do it so.

Signed-off-by: Andy Shevchenko <[email protected]>
---
include/linux/netdevice.h | 3 ++-
net/core/dev.c | 10 +++++-----
2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
index c41019f34179..d046dca18854 100644
--- a/include/linux/netdevice.h
+++ b/include/linux/netdevice.h
@@ -25,6 +25,7 @@
#include <linux/bug.h>
#include <linux/delay.h>
#include <linux/atomic.h>
+#include <linux/overflow.h>
#include <linux/prefetch.h>
#include <asm/cache.h>
#include <asm/byteorder.h>
@@ -2668,7 +2669,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 struct_data_pointer(dev, NETDEV_ALIGN);
}

/* Set the sysfs physical device reference for the network logical device
diff --git a/net/core/dev.c b/net/core/dev.c
index 69c3e3613372..80b765bb8ba2 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -10859,12 +10859,12 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
return NULL;
}

- alloc_size = sizeof(struct net_device);
- if (sizeof_priv) {
+ if (sizeof_priv)
/* ensure 32-byte alignment of private area */
- alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
- alloc_size += sizeof_priv;
- }
+ alloc_size = struct_size_with_data(p, NETDEV_ALIGN, sizeof_priv);
+ else
+ alloc_size = sizeof(struct net_device);
+
/* ensure 32-byte alignment of whole construct */
alloc_size += NETDEV_ALIGN - 1;

--
2.43.0.rc1.1.gbec44491f096



2024-02-29 01:15:24

by Jakub Kicinski

[permalink] [raw]
Subject: Re: [PATCH v4 7/8] net-device: Use new helpers from overflow.h in netdevice APIs

On Wed, 28 Feb 2024 19:03:12 -0600 Gustavo A. R. Silva wrote:
> On 2/28/24 18:57, Jakub Kicinski wrote:
> > On Wed, 28 Feb 2024 18:49:25 -0600 Gustavo A. R. Silva wrote:
> >> struct net_device {
> >> struct_group_tagged(net_device_hdr, hdr,
> >> ...
> >> u32 priv_size;
> >> );
> >> u8 priv_data[] __counted_by(priv_size) __aligned(NETDEV_ALIGN);
> >> }
> >
> > No, no, that's not happening.
>
> Thanks, one less flex-struct to change. :)

I like the flex struct.
I don't like struct group around a 360LoC declaration just to avoid
having to fix up a handful of users.

2024-02-29 01:36:42

by Gustavo A. R. Silva

[permalink] [raw]
Subject: Re: [PATCH v4 7/8] net-device: Use new helpers from overflow.h in netdevice APIs



On 2/28/24 19:15, Jakub Kicinski wrote:
> On Wed, 28 Feb 2024 19:03:12 -0600 Gustavo A. R. Silva wrote:
>> On 2/28/24 18:57, Jakub Kicinski wrote:
>>> On Wed, 28 Feb 2024 18:49:25 -0600 Gustavo A. R. Silva wrote:
>>>> struct net_device {
>>>> struct_group_tagged(net_device_hdr, hdr,
>>>> ...
>>>> u32 priv_size;
>>>> );
>>>> u8 priv_data[] __counted_by(priv_size) __aligned(NETDEV_ALIGN);
>>>> }
>>>
>>> No, no, that's not happening.
>>
>> Thanks, one less flex-struct to change. :)
>
> I like the flex struct.
> I don't like struct group around a 360LoC declaration just to avoid
> having to fix up a handful of users.

That's what I mean. If we can prevent the flex array ending up in the
middle of a struct by any means, then I wouldn't have to change the
flex struct.

--
Gustavo