page_pool.c 31 KB

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