Skip to content

Commit baebdf4

Browse files
Sebastian Andrzej Siewiordavem330
Sebastian Andrzej Siewior
authored andcommitted
net: dev: Makes sure netif_rx() can be invoked in any context.
Dave suggested a while ago (eleven years by now) "Let's make netif_rx() work in all contexts and get rid of netif_rx_ni()". Eric agreed and pointed out that modern devices should use netif_receive_skb() to avoid the overhead. In the meantime someone added another variant, netif_rx_any_context(), which behaves as suggested. netif_rx() must be invoked with disabled bottom halves to ensure that pending softirqs, which were raised within the function, are handled. netif_rx_ni() can be invoked only from process context (bottom halves must be enabled) because the function handles pending softirqs without checking if bottom halves were disabled or not. netif_rx_any_context() invokes on the former functions by checking in_interrupts(). netif_rx() could be taught to handle both cases (disabled and enabled bottom halves) by simply disabling bottom halves while invoking netif_rx_internal(). The local_bh_enable() invocation will then invoke pending softirqs only if the BH-disable counter drops to zero. Eric is concerned about the overhead of BH-disable+enable especially in regard to the loopback driver. As critical as this driver is, it will receive a shortcut to avoid the additional overhead which is not needed. Add a local_bh_disable() section in netif_rx() to ensure softirqs are handled if needed. Provide __netif_rx() which does not disable BH and has a lockdep assert to ensure that interrupts are disabled. Use this shortcut in the loopback driver and in drivers/net/*.c. Make netif_rx_ni() and netif_rx_any_context() invoke netif_rx() so they can be removed once they are no more users left. Link: https://lkml.kernel.org/r/20100415.020246.218622820.davem@davemloft.net Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Reviewed-by: Eric Dumazet <edumazet@google.com> Reviewed-by: Toke Høiland-Jørgensen <toke@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent f234ae2 commit baebdf4

File tree

16 files changed

+60
-73
lines changed

16 files changed

+60
-73
lines changed

drivers/net/amt.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2373,7 +2373,7 @@ static bool amt_membership_query_handler(struct amt_dev *amt,
23732373
skb->pkt_type = PACKET_MULTICAST;
23742374
skb->ip_summed = CHECKSUM_NONE;
23752375
len = skb->len;
2376-
if (netif_rx(skb) == NET_RX_SUCCESS) {
2376+
if (__netif_rx(skb) == NET_RX_SUCCESS) {
23772377
amt_update_gw_status(amt, AMT_STATUS_RECEIVED_QUERY, true);
23782378
dev_sw_netstats_rx_add(amt->dev, len);
23792379
} else {
@@ -2470,7 +2470,7 @@ static bool amt_update_handler(struct amt_dev *amt, struct sk_buff *skb)
24702470
skb->pkt_type = PACKET_MULTICAST;
24712471
skb->ip_summed = CHECKSUM_NONE;
24722472
len = skb->len;
2473-
if (netif_rx(skb) == NET_RX_SUCCESS) {
2473+
if (__netif_rx(skb) == NET_RX_SUCCESS) {
24742474
amt_update_relay_status(tunnel, AMT_STATUS_RECEIVED_UPDATE,
24752475
true);
24762476
dev_sw_netstats_rx_add(amt->dev, len);

drivers/net/geneve.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ static int geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
925925
}
926926

927927
skb->protocol = eth_type_trans(skb, geneve->dev);
928-
netif_rx(skb);
928+
__netif_rx(skb);
929929
dst_release(&rt->dst);
930930
return -EMSGSIZE;
931931
}
@@ -1021,7 +1021,7 @@ static int geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
10211021
}
10221022

10231023
skb->protocol = eth_type_trans(skb, geneve->dev);
1024-
netif_rx(skb);
1024+
__netif_rx(skb);
10251025
dst_release(dst);
10261026
return -EMSGSIZE;
10271027
}

drivers/net/gtp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static int gtp_rx(struct pdp_ctx *pctx, struct sk_buff *skb,
207207

208208
dev_sw_netstats_rx_add(pctx->dev, skb->len);
209209

210-
netif_rx(skb);
210+
__netif_rx(skb);
211211
return 0;
212212

213213
err:

drivers/net/loopback.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ static netdev_tx_t loopback_xmit(struct sk_buff *skb,
7878

7979
skb_orphan(skb);
8080

81-
/* Before queueing this packet to netif_rx(),
81+
/* Before queueing this packet to __netif_rx(),
8282
* make sure dst is refcounted.
8383
*/
8484
skb_dst_force(skb);
8585

8686
skb->protocol = eth_type_trans(skb, dev);
8787

8888
len = skb->len;
89-
if (likely(netif_rx(skb) == NET_RX_SUCCESS))
89+
if (likely(__netif_rx(skb) == NET_RX_SUCCESS))
9090
dev_lstats_add(dev, len);
9191

9292
return NETDEV_TX_OK;

drivers/net/macsec.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ static enum rx_handler_result handle_not_macsec(struct sk_buff *skb)
10331033
else
10341034
nskb->pkt_type = PACKET_MULTICAST;
10351035

1036-
netif_rx(nskb);
1036+
__netif_rx(nskb);
10371037
}
10381038
continue;
10391039
}
@@ -1056,7 +1056,7 @@ static enum rx_handler_result handle_not_macsec(struct sk_buff *skb)
10561056

10571057
nskb->dev = ndev;
10581058

1059-
if (netif_rx(nskb) == NET_RX_SUCCESS) {
1059+
if (__netif_rx(nskb) == NET_RX_SUCCESS) {
10601060
u64_stats_update_begin(&secy_stats->syncp);
10611061
secy_stats->stats.InPktsUntagged++;
10621062
u64_stats_update_end(&secy_stats->syncp);
@@ -1288,7 +1288,7 @@ static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
12881288

12891289
macsec_reset_skb(nskb, macsec->secy.netdev);
12901290

1291-
ret = netif_rx(nskb);
1291+
ret = __netif_rx(nskb);
12921292
if (ret == NET_RX_SUCCESS) {
12931293
u64_stats_update_begin(&secy_stats->syncp);
12941294
secy_stats->stats.InPktsUnknownSCI++;

drivers/net/macvlan.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ static void macvlan_forward_source_one(struct sk_buff *skb,
410410
if (ether_addr_equal_64bits(eth_hdr(skb)->h_dest, dev->dev_addr))
411411
nskb->pkt_type = PACKET_HOST;
412412

413-
ret = netif_rx(nskb);
413+
ret = __netif_rx(nskb);
414414
macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
415415
}
416416

@@ -468,7 +468,7 @@ static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
468468
/* forward to original port. */
469469
vlan = src;
470470
ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?:
471-
netif_rx(skb);
471+
__netif_rx(skb);
472472
handle_res = RX_HANDLER_CONSUMED;
473473
goto out;
474474
}

drivers/net/mhi_net.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static void mhi_net_dl_callback(struct mhi_device *mhi_dev,
225225
u64_stats_inc(&mhi_netdev->stats.rx_packets);
226226
u64_stats_add(&mhi_netdev->stats.rx_bytes, skb->len);
227227
u64_stats_update_end(&mhi_netdev->stats.rx_syncp);
228-
netif_rx(skb);
228+
__netif_rx(skb);
229229
}
230230

231231
/* Refill if RX buffers queue becomes low */

drivers/net/ntb_netdev.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static void ntb_netdev_rx_handler(struct ntb_transport_qp *qp, void *qp_data,
119119
skb->protocol = eth_type_trans(skb, ndev);
120120
skb->ip_summed = CHECKSUM_NONE;
121121

122-
if (netif_rx(skb) == NET_RX_DROP) {
122+
if (__netif_rx(skb) == NET_RX_DROP) {
123123
ndev->stats.rx_errors++;
124124
ndev->stats.rx_dropped++;
125125
} else {

drivers/net/rionet.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static int rionet_rx_clean(struct net_device *ndev)
109109
skb_put(rnet->rx_skb[i], RIO_MAX_MSG_SIZE);
110110
rnet->rx_skb[i]->protocol =
111111
eth_type_trans(rnet->rx_skb[i], ndev);
112-
error = netif_rx(rnet->rx_skb[i]);
112+
error = __netif_rx(rnet->rx_skb[i]);
113113

114114
if (error == NET_RX_DROP) {
115115
ndev->stats.rx_dropped++;

drivers/net/sb1000.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ printk("cm0: IP identification: %02x%02x fragment offset: %02x%02x\n", buffer[3
872872

873873
/* datagram completed: send to upper level */
874874
skb_trim(skb, dlen);
875-
netif_rx(skb);
875+
__netif_rx(skb);
876876
stats->rx_bytes+=dlen;
877877
stats->rx_packets++;
878878
lp->rx_skb[ns] = NULL;

drivers/net/veth.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ static int veth_forward_skb(struct net_device *dev, struct sk_buff *skb,
287287
{
288288
return __dev_forward_skb(dev, skb) ?: xdp ?
289289
veth_xdp_rx(rq, skb) :
290-
netif_rx(skb);
290+
__netif_rx(skb);
291291
}
292292

293293
/* return true if the specified skb has chances of GRO aggregation

drivers/net/vrf.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ static int vrf_local_xmit(struct sk_buff *skb, struct net_device *dev,
418418

419419
skb->protocol = eth_type_trans(skb, dev);
420420

421-
if (likely(netif_rx(skb) == NET_RX_SUCCESS))
421+
if (likely(__netif_rx(skb) == NET_RX_SUCCESS))
422422
vrf_rx_stats(dev, len);
423423
else
424424
this_cpu_inc(dev->dstats->rx_drps);

drivers/net/vxlan.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2541,7 +2541,7 @@ static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
25412541
tx_stats->tx_bytes += len;
25422542
u64_stats_update_end(&tx_stats->syncp);
25432543

2544-
if (netif_rx(skb) == NET_RX_SUCCESS) {
2544+
if (__netif_rx(skb) == NET_RX_SUCCESS) {
25452545
u64_stats_update_begin(&rx_stats->syncp);
25462546
rx_stats->rx_packets++;
25472547
rx_stats->rx_bytes += len;

include/linux/netdevice.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -3672,8 +3672,18 @@ u32 bpf_prog_run_generic_xdp(struct sk_buff *skb, struct xdp_buff *xdp,
36723672
void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog);
36733673
int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb);
36743674
int netif_rx(struct sk_buff *skb);
3675-
int netif_rx_ni(struct sk_buff *skb);
3676-
int netif_rx_any_context(struct sk_buff *skb);
3675+
int __netif_rx(struct sk_buff *skb);
3676+
3677+
static inline int netif_rx_ni(struct sk_buff *skb)
3678+
{
3679+
return netif_rx(skb);
3680+
}
3681+
3682+
static inline int netif_rx_any_context(struct sk_buff *skb)
3683+
{
3684+
return netif_rx(skb);
3685+
}
3686+
36773687
int netif_receive_skb(struct sk_buff *skb);
36783688
int netif_receive_skb_core(struct sk_buff *skb);
36793689
void netif_receive_skb_list_internal(struct list_head *head);

include/trace/events/net.h

-14
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,6 @@ DEFINE_EVENT(net_dev_rx_verbose_template, netif_rx_entry,
260260
TP_ARGS(skb)
261261
);
262262

263-
DEFINE_EVENT(net_dev_rx_verbose_template, netif_rx_ni_entry,
264-
265-
TP_PROTO(const struct sk_buff *skb),
266-
267-
TP_ARGS(skb)
268-
);
269-
270263
DECLARE_EVENT_CLASS(net_dev_rx_exit_template,
271264

272265
TP_PROTO(int ret),
@@ -312,13 +305,6 @@ DEFINE_EVENT(net_dev_rx_exit_template, netif_rx_exit,
312305
TP_ARGS(ret)
313306
);
314307

315-
DEFINE_EVENT(net_dev_rx_exit_template, netif_rx_ni_exit,
316-
317-
TP_PROTO(int ret),
318-
319-
TP_ARGS(ret)
320-
);
321-
322308
DEFINE_EVENT(net_dev_rx_exit_template, netif_receive_skb_list_exit,
323309

324310
TP_PROTO(int ret),

net/core/dev.c

+29-38
Original file line numberDiff line numberDiff line change
@@ -4815,66 +4815,57 @@ static int netif_rx_internal(struct sk_buff *skb)
48154815
return ret;
48164816
}
48174817

4818+
/**
4819+
* __netif_rx - Slightly optimized version of netif_rx
4820+
* @skb: buffer to post
4821+
*
4822+
* This behaves as netif_rx except that it does not disable bottom halves.
4823+
* As a result this function may only be invoked from the interrupt context
4824+
* (either hard or soft interrupt).
4825+
*/
4826+
int __netif_rx(struct sk_buff *skb)
4827+
{
4828+
int ret;
4829+
4830+
lockdep_assert_once(hardirq_count() | softirq_count());
4831+
4832+
trace_netif_rx_entry(skb);
4833+
ret = netif_rx_internal(skb);
4834+
trace_netif_rx_exit(ret);
4835+
return ret;
4836+
}
4837+
EXPORT_SYMBOL(__netif_rx);
4838+
48184839
/**
48194840
* netif_rx - post buffer to the network code
48204841
* @skb: buffer to post
48214842
*
48224843
* This function receives a packet from a device driver and queues it for
4823-
* the upper (protocol) levels to process. It always succeeds. The buffer
4824-
* may be dropped during processing for congestion control or by the
4825-
* protocol layers.
4844+
* the upper (protocol) levels to process via the backlog NAPI device. It
4845+
* always succeeds. The buffer may be dropped during processing for
4846+
* congestion control or by the protocol layers.
4847+
* The network buffer is passed via the backlog NAPI device. Modern NIC
4848+
* driver should use NAPI and GRO.
4849+
* This function can used from any context.
48264850
*
48274851
* return values:
48284852
* NET_RX_SUCCESS (no congestion)
48294853
* NET_RX_DROP (packet was dropped)
48304854
*
48314855
*/
4832-
48334856
int netif_rx(struct sk_buff *skb)
48344857
{
48354858
int ret;
48364859

4860+
local_bh_disable();
48374861
trace_netif_rx_entry(skb);
4838-
48394862
ret = netif_rx_internal(skb);
48404863
trace_netif_rx_exit(ret);
4841-
4864+
local_bh_enable();
48424865
return ret;
48434866
}
48444867
EXPORT_SYMBOL(netif_rx);
48454868

4846-
int netif_rx_ni(struct sk_buff *skb)
4847-
{
4848-
int err;
4849-
4850-
trace_netif_rx_ni_entry(skb);
4851-
4852-
preempt_disable();
4853-
err = netif_rx_internal(skb);
4854-
if (local_softirq_pending())
4855-
do_softirq();
4856-
preempt_enable();
4857-
trace_netif_rx_ni_exit(err);
4858-
4859-
return err;
4860-
}
4861-
EXPORT_SYMBOL(netif_rx_ni);
4862-
4863-
int netif_rx_any_context(struct sk_buff *skb)
4864-
{
4865-
/*
4866-
* If invoked from contexts which do not invoke bottom half
4867-
* processing either at return from interrupt or when softrqs are
4868-
* reenabled, use netif_rx_ni() which invokes bottomhalf processing
4869-
* directly.
4870-
*/
4871-
if (in_interrupt())
4872-
return netif_rx(skb);
4873-
else
4874-
return netif_rx_ni(skb);
4875-
}
4876-
EXPORT_SYMBOL(netif_rx_any_context);
4877-
48784869
static __latent_entropy void net_tx_action(struct softirq_action *h)
48794870
{
48804871
struct softnet_data *sd = this_cpu_ptr(&softnet_data);

0 commit comments

Comments
 (0)