1 /* SPDX-License-Identifier: GPL-2.0 2 * 3 * page_pool.c 4 * Author: Jesper Dangaard Brouer <netoptimizer@brouer.com> 5 * Copyright (C) 2016 Red Hat, Inc. 6 */ 7 8 #include <linux/error-injection.h> 9 #include <linux/types.h> 10 #include <linux/kernel.h> 11 #include <linux/slab.h> 12 #include <linux/device.h> 13 14 #include <net/netdev_lock.h> 15 #include <net/netdev_rx_queue.h> 16 #include <net/page_pool/helpers.h> 17 #include <net/page_pool/memory_provider.h> 18 #include <net/xdp.h> 19 20 #include <linux/dma-direction.h> 21 #include <linux/dma-mapping.h> 22 #include <linux/page-flags.h> 23 #include <linux/mm.h> /* for put_page() */ 24 #include <linux/poison.h> 25 #include <linux/ethtool.h> 26 #include <linux/netdevice.h> 27 28 #include <trace/events/page_pool.h> 29 30 #include "dev.h" 31 #include "mp_dmabuf_devmem.h" 32 #include "netmem_priv.h" 33 #include "page_pool_priv.h" 34 35 DEFINE_STATIC_KEY_FALSE(page_pool_mem_providers); 36 37 #define DEFER_TIME (msecs_to_jiffies(1000)) 38 #define DEFER_WARN_INTERVAL (60 * HZ) 39 40 #define BIAS_MAX (LONG_MAX >> 1) 41 42 #ifdef CONFIG_PAGE_POOL_STATS 43 static DEFINE_PER_CPU(struct page_pool_recycle_stats, pp_system_recycle_stats); 44 45 /* alloc_stat_inc is intended to be used in softirq context */ 46 #define alloc_stat_inc(pool, __stat) (pool->alloc_stats.__stat++) 47 /* recycle_stat_inc is safe to use when preemption is possible. */ 48 #define recycle_stat_inc(pool, __stat) \ 49 do { \ 50 struct page_pool_recycle_stats __percpu *s = pool->recycle_stats; \ 51 this_cpu_inc(s->__stat); \ 52 } while (0) 53 54 #define recycle_stat_add(pool, __stat, val) \ 55 do { \ 56 struct page_pool_recycle_stats __percpu *s = pool->recycle_stats; \ 57 this_cpu_add(s->__stat, val); \ 58 } while (0) 59 60 static const char pp_stats[][ETH_GSTRING_LEN] = { 61 "rx_pp_alloc_fast", 62 "rx_pp_alloc_slow", 63 "rx_pp_alloc_slow_ho", 64 "rx_pp_alloc_empty", 65 "rx_pp_alloc_refill", 66 "rx_pp_alloc_waive", 67 "rx_pp_recycle_cached", 68 "rx_pp_recycle_cache_full", 69 "rx_pp_recycle_ring", 70 "rx_pp_recycle_ring_full", 71 "rx_pp_recycle_released_ref", 72 }; 73 74 /** 75 * page_pool_get_stats() - fetch page pool stats 76 * @pool: pool from which page was allocated 77 * @stats: struct page_pool_stats to fill in 78 * 79 * Deprecated driver API for querying stats. Page pool stats can be queried 80 * via netdev Netlink. 81 * 82 * Retrieve statistics about the page_pool. This API is only available 83 * if the kernel has been configured with ``CONFIG_PAGE_POOL_STATS=y``. 84 * A pointer to a caller allocated struct page_pool_stats structure 85 * is passed to this API which is filled in. The caller can then report 86 * those stats to the user (perhaps via ethtool, debugfs, etc.). 87 */ 88 void page_pool_get_stats(const struct page_pool *pool, 89 struct page_pool_stats *stats) 90 { 91 int cpu = 0; 92 93 /* The caller is responsible to initialize stats. */ 94 stats->alloc_stats.fast += pool->alloc_stats.fast; 95 stats->alloc_stats.slow += pool->alloc_stats.slow; 96 stats->alloc_stats.slow_high_order += pool->alloc_stats.slow_high_order; 97 stats->alloc_stats.empty += pool->alloc_stats.empty; 98 stats->alloc_stats.refill += pool->alloc_stats.refill; 99 stats->alloc_stats.waive += pool->alloc_stats.waive; 100 101 for_each_possible_cpu(cpu) { 102 const struct page_pool_recycle_stats *pcpu = 103 per_cpu_ptr(pool->recycle_stats, cpu); 104 105 stats->recycle_stats.cached += pcpu->cached; 106 stats->recycle_stats.cache_full += pcpu->cache_full; 107 stats->recycle_stats.ring += pcpu->ring; 108 stats->recycle_stats.ring_full += pcpu->ring_full; 109 stats->recycle_stats.released_refcnt += pcpu->released_refcnt; 110 } 111 } 112 EXPORT_SYMBOL(page_pool_get_stats); 113 114 u8 *page_pool_ethtool_stats_get_strings(u8 *data) 115 { 116 int i; 117 118 for (i = 0; i < ARRAY_SIZE(pp_stats); i++) { 119 memcpy(data, pp_stats[i], ETH_GSTRING_LEN); 120 data += ETH_GSTRING_LEN; 121 } 122 123 return data; 124 } 125 EXPORT_SYMBOL(page_pool_ethtool_stats_get_strings); 126 127 int page_pool_ethtool_stats_get_count(void) 128 { 129 return ARRAY_SIZE(pp_stats); 130 } 131 EXPORT_SYMBOL(page_pool_ethtool_stats_get_count); 132 133 u64 *page_pool_ethtool_stats_get(u64 *data, const void *stats) 134 { 135 const struct page_pool_stats *pool_stats = stats; 136 137 *data++ = pool_stats->alloc_stats.fast; 138 *data++ = pool_stats->alloc_stats.slow; 139 *data++ = pool_stats->alloc_stats.slow_high_order; 140 *data++ = pool_stats->alloc_stats.empty; 141 *data++ = pool_stats->alloc_stats.refill; 142 *data++ = pool_stats->alloc_stats.waive; 143 *data++ = pool_stats->recycle_stats.cached; 144 *data++ = pool_stats->recycle_stats.cache_full; 145 *data++ = pool_stats->recycle_stats.ring; 146 *data++ = pool_stats->recycle_stats.ring_full; 147 *data++ = pool_stats->recycle_stats.released_refcnt; 148 149 return data; 150 } 151 EXPORT_SYMBOL(page_pool_ethtool_stats_get); 152 153 #else 154 #define alloc_stat_inc(...) do { } while (0) 155 #define recycle_stat_inc(...) do { } while (0) 156 #define recycle_stat_add(...) do { } while (0) 157 #endif 158 159 static bool page_pool_producer_lock(struct page_pool *pool) 160 __acquires(&pool->ring.producer_lock) 161 { 162 bool in_softirq = in_softirq(); 163 164 if (in_softirq) 165 spin_lock(&pool->ring.producer_lock); 166 else 167 spin_lock_bh(&pool->ring.producer_lock); 168 169 return in_softirq; 170 } 171 172 static void page_pool_producer_unlock(struct page_pool *pool, 173 bool in_softirq) 174 __releases(&pool->ring.producer_lock) 175 { 176 if (in_softirq) 177 spin_unlock(&pool->ring.producer_lock); 178 else 179 spin_unlock_bh(&pool->ring.producer_lock); 180 } 181 182 static void page_pool_struct_check(void) 183 { 184 CACHELINE_ASSERT_GROUP_MEMBER(struct page_pool, frag, frag_users); 185 CACHELINE_ASSERT_GROUP_MEMBER(struct page_pool, frag, frag_page); 186 CACHELINE_ASSERT_GROUP_MEMBER(struct page_pool, frag, frag_offset); 187 CACHELINE_ASSERT_GROUP_SIZE(struct page_pool, frag, 188 PAGE_POOL_FRAG_GROUP_ALIGN); 189 } 190 191 static int page_pool_init(struct page_pool *pool, 192 const struct page_pool_params *params, 193 int cpuid) 194 { 195 unsigned int ring_qsize = 1024; /* Default */ 196 struct netdev_rx_queue *rxq; 197 int err; 198 199 page_pool_struct_check(); 200 201 memcpy(&pool->p, ¶ms->fast, sizeof(pool->p)); 202 memcpy(&pool->slow, ¶ms->slow, sizeof(pool->slow)); 203 204 pool->cpuid = cpuid; 205 pool->dma_sync_for_cpu = true; 206 207 /* Validate only known flags were used */ 208 if (pool->slow.flags & ~PP_FLAG_ALL) 209 return -EINVAL; 210 211 if (pool->p.pool_size) 212 ring_qsize = min(pool->p.pool_size, 16384); 213 214 /* DMA direction is either DMA_FROM_DEVICE or DMA_BIDIRECTIONAL. 215 * DMA_BIDIRECTIONAL is for allowing page used for DMA sending, 216 * which is the XDP_TX use-case. 217 */ 218 if (pool->slow.flags & PP_FLAG_DMA_MAP) { 219 if ((pool->p.dma_dir != DMA_FROM_DEVICE) && 220 (pool->p.dma_dir != DMA_BIDIRECTIONAL)) 221 return -EINVAL; 222 223 pool->dma_map = true; 224 } 225 226 if (pool->slow.flags & PP_FLAG_DMA_SYNC_DEV) { 227 /* In order to request DMA-sync-for-device the page 228 * needs to be mapped 229 */ 230 if (!(pool->slow.flags & PP_FLAG_DMA_MAP)) 231 return -EINVAL; 232 233 if (!pool->p.max_len) 234 return -EINVAL; 235 236 pool->dma_sync = true; 237 238 /* pool->p.offset has to be set according to the address 239 * offset used by the DMA engine to start copying rx data 240 */ 241 } 242 243 pool->has_init_callback = !!pool->slow.init_callback; 244 245 #ifdef CONFIG_PAGE_POOL_STATS 246 if (!(pool->slow.flags & PP_FLAG_SYSTEM_POOL)) { 247 pool->recycle_stats = alloc_percpu(struct page_pool_recycle_stats); 248 if (!pool->recycle_stats) 249 return -ENOMEM; 250 } else { 251 /* For system page pool instance we use a singular stats object 252 * instead of allocating a separate percpu variable for each 253 * (also percpu) page pool instance. 254 */ 255 pool->recycle_stats = &pp_system_recycle_stats; 256 pool->system = true; 257 } 258 #endif 259 260 if (ptr_ring_init(&pool->ring, ring_qsize, GFP_KERNEL) < 0) { 261 #ifdef CONFIG_PAGE_POOL_STATS 262 if (!pool->system) 263 free_percpu(pool->recycle_stats); 264 #endif 265 return -ENOMEM; 266 } 267 268 atomic_set(&pool->pages_state_release_cnt, 0); 269 270 /* Driver calling page_pool_create() also call page_pool_destroy() */ 271 refcount_set(&pool->user_cnt, 1); 272 273 xa_init_flags(&pool->dma_mapped, XA_FLAGS_ALLOC1); 274 275 if (pool->slow.flags & PP_FLAG_ALLOW_UNREADABLE_NETMEM) { 276 netdev_assert_locked(pool->slow.netdev); 277 rxq = __netif_get_rx_queue(pool->slow.netdev, 278 pool->slow.queue_idx); 279 pool->mp_priv = rxq->mp_params.mp_priv; 280 pool->mp_ops = rxq->mp_params.mp_ops; 281 } 282 283 if (pool->mp_ops) { 284 if (!pool->dma_map || !pool->dma_sync) { 285 err = -EOPNOTSUPP; 286 goto free_ptr_ring; 287 } 288 289 if (WARN_ON(!is_kernel_rodata((unsigned long)pool->mp_ops))) { 290 err = -EFAULT; 291 goto free_ptr_ring; 292 } 293 294 err = pool->mp_ops->init(pool); 295 if (err) { 296 pr_warn("%s() mem-provider init failed %d\n", __func__, 297 err); 298 goto free_ptr_ring; 299 } 300 301 static_branch_inc(&page_pool_mem_providers); 302 } else if (pool->p.order > MAX_PAGE_ORDER) { 303 err = -EINVAL; 304 goto free_ptr_ring; 305 } 306 307 return 0; 308 309 free_ptr_ring: 310 ptr_ring_cleanup(&pool->ring, NULL); 311 xa_destroy(&pool->dma_mapped); 312 #ifdef CONFIG_PAGE_POOL_STATS 313 if (!pool->system) 314 free_percpu(pool->recycle_stats); 315 #endif 316 return err; 317 } 318 319 static void page_pool_uninit(struct page_pool *pool) 320 { 321 ptr_ring_cleanup(&pool->ring, NULL); 322 xa_destroy(&pool->dma_mapped); 323 324 #ifdef CONFIG_PAGE_POOL_STATS 325 if (!pool->system) 326 free_percpu(pool->recycle_stats); 327 #endif 328 329 if (pool->mp_ops) { 330 pool->mp_ops->destroy(pool); 331 static_branch_dec(&page_pool_mem_providers); 332 } 333 } 334 335 /** 336 * page_pool_create_percpu() - create a page pool for a given cpu. 337 * @params: parameters, see struct page_pool_params 338 * @cpuid: cpu identifier 339 */ 340 struct page_pool * 341 page_pool_create_percpu(const struct page_pool_params *params, int cpuid) 342 { 343 struct page_pool *pool; 344 int err; 345 346 pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, params->nid); 347 if (!pool) 348 return ERR_PTR(-ENOMEM); 349 350 err = page_pool_init(pool, params, cpuid); 351 if (err < 0) 352 goto err_free; 353 354 err = page_pool_list(pool); 355 if (err) 356 goto err_uninit; 357 358 return pool; 359 360 err_uninit: 361 page_pool_uninit(pool); 362 err_free: 363 pr_warn("%s() gave up with errno %d\n", __func__, err); 364 kfree(pool); 365 return ERR_PTR(err); 366 } 367 EXPORT_SYMBOL(page_pool_create_percpu); 368 369 /** 370 * page_pool_create() - create a page pool 371 * @params: parameters, see struct page_pool_params 372 */ 373 struct page_pool *page_pool_create(const struct page_pool_params *params) 374 { 375 return page_pool_create_percpu(params, -1); 376 } 377 EXPORT_SYMBOL(page_pool_create); 378 379 static void page_pool_return_netmem(struct page_pool *pool, netmem_ref netmem); 380 381 static noinline netmem_ref page_pool_refill_alloc_cache(struct page_pool *pool) 382 { 383 struct ptr_ring *r = &pool->ring; 384 netmem_ref netmem; 385 int pref_nid; /* preferred NUMA node */ 386 387 /* Quicker fallback, avoid locks when ring is empty */ 388 if (__ptr_ring_empty(r)) { 389 alloc_stat_inc(pool, empty); 390 return 0; 391 } 392 393 /* Softirq guarantee CPU and thus NUMA node is stable. This, 394 * assumes CPU refilling driver RX-ring will also run RX-NAPI. 395 */ 396 #ifdef CONFIG_NUMA 397 pref_nid = (pool->p.nid == NUMA_NO_NODE) ? numa_mem_id() : pool->p.nid; 398 #else 399 /* Ignore pool->p.nid setting if !CONFIG_NUMA, helps compiler */ 400 pref_nid = numa_mem_id(); /* will be zero like page_to_nid() */ 401 #endif 402 403 /* Refill alloc array, but only if NUMA match */ 404 do { 405 netmem = (__force netmem_ref)__ptr_ring_consume(r); 406 if (unlikely(!netmem)) 407 break; 408 409 if (likely(netmem_is_pref_nid(netmem, pref_nid))) { 410 pool->alloc.cache[pool->alloc.count++] = netmem; 411 } else { 412 /* NUMA mismatch; 413 * (1) release 1 page to page-allocator and 414 * (2) break out to fallthrough to alloc_pages_node. 415 * This limit stress on page buddy alloactor. 416 */ 417 page_pool_return_netmem(pool, netmem); 418 alloc_stat_inc(pool, waive); 419 netmem = 0; 420 break; 421 } 422 } while (pool->alloc.count < PP_ALLOC_CACHE_REFILL); 423 424 /* Return last page */ 425 if (likely(pool->alloc.count > 0)) { 426 netmem = pool->alloc.cache[--pool->alloc.count]; 427 alloc_stat_inc(pool, refill); 428 } 429 430 return netmem; 431 } 432 433 /* fast path */ 434 static netmem_ref __page_pool_get_cached(struct page_pool *pool) 435 { 436 netmem_ref netmem; 437 438 /* Caller MUST guarantee safe non-concurrent access, e.g. softirq */ 439 if (likely(pool->alloc.count)) { 440 /* Fast-path */ 441 netmem = pool->alloc.cache[--pool->alloc.count]; 442 alloc_stat_inc(pool, fast); 443 } else { 444 netmem = page_pool_refill_alloc_cache(pool); 445 } 446 447 return netmem; 448 } 449 450 static void __page_pool_dma_sync_for_device(const struct page_pool *pool, 451 netmem_ref netmem, 452 u32 dma_sync_size) 453 { 454 #if defined(CONFIG_HAS_DMA) && defined(CONFIG_DMA_NEED_SYNC) 455 dma_addr_t dma_addr = page_pool_get_dma_addr_netmem(netmem); 456 457 dma_sync_size = min(dma_sync_size, pool->p.max_len); 458 __dma_sync_single_for_device(pool->p.dev, dma_addr + pool->p.offset, 459 dma_sync_size, pool->p.dma_dir); 460 #endif 461 } 462 463 static __always_inline void 464 page_pool_dma_sync_for_device(const struct page_pool *pool, 465 netmem_ref netmem, 466 u32 dma_sync_size) 467 { 468 if (pool->dma_sync && dma_dev_need_sync(pool->p.dev)) { 469 rcu_read_lock(); 470 /* re-check under rcu_read_lock() to sync with page_pool_scrub() */ 471 if (pool->dma_sync) 472 __page_pool_dma_sync_for_device(pool, netmem, 473 dma_sync_size); 474 rcu_read_unlock(); 475 } 476 } 477 478 static int page_pool_register_dma_index(struct page_pool *pool, 479 netmem_ref netmem, gfp_t gfp) 480 { 481 int err = 0; 482 u32 id; 483 484 if (unlikely(!PP_DMA_INDEX_BITS)) 485 goto out; 486 487 if (in_softirq()) 488 err = xa_alloc(&pool->dma_mapped, &id, netmem_to_page(netmem), 489 PP_DMA_INDEX_LIMIT, gfp); 490 else 491 err = xa_alloc_bh(&pool->dma_mapped, &id, netmem_to_page(netmem), 492 PP_DMA_INDEX_LIMIT, gfp); 493 if (err) { 494 WARN_ONCE(err != -ENOMEM, "couldn't track DMA mapping, please report to netdev@"); 495 goto out; 496 } 497 498 netmem_set_dma_index(netmem, id); 499 out: 500 return err; 501 } 502 503 static int page_pool_release_dma_index(struct page_pool *pool, 504 netmem_ref netmem) 505 { 506 struct page *old, *page = netmem_to_page(netmem); 507 unsigned long id; 508 509 if (unlikely(!PP_DMA_INDEX_BITS)) 510 return 0; 511 512 id = netmem_get_dma_index(netmem); 513 if (!id) 514 return -1; 515 516 if (in_softirq()) 517 old = xa_cmpxchg(&pool->dma_mapped, id, page, NULL, 0); 518 else 519 old = xa_cmpxchg_bh(&pool->dma_mapped, id, page, NULL, 0); 520 if (old != page) 521 return -1; 522 523 netmem_set_dma_index(netmem, 0); 524 525 return 0; 526 } 527 528 static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem, gfp_t gfp) 529 { 530 dma_addr_t dma; 531 int err; 532 533 /* Setup DMA mapping: use 'struct page' area for storing DMA-addr 534 * since dma_addr_t can be either 32 or 64 bits and does not always fit 535 * into page private data (i.e 32bit cpu with 64bit DMA caps) 536 * This mapping is kept for lifetime of page, until leaving pool. 537 */ 538 dma = dma_map_page_attrs(pool->p.dev, netmem_to_page(netmem), 0, 539 (PAGE_SIZE << pool->p.order), pool->p.dma_dir, 540 DMA_ATTR_SKIP_CPU_SYNC | 541 DMA_ATTR_WEAK_ORDERING); 542 if (dma_mapping_error(pool->p.dev, dma)) 543 return false; 544 545 if (page_pool_set_dma_addr_netmem(netmem, dma)) { 546 WARN_ONCE(1, "unexpected DMA address, please report to netdev@"); 547 goto unmap_failed; 548 } 549 550 err = page_pool_register_dma_index(pool, netmem, gfp); 551 if (err) 552 goto unset_failed; 553 554 page_pool_dma_sync_for_device(pool, netmem, pool->p.max_len); 555 556 return true; 557 558 unset_failed: 559 page_pool_set_dma_addr_netmem(netmem, 0); 560 unmap_failed: 561 dma_unmap_page_attrs(pool->p.dev, dma, 562 PAGE_SIZE << pool->p.order, pool->p.dma_dir, 563 DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING); 564 return false; 565 } 566 567 static struct page *__page_pool_alloc_page_order(struct page_pool *pool, 568 gfp_t gfp) 569 { 570 struct page *page; 571 572 gfp |= __GFP_COMP; 573 page = alloc_pages_node(pool->p.nid, gfp, pool->p.order); 574 if (unlikely(!page)) 575 return NULL; 576 577 if (pool->dma_map && unlikely(!page_pool_dma_map(pool, page_to_netmem(page), gfp))) { 578 put_page(page); 579 return NULL; 580 } 581 582 alloc_stat_inc(pool, slow_high_order); 583 page_pool_set_pp_info(pool, page_to_netmem(page)); 584 585 /* Track how many pages are held 'in-flight' */ 586 pool->pages_state_hold_cnt++; 587 trace_page_pool_state_hold(pool, page_to_netmem(page), 588 pool->pages_state_hold_cnt); 589 return page; 590 } 591 592 /* slow path */ 593 static noinline netmem_ref __page_pool_alloc_netmems_slow(struct page_pool *pool, 594 gfp_t gfp) 595 { 596 const int bulk = PP_ALLOC_CACHE_REFILL; 597 unsigned int pp_order = pool->p.order; 598 bool dma_map = pool->dma_map; 599 netmem_ref netmem; 600 int i, nr_pages; 601 602 /* Unconditionally set NOWARN if allocating from NAPI. 603 * Drivers forget to set it, and OOM reports on packet Rx are useless. 604 */ 605 if ((gfp & GFP_ATOMIC) == GFP_ATOMIC) 606 gfp |= __GFP_NOWARN; 607 608 /* Don't support bulk alloc for high-order pages */ 609 if (unlikely(pp_order)) 610 return page_to_netmem(__page_pool_alloc_page_order(pool, gfp)); 611 612 /* Unnecessary as alloc cache is empty, but guarantees zero count */ 613 if (unlikely(pool->alloc.count > 0)) 614 return pool->alloc.cache[--pool->alloc.count]; 615 616 /* Mark empty alloc.cache slots "empty" for alloc_pages_bulk */ 617 memset(&pool->alloc.cache, 0, sizeof(void *) * bulk); 618 619 nr_pages = alloc_pages_bulk_node(gfp, pool->p.nid, bulk, 620 (struct page **)pool->alloc.cache); 621 if (unlikely(!nr_pages)) 622 return 0; 623 624 /* Pages have been filled into alloc.cache array, but count is zero and 625 * page element have not been (possibly) DMA mapped. 626 */ 627 for (i = 0; i < nr_pages; i++) { 628 netmem = pool->alloc.cache[i]; 629 if (dma_map && unlikely(!page_pool_dma_map(pool, netmem, gfp))) { 630 put_page(netmem_to_page(netmem)); 631 continue; 632 } 633 634 page_pool_set_pp_info(pool, netmem); 635 pool->alloc.cache[pool->alloc.count++] = netmem; 636 /* Track how many pages are held 'in-flight' */ 637 pool->pages_state_hold_cnt++; 638 trace_page_pool_state_hold(pool, netmem, 639 pool->pages_state_hold_cnt); 640 } 641 642 /* Return last page */ 643 if (likely(pool->alloc.count > 0)) { 644 netmem = pool->alloc.cache[--pool->alloc.count]; 645 alloc_stat_inc(pool, slow); 646 } else { 647 netmem = 0; 648 } 649 650 /* When page just alloc'ed is should/must have refcnt 1. */ 651 return netmem; 652 } 653 654 /* For using page_pool replace: alloc_pages() API calls, but provide 655 * synchronization guarantee for allocation side. 656 */ 657 netmem_ref page_pool_alloc_netmems(struct page_pool *pool, gfp_t gfp) 658 { 659 netmem_ref netmem; 660 661 /* Fast-path: Get a page from cache */ 662 netmem = __page_pool_get_cached(pool); 663 if (netmem) 664 return netmem; 665 666 /* Slow-path: cache empty, do real allocation */ 667 if (static_branch_unlikely(&page_pool_mem_providers) && pool->mp_ops) 668 netmem = pool->mp_ops->alloc_netmems(pool, gfp); 669 else 670 netmem = __page_pool_alloc_netmems_slow(pool, gfp); 671 return netmem; 672 } 673 EXPORT_SYMBOL(page_pool_alloc_netmems); 674 ALLOW_ERROR_INJECTION(page_pool_alloc_netmems, NULL); 675 676 struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp) 677 { 678 return netmem_to_page(page_pool_alloc_netmems(pool, gfp)); 679 } 680 EXPORT_SYMBOL(page_pool_alloc_pages); 681 682 /* Calculate distance between two u32 values, valid if distance is below 2^(31) 683 * https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution 684 */ 685 #define _distance(a, b) (s32)((a) - (b)) 686 687 s32 page_pool_inflight(const struct page_pool *pool, bool strict) 688 { 689 u32 release_cnt = atomic_read(&pool->pages_state_release_cnt); 690 u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt); 691 s32 inflight; 692 693 inflight = _distance(hold_cnt, release_cnt); 694 695 if (strict) { 696 trace_page_pool_release(pool, inflight, hold_cnt, release_cnt); 697 WARN(inflight < 0, "Negative(%d) inflight packet-pages", 698 inflight); 699 } else { 700 inflight = max(0, inflight); 701 } 702 703 return inflight; 704 } 705 706 void page_pool_set_pp_info(struct page_pool *pool, netmem_ref netmem) 707 { 708 netmem_set_pp(netmem, pool); 709 netmem_or_pp_magic(netmem, PP_SIGNATURE); 710 711 /* Ensuring all pages have been split into one fragment initially: 712 * page_pool_set_pp_info() is only called once for every page when it 713 * is allocated from the page allocator and page_pool_fragment_page() 714 * is dirtying the same cache line as the page->pp_magic above, so 715 * the overhead is negligible. 716 */ 717 page_pool_fragment_netmem(netmem, 1); 718 if (pool->has_init_callback) 719 pool->slow.init_callback(netmem, pool->slow.init_arg); 720 } 721 722 void page_pool_clear_pp_info(netmem_ref netmem) 723 { 724 netmem_clear_pp_magic(netmem); 725 netmem_set_pp(netmem, NULL); 726 } 727 728 static __always_inline void __page_pool_release_netmem_dma(struct page_pool *pool, 729 netmem_ref netmem) 730 { 731 dma_addr_t dma; 732 733 if (!pool->dma_map) 734 /* Always account for inflight pages, even if we didn't 735 * map them 736 */ 737 return; 738 739 if (page_pool_release_dma_index(pool, netmem)) 740 return; 741 742 dma = page_pool_get_dma_addr_netmem(netmem); 743 744 /* When page is unmapped, it cannot be returned to our pool */ 745 dma_unmap_page_attrs(pool->p.dev, dma, 746 PAGE_SIZE << pool->p.order, pool->p.dma_dir, 747 DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING); 748 page_pool_set_dma_addr_netmem(netmem, 0); 749 } 750 751 /* Disconnects a page (from a page_pool). API users can have a need 752 * to disconnect a page (from a page_pool), to allow it to be used as 753 * a regular page (that will eventually be returned to the normal 754 * page-allocator via put_page). 755 */ 756 static void page_pool_return_netmem(struct page_pool *pool, netmem_ref netmem) 757 { 758 int count; 759 bool put; 760 761 put = true; 762 if (static_branch_unlikely(&page_pool_mem_providers) && pool->mp_ops) 763 put = pool->mp_ops->release_netmem(pool, netmem); 764 else 765 __page_pool_release_netmem_dma(pool, netmem); 766 767 /* This may be the last page returned, releasing the pool, so 768 * it is not safe to reference pool afterwards. 769 */ 770 count = atomic_inc_return_relaxed(&pool->pages_state_release_cnt); 771 trace_page_pool_state_release(pool, netmem, count); 772 773 if (put) { 774 page_pool_clear_pp_info(netmem); 775 put_page(netmem_to_page(netmem)); 776 } 777 /* An optimization would be to call __free_pages(page, pool->p.order) 778 * knowing page is not part of page-cache (thus avoiding a 779 * __page_cache_release() call). 780 */ 781 } 782 783 static bool page_pool_recycle_in_ring(struct page_pool *pool, netmem_ref netmem) 784 { 785 bool in_softirq, ret; 786 787 /* BH protection not needed if current is softirq */ 788 in_softirq = page_pool_producer_lock(pool); 789 ret = !__ptr_ring_produce(&pool->ring, (__force void *)netmem); 790 if (ret) 791 recycle_stat_inc(pool, ring); 792 page_pool_producer_unlock(pool, in_softirq); 793 794 return ret; 795 } 796 797 /* Only allow direct recycling in special circumstances, into the 798 * alloc side cache. E.g. during RX-NAPI processing for XDP_DROP use-case. 799 * 800 * Caller must provide appropriate safe context. 801 */ 802 static bool page_pool_recycle_in_cache(netmem_ref netmem, 803 struct page_pool *pool) 804 { 805 if (unlikely(pool->alloc.count == PP_ALLOC_CACHE_SIZE)) { 806 recycle_stat_inc(pool, cache_full); 807 return false; 808 } 809 810 /* Caller MUST have verified/know (page_ref_count(page) == 1) */ 811 pool->alloc.cache[pool->alloc.count++] = netmem; 812 recycle_stat_inc(pool, cached); 813 return true; 814 } 815 816 static bool __page_pool_page_can_be_recycled(netmem_ref netmem) 817 { 818 return netmem_is_net_iov(netmem) || 819 (page_ref_count(netmem_to_page(netmem)) == 1 && 820 !page_is_pfmemalloc(netmem_to_page(netmem))); 821 } 822 823 /* If the page refcnt == 1, this will try to recycle the page. 824 * If pool->dma_sync is set, we'll try to sync the DMA area for 825 * the configured size min(dma_sync_size, pool->max_len). 826 * If the page refcnt != 1, then the page will be returned to memory 827 * subsystem. 828 */ 829 static __always_inline netmem_ref 830 __page_pool_put_page(struct page_pool *pool, netmem_ref netmem, 831 unsigned int dma_sync_size, bool allow_direct) 832 { 833 lockdep_assert_no_hardirq(); 834 835 /* This allocator is optimized for the XDP mode that uses 836 * one-frame-per-page, but have fallbacks that act like the 837 * regular page allocator APIs. 838 * 839 * refcnt == 1 means page_pool owns page, and can recycle it. 840 * 841 * page is NOT reusable when allocated when system is under 842 * some pressure. (page_is_pfmemalloc) 843 */ 844 if (likely(__page_pool_page_can_be_recycled(netmem))) { 845 /* Read barrier done in page_ref_count / READ_ONCE */ 846 847 page_pool_dma_sync_for_device(pool, netmem, dma_sync_size); 848 849 if (allow_direct && page_pool_recycle_in_cache(netmem, pool)) 850 return 0; 851 852 /* Page found as candidate for recycling */ 853 return netmem; 854 } 855 856 /* Fallback/non-XDP mode: API user have elevated refcnt. 857 * 858 * Many drivers split up the page into fragments, and some 859 * want to keep doing this to save memory and do refcnt based 860 * recycling. Support this use case too, to ease drivers 861 * switching between XDP/non-XDP. 862 * 863 * In-case page_pool maintains the DMA mapping, API user must 864 * call page_pool_put_page once. In this elevated refcnt 865 * case, the DMA is unmapped/released, as driver is likely 866 * doing refcnt based recycle tricks, meaning another process 867 * will be invoking put_page. 868 */ 869 recycle_stat_inc(pool, released_refcnt); 870 page_pool_return_netmem(pool, netmem); 871 872 return 0; 873 } 874 875 static bool page_pool_napi_local(const struct page_pool *pool) 876 { 877 const struct napi_struct *napi; 878 u32 cpuid; 879 880 /* On PREEMPT_RT the softirq can be preempted by the consumer */ 881 if (IS_ENABLED(CONFIG_PREEMPT_RT)) 882 return false; 883 884 if (unlikely(!in_softirq())) 885 return false; 886 887 /* Allow direct recycle if we have reasons to believe that we are 888 * in the same context as the consumer would run, so there's 889 * no possible race. 890 * __page_pool_put_page() makes sure we're not in hardirq context 891 * and interrupts are enabled prior to accessing the cache. 892 */ 893 cpuid = smp_processor_id(); 894 if (READ_ONCE(pool->cpuid) == cpuid) 895 return true; 896 897 napi = READ_ONCE(pool->p.napi); 898 899 return napi && READ_ONCE(napi->list_owner) == cpuid; 900 } 901 902 void page_pool_put_unrefed_netmem(struct page_pool *pool, netmem_ref netmem, 903 unsigned int dma_sync_size, bool allow_direct) 904 { 905 if (!allow_direct) 906 allow_direct = page_pool_napi_local(pool); 907 908 netmem = __page_pool_put_page(pool, netmem, dma_sync_size, 909 allow_direct); 910 if (netmem && !page_pool_recycle_in_ring(pool, netmem)) { 911 /* Cache full, fallback to free pages */ 912 recycle_stat_inc(pool, ring_full); 913 page_pool_return_netmem(pool, netmem); 914 } 915 } 916 EXPORT_SYMBOL(page_pool_put_unrefed_netmem); 917 918 void page_pool_put_unrefed_page(struct page_pool *pool, struct page *page, 919 unsigned int dma_sync_size, bool allow_direct) 920 { 921 page_pool_put_unrefed_netmem(pool, page_to_netmem(page), dma_sync_size, 922 allow_direct); 923 } 924 EXPORT_SYMBOL(page_pool_put_unrefed_page); 925 926 static void page_pool_recycle_ring_bulk(struct page_pool *pool, 927 netmem_ref *bulk, 928 u32 bulk_len) 929 { 930 bool in_softirq; 931 u32 i; 932 933 /* Bulk produce into ptr_ring page_pool cache */ 934 in_softirq = page_pool_producer_lock(pool); 935 936 for (i = 0; i < bulk_len; i++) { 937 if (__ptr_ring_produce(&pool->ring, (__force void *)bulk[i])) { 938 /* ring full */ 939 recycle_stat_inc(pool, ring_full); 940 break; 941 } 942 } 943 944 page_pool_producer_unlock(pool, in_softirq); 945 recycle_stat_add(pool, ring, i); 946 947 /* Hopefully all pages were returned into ptr_ring */ 948 if (likely(i == bulk_len)) 949 return; 950 951 /* 952 * ptr_ring cache is full, free remaining pages outside producer lock 953 * since put_page() with refcnt == 1 can be an expensive operation. 954 */ 955 for (; i < bulk_len; i++) 956 page_pool_return_netmem(pool, bulk[i]); 957 } 958 959 /** 960 * page_pool_put_netmem_bulk() - release references on multiple netmems 961 * @data: array holding netmem references 962 * @count: number of entries in @data 963 * 964 * Tries to refill a number of netmems into the ptr_ring cache holding ptr_ring 965 * producer lock. If the ptr_ring is full, page_pool_put_netmem_bulk() 966 * will release leftover netmems to the memory provider. 967 * page_pool_put_netmem_bulk() is suitable to be run inside the driver NAPI tx 968 * completion loop for the XDP_REDIRECT use case. 969 * 970 * Please note the caller must not use data area after running 971 * page_pool_put_netmem_bulk(), as this function overwrites it. 972 */ 973 void page_pool_put_netmem_bulk(netmem_ref *data, u32 count) 974 { 975 u32 bulk_len = 0; 976 977 for (u32 i = 0; i < count; i++) { 978 netmem_ref netmem = netmem_compound_head(data[i]); 979 980 if (page_pool_unref_and_test(netmem)) 981 data[bulk_len++] = netmem; 982 } 983 984 count = bulk_len; 985 while (count) { 986 netmem_ref bulk[XDP_BULK_QUEUE_SIZE]; 987 struct page_pool *pool = NULL; 988 bool allow_direct; 989 u32 foreign = 0; 990 991 bulk_len = 0; 992 993 for (u32 i = 0; i < count; i++) { 994 struct page_pool *netmem_pp; 995 netmem_ref netmem = data[i]; 996 997 netmem_pp = netmem_get_pp(netmem); 998 if (unlikely(!pool)) { 999 pool = netmem_pp; 1000 allow_direct = page_pool_napi_local(pool); 1001 } else if (netmem_pp != pool) { 1002 /* 1003 * If the netmem belongs to a different 1004 * page_pool, save it for another round. 1005 */ 1006 data[foreign++] = netmem; 1007 continue; 1008 } 1009 1010 netmem = __page_pool_put_page(pool, netmem, -1, 1011 allow_direct); 1012 /* Approved for bulk recycling in ptr_ring cache */ 1013 if (netmem) 1014 bulk[bulk_len++] = netmem; 1015 } 1016 1017 if (bulk_len) 1018 page_pool_recycle_ring_bulk(pool, bulk, bulk_len); 1019 1020 count = foreign; 1021 } 1022 } 1023 EXPORT_SYMBOL(page_pool_put_netmem_bulk); 1024 1025 static netmem_ref page_pool_drain_frag(struct page_pool *pool, 1026 netmem_ref netmem) 1027 { 1028 long drain_count = BIAS_MAX - pool->frag_users; 1029 1030 /* Some user is still using the page frag */ 1031 if (likely(page_pool_unref_netmem(netmem, drain_count))) 1032 return 0; 1033 1034 if (__page_pool_page_can_be_recycled(netmem)) { 1035 page_pool_dma_sync_for_device(pool, netmem, -1); 1036 return netmem; 1037 } 1038 1039 page_pool_return_netmem(pool, netmem); 1040 return 0; 1041 } 1042 1043 static void page_pool_free_frag(struct page_pool *pool) 1044 { 1045 long drain_count = BIAS_MAX - pool->frag_users; 1046 netmem_ref netmem = pool->frag_page; 1047 1048 pool->frag_page = 0; 1049 1050 if (!netmem || page_pool_unref_netmem(netmem, drain_count)) 1051 return; 1052 1053 page_pool_return_netmem(pool, netmem); 1054 } 1055 1056 netmem_ref page_pool_alloc_frag_netmem(struct page_pool *pool, 1057 unsigned int *offset, unsigned int size, 1058 gfp_t gfp) 1059 { 1060 unsigned int max_size = PAGE_SIZE << pool->p.order; 1061 netmem_ref netmem = pool->frag_page; 1062 1063 if (WARN_ON(size > max_size)) 1064 return 0; 1065 1066 size = ALIGN(size, dma_get_cache_alignment()); 1067 *offset = pool->frag_offset; 1068 1069 if (netmem && *offset + size > max_size) { 1070 netmem = page_pool_drain_frag(pool, netmem); 1071 if (netmem) { 1072 recycle_stat_inc(pool, cached); 1073 alloc_stat_inc(pool, fast); 1074 goto frag_reset; 1075 } 1076 } 1077 1078 if (!netmem) { 1079 netmem = page_pool_alloc_netmems(pool, gfp); 1080 if (unlikely(!netmem)) { 1081 pool->frag_page = 0; 1082 return 0; 1083 } 1084 1085 pool->frag_page = netmem; 1086 1087 frag_reset: 1088 pool->frag_users = 1; 1089 *offset = 0; 1090 pool->frag_offset = size; 1091 page_pool_fragment_netmem(netmem, BIAS_MAX); 1092 return netmem; 1093 } 1094 1095 pool->frag_users++; 1096 pool->frag_offset = *offset + size; 1097 return netmem; 1098 } 1099 EXPORT_SYMBOL(page_pool_alloc_frag_netmem); 1100 1101 struct page *page_pool_alloc_frag(struct page_pool *pool, unsigned int *offset, 1102 unsigned int size, gfp_t gfp) 1103 { 1104 return netmem_to_page(page_pool_alloc_frag_netmem(pool, offset, size, 1105 gfp)); 1106 } 1107 EXPORT_SYMBOL(page_pool_alloc_frag); 1108 1109 static void page_pool_empty_ring(struct page_pool *pool) 1110 { 1111 netmem_ref netmem; 1112 1113 /* Empty recycle ring */ 1114 while ((netmem = (__force netmem_ref)ptr_ring_consume_bh(&pool->ring))) { 1115 /* Verify the refcnt invariant of cached pages */ 1116 if (!(netmem_ref_count(netmem) == 1)) 1117 pr_crit("%s() page_pool refcnt %d violation\n", 1118 __func__, netmem_ref_count(netmem)); 1119 1120 page_pool_return_netmem(pool, netmem); 1121 } 1122 } 1123 1124 static void __page_pool_destroy(struct page_pool *pool) 1125 { 1126 if (pool->disconnect) 1127 pool->disconnect(pool); 1128 1129 page_pool_unlist(pool); 1130 page_pool_uninit(pool); 1131 1132 kfree(pool); 1133 } 1134 1135 static void page_pool_empty_alloc_cache_once(struct page_pool *pool) 1136 { 1137 netmem_ref netmem; 1138 1139 if (pool->destroy_cnt) 1140 return; 1141 1142 /* Empty alloc cache, assume caller made sure this is 1143 * no-longer in use, and page_pool_alloc_pages() cannot be 1144 * call concurrently. 1145 */ 1146 while (pool->alloc.count) { 1147 netmem = pool->alloc.cache[--pool->alloc.count]; 1148 page_pool_return_netmem(pool, netmem); 1149 } 1150 } 1151 1152 static void page_pool_scrub(struct page_pool *pool) 1153 { 1154 unsigned long id; 1155 void *ptr; 1156 1157 page_pool_empty_alloc_cache_once(pool); 1158 if (!pool->destroy_cnt++ && pool->dma_map) { 1159 if (pool->dma_sync) { 1160 /* Disable page_pool_dma_sync_for_device() */ 1161 pool->dma_sync = false; 1162 1163 /* Make sure all concurrent returns that may see the old 1164 * value of dma_sync (and thus perform a sync) have 1165 * finished before doing the unmapping below. Skip the 1166 * wait if the device doesn't actually need syncing, or 1167 * if there are no outstanding mapped pages. 1168 */ 1169 if (dma_dev_need_sync(pool->p.dev) && 1170 !xa_empty(&pool->dma_mapped)) 1171 synchronize_net(); 1172 } 1173 1174 xa_for_each(&pool->dma_mapped, id, ptr) 1175 __page_pool_release_netmem_dma(pool, page_to_netmem((struct page *)ptr)); 1176 } 1177 1178 /* No more consumers should exist, but producers could still 1179 * be in-flight. 1180 */ 1181 page_pool_empty_ring(pool); 1182 } 1183 1184 static int page_pool_release(struct page_pool *pool) 1185 { 1186 bool in_softirq; 1187 int inflight; 1188 1189 page_pool_scrub(pool); 1190 inflight = page_pool_inflight(pool, true); 1191 /* Acquire producer lock to make sure producers have exited. */ 1192 in_softirq = page_pool_producer_lock(pool); 1193 page_pool_producer_unlock(pool, in_softirq); 1194 if (!inflight) 1195 __page_pool_destroy(pool); 1196 1197 return inflight; 1198 } 1199 1200 static void page_pool_release_retry(struct work_struct *wq) 1201 { 1202 struct delayed_work *dwq = to_delayed_work(wq); 1203 struct page_pool *pool = container_of(dwq, typeof(*pool), release_dw); 1204 void *netdev; 1205 int inflight; 1206 1207 inflight = page_pool_release(pool); 1208 /* In rare cases, a driver bug may cause inflight to go negative. 1209 * Don't reschedule release if inflight is 0 or negative. 1210 * - If 0, the page_pool has been destroyed 1211 * - if negative, we will never recover 1212 * in both cases no reschedule is necessary. 1213 */ 1214 if (inflight <= 0) 1215 return; 1216 1217 /* Periodic warning for page pools the user can't see */ 1218 netdev = READ_ONCE(pool->slow.netdev); 1219 if (time_after_eq(jiffies, pool->defer_warn) && 1220 (!netdev || netdev == NET_PTR_POISON)) { 1221 int sec = (s32)((u32)jiffies - (u32)pool->defer_start) / HZ; 1222 1223 pr_warn("%s() stalled pool shutdown: id %u, %d inflight %d sec\n", 1224 __func__, pool->user.id, inflight, sec); 1225 pool->defer_warn = jiffies + DEFER_WARN_INTERVAL; 1226 } 1227 1228 /* Still not ready to be disconnected, retry later */ 1229 schedule_delayed_work(&pool->release_dw, DEFER_TIME); 1230 } 1231 1232 void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *), 1233 const struct xdp_mem_info *mem) 1234 { 1235 refcount_inc(&pool->user_cnt); 1236 pool->disconnect = disconnect; 1237 pool->xdp_mem_id = mem->id; 1238 } 1239 1240 /** 1241 * page_pool_enable_direct_recycling() - mark page pool as owned by NAPI 1242 * @pool: page pool to modify 1243 * @napi: NAPI instance to associate the page pool with 1244 * 1245 * Associate a page pool with a NAPI instance for lockless page recycling. 1246 * This is useful when a new page pool has to be added to a NAPI instance 1247 * without disabling that NAPI instance, to mark the point at which control 1248 * path "hands over" the page pool to the NAPI instance. In most cases driver 1249 * can simply set the @napi field in struct page_pool_params, and does not 1250 * have to call this helper. 1251 * 1252 * The function is idempotent, but does not implement any refcounting. 1253 * Single page_pool_disable_direct_recycling() will disable recycling, 1254 * no matter how many times enable was called. 1255 */ 1256 void page_pool_enable_direct_recycling(struct page_pool *pool, 1257 struct napi_struct *napi) 1258 { 1259 if (READ_ONCE(pool->p.napi) == napi) 1260 return; 1261 WARN_ON(!napi || pool->p.napi); 1262 1263 mutex_lock(&page_pools_lock); 1264 WRITE_ONCE(pool->p.napi, napi); 1265 mutex_unlock(&page_pools_lock); 1266 } 1267 EXPORT_SYMBOL(page_pool_enable_direct_recycling); 1268 1269 void page_pool_disable_direct_recycling(struct page_pool *pool) 1270 { 1271 /* Disable direct recycling based on pool->cpuid. 1272 * Paired with READ_ONCE() in page_pool_napi_local(). 1273 */ 1274 WRITE_ONCE(pool->cpuid, -1); 1275 1276 if (!pool->p.napi) 1277 return; 1278 1279 napi_assert_will_not_race(pool->p.napi); 1280 1281 mutex_lock(&page_pools_lock); 1282 WRITE_ONCE(pool->p.napi, NULL); 1283 mutex_unlock(&page_pools_lock); 1284 } 1285 EXPORT_SYMBOL(page_pool_disable_direct_recycling); 1286 1287 void page_pool_destroy(struct page_pool *pool) 1288 { 1289 if (!pool) 1290 return; 1291 1292 if (!page_pool_put(pool)) 1293 return; 1294 1295 page_pool_disable_direct_recycling(pool); 1296 page_pool_free_frag(pool); 1297 1298 if (!page_pool_release(pool)) 1299 return; 1300 1301 page_pool_detached(pool); 1302 pool->defer_start = jiffies; 1303 pool->defer_warn = jiffies + DEFER_WARN_INTERVAL; 1304 1305 INIT_DELAYED_WORK(&pool->release_dw, page_pool_release_retry); 1306 schedule_delayed_work(&pool->release_dw, DEFER_TIME); 1307 } 1308 EXPORT_SYMBOL(page_pool_destroy); 1309 1310 /* Caller must provide appropriate safe context, e.g. NAPI. */ 1311 void page_pool_update_nid(struct page_pool *pool, int new_nid) 1312 { 1313 netmem_ref netmem; 1314 1315 trace_page_pool_update_nid(pool, new_nid); 1316 pool->p.nid = new_nid; 1317 1318 /* Flush pool alloc cache, as refill will check NUMA node */ 1319 while (pool->alloc.count) { 1320 netmem = pool->alloc.cache[--pool->alloc.count]; 1321 page_pool_return_netmem(pool, netmem); 1322 } 1323 } 1324 EXPORT_SYMBOL(page_pool_update_nid); 1325 1326 bool net_mp_niov_set_dma_addr(struct net_iov *niov, dma_addr_t addr) 1327 { 1328 return page_pool_set_dma_addr_netmem(net_iov_to_netmem(niov), addr); 1329 } 1330 1331 /* Associate a niov with a page pool. Should follow with a matching 1332 * net_mp_niov_clear_page_pool() 1333 */ 1334 void net_mp_niov_set_page_pool(struct page_pool *pool, struct net_iov *niov) 1335 { 1336 netmem_ref netmem = net_iov_to_netmem(niov); 1337 1338 page_pool_set_pp_info(pool, netmem); 1339 1340 pool->pages_state_hold_cnt++; 1341 trace_page_pool_state_hold(pool, netmem, pool->pages_state_hold_cnt); 1342 } 1343 1344 /* Disassociate a niov from a page pool. Should only be used in the 1345 * ->release_netmem() path. 1346 */ 1347 void net_mp_niov_clear_page_pool(struct net_iov *niov) 1348 { 1349 netmem_ref netmem = net_iov_to_netmem(niov); 1350 1351 page_pool_clear_pp_info(netmem); 1352 } 1353