xref: /linux/kernel/bpf/cpumap.c (revision a075082a15e7f5c4889d0cbb51a4041c332cb00c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* bpf/cpumap.c
3  *
4  * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc.
5  */
6 
7 /**
8  * DOC: cpu map
9  * The 'cpumap' is primarily used as a backend map for XDP BPF helper
10  * call bpf_redirect_map() and XDP_REDIRECT action, like 'devmap'.
11  *
12  * Unlike devmap which redirects XDP frames out to another NIC device,
13  * this map type redirects raw XDP frames to another CPU.  The remote
14  * CPU will do SKB-allocation and call the normal network stack.
15  */
16 /*
17  * This is a scalability and isolation mechanism, that allow
18  * separating the early driver network XDP layer, from the rest of the
19  * netstack, and assigning dedicated CPUs for this stage.  This
20  * basically allows for 10G wirespeed pre-filtering via bpf.
21  */
22 #include <linux/bitops.h>
23 #include <linux/bpf.h>
24 #include <linux/filter.h>
25 #include <linux/ptr_ring.h>
26 #include <net/xdp.h>
27 #include <net/hotdata.h>
28 
29 #include <linux/sched.h>
30 #include <linux/workqueue.h>
31 #include <linux/kthread.h>
32 #include <linux/local_lock.h>
33 #include <linux/completion.h>
34 #include <trace/events/xdp.h>
35 #include <linux/btf_ids.h>
36 
37 #include <linux/netdevice.h>
38 #include <net/gro.h>
39 
40 /* General idea: XDP packets getting XDP redirected to another CPU,
41  * will maximum be stored/queued for one driver ->poll() call.  It is
42  * guaranteed that queueing the frame and the flush operation happen on
43  * same CPU.  Thus, cpu_map_flush operation can deduct via this_cpu_ptr()
44  * which queue in bpf_cpu_map_entry contains packets.
45  */
46 
47 #define CPU_MAP_BULK_SIZE 8  /* 8 == one cacheline on 64-bit archs */
48 struct bpf_cpu_map_entry;
49 struct bpf_cpu_map;
50 
51 struct xdp_bulk_queue {
52 	void *q[CPU_MAP_BULK_SIZE];
53 	struct list_head flush_node;
54 	struct bpf_cpu_map_entry *obj;
55 	unsigned int count;
56 	local_lock_t bq_lock;
57 };
58 
59 /* Struct for every remote "destination" CPU in map */
60 struct bpf_cpu_map_entry {
61 	u32 cpu;    /* kthread CPU and map index */
62 	int map_id; /* Back reference to map */
63 
64 	/* XDP can run multiple RX-ring queues, need __percpu enqueue store */
65 	struct xdp_bulk_queue __percpu *bulkq;
66 
67 	/* Queue with potential multi-producers, and single-consumer kthread */
68 	struct ptr_ring *queue;
69 	struct task_struct *kthread;
70 
71 	struct bpf_cpumap_val value;
72 	struct bpf_prog *prog;
73 	struct gro_node gro;
74 
75 	struct completion kthread_running;
76 	struct rcu_work free_work;
77 };
78 
79 struct bpf_cpu_map {
80 	struct bpf_map map;
81 	/* Below members specific for map type */
82 	struct bpf_cpu_map_entry __rcu **cpu_map;
83 };
84 
85 static struct bpf_map *cpu_map_alloc(union bpf_attr *attr)
86 {
87 	u32 value_size = attr->value_size;
88 	struct bpf_cpu_map *cmap;
89 
90 	/* check sanity of attributes */
91 	if (attr->max_entries == 0 || attr->key_size != 4 ||
92 	    (value_size != offsetofend(struct bpf_cpumap_val, qsize) &&
93 	     value_size != offsetofend(struct bpf_cpumap_val, bpf_prog.fd)) ||
94 	    attr->map_flags & ~BPF_F_NUMA_NODE)
95 		return ERR_PTR(-EINVAL);
96 
97 	/* Pre-limit array size based on NR_CPUS, not final CPU check */
98 	if (attr->max_entries > NR_CPUS)
99 		return ERR_PTR(-E2BIG);
100 
101 	cmap = bpf_map_area_alloc(sizeof(*cmap), NUMA_NO_NODE);
102 	if (!cmap)
103 		return ERR_PTR(-ENOMEM);
104 
105 	bpf_map_init_from_attr(&cmap->map, attr);
106 
107 	/* Alloc array for possible remote "destination" CPUs */
108 	cmap->cpu_map = bpf_map_area_alloc(cmap->map.max_entries *
109 					   sizeof(struct bpf_cpu_map_entry *),
110 					   cmap->map.numa_node);
111 	if (!cmap->cpu_map) {
112 		bpf_map_area_free(cmap);
113 		return ERR_PTR(-ENOMEM);
114 	}
115 
116 	return &cmap->map;
117 }
118 
119 static void __cpu_map_ring_cleanup(struct ptr_ring *ring)
120 {
121 	/* The tear-down procedure should have made sure that queue is
122 	 * empty.  See __cpu_map_entry_replace() and work-queue
123 	 * invoked cpu_map_kthread_stop(). Catch any broken behaviour
124 	 * gracefully and warn once.
125 	 */
126 	void *ptr;
127 
128 	while ((ptr = ptr_ring_consume(ring))) {
129 		WARN_ON_ONCE(1);
130 		if (unlikely(__ptr_test_bit(0, &ptr))) {
131 			__ptr_clear_bit(0, &ptr);
132 			kfree_skb(ptr);
133 			continue;
134 		}
135 		xdp_return_frame(ptr);
136 	}
137 }
138 
139 static u32 cpu_map_bpf_prog_run_skb(struct bpf_cpu_map_entry *rcpu,
140 				    void **skbs, u32 skb_n,
141 				    struct xdp_cpumap_stats *stats)
142 {
143 	struct xdp_buff xdp;
144 	u32 act, pass = 0;
145 	int err;
146 
147 	for (u32 i = 0; i < skb_n; i++) {
148 		struct sk_buff *skb = skbs[i];
149 
150 		act = bpf_prog_run_generic_xdp(skb, &xdp, rcpu->prog);
151 		switch (act) {
152 		case XDP_PASS:
153 			skbs[pass++] = skb;
154 			break;
155 		case XDP_REDIRECT:
156 			err = xdp_do_generic_redirect(skb->dev, skb, &xdp,
157 						      rcpu->prog);
158 			if (unlikely(err)) {
159 				kfree_skb(skb);
160 				stats->drop++;
161 			} else {
162 				stats->redirect++;
163 			}
164 			break;
165 		default:
166 			bpf_warn_invalid_xdp_action(NULL, rcpu->prog, act);
167 			fallthrough;
168 		case XDP_ABORTED:
169 			trace_xdp_exception(skb->dev, rcpu->prog, act);
170 			fallthrough;
171 		case XDP_DROP:
172 			napi_consume_skb(skb, true);
173 			stats->drop++;
174 			break;
175 		}
176 	}
177 
178 	stats->pass += pass;
179 
180 	return pass;
181 }
182 
183 static int cpu_map_bpf_prog_run_xdp(struct bpf_cpu_map_entry *rcpu,
184 				    void **frames, int n,
185 				    struct xdp_cpumap_stats *stats)
186 {
187 	struct xdp_rxq_info rxq = {};
188 	struct xdp_buff xdp;
189 	int i, nframes = 0;
190 
191 	xdp.rxq = &rxq;
192 
193 	for (i = 0; i < n; i++) {
194 		struct xdp_frame *xdpf = frames[i];
195 		u32 act;
196 		int err;
197 
198 		rxq.dev = xdpf->dev_rx;
199 		rxq.mem.type = xdpf->mem_type;
200 		/* TODO: report queue_index to xdp_rxq_info */
201 
202 		xdp_convert_frame_to_buff(xdpf, &xdp);
203 
204 		act = bpf_prog_run_xdp(rcpu->prog, &xdp);
205 		switch (act) {
206 		case XDP_PASS:
207 			err = xdp_update_frame_from_buff(&xdp, xdpf);
208 			if (err < 0) {
209 				xdp_return_frame(xdpf);
210 				stats->drop++;
211 			} else {
212 				frames[nframes++] = xdpf;
213 			}
214 			break;
215 		case XDP_REDIRECT:
216 			err = xdp_do_redirect(xdpf->dev_rx, &xdp,
217 					      rcpu->prog);
218 			if (unlikely(err)) {
219 				xdp_return_frame(xdpf);
220 				stats->drop++;
221 			} else {
222 				stats->redirect++;
223 			}
224 			break;
225 		default:
226 			bpf_warn_invalid_xdp_action(NULL, rcpu->prog, act);
227 			fallthrough;
228 		case XDP_DROP:
229 			xdp_return_frame(xdpf);
230 			stats->drop++;
231 			break;
232 		}
233 	}
234 
235 	stats->pass += nframes;
236 
237 	return nframes;
238 }
239 
240 #define CPUMAP_BATCH 8
241 
242 struct cpu_map_ret {
243 	u32 xdp_n;
244 	u32 skb_n;
245 };
246 
247 static void cpu_map_bpf_prog_run(struct bpf_cpu_map_entry *rcpu, void **frames,
248 				 void **skbs, struct cpu_map_ret *ret,
249 				 struct xdp_cpumap_stats *stats)
250 {
251 	struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
252 
253 	if (!rcpu->prog)
254 		goto out;
255 
256 	rcu_read_lock();
257 	bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
258 	xdp_set_return_frame_no_direct();
259 
260 	ret->xdp_n = cpu_map_bpf_prog_run_xdp(rcpu, frames, ret->xdp_n, stats);
261 	if (unlikely(ret->skb_n))
262 		ret->skb_n = cpu_map_bpf_prog_run_skb(rcpu, skbs, ret->skb_n,
263 						      stats);
264 
265 	if (stats->redirect)
266 		xdp_do_flush();
267 
268 	xdp_clear_return_frame_no_direct();
269 	bpf_net_ctx_clear(bpf_net_ctx);
270 	rcu_read_unlock();
271 
272 out:
273 	if (unlikely(ret->skb_n) && ret->xdp_n)
274 		memmove(&skbs[ret->xdp_n], skbs, ret->skb_n * sizeof(*skbs));
275 }
276 
277 static void cpu_map_gro_flush(struct bpf_cpu_map_entry *rcpu, bool empty)
278 {
279 	/*
280 	 * If the ring is not empty, there'll be a new iteration soon, and we
281 	 * only need to do a full flush if a tick is long (> 1 ms).
282 	 * If the ring is empty, to not hold GRO packets in the stack for too
283 	 * long, do a full flush.
284 	 * This is equivalent to how NAPI decides whether to perform a full
285 	 * flush.
286 	 */
287 	gro_flush_normal(&rcpu->gro, !empty && HZ >= 1000);
288 }
289 
290 static int cpu_map_kthread_run(void *data)
291 {
292 	struct bpf_cpu_map_entry *rcpu = data;
293 	unsigned long last_qs = jiffies;
294 	u32 packets = 0;
295 
296 	complete(&rcpu->kthread_running);
297 	set_current_state(TASK_INTERRUPTIBLE);
298 
299 	/* When kthread gives stop order, then rcpu have been disconnected
300 	 * from map, thus no new packets can enter. Remaining in-flight
301 	 * per CPU stored packets are flushed to this queue.  Wait honoring
302 	 * kthread_stop signal until queue is empty.
303 	 */
304 	while (!kthread_should_stop() || !__ptr_ring_empty(rcpu->queue)) {
305 		struct xdp_cpumap_stats stats = {}; /* zero stats */
306 		unsigned int kmem_alloc_drops = 0, sched = 0;
307 		struct cpu_map_ret ret = { };
308 		void *frames[CPUMAP_BATCH];
309 		void *skbs[CPUMAP_BATCH];
310 		u32 i, n, m;
311 		bool empty;
312 
313 		/* Release CPU reschedule checks */
314 		if (__ptr_ring_empty(rcpu->queue)) {
315 			set_current_state(TASK_INTERRUPTIBLE);
316 			/* Recheck to avoid lost wake-up */
317 			if (__ptr_ring_empty(rcpu->queue)) {
318 				schedule();
319 				sched = 1;
320 				last_qs = jiffies;
321 			} else {
322 				__set_current_state(TASK_RUNNING);
323 			}
324 		} else {
325 			rcu_softirq_qs_periodic(last_qs);
326 			sched = cond_resched();
327 		}
328 
329 		/*
330 		 * The bpf_cpu_map_entry is single consumer, with this
331 		 * kthread CPU pinned. Lockless access to ptr_ring
332 		 * consume side valid as no-resize allowed of queue.
333 		 */
334 		n = __ptr_ring_consume_batched(rcpu->queue, frames,
335 					       CPUMAP_BATCH);
336 		for (i = 0; i < n; i++) {
337 			void *f = frames[i];
338 			struct page *page;
339 
340 			if (unlikely(__ptr_test_bit(0, &f))) {
341 				struct sk_buff *skb = f;
342 
343 				__ptr_clear_bit(0, &skb);
344 				skbs[ret.skb_n++] = skb;
345 				continue;
346 			}
347 
348 			frames[ret.xdp_n++] = f;
349 			page = virt_to_page(f);
350 
351 			/* Bring struct page memory area to curr CPU. Read by
352 			 * build_skb_around via page_is_pfmemalloc(), and when
353 			 * freed written by page_frag_free call.
354 			 */
355 			prefetchw(page);
356 		}
357 
358 		local_bh_disable();
359 
360 		/* Support running another XDP prog on this CPU */
361 		cpu_map_bpf_prog_run(rcpu, frames, skbs, &ret, &stats);
362 		if (!ret.xdp_n)
363 			goto stats;
364 
365 		m = napi_skb_cache_get_bulk(skbs, ret.xdp_n);
366 		if (unlikely(m < ret.xdp_n)) {
367 			for (i = m; i < ret.xdp_n; i++)
368 				xdp_return_frame(frames[i]);
369 
370 			if (ret.skb_n)
371 				memmove(&skbs[m], &skbs[ret.xdp_n],
372 					ret.skb_n * sizeof(*skbs));
373 
374 			kmem_alloc_drops += ret.xdp_n - m;
375 			ret.xdp_n = m;
376 		}
377 
378 		for (i = 0; i < ret.xdp_n; i++) {
379 			struct xdp_frame *xdpf = frames[i];
380 
381 			/* Can fail only when !skb -- already handled above */
382 			__xdp_build_skb_from_frame(xdpf, skbs[i], xdpf->dev_rx);
383 		}
384 
385 stats:
386 		/* Feedback loop via tracepoint.
387 		 * NB: keep before recv to allow measuring enqueue/dequeue latency.
388 		 */
389 		trace_xdp_cpumap_kthread(rcpu->map_id, n, kmem_alloc_drops,
390 					 sched, &stats);
391 
392 		for (i = 0; i < ret.xdp_n + ret.skb_n; i++)
393 			gro_receive_skb(&rcpu->gro, skbs[i]);
394 
395 		/* Flush either every 64 packets or in case of empty ring */
396 		packets += n;
397 		empty = __ptr_ring_empty(rcpu->queue);
398 		if (packets >= NAPI_POLL_WEIGHT || empty) {
399 			cpu_map_gro_flush(rcpu, empty);
400 			packets = 0;
401 		}
402 
403 		local_bh_enable(); /* resched point, may call do_softirq() */
404 	}
405 	__set_current_state(TASK_RUNNING);
406 
407 	return 0;
408 }
409 
410 static int __cpu_map_load_bpf_program(struct bpf_cpu_map_entry *rcpu,
411 				      struct bpf_map *map, int fd)
412 {
413 	struct bpf_prog *prog;
414 
415 	prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_XDP);
416 	if (IS_ERR(prog))
417 		return PTR_ERR(prog);
418 
419 	if (prog->expected_attach_type != BPF_XDP_CPUMAP ||
420 	    !bpf_prog_map_compatible(map, prog)) {
421 		bpf_prog_put(prog);
422 		return -EINVAL;
423 	}
424 
425 	rcpu->value.bpf_prog.id = prog->aux->id;
426 	rcpu->prog = prog;
427 
428 	return 0;
429 }
430 
431 static struct bpf_cpu_map_entry *
432 __cpu_map_entry_alloc(struct bpf_map *map, struct bpf_cpumap_val *value,
433 		      u32 cpu)
434 {
435 	int numa, err = -ENOMEM, i, fd = value->bpf_prog.fd;
436 	gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
437 	struct bpf_cpu_map_entry *rcpu;
438 	struct xdp_bulk_queue *bq;
439 
440 	/* Have map->numa_node, but choose node of redirect target CPU */
441 	numa = cpu_to_node(cpu);
442 
443 	rcpu = bpf_map_kmalloc_node(map, sizeof(*rcpu), gfp | __GFP_ZERO, numa);
444 	if (!rcpu)
445 		return ERR_PTR(err);
446 
447 	/* Alloc percpu bulkq */
448 	rcpu->bulkq = bpf_map_alloc_percpu(map, sizeof(*rcpu->bulkq),
449 					   sizeof(void *), gfp);
450 	if (!rcpu->bulkq)
451 		goto free_rcu;
452 
453 	for_each_possible_cpu(i) {
454 		bq = per_cpu_ptr(rcpu->bulkq, i);
455 		bq->obj = rcpu;
456 		local_lock_init(&bq->bq_lock);
457 	}
458 
459 	/* Alloc queue */
460 	rcpu->queue = bpf_map_kmalloc_node(map, sizeof(*rcpu->queue), gfp,
461 					   numa);
462 	if (!rcpu->queue)
463 		goto free_bulkq;
464 
465 	err = ptr_ring_init(rcpu->queue, value->qsize, gfp);
466 	if (err)
467 		goto free_queue;
468 
469 	rcpu->cpu    = cpu;
470 	rcpu->map_id = map->id;
471 	rcpu->value.qsize  = value->qsize;
472 	gro_init(&rcpu->gro);
473 
474 	if (fd > 0) {
475 		err = __cpu_map_load_bpf_program(rcpu, map, fd);
476 		if (err)
477 			goto free_ptr_ring;
478 	}
479 
480 	/* Setup kthread */
481 	init_completion(&rcpu->kthread_running);
482 	rcpu->kthread = kthread_create_on_node(cpu_map_kthread_run, rcpu, numa,
483 					       "cpumap/%d/map:%d", cpu,
484 					       map->id);
485 	if (IS_ERR(rcpu->kthread)) {
486 		err = PTR_ERR(rcpu->kthread);
487 		goto free_prog;
488 	}
489 
490 	/* Make sure kthread runs on a single CPU */
491 	kthread_bind(rcpu->kthread, cpu);
492 	wake_up_process(rcpu->kthread);
493 
494 	/* Make sure kthread has been running, so kthread_stop() will not
495 	 * stop the kthread prematurely and all pending frames or skbs
496 	 * will be handled by the kthread before kthread_stop() returns.
497 	 */
498 	wait_for_completion(&rcpu->kthread_running);
499 
500 	return rcpu;
501 
502 free_prog:
503 	if (rcpu->prog)
504 		bpf_prog_put(rcpu->prog);
505 free_ptr_ring:
506 	gro_cleanup(&rcpu->gro);
507 	ptr_ring_cleanup(rcpu->queue, NULL);
508 free_queue:
509 	kfree(rcpu->queue);
510 free_bulkq:
511 	free_percpu(rcpu->bulkq);
512 free_rcu:
513 	kfree(rcpu);
514 	return ERR_PTR(err);
515 }
516 
517 static void __cpu_map_entry_free(struct work_struct *work)
518 {
519 	struct bpf_cpu_map_entry *rcpu;
520 
521 	/* This cpu_map_entry have been disconnected from map and one
522 	 * RCU grace-period have elapsed. Thus, XDP cannot queue any
523 	 * new packets and cannot change/set flush_needed that can
524 	 * find this entry.
525 	 */
526 	rcpu = container_of(to_rcu_work(work), struct bpf_cpu_map_entry, free_work);
527 
528 	/* kthread_stop will wake_up_process and wait for it to complete.
529 	 * cpu_map_kthread_run() makes sure the pointer ring is empty
530 	 * before exiting.
531 	 */
532 	kthread_stop(rcpu->kthread);
533 
534 	if (rcpu->prog)
535 		bpf_prog_put(rcpu->prog);
536 	gro_cleanup(&rcpu->gro);
537 	/* The queue should be empty at this point */
538 	__cpu_map_ring_cleanup(rcpu->queue);
539 	ptr_ring_cleanup(rcpu->queue, NULL);
540 	kfree(rcpu->queue);
541 	free_percpu(rcpu->bulkq);
542 	kfree(rcpu);
543 }
544 
545 /* After the xchg of the bpf_cpu_map_entry pointer, we need to make sure the old
546  * entry is no longer in use before freeing. We use queue_rcu_work() to call
547  * __cpu_map_entry_free() in a separate workqueue after waiting for an RCU grace
548  * period. This means that (a) all pending enqueue and flush operations have
549  * completed (because of the RCU callback), and (b) we are in a workqueue
550  * context where we can stop the kthread and wait for it to exit before freeing
551  * everything.
552  */
553 static void __cpu_map_entry_replace(struct bpf_cpu_map *cmap,
554 				    u32 key_cpu, struct bpf_cpu_map_entry *rcpu)
555 {
556 	struct bpf_cpu_map_entry *old_rcpu;
557 
558 	old_rcpu = unrcu_pointer(xchg(&cmap->cpu_map[key_cpu], RCU_INITIALIZER(rcpu)));
559 	if (old_rcpu) {
560 		INIT_RCU_WORK(&old_rcpu->free_work, __cpu_map_entry_free);
561 		queue_rcu_work(system_percpu_wq, &old_rcpu->free_work);
562 	}
563 }
564 
565 static long cpu_map_delete_elem(struct bpf_map *map, void *key)
566 {
567 	struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
568 	u32 key_cpu = *(u32 *)key;
569 
570 	if (key_cpu >= map->max_entries)
571 		return -EINVAL;
572 
573 	/* notice caller map_delete_elem() uses rcu_read_lock() */
574 	__cpu_map_entry_replace(cmap, key_cpu, NULL);
575 	return 0;
576 }
577 
578 static long cpu_map_update_elem(struct bpf_map *map, void *key, void *value,
579 				u64 map_flags)
580 {
581 	struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
582 	struct bpf_cpumap_val cpumap_value = {};
583 	struct bpf_cpu_map_entry *rcpu;
584 	/* Array index key correspond to CPU number */
585 	u32 key_cpu = *(u32 *)key;
586 
587 	memcpy(&cpumap_value, value, map->value_size);
588 
589 	if (unlikely(map_flags > BPF_EXIST))
590 		return -EINVAL;
591 	if (unlikely(key_cpu >= cmap->map.max_entries))
592 		return -E2BIG;
593 	if (unlikely(map_flags == BPF_NOEXIST))
594 		return -EEXIST;
595 	if (unlikely(cpumap_value.qsize > 16384)) /* sanity limit on qsize */
596 		return -EOVERFLOW;
597 
598 	/* Make sure CPU is a valid possible cpu */
599 	if (key_cpu >= nr_cpumask_bits || !cpu_possible(key_cpu))
600 		return -ENODEV;
601 
602 	if (cpumap_value.qsize == 0) {
603 		rcpu = NULL; /* Same as deleting */
604 	} else {
605 		/* Updating qsize cause re-allocation of bpf_cpu_map_entry */
606 		rcpu = __cpu_map_entry_alloc(map, &cpumap_value, key_cpu);
607 		if (IS_ERR(rcpu))
608 			return PTR_ERR(rcpu);
609 	}
610 	rcu_read_lock();
611 	__cpu_map_entry_replace(cmap, key_cpu, rcpu);
612 	rcu_read_unlock();
613 	return 0;
614 }
615 
616 static void cpu_map_free(struct bpf_map *map)
617 {
618 	struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
619 	u32 i;
620 
621 	/* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
622 	 * so the bpf programs (can be more than one that used this map) were
623 	 * disconnected from events. Wait for outstanding critical sections in
624 	 * these programs to complete. synchronize_rcu() below not only
625 	 * guarantees no further "XDP/bpf-side" reads against
626 	 * bpf_cpu_map->cpu_map, but also ensure pending flush operations
627 	 * (if any) are completed.
628 	 */
629 	synchronize_rcu();
630 
631 	/* The only possible user of bpf_cpu_map_entry is
632 	 * cpu_map_kthread_run().
633 	 */
634 	for (i = 0; i < cmap->map.max_entries; i++) {
635 		struct bpf_cpu_map_entry *rcpu;
636 
637 		rcpu = rcu_dereference_raw(cmap->cpu_map[i]);
638 		if (!rcpu)
639 			continue;
640 
641 		/* Stop kthread and cleanup entry directly */
642 		__cpu_map_entry_free(&rcpu->free_work.work);
643 	}
644 	bpf_map_area_free(cmap->cpu_map);
645 	bpf_map_area_free(cmap);
646 }
647 
648 /* Elements are kept alive by RCU; either by rcu_read_lock() (from syscall) or
649  * by local_bh_disable() (from XDP calls inside NAPI). The
650  * rcu_read_lock_bh_held() below makes lockdep accept both.
651  */
652 static void *__cpu_map_lookup_elem(struct bpf_map *map, u32 key)
653 {
654 	struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
655 	struct bpf_cpu_map_entry *rcpu;
656 
657 	if (key >= map->max_entries)
658 		return NULL;
659 
660 	rcpu = rcu_dereference_check(cmap->cpu_map[key],
661 				     rcu_read_lock_bh_held());
662 	return rcpu;
663 }
664 
665 static void *cpu_map_lookup_elem(struct bpf_map *map, void *key)
666 {
667 	struct bpf_cpu_map_entry *rcpu =
668 		__cpu_map_lookup_elem(map, *(u32 *)key);
669 
670 	return rcpu ? &rcpu->value : NULL;
671 }
672 
673 static int cpu_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
674 {
675 	struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map);
676 	u32 index = key ? *(u32 *)key : U32_MAX;
677 	u32 *next = next_key;
678 
679 	if (index >= cmap->map.max_entries) {
680 		*next = 0;
681 		return 0;
682 	}
683 
684 	if (index == cmap->map.max_entries - 1)
685 		return -ENOENT;
686 	*next = index + 1;
687 	return 0;
688 }
689 
690 static long cpu_map_redirect(struct bpf_map *map, u64 index, u64 flags)
691 {
692 	return __bpf_xdp_redirect_map(map, index, flags, 0,
693 				      __cpu_map_lookup_elem);
694 }
695 
696 static u64 cpu_map_mem_usage(const struct bpf_map *map)
697 {
698 	u64 usage = sizeof(struct bpf_cpu_map);
699 
700 	/* Currently the dynamically allocated elements are not counted */
701 	usage += (u64)map->max_entries * sizeof(struct bpf_cpu_map_entry *);
702 	return usage;
703 }
704 
705 BTF_ID_LIST_SINGLE(cpu_map_btf_ids, struct, bpf_cpu_map)
706 const struct bpf_map_ops cpu_map_ops = {
707 	.map_meta_equal		= bpf_map_meta_equal,
708 	.map_alloc		= cpu_map_alloc,
709 	.map_free		= cpu_map_free,
710 	.map_delete_elem	= cpu_map_delete_elem,
711 	.map_update_elem	= cpu_map_update_elem,
712 	.map_lookup_elem	= cpu_map_lookup_elem,
713 	.map_get_next_key	= cpu_map_get_next_key,
714 	.map_check_btf		= map_check_no_btf,
715 	.map_mem_usage		= cpu_map_mem_usage,
716 	.map_btf_id		= &cpu_map_btf_ids[0],
717 	.map_redirect		= cpu_map_redirect,
718 };
719 
720 static void bq_flush_to_queue(struct xdp_bulk_queue *bq)
721 {
722 	struct bpf_cpu_map_entry *rcpu = bq->obj;
723 	unsigned int processed = 0, drops = 0;
724 	const int to_cpu = rcpu->cpu;
725 	struct ptr_ring *q;
726 	int i;
727 
728 	lockdep_assert_held(&bq->bq_lock);
729 
730 	if (unlikely(!bq->count))
731 		return;
732 
733 	q = rcpu->queue;
734 	spin_lock(&q->producer_lock);
735 
736 	for (i = 0; i < bq->count; i++) {
737 		struct xdp_frame *xdpf = bq->q[i];
738 		int err;
739 
740 		err = __ptr_ring_produce(q, xdpf);
741 		if (err) {
742 			drops++;
743 			xdp_return_frame_rx_napi(xdpf);
744 		}
745 		processed++;
746 	}
747 	bq->count = 0;
748 	spin_unlock(&q->producer_lock);
749 
750 	__list_del_clearprev(&bq->flush_node);
751 
752 	/* Feedback loop via tracepoints */
753 	trace_xdp_cpumap_enqueue(rcpu->map_id, processed, drops, to_cpu);
754 }
755 
756 /* Runs under RCU-read-side, plus in softirq under NAPI protection.
757  * Thus, safe percpu variable access. PREEMPT_RT relies on
758  * local_lock_nested_bh() to serialise access to the per-CPU bq.
759  */
760 static void bq_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf)
761 {
762 	struct xdp_bulk_queue *bq;
763 
764 	local_lock_nested_bh(&rcpu->bulkq->bq_lock);
765 	bq = this_cpu_ptr(rcpu->bulkq);
766 
767 	if (unlikely(bq->count == CPU_MAP_BULK_SIZE))
768 		bq_flush_to_queue(bq);
769 
770 	/* Notice, xdp_buff/page MUST be queued here, long enough for
771 	 * driver to code invoking us to finished, due to driver
772 	 * (e.g. ixgbe) recycle tricks based on page-refcnt.
773 	 *
774 	 * Thus, incoming xdp_frame is always queued here (else we race
775 	 * with another CPU on page-refcnt and remaining driver code).
776 	 * Queue time is very short, as driver will invoke flush
777 	 * operation, when completing napi->poll call.
778 	 */
779 	bq->q[bq->count++] = xdpf;
780 
781 	if (!bq->flush_node.prev) {
782 		struct list_head *flush_list = bpf_net_ctx_get_cpu_map_flush_list();
783 
784 		list_add(&bq->flush_node, flush_list);
785 	}
786 
787 	local_unlock_nested_bh(&rcpu->bulkq->bq_lock);
788 }
789 
790 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_frame *xdpf,
791 		    struct net_device *dev_rx)
792 {
793 	/* Info needed when constructing SKB on remote CPU */
794 	xdpf->dev_rx = dev_rx;
795 
796 	bq_enqueue(rcpu, xdpf);
797 	return 0;
798 }
799 
800 int cpu_map_generic_redirect(struct bpf_cpu_map_entry *rcpu,
801 			     struct sk_buff *skb)
802 {
803 	int ret;
804 
805 	__skb_pull(skb, skb->mac_len);
806 	skb_set_redirected(skb, false);
807 	__ptr_set_bit(0, &skb);
808 
809 	ret = ptr_ring_produce(rcpu->queue, skb);
810 	if (ret < 0)
811 		goto trace;
812 
813 	wake_up_process(rcpu->kthread);
814 trace:
815 	trace_xdp_cpumap_enqueue(rcpu->map_id, !ret, !!ret, rcpu->cpu);
816 	return ret;
817 }
818 
819 void __cpu_map_flush(struct list_head *flush_list)
820 {
821 	struct xdp_bulk_queue *bq, *tmp;
822 
823 	list_for_each_entry_safe(bq, tmp, flush_list, flush_node) {
824 		local_lock_nested_bh(&bq->obj->bulkq->bq_lock);
825 		bq_flush_to_queue(bq);
826 		local_unlock_nested_bh(&bq->obj->bulkq->bq_lock);
827 
828 		/* If already running, costs spin_lock_irqsave + smb_mb */
829 		wake_up_process(bq->obj->kthread);
830 	}
831 }
832