Skip to content

Commit 7736182

Browse files
netoptimizerAlexei Starovoitov
authored andcommitted
bpf: cpumap use ptr_ring_consume_batched
Move ptr_ring dequeue outside loop, that allocate SKBs and calls network stack, as these operations that can take some time. The ptr_ring is a communication channel between CPUs, where we want to reduce/limit any cacheline bouncing. Do a concentrated bulk dequeue via ptr_ring_consume_batched, to shorten the period and times the remote cacheline in ptr_ring is read Batch size 8 is both to (1) limit BH-disable period, and (2) consume one cacheline on 64-bit archs. After reducing the BH-disable section further then we can consider changing this, while still thinking about L1 cacheline size being active. Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com> Acked-by: Song Liu <songliubraving@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 00967e8 commit 7736182

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

‎kernel/bpf/cpumap.c‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ static void put_cpu_map_entry(struct bpf_cpu_map_entry *rcpu)
240240
}
241241
}
242242

243+
#define CPUMAP_BATCH 8
244+
243245
static int cpu_map_kthread_run(void *data)
244246
{
245247
struct bpf_cpu_map_entry *rcpu = data;
@@ -252,8 +254,9 @@ static int cpu_map_kthread_run(void *data)
252254
* kthread_stop signal until queue is empty.
253255
*/
254256
while (!kthread_should_stop() || !__ptr_ring_empty(rcpu->queue)) {
255-
unsigned int processed = 0, drops = 0, sched = 0;
256-
struct xdp_frame *xdpf;
257+
unsigned int drops = 0, sched = 0;
258+
void *frames[CPUMAP_BATCH];
259+
int i, n;
257260

258261
/* Release CPU reschedule checks */
259262
if (__ptr_ring_empty(rcpu->queue)) {
@@ -269,14 +272,16 @@ static int cpu_map_kthread_run(void *data)
269272
sched = cond_resched();
270273
}
271274

272-
/* Process packets in rcpu->queue */
273-
local_bh_disable();
274275
/*
275276
* The bpf_cpu_map_entry is single consumer, with this
276277
* kthread CPU pinned. Lockless access to ptr_ring
277278
* consume side valid as no-resize allowed of queue.
278279
*/
279-
while ((xdpf = __ptr_ring_consume(rcpu->queue))) {
280+
n = ptr_ring_consume_batched(rcpu->queue, frames, CPUMAP_BATCH);
281+
282+
local_bh_disable();
283+
for (i = 0; i < n; i++) {
284+
struct xdp_frame *xdpf = frames[i];
280285
struct sk_buff *skb;
281286
int ret;
282287

@@ -290,13 +295,9 @@ static int cpu_map_kthread_run(void *data)
290295
ret = netif_receive_skb_core(skb);
291296
if (ret == NET_RX_DROP)
292297
drops++;
293-
294-
/* Limit BH-disable period */
295-
if (++processed == 8)
296-
break;
297298
}
298299
/* Feedback loop via tracepoint */
299-
trace_xdp_cpumap_kthread(rcpu->map_id, processed, drops, sched);
300+
trace_xdp_cpumap_kthread(rcpu->map_id, n, drops, sched);
300301

301302
local_bh_enable(); /* resched point, may call do_softirq() */
302303
}

0 commit comments

Comments
 (0)