Skip to content

Commit 6236cd4

Browse files
committed
Merge: CNB96: net: Move {l,t,d}stats allocation to core and convert veth & vrf
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/5146 JIRA: https://issues.redhat.com/browse/RHEL-57740 Commits: ``` 79e0c5b ("net, vrf: Move dstats structure to core") 34d21de ("net: Move {l,t,d}stats allocation to core and convert veth & vrf") ``` Signed-off-by: Ivan Vecera <ivecera@redhat.com> Approved-by: José Ignacio Tornos Martínez <jtornosm@redhat.com> Approved-by: Antoine Tenart <atenart@redhat.com> Approved-by: Petr Oros <poros@redhat.com> Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com> Merged-by: Rado Vrbovsky <rvrbovsk@redhat.com>
2 parents 7cfacd1 + 28e5691 commit 6236cd4

File tree

4 files changed

+88
-47
lines changed

4 files changed

+88
-47
lines changed

drivers/net/veth.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,26 +1430,13 @@ static void veth_free_queues(struct net_device *dev)
14301430

14311431
static int veth_dev_init(struct net_device *dev)
14321432
{
1433-
int err;
1434-
1435-
dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
1436-
if (!dev->lstats)
1437-
return -ENOMEM;
1438-
14391433
netdev_lockdep_set_classes(dev);
1440-
err = veth_alloc_queues(dev);
1441-
if (err) {
1442-
free_percpu(dev->lstats);
1443-
return err;
1444-
}
1445-
1446-
return 0;
1434+
return veth_alloc_queues(dev);
14471435
}
14481436

14491437
static void veth_dev_free(struct net_device *dev)
14501438
{
14511439
veth_free_queues(dev);
1452-
free_percpu(dev->lstats);
14531440
}
14541441

14551442
#ifdef CONFIG_NET_POLL_CONTROLLER
@@ -1738,6 +1725,7 @@ static void veth_setup(struct net_device *dev)
17381725
NETIF_F_HW_VLAN_STAG_RX);
17391726
dev->needs_free_netdev = true;
17401727
dev->priv_destructor = veth_dev_free;
1728+
dev->pcpu_stat_type = NETDEV_PCPU_STAT_LSTATS;
17411729
dev->max_mtu = ETH_MAX_MTU;
17421730

17431731
dev->hw_features = VETH_FEATURES;

drivers/net/vrf.c

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,12 @@ struct net_vrf {
121121
int ifindex;
122122
};
123123

124-
struct pcpu_dstats {
125-
u64 tx_pkts;
126-
u64 tx_bytes;
127-
u64 tx_drps;
128-
u64 rx_pkts;
129-
u64 rx_bytes;
130-
u64 rx_drps;
131-
struct u64_stats_sync syncp;
132-
};
133-
134124
static void vrf_rx_stats(struct net_device *dev, int len)
135125
{
136126
struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
137127

138128
u64_stats_update_begin(&dstats->syncp);
139-
dstats->rx_pkts++;
129+
dstats->rx_packets++;
140130
dstats->rx_bytes += len;
141131
u64_stats_update_end(&dstats->syncp);
142132
}
@@ -161,10 +151,10 @@ static void vrf_get_stats64(struct net_device *dev,
161151
do {
162152
start = u64_stats_fetch_begin(&dstats->syncp);
163153
tbytes = dstats->tx_bytes;
164-
tpkts = dstats->tx_pkts;
165-
tdrops = dstats->tx_drps;
154+
tpkts = dstats->tx_packets;
155+
tdrops = dstats->tx_drops;
166156
rbytes = dstats->rx_bytes;
167-
rpkts = dstats->rx_pkts;
157+
rpkts = dstats->rx_packets;
168158
} while (u64_stats_fetch_retry(&dstats->syncp, start));
169159
stats->tx_bytes += tbytes;
170160
stats->tx_packets += tpkts;
@@ -421,7 +411,7 @@ static int vrf_local_xmit(struct sk_buff *skb, struct net_device *dev,
421411
if (likely(__netif_rx(skb) == NET_RX_SUCCESS))
422412
vrf_rx_stats(dev, len);
423413
else
424-
this_cpu_inc(dev->dstats->rx_drps);
414+
this_cpu_inc(dev->dstats->rx_drops);
425415

426416
return NETDEV_TX_OK;
427417
}
@@ -616,11 +606,11 @@ static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
616606
struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
617607

618608
u64_stats_update_begin(&dstats->syncp);
619-
dstats->tx_pkts++;
609+
dstats->tx_packets++;
620610
dstats->tx_bytes += len;
621611
u64_stats_update_end(&dstats->syncp);
622612
} else {
623-
this_cpu_inc(dev->dstats->tx_drps);
613+
this_cpu_inc(dev->dstats->tx_drops);
624614
}
625615

626616
return ret;
@@ -1176,22 +1166,15 @@ static void vrf_dev_uninit(struct net_device *dev)
11761166

11771167
vrf_rtable_release(dev, vrf);
11781168
vrf_rt6_release(dev, vrf);
1179-
1180-
free_percpu(dev->dstats);
1181-
dev->dstats = NULL;
11821169
}
11831170

11841171
static int vrf_dev_init(struct net_device *dev)
11851172
{
11861173
struct net_vrf *vrf = netdev_priv(dev);
11871174

1188-
dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
1189-
if (!dev->dstats)
1190-
goto out_nomem;
1191-
11921175
/* create the default dst which points back to us */
11931176
if (vrf_rtable_create(dev) != 0)
1194-
goto out_stats;
1177+
goto out_nomem;
11951178

11961179
if (vrf_rt6_create(dev) != 0)
11971180
goto out_rth;
@@ -1205,9 +1188,6 @@ static int vrf_dev_init(struct net_device *dev)
12051188

12061189
out_rth:
12071190
vrf_rtable_release(dev, vrf);
1208-
out_stats:
1209-
free_percpu(dev->dstats);
1210-
dev->dstats = NULL;
12111191
out_nomem:
12121192
return -ENOMEM;
12131193
}
@@ -1706,6 +1686,8 @@ static void vrf_setup(struct net_device *dev)
17061686
dev->min_mtu = IPV6_MIN_MTU;
17071687
dev->max_mtu = IP6_MAX_MTU;
17081688
dev->mtu = dev->max_mtu;
1689+
1690+
dev->pcpu_stat_type = NETDEV_PCPU_STAT_DSTATS;
17091691
}
17101692

17111693
static int vrf_validate(struct nlattr *tb[], struct nlattr *data[],

include/linux/netdevice.h

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,6 +1769,13 @@ enum netdev_ml_priv_type {
17691769
ML_PRIV_CAN,
17701770
};
17711771

1772+
enum netdev_stat_type {
1773+
NETDEV_PCPU_STAT_NONE,
1774+
NETDEV_PCPU_STAT_LSTATS, /* struct pcpu_lstats */
1775+
NETDEV_PCPU_STAT_TSTATS, /* struct pcpu_sw_netstats */
1776+
NETDEV_PCPU_STAT_DSTATS, /* struct pcpu_dstats */
1777+
};
1778+
17721779
/**
17731780
* struct net_device - The DEVICE structure.
17741781
*
@@ -1964,10 +1971,14 @@ enum netdev_ml_priv_type {
19641971
*
19651972
* @ml_priv: Mid-layer private
19661973
* @ml_priv_type: Mid-layer private type
1967-
* @lstats: Loopback statistics
1968-
* @tstats: Tunnel statistics
1969-
* @dstats: Dummy statistics
1970-
* @vstats: Virtual ethernet statistics
1974+
*
1975+
* @pcpu_stat_type: Type of device statistics which the core should
1976+
* allocate/free: none, lstats, tstats, dstats. none
1977+
* means the driver is handling statistics allocation/
1978+
* freeing internally.
1979+
* @lstats: Loopback statistics: packets, bytes
1980+
* @tstats: Tunnel statistics: RX/TX packets, RX/TX bytes
1981+
* @dstats: Dummy statistics: RX/TX/drop packets, RX/TX bytes
19711982
*
19721983
* @garp_port: GARP
19731984
* @mrp_port: MRP
@@ -2356,6 +2367,8 @@ struct net_device {
23562367
void *ml_priv;
23572368
enum netdev_ml_priv_type ml_priv_type;
23582369

2370+
enum netdev_stat_type pcpu_stat_type:8;
2371+
23592372
#if IS_ENABLED(CONFIG_GARP)
23602373
struct garp_port __rcu *garp_port;
23612374
#endif
@@ -2797,6 +2810,16 @@ struct pcpu_sw_netstats {
27972810
struct u64_stats_sync syncp;
27982811
} __aligned(4 * sizeof(u64));
27992812

2813+
struct pcpu_dstats {
2814+
u64 rx_packets;
2815+
u64 rx_bytes;
2816+
u64 rx_drops;
2817+
u64 tx_packets;
2818+
u64 tx_bytes;
2819+
u64 tx_drops;
2820+
struct u64_stats_sync syncp;
2821+
} __aligned(8 * sizeof(u64));
2822+
28002823
struct pcpu_lstats {
28012824
u64_stats_t packets;
28022825
u64_stats_t bytes;

net/core/dev.c

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9971,6 +9971,46 @@ void netif_tx_stop_all_queues(struct net_device *dev)
99719971
}
99729972
EXPORT_SYMBOL(netif_tx_stop_all_queues);
99739973

9974+
static int netdev_do_alloc_pcpu_stats(struct net_device *dev)
9975+
{
9976+
void __percpu *v;
9977+
9978+
switch (dev->pcpu_stat_type) {
9979+
case NETDEV_PCPU_STAT_NONE:
9980+
return 0;
9981+
case NETDEV_PCPU_STAT_LSTATS:
9982+
v = dev->lstats = netdev_alloc_pcpu_stats(struct pcpu_lstats);
9983+
break;
9984+
case NETDEV_PCPU_STAT_TSTATS:
9985+
v = dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
9986+
break;
9987+
case NETDEV_PCPU_STAT_DSTATS:
9988+
v = dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
9989+
break;
9990+
default:
9991+
return -EINVAL;
9992+
}
9993+
9994+
return v ? 0 : -ENOMEM;
9995+
}
9996+
9997+
static void netdev_do_free_pcpu_stats(struct net_device *dev)
9998+
{
9999+
switch (dev->pcpu_stat_type) {
10000+
case NETDEV_PCPU_STAT_NONE:
10001+
return;
10002+
case NETDEV_PCPU_STAT_LSTATS:
10003+
free_percpu(dev->lstats);
10004+
break;
10005+
case NETDEV_PCPU_STAT_TSTATS:
10006+
free_percpu(dev->tstats);
10007+
break;
10008+
case NETDEV_PCPU_STAT_DSTATS:
10009+
free_percpu(dev->dstats);
10010+
break;
10011+
}
10012+
}
10013+
997410014
/**
997510015
* register_netdevice - register a network device
997610016
* @dev: device to register
@@ -10039,9 +10079,13 @@ int register_netdevice(struct net_device *dev)
1003910079
goto err_uninit;
1004010080
}
1004110081

10082+
ret = netdev_do_alloc_pcpu_stats(dev);
10083+
if (ret)
10084+
goto err_uninit;
10085+
1004210086
ret = dev_index_reserve(net, dev->ifindex);
1004310087
if (ret < 0)
10044-
goto err_uninit;
10088+
goto err_free_pcpu;
1004510089
dev->ifindex = ret;
1004610090

1004710091
/* Transfer changeable features to wanted_features and enable
@@ -10147,6 +10191,8 @@ int register_netdevice(struct net_device *dev)
1014710191
call_netdevice_notifiers(NETDEV_PRE_UNINIT, dev);
1014810192
err_ifindex_release:
1014910193
dev_index_release(net, dev->ifindex);
10194+
err_free_pcpu:
10195+
netdev_do_free_pcpu_stats(dev);
1015010196
err_uninit:
1015110197
if (dev->netdev_ops->ndo_uninit)
1015210198
dev->netdev_ops->ndo_uninit(dev);
@@ -10406,6 +10452,8 @@ void netdev_run_todo(void)
1040610452
#if IS_ENABLED(CONFIG_DECNET)
1040710453
WARN_ON(dev->dn_ptr);
1040810454
#endif
10455+
10456+
netdev_do_free_pcpu_stats(dev);
1040910457
if (dev->priv_destructor)
1041010458
dev->priv_destructor(dev);
1041110459
if (dev->needs_free_netdev)

0 commit comments

Comments
 (0)