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/page_pool/helpers.h> 15 #include <net/xdp.h> 16 17 #include <linux/dma-direction.h> 18 #include <linux/dma-mapping.h> 19 #include <linux/page-flags.h> 20 #include <linux/mm.h> /* for put_page() */ 21 #include <linux/poison.h> 22 #include <linux/ethtool.h> 23 #include <linux/netdevice.h> 24 25 #include <trace/events/page_pool.h> 26 27 #include "page_pool_priv.h" 28 29 #define DEFER_TIME (msecs_to_jiffies(1000)) 30 #define DEFER_WARN_INTERVAL (60 * HZ) 31 32 #define BIAS_MAX (LONG_MAX >> 1) 33 34 #ifdef CONFIG_PAGE_POOL_STATS 35 static DEFINE_PER_CPU(struct page_pool_recycle_stats, pp_system_recycle_stats); 36 37 /* alloc_stat_inc is intended to be used in softirq context */ 38 #define alloc_stat_inc(pool, __stat) (pool->alloc_stats.__stat++) 39 /* recycle_stat_inc is safe to use when preemption is possible. */ 40 #define recycle_stat_inc(pool, __stat) \ 41 do { \ 42 struct page_pool_recycle_stats __percpu *s = pool->recycle_stats; \ 43 this_cpu_inc(s->__stat); \ 44 } while (0) 45 46 #define recycle_stat_add(pool, __stat, val) \ 47 do { \ 48 struct page_pool_recycle_stats __percpu *s = pool->recycle_stats; \ 49 this_cpu_add(s->__stat, val); \ 50 } while (0) 51 52 static const char pp_stats[][ETH_GSTRING_LEN] = { 53 "rx_pp_alloc_fast", 54 "rx_pp_alloc_slow", 55 "rx_pp_alloc_slow_ho", 56 "rx_pp_alloc_empty", 57 "rx_pp_alloc_refill", 58 "rx_pp_alloc_waive", 59 "rx_pp_recycle_cached", 60 "rx_pp_recycle_cache_full", 61 "rx_pp_recycle_ring", 62 "rx_pp_recycle_ring_full", 63 "rx_pp_recycle_released_ref", 64 }; 65 66 /** 67 * page_pool_get_stats() - fetch page pool stats 68 * @pool: pool from which page was allocated 69 * @stats: struct page_pool_stats to fill in 70 * 71 * Retrieve statistics about the page_pool. This API is only available 72 * if the kernel has been configured with ``CONFIG_PAGE_POOL_STATS=y``. 73 * A pointer to a caller allocated struct page_pool_stats structure 74 * is passed to this API which is filled in. The caller can then report 75 * those stats to the user (perhaps via ethtool, debugfs, etc.). 76 */ 77 bool page_pool_get_stats(const struct page_pool *pool, 78 struct page_pool_stats *stats) 79 { 80 int cpu = 0; 81 82 if (!stats) 83 return false; 84 85 /* The caller is responsible to initialize stats. */ 86 stats->alloc_stats.fast += pool->alloc_stats.fast; 87 stats->alloc_stats.slow += pool->alloc_stats.slow; 88 stats->alloc_stats.slow_high_order += pool->alloc_stats.slow_high_order; 89 stats->alloc_stats.empty += pool->alloc_stats.empty; 90 stats->alloc_stats.refill += pool->alloc_stats.refill; 91 stats->alloc_stats.waive += pool->alloc_stats.waive; 92 93 for_each_possible_cpu(cpu) { 94 const struct page_pool_recycle_stats *pcpu = 95 per_cpu_ptr(pool->recycle_stats, cpu); 96 97 stats->recycle_stats.cached += pcpu->cached; 98 stats->recycle_stats.cache_full += pcpu->cache_full; 99 stats->recycle_stats.ring += pcpu->ring; 100 stats->recycle_stats.ring_full += pcpu->ring_full; 101 stats->recycle_stats.released_refcnt += pcpu->released_refcnt; 102 } 103 104 return true; 105 } 106 EXPORT_SYMBOL(page_pool_get_stats); 107 108 u8 *page_pool_ethtool_stats_get_strings(u8 *data) 109 { 110 int i; 111 112 for (i = 0; i < ARRAY_SIZE(pp_stats); i++) { 113 memcpy(data, pp_stats[i], ETH_GSTRING_LEN); 114 data += ETH_GSTRING_LEN; 115 } 116 117 return data; 118 } 119 EXPORT_SYMBOL(page_pool_ethtool_stats_get_strings); 120 121 int page_pool_ethtool_stats_get_count(void) 122 { 123 return ARRAY_SIZE(pp_stats); 124 } 125 EXPORT_SYMBOL(page_pool_ethtool_stats_get_count); 126 127 u64 *page_pool_ethtool_stats_get(u64 *data, const void *stats) 128 { 129 const struct page_pool_stats *pool_stats = stats; 130 131 *data++ = pool_stats->alloc_stats.fast; 132 *data++ = pool_stats->alloc_stats.slow; 133 *data++ = pool_stats->alloc_stats.slow_high_order; 134 *data++ = pool_stats->alloc_stats.empty; 135 *data++ = pool_stats->alloc_stats.refill; 136 *data++ = pool_stats->alloc_stats.waive; 137 *data++ = pool_stats->recycle_stats.cached; 138 *data++ = pool_stats->recycle_stats.cache_full; 139 *data++ = pool_stats->recycle_stats.ring; 140 *data++ = pool_stats->recycle_stats.ring_full; 141 *data++ = pool_stats->recycle_stats.released_refcnt; 142 143 return data; 144 } 145 EXPORT_SYMBOL(page_pool_ethtool_stats_get); 146 147 #else 148 #define alloc_stat_inc(pool, __stat) 149 #define recycle_stat_inc(pool, __stat) 150 #define recycle_stat_add(pool, __stat, val) 151 #endif 152 153 static bool page_pool_producer_lock(struct page_pool *pool) 154 __acquires(&pool->ring.producer_lock) 155 { 156 bool in_softirq = in_softirq(); 157 158 if (in_softirq) 159 spin_lock(&pool->ring.producer_lock); 160 else 161 spin_lock_bh(&pool->ring.producer_lock); 162 163 return in_softirq; 164 } 165 166 static void page_pool_producer_unlock(struct page_pool *pool, 167 bool in_softirq) 168 __releases(&pool->ring.producer_lock) 169 { 170 if (in_softirq) 171 spin_unlock(&pool->ring.producer_lock); 172 else 173 spin_unlock_bh(&pool->ring.producer_lock); 174 } 175 176 static void page_pool_struct_check(void) 177 { 178 CACHELINE_ASSERT_GROUP_MEMBER(struct page_pool, frag, frag_users); 179 CACHELINE_ASSERT_GROUP_MEMBER(struct page_pool, frag, frag_page); 180 CACHELINE_ASSERT_GROUP_MEMBER(struct page_pool, frag, frag_offset); 181 CACHELINE_ASSERT_GROUP_SIZE(struct page_pool, frag, 4 * sizeof(long)); 182 } 183 184 static int page_pool_init(struct page_pool *pool, 185 const struct page_pool_params *params, 186 int cpuid) 187 { 188 unsigned int ring_qsize = 1024; /* Default */ 189 190 page_pool_struct_check(); 191 192 memcpy(&pool->p, ¶ms->fast, sizeof(pool->p)); 193 memcpy(&pool->slow, ¶ms->slow, sizeof(pool->slow)); 194 195 pool->cpuid = cpuid; 196 197 /* Validate only known flags were used */ 198 if (pool->slow.flags & ~PP_FLAG_ALL) 199 return -EINVAL; 200 201 if (pool->p.pool_size) 202 ring_qsize = pool->p.pool_size; 203 204 /* Sanity limit mem that can be pinned down */ 205 if (ring_qsize > 32768) 206 return -E2BIG; 207 208 /* DMA direction is either DMA_FROM_DEVICE or DMA_BIDIRECTIONAL. 209 * DMA_BIDIRECTIONAL is for allowing page used for DMA sending, 210 * which is the XDP_TX use-case. 211 */ 212 if (pool->slow.flags & PP_FLAG_DMA_MAP) { 213 if ((pool->p.dma_dir != DMA_FROM_DEVICE) && 214 (pool->p.dma_dir != DMA_BIDIRECTIONAL)) 215 return -EINVAL; 216 217 pool->dma_map = true; 218 } 219 220 if (pool->slow.flags & PP_FLAG_DMA_SYNC_DEV) { 221 /* In order to request DMA-sync-for-device the page 222 * needs to be mapped 223 */ 224 if (!(pool->slow.flags & PP_FLAG_DMA_MAP)) 225 return -EINVAL; 226 227 if (!pool->p.max_len) 228 return -EINVAL; 229 230 pool->dma_sync = true; 231 232 /* pool->p.offset has to be set according to the address 233 * offset used by the DMA engine to start copying rx data 234 */ 235 } 236 237 pool->has_init_callback = !!pool->slow.init_callback; 238 239 #ifdef CONFIG_PAGE_POOL_STATS 240 if (!(pool->slow.flags & PP_FLAG_SYSTEM_POOL)) { 241 pool->recycle_stats = alloc_percpu(struct page_pool_recycle_stats); 242 if (!pool->recycle_stats) 243 return -ENOMEM; 244 } else { 245 /* For system page pool instance we use a singular stats object 246 * instead of allocating a separate percpu variable for each 247 * (also percpu) page pool instance. 248 */ 249 pool->recycle_stats = &pp_system_recycle_stats; 250 pool->system = true; 251 } 252 #endif 253 254 if (ptr_ring_init(&pool->ring, ring_qsize, GFP_KERNEL) < 0) { 255 #ifdef CONFIG_PAGE_POOL_STATS 256 if (!pool->system) 257 free_percpu(pool->recycle_stats); 258 #endif 259 return -ENOMEM; 260 } 261 262 atomic_set(&pool->pages_state_release_cnt, 0); 263 264 /* Driver calling page_pool_create() also call page_pool_destroy() */ 265 refcount_set(&pool->user_cnt, 1); 266 267 if (pool->dma_map) 268 get_device(pool->p.dev); 269 270 return 0; 271 } 272 273 static void page_pool_uninit(struct page_pool *pool) 274 { 275 ptr_ring_cleanup(&pool->ring, NULL); 276 277 if (pool->dma_map) 278 put_device(pool->p.dev); 279 280 #ifdef CONFIG_PAGE_POOL_STATS 281 if (!pool->system) 282 free_percpu(pool->recycle_stats); 283 #endif 284 } 285 286 /** 287 * page_pool_create_percpu() - create a page pool for a given cpu. 288 * @params: parameters, see struct page_pool_params 289 * @cpuid: cpu identifier 290 */ 291 struct page_pool * 292 page_pool_create_percpu(const struct page_pool_params *params, int cpuid) 293 { 294 struct page_pool *pool; 295 int err; 296 297 pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, params->nid); 298 if (!pool) 299 return ERR_PTR(-ENOMEM); 300 301 err = page_pool_init(pool, params, cpuid); 302 if (err < 0) 303 goto err_free; 304 305 err = page_pool_list(pool); 306 if (err) 307 goto err_uninit; 308 309 return pool; 310 311 err_uninit: 312 page_pool_uninit(pool); 313 err_free: 314 pr_warn("%s() gave up with errno %d\n", __func__, err); 315 kfree(pool); 316 return ERR_PTR(err); 317 } 318 EXPORT_SYMBOL(page_pool_create_percpu); 319 320 /** 321 * page_pool_create() - create a page pool 322 * @params: parameters, see struct page_pool_params 323 */ 324 struct page_pool *page_pool_create(const struct page_pool_params *params) 325 { 326 return page_pool_create_percpu(params, -1); 327 } 328 EXPORT_SYMBOL(page_pool_create); 329 330 static void page_pool_return_page(struct page_pool *pool, netmem_ref netmem); 331 332 static noinline netmem_ref page_pool_refill_alloc_cache(struct page_pool *pool) 333 { 334 struct ptr_ring *r = &pool->ring; 335 netmem_ref netmem; 336 int pref_nid; /* preferred NUMA node */ 337 338 /* Quicker fallback, avoid locks when ring is empty */ 339 if (__ptr_ring_empty(r)) { 340 alloc_stat_inc(pool, empty); 341 return 0; 342 } 343 344 /* Softirq guarantee CPU and thus NUMA node is stable. This, 345 * assumes CPU refilling driver RX-ring will also run RX-NAPI. 346 */ 347 #ifdef CONFIG_NUMA 348 pref_nid = (pool->p.nid == NUMA_NO_NODE) ? numa_mem_id() : pool->p.nid; 349 #else 350 /* Ignore pool->p.nid setting if !CONFIG_NUMA, helps compiler */ 351 pref_nid = numa_mem_id(); /* will be zero like page_to_nid() */ 352 #endif 353 354 /* Refill alloc array, but only if NUMA match */ 355 do { 356 netmem = (__force netmem_ref)__ptr_ring_consume(r); 357 if (unlikely(!netmem)) 358 break; 359 360 if (likely(page_to_nid(netmem_to_page(netmem)) == pref_nid)) { 361 pool->alloc.cache[pool->alloc.count++] = netmem; 362 } else { 363 /* NUMA mismatch; 364 * (1) release 1 page to page-allocator and 365 * (2) break out to fallthrough to alloc_pages_node. 366 * This limit stress on page buddy alloactor. 367 */ 368 page_pool_return_page(pool, netmem); 369 alloc_stat_inc(pool, waive); 370 netmem = 0; 371 break; 372 } 373 } while (pool->alloc.count < PP_ALLOC_CACHE_REFILL); 374 375 /* Return last page */ 376 if (likely(pool->alloc.count > 0)) { 377 netmem = pool->alloc.cache[--pool->alloc.count]; 378 alloc_stat_inc(pool, refill); 379 } 380 381 return netmem; 382 } 383 384 /* fast path */ 385 static netmem_ref __page_pool_get_cached(struct page_pool *pool) 386 { 387 netmem_ref netmem; 388 389 /* Caller MUST guarantee safe non-concurrent access, e.g. softirq */ 390 if (likely(pool->alloc.count)) { 391 /* Fast-path */ 392 netmem = pool->alloc.cache[--pool->alloc.count]; 393 alloc_stat_inc(pool, fast); 394 } else { 395 netmem = page_pool_refill_alloc_cache(pool); 396 } 397 398 return netmem; 399 } 400 401 static void __page_pool_dma_sync_for_device(const struct page_pool *pool, 402 netmem_ref netmem, 403 u32 dma_sync_size) 404 { 405 #if defined(CONFIG_HAS_DMA) && defined(CONFIG_DMA_NEED_SYNC) 406 dma_addr_t dma_addr = page_pool_get_dma_addr_netmem(netmem); 407 408 dma_sync_size = min(dma_sync_size, pool->p.max_len); 409 __dma_sync_single_for_device(pool->p.dev, dma_addr + pool->p.offset, 410 dma_sync_size, pool->p.dma_dir); 411 #endif 412 } 413 414 static __always_inline void 415 page_pool_dma_sync_for_device(const struct page_pool *pool, 416 netmem_ref netmem, 417 u32 dma_sync_size) 418 { 419 if (pool->dma_sync && dma_dev_need_sync(pool->p.dev)) 420 __page_pool_dma_sync_for_device(pool, netmem, dma_sync_size); 421 } 422 423 static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem) 424 { 425 dma_addr_t dma; 426 427 /* Setup DMA mapping: use 'struct page' area for storing DMA-addr 428 * since dma_addr_t can be either 32 or 64 bits and does not always fit 429 * into page private data (i.e 32bit cpu with 64bit DMA caps) 430 * This mapping is kept for lifetime of page, until leaving pool. 431 */ 432 dma = dma_map_page_attrs(pool->p.dev, netmem_to_page(netmem), 0, 433 (PAGE_SIZE << pool->p.order), pool->p.dma_dir, 434 DMA_ATTR_SKIP_CPU_SYNC | 435 DMA_ATTR_WEAK_ORDERING); 436 if (dma_mapping_error(pool->p.dev, dma)) 437 return false; 438 439 if (page_pool_set_dma_addr_netmem(netmem, dma)) 440 goto unmap_failed; 441 442 page_pool_dma_sync_for_device(pool, netmem, pool->p.max_len); 443 444 return true; 445 446 unmap_failed: 447 WARN_ON_ONCE("unexpected DMA address, please report to netdev@"); 448 dma_unmap_page_attrs(pool->p.dev, dma, 449 PAGE_SIZE << pool->p.order, pool->p.dma_dir, 450 DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING); 451 return false; 452 } 453 454 static void page_pool_set_pp_info(struct page_pool *pool, netmem_ref netmem) 455 { 456 struct page *page = netmem_to_page(netmem); 457 458 page->pp = pool; 459 page->pp_magic |= PP_SIGNATURE; 460 461 /* Ensuring all pages have been split into one fragment initially: 462 * page_pool_set_pp_info() is only called once for every page when it 463 * is allocated from the page allocator and page_pool_fragment_page() 464 * is dirtying the same cache line as the page->pp_magic above, so 465 * the overhead is negligible. 466 */ 467 page_pool_fragment_netmem(netmem, 1); 468 if (pool->has_init_callback) 469 pool->slow.init_callback(netmem, pool->slow.init_arg); 470 } 471 472 static void page_pool_clear_pp_info(netmem_ref netmem) 473 { 474 struct page *page = netmem_to_page(netmem); 475 476 page->pp_magic = 0; 477 page->pp = NULL; 478 } 479 480 static struct page *__page_pool_alloc_page_order(struct page_pool *pool, 481 gfp_t gfp) 482 { 483 struct page *page; 484 485 gfp |= __GFP_COMP; 486 page = alloc_pages_node(pool->p.nid, gfp, pool->p.order); 487 if (unlikely(!page)) 488 return NULL; 489 490 if (pool->dma_map && unlikely(!page_pool_dma_map(pool, page_to_netmem(page)))) { 491 put_page(page); 492 return NULL; 493 } 494 495 alloc_stat_inc(pool, slow_high_order); 496 page_pool_set_pp_info(pool, page_to_netmem(page)); 497 498 /* Track how many pages are held 'in-flight' */ 499 pool->pages_state_hold_cnt++; 500 trace_page_pool_state_hold(pool, page_to_netmem(page), 501 pool->pages_state_hold_cnt); 502 return page; 503 } 504 505 /* slow path */ 506 static noinline netmem_ref __page_pool_alloc_pages_slow(struct page_pool *pool, 507 gfp_t gfp) 508 { 509 const int bulk = PP_ALLOC_CACHE_REFILL; 510 unsigned int pp_order = pool->p.order; 511 bool dma_map = pool->dma_map; 512 netmem_ref netmem; 513 int i, nr_pages; 514 515 /* Don't support bulk alloc for high-order pages */ 516 if (unlikely(pp_order)) 517 return page_to_netmem(__page_pool_alloc_page_order(pool, gfp)); 518 519 /* Unnecessary as alloc cache is empty, but guarantees zero count */ 520 if (unlikely(pool->alloc.count > 0)) 521 return pool->alloc.cache[--pool->alloc.count]; 522 523 /* Mark empty alloc.cache slots "empty" for alloc_pages_bulk_array */ 524 memset(&pool->alloc.cache, 0, sizeof(void *) * bulk); 525 526 nr_pages = alloc_pages_bulk_array_node(gfp, 527 pool->p.nid, bulk, 528 (struct page **)pool->alloc.cache); 529 if (unlikely(!nr_pages)) 530 return 0; 531 532 /* Pages have been filled into alloc.cache array, but count is zero and 533 * page element have not been (possibly) DMA mapped. 534 */ 535 for (i = 0; i < nr_pages; i++) { 536 netmem = pool->alloc.cache[i]; 537 if (dma_map && unlikely(!page_pool_dma_map(pool, netmem))) { 538 put_page(netmem_to_page(netmem)); 539 continue; 540 } 541 542 page_pool_set_pp_info(pool, netmem); 543 pool->alloc.cache[pool->alloc.count++] = netmem; 544 /* Track how many pages are held 'in-flight' */ 545 pool->pages_state_hold_cnt++; 546 trace_page_pool_state_hold(pool, netmem, 547 pool->pages_state_hold_cnt); 548 } 549 550 /* Return last page */ 551 if (likely(pool->alloc.count > 0)) { 552 netmem = pool->alloc.cache[--pool->alloc.count]; 553 alloc_stat_inc(pool, slow); 554 } else { 555 netmem = 0; 556 } 557 558 /* When page just alloc'ed is should/must have refcnt 1. */ 559 return netmem; 560 } 561 562 /* For using page_pool replace: alloc_pages() API calls, but provide 563 * synchronization guarantee for allocation side. 564 */ 565 netmem_ref page_pool_alloc_netmem(struct page_pool *pool, gfp_t gfp) 566 { 567 netmem_ref netmem; 568 569 /* Fast-path: Get a page from cache */ 570 netmem = __page_pool_get_cached(pool); 571 if (netmem) 572 return netmem; 573 574 /* Slow-path: cache empty, do real allocation */ 575 netmem = __page_pool_alloc_pages_slow(pool, gfp); 576 return netmem; 577 } 578 EXPORT_SYMBOL(page_pool_alloc_netmem); 579 580 struct page *page_pool_alloc_pages(struct page_pool *pool, gfp_t gfp) 581 { 582 return netmem_to_page(page_pool_alloc_netmem(pool, gfp)); 583 } 584 EXPORT_SYMBOL(page_pool_alloc_pages); 585 ALLOW_ERROR_INJECTION(page_pool_alloc_pages, NULL); 586 587 /* Calculate distance between two u32 values, valid if distance is below 2^(31) 588 * https://en.wikipedia.org/wiki/Serial_number_arithmetic#General_Solution 589 */ 590 #define _distance(a, b) (s32)((a) - (b)) 591 592 s32 page_pool_inflight(const struct page_pool *pool, bool strict) 593 { 594 u32 release_cnt = atomic_read(&pool->pages_state_release_cnt); 595 u32 hold_cnt = READ_ONCE(pool->pages_state_hold_cnt); 596 s32 inflight; 597 598 inflight = _distance(hold_cnt, release_cnt); 599 600 if (strict) { 601 trace_page_pool_release(pool, inflight, hold_cnt, release_cnt); 602 WARN(inflight < 0, "Negative(%d) inflight packet-pages", 603 inflight); 604 } else { 605 inflight = max(0, inflight); 606 } 607 608 return inflight; 609 } 610 611 static __always_inline void __page_pool_release_page_dma(struct page_pool *pool, 612 netmem_ref netmem) 613 { 614 dma_addr_t dma; 615 616 if (!pool->dma_map) 617 /* Always account for inflight pages, even if we didn't 618 * map them 619 */ 620 return; 621 622 dma = page_pool_get_dma_addr_netmem(netmem); 623 624 /* When page is unmapped, it cannot be returned to our pool */ 625 dma_unmap_page_attrs(pool->p.dev, dma, 626 PAGE_SIZE << pool->p.order, pool->p.dma_dir, 627 DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING); 628 page_pool_set_dma_addr_netmem(netmem, 0); 629 } 630 631 /* Disconnects a page (from a page_pool). API users can have a need 632 * to disconnect a page (from a page_pool), to allow it to be used as 633 * a regular page (that will eventually be returned to the normal 634 * page-allocator via put_page). 635 */ 636 void page_pool_return_page(struct page_pool *pool, netmem_ref netmem) 637 { 638 int count; 639 640 __page_pool_release_page_dma(pool, netmem); 641 642 /* This may be the last page returned, releasing the pool, so 643 * it is not safe to reference pool afterwards. 644 */ 645 count = atomic_inc_return_relaxed(&pool->pages_state_release_cnt); 646 trace_page_pool_state_release(pool, netmem, count); 647 648 page_pool_clear_pp_info(netmem); 649 put_page(netmem_to_page(netmem)); 650 /* An optimization would be to call __free_pages(page, pool->p.order) 651 * knowing page is not part of page-cache (thus avoiding a 652 * __page_cache_release() call). 653 */ 654 } 655 656 static bool page_pool_recycle_in_ring(struct page_pool *pool, netmem_ref netmem) 657 { 658 int ret; 659 /* BH protection not needed if current is softirq */ 660 if (in_softirq()) 661 ret = ptr_ring_produce(&pool->ring, (__force void *)netmem); 662 else 663 ret = ptr_ring_produce_bh(&pool->ring, (__force void *)netmem); 664 665 if (!ret) { 666 recycle_stat_inc(pool, ring); 667 return true; 668 } 669 670 return false; 671 } 672 673 /* Only allow direct recycling in special circumstances, into the 674 * alloc side cache. E.g. during RX-NAPI processing for XDP_DROP use-case. 675 * 676 * Caller must provide appropriate safe context. 677 */ 678 static bool page_pool_recycle_in_cache(netmem_ref netmem, 679 struct page_pool *pool) 680 { 681 if (unlikely(pool->alloc.count == PP_ALLOC_CACHE_SIZE)) { 682 recycle_stat_inc(pool, cache_full); 683 return false; 684 } 685 686 /* Caller MUST have verified/know (page_ref_count(page) == 1) */ 687 pool->alloc.cache[pool->alloc.count++] = netmem; 688 recycle_stat_inc(pool, cached); 689 return true; 690 } 691 692 static bool __page_pool_page_can_be_recycled(netmem_ref netmem) 693 { 694 return page_ref_count(netmem_to_page(netmem)) == 1 && 695 !page_is_pfmemalloc(netmem_to_page(netmem)); 696 } 697 698 /* If the page refcnt == 1, this will try to recycle the page. 699 * If pool->dma_sync is set, we'll try to sync the DMA area for 700 * the configured size min(dma_sync_size, pool->max_len). 701 * If the page refcnt != 1, then the page will be returned to memory 702 * subsystem. 703 */ 704 static __always_inline netmem_ref 705 __page_pool_put_page(struct page_pool *pool, netmem_ref netmem, 706 unsigned int dma_sync_size, bool allow_direct) 707 { 708 lockdep_assert_no_hardirq(); 709 710 /* This allocator is optimized for the XDP mode that uses 711 * one-frame-per-page, but have fallbacks that act like the 712 * regular page allocator APIs. 713 * 714 * refcnt == 1 means page_pool owns page, and can recycle it. 715 * 716 * page is NOT reusable when allocated when system is under 717 * some pressure. (page_is_pfmemalloc) 718 */ 719 if (likely(__page_pool_page_can_be_recycled(netmem))) { 720 /* Read barrier done in page_ref_count / READ_ONCE */ 721 722 page_pool_dma_sync_for_device(pool, netmem, dma_sync_size); 723 724 if (allow_direct && page_pool_recycle_in_cache(netmem, pool)) 725 return 0; 726 727 /* Page found as candidate for recycling */ 728 return netmem; 729 } 730 /* Fallback/non-XDP mode: API user have elevated refcnt. 731 * 732 * Many drivers split up the page into fragments, and some 733 * want to keep doing this to save memory and do refcnt based 734 * recycling. Support this use case too, to ease drivers 735 * switching between XDP/non-XDP. 736 * 737 * In-case page_pool maintains the DMA mapping, API user must 738 * call page_pool_put_page once. In this elevated refcnt 739 * case, the DMA is unmapped/released, as driver is likely 740 * doing refcnt based recycle tricks, meaning another process 741 * will be invoking put_page. 742 */ 743 recycle_stat_inc(pool, released_refcnt); 744 page_pool_return_page(pool, netmem); 745 746 return 0; 747 } 748 749 static bool page_pool_napi_local(const struct page_pool *pool) 750 { 751 const struct napi_struct *napi; 752 u32 cpuid; 753 754 if (unlikely(!in_softirq())) 755 return false; 756 757 /* Allow direct recycle if we have reasons to believe that we are 758 * in the same context as the consumer would run, so there's 759 * no possible race. 760 * __page_pool_put_page() makes sure we're not in hardirq context 761 * and interrupts are enabled prior to accessing the cache. 762 */ 763 cpuid = smp_processor_id(); 764 if (READ_ONCE(pool->cpuid) == cpuid) 765 return true; 766 767 napi = READ_ONCE(pool->p.napi); 768 769 return napi && READ_ONCE(napi->list_owner) == cpuid; 770 } 771 772 void page_pool_put_unrefed_netmem(struct page_pool *pool, netmem_ref netmem, 773 unsigned int dma_sync_size, bool allow_direct) 774 { 775 if (!allow_direct) 776 allow_direct = page_pool_napi_local(pool); 777 778 netmem = 779 __page_pool_put_page(pool, netmem, dma_sync_size, allow_direct); 780 if (netmem && !page_pool_recycle_in_ring(pool, netmem)) { 781 /* Cache full, fallback to free pages */ 782 recycle_stat_inc(pool, ring_full); 783 page_pool_return_page(pool, netmem); 784 } 785 } 786 EXPORT_SYMBOL(page_pool_put_unrefed_netmem); 787 788 void page_pool_put_unrefed_page(struct page_pool *pool, struct page *page, 789 unsigned int dma_sync_size, bool allow_direct) 790 { 791 page_pool_put_unrefed_netmem(pool, page_to_netmem(page), dma_sync_size, 792 allow_direct); 793 } 794 EXPORT_SYMBOL(page_pool_put_unrefed_page); 795 796 /** 797 * page_pool_put_page_bulk() - release references on multiple pages 798 * @pool: pool from which pages were allocated 799 * @data: array holding page pointers 800 * @count: number of pages in @data 801 * 802 * Tries to refill a number of pages into the ptr_ring cache holding ptr_ring 803 * producer lock. If the ptr_ring is full, page_pool_put_page_bulk() 804 * will release leftover pages to the page allocator. 805 * page_pool_put_page_bulk() is suitable to be run inside the driver NAPI tx 806 * completion loop for the XDP_REDIRECT use case. 807 * 808 * Please note the caller must not use data area after running 809 * page_pool_put_page_bulk(), as this function overwrites it. 810 */ 811 void page_pool_put_page_bulk(struct page_pool *pool, void **data, 812 int count) 813 { 814 int i, bulk_len = 0; 815 bool allow_direct; 816 bool in_softirq; 817 818 allow_direct = page_pool_napi_local(pool); 819 820 for (i = 0; i < count; i++) { 821 netmem_ref netmem = page_to_netmem(virt_to_head_page(data[i])); 822 823 /* It is not the last user for the page frag case */ 824 if (!page_pool_is_last_ref(netmem)) 825 continue; 826 827 netmem = __page_pool_put_page(pool, netmem, -1, allow_direct); 828 /* Approved for bulk recycling in ptr_ring cache */ 829 if (netmem) 830 data[bulk_len++] = (__force void *)netmem; 831 } 832 833 if (!bulk_len) 834 return; 835 836 /* Bulk producer into ptr_ring page_pool cache */ 837 in_softirq = page_pool_producer_lock(pool); 838 for (i = 0; i < bulk_len; i++) { 839 if (__ptr_ring_produce(&pool->ring, data[i])) { 840 /* ring full */ 841 recycle_stat_inc(pool, ring_full); 842 break; 843 } 844 } 845 recycle_stat_add(pool, ring, i); 846 page_pool_producer_unlock(pool, in_softirq); 847 848 /* Hopefully all pages was return into ptr_ring */ 849 if (likely(i == bulk_len)) 850 return; 851 852 /* ptr_ring cache full, free remaining pages outside producer lock 853 * since put_page() with refcnt == 1 can be an expensive operation 854 */ 855 for (; i < bulk_len; i++) 856 page_pool_return_page(pool, (__force netmem_ref)data[i]); 857 } 858 EXPORT_SYMBOL(page_pool_put_page_bulk); 859 860 static netmem_ref page_pool_drain_frag(struct page_pool *pool, 861 netmem_ref netmem) 862 { 863 long drain_count = BIAS_MAX - pool->frag_users; 864 865 /* Some user is still using the page frag */ 866 if (likely(page_pool_unref_netmem(netmem, drain_count))) 867 return 0; 868 869 if (__page_pool_page_can_be_recycled(netmem)) { 870 page_pool_dma_sync_for_device(pool, netmem, -1); 871 return netmem; 872 } 873 874 page_pool_return_page(pool, netmem); 875 return 0; 876 } 877 878 static void page_pool_free_frag(struct page_pool *pool) 879 { 880 long drain_count = BIAS_MAX - pool->frag_users; 881 netmem_ref netmem = pool->frag_page; 882 883 pool->frag_page = 0; 884 885 if (!netmem || page_pool_unref_netmem(netmem, drain_count)) 886 return; 887 888 page_pool_return_page(pool, netmem); 889 } 890 891 netmem_ref page_pool_alloc_frag_netmem(struct page_pool *pool, 892 unsigned int *offset, unsigned int size, 893 gfp_t gfp) 894 { 895 unsigned int max_size = PAGE_SIZE << pool->p.order; 896 netmem_ref netmem = pool->frag_page; 897 898 if (WARN_ON(size > max_size)) 899 return 0; 900 901 size = ALIGN(size, dma_get_cache_alignment()); 902 *offset = pool->frag_offset; 903 904 if (netmem && *offset + size > max_size) { 905 netmem = page_pool_drain_frag(pool, netmem); 906 if (netmem) { 907 alloc_stat_inc(pool, fast); 908 goto frag_reset; 909 } 910 } 911 912 if (!netmem) { 913 netmem = page_pool_alloc_netmem(pool, gfp); 914 if (unlikely(!netmem)) { 915 pool->frag_page = 0; 916 return 0; 917 } 918 919 pool->frag_page = netmem; 920 921 frag_reset: 922 pool->frag_users = 1; 923 *offset = 0; 924 pool->frag_offset = size; 925 page_pool_fragment_netmem(netmem, BIAS_MAX); 926 return netmem; 927 } 928 929 pool->frag_users++; 930 pool->frag_offset = *offset + size; 931 alloc_stat_inc(pool, fast); 932 return netmem; 933 } 934 EXPORT_SYMBOL(page_pool_alloc_frag_netmem); 935 936 struct page *page_pool_alloc_frag(struct page_pool *pool, unsigned int *offset, 937 unsigned int size, gfp_t gfp) 938 { 939 return netmem_to_page(page_pool_alloc_frag_netmem(pool, offset, size, 940 gfp)); 941 } 942 EXPORT_SYMBOL(page_pool_alloc_frag); 943 944 static void page_pool_empty_ring(struct page_pool *pool) 945 { 946 netmem_ref netmem; 947 948 /* Empty recycle ring */ 949 while ((netmem = (__force netmem_ref)ptr_ring_consume_bh(&pool->ring))) { 950 /* Verify the refcnt invariant of cached pages */ 951 if (!(page_ref_count(netmem_to_page(netmem)) == 1)) 952 pr_crit("%s() page_pool refcnt %d violation\n", 953 __func__, netmem_ref_count(netmem)); 954 955 page_pool_return_page(pool, netmem); 956 } 957 } 958 959 static void __page_pool_destroy(struct page_pool *pool) 960 { 961 if (pool->disconnect) 962 pool->disconnect(pool); 963 964 page_pool_unlist(pool); 965 page_pool_uninit(pool); 966 kfree(pool); 967 } 968 969 static void page_pool_empty_alloc_cache_once(struct page_pool *pool) 970 { 971 netmem_ref netmem; 972 973 if (pool->destroy_cnt) 974 return; 975 976 /* Empty alloc cache, assume caller made sure this is 977 * no-longer in use, and page_pool_alloc_pages() cannot be 978 * call concurrently. 979 */ 980 while (pool->alloc.count) { 981 netmem = pool->alloc.cache[--pool->alloc.count]; 982 page_pool_return_page(pool, netmem); 983 } 984 } 985 986 static void page_pool_scrub(struct page_pool *pool) 987 { 988 page_pool_empty_alloc_cache_once(pool); 989 pool->destroy_cnt++; 990 991 /* No more consumers should exist, but producers could still 992 * be in-flight. 993 */ 994 page_pool_empty_ring(pool); 995 } 996 997 static int page_pool_release(struct page_pool *pool) 998 { 999 int inflight; 1000 1001 page_pool_scrub(pool); 1002 inflight = page_pool_inflight(pool, true); 1003 if (!inflight) 1004 __page_pool_destroy(pool); 1005 1006 return inflight; 1007 } 1008 1009 static void page_pool_release_retry(struct work_struct *wq) 1010 { 1011 struct delayed_work *dwq = to_delayed_work(wq); 1012 struct page_pool *pool = container_of(dwq, typeof(*pool), release_dw); 1013 void *netdev; 1014 int inflight; 1015 1016 inflight = page_pool_release(pool); 1017 if (!inflight) 1018 return; 1019 1020 /* Periodic warning for page pools the user can't see */ 1021 netdev = READ_ONCE(pool->slow.netdev); 1022 if (time_after_eq(jiffies, pool->defer_warn) && 1023 (!netdev || netdev == NET_PTR_POISON)) { 1024 int sec = (s32)((u32)jiffies - (u32)pool->defer_start) / HZ; 1025 1026 pr_warn("%s() stalled pool shutdown: id %u, %d inflight %d sec\n", 1027 __func__, pool->user.id, inflight, sec); 1028 pool->defer_warn = jiffies + DEFER_WARN_INTERVAL; 1029 } 1030 1031 /* Still not ready to be disconnected, retry later */ 1032 schedule_delayed_work(&pool->release_dw, DEFER_TIME); 1033 } 1034 1035 void page_pool_use_xdp_mem(struct page_pool *pool, void (*disconnect)(void *), 1036 const struct xdp_mem_info *mem) 1037 { 1038 refcount_inc(&pool->user_cnt); 1039 pool->disconnect = disconnect; 1040 pool->xdp_mem_id = mem->id; 1041 } 1042 1043 void page_pool_disable_direct_recycling(struct page_pool *pool) 1044 { 1045 /* Disable direct recycling based on pool->cpuid. 1046 * Paired with READ_ONCE() in page_pool_napi_local(). 1047 */ 1048 WRITE_ONCE(pool->cpuid, -1); 1049 1050 if (!pool->p.napi) 1051 return; 1052 1053 /* To avoid races with recycling and additional barriers make sure 1054 * pool and NAPI are unlinked when NAPI is disabled. 1055 */ 1056 WARN_ON(!test_bit(NAPI_STATE_SCHED, &pool->p.napi->state)); 1057 WARN_ON(READ_ONCE(pool->p.napi->list_owner) != -1); 1058 1059 WRITE_ONCE(pool->p.napi, NULL); 1060 } 1061 EXPORT_SYMBOL(page_pool_disable_direct_recycling); 1062 1063 void page_pool_destroy(struct page_pool *pool) 1064 { 1065 if (!pool) 1066 return; 1067 1068 if (!page_pool_put(pool)) 1069 return; 1070 1071 page_pool_disable_direct_recycling(pool); 1072 page_pool_free_frag(pool); 1073 1074 if (!page_pool_release(pool)) 1075 return; 1076 1077 page_pool_detached(pool); 1078 pool->defer_start = jiffies; 1079 pool->defer_warn = jiffies + DEFER_WARN_INTERVAL; 1080 1081 INIT_DELAYED_WORK(&pool->release_dw, page_pool_release_retry); 1082 schedule_delayed_work(&pool->release_dw, DEFER_TIME); 1083 } 1084 EXPORT_SYMBOL(page_pool_destroy); 1085 1086 /* Caller must provide appropriate safe context, e.g. NAPI. */ 1087 void page_pool_update_nid(struct page_pool *pool, int new_nid) 1088 { 1089 netmem_ref netmem; 1090 1091 trace_page_pool_update_nid(pool, new_nid); 1092 pool->p.nid = new_nid; 1093 1094 /* Flush pool alloc cache, as refill will check NUMA node */ 1095 while (pool->alloc.count) { 1096 netmem = pool->alloc.cache[--pool->alloc.count]; 1097 page_pool_return_page(pool, netmem); 1098 } 1099 } 1100 EXPORT_SYMBOL(page_pool_update_nid); 1101