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