1 // SPDX-License-Identifier: GPL-2.0 2 3 #include <net/xsk_buff_pool.h> 4 #include <net/xdp_sock.h> 5 #include <net/xdp_sock_drv.h> 6 7 #include "xsk_queue.h" 8 #include "xdp_umem.h" 9 #include "xsk.h" 10 11 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs) 12 { 13 unsigned long flags; 14 15 if (!xs->tx) 16 return; 17 18 spin_lock_irqsave(&pool->xsk_tx_list_lock, flags); 19 list_add_rcu(&xs->tx_list, &pool->xsk_tx_list); 20 spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags); 21 } 22 23 void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs) 24 { 25 unsigned long flags; 26 27 if (!xs->tx) 28 return; 29 30 spin_lock_irqsave(&pool->xsk_tx_list_lock, flags); 31 list_del_rcu(&xs->tx_list); 32 spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags); 33 } 34 35 void xp_destroy(struct xsk_buff_pool *pool) 36 { 37 if (!pool) 38 return; 39 40 kvfree(pool->heads); 41 kvfree(pool); 42 } 43 44 struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs, 45 struct xdp_umem *umem) 46 { 47 struct xsk_buff_pool *pool; 48 struct xdp_buff_xsk *xskb; 49 u32 i; 50 51 pool = kvzalloc(struct_size(pool, free_heads, umem->chunks), 52 GFP_KERNEL); 53 if (!pool) 54 goto out; 55 56 pool->heads = kvcalloc(umem->chunks, sizeof(*pool->heads), GFP_KERNEL); 57 if (!pool->heads) 58 goto out; 59 60 pool->chunk_mask = ~((u64)umem->chunk_size - 1); 61 pool->addrs_cnt = umem->size; 62 pool->heads_cnt = umem->chunks; 63 pool->free_heads_cnt = umem->chunks; 64 pool->headroom = umem->headroom; 65 pool->chunk_size = umem->chunk_size; 66 pool->unaligned = umem->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG; 67 pool->frame_len = umem->chunk_size - umem->headroom - 68 XDP_PACKET_HEADROOM; 69 pool->umem = umem; 70 pool->addrs = umem->addrs; 71 INIT_LIST_HEAD(&pool->free_list); 72 INIT_LIST_HEAD(&pool->xsk_tx_list); 73 spin_lock_init(&pool->xsk_tx_list_lock); 74 refcount_set(&pool->users, 1); 75 76 pool->fq = xs->fq_tmp; 77 pool->cq = xs->cq_tmp; 78 xs->fq_tmp = NULL; 79 xs->cq_tmp = NULL; 80 81 for (i = 0; i < pool->free_heads_cnt; i++) { 82 xskb = &pool->heads[i]; 83 xskb->pool = pool; 84 xskb->xdp.frame_sz = umem->chunk_size - umem->headroom; 85 pool->free_heads[i] = xskb; 86 } 87 88 return pool; 89 90 out: 91 xp_destroy(pool); 92 return NULL; 93 } 94 95 void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq) 96 { 97 u32 i; 98 99 for (i = 0; i < pool->heads_cnt; i++) 100 pool->heads[i].xdp.rxq = rxq; 101 } 102 EXPORT_SYMBOL(xp_set_rxq_info); 103 104 static void xp_disable_drv_zc(struct xsk_buff_pool *pool) 105 { 106 struct netdev_bpf bpf; 107 int err; 108 109 ASSERT_RTNL(); 110 111 if (pool->umem->zc) { 112 bpf.command = XDP_SETUP_XSK_POOL; 113 bpf.xsk.pool = NULL; 114 bpf.xsk.queue_id = pool->queue_id; 115 116 err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf); 117 118 if (err) 119 WARN(1, "Failed to disable zero-copy!\n"); 120 } 121 } 122 123 static int __xp_assign_dev(struct xsk_buff_pool *pool, 124 struct net_device *netdev, u16 queue_id, u16 flags) 125 { 126 bool force_zc, force_copy; 127 struct netdev_bpf bpf; 128 int err = 0; 129 130 ASSERT_RTNL(); 131 132 force_zc = flags & XDP_ZEROCOPY; 133 force_copy = flags & XDP_COPY; 134 135 if (force_zc && force_copy) 136 return -EINVAL; 137 138 if (xsk_get_pool_from_qid(netdev, queue_id)) 139 return -EBUSY; 140 141 pool->netdev = netdev; 142 pool->queue_id = queue_id; 143 err = xsk_reg_pool_at_qid(netdev, pool, queue_id); 144 if (err) 145 return err; 146 147 if (flags & XDP_USE_NEED_WAKEUP) { 148 pool->uses_need_wakeup = true; 149 /* Tx needs to be explicitly woken up the first time. 150 * Also for supporting drivers that do not implement this 151 * feature. They will always have to call sendto(). 152 */ 153 pool->cached_need_wakeup = XDP_WAKEUP_TX; 154 } 155 156 dev_hold(netdev); 157 158 if (force_copy) 159 /* For copy-mode, we are done. */ 160 return 0; 161 162 if (!netdev->netdev_ops->ndo_bpf || 163 !netdev->netdev_ops->ndo_xsk_wakeup) { 164 err = -EOPNOTSUPP; 165 goto err_unreg_pool; 166 } 167 168 bpf.command = XDP_SETUP_XSK_POOL; 169 bpf.xsk.pool = pool; 170 bpf.xsk.queue_id = queue_id; 171 172 err = netdev->netdev_ops->ndo_bpf(netdev, &bpf); 173 if (err) 174 goto err_unreg_pool; 175 176 if (!pool->dma_pages) { 177 WARN(1, "Driver did not DMA map zero-copy buffers"); 178 goto err_unreg_xsk; 179 } 180 pool->umem->zc = true; 181 return 0; 182 183 err_unreg_xsk: 184 xp_disable_drv_zc(pool); 185 err_unreg_pool: 186 if (!force_zc) 187 err = 0; /* fallback to copy mode */ 188 if (err) { 189 xsk_clear_pool_at_qid(netdev, queue_id); 190 dev_put(netdev); 191 } 192 return err; 193 } 194 195 int xp_assign_dev(struct xsk_buff_pool *pool, struct net_device *dev, 196 u16 queue_id, u16 flags) 197 { 198 return __xp_assign_dev(pool, dev, queue_id, flags); 199 } 200 201 int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_umem *umem, 202 struct net_device *dev, u16 queue_id) 203 { 204 u16 flags; 205 206 /* One fill and completion ring required for each queue id. */ 207 if (!pool->fq || !pool->cq) 208 return -EINVAL; 209 210 flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY; 211 if (pool->uses_need_wakeup) 212 flags |= XDP_USE_NEED_WAKEUP; 213 214 return __xp_assign_dev(pool, dev, queue_id, flags); 215 } 216 217 void xp_clear_dev(struct xsk_buff_pool *pool) 218 { 219 if (!pool->netdev) 220 return; 221 222 xp_disable_drv_zc(pool); 223 xsk_clear_pool_at_qid(pool->netdev, pool->queue_id); 224 dev_put(pool->netdev); 225 pool->netdev = NULL; 226 } 227 228 static void xp_release_deferred(struct work_struct *work) 229 { 230 struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool, 231 work); 232 233 rtnl_lock(); 234 xp_clear_dev(pool); 235 rtnl_unlock(); 236 237 if (pool->fq) { 238 xskq_destroy(pool->fq); 239 pool->fq = NULL; 240 } 241 242 if (pool->cq) { 243 xskq_destroy(pool->cq); 244 pool->cq = NULL; 245 } 246 247 xdp_put_umem(pool->umem, false); 248 xp_destroy(pool); 249 } 250 251 void xp_get_pool(struct xsk_buff_pool *pool) 252 { 253 refcount_inc(&pool->users); 254 } 255 256 bool xp_put_pool(struct xsk_buff_pool *pool) 257 { 258 if (!pool) 259 return false; 260 261 if (refcount_dec_and_test(&pool->users)) { 262 INIT_WORK(&pool->work, xp_release_deferred); 263 schedule_work(&pool->work); 264 return true; 265 } 266 267 return false; 268 } 269 270 static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool) 271 { 272 struct xsk_dma_map *dma_map; 273 274 list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) { 275 if (dma_map->netdev == pool->netdev) 276 return dma_map; 277 } 278 279 return NULL; 280 } 281 282 static struct xsk_dma_map *xp_create_dma_map(struct device *dev, struct net_device *netdev, 283 u32 nr_pages, struct xdp_umem *umem) 284 { 285 struct xsk_dma_map *dma_map; 286 287 dma_map = kzalloc(sizeof(*dma_map), GFP_KERNEL); 288 if (!dma_map) 289 return NULL; 290 291 dma_map->dma_pages = kvcalloc(nr_pages, sizeof(*dma_map->dma_pages), GFP_KERNEL); 292 if (!dma_map->dma_pages) { 293 kfree(dma_map); 294 return NULL; 295 } 296 297 dma_map->netdev = netdev; 298 dma_map->dev = dev; 299 dma_map->dma_need_sync = false; 300 dma_map->dma_pages_cnt = nr_pages; 301 refcount_set(&dma_map->users, 1); 302 list_add(&dma_map->list, &umem->xsk_dma_list); 303 return dma_map; 304 } 305 306 static void xp_destroy_dma_map(struct xsk_dma_map *dma_map) 307 { 308 list_del(&dma_map->list); 309 kvfree(dma_map->dma_pages); 310 kfree(dma_map); 311 } 312 313 static void __xp_dma_unmap(struct xsk_dma_map *dma_map, unsigned long attrs) 314 { 315 dma_addr_t *dma; 316 u32 i; 317 318 for (i = 0; i < dma_map->dma_pages_cnt; i++) { 319 dma = &dma_map->dma_pages[i]; 320 if (*dma) { 321 dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE, 322 DMA_BIDIRECTIONAL, attrs); 323 *dma = 0; 324 } 325 } 326 327 xp_destroy_dma_map(dma_map); 328 } 329 330 void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs) 331 { 332 struct xsk_dma_map *dma_map; 333 334 if (pool->dma_pages_cnt == 0) 335 return; 336 337 dma_map = xp_find_dma_map(pool); 338 if (!dma_map) { 339 WARN(1, "Could not find dma_map for device"); 340 return; 341 } 342 343 if (!refcount_dec_and_test(&dma_map->users)) 344 return; 345 346 __xp_dma_unmap(dma_map, attrs); 347 kvfree(pool->dma_pages); 348 pool->dma_pages_cnt = 0; 349 pool->dev = NULL; 350 } 351 EXPORT_SYMBOL(xp_dma_unmap); 352 353 static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map) 354 { 355 u32 i; 356 357 for (i = 0; i < dma_map->dma_pages_cnt - 1; i++) { 358 if (dma_map->dma_pages[i] + PAGE_SIZE == dma_map->dma_pages[i + 1]) 359 dma_map->dma_pages[i] |= XSK_NEXT_PG_CONTIG_MASK; 360 else 361 dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK; 362 } 363 } 364 365 static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map) 366 { 367 pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL); 368 if (!pool->dma_pages) 369 return -ENOMEM; 370 371 pool->dev = dma_map->dev; 372 pool->dma_pages_cnt = dma_map->dma_pages_cnt; 373 pool->dma_need_sync = dma_map->dma_need_sync; 374 memcpy(pool->dma_pages, dma_map->dma_pages, 375 pool->dma_pages_cnt * sizeof(*pool->dma_pages)); 376 377 return 0; 378 } 379 380 int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev, 381 unsigned long attrs, struct page **pages, u32 nr_pages) 382 { 383 struct xsk_dma_map *dma_map; 384 dma_addr_t dma; 385 int err; 386 u32 i; 387 388 dma_map = xp_find_dma_map(pool); 389 if (dma_map) { 390 err = xp_init_dma_info(pool, dma_map); 391 if (err) 392 return err; 393 394 refcount_inc(&dma_map->users); 395 return 0; 396 } 397 398 dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem); 399 if (!dma_map) 400 return -ENOMEM; 401 402 for (i = 0; i < dma_map->dma_pages_cnt; i++) { 403 dma = dma_map_page_attrs(dev, pages[i], 0, PAGE_SIZE, 404 DMA_BIDIRECTIONAL, attrs); 405 if (dma_mapping_error(dev, dma)) { 406 __xp_dma_unmap(dma_map, attrs); 407 return -ENOMEM; 408 } 409 if (dma_need_sync(dev, dma)) 410 dma_map->dma_need_sync = true; 411 dma_map->dma_pages[i] = dma; 412 } 413 414 if (pool->unaligned) 415 xp_check_dma_contiguity(dma_map); 416 417 err = xp_init_dma_info(pool, dma_map); 418 if (err) { 419 __xp_dma_unmap(dma_map, attrs); 420 return err; 421 } 422 423 return 0; 424 } 425 EXPORT_SYMBOL(xp_dma_map); 426 427 static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool, 428 u64 addr) 429 { 430 return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size); 431 } 432 433 static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr) 434 { 435 *addr = xp_unaligned_extract_addr(*addr); 436 if (*addr >= pool->addrs_cnt || 437 *addr + pool->chunk_size > pool->addrs_cnt || 438 xp_addr_crosses_non_contig_pg(pool, *addr)) 439 return false; 440 return true; 441 } 442 443 static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr) 444 { 445 *addr = xp_aligned_extract_addr(pool, *addr); 446 return *addr < pool->addrs_cnt; 447 } 448 449 static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool) 450 { 451 struct xdp_buff_xsk *xskb; 452 u64 addr; 453 bool ok; 454 455 if (pool->free_heads_cnt == 0) 456 return NULL; 457 458 xskb = pool->free_heads[--pool->free_heads_cnt]; 459 460 for (;;) { 461 if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) { 462 pool->fq->queue_empty_descs++; 463 xp_release(xskb); 464 return NULL; 465 } 466 467 ok = pool->unaligned ? xp_check_unaligned(pool, &addr) : 468 xp_check_aligned(pool, &addr); 469 if (!ok) { 470 pool->fq->invalid_descs++; 471 xskq_cons_release(pool->fq); 472 continue; 473 } 474 break; 475 } 476 xskq_cons_release(pool->fq); 477 478 xskb->orig_addr = addr; 479 xskb->xdp.data_hard_start = pool->addrs + addr + pool->headroom; 480 if (pool->dma_pages_cnt) { 481 xskb->frame_dma = (pool->dma_pages[addr >> PAGE_SHIFT] & 482 ~XSK_NEXT_PG_CONTIG_MASK) + 483 (addr & ~PAGE_MASK); 484 xskb->dma = xskb->frame_dma + pool->headroom + 485 XDP_PACKET_HEADROOM; 486 } 487 return xskb; 488 } 489 490 struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool) 491 { 492 struct xdp_buff_xsk *xskb; 493 494 if (!pool->free_list_cnt) { 495 xskb = __xp_alloc(pool); 496 if (!xskb) 497 return NULL; 498 } else { 499 pool->free_list_cnt--; 500 xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, 501 free_list_node); 502 list_del(&xskb->free_list_node); 503 } 504 505 xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM; 506 xskb->xdp.data_meta = xskb->xdp.data; 507 508 if (pool->dma_need_sync) { 509 dma_sync_single_range_for_device(pool->dev, xskb->dma, 0, 510 pool->frame_len, 511 DMA_BIDIRECTIONAL); 512 } 513 return &xskb->xdp; 514 } 515 EXPORT_SYMBOL(xp_alloc); 516 517 bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count) 518 { 519 if (pool->free_list_cnt >= count) 520 return true; 521 return xskq_cons_has_entries(pool->fq, count - pool->free_list_cnt); 522 } 523 EXPORT_SYMBOL(xp_can_alloc); 524 525 void xp_free(struct xdp_buff_xsk *xskb) 526 { 527 xskb->pool->free_list_cnt++; 528 list_add(&xskb->free_list_node, &xskb->pool->free_list); 529 } 530 EXPORT_SYMBOL(xp_free); 531 532 void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr) 533 { 534 addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr; 535 return pool->addrs + addr; 536 } 537 EXPORT_SYMBOL(xp_raw_get_data); 538 539 dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr) 540 { 541 addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr; 542 return (pool->dma_pages[addr >> PAGE_SHIFT] & 543 ~XSK_NEXT_PG_CONTIG_MASK) + 544 (addr & ~PAGE_MASK); 545 } 546 EXPORT_SYMBOL(xp_raw_get_dma); 547 548 void xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb) 549 { 550 dma_sync_single_range_for_cpu(xskb->pool->dev, xskb->dma, 0, 551 xskb->pool->frame_len, DMA_BIDIRECTIONAL); 552 } 553 EXPORT_SYMBOL(xp_dma_sync_for_cpu_slow); 554 555 void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma, 556 size_t size) 557 { 558 dma_sync_single_range_for_device(pool->dev, dma, 0, 559 size, DMA_BIDIRECTIONAL); 560 } 561 EXPORT_SYMBOL(xp_dma_sync_for_device_slow); 562