1 /* bpf/cpumap.c 2 * 3 * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc. 4 * Released under terms in GPL version 2. See COPYING. 5 */ 6 7 /* The 'cpumap' is primarily used as a backend map for XDP BPF helper 8 * call bpf_redirect_map() and XDP_REDIRECT action, like 'devmap'. 9 * 10 * Unlike devmap which redirects XDP frames out another NIC device, 11 * this map type redirects raw XDP frames to another CPU. The remote 12 * CPU will do SKB-allocation and call the normal network stack. 13 * 14 * This is a scalability and isolation mechanism, that allow 15 * separating the early driver network XDP layer, from the rest of the 16 * netstack, and assigning dedicated CPUs for this stage. This 17 * basically allows for 10G wirespeed pre-filtering via bpf. 18 */ 19 #include <linux/bpf.h> 20 #include <linux/filter.h> 21 #include <linux/ptr_ring.h> 22 23 #include <linux/sched.h> 24 #include <linux/workqueue.h> 25 #include <linux/kthread.h> 26 #include <linux/capability.h> 27 #include <trace/events/xdp.h> 28 29 #include <linux/netdevice.h> /* netif_receive_skb_core */ 30 #include <linux/etherdevice.h> /* eth_type_trans */ 31 32 /* General idea: XDP packets getting XDP redirected to another CPU, 33 * will maximum be stored/queued for one driver ->poll() call. It is 34 * guaranteed that setting flush bit and flush operation happen on 35 * same CPU. Thus, cpu_map_flush operation can deduct via this_cpu_ptr() 36 * which queue in bpf_cpu_map_entry contains packets. 37 */ 38 39 #define CPU_MAP_BULK_SIZE 8 /* 8 == one cacheline on 64-bit archs */ 40 struct xdp_bulk_queue { 41 void *q[CPU_MAP_BULK_SIZE]; 42 unsigned int count; 43 }; 44 45 /* Struct for every remote "destination" CPU in map */ 46 struct bpf_cpu_map_entry { 47 u32 cpu; /* kthread CPU and map index */ 48 int map_id; /* Back reference to map */ 49 u32 qsize; /* Queue size placeholder for map lookup */ 50 51 /* XDP can run multiple RX-ring queues, need __percpu enqueue store */ 52 struct xdp_bulk_queue __percpu *bulkq; 53 54 /* Queue with potential multi-producers, and single-consumer kthread */ 55 struct ptr_ring *queue; 56 struct task_struct *kthread; 57 struct work_struct kthread_stop_wq; 58 59 atomic_t refcnt; /* Control when this struct can be free'ed */ 60 struct rcu_head rcu; 61 }; 62 63 struct bpf_cpu_map { 64 struct bpf_map map; 65 /* Below members specific for map type */ 66 struct bpf_cpu_map_entry **cpu_map; 67 unsigned long __percpu *flush_needed; 68 }; 69 70 static int bq_flush_to_queue(struct bpf_cpu_map_entry *rcpu, 71 struct xdp_bulk_queue *bq); 72 73 static u64 cpu_map_bitmap_size(const union bpf_attr *attr) 74 { 75 return BITS_TO_LONGS(attr->max_entries) * sizeof(unsigned long); 76 } 77 78 static struct bpf_map *cpu_map_alloc(union bpf_attr *attr) 79 { 80 struct bpf_cpu_map *cmap; 81 int err = -ENOMEM; 82 u64 cost; 83 int ret; 84 85 if (!capable(CAP_SYS_ADMIN)) 86 return ERR_PTR(-EPERM); 87 88 /* check sanity of attributes */ 89 if (attr->max_entries == 0 || attr->key_size != 4 || 90 attr->value_size != 4 || attr->map_flags & ~BPF_F_NUMA_NODE) 91 return ERR_PTR(-EINVAL); 92 93 cmap = kzalloc(sizeof(*cmap), GFP_USER); 94 if (!cmap) 95 return ERR_PTR(-ENOMEM); 96 97 /* mandatory map attributes */ 98 cmap->map.map_type = attr->map_type; 99 cmap->map.key_size = attr->key_size; 100 cmap->map.value_size = attr->value_size; 101 cmap->map.max_entries = attr->max_entries; 102 cmap->map.map_flags = attr->map_flags; 103 cmap->map.numa_node = bpf_map_attr_numa_node(attr); 104 105 /* Pre-limit array size based on NR_CPUS, not final CPU check */ 106 if (cmap->map.max_entries > NR_CPUS) { 107 err = -E2BIG; 108 goto free_cmap; 109 } 110 111 /* make sure page count doesn't overflow */ 112 cost = (u64) cmap->map.max_entries * sizeof(struct bpf_cpu_map_entry *); 113 cost += cpu_map_bitmap_size(attr) * num_possible_cpus(); 114 if (cost >= U32_MAX - PAGE_SIZE) 115 goto free_cmap; 116 cmap->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT; 117 118 /* Notice returns -EPERM on if map size is larger than memlock limit */ 119 ret = bpf_map_precharge_memlock(cmap->map.pages); 120 if (ret) { 121 err = ret; 122 goto free_cmap; 123 } 124 125 /* A per cpu bitfield with a bit per possible CPU in map */ 126 cmap->flush_needed = __alloc_percpu(cpu_map_bitmap_size(attr), 127 __alignof__(unsigned long)); 128 if (!cmap->flush_needed) 129 goto free_cmap; 130 131 /* Alloc array for possible remote "destination" CPUs */ 132 cmap->cpu_map = bpf_map_area_alloc(cmap->map.max_entries * 133 sizeof(struct bpf_cpu_map_entry *), 134 cmap->map.numa_node); 135 if (!cmap->cpu_map) 136 goto free_percpu; 137 138 return &cmap->map; 139 free_percpu: 140 free_percpu(cmap->flush_needed); 141 free_cmap: 142 kfree(cmap); 143 return ERR_PTR(err); 144 } 145 146 void __cpu_map_queue_destructor(void *ptr) 147 { 148 /* The tear-down procedure should have made sure that queue is 149 * empty. See __cpu_map_entry_replace() and work-queue 150 * invoked cpu_map_kthread_stop(). Catch any broken behaviour 151 * gracefully and warn once. 152 */ 153 if (WARN_ON_ONCE(ptr)) 154 page_frag_free(ptr); 155 } 156 157 static void put_cpu_map_entry(struct bpf_cpu_map_entry *rcpu) 158 { 159 if (atomic_dec_and_test(&rcpu->refcnt)) { 160 /* The queue should be empty at this point */ 161 ptr_ring_cleanup(rcpu->queue, __cpu_map_queue_destructor); 162 kfree(rcpu->queue); 163 kfree(rcpu); 164 } 165 } 166 167 static void get_cpu_map_entry(struct bpf_cpu_map_entry *rcpu) 168 { 169 atomic_inc(&rcpu->refcnt); 170 } 171 172 /* called from workqueue, to workaround syscall using preempt_disable */ 173 static void cpu_map_kthread_stop(struct work_struct *work) 174 { 175 struct bpf_cpu_map_entry *rcpu; 176 177 rcpu = container_of(work, struct bpf_cpu_map_entry, kthread_stop_wq); 178 179 /* Wait for flush in __cpu_map_entry_free(), via full RCU barrier, 180 * as it waits until all in-flight call_rcu() callbacks complete. 181 */ 182 rcu_barrier(); 183 184 /* kthread_stop will wake_up_process and wait for it to complete */ 185 kthread_stop(rcpu->kthread); 186 } 187 188 /* For now, xdp_pkt is a cpumap internal data structure, with info 189 * carried between enqueue to dequeue. It is mapped into the top 190 * headroom of the packet, to avoid allocating separate mem. 191 */ 192 struct xdp_pkt { 193 void *data; 194 u16 len; 195 u16 headroom; 196 u16 metasize; 197 struct net_device *dev_rx; 198 }; 199 200 /* Convert xdp_buff to xdp_pkt */ 201 static struct xdp_pkt *convert_to_xdp_pkt(struct xdp_buff *xdp) 202 { 203 struct xdp_pkt *xdp_pkt; 204 int metasize; 205 int headroom; 206 207 /* Assure headroom is available for storing info */ 208 headroom = xdp->data - xdp->data_hard_start; 209 metasize = xdp->data - xdp->data_meta; 210 metasize = metasize > 0 ? metasize : 0; 211 if ((headroom - metasize) < sizeof(*xdp_pkt)) 212 return NULL; 213 214 /* Store info in top of packet */ 215 xdp_pkt = xdp->data_hard_start; 216 217 xdp_pkt->data = xdp->data; 218 xdp_pkt->len = xdp->data_end - xdp->data; 219 xdp_pkt->headroom = headroom - sizeof(*xdp_pkt); 220 xdp_pkt->metasize = metasize; 221 222 return xdp_pkt; 223 } 224 225 struct sk_buff *cpu_map_build_skb(struct bpf_cpu_map_entry *rcpu, 226 struct xdp_pkt *xdp_pkt) 227 { 228 unsigned int frame_size; 229 void *pkt_data_start; 230 struct sk_buff *skb; 231 232 /* build_skb need to place skb_shared_info after SKB end, and 233 * also want to know the memory "truesize". Thus, need to 234 * know the memory frame size backing xdp_buff. 235 * 236 * XDP was designed to have PAGE_SIZE frames, but this 237 * assumption is not longer true with ixgbe and i40e. It 238 * would be preferred to set frame_size to 2048 or 4096 239 * depending on the driver. 240 * frame_size = 2048; 241 * frame_len = frame_size - sizeof(*xdp_pkt); 242 * 243 * Instead, with info avail, skb_shared_info in placed after 244 * packet len. This, unfortunately fakes the truesize. 245 * Another disadvantage of this approach, the skb_shared_info 246 * is not at a fixed memory location, with mixed length 247 * packets, which is bad for cache-line hotness. 248 */ 249 frame_size = SKB_DATA_ALIGN(xdp_pkt->len) + xdp_pkt->headroom + 250 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 251 252 pkt_data_start = xdp_pkt->data - xdp_pkt->headroom; 253 skb = build_skb(pkt_data_start, frame_size); 254 if (!skb) 255 return NULL; 256 257 skb_reserve(skb, xdp_pkt->headroom); 258 __skb_put(skb, xdp_pkt->len); 259 if (xdp_pkt->metasize) 260 skb_metadata_set(skb, xdp_pkt->metasize); 261 262 /* Essential SKB info: protocol and skb->dev */ 263 skb->protocol = eth_type_trans(skb, xdp_pkt->dev_rx); 264 265 /* Optional SKB info, currently missing: 266 * - HW checksum info (skb->ip_summed) 267 * - HW RX hash (skb_set_hash) 268 * - RX ring dev queue index (skb_record_rx_queue) 269 */ 270 271 return skb; 272 } 273 274 static int cpu_map_kthread_run(void *data) 275 { 276 struct bpf_cpu_map_entry *rcpu = data; 277 278 set_current_state(TASK_INTERRUPTIBLE); 279 280 /* When kthread gives stop order, then rcpu have been disconnected 281 * from map, thus no new packets can enter. Remaining in-flight 282 * per CPU stored packets are flushed to this queue. Wait honoring 283 * kthread_stop signal until queue is empty. 284 */ 285 while (!kthread_should_stop() || !__ptr_ring_empty(rcpu->queue)) { 286 unsigned int processed = 0, drops = 0, sched = 0; 287 struct xdp_pkt *xdp_pkt; 288 289 /* Release CPU reschedule checks */ 290 if (__ptr_ring_empty(rcpu->queue)) { 291 __set_current_state(TASK_INTERRUPTIBLE); 292 schedule(); 293 sched = 1; 294 } else { 295 sched = cond_resched(); 296 } 297 __set_current_state(TASK_RUNNING); 298 299 /* Process packets in rcpu->queue */ 300 local_bh_disable(); 301 /* 302 * The bpf_cpu_map_entry is single consumer, with this 303 * kthread CPU pinned. Lockless access to ptr_ring 304 * consume side valid as no-resize allowed of queue. 305 */ 306 while ((xdp_pkt = __ptr_ring_consume(rcpu->queue))) { 307 struct sk_buff *skb; 308 int ret; 309 310 skb = cpu_map_build_skb(rcpu, xdp_pkt); 311 if (!skb) { 312 page_frag_free(xdp_pkt); 313 continue; 314 } 315 316 /* Inject into network stack */ 317 ret = netif_receive_skb_core(skb); 318 if (ret == NET_RX_DROP) 319 drops++; 320 321 /* Limit BH-disable period */ 322 if (++processed == 8) 323 break; 324 } 325 /* Feedback loop via tracepoint */ 326 trace_xdp_cpumap_kthread(rcpu->map_id, processed, drops, sched); 327 328 local_bh_enable(); /* resched point, may call do_softirq() */ 329 } 330 __set_current_state(TASK_RUNNING); 331 332 put_cpu_map_entry(rcpu); 333 return 0; 334 } 335 336 struct bpf_cpu_map_entry *__cpu_map_entry_alloc(u32 qsize, u32 cpu, int map_id) 337 { 338 gfp_t gfp = GFP_ATOMIC|__GFP_NOWARN; 339 struct bpf_cpu_map_entry *rcpu; 340 int numa, err; 341 342 /* Have map->numa_node, but choose node of redirect target CPU */ 343 numa = cpu_to_node(cpu); 344 345 rcpu = kzalloc_node(sizeof(*rcpu), gfp, numa); 346 if (!rcpu) 347 return NULL; 348 349 /* Alloc percpu bulkq */ 350 rcpu->bulkq = __alloc_percpu_gfp(sizeof(*rcpu->bulkq), 351 sizeof(void *), gfp); 352 if (!rcpu->bulkq) 353 goto free_rcu; 354 355 /* Alloc queue */ 356 rcpu->queue = kzalloc_node(sizeof(*rcpu->queue), gfp, numa); 357 if (!rcpu->queue) 358 goto free_bulkq; 359 360 err = ptr_ring_init(rcpu->queue, qsize, gfp); 361 if (err) 362 goto free_queue; 363 364 rcpu->cpu = cpu; 365 rcpu->map_id = map_id; 366 rcpu->qsize = qsize; 367 368 /* Setup kthread */ 369 rcpu->kthread = kthread_create_on_node(cpu_map_kthread_run, rcpu, numa, 370 "cpumap/%d/map:%d", cpu, map_id); 371 if (IS_ERR(rcpu->kthread)) 372 goto free_ptr_ring; 373 374 get_cpu_map_entry(rcpu); /* 1-refcnt for being in cmap->cpu_map[] */ 375 get_cpu_map_entry(rcpu); /* 1-refcnt for kthread */ 376 377 /* Make sure kthread runs on a single CPU */ 378 kthread_bind(rcpu->kthread, cpu); 379 wake_up_process(rcpu->kthread); 380 381 return rcpu; 382 383 free_ptr_ring: 384 ptr_ring_cleanup(rcpu->queue, NULL); 385 free_queue: 386 kfree(rcpu->queue); 387 free_bulkq: 388 free_percpu(rcpu->bulkq); 389 free_rcu: 390 kfree(rcpu); 391 return NULL; 392 } 393 394 void __cpu_map_entry_free(struct rcu_head *rcu) 395 { 396 struct bpf_cpu_map_entry *rcpu; 397 int cpu; 398 399 /* This cpu_map_entry have been disconnected from map and one 400 * RCU graze-period have elapsed. Thus, XDP cannot queue any 401 * new packets and cannot change/set flush_needed that can 402 * find this entry. 403 */ 404 rcpu = container_of(rcu, struct bpf_cpu_map_entry, rcu); 405 406 /* Flush remaining packets in percpu bulkq */ 407 for_each_online_cpu(cpu) { 408 struct xdp_bulk_queue *bq = per_cpu_ptr(rcpu->bulkq, cpu); 409 410 /* No concurrent bq_enqueue can run at this point */ 411 bq_flush_to_queue(rcpu, bq); 412 } 413 free_percpu(rcpu->bulkq); 414 /* Cannot kthread_stop() here, last put free rcpu resources */ 415 put_cpu_map_entry(rcpu); 416 } 417 418 /* After xchg pointer to bpf_cpu_map_entry, use the call_rcu() to 419 * ensure any driver rcu critical sections have completed, but this 420 * does not guarantee a flush has happened yet. Because driver side 421 * rcu_read_lock/unlock only protects the running XDP program. The 422 * atomic xchg and NULL-ptr check in __cpu_map_flush() makes sure a 423 * pending flush op doesn't fail. 424 * 425 * The bpf_cpu_map_entry is still used by the kthread, and there can 426 * still be pending packets (in queue and percpu bulkq). A refcnt 427 * makes sure to last user (kthread_stop vs. call_rcu) free memory 428 * resources. 429 * 430 * The rcu callback __cpu_map_entry_free flush remaining packets in 431 * percpu bulkq to queue. Due to caller map_delete_elem() disable 432 * preemption, cannot call kthread_stop() to make sure queue is empty. 433 * Instead a work_queue is started for stopping kthread, 434 * cpu_map_kthread_stop, which waits for an RCU graze period before 435 * stopping kthread, emptying the queue. 436 */ 437 void __cpu_map_entry_replace(struct bpf_cpu_map *cmap, 438 u32 key_cpu, struct bpf_cpu_map_entry *rcpu) 439 { 440 struct bpf_cpu_map_entry *old_rcpu; 441 442 old_rcpu = xchg(&cmap->cpu_map[key_cpu], rcpu); 443 if (old_rcpu) { 444 call_rcu(&old_rcpu->rcu, __cpu_map_entry_free); 445 INIT_WORK(&old_rcpu->kthread_stop_wq, cpu_map_kthread_stop); 446 schedule_work(&old_rcpu->kthread_stop_wq); 447 } 448 } 449 450 int cpu_map_delete_elem(struct bpf_map *map, void *key) 451 { 452 struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map); 453 u32 key_cpu = *(u32 *)key; 454 455 if (key_cpu >= map->max_entries) 456 return -EINVAL; 457 458 /* notice caller map_delete_elem() use preempt_disable() */ 459 __cpu_map_entry_replace(cmap, key_cpu, NULL); 460 return 0; 461 } 462 463 int cpu_map_update_elem(struct bpf_map *map, void *key, void *value, 464 u64 map_flags) 465 { 466 struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map); 467 struct bpf_cpu_map_entry *rcpu; 468 469 /* Array index key correspond to CPU number */ 470 u32 key_cpu = *(u32 *)key; 471 /* Value is the queue size */ 472 u32 qsize = *(u32 *)value; 473 474 if (unlikely(map_flags > BPF_EXIST)) 475 return -EINVAL; 476 if (unlikely(key_cpu >= cmap->map.max_entries)) 477 return -E2BIG; 478 if (unlikely(map_flags == BPF_NOEXIST)) 479 return -EEXIST; 480 if (unlikely(qsize > 16384)) /* sanity limit on qsize */ 481 return -EOVERFLOW; 482 483 /* Make sure CPU is a valid possible cpu */ 484 if (!cpu_possible(key_cpu)) 485 return -ENODEV; 486 487 if (qsize == 0) { 488 rcpu = NULL; /* Same as deleting */ 489 } else { 490 /* Updating qsize cause re-allocation of bpf_cpu_map_entry */ 491 rcpu = __cpu_map_entry_alloc(qsize, key_cpu, map->id); 492 if (!rcpu) 493 return -ENOMEM; 494 } 495 rcu_read_lock(); 496 __cpu_map_entry_replace(cmap, key_cpu, rcpu); 497 rcu_read_unlock(); 498 return 0; 499 } 500 501 void cpu_map_free(struct bpf_map *map) 502 { 503 struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map); 504 int cpu; 505 u32 i; 506 507 /* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0, 508 * so the bpf programs (can be more than one that used this map) were 509 * disconnected from events. Wait for outstanding critical sections in 510 * these programs to complete. The rcu critical section only guarantees 511 * no further "XDP/bpf-side" reads against bpf_cpu_map->cpu_map. 512 * It does __not__ ensure pending flush operations (if any) are 513 * complete. 514 */ 515 synchronize_rcu(); 516 517 /* To ensure all pending flush operations have completed wait for flush 518 * bitmap to indicate all flush_needed bits to be zero on _all_ cpus. 519 * Because the above synchronize_rcu() ensures the map is disconnected 520 * from the program we can assume no new bits will be set. 521 */ 522 for_each_online_cpu(cpu) { 523 unsigned long *bitmap = per_cpu_ptr(cmap->flush_needed, cpu); 524 525 while (!bitmap_empty(bitmap, cmap->map.max_entries)) 526 cond_resched(); 527 } 528 529 /* For cpu_map the remote CPUs can still be using the entries 530 * (struct bpf_cpu_map_entry). 531 */ 532 for (i = 0; i < cmap->map.max_entries; i++) { 533 struct bpf_cpu_map_entry *rcpu; 534 535 rcpu = READ_ONCE(cmap->cpu_map[i]); 536 if (!rcpu) 537 continue; 538 539 /* bq flush and cleanup happens after RCU graze-period */ 540 __cpu_map_entry_replace(cmap, i, NULL); /* call_rcu */ 541 } 542 free_percpu(cmap->flush_needed); 543 bpf_map_area_free(cmap->cpu_map); 544 kfree(cmap); 545 } 546 547 struct bpf_cpu_map_entry *__cpu_map_lookup_elem(struct bpf_map *map, u32 key) 548 { 549 struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map); 550 struct bpf_cpu_map_entry *rcpu; 551 552 if (key >= map->max_entries) 553 return NULL; 554 555 rcpu = READ_ONCE(cmap->cpu_map[key]); 556 return rcpu; 557 } 558 559 static void *cpu_map_lookup_elem(struct bpf_map *map, void *key) 560 { 561 struct bpf_cpu_map_entry *rcpu = 562 __cpu_map_lookup_elem(map, *(u32 *)key); 563 564 return rcpu ? &rcpu->qsize : NULL; 565 } 566 567 static int cpu_map_get_next_key(struct bpf_map *map, void *key, void *next_key) 568 { 569 struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map); 570 u32 index = key ? *(u32 *)key : U32_MAX; 571 u32 *next = next_key; 572 573 if (index >= cmap->map.max_entries) { 574 *next = 0; 575 return 0; 576 } 577 578 if (index == cmap->map.max_entries - 1) 579 return -ENOENT; 580 *next = index + 1; 581 return 0; 582 } 583 584 const struct bpf_map_ops cpu_map_ops = { 585 .map_alloc = cpu_map_alloc, 586 .map_free = cpu_map_free, 587 .map_delete_elem = cpu_map_delete_elem, 588 .map_update_elem = cpu_map_update_elem, 589 .map_lookup_elem = cpu_map_lookup_elem, 590 .map_get_next_key = cpu_map_get_next_key, 591 }; 592 593 static int bq_flush_to_queue(struct bpf_cpu_map_entry *rcpu, 594 struct xdp_bulk_queue *bq) 595 { 596 unsigned int processed = 0, drops = 0; 597 const int to_cpu = rcpu->cpu; 598 struct ptr_ring *q; 599 int i; 600 601 if (unlikely(!bq->count)) 602 return 0; 603 604 q = rcpu->queue; 605 spin_lock(&q->producer_lock); 606 607 for (i = 0; i < bq->count; i++) { 608 void *xdp_pkt = bq->q[i]; 609 int err; 610 611 err = __ptr_ring_produce(q, xdp_pkt); 612 if (err) { 613 drops++; 614 page_frag_free(xdp_pkt); /* Free xdp_pkt */ 615 } 616 processed++; 617 } 618 bq->count = 0; 619 spin_unlock(&q->producer_lock); 620 621 /* Feedback loop via tracepoints */ 622 trace_xdp_cpumap_enqueue(rcpu->map_id, processed, drops, to_cpu); 623 return 0; 624 } 625 626 /* Runs under RCU-read-side, plus in softirq under NAPI protection. 627 * Thus, safe percpu variable access. 628 */ 629 static int bq_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_pkt *xdp_pkt) 630 { 631 struct xdp_bulk_queue *bq = this_cpu_ptr(rcpu->bulkq); 632 633 if (unlikely(bq->count == CPU_MAP_BULK_SIZE)) 634 bq_flush_to_queue(rcpu, bq); 635 636 /* Notice, xdp_buff/page MUST be queued here, long enough for 637 * driver to code invoking us to finished, due to driver 638 * (e.g. ixgbe) recycle tricks based on page-refcnt. 639 * 640 * Thus, incoming xdp_pkt is always queued here (else we race 641 * with another CPU on page-refcnt and remaining driver code). 642 * Queue time is very short, as driver will invoke flush 643 * operation, when completing napi->poll call. 644 */ 645 bq->q[bq->count++] = xdp_pkt; 646 return 0; 647 } 648 649 int cpu_map_enqueue(struct bpf_cpu_map_entry *rcpu, struct xdp_buff *xdp, 650 struct net_device *dev_rx) 651 { 652 struct xdp_pkt *xdp_pkt; 653 654 xdp_pkt = convert_to_xdp_pkt(xdp); 655 if (!xdp_pkt) 656 return -EOVERFLOW; 657 658 /* Info needed when constructing SKB on remote CPU */ 659 xdp_pkt->dev_rx = dev_rx; 660 661 bq_enqueue(rcpu, xdp_pkt); 662 return 0; 663 } 664 665 void __cpu_map_insert_ctx(struct bpf_map *map, u32 bit) 666 { 667 struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map); 668 unsigned long *bitmap = this_cpu_ptr(cmap->flush_needed); 669 670 __set_bit(bit, bitmap); 671 } 672 673 void __cpu_map_flush(struct bpf_map *map) 674 { 675 struct bpf_cpu_map *cmap = container_of(map, struct bpf_cpu_map, map); 676 unsigned long *bitmap = this_cpu_ptr(cmap->flush_needed); 677 u32 bit; 678 679 /* The napi->poll softirq makes sure __cpu_map_insert_ctx() 680 * and __cpu_map_flush() happen on same CPU. Thus, the percpu 681 * bitmap indicate which percpu bulkq have packets. 682 */ 683 for_each_set_bit(bit, bitmap, map->max_entries) { 684 struct bpf_cpu_map_entry *rcpu = READ_ONCE(cmap->cpu_map[bit]); 685 struct xdp_bulk_queue *bq; 686 687 /* This is possible if entry is removed by user space 688 * between xdp redirect and flush op. 689 */ 690 if (unlikely(!rcpu)) 691 continue; 692 693 __clear_bit(bit, bitmap); 694 695 /* Flush all frames in bulkq to real queue */ 696 bq = this_cpu_ptr(rcpu->bulkq); 697 bq_flush_to_queue(rcpu, bq); 698 699 /* If already running, costs spin_lock_irqsave + smb_mb */ 700 wake_up_process(rcpu->kthread); 701 } 702 } 703