xref: /linux/net/core/gro_cells.c (revision 42a9fb3382fc2573e92f41d203b095d9a372cfc9)
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 
99 		netif_napi_add(dev, &cell->napi, gro_cell_poll);
100 		napi_enable(&cell->napi);
101 	}
102 	return 0;
103 }
104 EXPORT_SYMBOL(gro_cells_init);
105 
106 struct percpu_free_defer {
107 	struct rcu_head rcu;
108 	void __percpu	*ptr;
109 };
110 
111 static void percpu_free_defer_callback(struct rcu_head *head)
112 {
113 	struct percpu_free_defer *defer;
114 
115 	defer = container_of(head, struct percpu_free_defer, rcu);
116 	free_percpu(defer->ptr);
117 	kfree(defer);
118 }
119 
120 void gro_cells_destroy(struct gro_cells *gcells)
121 {
122 	struct percpu_free_defer *defer;
123 	int i;
124 
125 	if (!gcells->cells)
126 		return;
127 	for_each_possible_cpu(i) {
128 		struct gro_cell *cell = per_cpu_ptr(gcells->cells, i);
129 
130 		napi_disable(&cell->napi);
131 		__netif_napi_del(&cell->napi);
132 		__skb_queue_purge(&cell->napi_skbs);
133 	}
134 	/* We need to observe an rcu grace period before freeing ->cells,
135 	 * because netpoll could access dev->napi_list under rcu protection.
136 	 * Try hard using call_rcu() instead of synchronize_rcu(),
137 	 * because we might be called from cleanup_net(), and we
138 	 * definitely do not want to block this critical task.
139 	 */
140 	defer = kmalloc_obj(*defer, GFP_KERNEL | __GFP_NOWARN);
141 	if (likely(defer)) {
142 		defer->ptr = gcells->cells;
143 		call_rcu(&defer->rcu, percpu_free_defer_callback);
144 	} else {
145 		/* We do not hold RTNL at this point, synchronize_net()
146 		 * would not be able to expedite this sync.
147 		 */
148 		synchronize_rcu_expedited();
149 		free_percpu(gcells->cells);
150 	}
151 	gcells->cells = NULL;
152 }
153 EXPORT_SYMBOL(gro_cells_destroy);
154