Skip to content

Commit 9256645

Browse files
jarodwilsondavem330
authored andcommitted
net/core: relax BUILD_BUG_ON in netdev_stats_to_stats64
The netdev_stats_to_stats64 function copies the deprecated net_device_stats format stats into rtnl_link_stats64 for legacy support purposes, but with the BUILD_BUG_ON as it was, it wasn't possible to extend rtnl_link_stats64 without also extending net_device_stats. Relax the BUILD_BUG_ON to only require that rtnl_link_stats64 is larger, and zero out all the stat counters that aren't present in net_device_stats. CC: Eric Dumazet <edumazet@google.com> CC: netdev@vger.kernel.org Signed-off-by: Jarod Wilson <jarod@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 8172981 commit 9256645

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

net/core/dev.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -7253,24 +7253,31 @@ void netdev_run_todo(void)
72537253
}
72547254
}
72557255

7256-
/* Convert net_device_stats to rtnl_link_stats64. They have the same
7257-
* fields in the same order, with only the type differing.
7256+
/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
7257+
* all the same fields in the same order as net_device_stats, with only
7258+
* the type differing, but rtnl_link_stats64 may have additional fields
7259+
* at the end for newer counters.
72587260
*/
72597261
void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
72607262
const struct net_device_stats *netdev_stats)
72617263
{
72627264
#if BITS_PER_LONG == 64
7263-
BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats));
7265+
BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
72647266
memcpy(stats64, netdev_stats, sizeof(*stats64));
7267+
/* zero out counters that only exist in rtnl_link_stats64 */
7268+
memset((char *)stats64 + sizeof(*netdev_stats), 0,
7269+
sizeof(*stats64) - sizeof(*netdev_stats));
72657270
#else
7266-
size_t i, n = sizeof(*stats64) / sizeof(u64);
7271+
size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
72677272
const unsigned long *src = (const unsigned long *)netdev_stats;
72687273
u64 *dst = (u64 *)stats64;
72697274

7270-
BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) !=
7271-
sizeof(*stats64) / sizeof(u64));
7275+
BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
72727276
for (i = 0; i < n; i++)
72737277
dst[i] = src[i];
7278+
/* zero out counters that only exist in rtnl_link_stats64 */
7279+
memset((char *)stats64 + n * sizeof(u64), 0,
7280+
sizeof(*stats64) - n * sizeof(u64));
72747281
#endif
72757282
}
72767283
EXPORT_SYMBOL(netdev_stats_to_stats64);

0 commit comments

Comments
 (0)