1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Wed, 25 Nov 2020 18:10:34 +0100
3 Subject: [PATCH] net/fq_impl: do not maintain a backlog-sorted list of
6 A sorted flow list is only needed to drop packets in the biggest flow when
7 hitting the overmemory condition.
8 By scanning flows only when needed, we can avoid paying the cost of
9 maintaining the list under normal conditions
10 In order to avoid scanning lots of empty flows and touching too many cold
11 cache lines, a bitmap of flows with backlog is maintained
13 Signed-off-by: Felix Fietkau <nbd@nbd.name>
16 --- a/include/net/fq.h
17 +++ b/include/net/fq.h
18 @@ -19,8 +19,6 @@ struct fq_tin;
19 * @flowchain: can be linked to fq_tin's new_flows or old_flows. Used for DRR++
20 * (deficit round robin) based round robin queuing similar to the one
21 * found in net/sched/sch_fq_codel.c
22 - * @backlogchain: can be linked to other fq_flow and fq. Used to keep track of
23 - * fat flows and efficient head-dropping if packet limit is reached
24 * @queue: sk_buff queue to hold packets
25 * @backlog: number of bytes pending in the queue. The number of packets can be
26 * found in @queue.qlen
27 @@ -29,7 +27,6 @@ struct fq_tin;
30 struct list_head flowchain;
31 - struct list_head backlogchain;
32 struct sk_buff_head queue;
35 @@ -47,6 +44,7 @@ struct fq_flow {
37 struct list_head new_flows;
38 struct list_head old_flows;
39 + struct list_head tin_list;
40 struct fq_flow default_flow;
43 @@ -60,14 +58,14 @@ struct fq_tin {
45 * struct fq - main container for fair queuing purposes
47 - * @backlogs: linked to fq_flows. Used to maintain fat flows for efficient
48 - * head-dropping when @backlog reaches @limit
49 * @limit: max number of packets that can be queued across all flows
50 * @backlog: number of packets queued across all flows
53 struct fq_flow *flows;
54 - struct list_head backlogs;
55 + unsigned long *flows_bitmap;
57 + struct list_head tin_backlog;
61 --- a/include/net/fq_impl.h
62 +++ b/include/net/fq_impl.h
63 @@ -17,12 +17,24 @@ __fq_adjust_removal(struct fq *fq, struc
64 unsigned int bytes, unsigned int truesize)
66 struct fq_tin *tin = flow->tin;
69 tin->backlog_bytes -= bytes;
70 tin->backlog_packets -= packets;
71 flow->backlog -= bytes;
72 fq->backlog -= packets;
73 fq->memory_usage -= truesize;
78 + if (flow == &tin->default_flow) {
79 + list_del_init(&tin->tin_list);
83 + idx = flow - fq->flows;
84 + __clear_bit(idx, fq->flows_bitmap);
87 static void fq_adjust_removal(struct fq *fq,
88 @@ -32,24 +44,6 @@ static void fq_adjust_removal(struct fq
89 __fq_adjust_removal(fq, flow, 1, skb->len, skb->truesize);
92 -static void fq_rejigger_backlog(struct fq *fq, struct fq_flow *flow)
96 - if (flow->backlog == 0) {
97 - list_del_init(&flow->backlogchain);
101 - list_for_each_entry_continue(i, &fq->backlogs, backlogchain)
102 - if (i->backlog < flow->backlog)
105 - list_move_tail(&flow->backlogchain,
110 static struct sk_buff *fq_flow_dequeue(struct fq *fq,
111 struct fq_flow *flow)
113 @@ -62,7 +56,6 @@ static struct sk_buff *fq_flow_dequeue(s
116 fq_adjust_removal(fq, flow, skb);
117 - fq_rejigger_backlog(fq, flow);
121 @@ -90,7 +83,6 @@ static int fq_flow_drop(struct fq *fq, s
122 } while (packets < pending);
124 __fq_adjust_removal(fq, flow, packets, bytes, truesize);
125 - fq_rejigger_backlog(fq, flow);
129 @@ -170,22 +162,36 @@ static struct fq_flow *fq_flow_classify(
133 -static void fq_recalc_backlog(struct fq *fq,
134 - struct fq_tin *tin,
135 - struct fq_flow *flow)
136 +static struct fq_flow *fq_find_fattest_flow(struct fq *fq)
139 + struct fq_tin *tin;
140 + struct fq_flow *flow = NULL;
144 - if (list_empty(&flow->backlogchain))
145 - list_add_tail(&flow->backlogchain, &fq->backlogs);
146 + for_each_set_bit(i, fq->flows_bitmap, fq->flows_cnt) {
147 + struct fq_flow *cur = &fq->flows[i];
148 + unsigned int cur_len;
151 - list_for_each_entry_continue_reverse(i, &fq->backlogs,
153 - if (i->backlog > flow->backlog)
155 + cur_len = cur->backlog;
156 + if (cur_len <= len)
159 - list_move(&flow->backlogchain, &i->backlogchain);
164 + list_for_each_entry(tin, &fq->tin_backlog, tin_list) {
165 + unsigned int cur_len = tin->default_flow.backlog;
167 + if (cur_len <= len)
170 + flow = &tin->default_flow;
177 static void fq_tin_enqueue(struct fq *fq,
178 @@ -200,6 +206,13 @@ static void fq_tin_enqueue(struct fq *fq
180 flow = fq_flow_classify(fq, tin, idx, skb);
182 + if (!flow->backlog) {
183 + if (flow != &tin->default_flow)
184 + __set_bit(idx, fq->flows_bitmap);
185 + else if (list_empty(&tin->tin_list))
186 + list_add(&tin->tin_list, &fq->tin_backlog);
190 flow->backlog += skb->len;
191 tin->backlog_bytes += skb->len;
192 @@ -207,8 +220,6 @@ static void fq_tin_enqueue(struct fq *fq
193 fq->memory_usage += skb->truesize;
196 - fq_recalc_backlog(fq, tin, flow);
198 if (list_empty(&flow->flowchain)) {
199 flow->deficit = fq->quantum;
200 list_add_tail(&flow->flowchain,
201 @@ -218,9 +229,7 @@ static void fq_tin_enqueue(struct fq *fq
202 __skb_queue_tail(&flow->queue, skb);
203 oom = (fq->memory_usage > fq->memory_limit);
204 while (fq->backlog > fq->limit || oom) {
205 - flow = list_first_entry_or_null(&fq->backlogs,
208 + flow = fq_find_fattest_flow(fq);
212 @@ -255,8 +264,6 @@ static void fq_flow_filter(struct fq *fq
213 fq_adjust_removal(fq, flow, skb);
214 free_func(fq, tin, flow, skb);
217 - fq_rejigger_backlog(fq, flow);
220 static void fq_tin_filter(struct fq *fq,
221 @@ -279,16 +286,18 @@ static void fq_flow_reset(struct fq *fq,
222 struct fq_flow *flow,
223 fq_skb_free_t free_func)
225 + struct fq_tin *tin = flow->tin;
228 while ((skb = fq_flow_dequeue(fq, flow)))
229 - free_func(fq, flow->tin, flow, skb);
230 + free_func(fq, tin, flow, skb);
232 - if (!list_empty(&flow->flowchain))
233 + if (!list_empty(&flow->flowchain)) {
234 list_del_init(&flow->flowchain);
236 - if (!list_empty(&flow->backlogchain))
237 - list_del_init(&flow->backlogchain);
238 + if (list_empty(&tin->new_flows) &&
239 + list_empty(&tin->old_flows))
240 + list_del_init(&tin->tin_list);
245 @@ -314,6 +323,7 @@ static void fq_tin_reset(struct fq *fq,
246 fq_flow_reset(fq, flow, free_func);
249 + WARN_ON_ONCE(!list_empty(&tin->tin_list));
250 WARN_ON_ONCE(tin->backlog_bytes);
251 WARN_ON_ONCE(tin->backlog_packets);
253 @@ -321,7 +331,6 @@ static void fq_tin_reset(struct fq *fq,
254 static void fq_flow_init(struct fq_flow *flow)
256 INIT_LIST_HEAD(&flow->flowchain);
257 - INIT_LIST_HEAD(&flow->backlogchain);
258 __skb_queue_head_init(&flow->queue);
261 @@ -329,6 +338,7 @@ static void fq_tin_init(struct fq_tin *t
263 INIT_LIST_HEAD(&tin->new_flows);
264 INIT_LIST_HEAD(&tin->old_flows);
265 + INIT_LIST_HEAD(&tin->tin_list);
266 fq_flow_init(&tin->default_flow);
269 @@ -337,8 +347,8 @@ static int fq_init(struct fq *fq, int fl
272 memset(fq, 0, sizeof(fq[0]));
273 - INIT_LIST_HEAD(&fq->backlogs);
274 spin_lock_init(&fq->lock);
275 + INIT_LIST_HEAD(&fq->tin_backlog);
276 fq->flows_cnt = max_t(u32, flows_cnt, 1);
279 @@ -348,6 +358,14 @@ static int fq_init(struct fq *fq, int fl
283 + fq->flows_bitmap = kcalloc(BITS_TO_LONGS(fq->flows_cnt), sizeof(long),
285 + if (!fq->flows_bitmap) {
291 for (i = 0; i < fq->flows_cnt; i++)
292 fq_flow_init(&fq->flows[i]);
294 @@ -364,6 +382,9 @@ static void fq_reset(struct fq *fq,
299 + kfree(fq->flows_bitmap);
300 + fq->flows_bitmap = NULL;
304 --- a/net/mac80211/tx.c
305 +++ b/net/mac80211/tx.c
306 @@ -3364,8 +3364,6 @@ out_recalc:
307 if (head->len != orig_len) {
308 flow->backlog += head->len - orig_len;
309 tin->backlog_bytes += head->len - orig_len;
311 - fq_recalc_backlog(fq, tin, flow);
314 spin_unlock_bh(&fq->lock);