xref: /linux/net/core/gro_cells.c (revision 4e3b56eb2ed879353de392acec7b485b123840a4)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/skbuff.h>
3 #include <linux/slab.h>
4 #include <linux/netdevice.h>
5 #include <net/gro_cells.h>
6 #include <net/hotdata.h>
7 
8 struct gro_cell {
9 	struct sk_buff_head	napi_skbs;
10 	struct napi_struct	napi;
11 	local_lock_t		bh_lock;
12 };
13 
14 int gro_cells_receive(struct gro_cells *gcells, struct sk_buff *skb)
15 {
16 	struct net_device *dev = skb->dev;
17 	enum skb_drop_reason reason;
18 	bool have_bh_lock = false;
19 	struct gro_cell *cell;
20 	int res;
21 
22 	rcu_read_lock();
23 	reason = SKB_DROP_REASON_DEV_READY;
24 	if (unlikely(!(dev->flags & IFF_UP)))
25 		goto drop;
26 
27 	skb_unset_transport_header(skb);
28 
29 	if (!gcells->cells || skb_cloned(skb) || netif_elide_gro(dev)) {
30 		res = netif_rx(skb);
31 		goto unlock;
32 	}
33 
34 	local_lock_nested_bh(&gcells->cells->bh_lock);
35 	have_bh_lock = true;
36 	cell = this_cpu_ptr(gcells->cells);
37 
38 	if (skb_queue_len(&cell->napi_skbs) > READ_ONCE(net_hotdata.max_backlog)) {
39 		reason = SKB_DROP_REASON_CPU_BACKLOG;
40 drop:
41 		dev_core_stats_rx_dropped_inc(dev);
42 		kfree_skb_reason(skb, reason);
43 		res = NET_RX_DROP;
44 		goto unlock;
45 	}
46 
47 	__skb_queue_tail(&cell->napi_skbs, skb);
48 	if (skb_queue_len(&cell->napi_skbs) == 1)
49 		napi_schedule(&cell->napi);
50 
51 	res = NET_RX_SUCCESS;
52 
53 unlock:
54 	if (have_bh_lock)
55 		local_unlock_nested_bh(&gcells->cells->bh_lock);
56 	rcu_read_unlock();
57 	return res;
58 }
59 EXPORT_SYMBOL(gro_cells_receive);
60 
61 /* called under BH context */
62 static int gro_cell_poll(struct napi_struct *napi, int budget)
63 {
64 	struct gro_cell *cell = container_of(napi, struct gro_cell, napi);
65 	struct sk_buff *skb;
66 	int work_done = 0;
67 
68 	while (work_done < budget) {
69 		__local_lock_nested_bh(&cell->bh_lock);
70 		skb = __skb_dequeue(&cell->napi_skbs);
71 		__local_unlock_nested_bh(&cell->bh_lock);
72 		if (!skb)
73 			break;
74 		napi_gro_receive(napi, skb);
75 		work_done++;
76 	}
77 
78 	if (work_done < budget)
79 		napi_complete_done(napi, work_done);
80 	return work_done;
81 }
82 
83 int gro_cells_init(struct gro_cells *gcells, struct net_device *dev)
84 {
85 	int i;
86 
87 	gcells->cells = alloc_percpu(struct gro_cell);
88 	if (!gcells->cells)
89 		return -ENOMEM;
90 
91 	for_each_possible_cpu(i) {
92 		struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
93 
94 		__skb_queue_head_init(&cell->napi_skbs);
95 		local_lock_init(&cell->bh_lock);
96 
97 		set_bit(NAPI_STATE_NO_BUSY_POLL, &cell->napi.state);
98 		set_bit(NAPI_STATE_PERCPU, &cell->napi.state);
99 
100 		netif_napi_add(dev, &cell->napi, gro_cell_poll);
101 		napi_enable(&cell->napi);
102 	}
103 	return 0;
104 }
105 EXPORT_SYMBOL(gro_cells_init);
106 
107 struct percpu_free_defer {
108 	struct rcu_head rcu;
109 	void __percpu	*ptr;
110 };
111 
112 static void percpu_free_defer_callback(struct rcu_head *head)
113 {
114 	struct percpu_free_defer *defer;
115 
116 	defer = container_of(head, struct percpu_free_defer, rcu);
117 	free_percpu(defer->ptr);
118 	kfree(defer);
119 }
120 
121 void gro_cells_destroy(struct gro_cells *gcells)
122 {
123 	struct percpu_free_defer *defer;
124 	int i;
125 
126 	if (!gcells->cells)
127 		return;
128 	for_each_possible_cpu(i) {
129 		struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
130 
131 		napi_disable(&cell->napi);
132 		__netif_napi_del(&cell->napi);
133 		__skb_queue_purge(&cell->napi_skbs);
134 	}
135 	/* We need to observe an rcu grace period before freeing ->cells,
136 	 * because netpoll could access dev->napi_list under rcu protection.
137 	 * Try hard using call_rcu() instead of synchronize_rcu(),
138 	 * because we might be called from cleanup_net(), and we
139 	 * definitely do not want to block this critical task.
140 	 */
141 	defer = kmalloc_obj(*defer, GFP_KERNEL | __GFP_NOWARN);
142 	if (likely(defer)) {
143 		defer->ptr = gcells->cells;
144 		call_rcu(&defer->rcu, percpu_free_defer_callback);
145 	} else {
146 		/* We do not hold RTNL at this point, synchronize_net()
147 		 * would not be able to expedite this sync.
148 		 */
149 		synchronize_rcu_expedited();
150 		free_percpu(gcells->cells);
151 	}
152 	gcells->cells = NULL;
153 }
154 EXPORT_SYMBOL(gro_cells_destroy);
155